Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/StressTest.java
41159 views
1
/*
2
* Copyright (c) 2013, 2021, 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
*
27
* @modules java.base/jdk.internal.org.objectweb.asm:+open java.base/jdk.internal.org.objectweb.asm.util:+open
28
* @library /vmTestbase /test/lib
29
*
30
* @comment build retransform.jar in current dir
31
* @run driver vm.runtime.defmeth.shared.BuildJar
32
*
33
* @run driver jdk.test.lib.FileInstaller . .
34
*
35
* @run main/othervm/native
36
* -agentlib:redefineClasses
37
* -javaagent:retransform.jar
38
* vm.runtime.defmeth.StressTest
39
*/
40
package vm.runtime.defmeth;
41
42
import java.util.ArrayList;
43
import java.util.Arrays;
44
import java.util.List;
45
import java.util.Random;
46
47
import nsk.share.TestFailure;
48
import nsk.share.test.StressOptions;
49
import nsk.share.test.Stresser;
50
import vm.runtime.defmeth.shared.Constants;
51
import vm.runtime.defmeth.shared.DefMethTest;
52
import vm.runtime.defmeth.shared.ExecutionMode;
53
import vm.share.options.Option;
54
import vm.share.options.OptionSupport;
55
import vm.share.options.Options;
56
import jdk.test.lib.Utils;
57
58
import static jdk.internal.org.objectweb.asm.Opcodes.*;
59
import static vm.runtime.defmeth.shared.DefMethTest.MAX_MAJOR_VER;
60
import static vm.runtime.defmeth.shared.DefMethTest.MIN_MAJOR_VER;
61
62
/*
63
* Stress test for default methods implementation.
64
*
65
* Stress scenario is the following:
66
* - in multiple threads ...
67
* - ... continuously run random tests ...
68
* - ... in random configuration ...
69
* - ... until predefined period of time is over...
70
* - ... or any failures occured.
71
*/
72
public class StressTest implements Runnable {
73
@Options
74
private StressOptions opts = new StressOptions();
75
76
@Option(name="seed", default_value="0", description="force deterministic behavior")
77
private long seed;
78
79
@Option(name="noredefine", default_value="false", description="skip scenarios w/ class redefinition")
80
private boolean noRedefine;
81
82
@Option(name="ignoreTestFailures", default_value="false", description="ignore failures of the executed tests")
83
private boolean ignoreTestFailures;
84
85
class Worker extends Thread {
86
private final Random rand;
87
88
private volatile DefMethTest failedTest;
89
private Throwable reason;
90
private volatile long executedTests = 0;
91
92
public Worker(String id, long seed) {
93
setName(id);
94
this.rand = new Random(seed);
95
}
96
97
@Override
98
public void run() {
99
while (!Thread.interrupted()) {
100
int idx = rand.nextInt(testlist.size());
101
DefMethTest test = testlist.get(idx);
102
try {
103
test.run();
104
executedTests++;
105
if (test.isFailed()) {
106
throw new TestFailure(test.toString());
107
}
108
} catch (Throwable e) {
109
if (!ignoreTestFailures) {
110
failedTest = test;
111
reason = e;
112
break;
113
}
114
}
115
}
116
}
117
118
public boolean isFailed() { return failedTest != null; }
119
public Throwable getReason() { return reason; }
120
public DefMethTest getFailedTest() { return failedTest; }
121
public long getExecutedTests() { return executedTests; }
122
}
123
124
private List<DefMethTest> testlist;
125
126
private Worker[] workers;
127
128
Stresser stresser;
129
130
public static void main(String[] args) {
131
StressTest test = new StressTest();
132
OptionSupport.setupAndRun(test, args);
133
}
134
135
@Override
136
public void run() {
137
configureTests();
138
startWorkers();
139
140
stresser = new Stresser(opts);
141
try {
142
stresser.start(0);
143
while (workersAlive() && stresser.continueExecution()) {
144
printStats();
145
146
try {
147
Thread.sleep(1000);
148
} catch (InterruptedException ex) {}
149
}
150
} finally {
151
interruptWorkers();
152
joinWorkers();
153
154
stresser.finish();
155
}
156
}
157
158
private void configureTests() {
159
int[] majorVerValues = new int[MAX_MAJOR_VER - MIN_MAJOR_VER + 1];
160
for (int i = 0; i< majorVerValues.length; i++) {
161
majorVerValues[i] = MIN_MAJOR_VER + i;
162
}
163
164
int[] flagsValues = new int[] {0, ACC_SYNCHRONIZED};
165
166
boolean[] doRedefineValues;
167
if (noRedefine) {
168
doRedefineValues = new boolean[] { false };
169
} else {
170
doRedefineValues = new boolean[] { false, true };
171
}
172
173
// Upper limit for test count
174
int testCount = DefMethTest.getTests().size() * majorVerValues.length
175
* flagsValues.length * doRedefineValues.length;
176
177
testlist = new ArrayList<>(testCount);
178
179
// Enumerate all tests in all possible modes
180
for (Class<? extends DefMethTest> testClass : DefMethTest.getTests()) {
181
for (ExecutionMode mode : ExecutionMode.values()) {
182
// Skip REDEFINITION execmode, the top README file indicates that it isn't a
183
// valid execution mode and there's also no code supporting this in the test generator.
184
if ("REDEFINITION".equals(mode.toString())) {
185
continue;
186
}
187
188
for (int majorVer : majorVerValues) {
189
for (int flags : flagsValues ) {
190
for (boolean redefine : doRedefineValues) {
191
// RedefineTest isn't applicable to reflection-based execution scenario (REDEFINE & INVOKE_WITH_ARGS)
192
if (testClass == RedefineTest.class && mode.isReflectionBased()) {
193
continue;
194
}
195
196
// Only run the RedefineTest tests when redefining
197
if (!redefine && testClass == RedefineTest.class) {
198
continue;
199
}
200
201
try {
202
DefMethTest test = testClass.getDeclaredConstructor().newInstance();
203
204
OptionSupport.setup(test, new String[] {
205
"-execMode", mode.toString(),
206
"-ver", Integer.toString(majorVer),
207
"-flags", Integer.toString(flags),
208
"-redefine", Boolean.toString(redefine),
209
"-silent",
210
"-failfast"});
211
212
testlist.add(test);
213
} catch (ReflectiveOperationException ex) {
214
throw new TestFailure(ex);
215
}
216
}
217
}
218
}
219
}
220
}
221
222
System.out.printf("Testlist size: %d\n", testlist.size());
223
}
224
225
private void startWorkers() {
226
Random rand;
227
if (seed == 0) {
228
seed = Utils.SEED;
229
}
230
231
System.out.printf("Seed: %d\n", seed);
232
rand = new Random(seed);
233
234
int threadsCount = opts.getThreadsFactor();
235
if (threadsCount == 1) {
236
threadsCount = 5;
237
}
238
239
workers = new Worker[threadsCount];
240
241
System.out.printf("Spawning %d workers...\n", workers.length);
242
243
for (int i = 0; i < workers.length; i++) {
244
workers[i] = new Worker(
245
String.format("Worker #%d/%d", i+1, workers.length),
246
rand.nextLong());
247
}
248
249
for (Worker worker : workers) {
250
worker.start();
251
}
252
}
253
254
private void interruptWorkers() {
255
for (Worker worker : workers) {
256
worker.interrupt();
257
}
258
}
259
260
private void joinWorkers() {
261
boolean isFailed = false;
262
263
for (Worker worker : workers) {
264
while (worker.isAlive()) {
265
try {
266
worker.join();
267
} catch (InterruptedException e) {}
268
}
269
270
System.out.printf("%s: %s (executed: %d)\n",
271
worker.getName(),
272
worker.isFailed() ? "FAILED: " + worker.getFailedTest() : "PASSED",
273
worker.getExecutedTests());
274
275
if (worker.isFailed()) {
276
if (Constants.PRINT_STACK_TRACE) {
277
worker.getReason().printStackTrace();
278
}
279
280
isFailed = true;
281
}
282
}
283
284
if (isFailed) {
285
throw new TestFailure("Some of the worker threads failed.");
286
}
287
}
288
289
private boolean workersAlive() {
290
for (Worker worker : workers) {
291
if (!worker.isAlive()) {
292
return false;
293
}
294
}
295
296
return true;
297
}
298
299
private void printStats() {
300
long[] counts = new long[workers.length];
301
for (int i = 0; i < counts.length; i++) {
302
counts[i] = workers[i].executedTests;
303
}
304
305
StringBuilder msg = new StringBuilder();
306
msg.append(stresser.getTimeLeft() / 1000).append("s left: ");
307
msg.append(Arrays.toString(counts));
308
309
System.out.println(msg);
310
}
311
}
312
313