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