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 100 }