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