1 /*
   2  * Copyright (c) 2005, 2020, 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.test.lib.Asserts.assertTrue;
  25 import static jdk.test.lib.Asserts.fail;
  26 
  27 import java.io.File;
  28 import java.util.Arrays;
  29 
  30 import jdk.test.lib.JDKToolLauncher;
  31 import jdk.test.lib.Utils;
  32 import jdk.test.lib.hprof.HprofParser;
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import jdk.test.lib.process.ProcessTools;
  35 
  36 /*
  37  * @test
  38  * @summary Unit test for jmap utility
  39  * @key intermittent
  40  * @library /test/lib
  41  * @build jdk.test.lib.hprof.*
  42  * @build jdk.test.lib.hprof.model.*
  43  * @build jdk.test.lib.hprof.parser.*
  44  * @build jdk.test.lib.hprof.util.*
  45  * @run main/timeout=240 BasicJMapTest
  46  */
  47 public class BasicJMapTest {
  48 
  49     private static ProcessBuilder processBuilder = new ProcessBuilder();
  50 
  51     public static void main(String[] args) throws Exception {
  52         testHisto();
  53         testHistoLive();
  54         testHistoAll();
  55         testHistoToFile();
  56         testHistoLiveToFile();
  57         testHistoAllToFile();
  58         testFinalizerInfo();
  59         testClstats();
  60         testDump();
  61         testDumpLive();
  62         testDumpAll();
  63     }
  64 
  65     private static void testHisto() throws Exception {
  66         OutputAnalyzer output = jmap("-histo:");
  67         output.shouldHaveExitValue(0);
  68         OutputAnalyzer output1 = jmap("-histo");
  69         output1.shouldHaveExitValue(0);
  70     }
  71 
  72     private static void testHistoLive() throws Exception {
  73         OutputAnalyzer output = jmap("-histo:live");
  74         output.shouldHaveExitValue(0);
  75     }
  76 
  77     private static void testHistoAll() throws Exception {
  78         OutputAnalyzer output = jmap("-histo:all");
  79         output.shouldHaveExitValue(0);
  80     }
  81 
  82     private static void testHistoToFile() throws Exception {
  83         histoToFile(false);
  84     }
  85 
  86     private static void testHistoLiveToFile() throws Exception {
  87         histoToFile(true);
  88     }
  89 
  90     private static void testHistoAllToFile() throws Exception {
  91         boolean explicitAll = true;
  92         histoToFile(false, explicitAll);
  93     }
  94 
  95     private static void histoToFile(boolean live) throws Exception {
  96         boolean explicitAll = false;
  97         histoToFile(live, explicitAll);
  98     }
  99 
 100     private static void histoToFile(boolean live, boolean explicitAll) throws Exception {
 101         if (live == true && explicitAll == true) {
 102             fail("Illegal argument setting for jmap -histo");
 103         }
 104         File file = new File("jmap.histo.file" + System.currentTimeMillis() + ".histo");
 105         if (file.exists()) {
 106             file.delete();
 107         }
 108         OutputAnalyzer output;
 109         if (live) {
 110             output = jmap("-histo:live,file=" + file.getName());
 111         } else if (explicitAll == true) {
 112             output = jmap("-histo:all,file=" + file.getName());
 113         } else {
 114             output = jmap("-histo:file=" + file.getName());
 115         }
 116         output.shouldHaveExitValue(0);
 117         output.shouldContain("Heap inspection file created");
 118         file.delete();
 119     }
 120 
 121     private static void testFinalizerInfo() throws Exception {
 122         OutputAnalyzer output = jmap("-finalizerinfo");
 123         output.shouldHaveExitValue(0);
 124     }
 125 
 126     private static void testClstats() throws Exception {
 127         OutputAnalyzer output = jmap("-clstats");
 128         output.shouldHaveExitValue(0);
 129     }
 130 
 131     private static void testDump() throws Exception {
 132         dump(false);
 133     }
 134 
 135     private static void testDumpLive() throws Exception {
 136         dump(true);
 137     }
 138 
 139     private static void testDumpAll() throws Exception {
 140         boolean explicitAll = true;
 141         dump(false, explicitAll);
 142     }
 143 
 144     private static void dump(boolean live) throws Exception {
 145         boolean explicitAll = false;
 146         dump(live, explicitAll);
 147     }
 148 
 149     private static void dump(boolean live, boolean explicitAll) throws Exception {
 150         if (live == true && explicitAll == true) {
 151           fail("Illegal argument setting for jmap -dump");
 152         }
 153         File dump = new File("jmap.dump." + System.currentTimeMillis() + ".hprof");
 154         if (dump.exists()) {
 155             dump.delete();
 156         }
 157         OutputAnalyzer output;
 158         if (live) {
 159             output = jmap("-dump:live,format=b,file=" + dump.getName());
 160         } else if (explicitAll == true) {
 161             output = jmap("-dump:all,format=b,file=" + dump.getName());
 162         } else {
 163             output = jmap("-dump:format=b,file=" + dump.getName());
 164         }
 165         output.shouldHaveExitValue(0);
 166         output.shouldContain("Heap dump file created");
 167         verifyDumpFile(dump);
 168         dump.delete();
 169     }
 170 
 171     private static void verifyDumpFile(File dump) {
 172         assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
 173         try {
 174             HprofParser.parse(dump);
 175         } catch (Exception e) {
 176             e.printStackTrace();
 177             fail("Could not parse dump file " + dump.getAbsolutePath());
 178         }
 179     }
 180 
 181     private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
 182         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
 183         launcher.addVMArgs(Utils.getTestJavaOpts());
 184         if (toolArgs != null) {
 185             for (String toolArg : toolArgs) {
 186                 launcher.addToolArg(toolArg);
 187             }
 188         }
 189         launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
 190 
 191         processBuilder.command(launcher.getCommand());
 192         System.out.println(Arrays.toString(processBuilder.command().toArray()));
 193         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
 194         System.out.println(output.getOutput());
 195 
 196         return output;
 197     }
 198 
 199 }