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