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 } | 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 } |