Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestJhsdbJstackLineNumbers.java
41149 views
1
/*
2
* Copyright (c) 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
import java.io.OutputStream;
25
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
27
import java.util.stream.Collectors;
28
import java.util.TreeSet;
29
30
import jdk.test.lib.apps.LingeredApp;
31
import jdk.test.lib.JDKToolLauncher;
32
import jdk.test.lib.process.OutputAnalyzer;
33
import jdk.test.lib.process.ProcessTools;
34
import jdk.test.lib.SA.SATestUtils;
35
36
/**
37
* @test
38
* @requires vm.hasSA
39
* @requires os.arch=="amd64" | os.arch=="x86_64"
40
* @requires os.family=="windows" | os.family == "linux" | os.family == "mac"
41
* @library /test/lib
42
* @run main/othervm TestJhsdbJstackLineNumbers
43
*/
44
45
/*
46
* This test makes sure that SA gets the most accurate value for the line number of
47
* the current (topmost) frame. Many SA ports just rely on frame->bcp, but it is
48
* usually out of date since the current BCP is cached in a register and only flushed
49
* to frame->bcp when the register is needed for something else. Therefore SA ports
50
* need to fetch the register that the BCP is stored in and see if it is valid,
51
* and only defer to frame->bcp if it is not valid.
52
*
53
* The test works by spawning a process that sits in a 10 line loop in the busywork() method,
54
* all while the main test does repeated jstacks on the process. The expectation is
55
* that at least 5 of the lines in the busywork() loop will eventually show up in at
56
* least one of the jstack runs.
57
*/
58
59
class LingeredAppWithBusyWork extends LingeredApp {
60
static volatile boolean stop = false;
61
62
private static int busywork(int[] x) {
63
int i = 0;
64
while (!stop) {
65
i = x[0];
66
i += x[1];
67
i += x[2];
68
i += x[3];
69
i += x[4];
70
i += x[5];
71
i += x[6];
72
i += x[7];
73
}
74
return i;
75
}
76
77
public static void main(String... args) {
78
Thread t = new Thread(() -> {
79
busywork(new int[]{0,1,2,3,4,5,6,7});
80
});
81
82
try {
83
t.setName("BusyWorkThread");
84
t.start();
85
LingeredApp.main(args);
86
stop = true;
87
t.join();
88
} catch (InterruptedException e) {
89
}
90
}
91
}
92
93
public class TestJhsdbJstackLineNumbers {
94
// This is the number of lines in the busywork main loop
95
static final int TOTAL_BUSYWORK_LOOP_LINES = 10;
96
// The minimum number of lines that we must at some point see in the jstack output
97
static final int MIN_BUSYWORK_LOOP_LINES = 5;
98
99
static final int MAX_NUMBER_OF_JSTACK_RUNS = 25;
100
101
private static OutputAnalyzer runJstack(String... toolArgs) throws Exception {
102
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
103
launcher.addToolArg("jstack");
104
if (toolArgs != null) {
105
for (String toolArg : toolArgs) {
106
launcher.addToolArg(toolArg);
107
}
108
}
109
110
ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);
111
System.out.println(processBuilder.command().stream().collect(Collectors.joining(" ")));
112
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
113
114
return output;
115
}
116
117
public static void runTest(long pid) throws Exception {
118
// Keep running jstack until the target app is in the "busywork" method.
119
String output;
120
int maxRetries = 5;
121
do {
122
if (maxRetries-- == 0) {
123
throw new RuntimeException("Failed: LingeredAppWithBusyWork never entered busywork() method.");
124
}
125
OutputAnalyzer jstackOut = runJstack("--pid", Long.toString(pid));
126
output = jstackOut.getOutput();
127
System.out.println(output);
128
} while (!output.contains("busywork"));
129
130
// This is for tracking all the line numbers in busywork() that we've seen.
131
// Since it is a TreeSet, it will always be sorted and have no duplicates.
132
TreeSet<Integer> lineNumbersSeen = new TreeSet<Integer>();
133
134
// Keep running jstack until we see a sufficient number of different line
135
// numbers in the busywork() loop.
136
for (int x = 0; x < MAX_NUMBER_OF_JSTACK_RUNS; x++) {
137
OutputAnalyzer jstackOut = runJstack("--pid", Long.toString(pid));
138
output = jstackOut.getOutput();
139
// The stack dump will have a line that looks like:
140
// - LingeredAppWithBusyWork.busywork(int[]) @bci=32, line=74 (Interpreted frame)
141
// We want to match on the line number, "74" in this example. We also match on the
142
// full line just so we can print it out.
143
Pattern LINE_PATTERN = Pattern.compile(
144
".+(- LingeredAppWithBusyWork.busywork\\(int\\[\\]\\) \\@bci\\=[0-9]+, line\\=([0-9]+) \\(Interpreted frame\\)).+", Pattern.DOTALL);
145
Matcher matcher = LINE_PATTERN.matcher(output);
146
if (matcher.matches()) {
147
System.out.println(matcher.group(1)); // print matching stack trace line
148
int lineNum = Integer.valueOf(matcher.group(2)); // get matching line number
149
lineNumbersSeen.add(lineNum);
150
if (lineNumbersSeen.size() == MIN_BUSYWORK_LOOP_LINES) {
151
// We're done!
152
System.out.println("Found needed line numbers after " + (x+1) + " iterations");
153
break;
154
}
155
} else {
156
System.out.println("failed to match");
157
System.out.println(output);
158
continue; // Keep trying. This can happen on rare occasions when the stack cannot be determined.
159
}
160
}
161
System.out.println("Found Line Numbers: " + lineNumbersSeen);
162
163
// Make sure we saw the minimum required number of lines in busywork().
164
if (lineNumbersSeen.size() < MIN_BUSYWORK_LOOP_LINES) {
165
throw new RuntimeException("Failed: Didn't find enough line numbers: " + lineNumbersSeen);
166
}
167
168
// Make sure the distance between the lowest and highest line numbers seen
169
// is not more than the number of lines in the busywork() loop.
170
if (lineNumbersSeen.last() - lineNumbersSeen.first() > TOTAL_BUSYWORK_LOOP_LINES) {
171
throw new RuntimeException("Failed: lowest and highest line numbers are too far apart: " + lineNumbersSeen);
172
}
173
174
}
175
176
public static void main(String... args) throws Exception {
177
SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
178
179
LingeredApp theApp = null;
180
try {
181
// Launch the LingeredAppWithBusyWork process with the busywork() loop
182
theApp = new LingeredAppWithBusyWork();
183
LingeredApp.startAppExactJvmOpts(theApp, "-Xint");
184
System.out.println("Started LingeredApp with pid " + theApp.getPid());
185
186
runTest(theApp.getPid());
187
} finally {
188
LingeredApp.stopApp(theApp);
189
System.out.println("LingeredAppWithBusyWork finished");
190
}
191
}
192
}
193
194