1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test TestGCLogMessages
  26  * @bug 8035406 8027295 8035398 8019342 8027959 8048179 8027962 8069330
  27  * @summary Ensure the output for a minor GC with G1
  28  * includes the expected necessary messages.
  29  * @key gc
  30  * @library /testlibrary
  31  * @modules java.base/sun.misc
  32  *          java.management
  33  */
  34 
  35 import jdk.test.lib.ProcessTools;
  36 import jdk.test.lib.OutputAnalyzer;
  37 
  38 public class TestGCLogMessages {
  39 
  40     private enum Level {
  41         OFF(""),
  42         INFO("info"),
  43         DEBUG("debug"),
  44         TRACE("trace");
  45 
  46         private String logName;
  47 
  48         Level(String logName) {
  49             this.logName = logName;
  50         }
  51 
  52         public boolean lessThan(Level other) {
  53             return this.compareTo(other) < 0;
  54         }
  55 
  56         public String toString() {
  57             return logName;
  58         }
  59     }
  60 
  61     private class LogMessageWithLevel {
  62         String message;
  63         Level level;
  64 
  65         public LogMessageWithLevel(String message, Level level) {
  66             this.message = message;
  67             this.level = level;
  68         }
  69     };
  70 
  71     private LogMessageWithLevel allLogMessages[] = new LogMessageWithLevel[] {
  72         // Update RS
  73         new LogMessageWithLevel("Scan HCC", Level.TRACE),
  74         // Ext Root Scan
  75         new LogMessageWithLevel("Thread Roots", Level.TRACE),
  76         new LogMessageWithLevel("StringTable Roots", Level.TRACE),
  77         new LogMessageWithLevel("Universe Roots", Level.TRACE),
  78         new LogMessageWithLevel("JNI Handles Roots", Level.TRACE),
  79         new LogMessageWithLevel("ObjectSynchronizer Roots", Level.TRACE),
  80         new LogMessageWithLevel("FlatProfiler Roots", Level.TRACE),
  81         new LogMessageWithLevel("Management Roots", Level.TRACE),
  82         new LogMessageWithLevel("SystemDictionary Roots", Level.TRACE),
  83         new LogMessageWithLevel("CLDG Roots", Level.TRACE),
  84         new LogMessageWithLevel("JVMTI Roots", Level.TRACE),
  85         new LogMessageWithLevel("SATB Filtering", Level.TRACE),
  86         new LogMessageWithLevel("CM RefProcessor Roots", Level.TRACE),
  87         new LogMessageWithLevel("Wait For Strong CLD", Level.TRACE),
  88         new LogMessageWithLevel("Weak CLD Roots", Level.TRACE),
  89         // Redirty Cards
  90         new LogMessageWithLevel("Redirty Cards", Level.DEBUG),
  91         new LogMessageWithLevel("Parallel Redirty", Level.TRACE),
  92         new LogMessageWithLevel("Redirtied Cards", Level.TRACE),
  93         // Misc Top-level
  94         new LogMessageWithLevel("Code Roots Purge", Level.DEBUG),
  95         new LogMessageWithLevel("String Dedup Fixup", Level.INFO),
  96         new LogMessageWithLevel("Expand Heap After Collection", Level.INFO),
  97         // Free CSet
  98         new LogMessageWithLevel("Young Free Collection Set", Level.DEBUG),
  99         new LogMessageWithLevel("Non-Young Free Collection Set", Level.DEBUG),
 100         // Humongous Eager Reclaim
 101         new LogMessageWithLevel("Humongous Reclaim", Level.DEBUG),
 102         new LogMessageWithLevel("Humongous Register", Level.DEBUG),
 103     };
 104 
 105     void checkMessagesAtLevel(OutputAnalyzer output, LogMessageWithLevel messages[], Level level) throws Exception {
 106         for (LogMessageWithLevel l : messages) {
 107             if (level.lessThan(l.level)) {
 108                 output.shouldNotContain(l.message);
 109             } else {
 110                 output.shouldMatch("\\[" + l.level + ".*" + l.message);
 111             }
 112         }
 113     }
 114 
 115     public static void main(String[] args) throws Exception {
 116         new TestGCLogMessages().testNormalLogs();
 117         new TestGCLogMessages().testWithToSpaceExhaustionLogs();
 118     }
 119 
 120     private void testNormalLogs() throws Exception {
 121 
 122         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 123                                                                   "-Xmx10M",
 124                                                                   GCTest.class.getName());
 125 
 126         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 127         checkMessagesAtLevel(output, allLogMessages, Level.OFF);
 128         output.shouldHaveExitValue(0);
 129 
 130         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 131                                                    "-XX:+UseStringDeduplication",
 132                                                    "-Xmx10M",
 133                                                    "-Xlog:gc+phases=debug",
 134                                                    GCTest.class.getName());
 135 
 136         output = new OutputAnalyzer(pb.start());
 137         checkMessagesAtLevel(output, allLogMessages, Level.DEBUG);
 138 
 139         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 140                                                    "-XX:+UseStringDeduplication",
 141                                                    "-Xmx10M",
 142                                                    "-Xlog:gc+phases=trace",
 143                                                    GCTest.class.getName());
 144 
 145         output = new OutputAnalyzer(pb.start());
 146         checkMessagesAtLevel(output, allLogMessages, Level.TRACE);
 147         output.shouldHaveExitValue(0);
 148     }
 149 
 150     LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] {
 151         new LogMessageWithLevel("Evacuation Failure", Level.DEBUG),
 152         new LogMessageWithLevel("Recalculate Used", Level.TRACE),
 153         new LogMessageWithLevel("Remove Self Forwards", Level.TRACE),
 154         new LogMessageWithLevel("Restore RemSet", Level.TRACE),
 155     };
 156 
 157     private void testWithToSpaceExhaustionLogs() throws Exception {
 158         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 159                                                                   "-Xmx32M",
 160                                                                   "-Xmn16M",
 161                                                                   "-Xlog:gc+phases=debug",
 162                                                                   GCTestWithToSpaceExhaustion.class.getName());
 163 
 164         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 165         checkMessagesAtLevel(output, exhFailureMessages, Level.DEBUG);
 166         output.shouldHaveExitValue(0);
 167 
 168         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 169                                                    "-Xmx32M",
 170                                                    "-Xmn16M",
 171                                                    "-Xlog:gc+phases=trace",
 172                                                    GCTestWithToSpaceExhaustion.class.getName());
 173 
 174         output = new OutputAnalyzer(pb.start());
 175         checkMessagesAtLevel(output, exhFailureMessages, Level.TRACE);
 176         output.shouldHaveExitValue(0);
 177     }
 178 
 179     static class GCTest {
 180         private static byte[] garbage;
 181         public static void main(String [] args) {
 182             System.out.println("Creating garbage");
 183             // create 128MB of garbage. This should result in at least one GC
 184             for (int i = 0; i < 1024; i++) {
 185                 garbage = new byte[128 * 1024];
 186             }
 187             System.out.println("Done");
 188         }
 189     }
 190 
 191     static class GCTestWithToSpaceExhaustion {
 192         private static byte[] garbage;
 193         private static byte[] largeObject;
 194         public static void main(String [] args) {
 195             largeObject = new byte[16*1024*1024];
 196             System.out.println("Creating garbage");
 197             // create 128MB of garbage. This should result in at least one GC,
 198             // some of them with to-space exhaustion.
 199             for (int i = 0; i < 1024; i++) {
 200                 garbage = new byte[128 * 1024];
 201             }
 202             System.out.println("Done");
 203         }
 204     }
 205 }
 206