1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/serviceability/sa/jmap-hprof/JMapHProfLargeHeapTest.java Fri Oct 04 13:44:49 2013 +0200 1.3 @@ -0,0 +1,146 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.BufferedReader; 1.28 +import java.io.File; 1.29 +import java.io.FileNotFoundException; 1.30 +import java.io.FileReader; 1.31 +import java.io.IOException; 1.32 +import java.io.Reader; 1.33 +import java.nio.CharBuffer; 1.34 +import java.util.Arrays; 1.35 +import java.util.Scanner; 1.36 + 1.37 +import com.oracle.java.testlibrary.Asserts; 1.38 +import com.oracle.java.testlibrary.JDKToolFinder; 1.39 +import com.oracle.java.testlibrary.JDKToolLauncher; 1.40 +import com.oracle.java.testlibrary.OutputAnalyzer; 1.41 +import com.oracle.java.testlibrary.Platform; 1.42 +import com.oracle.java.testlibrary.ProcessTools; 1.43 + 1.44 +/* 1.45 + * @test 1.46 + * @bug 6313383 1.47 + * @key regression 1.48 + * @summary Regression test for hprof export issue due to large heaps (>2G) 1.49 + * @library /testlibrary 1.50 + * @compile JMapHProfLargeHeapProc.java 1.51 + * @run main JMapHProfLargeHeapTest 1.52 + */ 1.53 + 1.54 +public class JMapHProfLargeHeapTest { 1.55 + private static final String HEAP_DUMP_FILE_NAME = "heap.hprof"; 1.56 + private static final String HPROF_HEADER_1_0_1 = "JAVA PROFILE 1.0.1"; 1.57 + private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2"; 1.58 + private static final long M = 1024L; 1.59 + private static final long G = 1024L * M; 1.60 + 1.61 + public static void main(String[] args) throws Exception { 1.62 + // If we are on MacOSX, test if JMap tool is signed, otherwise return 1.63 + // since test will fail with privilege error. 1.64 + if (Platform.isOSX()) { 1.65 + String jmapToolPath = JDKToolFinder.getCurrentJDKTool("jmap"); 1.66 + ProcessBuilder codesignProcessBuilder = new ProcessBuilder( 1.67 + "codesign", "-v", jmapToolPath); 1.68 + Process codesignProcess = codesignProcessBuilder.start(); 1.69 + OutputAnalyzer analyser = new OutputAnalyzer(codesignProcess); 1.70 + try { 1.71 + analyser.shouldNotContain("code object is not signed at all"); 1.72 + System.out.println("Signed jmap found at: " + jmapToolPath); 1.73 + } catch (Exception e) { 1.74 + // Abort since we can't know if the test will work 1.75 + System.out 1.76 + .println("Test aborted since we are on MacOSX and the jmap tool is not signed."); 1.77 + return; 1.78 + } 1.79 + } 1.80 + 1.81 + // Small heap 22 megabytes, should create 1.0.1 file format 1.82 + testHProfFileFormat("-Xmx1g", 22 * M, HPROF_HEADER_1_0_1); 1.83 + 1.84 + /** 1.85 + * This test was deliberately commented out since the test system lacks 1.86 + * support to handle the requirements for this kind of heap size in a 1.87 + * good way. If or when it becomes possible to run this kind of tests in 1.88 + * the test environment the test should be enabled again. 1.89 + * */ 1.90 + // Large heap 2,2 gigabytes, should create 1.0.2 file format 1.91 + // testHProfFileFormat("-Xmx4g", 2 * G + 2 * M, HPROF_HEADER_1_0_2); 1.92 + } 1.93 + 1.94 + private static void testHProfFileFormat(String vmArgs, long heapSize, 1.95 + String expectedFormat) throws Exception, IOException, 1.96 + InterruptedException, FileNotFoundException { 1.97 + ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder( 1.98 + vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize)); 1.99 + procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); 1.100 + Process largeHeapProc = procBuilder.start(); 1.101 + 1.102 + try (Scanner largeHeapScanner = new Scanner( 1.103 + largeHeapProc.getInputStream());) { 1.104 + String pidstring = null; 1.105 + while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) { 1.106 + Thread.sleep(500); 1.107 + } 1.108 + int pid = Integer.parseInt(pidstring.substring(4, 1.109 + pidstring.length() - 1)); 1.110 + System.out.println("Extracted pid: " + pid); 1.111 + 1.112 + JDKToolLauncher jMapLauncher = JDKToolLauncher 1.113 + .create("jmap", false); 1.114 + jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-" 1.115 + + HEAP_DUMP_FILE_NAME); 1.116 + jMapLauncher.addToolArg(String.valueOf(pid)); 1.117 + 1.118 + ProcessBuilder jMapProcessBuilder = new ProcessBuilder( 1.119 + jMapLauncher.getCommand()); 1.120 + System.out.println("jmap command: " 1.121 + + Arrays.toString(jMapLauncher.getCommand())); 1.122 + 1.123 + Process jMapProcess = jMapProcessBuilder.start(); 1.124 + OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess); 1.125 + analyzer.shouldHaveExitValue(0); 1.126 + analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME); 1.127 + analyzer.shouldContain("Heap dump file created"); 1.128 + 1.129 + largeHeapProc.getOutputStream().write('\n'); 1.130 + 1.131 + File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME); 1.132 + Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found."); 1.133 + 1.134 + try (Reader reader = new BufferedReader(new FileReader(dumpFile))) { 1.135 + CharBuffer buf = CharBuffer.allocate(expectedFormat.length()); 1.136 + reader.read(buf); 1.137 + buf.clear(); 1.138 + Asserts.assertEQ(buf.toString(), expectedFormat, 1.139 + "Wrong file format. Expected '" + expectedFormat 1.140 + + "', but found '" + buf.toString() + "'"); 1.141 + } 1.142 + 1.143 + System.out.println("Success!"); 1.144 + 1.145 + } finally { 1.146 + largeHeapProc.destroyForcibly(); 1.147 + } 1.148 + } 1.149 +}