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 File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof"); 71 OutputAnalyzer output = jmap("-dump:format=b,file=" + dump.getName()); 72 output.shouldHaveExitValue(0); 73 output.shouldContain("Heap dump file created"); 74 verifyDumpFile(dump); 75 dump.delete(); 76 } 77 78 private static void testDumpLive() throws Exception { 79 File dump = new File("java_pid$" + ProcessTools.getProcessId() + ".hprof"); 80 OutputAnalyzer output = jmap("-dump:live,format=b,file=" + dump.getName()); 81 output.shouldHaveExitValue(0); 82 output.shouldContain("Heap dump file created"); 83 verifyDumpFile(dump); 84 dump.delete(); 85 } 86 87 private static void verifyDumpFile(File dump) { 88 assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath()); 89 try { 90 HprofParser.parse(dump); 91 } catch (Exception e) { 92 e.printStackTrace(); 93 fail("Could not parse dump file " + dump.getAbsolutePath()); 94 } 95 } 96 97 private static OutputAnalyzer jmap(String... toolArgs) throws Exception { 98 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap"); 99 launcher.addVMArg("-XX:+UsePerfData"); 100 if (toolArgs != null) { 101 for (String toolArg : toolArgs) { 102 launcher.addToolArg(toolArg); 103 } 104 } 105 launcher.addToolArg(Integer.toString(ProcessTools.getProcessId())); 106 107 processBuilder.command(launcher.getCommand()); 108 System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); 109 OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); 110 System.out.println(output.getOutput()); 111 112 return output; 113 } 114 115 }