Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/gc/shenandoah/TestStringDedup.java
40942 views
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
*
23
*/
24
25
/*
26
* @test TestStringDedup
27
* @summary Test Shenandoah string deduplication implementation
28
* @key randomness
29
* @requires vm.gc.Shenandoah
30
* @library /test/lib
31
* @modules java.base/jdk.internal.misc:open
32
* @modules java.base/java.lang:open
33
* java.management
34
*
35
* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
36
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
37
* -XX:+ShenandoahDegeneratedGC -DGCCount=1
38
* TestStringDedup
39
*
40
* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
41
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
42
* -XX:-ShenandoahDegeneratedGC -DGCCount=1
43
* TestStringDedup
44
*/
45
46
/*
47
* @test TestStringDedup
48
* @summary Test Shenandoah string deduplication implementation
49
* @key randomness
50
* @requires vm.gc.Shenandoah
51
* @library /test/lib
52
* @modules java.base/jdk.internal.misc:open
53
* @modules java.base/java.lang:open
54
* java.management
55
*
56
* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
57
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -XX:StringDeduplicationAgeThreshold=3
58
* TestStringDedup
59
*
60
* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
61
* -XX:+UseShenandoahGC -XX:StringDeduplicationAgeThreshold=3
62
* TestStringDedup
63
*
64
* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
65
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact -XX:StringDeduplicationAgeThreshold=3
66
* TestStringDedup
67
*/
68
69
/*
70
* @test TestStringDedup
71
* @summary Test Shenandoah string deduplication implementation
72
* @key randomness
73
* @requires vm.gc.Shenandoah
74
* @library /test/lib
75
* @modules java.base/jdk.internal.misc:open
76
* @modules java.base/java.lang:open
77
* java.management
78
*
79
* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
80
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:StringDeduplicationAgeThreshold=3
81
* TestStringDedup
82
*
83
* @run main/othervm -Xmx256m -Xlog:gc+stats -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseStringDeduplication
84
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive -XX:StringDeduplicationAgeThreshold=3
85
* TestStringDedup
86
*/
87
88
import java.lang.reflect.*;
89
import java.util.*;
90
import jdk.test.lib.Utils;
91
92
import sun.misc.*;
93
94
public class TestStringDedup {
95
private static Field valueField;
96
private static Unsafe unsafe;
97
98
private static final int UniqueStrings = 20;
99
// How many GC cycles are needed to complete deduplication.
100
private static final int GCCount = Integer.getInteger("GCCount", 3);
101
102
static {
103
try {
104
Field field = Unsafe.class.getDeclaredField("theUnsafe");
105
field.setAccessible(true);
106
unsafe = (Unsafe) field.get(null);
107
108
valueField = String.class.getDeclaredField("value");
109
valueField.setAccessible(true);
110
} catch (Exception e) {
111
throw new RuntimeException(e);
112
}
113
}
114
115
private static Object getValue(String string) {
116
try {
117
return valueField.get(string);
118
} catch (Exception e) {
119
throw new RuntimeException(e);
120
}
121
}
122
123
static class StringAndId {
124
private String str;
125
private int id;
126
127
public StringAndId(String str, int id) {
128
this.str = str;
129
this.id = id;
130
}
131
132
public String str() {
133
return str;
134
}
135
136
public int id() {
137
return id;
138
}
139
}
140
141
private static void generateStrings(ArrayList<StringAndId> strs, int unique_strs) {
142
Random rn = Utils.getRandomInstance();
143
for (int u = 0; u < unique_strs; u++) {
144
int n = rn.nextInt() % 10;
145
n = Math.max(n, 2);
146
for (int index = 0; index < n; index++) {
147
strs.add(new StringAndId("Unique String " + u, u));
148
}
149
}
150
}
151
152
private static int verifyDedepString(ArrayList<StringAndId> strs) {
153
HashMap<Object, StringAndId> seen = new HashMap<>();
154
int total = 0;
155
int dedup = 0;
156
157
for (StringAndId item : strs) {
158
total++;
159
StringAndId existing_item = seen.get(getValue(item.str()));
160
if (existing_item == null) {
161
seen.put(getValue(item.str()), item);
162
} else {
163
if (item.id() != existing_item.id() ||
164
!item.str().equals(existing_item.str())) {
165
System.out.println("StringDedup error:");
166
System.out.println("String: " + item.str() + " != " + existing_item.str());
167
throw new RuntimeException("StringDedup Test failed");
168
} else {
169
dedup++;
170
}
171
}
172
}
173
return (total - dedup);
174
}
175
176
public static void main(String[] args) {
177
ArrayList<StringAndId> astrs = new ArrayList<>();
178
generateStrings(astrs, UniqueStrings);
179
for (int count = 0; count < GCCount; count ++) {
180
System.gc();
181
}
182
183
int unique_count = 0;
184
for (int waitCount = 0; waitCount < 3; waitCount ++) {
185
// Let concurrent string dedup thread to run
186
try {
187
Thread.sleep(1000);
188
} catch (InterruptedException e) {
189
}
190
191
// All deduplicated, done.
192
unique_count = verifyDedepString(astrs);
193
if ( unique_count == UniqueStrings) {
194
return;
195
}
196
}
197
198
throw new RuntimeException("Expecting " + UniqueStrings + " unique strings, but got " + unique_count);
199
}
200
}
201
202