2014-01-14 11:08:33 +01:00
|
|
|
/*
|
2020-05-12 09:45:24 -07:00
|
|
|
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
|
2014-01-14 11:08:33 +01:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
2018-09-21 14:50:06 -07:00
|
|
|
import static jdk.test.lib.Asserts.assertTrue;
|
|
|
|
import static jdk.test.lib.Asserts.fail;
|
2015-04-28 14:33:32 +02:00
|
|
|
|
2014-01-14 11:08:33 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
2018-09-10 14:23:37 -07:00
|
|
|
import jdk.test.lib.JDKToolLauncher;
|
2020-05-12 09:45:24 -07:00
|
|
|
import jdk.test.lib.Utils;
|
2015-04-28 14:33:32 +02:00
|
|
|
import jdk.test.lib.hprof.HprofParser;
|
2018-09-07 14:01:52 -07:00
|
|
|
import jdk.test.lib.process.OutputAnalyzer;
|
|
|
|
import jdk.test.lib.process.ProcessTools;
|
2014-01-14 11:08:33 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @summary Unit test for jmap utility
|
2015-07-22 09:51:40 +02:00
|
|
|
* @key intermittent
|
2016-08-19 10:09:53 -04:00
|
|
|
* @library /test/lib
|
2015-04-28 14:33:32 +02:00
|
|
|
* @build jdk.test.lib.hprof.*
|
2015-11-23 16:14:33 +08:00
|
|
|
* @build jdk.test.lib.hprof.model.*
|
2015-04-28 14:33:32 +02:00
|
|
|
* @build jdk.test.lib.hprof.parser.*
|
2015-11-23 16:14:33 +08:00
|
|
|
* @build jdk.test.lib.hprof.util.*
|
2015-05-28 10:37:49 +02:00
|
|
|
* @run main/timeout=240 BasicJMapTest
|
2014-01-14 11:08:33 +01:00
|
|
|
*/
|
|
|
|
public class BasicJMapTest {
|
|
|
|
|
|
|
|
private static ProcessBuilder processBuilder = new ProcessBuilder();
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
testHisto();
|
|
|
|
testHistoLive();
|
2019-02-20 11:36:02 -08:00
|
|
|
testHistoAll();
|
|
|
|
testHistoToFile();
|
|
|
|
testHistoLiveToFile();
|
|
|
|
testHistoAllToFile();
|
2016-05-09 23:41:59 +03:00
|
|
|
testFinalizerInfo();
|
|
|
|
testClstats();
|
2014-01-14 11:08:33 +01:00
|
|
|
testDump();
|
|
|
|
testDumpLive();
|
2019-02-20 11:36:02 -08:00
|
|
|
testDumpAll();
|
2014-01-14 11:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void testHisto() throws Exception {
|
2019-02-20 11:36:02 -08:00
|
|
|
OutputAnalyzer output = jmap("-histo:");
|
2014-01-14 11:08:33 +01:00
|
|
|
output.shouldHaveExitValue(0);
|
2019-02-20 11:36:02 -08:00
|
|
|
OutputAnalyzer output1 = jmap("-histo");
|
|
|
|
output1.shouldHaveExitValue(0);
|
2014-01-14 11:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void testHistoLive() throws Exception {
|
|
|
|
OutputAnalyzer output = jmap("-histo:live");
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
}
|
|
|
|
|
2019-02-20 11:36:02 -08:00
|
|
|
private static void testHistoAll() throws Exception {
|
|
|
|
OutputAnalyzer output = jmap("-histo:all");
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:31:37 -07:00
|
|
|
private static void testHistoParallelZero() throws Exception {
|
|
|
|
OutputAnalyzer output = jmap("-histo:parallel=0");
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void testHistoParallel() throws Exception {
|
|
|
|
OutputAnalyzer output = jmap("-histo:parallel=2");
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void testHistoNonParallel() throws Exception {
|
|
|
|
OutputAnalyzer output = jmap("-histo:parallel=1");
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
}
|
|
|
|
|
2019-02-20 11:36:02 -08:00
|
|
|
private static void testHistoToFile() throws Exception {
|
2020-08-13 11:31:37 -07:00
|
|
|
histoToFile(false, false, 1);
|
2019-02-20 11:36:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void testHistoLiveToFile() throws Exception {
|
2020-08-13 11:31:37 -07:00
|
|
|
histoToFile(true, false, 1);
|
2019-02-20 11:36:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void testHistoAllToFile() throws Exception {
|
2020-08-13 11:31:37 -07:00
|
|
|
histoToFile(false, true, 1);
|
2019-02-20 11:36:02 -08:00
|
|
|
}
|
|
|
|
|
2020-08-13 11:31:37 -07:00
|
|
|
private static void testHistoFileParallelZero() throws Exception {
|
|
|
|
histoToFile(false, false, 0);
|
2019-02-20 11:36:02 -08:00
|
|
|
}
|
|
|
|
|
2020-08-13 11:31:37 -07:00
|
|
|
private static void testHistoFileParallel() throws Exception {
|
|
|
|
histoToFile(false, false, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void histoToFile(boolean live,
|
|
|
|
boolean explicitAll,
|
|
|
|
int parallelThreadNum) throws Exception {
|
|
|
|
String liveArg = "";
|
|
|
|
String fileArg = "";
|
|
|
|
String parArg = "parallel=" + parallelThreadNum;
|
|
|
|
String allArgs = "-histo:";
|
|
|
|
|
|
|
|
if (live && explicitAll) {
|
2019-02-20 11:36:02 -08:00
|
|
|
fail("Illegal argument setting for jmap -histo");
|
|
|
|
}
|
2020-08-13 11:31:37 -07:00
|
|
|
if (live) {
|
|
|
|
liveArg = "live,";
|
|
|
|
}
|
|
|
|
if (explicitAll) {
|
|
|
|
liveArg = "all,";
|
|
|
|
}
|
|
|
|
|
2019-02-20 11:36:02 -08:00
|
|
|
File file = new File("jmap.histo.file" + System.currentTimeMillis() + ".histo");
|
|
|
|
if (file.exists()) {
|
|
|
|
file.delete();
|
|
|
|
}
|
2020-08-13 11:31:37 -07:00
|
|
|
fileArg = "file=" + file.getName();
|
|
|
|
|
2019-02-20 11:36:02 -08:00
|
|
|
OutputAnalyzer output;
|
2020-08-13 11:31:37 -07:00
|
|
|
allArgs = allArgs + liveArg + fileArg + ',' + parArg;
|
|
|
|
output = jmap(allArgs);
|
2019-02-20 11:36:02 -08:00
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
output.shouldContain("Heap inspection file created");
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:41:59 +03:00
|
|
|
private static void testFinalizerInfo() throws Exception {
|
|
|
|
OutputAnalyzer output = jmap("-finalizerinfo");
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void testClstats() throws Exception {
|
|
|
|
OutputAnalyzer output = jmap("-clstats");
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
}
|
|
|
|
|
2014-01-14 11:08:33 +01:00
|
|
|
private static void testDump() throws Exception {
|
2020-08-13 11:31:37 -07:00
|
|
|
dump(false, false);
|
2014-01-14 11:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void testDumpLive() throws Exception {
|
2020-08-13 11:31:37 -07:00
|
|
|
dump(true, false);
|
2015-04-28 14:33:32 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:36:02 -08:00
|
|
|
private static void testDumpAll() throws Exception {
|
2020-08-13 11:31:37 -07:00
|
|
|
dump(false, true);
|
2019-02-20 11:36:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void dump(boolean live, boolean explicitAll) throws Exception {
|
2020-08-13 11:31:37 -07:00
|
|
|
String liveArg = "";
|
|
|
|
String fileArg = "";
|
|
|
|
String allArgs = "-dump:";
|
|
|
|
|
|
|
|
if (live && explicitAll) {
|
|
|
|
fail("Illegal argument setting for jmap -dump");
|
2015-04-28 14:33:32 +02:00
|
|
|
}
|
|
|
|
if (live) {
|
2020-08-13 11:31:37 -07:00
|
|
|
liveArg = "live,";
|
|
|
|
}
|
|
|
|
if (explicitAll) {
|
|
|
|
liveArg = "all,";
|
2015-04-28 14:33:32 +02:00
|
|
|
}
|
2020-08-13 11:31:37 -07:00
|
|
|
|
|
|
|
File file = new File("jmap.dump" + System.currentTimeMillis() + ".hprof");
|
|
|
|
if (file.exists()) {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
fileArg = "file=" + file.getName();
|
|
|
|
|
|
|
|
OutputAnalyzer output;
|
|
|
|
allArgs = allArgs + liveArg + "format=b," + fileArg;
|
|
|
|
output = jmap(allArgs);
|
2014-01-14 11:08:33 +01:00
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
output.shouldContain("Heap dump file created");
|
2020-08-13 11:31:37 -07:00
|
|
|
verifyDumpFile(file);
|
|
|
|
file.delete();
|
2014-01-14 11:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyDumpFile(File dump) {
|
2015-04-28 14:33:32 +02:00
|
|
|
assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
|
|
|
|
try {
|
|
|
|
HprofParser.parse(dump);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Could not parse dump file " + dump.getAbsolutePath());
|
|
|
|
}
|
2014-01-14 11:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
|
|
|
|
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
|
2020-05-12 09:45:24 -07:00
|
|
|
launcher.addVMArgs(Utils.getTestJavaOpts());
|
2014-01-14 11:08:33 +01:00
|
|
|
if (toolArgs != null) {
|
|
|
|
for (String toolArg : toolArgs) {
|
|
|
|
launcher.addToolArg(toolArg);
|
|
|
|
}
|
|
|
|
}
|
2015-11-23 11:49:04 -08:00
|
|
|
launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
|
2014-01-14 11:08:33 +01:00
|
|
|
|
|
|
|
processBuilder.command(launcher.getCommand());
|
2019-02-20 11:36:02 -08:00
|
|
|
System.out.println(Arrays.toString(processBuilder.command().toArray()));
|
2014-10-17 10:53:30 +02:00
|
|
|
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
|
2014-01-14 11:08:33 +01:00
|
|
|
System.out.println(output.getOutput());
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|