Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/jdk/jfr/api/consumer/TestRecordedFullStackTrace.java
42281 views
1
/*
2
* Copyright (c) 2013, 2018, 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
package jdk.jfr.api.consumer;
24
25
import java.nio.file.Path;
26
import java.time.Duration;
27
import java.util.ArrayList;
28
import java.util.List;
29
30
import jdk.jfr.Recording;
31
import jdk.jfr.consumer.RecordedEvent;
32
import jdk.jfr.consumer.RecordedFrame;
33
import jdk.jfr.consumer.RecordedStackTrace;
34
import jdk.jfr.consumer.RecordingFile;
35
import jdk.test.lib.Asserts;
36
import jdk.test.lib.Utils;
37
import jdk.test.lib.jfr.EventNames;
38
import jdk.test.lib.jfr.Events;
39
import jdk.test.lib.jfr.RecurseThread;
40
41
/**
42
* @test
43
* @key jfr
44
* @requires vm.hasJFR
45
* @library /test/lib
46
* @run main/othervm jdk.jfr.api.consumer.TestRecordedFullStackTrace
47
*/
48
public class TestRecordedFullStackTrace {
49
50
private final static String EVENT_NAME = EventNames.ExecutionSample;
51
private final static int MAX_DEPTH = 64; // currently hardcoded in jvm
52
53
public static void main(String[] args) throws Throwable {
54
55
RecurseThread[] threads = new RecurseThread[3];
56
for (int i = 0; i < threads.length; ++i) {
57
int depth = MAX_DEPTH - 1 + i;
58
threads[i] = new RecurseThread(depth);
59
threads[i].setName("recursethread-" + depth);
60
threads[i].start();
61
}
62
63
for (RecurseThread thread : threads) {
64
while (!thread.isInRunLoop()) {
65
Thread.sleep(20);
66
}
67
}
68
69
assertStackTraces(threads);
70
71
for (RecurseThread thread : threads) {
72
thread.quit();
73
thread.join();
74
}
75
}
76
77
private static void assertStackTraces(RecurseThread[] threads) throws Throwable {
78
Path path = null;
79
do {
80
try (Recording recording = new Recording()) {
81
recording.enable(EVENT_NAME).withPeriod(Duration.ofMillis(1));
82
recording.start();
83
Thread.sleep(50);
84
recording.stop();
85
// Dump the recording to a file
86
path = Utils.createTempFile("execution-stack-trace", ".jfr");
87
System.out.println("Dumping to " + path);
88
recording.dump(path);
89
}
90
} while (!hasValidStackTraces(path, threads));
91
}
92
93
private static boolean hasValidStackTraces(Path path, RecurseThread[] threads) throws Throwable {
94
boolean[] isEventFound = new boolean[threads.length];
95
96
for (RecordedEvent event : RecordingFile.readAllEvents(path)) {
97
//System.out.println("Event: " + event);
98
String threadName = Events.assertField(event, "sampledThread.javaName").getValue();
99
long threadId = Events.assertField(event, "sampledThread.javaThreadId").getValue();
100
101
for (int threadIndex = 0; threadIndex < threads.length; ++threadIndex) {
102
RecurseThread currThread = threads[threadIndex];
103
if (threadId == currThread.getId()) {
104
Asserts.assertEquals(threadName, currThread.getName(), "Wrong thread name, deptth=" + currThread.totalDepth);
105
if ("recurseEnd".equals(getTopMethodName(event))) {
106
isEventFound[threadIndex] = true;
107
checkEvent(event, currThread.totalDepth);
108
break;
109
}
110
}
111
}
112
}
113
114
for (int i = 0; i < threads.length; ++i) {
115
String msg = "threadIndex=%d, recurseDepth=%d, isEventFound=%b%n";
116
System.out.printf(msg, i, threads[i].totalDepth, isEventFound[i]);
117
}
118
for (int i = 0; i < threads.length; ++i) {
119
if (!isEventFound[i]) {
120
// no assertion, let's retry.
121
// Could be race condition, i.e safe point during Thread.sleep
122
System.out.println("Falied to validate all threads, will retry.");
123
return false;
124
}
125
}
126
return true;
127
}
128
129
public static String getTopMethodName(RecordedEvent event) {
130
List<RecordedFrame> frames = event.getStackTrace().getFrames();
131
Asserts.assertFalse(frames.isEmpty(), "JavaFrames was empty");
132
return frames.get(0).getMethod().getName();
133
}
134
135
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {
136
RecordedStackTrace stacktrace = null;
137
try {
138
stacktrace = event.getStackTrace();
139
List<RecordedFrame> frames = stacktrace.getFrames();
140
Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);
141
List<String> expectedMethods = getExpectedMethods(expectedDepth);
142
Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");
143
144
for (int i = 0; i < frames.size(); ++i) {
145
String name = frames.get(i).getMethod().getName();
146
String expectedName = expectedMethods.get(i);
147
Asserts.assertEquals(name, expectedName, "Wrong method name at index " + i);
148
}
149
150
boolean isTruncated = stacktrace.isTruncated();
151
boolean isTruncateExpected = expectedDepth > MAX_DEPTH;
152
Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);
153
154
String firstMethod = frames.get(frames.size() - 1).getMethod().getName();
155
boolean isFullTrace = "run".equals(firstMethod);
156
String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);
157
Asserts.assertTrue(isTruncated != isFullTrace, msg);
158
} catch (Throwable t) {
159
System.out.println(String.format("stacktrace:%n%s", stacktrace));
160
throw t;
161
}
162
}
163
164
private static List<String> getExpectedMethods(int depth) {
165
List<String> methods = new ArrayList<>();
166
methods.add("recurseEnd");
167
for (int i = 0; i < depth - 2; ++i) {
168
methods.add((i % 2) == 0 ? "recurseA" : "recurseB");
169
}
170
methods.add("run");
171
if (depth > MAX_DEPTH) {
172
methods = methods.subList(0, MAX_DEPTH);
173
}
174
return methods;
175
}
176
}
177
178