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