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