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 that the PrintGCDetails 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, FINER, FINEST;
  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 (ms)", Level.FINER),
  60         // Ext Root Scan
  61         new LogMessageWithLevel("Thread Roots (ms)", Level.FINEST),
  62         new LogMessageWithLevel("StringTable Roots (ms)", Level.FINEST),
  63         new LogMessageWithLevel("Universe Roots (ms)", Level.FINEST),
  64         new LogMessageWithLevel("JNI Handles Roots (ms)", Level.FINEST),
  65         new LogMessageWithLevel("ObjectSynchronizer Roots (ms)", Level.FINEST),
  66         new LogMessageWithLevel("FlatProfiler Roots", Level.FINEST),
  67         new LogMessageWithLevel("Management Roots", Level.FINEST),
  68         new LogMessageWithLevel("SystemDictionary Roots", Level.FINEST),
  69         new LogMessageWithLevel("CLDG Roots", Level.FINEST),
  70         new LogMessageWithLevel("JVMTI Roots", Level.FINEST),
  71         new LogMessageWithLevel("SATB Filtering", Level.FINEST),
  72         new LogMessageWithLevel("CM RefProcessor Roots", Level.FINEST),
  73         new LogMessageWithLevel("Wait For Strong CLD", Level.FINEST),
  74         new LogMessageWithLevel("Weak CLD Roots", Level.FINEST),
  75         // Redirty Cards
  76         new LogMessageWithLevel("Redirty Cards", Level.FINER),
  77         new LogMessageWithLevel("Parallel Redirty", Level.FINEST),
  78         new LogMessageWithLevel("Redirtied Cards", Level.FINEST),
  79         // Misc Top-level
  80         new LogMessageWithLevel("Code Root Purge", Level.FINER),
  81         new LogMessageWithLevel("String Dedup Fixup", Level.FINER),
  82         new LogMessageWithLevel("Expand Heap After Collection", Level.FINER),
  83         // Free CSet
  84         new LogMessageWithLevel("Young Free CSet", Level.FINEST),
  85         new LogMessageWithLevel("Non-Young Free CSet", Level.FINEST),
  86         // Humongous Eager Reclaim
  87         new LogMessageWithLevel("Humongous Reclaim", Level.FINER),
  88         new LogMessageWithLevel("Humongous Register", Level.FINER),
  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                                                    "-XX:+PrintGCDetails",
 120                                                    GCTest.class.getName());
 121 
 122         output = new OutputAnalyzer(pb.start());
 123         checkMessagesAtLevel(output, allLogMessages, Level.FINER);
 124 
 125         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 126                                                    "-XX:+UseStringDeduplication",
 127                                                    "-Xmx10M",
 128                                                    "-XX:+PrintGCDetails",
 129                                                    "-XX:+UnlockExperimentalVMOptions",
 130                                                    "-XX:G1LogLevel=finest",
 131                                                    GCTest.class.getName());
 132 
 133         output = new OutputAnalyzer(pb.start());
 134         checkMessagesAtLevel(output, allLogMessages, Level.FINEST);
 135         output.shouldHaveExitValue(0);
 136     }
 137 
 138     LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] {
 139         new LogMessageWithLevel("Evacuation Failure", Level.FINER),
 140         new LogMessageWithLevel("Recalculate Used", Level.FINEST),
 141         new LogMessageWithLevel("Remove Self Forwards", Level.FINEST),
 142         new LogMessageWithLevel("Restore RemSet", Level.FINEST),
 143     };
 144 
 145     private void testWithToSpaceExhaustionLogs() throws Exception {
 146         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 147                                                                   "-Xmx32M",
 148                                                                   "-Xmn16M",
 149                                                                   "-XX:+PrintGCDetails",
 150                                                                   GCTestWithToSpaceExhaustion.class.getName());
 151 
 152         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 153         checkMessagesAtLevel(output, exhFailureMessages, Level.FINER);
 154         output.shouldHaveExitValue(0);
 155 
 156         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 157                                                    "-Xmx32M",
 158                                                    "-Xmn16M",
 159                                                    "-XX:+PrintGCDetails",
 160                                                    "-XX:+UnlockExperimentalVMOptions",
 161                                                    "-XX:G1LogLevel=finest",
 162                                                    GCTestWithToSpaceExhaustion.class.getName());
 163 
 164         output = new OutputAnalyzer(pb.start());
 165         checkMessagesAtLevel(output, exhFailureMessages, Level.FINEST);
 166         output.shouldHaveExitValue(0);
 167     }
 168 
 169     static class GCTest {
 170         private static byte[] garbage;
 171         public static void main(String [] args) {
 172             System.out.println("Creating garbage");
 173             // create 128MB of garbage. This should result in at least one GC
 174             for (int i = 0; i < 1024; i++) {
 175                 garbage = new byte[128 * 1024];
 176             }
 177             System.out.println("Done");
 178         }
 179     }
 180 
 181     static class GCTestWithToSpaceExhaustion {
 182         private static byte[] garbage;
 183         private static byte[] largeObject;
 184         public static void main(String [] args) {
 185             largeObject = new byte[16*1024*1024];
 186             System.out.println("Creating garbage");
 187             // create 128MB of garbage. This should result in at least one GC,
 188             // some of them with to-space exhaustion.
 189             for (int i = 0; i < 1024; i++) {
 190                 garbage = new byte[128 * 1024];
 191             }
 192             System.out.println("Done");
 193         }
 194     }
 195 }
 196