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 testHistoParallelZero() throws Exception { 83 OutputAnalyzer output = jmap("-histo:parallel=0"); 84 output.shouldHaveExitValue(0); 85 } 86 87 private static void testHistoParallel() throws Exception { 88 OutputAnalyzer output = jmap("-histo:parallel=2"); 89 output.shouldHaveExitValue(0); 90 } 91 92 private static void testHistoNonParallel() throws Exception { 93 OutputAnalyzer output = jmap("-histo:parallel=1"); 94 output.shouldHaveExitValue(0); 95 } 96 97 private static void testHistoToFile() throws Exception { 98 histoToFile(false, false, 1); 99 } 100 101 private static void testHistoLiveToFile() throws Exception { 102 histoToFile(true, false, 1); 103 } 104 105 private static void testHistoAllToFile() throws Exception { 106 histoToFile(false, true, 1); 107 } 108 109 private static void testHistoFileParallelZero() throws Exception { 110 histoToFile(false, false, 0); 111 } 112 113 private static void testHistoFileParallel() throws Exception { 114 histoToFile(false, false, 2); 115 } 116 117 private static void histoToFile(boolean live, 118 boolean explicitAll, 119 int parallelThreadNum) throws Exception { 120 String liveArg = ""; 121 String fileArg = ""; 122 String parArg = "parallel=" + parallelThreadNum; 123 String allArgs = "-histo:"; 124 125 if (live && explicitAll) { 126 fail("Illegal argument setting for jmap -histo"); 127 } 128 if (live) { 129 liveArg = "live,"; 130 } 131 if (explicitAll) { 132 liveArg = "all,"; 133 } 134 135 File file = new File("jmap.histo.file" + System.currentTimeMillis() + ".histo"); 136 if (file.exists()) { 137 file.delete(); 138 } 139 fileArg = "file=" + file.getName(); 140 141 OutputAnalyzer output; 142 allArgs = allArgs + liveArg + fileArg + ',' + parArg; 143 output = jmap(allArgs); 144 output.shouldHaveExitValue(0); 145 output.shouldContain("Heap inspection file created"); 146 file.delete(); 147 } 148 149 private static void testFinalizerInfo() throws Exception { 150 OutputAnalyzer output = jmap("-finalizerinfo"); 151 output.shouldHaveExitValue(0); 152 } 153 154 private static void testClstats() throws Exception { 155 OutputAnalyzer output = jmap("-clstats"); 156 output.shouldHaveExitValue(0); 157 } 158 159 private static void testDump() throws Exception { 160 dump(false, false); 161 } 162 163 private static void testDumpLive() throws Exception { 164 dump(true, false); 165 } 166 167 private static void testDumpAll() throws Exception { 168 dump(false, true); 169 } 170 171 private static void dump(boolean live, boolean explicitAll) throws Exception { 172 String liveArg = ""; 173 String fileArg = ""; 174 String allArgs = "-dump:"; 175 176 if (live && explicitAll) { 177 fail("Illegal argument setting for jmap -dump"); 178 } 179 if (live) { 180 liveArg = "live,"; 181 } 182 if (explicitAll) { 183 liveArg = "all,"; 184 } 185 186 File file = new File("jmap.dump" + System.currentTimeMillis() + ".hprof"); 187 if (file.exists()) { 188 file.delete(); 189 } 190 fileArg = "file=" + file.getName(); 191 192 OutputAnalyzer output; 193 allArgs = allArgs + liveArg + "format=b," + fileArg; 194 output = jmap(allArgs); 195 output.shouldHaveExitValue(0); 196 output.shouldContain("Heap dump file created"); 197 verifyDumpFile(file); 198 file.delete(); 199 } 200 201 private static void verifyDumpFile(File dump) { 202 assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath()); 203 try { 204 HprofParser.parse(dump); 205 } catch (Exception e) { 206 e.printStackTrace(); 207 fail("Could not parse dump file " + dump.getAbsolutePath()); 208 } 209 } 210 211 private static OutputAnalyzer jmap(String... toolArgs) throws Exception { 212 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap"); 213 launcher.addVMArgs(Utils.getTestJavaOpts()); 214 if (toolArgs != null) { 215 for (String toolArg : toolArgs) { 216 launcher.addToolArg(toolArg); 217 } 218 } 219 launcher.addToolArg(Long.toString(ProcessTools.getProcessId())); 220 221 processBuilder.command(launcher.getCommand()); 222 System.out.println(Arrays.toString(processBuilder.command().toArray())); 223 OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); 224 System.out.println(output.getOutput()); 225 226 return output; 227 } 228 }