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