Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/strace/strace002.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
/*
26
* @test
27
* @key stress
28
*
29
* @summary converted from VM testbase nsk/stress/strace/strace002.
30
* VM testbase keywords: [stress, quick, strace]
31
* VM testbase readme:
32
* DESCRIPTION
33
* The test checks up java.lang.Thread.getAllStackTraces() method for many
34
* threads, that recursively invoke a pure java method in running mode
35
* ("alive" stack).
36
* The test fails if:
37
* - amount of stack trace elements is more than depth of recursion plus
38
* four elements corresponding to invocations of Thread.run(), Thread.wait(),
39
* Thread.exit(), Thread.yield() and ThreadGroup.remove() methods;
40
* - there is at least one element corresponding to invocation of unexpected
41
* method.
42
* This test is almost the same as nsk.stress.strace.strace001 except for
43
* checking is performed for java.lang.Thread.getAllStackTraces() method.
44
*
45
* @library /vmTestbase
46
* /test/lib
47
* @run main/othervm nsk.stress.strace.strace002
48
*/
49
50
51
package nsk.stress.strace;
52
53
import nsk.share.ArgumentParser;
54
import nsk.share.Failure;
55
import nsk.share.Log;
56
57
import java.io.PrintStream;
58
import java.util.Map;
59
60
/**
61
* The test check up <code>java.lang.Thread.getAllStackTraces()</code> method for many
62
* threads, that recursively invoke a pure java method in running mode ("alive" stack).
63
* <p>
64
* <p>The test creates <code>THRD_COUNT</code> instances of <code>strace002Thread</code>
65
* class, tries to get their stack traces and checks up that returned array contains
66
* correct stack frames. Each stack frame must be corresponded to one of the following
67
* methods defined by the <code>EXPECTED_METHODS</code> array.</p>
68
* <p>These checking are performed <code>REPEAT_COUNT</code> times.</p>
69
*/
70
public class strace002 {
71
72
static final int DEPTH = 200;
73
static final int THRD_COUNT = 100;
74
static final int REPEAT_COUNT = 10;
75
static final String[] EXPECTED_METHODS = {
76
"java.lang.System.arraycopy",
77
"java.lang.Object.wait",
78
"java.lang.Thread.exit",
79
"java.lang.Thread.yield",
80
"java.lang.ThreadGroup.remove",
81
"java.lang.ThreadGroup.threadTerminated",
82
"nsk.stress.strace.strace002Thread.run",
83
"nsk.stress.strace.strace002Thread.recursiveMethod"
84
};
85
86
87
static volatile boolean isLocked = false;
88
static PrintStream out;
89
static long waitTime = 2;
90
91
static Object waitStart = new Object();
92
93
static strace002Thread[] threads;
94
static StackTraceElement[][] snapshots = new StackTraceElement[THRD_COUNT][];
95
static Log log;
96
97
public static void main(String[] args) {
98
out = System.out;
99
int exitCode = run(args);
100
System.exit(exitCode + 95);
101
}
102
103
volatile int achivedCount = 0;
104
105
public static int run(String[] args) {
106
107
ArgumentParser argHandler = new ArgumentParser(args);
108
log = new Log(out, argHandler);
109
waitTime = argHandler.getWaitTime() * 60000;
110
111
strace002 test = new strace002();
112
boolean res = true;
113
114
for (int j = 0; j < REPEAT_COUNT; j++) {
115
test.startThreads();
116
117
if (!test.makeSnapshot(j + 1)) res = false;
118
119
display("waiting for threads finished\n");
120
test.finishThreads();
121
}
122
123
if (!res) {
124
complain("***>>>Test failed<<<***");
125
return 2;
126
}
127
128
return 0;
129
}
130
131
void startThreads() {
132
threads = new strace002Thread[THRD_COUNT];
133
achivedCount = 0;
134
135
String tmp_name;
136
for (int i = 0; i < THRD_COUNT; i++) {
137
tmp_name = "strace002Thread" + Integer.toString(i);
138
threads[i] = new strace002Thread(this, tmp_name);
139
}
140
141
for (int i = 0; i < THRD_COUNT; i++) {
142
threads[i].start();
143
}
144
145
waitFor("all threads started ...");
146
synchronized (waitStart) {
147
isLocked = true;
148
waitStart.notifyAll();
149
}
150
try {
151
Thread.yield();
152
Thread.sleep(1);
153
} catch (InterruptedException e) {
154
complain("" + e);
155
}
156
}
157
158
void waitFor(String msg) {
159
if (msg.length() > 0)
160
display("waiting for " + msg);
161
162
while (achivedCount < THRD_COUNT) {
163
try {
164
Thread.sleep(1);
165
} catch (InterruptedException e) {
166
complain("" + e);
167
}
168
}
169
achivedCount = 0;
170
}
171
172
boolean makeSnapshot(int repeat_number) {
173
174
Map traces = Thread.getAllStackTraces();
175
for (int i = 0; i < threads.length; i++) {
176
snapshots[i] = (StackTraceElement[]) traces.get(threads[i]);
177
}
178
179
return checkTraces(repeat_number);
180
}
181
182
boolean checkTraces(int repeat_number) {
183
StackTraceElement[] elements;
184
185
boolean res = true;
186
display(">>> snapshot " + repeat_number);
187
int expectedCount = DEPTH + 1;
188
189
for (int i = 0; i < threads.length; i++) {
190
elements = snapshots[i];
191
192
if (elements == null)
193
continue;
194
195
if (elements.length == 0)
196
continue;
197
198
if (elements.length > 3) {
199
display("\tchecking " + threads[i].getName()
200
+ "(trace elements: " + elements.length + ")");
201
}
202
203
if (elements.length > expectedCount) {
204
complain(threads[i].getName() + ">Contains more then " +
205
+expectedCount + " elements");
206
}
207
208
for (int j = 0; j < elements.length; j++) {
209
if (!checkElement(elements[j])) {
210
complain(threads[i].getName() + ">Unexpected method name: "
211
+ elements[j].getMethodName());
212
complain("\tat " + j + " position");
213
if (elements[j].isNativeMethod()) {
214
complain("\tline number: (native method)");
215
complain("\tclass name: " + elements[j].getClassName());
216
} else {
217
complain("\tline number: " + elements[j].getLineNumber());
218
complain("\tclass name: " + elements[j].getClassName());
219
complain("\tfile name: " + elements[j].getFileName());
220
}
221
res = false;
222
}
223
}
224
}
225
return res;
226
}
227
228
boolean checkElement(StackTraceElement element) {
229
String name = element.getClassName() + "." + element.getMethodName();
230
for (int i = 0; i < EXPECTED_METHODS.length; i++) {
231
if (EXPECTED_METHODS[i].compareTo(name) == 0)
232
return true;
233
}
234
return false;
235
}
236
237
void finishThreads() {
238
try {
239
for (int i = 0; i < threads.length; i++) {
240
if (threads[i].isAlive())
241
threads[i].join(waitTime / THRD_COUNT);
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
260
class strace002Thread extends Thread {
261
262
private int currentDepth = 0;
263
264
strace002 test;
265
266
strace002Thread(strace002 test, String name) {
267
this.test = test;
268
setName(name);
269
}
270
271
public void run() {
272
try {
273
recursiveMethod();
274
} catch (Throwable throwable) {
275
System.err.println("# ERROR: " + getName() + ": " + throwable);
276
System.exit(1);
277
}
278
}
279
280
void recursiveMethod() {
281
282
currentDepth++;
283
284
if (currentDepth == 1) {
285
synchronized (test) {
286
test.achivedCount++;
287
}
288
289
int alltime = 0;
290
while (!test.isLocked) {
291
synchronized (test) {
292
try {
293
test.wait(1);
294
alltime++;
295
} catch (InterruptedException e) {
296
strace002.complain("" + e);
297
}
298
if (alltime > strace002.waitTime) {
299
throw new Failure("out of wait time");
300
}
301
}
302
}
303
}
304
305
if (strace002.DEPTH - currentDepth > 0) {
306
Thread.yield();
307
recursiveMethod();
308
}
309
310
currentDepth--;
311
}
312
}
313
314