1 /*
2 * Copyright (c) 2017, 2021, Red Hat, Inc. 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 *
25 /*
26 * @test id=passive
27 * @summary Test Shenandoah string deduplication implementation
28 * @key randomness
29 * @requires vm.gc.Shenandoah
30 * @library /test/lib
31 * @modules java.base/java.lang:open
32 * java.management
33 *
34 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
35 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
36 * -XX:+ShenandoahDegeneratedGC
37 * TestStringDedupStress
38 *
39 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
40 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
41 * -XX:-ShenandoahDegeneratedGC
42 * TestStringDedupStress
43 */
44
45 /*
46 * @test id=default
47 * @summary Test Shenandoah string deduplication implementation
48 * @key randomness
49 * @requires vm.gc.Shenandoah
50 * @library /test/lib
51 * @modules java.base/java.lang:open
52 * java.management
53 *
54 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
55 * -XX:+UseShenandoahGC
56 * -DtargetStrings=3000000
57 * TestStringDedupStress
58 *
59 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
60 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
61 * -DtargetStrings=2000000
62 * TestStringDedupStress
63 *
64 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
96 * -DtargetStrings=2000000
97 * TestStringDedupStress
98 *
99 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
100 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
101 * -XX:+ShenandoahOOMDuringEvacALot
102 * -DtargetStrings=2000000
103 * TestStringDedupStress
104 */
105
106 import java.lang.management.*;
107 import java.lang.reflect.*;
108 import java.util.*;
109 import jdk.test.lib.Utils;
110
111 public class TestStringDedupStress {
112 private static Field valueField;
113
114 private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
115 private static final long MAX_REWRITE_GC_CYCLES = 6;
116 private static final long MAX_REWRITE_TIME = 30*1000; // ms
117
118 private static final int UNIQUE_STRINGS = 20;
119
120 static {
121 try {
122 valueField = String.class.getDeclaredField("value");
123 valueField.setAccessible(true);
124 } catch (Exception e) {
125 throw new RuntimeException(e);
126 }
127 }
128
129 private static Object getValue(String string) {
130 try {
131 return valueField.get(string);
132 } catch (Exception e) {
133 throw new RuntimeException(e);
134 }
135 }
136
194 Random rn = Utils.getRandomInstance();
195
196 for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
197 if ("Shenandoah Cycles".equals(bean.getName())) {
198 gcCycleMBean = bean;
199 break;
200 }
201 }
202
203 if (gcCycleMBean == null) {
204 throw new RuntimeException("Can not find Shenandoah GC cycle mbean");
205 }
206
207 // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique
208 int genIters = TARGET_STRINGS / UNIQUE_STRINGS;
209 for (int index = 0; index < genIters; index++) {
210 generateStrings(astrs, UNIQUE_STRINGS);
211 }
212
213 long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
214 long timeBeforeRewrite = System.currentTimeMillis();
215
216 long loop = 1;
217 while (true) {
218 int arrSize = astrs.size();
219 int index = rn.nextInt(arrSize);
220 StringAndId item = astrs.get(index);
221 int n = rn.nextInt(UNIQUE_STRINGS);
222 item.str = "Unique String " + n;
223 item.id = n;
224
225 if (loop++ % 1000 == 0) {
226 // enough GC cycles for rewritten strings to be deduplicated
227 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {
228 break;
229 }
230
231 // enough time is spent waiting for GC to happen
232 if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {
233 break;
234 }
235 }
236 }
237 verifyDedupString(astrs);
238 }
239 }
|
1 /*
2 * Copyright (c) 2017, 2021, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
26 /*
27 * @test id=passive
28 * @summary Test Shenandoah string deduplication implementation
29 * @key randomness
30 * @requires vm.gc.Shenandoah
31 * @library /test/lib
32 * @modules java.base/java.lang:open
33 * java.management
34 *
35 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
36 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
37 * -XX:+ShenandoahDegeneratedGC
38 * TestStringDedupStress
39 *
40 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
41 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
42 * -XX:-ShenandoahDegeneratedGC
43 * TestStringDedupStress
44 */
45
46 /*
47 * @test id=generational
48 * @summary Test Shenandoah string deduplication implementation
49 * @key randomness
50 * @requires vm.gc.Shenandoah
51 * @library /test/lib
52 * @modules java.base/java.lang:open
53 * java.management
54 *
55 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
56 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
57 * -XX:+ShenandoahDegeneratedGC
58 * -DtargetStrings=3000000
59 * TestStringDedupStress
60 */
61
62 /*
63 * @test id=default
64 * @summary Test Shenandoah string deduplication implementation
65 * @key randomness
66 * @requires vm.gc.Shenandoah
67 * @library /test/lib
68 * @modules java.base/java.lang:open
69 * java.management
70 *
71 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
72 * -XX:+UseShenandoahGC
73 * -DtargetStrings=3000000
74 * TestStringDedupStress
75 *
76 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
77 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
78 * -DtargetStrings=2000000
79 * TestStringDedupStress
80 *
81 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
113 * -DtargetStrings=2000000
114 * TestStringDedupStress
115 *
116 * @run main/othervm -Xmx1g -Xlog:gc+stats -Xlog:gc -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
117 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
118 * -XX:+ShenandoahOOMDuringEvacALot
119 * -DtargetStrings=2000000
120 * TestStringDedupStress
121 */
122
123 import java.lang.management.*;
124 import java.lang.reflect.*;
125 import java.util.*;
126 import jdk.test.lib.Utils;
127
128 public class TestStringDedupStress {
129 private static Field valueField;
130
131 private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
132 private static final long MAX_REWRITE_GC_CYCLES = 6;
133 private static final long MAX_REWRITE_TIME_NS = 30L * 1_000_000_000L; // 30s in ns
134
135 private static final int UNIQUE_STRINGS = 20;
136
137 static {
138 try {
139 valueField = String.class.getDeclaredField("value");
140 valueField.setAccessible(true);
141 } catch (Exception e) {
142 throw new RuntimeException(e);
143 }
144 }
145
146 private static Object getValue(String string) {
147 try {
148 return valueField.get(string);
149 } catch (Exception e) {
150 throw new RuntimeException(e);
151 }
152 }
153
211 Random rn = Utils.getRandomInstance();
212
213 for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
214 if ("Shenandoah Cycles".equals(bean.getName())) {
215 gcCycleMBean = bean;
216 break;
217 }
218 }
219
220 if (gcCycleMBean == null) {
221 throw new RuntimeException("Can not find Shenandoah GC cycle mbean");
222 }
223
224 // Generate roughly TARGET_STRINGS strings, only UNIQUE_STRINGS are unique
225 int genIters = TARGET_STRINGS / UNIQUE_STRINGS;
226 for (int index = 0; index < genIters; index++) {
227 generateStrings(astrs, UNIQUE_STRINGS);
228 }
229
230 long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
231 long timeBeforeRewriteNanos = System.nanoTime();
232
233 long loop = 1;
234 while (true) {
235 int arrSize = astrs.size();
236 int index = rn.nextInt(arrSize);
237 StringAndId item = astrs.get(index);
238 int n = rn.nextInt(UNIQUE_STRINGS);
239 item.str = "Unique String " + n;
240 item.id = n;
241
242 if (loop++ % 1000 == 0) {
243 // enough GC cycles for rewritten strings to be deduplicated
244 if (gcCycleMBean.getCollectionCount() - cycleBeforeRewrite >= MAX_REWRITE_GC_CYCLES) {
245 break;
246 }
247
248 // enough time is spent waiting for GC to happen
249 if (System.nanoTime() - timeBeforeRewriteNanos >= MAX_REWRITE_TIME_NS) {
250 break;
251 }
252 }
253 }
254 verifyDedupString(astrs);
255 }
256 }
|