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 TestVerifyGCType
26 * @summary Test the VerifyGCType flag to ensure basic functionality.
27 * @key gc
28 * @requires vm.gc.G1
29 * @library /test/lib
30 * @run driver TestVerifyGCType
31 */
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35
36 import jdk.test.lib.Asserts;
37 import jdk.test.lib.Utils;
38 import jdk.test.lib.process.OutputAnalyzer;
39 import jdk.test.lib.process.ProcessTools;
40
41 public class TestVerifyGCType {
42 public static final String VERIFY_TAG = "[gc,verify]";
43 public static final String VERIFY_BEFORE = "Verifying Before GC";
44 public static final String VERIFY_DURING = "Verifying During GC";
45 public static final String VERIFY_AFTER = "Verifying After GC";
46
47 public static void main(String args[]) throws Exception {
48 testAllVerificationEnabled();
49 testAllExplicitlyEnabled();
50 testFullAndRemark();
51 testBadVerificationType();
52 testUnsupportedCollector();
53 }
54
55 private static void testAllVerificationEnabled() throws Exception {
56 // Test with all verification enabled
57 OutputAnalyzer output = testWithVerificationType(new String[0]);
58 output.shouldHaveExitValue(0);
59
60 verifyCollection("Pause Young", true, false, true, output.getStdout());
61 verifyCollection("Pause Remark", false, true, false, output.getStdout());
62 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
63 verifyCollection("Pause Full", true, true, true, output.getStdout());
64 }
65
66 private static void testAllExplicitlyEnabled() throws Exception {
67 OutputAnalyzer output;
68 // Test with all explicitly enabled
69 output = testWithVerificationType(new String[] {"young", "remark", "cleanup", "full"});
70 output.shouldHaveExitValue(0);
71
72 verifyCollection("Pause Young", true, false, true, output.getStdout());
73 verifyCollection("Pause Remark", false, true, false, output.getStdout());
74 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
75 verifyCollection("Pause Full", true, true, true, output.getStdout());
76 }
77
78 private static void testFullAndRemark() throws Exception {
79 OutputAnalyzer output;
80 // Test with full and remark
81 output = testWithVerificationType(new String[] {"remark", "full"});
82 output.shouldHaveExitValue(0);
83
84 verifyCollection("Pause Young", false, false, false, output.getStdout());
85 verifyCollection("Pause Remark", false, true, false, output.getStdout());
86 verifyCollection("Pause Cleanup", false, false, false, output.getStdout());
87 verifyCollection("Pause Full", true, true, true, output.getStdout());
88 }
89
90 private static void testBadVerificationType() throws Exception {
91 OutputAnalyzer output;
92 // Test bad type
93 output = testWithVerificationType(new String[] {"old"});
94 output.shouldHaveExitValue(0);
95
96 output.shouldMatch("VerifyGCType: '.*' is unknown. Available types are: young, mixed, remark, cleanup and full");
97 verifyCollection("Pause Young", true, false, true, output.getStdout());
98 verifyCollection("Pause Remark", false, true, false, output.getStdout());
99 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
100 verifyCollection("Pause Full", true, true, true, output.getStdout());
101 }
102
103 private static void testUnsupportedCollector() throws Exception {
104 OutputAnalyzer output;
105 // Test bad gc
106 output = testWithBadGC();
107 output.shouldHaveExitValue(0);
108 output.shouldMatch("VerifyGCType is not supported by this collector.");
109 }
110
111 private static OutputAnalyzer testWithVerificationType(String[] types) throws Exception {
112 ArrayList<String> basicOpts = new ArrayList<>();
113 Collections.addAll(basicOpts, new String[] {
114 "-XX:+UseG1GC",
115 "-XX:InitiatingHeapOccupancyPercent=1",
116 "-Xlog:gc,gc+start,gc+verify=info",
117 "-Xmx10m",
118 "-Xms10m",
119 "-Xmn2m",
120 "-XX:+UnlockDiagnosticVMOptions",
121 "-XX:+VerifyBeforeGC",
122 "-XX:+VerifyAfterGC",
123 "-XX:+VerifyDuringGC"});
124
125 for(String verifyType : types) {
126 basicOpts.add("-XX:VerifyGCType="+verifyType);
127 }
128
129 basicOpts.add(GarbageProducer.class.getName());
130
131 ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(basicOpts.toArray(
132 new String[basicOpts.size()]));
133 OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
134 return analyzer;
135 }
136
137 private static OutputAnalyzer testWithBadGC() throws Exception {
138 ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(new String[] {
139 "-XX:+UseParallelGC",
140 "-XX:+UnlockDiagnosticVMOptions",
141 "-XX:VerifyGCType=full",
142 "-version"});
143
144 OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
145 return analyzer;
146 }
147
148 private static void verifyCollection(String name, boolean expectBefore, boolean expectDuring, boolean expectAfter, String data) {
149 CollectionInfo ci = CollectionInfo.parseFirst(name, data);
197 int firstIndex = data.indexOf(name);
198 if (firstIndex == -1) {
199 return result;
200 }
201 int nextIndex = data.indexOf(name, firstIndex + 1);
202 if (nextIndex == -1) {
203 return result;
204 }
205 // Found an entry for this name
206 result = new CollectionInfo(name);
207 String collectionData = data.substring(firstIndex, nextIndex + name.length());
208 for (String line : collectionData.split(System.getProperty("line.separator"))) {
209 if (line.contains(VERIFY_TAG)) {
210 result.addVerification(line);
211 }
212 }
213 return result;
214 }
215 }
216
217 public static class GarbageProducer {
218 static Object[] garbage = new Object[100];
219
220 public static void main(String args[]) throws Exception {
221 // Ensure at least one full GC
222 System.gc();
223 // Alloc 10M, but only keep 1M alive.
224 for(int i = 0; i<1000; i++) {
225 garbage[i%garbage.length] = new byte[10240];
226 }
227 // Sleep to make sure concurrent cycle is done
228 Thread.sleep(1000);
229 }
230 }
231 }
|
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 TestVerifyGCType
26 * @summary Test the VerifyGCType flag to ensure basic functionality.
27 * @key gc
28 * @requires vm.gc.G1
29 * @library /test/lib
30 * @build sun.hotspot.WhiteBox
31 * @run main ClassFileInstaller sun.hotspot.WhiteBox
32 * @run driver TestVerifyGCType
33 */
34
35 import java.util.ArrayList;
36 import java.util.Collections;
37
38 import jdk.test.lib.Asserts;
39 import jdk.test.lib.Utils;
40 import jdk.test.lib.process.OutputAnalyzer;
41 import jdk.test.lib.process.ProcessTools;
42 import sun.hotspot.WhiteBox;
43
44 public class TestVerifyGCType {
45 public static final String VERIFY_TAG = "[gc,verify]";
46 public static final String VERIFY_BEFORE = "Verifying Before GC";
47 public static final String VERIFY_DURING = "Verifying During GC";
48 public static final String VERIFY_AFTER = "Verifying After GC";
49
50 public static void main(String args[]) throws Exception {
51 testAllVerificationEnabled();
52 testAllExplicitlyEnabled();
53 testFullAndRemark();
54 testConcurrentMark();
55 testBadVerificationType();
56 testUnsupportedCollector();
57 }
58
59 private static void testAllVerificationEnabled() throws Exception {
60 // Test with all verification enabled
61 OutputAnalyzer output = testWithVerificationType(new String[0]);
62 output.shouldHaveExitValue(0);
63
64 verifyCollection("Pause Young", true, false, true, output.getStdout());
65 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
66 verifyCollection("Pause Mixed", true, false, true, output.getStdout());
67 verifyCollection("Pause Remark", false, true, false, output.getStdout());
68 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
69 verifyCollection("Pause Full", true, true, true, output.getStdout());
70 }
71
72 private static void testAllExplicitlyEnabled() throws Exception {
73 OutputAnalyzer output;
74 // Test with all explicitly enabled
75 output = testWithVerificationType(new String[] {
76 "young-only", "initial-mark", "mixed", "remark", "cleanup", "full"});
77 output.shouldHaveExitValue(0);
78
79 verifyCollection("Pause Young", true, false, true, output.getStdout());
80 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
81 verifyCollection("Pause Mixed", true, false, true, output.getStdout());
82 verifyCollection("Pause Remark", false, true, false, output.getStdout());
83 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
84 verifyCollection("Pause Full", true, true, true, output.getStdout());
85 }
86
87 private static void testFullAndRemark() throws Exception {
88 OutputAnalyzer output;
89 // Test with full and remark
90 output = testWithVerificationType(new String[] {"remark", "full"});
91 output.shouldHaveExitValue(0);
92
93 verifyCollection("Pause Young", false, false, false, output.getStdout());
94 verifyCollection("Pause Initial Mark", false, false, false, output.getStdout());
95 verifyCollection("Pause Mixed", false, false, false, output.getStdout());
96 verifyCollection("Pause Remark", false, true, false, output.getStdout());
97 verifyCollection("Pause Cleanup", false, false, false, output.getStdout());
98 verifyCollection("Pause Full", true, true, true, output.getStdout());
99 }
100
101 private static void testConcurrentMark() throws Exception {
102 OutputAnalyzer output;
103 // Test with full and remark
104 output = testWithVerificationType(new String[] {"initial-mark", "cleanup", "remark"});
105 output.shouldHaveExitValue(0);
106
107 verifyCollection("Pause Young", false, false, false, output.getStdout());
108 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
109 verifyCollection("Pause Mixed", false, false, false, output.getStdout());
110 verifyCollection("Pause Remark", false, true, false, output.getStdout());
111 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
112 verifyCollection("Pause Full", false, false, false, output.getStdout());
113 }
114
115 private static void testBadVerificationType() throws Exception {
116 OutputAnalyzer output;
117 // Test bad type
118 output = testWithVerificationType(new String[] {"old"});
119 output.shouldHaveExitValue(0);
120
121 output.shouldMatch("VerifyGCType: '.*' is unknown. Available types are: young-only, initial-mark, mixed, remark, cleanup and full");
122 verifyCollection("Pause Young", true, false, true, output.getStdout());
123 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
124 verifyCollection("Pause Mixed", true, false, true, output.getStdout());
125 verifyCollection("Pause Remark", false, true, false, output.getStdout());
126 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
127 verifyCollection("Pause Full", true, true, true, output.getStdout());
128 }
129
130 private static void testUnsupportedCollector() throws Exception {
131 OutputAnalyzer output;
132 // Test bad gc
133 output = testWithBadGC();
134 output.shouldHaveExitValue(0);
135 output.shouldMatch("VerifyGCType is not supported by this collector.");
136 }
137
138 private static OutputAnalyzer testWithVerificationType(String[] types) throws Exception {
139 ArrayList<String> basicOpts = new ArrayList<>();
140 Collections.addAll(basicOpts, new String[] {
141 "-Xbootclasspath/a:.",
142 "-XX:+UseG1GC",
143 "-XX:+WhiteBoxAPI",
144 "-XX:+ExplicitGCInvokesConcurrent",
145 "-Xlog:gc,gc+start,gc+verify=info",
146 "-XX:+UnlockDiagnosticVMOptions",
147 "-XX:+VerifyBeforeGC",
148 "-XX:+VerifyAfterGC",
149 "-XX:+VerifyDuringGC"});
150
151 for(String verifyType : types) {
152 basicOpts.add("-XX:VerifyGCType="+verifyType);
153 }
154
155 basicOpts.add(TriggerGCs.class.getName());
156
157 ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(basicOpts.toArray(
158 new String[basicOpts.size()]));
159 OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
160 return analyzer;
161 }
162
163 private static OutputAnalyzer testWithBadGC() throws Exception {
164 ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(new String[] {
165 "-XX:+UseParallelGC",
166 "-XX:+UnlockDiagnosticVMOptions",
167 "-XX:VerifyGCType=full",
168 "-version"});
169
170 OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
171 return analyzer;
172 }
173
174 private static void verifyCollection(String name, boolean expectBefore, boolean expectDuring, boolean expectAfter, String data) {
175 CollectionInfo ci = CollectionInfo.parseFirst(name, data);
223 int firstIndex = data.indexOf(name);
224 if (firstIndex == -1) {
225 return result;
226 }
227 int nextIndex = data.indexOf(name, firstIndex + 1);
228 if (nextIndex == -1) {
229 return result;
230 }
231 // Found an entry for this name
232 result = new CollectionInfo(name);
233 String collectionData = data.substring(firstIndex, nextIndex + name.length());
234 for (String line : collectionData.split(System.getProperty("line.separator"))) {
235 if (line.contains(VERIFY_TAG)) {
236 result.addVerification(line);
237 }
238 }
239 return result;
240 }
241 }
242
243 public static class TriggerGCs {
244 public static void main(String args[]) throws Exception {
245 WhiteBox wb = WhiteBox.getWhiteBox();
246 // Trigger the differnt GCs using the WhiteBox API and System.gc()
247 // to start a concurrent cycle with -XX:+ExplicitGCInvokesConcurrent.
248 wb.fullGC(); // full
249 System.gc(); // initial-mark, remark and cleanup
250 // Sleep to make sure concurrent cycle is done
251 Thread.sleep(1000);
252 wb.youngGC(); // young-only
253 wb.youngGC(); // mixed
254 }
255 }
256 }
|