Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/tools/jhsdb/JShellHeapDumpTest.java
66644 views
1
/*
2
* Copyright (c) 2019, 2021, 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
* @bug 8225715
27
* @requires vm.hasSA
28
* @library /test/lib
29
* @compile JShellHeapDumpTest.java
30
* @run main/timeout=240 JShellHeapDumpTest
31
*/
32
33
import static jdk.test.lib.Asserts.assertTrue;
34
35
import java.io.IOException;
36
import java.io.File;
37
import java.util.List;
38
import java.util.Arrays;
39
import java.util.Map;
40
41
import jdk.test.lib.Utils;
42
import jdk.test.lib.hprof.parser.HprofReader;
43
import jdk.test.lib.JDKToolLauncher;
44
import jdk.test.lib.JDKToolFinder;
45
import jdk.test.lib.process.OutputAnalyzer;
46
import jdk.test.lib.process.ProcessTools;
47
import jdk.test.lib.SA.SATestUtils;
48
49
import jdk.jshell.JShell;
50
51
public class JShellHeapDumpTest {
52
53
static Process jShellProcess;
54
static boolean doSleep = true; // By default do a short sleep when app starts up
55
56
public static void launch(String expectedMessage, List<String> toolArgs)
57
throws IOException {
58
59
try {
60
launchJshell();
61
long jShellPID = jShellProcess.pid();
62
63
System.out.println("Starting " + toolArgs.get(0) + " against " + jShellPID);
64
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
65
launcher.addVMArgs(Utils.getFilteredTestJavaOpts("-Xcomp"));
66
67
for (String cmd : toolArgs) {
68
launcher.addToolArg(cmd);
69
}
70
71
launcher.addToolArg("--pid=" + Long.toString(jShellPID));
72
73
ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);
74
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
75
System.out.println("jhsdb jmap stdout:");
76
System.out.println(output.getStdout());
77
System.out.println("jhsdb jmap stderr:");
78
System.out.println(output.getStderr());
79
System.out.println("###### End of all output:");
80
output.shouldHaveExitValue(0);
81
} catch (Exception ex) {
82
throw new RuntimeException("Test ERROR " + ex, ex);
83
} finally {
84
if (jShellProcess.isAlive()) {
85
System.out.println("Destroying jshell");
86
jShellProcess.destroy();
87
System.out.println("Jshell destroyed");
88
} else {
89
System.out.println("Jshell not alive");
90
}
91
}
92
}
93
94
public static void launch(String expectedMessage, String... toolArgs)
95
throws IOException {
96
97
launch(expectedMessage, Arrays.asList(toolArgs));
98
}
99
100
/* Returns false if the attempt should be retried. */
101
public static boolean printStackTraces(String file, boolean allowRetry) throws IOException {
102
try {
103
String output = HprofReader.getStack(file, 0);
104
// We only require JShellToolProvider to be in the output if we did the
105
// short sleep. If we did not, the java process may not have executed far
106
// enough along to even start the main thread.
107
if (doSleep && !output.contains("JShellToolProvider")) {
108
// This check will very rarely fail due to not be able to get the stack trace
109
// of the main thread do to it actively executing. See JDK-8269556. We retry once
110
// if that happens. This failure is so rare that this should be enough to make it
111
// extremely unlikely that we ever see this test fail again for this reason.
112
if (!allowRetry) {
113
throw new RuntimeException("'JShellToolProvider' missing from stdout/stderr");
114
} else {
115
System.out.println("'JShellToolProvider' missing. Allow one retry.");
116
return true; // Allow one retry
117
}
118
}
119
} catch (Exception ex) {
120
throw new RuntimeException("Test ERROR " + ex, ex);
121
}
122
return false;
123
}
124
125
/* Returns false if the attempt should be retried. */
126
public static boolean testHeapDump(boolean allowRetry) throws IOException {
127
File hprofFile = new File("jhsdb.jmap.heap." +
128
System.currentTimeMillis() + ".hprof");
129
if (hprofFile.exists()) {
130
hprofFile.delete();
131
}
132
133
launch("heap written to", "jmap",
134
"--binaryheap", "--dumpfile=" + hprofFile.getAbsolutePath());
135
136
assertTrue(hprofFile.exists() && hprofFile.isFile(),
137
"Could not create dump file " + hprofFile.getAbsolutePath());
138
139
boolean retry = printStackTraces(hprofFile.getAbsolutePath(), allowRetry);
140
141
System.out.println("hprof file size: " + hprofFile.length());
142
hprofFile.delete();
143
144
return retry;
145
}
146
147
public static void launchJshell() throws IOException {
148
System.out.println("Starting Jshell");
149
long startTime = System.currentTimeMillis();
150
try {
151
ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jshell"));
152
jShellProcess = ProcessTools.startProcess("JShell", pb,
153
s -> { // warm-up predicate
154
return s.contains("Welcome to JShell");
155
});
156
} catch (Exception ex) {
157
throw new RuntimeException("Test ERROR " + ex, ex);
158
}
159
160
long elapsedTime = System.currentTimeMillis() - startTime;
161
System.out.println("Jshell Started in " + elapsedTime + "ms");
162
163
// Give jshell a chance to fully start up. This makes SA more stable for the jmap dump.
164
try {
165
if (doSleep) {
166
Thread.sleep(4000);
167
}
168
} catch (Exception e) {
169
}
170
}
171
172
public static void main(String[] args) throws Exception {
173
SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
174
if (args.length == 1) {
175
if (args[0].equals("nosleep")) {
176
doSleep = false;
177
} else {
178
throw new RuntimeException("Invalid arg: " + args[0]);
179
}
180
} else if (args.length != 0) {
181
throw new RuntimeException("Too many args: " + args.length);
182
}
183
184
boolean retry = testHeapDump(true);
185
// In case of rare failure to find 'JShellToolProvider' in the output, allow one retry.
186
if (retry) {
187
testHeapDump(false);
188
}
189
190
// The test throws RuntimeException on error.
191
// IOException is thrown if Jshell can't start because of some bad
192
// environment condition
193
System.out.println("Test PASSED");
194
}
195
}
196
197