Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnistress002.java
41155 views
1
/*
2
* Copyright (c) 2007, 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
* jnistress002 is a class that sets up classes that do the work
26
* for the test.
27
*
28
* The Interrupter objects send interrupts to the JNIters.
29
* The GarbageGenerator objects generate garbage.
30
*
31
* sync[0] synchronizes the test cycles.
32
* sync[1] synchronizes access to exception counters.
33
* sync[2] synchronizes the cycle count update. It also insures that
34
* the interrupts do not interfere with the cycle count updates.
35
* This is because cycle count updates are used to define cycles.
36
*/
37
38
/*
39
* @test
40
* @key stress randomness
41
*
42
* @summary converted from VM testbase nsk/stress/jni/jnistress002.
43
* VM testbase keywords: [stress, quick, feature_283, nonconcurrent]
44
*
45
* @library /vmTestbase
46
* /test/lib
47
* @run main/othervm/native
48
* nsk.stress.jni.jnistress002
49
* -numTHREADer 20
50
* -threadInterval 200
51
* -numInterrupter 2
52
* -interruptInterval 500
53
* -numGarbage 80
54
* -garbageInterval 5
55
* -numIteration 260
56
*/
57
58
package nsk.stress.jni;
59
60
import nsk.share.Consts;
61
import nsk.share.Debug;
62
import nsk.share.test.StressOptions;
63
import jdk.test.lib.Utils;
64
65
import java.lang.reflect.Field;
66
import java.util.Random;
67
68
public class jnistress002 extends Thread {
69
70
/* Maximum number of iterations. Ignored if <= 0L */
71
static long numIteration = 0L;
72
/* Timeout */
73
static long timeOut;
74
/* Number of test class objects */
75
static int numJNIter = 1;
76
/* Time between JNI stressing by the threads under test */
77
/* (in milliseconds) */
78
static int jniInterval = 10000;
79
/* Number of interrupting threads */
80
static int numInterrupter = 1;
81
/* Time between interrupts in milliseconds */
82
static int interruptInterval = 100;
83
/* Number of garbage generating threads */
84
static int numGarbage = 1;
85
/* Time between garbage allocations in milliseconds */
86
static int garbageInterval = 100;
87
// The number of classes for creates via JNI
88
static int jniStringAllocSize = 10000;
89
90
private static StressOptions stressOptions;
91
92
public static void main(String[] argv) {
93
try {
94
int i = 0;
95
int nJNISync = 10;
96
jnistress002 dm = null;
97
boolean errArg = false;
98
99
stressOptions = new StressOptions(argv);
100
101
/* Process arguments */
102
while (!errArg && i < argv.length) {
103
/* Number of iterations. Ignored if <= 0. */
104
if (i < argv.length && argv[i].equals("-numIteration")) {
105
++i;
106
if (i < argv.length) {
107
try {
108
numIteration = Long.parseLong(argv[i++]);
109
} catch (NumberFormatException e) {
110
errArg = true;
111
}
112
}
113
} else if (i < argv.length && argv[i].equals("-numTHREADer")) {
114
++i;
115
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
116
try {
117
numJNIter = Integer.parseInt(argv[i++]);
118
} catch (NumberFormatException e) {
119
errArg = true;
120
}
121
if (numJNIter <= 0) errArg = true;
122
}
123
} else if (i < argv.length && argv[i].equals("-threadInterval")) {
124
++i;
125
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
126
try {
127
jniInterval = Integer.parseInt(argv[i++]);
128
} catch (NumberFormatException e) {
129
errArg = true;
130
}
131
}
132
} else if (i < argv.length && argv[i].equals("-numInterrupter")) {
133
++i;
134
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
135
try {
136
numInterrupter = Integer.parseInt(argv[i++]);
137
} catch (NumberFormatException e) {
138
errArg = true;
139
}
140
}
141
} else if (i < argv.length && argv[i].equals("-interruptInterval")) {
142
++i;
143
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
144
try {
145
interruptInterval = Integer.parseInt(argv[i++]);
146
} catch (NumberFormatException e) {
147
errArg = true;
148
}
149
}
150
} else if (i < argv.length && argv[i].equals("-numGarbage")) {
151
++i;
152
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
153
try {
154
numGarbage = Integer.parseInt(argv[i++]);
155
} catch (NumberFormatException e) {
156
errArg = true;
157
}
158
}
159
} else if (i < argv.length && argv[i].equals("-garbageInterval")) {
160
++i;
161
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
162
try {
163
garbageInterval = Integer.parseInt(argv[i++]);
164
} catch (NumberFormatException e) {
165
errArg = true;
166
}
167
}
168
} else if (i < argv.length && argv[i].equals("-jniStringAllocSize")) {
169
++i;
170
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
171
try {
172
jniStringAllocSize = Integer.parseInt(argv[i++]);
173
} catch (NumberFormatException e) {
174
errArg = true;
175
}
176
}
177
} else if (i < argv.length && argv[i].startsWith("-stress")) {
178
++i;
179
if (i < argv.length && Character.isDigit(argv[i].charAt(0))) {
180
++i;
181
}
182
} else System.out.println("Argument #" + i++ + " is incorrect");
183
}
184
185
numIteration *= stressOptions.getIterationsFactor();
186
numJNIter *= stressOptions.getThreadsFactor();
187
numInterrupter *= stressOptions.getThreadsFactor();
188
numGarbage *= stressOptions.getThreadsFactor();
189
timeOut = stressOptions.getTime() * 1000;
190
191
sync = new Synchronizer[10];
192
for (i = 0; i < nJNISync; i++)
193
sync[i] = new Synchronizer();
194
dm = new jnistress002(numIteration, numJNIter, jniInterval,
195
numInterrupter, interruptInterval, numGarbage, garbageInterval);
196
dm.start();
197
198
try {
199
dm.join(timeOut);
200
} catch (InterruptedException e) {
201
System.out.println("TESTER THREAD WAS INTERRUPTED");
202
System.exit(Consts.TEST_FAILED);
203
}
204
205
if (DEBUG) System.out.println("jnistress002::main(): halt!");
206
207
if (dm.isAlive()) {
208
System.out.println("TIME LIMIT EXCEEDED");
209
dm.halt();
210
if (DEBUG) System.out.println("jnistress002::main(): join!");
211
try {
212
dm.join(10000L);
213
} catch (InterruptedException e) {
214
System.out.println("TESTER THREAD WAS INTERRUPTED");
215
System.exit(Consts.TEST_FAILED);
216
}
217
} else {
218
System.out.println("TESTER THREAD FINISHED");
219
}
220
221
if (DEBUG) System.out.println("jnistress002::main(): zzzz...");
222
223
if (!JNIter002.passed())
224
System.exit(Consts.TEST_FAILED);
225
} catch (Throwable e) {
226
Debug.Fail(e);
227
}
228
}
229
230
jnistress002(
231
long iters,
232
int nJNI,
233
int jniInterval,
234
int nInter,
235
int iruptInterval,
236
int nGarb,
237
int garbInterval
238
) {
239
int i = 0;
240
nCycles = iters;
241
/* Should have at least one of nCycles>0 */
242
if (nCycles <= 0) nCycles = Long.MAX_VALUE;
243
jniter = new JNIter002[nJNI];
244
interval = jniInterval;
245
irupt = new Interrupter[nInter];
246
garb = new GarbageGenerator[nGarb];
247
for (i = 0; i < nJNI; i++)
248
jniter[i] = new JNIter002(sync);
249
for (i = 0; i < nInter; i++) {
250
irupt[i] = new Interrupter(jniter, sync);
251
irupt[i].setInterval(iruptInterval);
252
}
253
for (i = 0; i < nGarb; i++) {
254
garb[i] = new GarbageGenerator();
255
garb[i].setInterval(garbInterval);
256
257
}
258
259
// premtive special class loading
260
objectsJNI clStub = new objectsJNI(null, 0, 0, null, 0, 0);
261
}
262
263
public void run() {
264
try {
265
int i = 0;
266
long iCycle = 0L;
267
JNIter002.clearCount();
268
JNIter002.clearInterruptCount();
269
for (i = 0; i < jniter.length; i++)
270
jniter[i].start();
271
272
while (JNIter002.getCount() < jniter.length) {
273
try {
274
sleep(100);
275
} catch (InterruptedException e) {
276
}
277
}
278
JNIter002.clearCount();
279
// JNIter002.clearInterruptCount();
280
synchronized (sync[0]) {
281
sync[0].notifyAll();
282
}
283
284
for (i = 0; i < garb.length; i++)
285
garb[i].start();
286
for (i = 0; i < irupt.length - 1; i++)
287
irupt[i].start();
288
289
if (DEBUG) System.out.println("Cycles=" + nCycles);
290
for (iCycle = 0; iCycle < nCycles && !done && JNIter002.passed(); iCycle++) {
291
System.out.print("Cycle: " + iCycle);
292
try {
293
sleep(interval);
294
} catch (InterruptedException e) {
295
}
296
synchronized (sync[1]) {
297
System.out.println(" Interrupt count=" +
298
JNIter002.getInterruptCount());
299
}
300
JNIter002.clearCount();
301
synchronized (sync[0]) {
302
sync[0].notifyAll();
303
}
304
int n = 0;
305
for (i = 0; i < jniter.length; i++)
306
if (jniter[i].finished()) n++;
307
if (n == jniter.length) break;
308
}
309
if (JNIter002.passed()) {
310
System.out.println("JNI TEST PASSED");
311
} else {
312
System.out.println("JNI TEST FAILED");
313
}
314
for (i = 0; i < irupt.length; i++)
315
irupt[i].halt();
316
for (i = 0; i < garb.length; i++)
317
garb[i].halt();
318
for (i = 0; i < jniter.length; i++)
319
jniter[i].halt();
320
/* Flush any waiters */
321
if (DEBUG) System.out.println("jnistress002::run(): before sync[0]");
322
synchronized (sync[0]) {
323
sync[0].notifyAll();
324
}
325
if (DEBUG) System.out.println("jnistress002::run(): after sync[0]");
326
for (i = 0; i < irupt.length; i++) {
327
try {
328
irupt[i].join();
329
} catch (InterruptedException e) {
330
}
331
}
332
if (DEBUG) System.out.println("jnistress002::run(): X");
333
for (i = 0; i < garb.length; i++) {
334
try {
335
garb[i].join();
336
} catch (InterruptedException e) {
337
}
338
}
339
if (DEBUG) System.out.println("jnistress002::run(): Y");
340
System.out.println("jniter.length is " + jniter.length);
341
for (i = 0; i < jniter.length; i++) {
342
try {
343
if (jniter[i].isAlive()) {
344
jniter[i].join();
345
}
346
} catch (InterruptedException e) {
347
}
348
}
349
if (DEBUG) System.out.println("jnistress002::run(): Z");
350
} catch (Throwable e) {
351
Debug.Fail(e);
352
}
353
}
354
355
public void halt() {
356
done = true;
357
}
358
359
public boolean finished() {
360
return done;
361
}
362
363
long nCycles = 0;
364
JNIter002[] jniter;
365
static Synchronizer[] sync;
366
private int interval = 100;
367
Interrupter[] irupt;
368
GarbageGenerator[] garb;
369
private boolean done = false;
370
final private static boolean DEBUG = false;
371
}
372
373
class objectsJNI {
374
public String instName;
375
public int i;
376
public long l;
377
public char[] c;
378
public float f;
379
public double d;
380
381
public objectsJNI(String name,
382
int intgr,
383
long lng,
384
char[] charr,
385
float flt,
386
double dbl
387
) {
388
instName = name;
389
i = intgr;
390
l = lng;
391
f = flt;
392
d = dbl;
393
c = charr;
394
}
395
396
public boolean equals(Object o) {
397
398
if (this.getClass() != o.getClass())
399
return false;
400
401
Field[] fields = o.getClass().getFields();
402
try {
403
for (int i = 0; i < fields.length; i++) {
404
if (fields[i].get(o) instanceof char[]) {
405
for (int j = 0; j < ((char[]) fields[i].get(this)).length; j++)
406
if (((char[]) fields[i].get(this))[j] !=
407
((char[]) fields[i].get(o))[j]) {
408
System.out.println(
409
"Char arrays have difference in " + j);
410
return false;
411
}
412
} else if (!fields[i].get(this).equals(fields[i].get(o))) {
413
System.out.println(
414
"The fields No. " + i + " are different");
415
return false;
416
}
417
}
418
} catch (Exception e) {
419
System.out.println("Error : " + e);
420
}
421
;
422
return true;
423
}
424
}
425
426
class JNIter002 extends Thread {
427
428
// The native method for testing JNI Object's calls
429
public native objectsJNI[] jniobjects(String s, int i, long l,
430
char[] c, float f, double d);
431
432
static {
433
System.loadLibrary("jnistress002");
434
}
435
436
Random myRandom = new Random(Utils.getRandomInstance().nextLong());
437
438
public JNIter002(Synchronizer[] aSync) {
439
sync = aSync;
440
}
441
442
public void run() {
443
try {
444
String s;
445
int i;
446
long l;
447
char[] c;
448
float f;
449
double d;
450
int iter = 0;
451
452
/* Synchronize start of work */
453
incCount();
454
synchronized (sync[0]) {
455
try {
456
sync[0].wait();
457
} catch (InterruptedException e) {
458
}
459
}
460
while (!done && pass) {
461
try {
462
/* Synchronized the JNI stressing */
463
synchronized (sync[2]) {
464
incCount();
465
}
466
synchronized (sync[0]) {
467
try {
468
sync[0].wait();
469
} catch (InterruptedException e) {
470
synchronized (sync[1]) {
471
JNIter002.incInterruptCount();
472
}
473
}
474
}
475
synchronized (sync[0]) {
476
i = myRandom.nextInt(Integer.MAX_VALUE);
477
l = myRandom.nextLong();
478
f = myRandom.nextFloat();
479
d = myRandom.nextDouble();
480
s = getName();
481
c = s.toCharArray();
482
objectsJNI test = new objectsJNI(s, i, l, c, f, d);
483
Object[] testJNI = jniobjects(s, i, l, c, f, d);
484
485
for (int j = 0; j < testJNI.length; j++)
486
if (!testJNI[j].equals(test)) {
487
System.out.println("Objects are different");
488
fieldprint("JNI object", testJNI[j]);
489
fieldprint("Java object", test);
490
pass = false;
491
}
492
}
493
if (DEBUG) System.out.println("We have " + activeCount() +
494
" threads now.");
495
synchronized (this) {
496
try {
497
wait(1L);
498
} catch (InterruptedException e) {
499
throw new InterruptedException();
500
}
501
}
502
} catch (InterruptedException e) {
503
synchronized (sync[1]) {
504
JNIter002.incInterruptCount();
505
}
506
}
507
iter++;
508
iter = iter % CASECOUNT;
509
}
510
if (DEBUG) System.out.println("JNITer::run(): done=" + done);
511
done = true;
512
if (DEBUG) System.out.println("JNITer::run(): pass=" + JNIter002.pass);
513
if (DEBUG) System.out.println("JNIter002::run(): done");
514
} catch (Throwable e) {
515
Debug.Fail(e);
516
}
517
}
518
519
private synchronized static void incCount() {
520
count++;
521
}
522
523
public static int getCount() {
524
return count;
525
}
526
527
public synchronized static void clearCount() {
528
count = 0;
529
}
530
531
private synchronized static void incInterruptCount() {
532
interruptCount++;
533
}
534
535
public static int getInterruptCount() {
536
return interruptCount;
537
}
538
539
public synchronized static void clearInterruptCount() {
540
interruptCount = 0;
541
}
542
543
public static void halt() {
544
done = true;
545
}
546
547
public boolean finished() {
548
return done;
549
}
550
551
public static boolean passed() {
552
return pass;
553
}
554
555
Synchronizer[] sync;
556
private static int count = 0;
557
private static int interruptCount = 0;
558
private static boolean done = false;
559
private static boolean pass = true;
560
final private static int CASECOUNT = 2;
561
final private static boolean DEBUG = false;
562
563
static void fieldprint(String s, Object obj) {
564
Field[] fields = obj.getClass().getFields();
565
System.out.println(s);
566
try {
567
for (int i = 0; i < fields.length; i++) {
568
if (fields[i].get(obj) instanceof java.lang.String)
569
System.out.println(
570
fields[i] + " = \"" + fields[i].get(obj) + "\"");
571
else if (fields[i].get(obj) instanceof char[])
572
System.out.println(fields[i] + " = \"" +
573
new String((char[]) fields[i].get(obj)) + "\"");
574
else
575
System.out.println(
576
fields[i] + " = " + fields[i].get(obj));
577
}
578
} catch (Exception e) {
579
System.out.println("Error: " + e);
580
}
581
}
582
}
583
584