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