Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/gc/shenandoah/jni/TestStringCriticalWithDedup.java
64507 views
1
/*
2
* Copyright (c) 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
/* @test TestStringCriticalWithDedup
26
* @summary Test string deduplication should not cause string critical to crash VM
27
* @requires vm.gc.Shenandoah
28
* @modules java.base/java.lang:open
29
*
30
* @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
31
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:+UseStringDeduplication -XX:-CompactStrings
32
* -XX:+ShenandoahVerify -XX:+ShenandoahDegeneratedGC -XX:ShenandoahTargetNumRegions=4096
33
* TestStringCriticalWithDedup
34
*
35
* @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
36
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive -XX:+UseStringDeduplication -XX:-CompactStrings
37
* -XX:+ShenandoahVerify -XX:-ShenandoahDegeneratedGC -XX:ShenandoahTargetNumRegions=4096
38
* TestStringCriticalWithDedup
39
*/
40
41
/* @test TestPinnedGarbage
42
* @summary Test string deduplication should not cause string critical to crash VM
43
* @requires vm.gc.Shenandoah
44
* @modules java.base/java.lang:open
45
*
46
* @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx512m
47
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive -XX:+UseStringDeduplication -XX:-CompactStrings
48
* -XX:ShenandoahTargetNumRegions=4096
49
* TestStringCriticalWithDedup
50
*
51
* @run main/othervm/native -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -Xmx256m
52
* -XX:+UseShenandoahGC -XX:+UseStringDeduplication -XX:-CompactStrings
53
* -XX:ShenandoahTargetNumRegions=4096 -XX:+ShenandoahVerify
54
* TestStringCriticalWithDedup
55
*/
56
57
import java.util.List;
58
import java.util.ArrayList;
59
import java.util.concurrent.*;
60
import java.lang.reflect.*;
61
62
public class TestStringCriticalWithDedup {
63
private static Field valueField;
64
65
static {
66
System.loadLibrary("TestStringCriticalWithDedup");
67
try {
68
valueField = String.class.getDeclaredField("value");
69
valueField.setAccessible(true);
70
} catch (Exception e) {
71
throw new RuntimeException(e);
72
}
73
}
74
75
private static final int NUM_RUNS = 100;
76
private static final int STRING_COUNT = 1 << 16;
77
private static final int LITTLE_GARBAGE_COUNT = 1 << 5;
78
private static final int PINNED_STRING_COUNT = 1 << 4;
79
80
private static native long pin(String s);
81
private static native void unpin(String s, long p);
82
83
84
private static volatile MyClass sink;
85
public static void main(String[] args) {
86
ThreadLocalRandom rng = ThreadLocalRandom.current();
87
for (int i = 0; i < NUM_RUNS; i++) {
88
test(rng);
89
}
90
}
91
92
private static Object getValue(String string) {
93
try {
94
return valueField.get(string);
95
} catch (Exception e) {
96
throw new RuntimeException(e);
97
}
98
}
99
100
private static void pissiblePinString(ThreadLocalRandom rng, List<Tuple> pinnedList, String s) {
101
int oneInCounter = STRING_COUNT / PINNED_STRING_COUNT;
102
if (rng.nextInt(oneInCounter) == 1) {
103
long v = pin(s);
104
Object value = getValue(s);
105
pinnedList.add(new Tuple(s, value, v));
106
}
107
}
108
109
private static void test(ThreadLocalRandom rng) {
110
String[] strArray = new String[STRING_COUNT];
111
List<Tuple> pinnedStrings = new ArrayList<>(PINNED_STRING_COUNT);
112
for (int i = 0; i < STRING_COUNT; i++) {
113
// Create some garbage inbetween, so strings can be scattered in
114
// different regions
115
createLittleGarbage(rng);
116
117
strArray[i] = new String("Hello" + (i % 10));
118
pissiblePinString(rng, pinnedStrings, strArray[i]);
119
}
120
121
// Let deduplication thread to run a bit
122
try {
123
Thread.sleep(10);
124
} catch(Exception e) {
125
}
126
127
for (int i = 0; i < pinnedStrings.size(); i ++) {
128
Tuple p = pinnedStrings.get(i);
129
String s = p.getString();
130
if (getValue(s) != p.getValue()) {
131
System.out.println(getValue(s) + " != " + p.getValue());
132
throw new RuntimeException("String value should be pinned");
133
}
134
unpin(p.getString(), p.getValuePointer());
135
}
136
}
137
138
private static void createLittleGarbage(ThreadLocalRandom rng) {
139
int count = rng.nextInt(LITTLE_GARBAGE_COUNT);
140
for (int index = 0; index < count; index ++) {
141
sink = new MyClass();
142
}
143
}
144
145
private static class Tuple {
146
String s;
147
Object value;
148
long valuePointer;
149
public Tuple(String s, Object value, long vp) {
150
this.s = s;
151
this.value = value;
152
this.valuePointer = vp;
153
}
154
155
public String getString() {
156
return s;
157
}
158
public Object getValue() { return value; }
159
public long getValuePointer() {
160
return valuePointer;
161
}
162
}
163
164
private static class MyClass {
165
public long[] payload = new long[10];
166
}
167
}
168
169