Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace010.java
41155 views
1
/*
2
* Copyright (c) 2003, 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
27
*
28
* @summary converted from VM testbase nsk/stress/strace/strace010.
29
* VM testbase keywords: [stress, strace]
30
* VM testbase readme:
31
* DESCRIPTION
32
* The test runs many threads, that recursively invoke a pure java method.
33
* After arriving at defined depth of recursion, each thread is blocked
34
* on entering a monitor. Then the test calls java.lang.Thread.getStackTrace()
35
* and java.lang.Thread.getAllStackTraces() methods and checks their results.
36
* The test fails if:
37
* - amount of stack trace elements and stack trace elements themselves are
38
* the same for both methods;
39
* - there is at least one element corresponding to invocation of unexpected
40
* method. Expected methods are Thread.sleep(), Thread.run() and the
41
* recursive method.
42
*
43
* @library /vmTestbase
44
* /test/lib
45
* @run main/othervm nsk.stress.strace.strace010
46
*/
47
48
package nsk.stress.strace;
49
50
import nsk.share.ArgumentParser;
51
import nsk.share.Failure;
52
import nsk.share.Log;
53
54
import java.io.PrintStream;
55
import java.util.Map;
56
57
/**
58
* The test runs <code>THRD_COUNT</code> instances of <code>strace010Thread</code>,
59
* that recursively invoke a pure java method. After arriving at defined depth
60
* <code>DEPTH</code> of recursion, each thread is blocked on entering a monitor.
61
* Then the test calls <code>java.lang.Thread.getStackTrace()</code> and
62
* <code>java.lang.Thread.getAllStackTraces()</code> methods and checks their results.
63
* <p>
64
* <p>It is expected that these methods return the same stack traces. Each stack frame
65
* for both stack traces must be corresponded to invocation of one of the methods
66
* defined by the <code>EXPECTED_METHODS</code> array.</p>
67
*/
68
public class strace010 {
69
70
static final int DEPTH = 500;
71
static final int THRD_COUNT = 100;
72
static final String[] EXPECTED_METHODS = {
73
"java.lang.Thread.sleep",
74
"nsk.stress.strace.strace010Thread.run",
75
"nsk.stress.strace.strace010Thread.recursiveMethod"
76
};
77
78
79
static PrintStream out;
80
static long waitTime = 2;
81
82
static Object lockedObject = new Object();
83
static volatile boolean isLocked = false;
84
85
volatile int achivedCount = 0;
86
strace010Thread[] threads;
87
static Log log;
88
89
public static void main(String[] args) {
90
out = System.out;
91
int exitCode = run(args);
92
System.exit(exitCode + 95);
93
}
94
95
public static int run(String[] args) {
96
ArgumentParser argHandler = new ArgumentParser(args);
97
log = new Log(out, argHandler);
98
waitTime = argHandler.getWaitTime() * 60000;
99
100
strace010 test = new strace010();
101
boolean res = true;
102
103
test.startThreads();
104
105
res = test.makeSnapshot();
106
107
test.finishThreads();
108
109
if (!res) {
110
complain("***>>>Test failed<<<***");
111
return 2;
112
}
113
114
display(">>>Test passed<<<");
115
return 0;
116
}
117
118
void startThreads() {
119
threads = new strace010Thread[THRD_COUNT];
120
achivedCount = 0;
121
122
String tmp_name;
123
display("starting threads...");
124
for (int i = 0; i < THRD_COUNT; i++) {
125
tmp_name = "strace010Thread" + Integer.toString(i);
126
threads[i] = new strace010Thread(this, tmp_name);
127
threads[i].start();
128
}
129
130
waitFor("the defined recursion depth ...");
131
}
132
133
void waitFor(String msg) {
134
if (msg.length() > 0)
135
display("waiting for " + msg);
136
137
while (achivedCount < THRD_COUNT) {
138
try {
139
Thread.sleep(1);
140
} catch (InterruptedException e) {
141
complain("" + e);
142
}
143
}
144
achivedCount = 0;
145
}
146
147
boolean makeSnapshot() {
148
149
Map traces = null;
150
int count = 0;
151
StackTraceElement[][] elements = null;
152
153
display("locking object...");
154
synchronized (strace010.lockedObject) {
155
isLocked = true;
156
synchronized (this) {
157
notifyAll();
158
}
159
Thread.currentThread().yield();
160
waitFor("");
161
162
display("making all threads snapshots...");
163
traces = Thread.getAllStackTraces();
164
count = ((StackTraceElement[]) traces.get(threads[0])).length;
165
166
display("making snapshots of each thread...");
167
elements = new StackTraceElement[THRD_COUNT][];
168
for (int i = 0; i < THRD_COUNT; i++) {
169
elements[i] = threads[i].getStackTrace();
170
}
171
}
172
display("object unlocked");
173
174
display("");
175
176
display("checking lengths of stack traces...");
177
StackTraceElement[] all;
178
for (int i = 1; i < THRD_COUNT; i++) {
179
all = (StackTraceElement[]) traces.get(threads[i]);
180
int k = all.length;
181
if (count - k > 2) {
182
complain("wrong lengths of stack traces:\n\t"
183
+ threads[0].getName() + ": " + count
184
+ "\t"
185
+ threads[i].getName() + ": " + k);
186
return false;
187
}
188
}
189
190
display("checking stack traces...");
191
boolean res = true;
192
for (int i = 0; i < THRD_COUNT; i++) {
193
all = (StackTraceElement[]) traces.get(threads[i]);
194
if (!checkTraces(threads[i].getName(), elements[i], all)) {
195
res = false;
196
}
197
}
198
return res;
199
}
200
201
boolean checkTraces(String threadName, StackTraceElement[] threadSnap,
202
StackTraceElement[] allSnap) {
203
204
int checkedLength = threadSnap.length < allSnap.length ?
205
threadSnap.length : allSnap.length;
206
boolean res = true;
207
208
for (int j = 0; j < checkedLength; j++) {
209
if (!checkElement(threadSnap[j])) {
210
complain("Unexpected " + j + "-element:");
211
complain("\tmethod name: " + threadSnap[j].getMethodName());
212
complain("\tclass name: " + threadSnap[j].getClassName());
213
if (threadSnap[j].isNativeMethod()) {
214
complain("\tline number: (native method)");
215
} else {
216
complain("\tline number: " + threadSnap[j].getLineNumber());
217
complain("\tfile name: " + threadSnap[j].getFileName());
218
}
219
complain("");
220
res = false;
221
}
222
}
223
return res;
224
}
225
226
boolean checkElement(StackTraceElement element) {
227
String name = element.getClassName() + "." + element.getMethodName();
228
for (int i = 0; i < EXPECTED_METHODS.length; i++) {
229
if (EXPECTED_METHODS[i].compareTo(name) == 0)
230
return true;
231
}
232
return false;
233
}
234
235
void finishThreads() {
236
try {
237
for (int i = 0; i < threads.length; i++) {
238
if (threads[i].isAlive()) {
239
display("waiting for finish " + threads[i].getName());
240
threads[i].join(waitTime);
241
}
242
}
243
} catch (InterruptedException e) {
244
complain("" + e);
245
}
246
isLocked = false;
247
}
248
249
static void display(String message) {
250
log.display(message);
251
}
252
253
static void complain(String message) {
254
log.complain(message);
255
}
256
257
}
258
259
class strace010Thread extends Thread {
260
261
private int currentDepth = 0;
262
263
static int[] arr = new int[1000];
264
strace010 test;
265
266
strace010Thread(strace010 test, String name) {
267
this.test = test;
268
setName(name);
269
}
270
271
public void run() {
272
try {
273
recursiveMethod(arr);
274
} catch (Throwable throwable) {
275
System.err.println("# ERROR: " + getName() + ": " + throwable);
276
System.exit(1);
277
}
278
}
279
280
void recursiveMethod(int[] arr) {
281
currentDepth++;
282
283
if (strace010.DEPTH - currentDepth > 0) {
284
recursiveMethod(arr);
285
}
286
287
if (strace010.DEPTH == currentDepth) {
288
289
synchronized (test) {
290
test.achivedCount++;
291
}
292
293
int alltime = 0;
294
while (!test.isLocked) {
295
synchronized (test) {
296
try {
297
test.wait(1);
298
alltime++;
299
} catch (InterruptedException e) {
300
strace010.complain("" + e);
301
}
302
if (alltime > strace010.waitTime) {
303
throw new Failure("out of wait time");
304
}
305
}
306
}
307
308
strace010.display(getName() + ">entering to monitor");
309
synchronized (test) {
310
test.achivedCount++;
311
}
312
synchronized (strace010.lockedObject) {
313
strace010.display(getName() + ">exiting from monitor");
314
}
315
}
316
317
currentDepth--;
318
}
319
}
320
321