Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/StringInternSyncWithGC/StringInternSyncWithGC.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/StringInternSyncWithGC.
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 with GC.
32
* Test interns and drop the same strings in different threads and provokes GC.
33
* Additionally test creates weak/soft references to interned strings.
34
* Test fails if any string object is inaccessible.
35
*
36
* @library /vmTestbase
37
* /test/lib
38
* @run main/othervm
39
* -Xlog:gc:gc.log
40
* gc.gctests.StringInternSyncWithGC.StringInternSyncWithGC
41
* -ms low
42
* -memUsage 3
43
* -appTimeout 30
44
* -capacityVerPart 2
45
*/
46
47
package gc.gctests.StringInternSyncWithGC;
48
49
import java.util.ArrayList;
50
import java.util.List;
51
52
import nsk.share.gc.*;
53
import nsk.share.gc.gp.MemoryStrategy;
54
import nsk.share.gc.gp.MemoryStrategyAware;
55
import nsk.share.gc.gp.string.RandomStringProducer;
56
import nsk.share.test.ExecutionController;
57
58
public class StringInternSyncWithGC extends ThreadedGCTest implements MemoryStrategyAware {
59
60
// Maximum size of one string
61
// Depends from all size and memory strategy
62
private int maxStringSize;
63
private MemoryStrategy memoryStrategy;
64
private final int memUsageFactor;
65
private final long endTimeCapacityVer;
66
67
// The list of strings which are interned during iteration
68
private final List<String> stringsToIntern = new ArrayList();
69
private final RandomStringProducer gp = new RandomStringProducer();
70
71
public StringInternSyncWithGC(int memUsage, long endTimeCapVer) {
72
memUsageFactor = memUsage;
73
endTimeCapacityVer = endTimeCapVer;
74
}
75
76
@Override
77
public void setMemoryStrategy(MemoryStrategy memoryStrategy) {
78
this.memoryStrategy = memoryStrategy;
79
}
80
81
/**
82
* Verify that we could use certain amount of memory.
83
*/
84
private boolean verifyInternedStringCapacity(long initialSize) {
85
long currentSize = 0;
86
final int STEP = 1000;
87
int iter = 0;
88
char[] template = new char[(int) (initialSize / STEP)];
89
90
List<String> tmpList = new ArrayList<>(STEP);
91
try {
92
while (currentSize <= initialSize) {
93
if (endTimeCapacityVer < System.currentTimeMillis()) {
94
log.debug("Too long to verify interned string capacity");
95
log.debug("Silently pass.");
96
return false;
97
}
98
template[iter]++;
99
if (++iter == template.length) {
100
iter = 0;
101
}
102
String str = new String(template);
103
tmpList.add(str.intern());
104
currentSize += str.length() * 2; //each char costs 2 bytes
105
}
106
} catch (OutOfMemoryError oome) {
107
log.debug("It is not possible to allocate " + initialSize + " size of interned string.");
108
log.debug("Silently pass.");
109
return false;
110
}
111
return true;
112
}
113
114
@Override
115
public void run() {
116
long size = runParams.getTestMemory() / memUsageFactor;
117
if (!verifyInternedStringCapacity(size)) {
118
return;
119
}
120
// Approximate size occupied by all interned strings
121
long sizeOfAllInternedStrings = size / 2;
122
maxStringSize = (int) (sizeOfAllInternedStrings / memoryStrategy.getSize(sizeOfAllInternedStrings, Memory.getObjectExtraSize()));
123
// Each thread keeps reference to each created string.
124
long extraConsumption = runParams.getNumberOfThreads() * Memory.getReferenceSize();
125
log.debug("The overall size of interned strings : " + sizeOfAllInternedStrings / (1024 * 1024) + "M");
126
log.debug("The count of interned strings : " + sizeOfAllInternedStrings / (maxStringSize + extraConsumption));
127
for (long currentSize = 0; currentSize <= sizeOfAllInternedStrings;
128
currentSize += maxStringSize + extraConsumption) {
129
stringsToIntern.add(gp.create(maxStringSize));
130
}
131
super.run();
132
}
133
134
@Override
135
protected Runnable createRunnable(int threadId) {
136
return new StringGenerator(threadId, this);
137
}
138
139
public static void main(String[] args) {
140
int appTimeout = -1;
141
int memUsageFactor = 1;
142
// Part of time that function verifyInternedStringCapacity can take. Time = Application_Timeout / capacityVerTimePart
143
double capacityVerPart = 2;
144
for (int i = 0; i < args.length; ++i) {
145
switch (args[i]) {
146
case "-memUsage":
147
memUsageFactor = Integer.parseInt(args[i + 1]);
148
break;
149
case "-capacityVerPart":
150
capacityVerPart = Double.parseDouble(args[i + 1]);
151
break;
152
case "-appTimeout":
153
appTimeout = Integer.parseInt(args[i + 1]);
154
break;
155
default:
156
}
157
}
158
if (appTimeout == -1) {
159
throw new IllegalArgumentException("Specify -appTimeout.");
160
}
161
long endTimeCapacityVer = System.currentTimeMillis() + (long) (appTimeout / capacityVerPart * 60000);
162
GC.runTest(new StringInternSyncWithGC(memUsageFactor, endTimeCapacityVer), args);
163
}
164
165
protected List<String> getStringsToIntern() {
166
return stringsToIntern;
167
}
168
169
protected int getNumberOfThreads() {
170
return runParams.getNumberOfThreads();
171
}
172
173
protected RandomStringProducer getGarbageProducer() {
174
return gp;
175
}
176
177
protected int getMaxStringSize() {
178
return maxStringSize;
179
}
180
181
protected ExecutionController getExecController() {
182
return getExecutionController();
183
}
184
}
185
186