1 /*
2 * Copyright (c) 2005, 2014, 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.File;
25 import java.util.Arrays;
26
27 import static jdk.testlibrary.Asserts.*;
28 import jdk.testlibrary.JDKToolLauncher;
29 import jdk.testlibrary.OutputAnalyzer;
30 import jdk.testlibrary.ProcessTools;
31
32 /*
33 * @test
34 * @bug 6321286
35 * @summary Unit test for jmap utility
36 * @library /lib/testlibrary
37 * @build jdk.testlibrary.*
38 * @run main BasicJMapTest
39 */
40 public class BasicJMapTest {
41
42 private static ProcessBuilder processBuilder = new ProcessBuilder();
43
44 public static void main(String[] args) throws Exception {
45 testHisto();
46 testHistoLive();
47 testDump();
48 testDumpLive();
49 }
50
51 private static void testHisto() throws Exception {
52 OutputAnalyzer output = jmap("-histo");
53 output.shouldHaveExitValue(0);
54 }
55
56 private static void testHistoLive() throws Exception {
57 OutputAnalyzer output = jmap("-histo:live");
58 output.shouldHaveExitValue(0);
59 }
60
61 private static void testDump() throws Exception {
62 File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
63 OutputAnalyzer output = jmap("-dump:format=b,file=" + dump.getName());
64 output.shouldHaveExitValue(0);
65 output.shouldContain("Heap dump file created");
66 verifyDumpFile(dump);
67 }
68
69 private static void testDumpLive() throws Exception {
70 File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
71 OutputAnalyzer output = jmap("-dump:live,format=b,file=" + dump.getName());
72 output.shouldHaveExitValue(0);
73 output.shouldContain("Heap dump file created");
74 verifyDumpFile(dump);
75 }
76
77 private static void verifyDumpFile(File dump) {
78 assertTrue(dump.exists() && dump.isFile(), "Could not create dump file");
79 dump.delete();
80 }
81
82 private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
83 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
84 launcher.addVMArg("-XX:+UsePerfData");
85 if (toolArgs != null) {
86 for (String toolArg : toolArgs) {
87 launcher.addToolArg(toolArg);
88 }
89 }
90 launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
91
92 processBuilder.command(launcher.getCommand());
93 System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
94 OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
95 System.out.println(output.getOutput());
96
97 return output;
98 }
99
|
1 /*
2 * Copyright (c) 2005, 2015, 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 static jdk.testlibrary.Asserts.assertTrue;
25 import static jdk.testlibrary.Asserts.fail;
26
27 import java.io.File;
28 import java.util.Arrays;
29
30 import jdk.test.lib.hprof.HprofParser;
31 import jdk.testlibrary.JDKToolLauncher;
32 import jdk.testlibrary.OutputAnalyzer;
33 import jdk.testlibrary.ProcessTools;
34
35 /*
36 * @test
37 * @bug 6321286
38 * @summary Unit test for jmap utility
39 * @library /lib/testlibrary
40 * @library /../../test/lib/share/classes
41 * @build jdk.testlibrary.*
42 * @build jdk.test.lib.hprof.*
43 * @build jdk.test.lib.hprof.module.*
44 * @build jdk.test.lib.hprof.parser.*
45 * @build jdk.test.lib.hprof.utils.*
46 * @run main BasicJMapTest
47 */
48 public class BasicJMapTest {
49
50 private static ProcessBuilder processBuilder = new ProcessBuilder();
51
52 public static void main(String[] args) throws Exception {
53 testHisto();
54 testHistoLive();
55 testDump();
56 testDumpLive();
57 }
58
59 private static void testHisto() throws Exception {
60 OutputAnalyzer output = jmap("-histo");
61 output.shouldHaveExitValue(0);
62 }
63
64 private static void testHistoLive() throws Exception {
65 OutputAnalyzer output = jmap("-histo:live");
66 output.shouldHaveExitValue(0);
67 }
68
69 private static void testDump() throws Exception {
70 dump(false);
71 }
72
73 private static void testDumpLive() throws Exception {
74 dump(true);
75 }
76
77 private static void dump(boolean live) throws Exception {
78 File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof");
79 if (dump.exists()) {
80 dump.delete();
81 }
82 OutputAnalyzer output;
83 if (live) {
84 output = jmap("-dump:live,format=b,file=" + dump.getName());
85 } else {
86 output = jmap("-dump:format=b,file=" + dump.getName());
87 }
88 output.shouldHaveExitValue(0);
89 output.shouldContain("Heap dump file created");
90 verifyDumpFile(dump);
91 dump.delete();
92 }
93
94 private static void verifyDumpFile(File dump) {
95 assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
96 try {
97 HprofParser.parse(dump);
98 } catch (Exception e) {
99 e.printStackTrace();
100 fail("Could not parse dump file " + dump.getAbsolutePath());
101 }
102 }
103
104 private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
105 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
106 launcher.addVMArg("-XX:+UsePerfData");
107 if (toolArgs != null) {
108 for (String toolArg : toolArgs) {
109 launcher.addToolArg(toolArg);
110 }
111 }
112 launcher.addToolArg(Integer.toString(ProcessTools.getProcessId()));
113
114 processBuilder.command(launcher.getCommand());
115 System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
116 OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
117 System.out.println(output.getOutput());
118
119 return output;
120 }
121
|