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 /testlibrary
  31  * @modules java.base/jdk.internal.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(""),
  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("Young Free Collection Set", Level.DEBUG),
  99         new LogMessageWithLevel("Non-Young Free Collection Set", Level.DEBUG),
 100         // Humongous Eager Reclaim
 101         new LogMessageWithLevel("Humongous Reclaim", Level.DEBUG),
 102         new LogMessageWithLevel("Humongous Register", Level.DEBUG),
 103         // Preserve CM Referents
 104         new LogMessageWithLevel("Preserve CM Refs", Level.DEBUG),
 105         // Merge PSS
 106         new LogMessageWithLevel("Merge Per-Thread State", Level.INFO),
 107     };
 108 
 109     void checkMessagesAtLevel(OutputAnalyzer output, LogMessageWithLevel messages[], Level level) throws Exception {
 110         for (LogMessageWithLevel l : messages) {
 111             if (level.lessThan(l.level)) {
 112                 output.shouldNotContain(l.message);
 113             } else {
 114                 output.shouldMatch("\\[" + l.level + ".*" + l.message);
 115             }
 116         }
 117     }
 118 
 119     public static void main(String[] args) throws Exception {
 120         new TestGCLogMessages().testNormalLogs();
 121         new TestGCLogMessages().testWithToSpaceExhaustionLogs();
 122     }
 123 
 124     private void testNormalLogs() throws Exception {
 125 
 126         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 127                                                                   "-Xmx10M",
 128                                                                   GCTest.class.getName());
 129 
 130         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 131         checkMessagesAtLevel(output, allLogMessages, Level.OFF);
 132         output.shouldHaveExitValue(0);
 133 
 134         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 135                                                    "-XX:+UseStringDeduplication",
 136                                                    "-Xmx10M",
 137                                                    "-Xlog:gc+phases=debug",
 138                                                    GCTest.class.getName());
 139 
 140         output = new OutputAnalyzer(pb.start());
 141         checkMessagesAtLevel(output, allLogMessages, Level.DEBUG);
 142 
 143         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 144                                                    "-XX:+UseStringDeduplication",
 145                                                    "-Xmx10M",
 146                                                    "-Xlog:gc+phases=trace",
 147                                                    GCTest.class.getName());
 148 
 149         output = new OutputAnalyzer(pb.start());
 150         checkMessagesAtLevel(output, allLogMessages, Level.TRACE);
 151         output.shouldHaveExitValue(0);
 152     }
 153 
 154     LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] {
 155         new LogMessageWithLevel("Evacuation Failure", Level.DEBUG),
 156         new LogMessageWithLevel("Recalculate Used", Level.TRACE),
 157         new LogMessageWithLevel("Remove Self Forwards", Level.TRACE),
 158         new LogMessageWithLevel("Restore RemSet", Level.TRACE),
 159     };
 160 
 161     private void testWithToSpaceExhaustionLogs() throws Exception {
 162         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 163                                                                   "-Xmx32M",
 164                                                                   "-Xmn16M",
 165                                                                   "-Xlog:gc+phases=debug",
 166                                                                   GCTestWithToSpaceExhaustion.class.getName());
 167 
 168         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 169         checkMessagesAtLevel(output, exhFailureMessages, Level.DEBUG);
 170         output.shouldHaveExitValue(0);
 171 
 172         pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
 173                                                    "-Xmx32M",
 174                                                    "-Xmn16M",
 175                                                    "-Xlog:gc+phases=trace",
 176                                                    GCTestWithToSpaceExhaustion.class.getName());
 177 
 178         output = new OutputAnalyzer(pb.start());
 179         checkMessagesAtLevel(output, exhFailureMessages, Level.TRACE);
 180         output.shouldHaveExitValue(0);
 181     }
 182 
 183     static class GCTest {
 184         private static byte[] garbage;
 185         public static void main(String [] args) {
 186             System.out.println("Creating garbage");
 187             // create 128MB of garbage. This should result in at least one GC
 188             for (int i = 0; i < 1024; i++) {
 189                 garbage = new byte[128 * 1024];
 190             }
 191             System.out.println("Done");
 192         }
 193     }
 194 
 195     static class GCTestWithToSpaceExhaustion {
 196         private static byte[] garbage;
 197         private static byte[] largeObject;
 198         public static void main(String [] args) {
 199             largeObject = new byte[16*1024*1024];
 200             System.out.println("Creating garbage");
 201             // create 128MB of garbage. This should result in at least one GC,
 202             // some of them with to-space exhaustion.
 203             for (int i = 0; i < 1024; i++) {
 204                 garbage = new byte[128 * 1024];
 205             }
 206             System.out.println("Done");
 207         }
 208     }
 209 }
 210