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