Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/tools/jhsdb/JShellHeapDumpTest.java
41149 views
1
/*
2
* Copyright (c) 2019, 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
* @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
public static void printStackTraces(String file) throws IOException {
101
try {
102
String output = HprofReader.getStack(file, 0);
103
// We only require JShellToolProvider to be in the output if we did the
104
// short sleep. If we did not, the java process may not have executed far
105
// enough along to even start the main thread.
106
if (doSleep && !output.contains("JShellToolProvider")) {
107
throw new RuntimeException("'JShellToolProvider' missing from stdout/stderr");
108
}
109
} catch (Exception ex) {
110
throw new RuntimeException("Test ERROR " + ex, ex);
111
}
112
}
113
114
public static void testHeapDump() throws IOException {
115
File hprofFile = new File("jhsdb.jmap.heap." +
116
System.currentTimeMillis() + ".hprof");
117
if (hprofFile.exists()) {
118
hprofFile.delete();
119
}
120
121
launch("heap written to", "jmap",
122
"--binaryheap", "--dumpfile=" + hprofFile.getAbsolutePath());
123
124
assertTrue(hprofFile.exists() && hprofFile.isFile(),
125
"Could not create dump file " + hprofFile.getAbsolutePath());
126
127
printStackTraces(hprofFile.getAbsolutePath());
128
129
System.out.println("hprof file size: " + hprofFile.length());
130
hprofFile.delete();
131
}
132
133
public static void launchJshell() throws IOException {
134
System.out.println("Starting Jshell");
135
long startTime = System.currentTimeMillis();
136
try {
137
ProcessBuilder pb = new ProcessBuilder(JDKToolFinder.getTestJDKTool("jshell"));
138
jShellProcess = ProcessTools.startProcess("JShell", pb,
139
s -> { // warm-up predicate
140
return s.contains("Welcome to JShell");
141
});
142
} catch (Exception ex) {
143
throw new RuntimeException("Test ERROR " + ex, ex);
144
}
145
146
long elapsedTime = System.currentTimeMillis() - startTime;
147
System.out.println("Jshell Started in " + elapsedTime + "ms");
148
149
// Give jshell a chance to fully start up. This makes SA more stable for the jmap dump.
150
try {
151
if (doSleep) {
152
Thread.sleep(2000);
153
}
154
} catch (Exception e) {
155
}
156
}
157
158
public static void main(String[] args) throws Exception {
159
SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
160
if (args.length == 1) {
161
if (args[0].equals("nosleep")) {
162
doSleep = false;
163
} else {
164
throw new RuntimeException("Invalid arg: " + args[0]);
165
}
166
} else if (args.length != 0) {
167
throw new RuntimeException("Too many args: " + args.length);
168
}
169
testHeapDump();
170
171
// The test throws RuntimeException on error.
172
// IOException is thrown if Jshell can't start because of some bad
173
// environment condition
174
System.out.println("Test PASSED");
175
}
176
}
177
178