TestGCLogMessages.java revision 9155:8b8a3e7af130
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
35import jdk.test.lib.ProcessTools;
36import jdk.test.lib.OutputAnalyzer;
37
38public 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        // Free CSet
83        new LogMessageWithLevel("Young Free CSet", Level.FINEST),
84        new LogMessageWithLevel("Non-Young Free CSet", Level.FINEST),
85        // Humongous Eager Reclaim
86        new LogMessageWithLevel("Humongous Reclaim", Level.FINER),
87        new LogMessageWithLevel("Humongous Register", Level.FINER),
88    };
89
90    void checkMessagesAtLevel(OutputAnalyzer output, LogMessageWithLevel messages[], Level level) throws Exception {
91        for (LogMessageWithLevel l : messages) {
92            if (level.lessOrEqualTo(l.level)) {
93                output.shouldNotContain(l.message);
94            } else {
95                output.shouldContain(l.message);
96            }
97        }
98    }
99
100    public static void main(String[] args) throws Exception {
101        new TestGCLogMessages().testNormalLogs();
102        new TestGCLogMessages().testWithToSpaceExhaustionLogs();
103    }
104
105    private void testNormalLogs() throws Exception {
106
107        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
108                                                                  "-Xmx10M",
109                                                                  GCTest.class.getName());
110
111        OutputAnalyzer output = new OutputAnalyzer(pb.start());
112        checkMessagesAtLevel(output, allLogMessages, Level.OFF);
113        output.shouldHaveExitValue(0);
114
115        pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
116                                                   "-XX:+UseStringDeduplication",
117                                                   "-Xmx10M",
118                                                   "-XX:+PrintGCDetails",
119                                                   GCTest.class.getName());
120
121        output = new OutputAnalyzer(pb.start());
122        checkMessagesAtLevel(output, allLogMessages, Level.FINER);
123
124        pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
125                                                   "-XX:+UseStringDeduplication",
126                                                   "-Xmx10M",
127                                                   "-XX:+PrintGCDetails",
128                                                   "-XX:+UnlockExperimentalVMOptions",
129                                                   "-XX:G1LogLevel=finest",
130                                                   GCTest.class.getName());
131
132        output = new OutputAnalyzer(pb.start());
133        checkMessagesAtLevel(output, allLogMessages, Level.FINEST);
134        output.shouldHaveExitValue(0);
135    }
136
137    LogMessageWithLevel exhFailureMessages[] = new LogMessageWithLevel[] {
138        new LogMessageWithLevel("Evacuation Failure", Level.FINER),
139        new LogMessageWithLevel("Recalculate Used", Level.FINEST),
140        new LogMessageWithLevel("Remove Self Forwards", Level.FINEST),
141        new LogMessageWithLevel("Restore RemSet", Level.FINEST),
142    };
143
144    private void testWithToSpaceExhaustionLogs() throws Exception {
145        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
146                                                                  "-Xmx32M",
147                                                                  "-Xmn16M",
148                                                                  "-XX:+PrintGCDetails",
149                                                                  GCTestWithToSpaceExhaustion.class.getName());
150
151        OutputAnalyzer output = new OutputAnalyzer(pb.start());
152        checkMessagesAtLevel(output, exhFailureMessages, Level.FINER);
153        output.shouldHaveExitValue(0);
154
155        pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
156                                                   "-Xmx32M",
157                                                   "-Xmn16M",
158                                                   "-XX:+PrintGCDetails",
159                                                   "-XX:+UnlockExperimentalVMOptions",
160                                                   "-XX:G1LogLevel=finest",
161                                                   GCTestWithToSpaceExhaustion.class.getName());
162
163        output = new OutputAnalyzer(pb.start());
164        checkMessagesAtLevel(output, exhFailureMessages, Level.FINEST);
165        output.shouldHaveExitValue(0);
166    }
167
168    static class GCTest {
169        private static byte[] garbage;
170        public static void main(String [] args) {
171            System.out.println("Creating garbage");
172            // create 128MB of garbage. This should result in at least one GC
173            for (int i = 0; i < 1024; i++) {
174                garbage = new byte[128 * 1024];
175            }
176            System.out.println("Done");
177        }
178    }
179
180    static class GCTestWithToSpaceExhaustion {
181        private static byte[] garbage;
182        private static byte[] largeObject;
183        public static void main(String [] args) {
184            largeObject = new byte[16*1024*1024];
185            System.out.println("Creating garbage");
186            // create 128MB of garbage. This should result in at least one GC,
187            // some of them with to-space exhaustion.
188            for (int i = 0; i < 1024; i++) {
189                garbage = new byte[128 * 1024];
190            }
191            System.out.println("Done");
192        }
193    }
194}
195
196