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