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