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