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
  27  * @summary Ensure the output for a minor GC with G1
  28  * includes the expected necessary messages.
  29  * @key gc
  30  * @library /test/lib
  31  * @modules java.base/jdk.internal.misc
  32  *          java.management
  33  */
  34 
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.process.ProcessTools;
  37 
  38 public class TestGCLogMessages {
  39 
  40     private enum Level {
  41         OFF(""),
  42         INFO("info"),
  43         DEBUG("debug"),
  44         TRACE("trace");
  45 
  46         private String logName;
  47 
  48         Level(String logName) {
  49             this.logName = logName;
  50         }
  51 
  52         public boolean lessThan(Level other) {
  53             return this.compareTo(other) < 0;
  54         }
  55 
  56         public String toString() {
  57             return logName;
  58         }
  59     }
  60 
  61     private class LogMessageWithLevel {
  62         String message;
  63         Level level;
  64 
  65         public LogMessageWithLevel(String message, Level level) {
  66             this.message = message;
  67             this.level = level;
  68         }
  69     };
  70 
  71     private LogMessageWithLevel allLogMessages[] = new LogMessageWithLevel[] {
  72         // Update RS
  73         new LogMessageWithLevel("Scan HCC", Level.TRACE),
  74         // Ext Root Scan
  75         new LogMessageWithLevel("Thread Roots", Level.TRACE),
  76         new LogMessageWithLevel("StringTable Roots", Level.TRACE),
  77         new LogMessageWithLevel("Universe Roots", Level.TRACE),
  78         new LogMessageWithLevel("JNI Handles Roots", Level.TRACE),
  79         new LogMessageWithLevel("ObjectSynchronizer Roots", Level.TRACE),
  80         new LogMessageWithLevel("FlatProfiler Roots", Level.TRACE),
  81         new LogMessageWithLevel("Management Roots", Level.TRACE),
  82         new LogMessageWithLevel("SystemDictionary Roots", Level.TRACE),
  83         new LogMessageWithLevel("CLDG Roots", Level.TRACE),
  84         new LogMessageWithLevel("JVMTI Roots", Level.TRACE),
  85         new LogMessageWithLevel("SATB Filtering", Level.TRACE),
  86         new LogMessageWithLevel("CM RefProcessor Roots", Level.TRACE),
  87         new LogMessageWithLevel("Wait For Strong CLD", Level.TRACE),
  88         new LogMessageWithLevel("Weak CLD Roots", Level.TRACE),
  89         // Redirty Cards
  90         new LogMessageWithLevel("Redirty Cards", Level.DEBUG),
  91         new LogMessageWithLevel("Parallel Redirty", Level.TRACE),
  92         new LogMessageWithLevel("Redirtied Cards", Level.TRACE),
  93         // Misc Top-level
  94         new LogMessageWithLevel("Code Roots Purge", Level.DEBUG),
  95         new LogMessageWithLevel("String Dedup Fixup", Level.INFO),
  96         new LogMessageWithLevel("Expand Heap After Collection", Level.INFO),
  97         // Free CSet
  98         new LogMessageWithLevel("Free Collection Set", Level.INFO),
  99         new LogMessageWithLevel("Free Collection Set Serial", Level.DEBUG),
 100         new LogMessageWithLevel("Young Free Collection Set", Level.DEBUG),
 101         new LogMessageWithLevel("Non-Young Free Collection Set", Level.DEBUG),
 102         // Humongous Eager Reclaim
 103         new LogMessageWithLevel("Humongous Reclaim", Level.DEBUG),
 104         new LogMessageWithLevel("Humongous Register", Level.DEBUG),
 105         // Preserve CM Referents
 106         new LogMessageWithLevel("Preserve CM Refs", Level.DEBUG),
 107         // Merge PSS
 108         new LogMessageWithLevel("Merge Per-Thread State", Level.INFO),
 109     };
 110 
 111     void checkMessagesAtLevel(OutputAnalyzer output, LogMessageWithLevel messages[], Level level) throws Exception {
 112         for (LogMessageWithLevel l : messages) {
 113             if (level.lessThan(l.level)) {
 114                 output.shouldNotContain(l.message);
 115             } else {
 116                 output.shouldMatch("\\[" + l.level + ".*" + l.message);
 117             }
 118         }
 119     }
 120 
 121     public static void main(String[] args) throws Exception {
 122         new TestGCLogMessages().testNormalLogs();
 123         new TestGCLogMessages().testWithToSpaceExhaustionLogs();
 124     }
 125 
 126     private void testNormalLogs() throws Exception {
 127 
 128         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 129                                                                   "-Xmx10M",
 130                                                                   GCTest.class.getName());
 131 
 132         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 133         checkMessagesAtLevel(output, allLogMessages, Level.OFF);
 134         output.shouldHaveExitValue(0);
 135 
 136         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 137                                                    "-XX:+UseStringDeduplication",
 138                                                    "-Xmx10M",
 139                                                    "-Xlog:gc+phases=debug",
 140                                                    GCTest.class.getName());
 141 
 142         output = new OutputAnalyzer(pb.start());
 143         checkMessagesAtLevel(output, allLogMessages, Level.DEBUG);
 144 
 145         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 146                                                    "-XX:+UseStringDeduplication",
 147                                                    "-Xmx10M",
 148                                                    "-Xlog:gc+phases=trace",
 149                                                    GCTest.class.getName());
 150 
 151         output = new OutputAnalyzer(pb.start());
 152         checkMessagesAtLevel(output, allLogMessages, Level.TRACE);
 153         output.shouldHaveExitValue(0);
 154     }
 155 
 156     LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] {
 157         new LogMessageWithLevel("Evacuation Failure", Level.DEBUG),
 158         new LogMessageWithLevel("Recalculate Used", Level.TRACE),
 159         new LogMessageWithLevel("Remove Self Forwards", Level.TRACE),
 160         new LogMessageWithLevel("Restore RemSet", Level.TRACE),
 161     };
 162 
 163     private void testWithToSpaceExhaustionLogs() throws Exception {
 164         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 165                                                                   "-Xmx32M",
 166                                                                   "-Xmn16M",
 167                                                                   "-Xlog:gc+phases=debug",
 168                                                                   GCTestWithToSpaceExhaustion.class.getName());
 169 
 170         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 171         checkMessagesAtLevel(output, exhFailureMessages, Level.DEBUG);
 172         output.shouldHaveExitValue(0);
 173 
 174         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 175                                                    "-Xmx32M",
 176                                                    "-Xmn16M",
 177                                                    "-Xlog:gc+phases=trace",
 178                                                    GCTestWithToSpaceExhaustion.class.getName());
 179 
 180         output = new OutputAnalyzer(pb.start());
 181         checkMessagesAtLevel(output, exhFailureMessages, Level.TRACE);
 182         output.shouldHaveExitValue(0);
 183     }
 184 
 185     static class GCTest {
 186         private static byte[] garbage;
 187         public static void main(String [] args) {
 188             System.out.println("Creating garbage");
 189             // create 128MB of garbage. This should result in at least one GC
 190             for (int i = 0; i < 1024; i++) {
 191                 garbage = new byte[128 * 1024];
 192             }
 193             System.out.println("Done");
 194         }
 195     }
 196 
 197     static class GCTestWithToSpaceExhaustion {
 198         private static byte[] garbage;
 199         private static byte[] largeObject;
 200         public static void main(String [] args) {
 201             largeObject = new byte[16*1024*1024];
 202             System.out.println("Creating garbage");
 203             // create 128MB of garbage. This should result in at least one GC,
 204             // some of them with to-space exhaustion.
 205             for (int i = 0; i < 1024; i++) {
 206                 garbage = new byte[128 * 1024];
 207             }
 208             System.out.println("Done");
 209         }
 210     }
 211 }
 212