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