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 }
57
58 private static void testAllVerificationEnabled() throws Exception {
59 // Test with all verification enabled
60 OutputAnalyzer output = testWithVerificationType(new String[0]);
61 output.shouldHaveExitValue(0);
62
63 verifyCollection("Pause Young", true, false, true, output.getStdout());
64 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
65 verifyCollection("Pause Mixed", true, false, true, output.getStdout());
66 verifyCollection("Pause Remark", false, true, false, output.getStdout());
67 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
68 verifyCollection("Pause Full", true, true, true, output.getStdout());
69 }
70
71 private static void testAllExplicitlyEnabled() throws Exception {
72 OutputAnalyzer output;
73 // Test with all explicitly enabled
74 output = testWithVerificationType(new String[] {
75 "young-only", "initial-mark", "mixed", "remark", "cleanup", "full"});
76 output.shouldHaveExitValue(0);
77
78 verifyCollection("Pause Young", true, false, true, output.getStdout());
79 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
80 verifyCollection("Pause Mixed", true, false, true, output.getStdout());
81 verifyCollection("Pause Remark", false, true, false, output.getStdout());
82 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
83 verifyCollection("Pause Full", true, true, true, output.getStdout());
84 }
85
86 private static void testFullAndRemark() throws Exception {
87 OutputAnalyzer output;
88 // Test with full and remark
89 output = testWithVerificationType(new String[] {"remark", "full"});
90 output.shouldHaveExitValue(0);
91
92 verifyCollection("Pause Young", false, false, false, output.getStdout());
93 verifyCollection("Pause Initial Mark", false, false, false, output.getStdout());
94 verifyCollection("Pause Mixed", false, false, false, output.getStdout());
95 verifyCollection("Pause Remark", false, true, false, output.getStdout());
96 verifyCollection("Pause Cleanup", false, false, false, output.getStdout());
97 verifyCollection("Pause Full", true, true, true, output.getStdout());
98 }
99
100 private static void testConcurrentMark() throws Exception {
101 OutputAnalyzer output;
102 // Test with full and remark
103 output = testWithVerificationType(new String[] {"initial-mark", "cleanup", "remark"});
104 output.shouldHaveExitValue(0);
105
106 verifyCollection("Pause Young", false, false, false, output.getStdout());
107 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
108 verifyCollection("Pause Mixed", false, false, false, output.getStdout());
109 verifyCollection("Pause Remark", false, true, false, output.getStdout());
110 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
111 verifyCollection("Pause Full", false, false, false, output.getStdout());
112 }
113
114 private static void testBadVerificationType() throws Exception {
115 OutputAnalyzer output;
116 // Test bad type
117 output = testWithVerificationType(new String[] {"old"});
118 output.shouldHaveExitValue(0);
119
120 output.shouldMatch("VerifyGCType: '.*' is unknown. Available types are: young-only, initial-mark, mixed, remark, cleanup and full");
121 verifyCollection("Pause Young", true, false, true, output.getStdout());
122 verifyCollection("Pause Initial Mark", true, false, true, output.getStdout());
123 verifyCollection("Pause Mixed", true, false, true, output.getStdout());
124 verifyCollection("Pause Remark", false, true, false, output.getStdout());
125 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
126 verifyCollection("Pause Full", true, true, true, output.getStdout());
127 }
128
129 private static OutputAnalyzer testWithVerificationType(String[] types) throws Exception {
130 ArrayList<String> basicOpts = new ArrayList<>();
131 Collections.addAll(basicOpts, new String[] {
132 "-Xbootclasspath/a:.",
133 "-XX:+UnlockDiagnosticVMOptions",
134 "-XX:+UseG1GC",
135 "-XX:+WhiteBoxAPI",
136 "-Xlog:gc,gc+start,gc+verify=info",
137 "-Xms16m",
138 "-Xmx16m",
139 "-XX:ParallelGCThreads=1",
140 "-XX:G1HeapWastePercent=1",
141 "-XX:+VerifyBeforeGC",
142 "-XX:+VerifyAfterGC",
143 "-XX:+VerifyDuringGC"});
212 return result;
213 }
214 // Found an entry for this name
215 result = new CollectionInfo(name);
216 String collectionData = data.substring(firstIndex, nextIndex + name.length());
217 for (String line : collectionData.split(System.getProperty("line.separator"))) {
218 if (line.contains(VERIFY_TAG)) {
219 result.addVerification(line);
220 }
221 }
222 return result;
223 }
224 }
225
226 public static class TriggerGCs {
227 public static void main(String args[]) throws Exception {
228 WhiteBox wb = WhiteBox.getWhiteBox();
229 // Allocate some memory that can be turned into garbage.
230 Object[] used = alloc1M();
231
232 // Trigger the different GCs using the WhiteBox API.
233 wb.fullGC(); // full
234
235 // Memory have been promoted to old by full GC. Free
236 // some memory to be reclaimed by concurrent cycle.
237 partialFree(used);
238 wb.g1StartConcMarkCycle(); // initial-mark, remark and cleanup
239
240 // Sleep to make sure concurrent cycle is done
241 while (wb.g1InConcurrentMark()) {
242 Thread.sleep(1000);
243 }
244
245 // Trigger two young GCs, first will be young-only, second will be mixed.
246 wb.youngGC(); // young-only
247 wb.youngGC(); // mixed
248 }
249
250 private static Object[] alloc1M() {
251 Object[] ret = new Object[1024];
252 // Alloc 1024 1k byte arrays (~1M)
253 for (int i = 0; i < ret.length; i++) {
254 ret[i] = new byte[1024];
255 }
256 return ret;
257 }
258
259 private static void partialFree(Object[] array) {
260 // Free every other element
261 for (int i = 0; i < array.length; i+=2) {
262 array[i] = null;
263 }
264 }
265 }
266 }
|
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 }
57
58 private static void testAllVerificationEnabled() throws Exception {
59 // Test with all verification enabled
60 OutputAnalyzer output = testWithVerificationType(new String[0]);
61 output.shouldHaveExitValue(0);
62
63 verifyCollection("Pause Young (Normal)", true, false, true, output.getStdout());
64 verifyCollection("Pause Young (Concurrent Start)", true, false, true, output.getStdout());
65 verifyCollection("Pause Young (Mixed)", true, false, true, output.getStdout());
66 verifyCollection("Pause Young (Concurrent End)", 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-normal", "concurrent-start", "mixed", "remark", "cleanup", "full"});
77 output.shouldHaveExitValue(0);
78
79 verifyCollection("Pause Young (Normal)", true, false, true, output.getStdout());
80 verifyCollection("Pause Young (Concurrent Start)", true, false, true, output.getStdout());
81 verifyCollection("Pause Young (Mixed)", true, false, true, output.getStdout());
82 verifyCollection("Pause Young (Concurrent End)", true, false, true, output.getStdout());
83 verifyCollection("Pause Remark", false, true, false, output.getStdout());
84 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
85 verifyCollection("Pause Full", true, true, true, output.getStdout());
86 }
87
88 private static void testFullAndRemark() throws Exception {
89 OutputAnalyzer output;
90 // Test with full and remark
91 output = testWithVerificationType(new String[] {"remark", "full"});
92 output.shouldHaveExitValue(0);
93
94 verifyCollection("Pause Young (Normal)", false, false, false, output.getStdout());
95 verifyCollection("Pause Young (Concurrent Start)", false, false, false, output.getStdout());
96 verifyCollection("Pause Young (Mixed)", false, false, false, output.getStdout());
97 verifyCollection("Pause Young (Concurrent End)", false, false, false, output.getStdout());
98 verifyCollection("Pause Remark", false, true, false, output.getStdout());
99 verifyCollection("Pause Cleanup", false, false, false, output.getStdout());
100 verifyCollection("Pause Full", true, true, true, output.getStdout());
101 }
102
103 private static void testConcurrentMark() throws Exception {
104 OutputAnalyzer output;
105 // Test with full and remark
106 output = testWithVerificationType(new String[] {"concurrent-start", "cleanup", "remark"});
107 output.shouldHaveExitValue(0);
108
109 verifyCollection("Pause Young (Normal)", false, false, false, output.getStdout());
110 verifyCollection("Pause Young (Concurrent Start)", true, false, true, output.getStdout());
111 verifyCollection("Pause Young (Mixed)", false, false, false, output.getStdout());
112 verifyCollection("Pause Young (Concurrent End)", false, false, false, output.getStdout());
113 verifyCollection("Pause Remark", false, true, false, output.getStdout());
114 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
115 verifyCollection("Pause Full", false, false, false, output.getStdout());
116 }
117
118 private static void testBadVerificationType() throws Exception {
119 OutputAnalyzer output;
120 // Test bad type
121 output = testWithVerificationType(new String[] {"old"});
122 output.shouldHaveExitValue(0);
123
124 output.shouldMatch("VerifyGCType: '.*' is unknown. Available types are: young-normal, concurrent-start, mixed, remark, cleanup and full");
125 verifyCollection("Pause Young (Normal)", true, false, true, output.getStdout());
126 verifyCollection("Pause Young (Concurrent Start)", true, false, true, output.getStdout());
127 verifyCollection("Pause Young (Mixed)", true, false, true, output.getStdout());
128 verifyCollection("Pause Young (Concurrent End)", true, false, true, output.getStdout());
129 verifyCollection("Pause Remark", false, true, false, output.getStdout());
130 verifyCollection("Pause Cleanup", false, true, false, output.getStdout());
131 verifyCollection("Pause Full", true, true, true, output.getStdout());
132 }
133
134 private static OutputAnalyzer testWithVerificationType(String[] types) throws Exception {
135 ArrayList<String> basicOpts = new ArrayList<>();
136 Collections.addAll(basicOpts, new String[] {
137 "-Xbootclasspath/a:.",
138 "-XX:+UnlockDiagnosticVMOptions",
139 "-XX:+UseG1GC",
140 "-XX:+WhiteBoxAPI",
141 "-Xlog:gc,gc+start,gc+verify=info",
142 "-Xms16m",
143 "-Xmx16m",
144 "-XX:ParallelGCThreads=1",
145 "-XX:G1HeapWastePercent=1",
146 "-XX:+VerifyBeforeGC",
147 "-XX:+VerifyAfterGC",
148 "-XX:+VerifyDuringGC"});
217 return result;
218 }
219 // Found an entry for this name
220 result = new CollectionInfo(name);
221 String collectionData = data.substring(firstIndex, nextIndex + name.length());
222 for (String line : collectionData.split(System.getProperty("line.separator"))) {
223 if (line.contains(VERIFY_TAG)) {
224 result.addVerification(line);
225 }
226 }
227 return result;
228 }
229 }
230
231 public static class TriggerGCs {
232 public static void main(String args[]) throws Exception {
233 WhiteBox wb = WhiteBox.getWhiteBox();
234 // Allocate some memory that can be turned into garbage.
235 Object[] used = alloc1M();
236
237 wb.youngGC(); // young-normal
238
239 // Trigger the different GCs using the WhiteBox API.
240 wb.fullGC(); // full
241
242 // Memory have been promoted to old by full GC. Free
243 // some memory to be reclaimed by concurrent cycle.
244 partialFree(used);
245 wb.g1StartConcMarkCycle(); // concurrent-start, remark and cleanup
246
247 // Sleep to make sure concurrent cycle is done
248 while (wb.g1InConcurrentMark()) {
249 Thread.sleep(1000);
250 }
251
252 // Trigger two young GCs, first will be young-concurrent-end, second will be mixed.
253 wb.youngGC(); // young-concurrent-end
254 wb.youngGC(); // mixed
255 }
256
257 private static Object[] alloc1M() {
258 Object[] ret = new Object[1024];
259 // Alloc 1024 1k byte arrays (~1M)
260 for (int i = 0; i < ret.length; i++) {
261 ret[i] = new byte[1024];
262 }
263 return ret;
264 }
265
266 private static void partialFree(Object[] array) {
267 // Free every other element
268 for (int i = 0; i < array.length; i+=2) {
269 array[i] = null;
270 }
271 }
272 }
273 }
|