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