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