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