Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/StringInternSync/StringInternSync.java
41155 views
1
/*
2
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. 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
26
* @key stress randomness
27
*
28
* @summary converted from VM Testbase gc/gctests/StringInternSync.
29
* VM Testbase keywords: [gc, stress, stressopt, feature_perm_removal_jdk7, nonconcurrent]
30
* VM Testbase readme:
31
* The test verifies that String.intern is correctly synchronized.
32
* Test interns same strings in different threads and verifies that all interned equal
33
* strings are same objects.
34
* This test interns a few large strings.
35
*
36
* @library /vmTestbase
37
* /test/lib
38
* @run main/othervm
39
* -XX:+UnlockDiagnosticVMOptions
40
* -XX:+VerifyStringTableAtExit
41
* gc.gctests.StringInternSync.StringInternSync
42
* -ms low
43
*/
44
45
package gc.gctests.StringInternSync;
46
47
import java.util.*;
48
import java.util.ArrayList;
49
import java.util.concurrent.locks.ReadWriteLock;
50
import java.util.concurrent.locks.ReentrantReadWriteLock;
51
import nsk.share.TestBug;
52
import nsk.share.TestFailure;
53
import nsk.share.gc.*;
54
import nsk.share.gc.gp.MemoryStrategy;
55
import nsk.share.gc.gp.MemoryStrategyAware;
56
import nsk.share.gc.gp.string.RandomStringProducer;
57
58
public class StringInternSync extends ThreadedGCTest implements MemoryStrategyAware {
59
60
// The list of strings which will be interned
61
static final List<String> stringsToIntern = new ArrayList();
62
// The global container for references to internded strings
63
static final List<List<String>> internedStrings = new ArrayList<List<String>>();
64
// Approximate size occupied by all interned strings
65
long sizeOfAllInteredStrings = 0;
66
// maximum size of one string
67
int maxStringSize;
68
RandomStringProducer gp = new RandomStringProducer();
69
MemoryStrategy memoryStrategy;
70
ReadWriteLock rwlock = new ReentrantReadWriteLock();
71
72
@Override
73
public void setMemoryStrategy(MemoryStrategy memoryStrategy) {
74
this.memoryStrategy = memoryStrategy;
75
}
76
77
private class StringGenerator implements Runnable {
78
79
List<String> internedLocal;
80
81
public StringGenerator(List<String> internedLocal) {
82
this.internedLocal = internedLocal;
83
}
84
85
public void run() {
86
try {
87
rwlock.readLock().lock();
88
internedLocal.clear();
89
for (String str : stringsToIntern) {
90
// Intern copy of string
91
// and save reference to it
92
internedLocal.add(new String(str).intern());
93
}
94
} finally {
95
rwlock.readLock().unlock();
96
}
97
98
// after each iteration 0 thread
99
// lock our main resource and verify String.intern
100
if (internedLocal == internedStrings.get(0)) {
101
try {
102
rwlock.writeLock().lock();
103
// We select first list and compare all other with it
104
// if 2 strings are equal they should be the same "=="
105
List<String> interned = internedStrings.get(0);
106
107
for (List<String> list : internedStrings) {
108
if (list == interned) {
109
continue;
110
}
111
if (list.size() == 0) {
112
continue; // this thread haven't got lock
113
}
114
115
if (list.size() != interned.size()) {
116
throw new TestFailure("Size of interned string list differ from origial."
117
+ " interned " + list.size() + " original " + interned.size());
118
}
119
for (int i = 0; i < interned.size(); i++) {
120
String str = interned.get(i);
121
if (!str.equals(list.get(i))) {
122
throw new TestFailure("The interned strings are not the equals.");
123
}
124
if (str != list.get(i)) {
125
throw new TestFailure("The equal interned strings are not the same.");
126
}
127
}
128
list.clear();
129
130
}
131
interned.clear();
132
stringsToIntern.clear();
133
for (long currentSize = 0; currentSize <= sizeOfAllInteredStrings; currentSize++) {
134
stringsToIntern.add(gp.create(maxStringSize));
135
currentSize += maxStringSize;
136
}
137
} finally {
138
rwlock.writeLock().unlock();
139
}
140
}
141
}
142
}
143
144
@Override
145
public void run() {
146
sizeOfAllInteredStrings = 10 * 1024 * 1024; // let use 100 * strings of size 10000
147
maxStringSize = (int) (sizeOfAllInteredStrings / memoryStrategy.getSize(sizeOfAllInteredStrings));
148
log.debug("The overall size of interned strings : " + sizeOfAllInteredStrings / (1024 * 1024) + "M");
149
log.debug("The count of interned strings : " + sizeOfAllInteredStrings / maxStringSize);
150
for (long currentSize = 0; currentSize <= sizeOfAllInteredStrings; currentSize++) {
151
stringsToIntern.add(gp.create(maxStringSize));
152
currentSize += maxStringSize;
153
}
154
super.run();
155
}
156
157
@Override
158
protected Runnable createRunnable(int i) {
159
ArrayList list = new ArrayList();
160
internedStrings.add(list);
161
return new StringGenerator(list);
162
}
163
164
public static void main(String[] args) {
165
GC.runTest(new StringInternSync(), args);
166
}
167
}
168
169