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         // Merge Heap Roots
  99         new LogMessageWithLevel("Merge Heap Roots", Level.INFO),
 100         new LogMessageWithLevel("Prepare Merge Heap Roots", Level.DEBUG),
 101         new LogMessageWithLevel("Eager Reclaim", Level.DEBUG),
 102         new LogMessageWithLevel("Remembered Sets", Level.DEBUG),
 103         new LogMessageWithLevel("Merged Sparse", Level.DEBUG),
 104         new LogMessageWithLevel("Merged Fine", Level.DEBUG),
 105         new LogMessageWithLevel("Merged Coarse", Level.DEBUG),
 106         new LogMessageWithLevel("Hot Card Cache", Level.DEBUG),
 107         new LogMessageWithLevel("Log Buffers", Level.DEBUG),
 108         new LogMessageWithLevel("Dirty Cards", Level.DEBUG),
 109         new LogMessageWithLevel("Skipped Cards", Level.DEBUG),
 110         // Scan Heap Roots
 111         new LogMessageWithLevel("Scan Heap Roots", Level.DEBUG),
 112         new LogMessageWithLevel("Scanned Cards", Level.DEBUG),
 113         new LogMessageWithLevel("Scanned Blocks", Level.DEBUG),
 114         new LogMessageWithLevel("Claimed Chunks", Level.DEBUG),
 115         // Code Roots Scan
 116         new LogMessageWithLevel("Code Root Scan", Level.DEBUG),
 117         // Object Copy
 118         new LogMessageWithLevel("Object Copy", Level.DEBUG),
 119         new LogMessageWithLevel("LAB Waste", Level.DEBUG),
 120         new LogMessageWithLevel("LAB Undo Waste", Level.DEBUG),
 121         // Ext Root Scan
 122         new LogMessageWithLevel("Thread Roots", Level.TRACE),
 123         new LogMessageWithLevel("Universe Roots", Level.TRACE),
 124         new LogMessageWithLevel("JNI Handles Roots", Level.TRACE),
 125         new LogMessageWithLevel("ObjectSynchronizer Roots", Level.TRACE),
 126         new LogMessageWithLevel("Management Roots", Level.TRACE),
 127         new LogMessageWithLevel("SystemDictionary Roots", Level.TRACE),
 128         new LogMessageWithLevel("CLDG Roots", Level.TRACE),
 129         new LogMessageWithLevel("JVMTI Roots", Level.TRACE),
 130         new LogMessageWithLevel("CM RefProcessor Roots", Level.TRACE),
 131         new LogMessageWithLevel("Wait For Strong CLD", Level.TRACE),
 132         new LogMessageWithLevel("Weak CLD Roots", Level.TRACE),
 133         // Redirty Cards
 134         new LogMessageWithLevel("Redirty Cards", Level.DEBUG),
 135         new LogMessageWithLevel("Parallel Redirty", Level.TRACE),
 136         new LogMessageWithLevel("Redirtied Cards", Level.TRACE),
 137         // Misc Top-level
 138         new LogMessageWithLevel("Code Roots Purge", Level.DEBUG),
 139         new LogMessageWithLevel("String Deduplication", Level.DEBUG),
 140         new LogMessageWithLevel("Queue Fixup", Level.DEBUG),
 141         new LogMessageWithLevel("Table Fixup", Level.DEBUG),
 142         new LogMessageWithLevel("Expand Heap After Collection", Level.DEBUG),
 143         new LogMessageWithLevel("Region Register", Level.DEBUG),
 144         new LogMessageWithLevel("Prepare Heap Roots", Level.DEBUG),
 145         // Free CSet
 146         new LogMessageWithLevel("Free Collection Set", Level.DEBUG),
 147         new LogMessageWithLevel("Free Collection Set Serial", Level.TRACE),
 148         new LogMessageWithLevel("Young Free Collection Set", Level.TRACE),
 149         new LogMessageWithLevel("Non-Young Free Collection Set", Level.TRACE),
 150         // Humongous Eager Reclaim
 151         new LogMessageWithLevel("Humongous Reclaim", Level.DEBUG),
 152         // Merge PSS
 153         new LogMessageWithLevel("Merge Per-Thread State", Level.DEBUG),
 154         // TLAB handling
 155         new LogMessageWithLevel("Prepare TLABs", Level.DEBUG),
 156         new LogMessageWithLevel("Resize TLABs", Level.DEBUG),
 157         // Reference Processing
 158         new LogMessageWithLevel("Reference Processing", Level.DEBUG),
 159         // VM internal reference processing
 160         new LogMessageWithLevel("Weak Processing", Level.DEBUG),
 161         new LogMessageWithLevel("JNI weak", Level.DEBUG),
 162         new LogMessageWithLevel("StringTable weak", Level.DEBUG),
 163         new LogMessageWithLevel("ResolvedMethodTable weak", Level.DEBUG),
 164         new LogMessageWithLevel("VM weak", Level.DEBUG),
 165 
 166         new LogMessageWithLevelC2OrJVMCIOnly("DerivedPointerTable Update", Level.DEBUG),
 167         new LogMessageWithLevel("Start New Collection Set", Level.DEBUG),
 168     };
 169 
 170     void checkMessagesAtLevel(OutputAnalyzer output, LogMessageWithLevel messages[], Level level) throws Exception {
 171         for (LogMessageWithLevel l : messages) {
 172             if (level.lessThan(l.level) || !l.isAvailable()) {
 173                 output.shouldNotContain(l.message);
 174             } else {
 175                 output.shouldMatch("\\[" + l.level + ".*" + l.message);
 176             }
 177         }
 178     }
 179 
 180     public static void main(String[] args) throws Exception {
 181         new TestGCLogMessages().testNormalLogs();
 182         new TestGCLogMessages().testWithToSpaceExhaustionLogs();
 183         new TestGCLogMessages().testWithInitialMark();
 184         new TestGCLogMessages().testExpandHeap();
 185     }
 186 
 187     private void testNormalLogs() throws Exception {
 188 
 189         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 190                                                                   "-Xmx10M",
 191                                                                   GCTest.class.getName());
 192 
 193         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 194         checkMessagesAtLevel(output, allLogMessages, Level.OFF);
 195         output.shouldHaveExitValue(0);
 196 
 197         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 198                                                    "-XX:+UseStringDeduplication",
 199                                                    "-Xmx10M",
 200                                                    "-Xlog:gc+phases=debug",
 201                                                    GCTest.class.getName());
 202 
 203         output = new OutputAnalyzer(pb.start());
 204         checkMessagesAtLevel(output, allLogMessages, Level.DEBUG);
 205 
 206         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 207                                                    "-XX:+UseStringDeduplication",
 208                                                    "-Xmx10M",
 209                                                    "-Xlog:gc+phases=trace",
 210                                                    GCTest.class.getName());
 211 
 212         output = new OutputAnalyzer(pb.start());
 213         checkMessagesAtLevel(output, allLogMessages, Level.TRACE);
 214         output.shouldHaveExitValue(0);
 215     }
 216 
 217     LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] {
 218         new LogMessageWithLevel("Evacuation Failure", Level.DEBUG),
 219         new LogMessageWithLevel("Recalculate Used", Level.TRACE),
 220         new LogMessageWithLevel("Remove Self Forwards", Level.TRACE),
 221     };
 222 
 223     private void testWithToSpaceExhaustionLogs() throws Exception {
 224         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 225                                                                   "-Xmx32M",
 226                                                                   "-Xmn16M",
 227                                                                   "-Xlog:gc+phases=debug",
 228                                                                   GCTestWithToSpaceExhaustion.class.getName());
 229 
 230         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 231         checkMessagesAtLevel(output, exhFailureMessages, Level.DEBUG);
 232         output.shouldHaveExitValue(0);
 233 
 234         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 235                                                    "-Xmx32M",
 236                                                    "-Xmn16M",
 237                                                    "-Xlog:gc+phases=trace",
 238                                                    GCTestWithToSpaceExhaustion.class.getName());
 239 
 240         output = new OutputAnalyzer(pb.start());
 241         checkMessagesAtLevel(output, exhFailureMessages, Level.TRACE);
 242         output.shouldHaveExitValue(0);
 243     }
 244 
 245     private void testWithInitialMark() throws Exception {
 246         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 247                                                                   "-Xmx10M",
 248                                                                   "-Xbootclasspath/a:.",
 249                                                                   "-Xlog:gc*=debug",
 250                                                                   "-XX:+UnlockDiagnosticVMOptions",
 251                                                                   "-XX:+WhiteBoxAPI",
 252                                                                   GCTestWithInitialMark.class.getName());
 253 
 254         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 255         output.shouldContain("Clear Claimed Marks");
 256         output.shouldHaveExitValue(0);
 257     }
 258 
 259     private void testExpandHeap() throws Exception {
 260         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 261                                                                   "-Xmx10M",
 262                                                                   "-Xbootclasspath/a:.",
 263                                                                   "-Xlog:gc+ergo+heap=debug",
 264                                                                   "-XX:+UnlockDiagnosticVMOptions",
 265                                                                   "-XX:+WhiteBoxAPI",
 266                                                                   GCTest.class.getName());
 267 
 268         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 269         output.shouldContain("Expand the heap. requested expansion amount: ");
 270         output.shouldContain("B expansion amount: ");
 271         output.shouldHaveExitValue(0);
 272     }
 273 
 274 
 275     static class GCTest {
 276         private static byte[] garbage;
 277         public static void main(String [] args) {
 278             System.out.println("Creating garbage");
 279             // create 128MB of garbage. This should result in at least one GC
 280             for (int i = 0; i < 1024; i++) {
 281                 garbage = new byte[128 * 1024];
 282             }
 283             System.out.println("Done");
 284         }
 285     }
 286 
 287     static class GCTestWithToSpaceExhaustion {
 288         private static byte[] garbage;
 289         private static byte[] largeObject;
 290         public static void main(String [] args) {
 291             largeObject = new byte[16*1024*1024];
 292             System.out.println("Creating garbage");
 293             // create 128MB of garbage. This should result in at least one GC,
 294             // some of them with to-space exhaustion.
 295             for (int i = 0; i < 1024; i++) {
 296                 garbage = new byte[128 * 1024];
 297             }
 298             System.out.println("Done");
 299         }
 300     }
 301 
 302     static class GCTestWithInitialMark {
 303         public static void main(String [] args) {
 304             sun.hotspot.WhiteBox WB = sun.hotspot.WhiteBox.getWhiteBox();
 305             WB.g1StartConcMarkCycle();
 306         }
 307     }
 308 
 309 }
 310