1 /*
   2  * Copyright (c) 2014, 2016, 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
  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 
  42 public class TestGCLogMessages {
  43 
  44     private enum Level {
  45         OFF(""),
  46         INFO("info"),
  47         DEBUG("debug"),
  48         TRACE("trace");
  49 
  50         private String logName;
  51 
  52         Level(String logName) {
  53             this.logName = logName;
  54         }
  55 
  56         public boolean lessThan(Level other) {
  57             return this.compareTo(other) < 0;
  58         }
  59 
  60         public String toString() {
  61             return logName;
  62         }
  63     }
  64 
  65     private class LogMessageWithLevel {
  66         String message;
  67         Level level;
  68 
  69         public LogMessageWithLevel(String message, Level level) {
  70             this.message = message;
  71             this.level = level;
  72         }
  73     };
  74 
  75     private LogMessageWithLevel allLogMessages[] = new LogMessageWithLevel[] {
  76         new LogMessageWithLevel("Pre Evacuate Collection Set", Level.INFO),
  77         new LogMessageWithLevel("Evacuate Collection Set", Level.INFO),
  78         new LogMessageWithLevel("Post Evacuate Collection Set", Level.INFO),
  79         new LogMessageWithLevel("Other", Level.INFO),
  80 
  81         // Update RS
  82         new LogMessageWithLevel("Scan HCC", Level.TRACE),
  83         // Ext Root Scan
  84         new LogMessageWithLevel("Thread Roots", Level.TRACE),
  85         new LogMessageWithLevel("StringTable Roots", Level.TRACE),
  86         new LogMessageWithLevel("Universe Roots", Level.TRACE),
  87         new LogMessageWithLevel("JNI Handles Roots", Level.TRACE),
  88         new LogMessageWithLevel("ObjectSynchronizer Roots", Level.TRACE),
  89         new LogMessageWithLevel("FlatProfiler Roots", Level.TRACE),
  90         new LogMessageWithLevel("Management Roots", Level.TRACE),
  91         new LogMessageWithLevel("SystemDictionary Roots", Level.TRACE),
  92         new LogMessageWithLevel("CLDG Roots", Level.TRACE),
  93         new LogMessageWithLevel("JVMTI Roots", Level.TRACE),
  94         new LogMessageWithLevel("SATB Filtering", Level.TRACE),
  95         new LogMessageWithLevel("CM RefProcessor Roots", Level.TRACE),
  96         new LogMessageWithLevel("Wait For Strong CLD", Level.TRACE),
  97         new LogMessageWithLevel("Weak CLD Roots", Level.TRACE),
  98         // Redirty Cards
  99         new LogMessageWithLevel("Redirty Cards", Level.DEBUG),
 100         new LogMessageWithLevel("Parallel Redirty", Level.TRACE),
 101         new LogMessageWithLevel("Redirtied Cards", Level.TRACE),
 102         // Misc Top-level
 103         new LogMessageWithLevel("Code Roots Purge", Level.DEBUG),
 104         new LogMessageWithLevel("String Dedup Fixup", Level.DEBUG),
 105         new LogMessageWithLevel("Expand Heap After Collection", Level.DEBUG),
 106         // Free CSet
 107         new LogMessageWithLevel("Free Collection Set", Level.DEBUG),
 108         new LogMessageWithLevel("Free Collection Set Serial", Level.TRACE),
 109         new LogMessageWithLevel("Young Free Collection Set", Level.TRACE),
 110         new LogMessageWithLevel("Non-Young Free Collection Set", Level.TRACE),
 111         // Humongous Eager Reclaim
 112         new LogMessageWithLevel("Humongous Reclaim", Level.DEBUG),
 113         new LogMessageWithLevel("Humongous Register", Level.DEBUG),
 114         // Preserve CM Referents
 115         new LogMessageWithLevel("Preserve CM Refs", Level.DEBUG),
 116         // Merge PSS
 117         new LogMessageWithLevel("Merge Per-Thread State", Level.DEBUG),
 118     };
 119 
 120     void checkMessagesAtLevel(OutputAnalyzer output, LogMessageWithLevel messages[], Level level) throws Exception {
 121         for (LogMessageWithLevel l : messages) {
 122             if (level.lessThan(l.level)) {
 123                 output.shouldNotContain(l.message);
 124             } else {
 125                 output.shouldMatch("\\[" + l.level + ".*" + l.message);
 126             }
 127         }
 128     }
 129 
 130     public static void main(String[] args) throws Exception {
 131         new TestGCLogMessages().testNormalLogs();
 132         new TestGCLogMessages().testWithToSpaceExhaustionLogs();
 133         new TestGCLogMessages().testWithInitialMark();
 134     }
 135 
 136     private void testNormalLogs() throws Exception {
 137 
 138         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 139                                                                   "-Xmx10M",
 140                                                                   GCTest.class.getName());
 141 
 142         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 143         checkMessagesAtLevel(output, allLogMessages, Level.OFF);
 144         output.shouldHaveExitValue(0);
 145 
 146         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 147                                                    "-XX:+UseStringDeduplication",
 148                                                    "-Xmx10M",
 149                                                    "-Xlog:gc+phases=debug",
 150                                                    GCTest.class.getName());
 151 
 152         output = new OutputAnalyzer(pb.start());
 153         checkMessagesAtLevel(output, allLogMessages, Level.DEBUG);
 154 
 155         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 156                                                    "-XX:+UseStringDeduplication",
 157                                                    "-Xmx10M",
 158                                                    "-Xlog:gc+phases=trace",
 159                                                    GCTest.class.getName());
 160 
 161         output = new OutputAnalyzer(pb.start());
 162         checkMessagesAtLevel(output, allLogMessages, Level.TRACE);
 163         output.shouldHaveExitValue(0);
 164     }
 165 
 166     LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] {
 167         new LogMessageWithLevel("Evacuation Failure", Level.DEBUG),
 168         new LogMessageWithLevel("Recalculate Used", Level.TRACE),
 169         new LogMessageWithLevel("Remove Self Forwards", Level.TRACE),
 170         new LogMessageWithLevel("Restore RemSet", Level.TRACE),
 171     };
 172 
 173     private void testWithToSpaceExhaustionLogs() throws Exception {
 174         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 175                                                                   "-Xmx32M",
 176                                                                   "-Xmn16M",
 177                                                                   "-Xlog:gc+phases=debug",
 178                                                                   GCTestWithToSpaceExhaustion.class.getName());
 179 
 180         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 181         checkMessagesAtLevel(output, exhFailureMessages, Level.DEBUG);
 182         output.shouldHaveExitValue(0);
 183 
 184         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 185                                                    "-Xmx32M",
 186                                                    "-Xmn16M",
 187                                                    "-Xlog:gc+phases=trace",
 188                                                    GCTestWithToSpaceExhaustion.class.getName());
 189 
 190         output = new OutputAnalyzer(pb.start());
 191         checkMessagesAtLevel(output, exhFailureMessages, Level.TRACE);
 192         output.shouldHaveExitValue(0);
 193     }
 194 
 195     private void testWithInitialMark() throws Exception {
 196         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 197                                                                   "-Xmx10M",
 198                                                                   "-Xbootclasspath/a:.",
 199                                                                   "-Xlog:gc*=debug",
 200                                                                   "-XX:+UnlockDiagnosticVMOptions",
 201                                                                   "-XX:+WhiteBoxAPI",
 202                                                                   GCTestWithInitialMark.class.getName());
 203 
 204         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 205         output.shouldContain("Clear Claimed Marks");
 206         output.shouldHaveExitValue(0);
 207     }
 208 
 209     static class GCTest {
 210         private static byte[] garbage;
 211         public static void main(String [] args) {
 212             System.out.println("Creating garbage");
 213             // create 128MB of garbage. This should result in at least one GC
 214             for (int i = 0; i < 1024; i++) {
 215                 garbage = new byte[128 * 1024];
 216             }
 217             System.out.println("Done");
 218         }
 219     }
 220 
 221     static class GCTestWithToSpaceExhaustion {
 222         private static byte[] garbage;
 223         private static byte[] largeObject;
 224         public static void main(String [] args) {
 225             largeObject = new byte[16*1024*1024];
 226             System.out.println("Creating garbage");
 227             // create 128MB of garbage. This should result in at least one GC,
 228             // some of them with to-space exhaustion.
 229             for (int i = 0; i < 1024; i++) {
 230                 garbage = new byte[128 * 1024];
 231             }
 232             System.out.println("Done");
 233         }
 234     }
 235 
 236     static class GCTestWithInitialMark {
 237         public static void main(String [] args) {
 238             sun.hotspot.WhiteBox WB = sun.hotspot.WhiteBox.getWhiteBox();
 239             WB.g1StartConcMarkCycle();
 240         }
 241     }
 242 
 243 }
 244