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