50571
|
1 |
/*
|
|
2 |
* Copyright (c) 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 |
|
|
24 |
/*
|
|
25 |
* @test TestJmapCore
|
|
26 |
* @summary Test verifies that jhsdb jmap could generate heap dump from core when heap is full
|
|
27 |
* @library /test/lib
|
|
28 |
* @run driver/timeout=240 TestJmapCore run heap
|
|
29 |
*/
|
|
30 |
|
|
31 |
import jdk.test.lib.Asserts;
|
|
32 |
import jdk.test.lib.JDKToolFinder;
|
|
33 |
import jdk.test.lib.JDKToolLauncher;
|
|
34 |
import jdk.test.lib.Platform;
|
|
35 |
import jdk.test.lib.classloader.GeneratingClassLoader;
|
|
36 |
import jdk.test.lib.hprof.HprofParser;
|
|
37 |
import jdk.test.lib.process.ProcessTools;
|
|
38 |
import jdk.test.lib.process.OutputAnalyzer;
|
|
39 |
|
|
40 |
import java.io.File;
|
|
41 |
|
|
42 |
public class TestJmapCore {
|
|
43 |
static final String pidSeparator = ":KILLED_PID";
|
|
44 |
|
|
45 |
public static final String HEAP_OOME = "heap";
|
|
46 |
public static final String METASPACE_OOME = "metaspace";
|
|
47 |
|
|
48 |
|
|
49 |
public static void main(String[] args) throws Throwable {
|
|
50 |
if (args.length == 1) {
|
|
51 |
// If 1 argument is set prints pid so main process could find corefile
|
|
52 |
System.out.println(ProcessHandle.current().pid() + pidSeparator);
|
|
53 |
try {
|
|
54 |
if (args[0].equals(HEAP_OOME)) {
|
|
55 |
Object[] oa = new Object[Integer.MAX_VALUE / 2];
|
|
56 |
for(int i = 0; i < oa.length; i++) {
|
|
57 |
oa[i] = new Object[Integer.MAX_VALUE / 2];
|
|
58 |
}
|
|
59 |
} else {
|
|
60 |
GeneratingClassLoader loader = new GeneratingClassLoader();
|
|
61 |
for (int i = 0; ; i++) {
|
|
62 |
loader.loadClass(loader.getClassName(i));
|
|
63 |
}
|
|
64 |
}
|
|
65 |
throw new Error("OOME not triggered");
|
|
66 |
} catch (OutOfMemoryError err) {
|
|
67 |
return;
|
|
68 |
}
|
|
69 |
}
|
|
70 |
test(args[1]);
|
|
71 |
}
|
|
72 |
|
|
73 |
// Test tries to run java with ulimit unlimited if it is possible
|
|
74 |
static boolean useDefaultUlimit() {
|
|
75 |
if (Platform.isWindows()) {
|
|
76 |
return true;
|
|
77 |
}
|
|
78 |
try {
|
|
79 |
OutputAnalyzer output = ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && ulimit -c");
|
|
80 |
return !(output.getExitValue() == 0 && output.getStdout().contains("unlimited"));
|
|
81 |
} catch (Throwable t) {
|
|
82 |
return true;
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
static void test(String type) throws Throwable {
|
|
87 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-XX:+CreateCoredumpOnCrash",
|
|
88 |
"-XX:MaxMetaspaceSize=64m", "-XX:+CrashOnOutOfMemoryError", "-XX:-TransmitErrorReport",
|
|
89 |
TestJmapCore.class.getName(), type);
|
|
90 |
|
|
91 |
boolean useDefaultUlimit = useDefaultUlimit();
|
|
92 |
System.out.println("Run test with ulimit: " + (useDefaultUlimit ? "default" : "unlimited"));
|
|
93 |
OutputAnalyzer output = useDefaultUlimit
|
|
94 |
? ProcessTools.executeProcess(pb)
|
|
95 |
: ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && "
|
|
96 |
+ ProcessTools.getCommandLine(pb));
|
|
97 |
File core;
|
|
98 |
String pattern = Platform.isWindows() ? "mdmp" : "core";
|
|
99 |
File[] cores = new File(".").listFiles((dir, name) -> name.contains(pattern));
|
|
100 |
if (cores.length == 0) {
|
|
101 |
// /cores/core.$pid might be generated on macosx by default
|
|
102 |
String pid = output.firstMatch("^(\\d+)" + pidSeparator, 1);
|
|
103 |
core = new File("cores/core." + pid);
|
|
104 |
if (!core.exists()) {
|
|
105 |
System.out.println("Has not been able to find coredump. Test skipped.");
|
|
106 |
return;
|
|
107 |
}
|
|
108 |
} else {
|
|
109 |
Asserts.assertTrue(cores.length == 1,
|
|
110 |
"There are unexpected files containing core "
|
|
111 |
+ ": " + String.join(",", new File(".").list()) + ".");
|
|
112 |
core = cores[0];
|
|
113 |
}
|
|
114 |
System.out.println("Found corefile: " + core.getAbsolutePath());
|
|
115 |
|
|
116 |
File dumpFile = new File("heap.hprof");
|
|
117 |
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
|
|
118 |
launcher.addToolArg("jmap");
|
|
119 |
launcher.addToolArg("--binaryheap");
|
|
120 |
launcher.addToolArg("--dumpfile=" + dumpFile);
|
|
121 |
launcher.addToolArg("--exe");
|
|
122 |
launcher.addToolArg(JDKToolFinder.getTestJDKTool("java"));
|
|
123 |
launcher.addToolArg("--core");
|
|
124 |
launcher.addToolArg(core.getPath());
|
|
125 |
|
|
126 |
ProcessBuilder jhsdpb = new ProcessBuilder();
|
|
127 |
jhsdpb.command(launcher.getCommand());
|
|
128 |
Process jhsdb = jhsdpb.start();
|
|
129 |
OutputAnalyzer out = new OutputAnalyzer(jhsdb);
|
|
130 |
|
|
131 |
jhsdb.waitFor();
|
|
132 |
|
|
133 |
System.out.println(out.getStdout());
|
|
134 |
System.err.println(out.getStderr());
|
|
135 |
|
|
136 |
Asserts.assertTrue(dumpFile.exists() && dumpFile.isFile(),
|
|
137 |
"Could not find dump file " + dumpFile.getAbsolutePath());
|
|
138 |
|
|
139 |
HprofParser.parse(dumpFile);
|
|
140 |
System.out.println("PASSED");
|
|
141 |
}
|
|
142 |
}
|