1 /* 2 * Copyright (c) 2014, 2017, 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 8076463 8150630 8160055 8177059 27 * @summary Ensure the output for a minor GC with G1 28 * includes the expected necessary messages. 29 * @key gc 30 * @requires vm.gc.G1 31 * @library /test/lib 32 * @modules java.base/jdk.internal.misc 33 * java.management 34 * @build sun.hotspot.WhiteBox 35 * @run main ClassFileInstaller sun.hotspot.WhiteBox 36 * @run main TestGCLogMessages 37 */ 38 39 import jdk.test.lib.process.OutputAnalyzer; 40 import jdk.test.lib.process.ProcessTools; 41 import jdk.test.lib.Platform; 42 43 public class TestGCLogMessages { 44 45 private enum Level { 46 OFF(""), 47 INFO("info"), 48 DEBUG("debug"), 49 TRACE("trace"); 50 51 private String logName; 52 53 Level(String logName) { 54 this.logName = logName; 55 } 56 57 public boolean lessThan(Level other) { 58 return this.compareTo(other) < 0; 59 } 60 61 public String toString() { 62 return logName; 63 } 64 } 65 66 private class LogMessageWithLevel { 67 String message; 68 Level level; 69 70 public LogMessageWithLevel(String message, Level level) { 71 this.message = message; 72 this.level = level; 73 } 74 75 public boolean isAvailable() { 76 return true; 77 } 78 }; 79 80 private class LogMessageWithLevelC2OrJVMCIOnly extends LogMessageWithLevel { 81 public LogMessageWithLevelC2OrJVMCIOnly(String message, Level level) { 82 super(message, level); 83 } 84 85 public boolean isAvailable() { 86 return Platform.isGraal() || Platform.isServer(); 87 } 88 } 89 90 private LogMessageWithLevel allLogMessages[] = new LogMessageWithLevel[] { 91 new LogMessageWithLevel("Pre Evacuate Collection Set", Level.INFO), 92 new LogMessageWithLevel("Evacuate Collection Set", Level.INFO), 93 new LogMessageWithLevel("Post Evacuate Collection Set", Level.INFO), 94 new LogMessageWithLevel("Other", Level.INFO), 95 96 // Update RS 97 new LogMessageWithLevel("Update RS", Level.DEBUG), 98 new LogMessageWithLevel("Processed Buffers", Level.DEBUG), 99 new LogMessageWithLevel("Scan HCC", Level.TRACE), 100 // Scan RS 101 new LogMessageWithLevel("Scan RS", Level.DEBUG), 102 new LogMessageWithLevel("Scanned Cards", Level.DEBUG), 103 new LogMessageWithLevel("Claimed Cards", Level.DEBUG), 104 new LogMessageWithLevel("Skipped Cards", Level.DEBUG), 105 // Ext Root Scan 106 new LogMessageWithLevel("Thread Roots", Level.TRACE), 107 new LogMessageWithLevel("StringTable Roots", Level.TRACE), 108 new LogMessageWithLevel("Universe Roots", Level.TRACE), 109 new LogMessageWithLevel("JNI Handles Roots", Level.TRACE), 110 new LogMessageWithLevel("ObjectSynchronizer Roots", Level.TRACE), 111 new LogMessageWithLevel("FlatProfiler Roots", Level.TRACE), 112 new LogMessageWithLevel("Management Roots", Level.TRACE), 113 new LogMessageWithLevel("SystemDictionary Roots", Level.TRACE), 114 new LogMessageWithLevel("CLDG Roots", Level.TRACE), 115 new LogMessageWithLevel("JVMTI Roots", Level.TRACE), 116 new LogMessageWithLevel("SATB Filtering", Level.TRACE), 117 new LogMessageWithLevel("CM RefProcessor Roots", Level.TRACE), 118 new LogMessageWithLevel("Wait For Strong CLD", Level.TRACE), 119 new LogMessageWithLevel("Weak CLD Roots", Level.TRACE), 120 // Redirty Cards 121 new LogMessageWithLevel("Redirty Cards", Level.DEBUG), 122 new LogMessageWithLevel("Parallel Redirty", Level.TRACE), 123 new LogMessageWithLevel("Redirtied Cards", Level.TRACE), 124 // Misc Top-level 125 new LogMessageWithLevel("Code Roots Purge", Level.DEBUG), 126 new LogMessageWithLevel("String Dedup Fixup", Level.DEBUG), 127 new LogMessageWithLevel("Expand Heap After Collection", Level.DEBUG), 128 // Free CSet 129 new LogMessageWithLevel("Free Collection Set", Level.DEBUG), 130 new LogMessageWithLevel("Free Collection Set Serial", Level.TRACE), 131 new LogMessageWithLevel("Young Free Collection Set", Level.TRACE), 132 new LogMessageWithLevel("Non-Young Free Collection Set", Level.TRACE), 133 // Humongous Eager Reclaim 134 new LogMessageWithLevel("Humongous Reclaim", Level.DEBUG), 135 new LogMessageWithLevel("Humongous Register", Level.DEBUG), 136 // Preserve CM Referents 137 new LogMessageWithLevel("Preserve CM Refs", Level.DEBUG), 138 // Merge PSS 139 new LogMessageWithLevel("Merge Per-Thread State", Level.DEBUG), 140 // TLAB handling 141 new LogMessageWithLevel("Prepare TLABs", Level.DEBUG), 142 new LogMessageWithLevel("Resize TLABs", Level.DEBUG), 143 144 new LogMessageWithLevelC2OrJVMCIOnly("DerivedPointerTable Update", Level.DEBUG), 145 new LogMessageWithLevel("Start New Collection Set", Level.DEBUG), 146 }; 147 148 void checkMessagesAtLevel(OutputAnalyzer output, LogMessageWithLevel messages[], Level level) throws Exception { 149 for (LogMessageWithLevel l : messages) { 150 if (level.lessThan(l.level) || !l.isAvailable()) { 151 output.shouldNotContain(l.message); 152 } else { 153 output.shouldMatch("\\[" + l.level + ".*" + l.message); 154 } 155 } 156 } 157 158 public static void main(String[] args) throws Exception { 159 new TestGCLogMessages().testNormalLogs(); 160 new TestGCLogMessages().testWithToSpaceExhaustionLogs(); 161 new TestGCLogMessages().testWithInitialMark(); 162 } 163 164 private void testNormalLogs() throws Exception { 165 166 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 167 "-Xmx10M", 168 GCTest.class.getName()); 169 170 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 171 checkMessagesAtLevel(output, allLogMessages, Level.OFF); 172 output.shouldHaveExitValue(0); 173 174 pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 175 "-XX:+UseStringDeduplication", 176 "-Xmx10M", 177 "-Xlog:gc+phases=debug", 178 GCTest.class.getName()); 179 180 output = new OutputAnalyzer(pb.start()); 181 checkMessagesAtLevel(output, allLogMessages, Level.DEBUG); 182 183 pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 184 "-XX:+UseStringDeduplication", 185 "-Xmx10M", 186 "-Xlog:gc+phases=trace", 187 GCTest.class.getName()); 188 189 output = new OutputAnalyzer(pb.start()); 190 checkMessagesAtLevel(output, allLogMessages, Level.TRACE); 191 output.shouldHaveExitValue(0); 192 } 193 194 LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] { 195 new LogMessageWithLevel("Evacuation Failure", Level.DEBUG), 196 new LogMessageWithLevel("Recalculate Used", Level.TRACE), 197 new LogMessageWithLevel("Remove Self Forwards", Level.TRACE), 198 new LogMessageWithLevel("Restore RemSet", Level.TRACE), 199 }; 200 201 private void testWithToSpaceExhaustionLogs() throws Exception { 202 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 203 "-Xmx32M", 204 "-Xmn16M", 205 "-Xlog:gc+phases=debug", 206 GCTestWithToSpaceExhaustion.class.getName()); 207 208 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 209 checkMessagesAtLevel(output, exhFailureMessages, Level.DEBUG); 210 output.shouldHaveExitValue(0); 211 212 pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 213 "-Xmx32M", 214 "-Xmn16M", 215 "-Xlog:gc+phases=trace", 216 GCTestWithToSpaceExhaustion.class.getName()); 217 218 output = new OutputAnalyzer(pb.start()); 219 checkMessagesAtLevel(output, exhFailureMessages, Level.TRACE); 220 output.shouldHaveExitValue(0); 221 } 222 223 private void testWithInitialMark() throws Exception { 224 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC", 225 "-Xmx10M", 226 "-Xbootclasspath/a:.", 227 "-Xlog:gc*=debug", 228 "-XX:+UnlockDiagnosticVMOptions", 229 "-XX:+WhiteBoxAPI", 230 GCTestWithInitialMark.class.getName()); 231 232 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 233 output.shouldContain("Clear Claimed Marks"); 234 output.shouldHaveExitValue(0); 235 } 236 237 static class GCTest { 238 private static byte[] garbage; 239 public static void main(String [] args) { 240 System.out.println("Creating garbage"); 241 // create 128MB of garbage. This should result in at least one GC 242 for (int i = 0; i < 1024; i++) { 243 garbage = new byte[128 * 1024]; 244 } 245 System.out.println("Done"); 246 } 247 } 248 249 static class GCTestWithToSpaceExhaustion { 250 private static byte[] garbage; 251 private static byte[] largeObject; 252 public static void main(String [] args) { 253 largeObject = new byte[16*1024*1024]; 254 System.out.println("Creating garbage"); 255 // create 128MB of garbage. This should result in at least one GC, 256 // some of them with to-space exhaustion. 257 for (int i = 0; i < 1024; i++) { 258 garbage = new byte[128 * 1024]; 259 } 260 System.out.println("Done"); 261 } 262 } 263 264 static class GCTestWithInitialMark { 265 public static void main(String [] args) { 266 sun.hotspot.WhiteBox WB = sun.hotspot.WhiteBox.getWhiteBox(); 267 WB.g1StartConcMarkCycle(); 268 } 269 } 270 271 } 272