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