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