sla@5845: /* sla@5845: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. sla@5845: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. sla@5845: * sla@5845: * This code is free software; you can redistribute it and/or modify it sla@5845: * under the terms of the GNU General Public License version 2 only, as sla@5845: * published by the Free Software Foundation. sla@5845: * sla@5845: * This code is distributed in the hope that it will be useful, but WITHOUT sla@5845: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or sla@5845: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License sla@5845: * version 2 for more details (a copy is included in the LICENSE file that sla@5845: * accompanied this code). sla@5845: * sla@5845: * You should have received a copy of the GNU General Public License version sla@5845: * 2 along with this work; if not, write to the Free Software Foundation, sla@5845: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. sla@5845: * sla@5845: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA sla@5845: * or visit www.oracle.com if you need additional information or have any sla@5845: * questions. sla@5845: */ sla@5845: sla@5845: import java.io.BufferedReader; sla@5845: import java.io.File; sla@5845: import java.io.FileNotFoundException; sla@5845: import java.io.FileReader; sla@5845: import java.io.IOException; sla@5845: import java.io.Reader; sla@5845: import java.nio.CharBuffer; sla@5845: import java.util.Arrays; sla@5845: import java.util.Scanner; sla@5845: sla@5845: import com.oracle.java.testlibrary.Asserts; sla@5845: import com.oracle.java.testlibrary.JDKToolFinder; sla@5845: import com.oracle.java.testlibrary.JDKToolLauncher; sla@5845: import com.oracle.java.testlibrary.OutputAnalyzer; sla@5845: import com.oracle.java.testlibrary.Platform; sla@5845: import com.oracle.java.testlibrary.ProcessTools; sla@5845: sla@5845: /* sla@5845: * @test sla@5845: * @bug 6313383 sla@5845: * @key regression sla@5845: * @summary Regression test for hprof export issue due to large heaps (>2G) sla@5845: * @library /testlibrary sla@5845: * @compile JMapHProfLargeHeapProc.java sla@5845: * @run main JMapHProfLargeHeapTest sla@5845: */ sla@5845: sla@5845: public class JMapHProfLargeHeapTest { sla@5845: private static final String HEAP_DUMP_FILE_NAME = "heap.hprof"; sla@5845: private static final String HPROF_HEADER_1_0_1 = "JAVA PROFILE 1.0.1"; sla@5845: private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2"; sla@5845: private static final long M = 1024L; sla@5845: private static final long G = 1024L * M; sla@5845: sla@5845: public static void main(String[] args) throws Exception { sla@5845: // If we are on MacOSX, test if JMap tool is signed, otherwise return sla@5845: // since test will fail with privilege error. sla@5845: if (Platform.isOSX()) { tschatzl@5934: String jmapToolPath = JDKToolFinder.getTestJDKTool("jmap"); sla@5845: ProcessBuilder codesignProcessBuilder = new ProcessBuilder( sla@5845: "codesign", "-v", jmapToolPath); sla@5845: Process codesignProcess = codesignProcessBuilder.start(); sla@5845: OutputAnalyzer analyser = new OutputAnalyzer(codesignProcess); sla@5845: try { sla@5845: analyser.shouldNotContain("code object is not signed at all"); sla@5845: System.out.println("Signed jmap found at: " + jmapToolPath); sla@5845: } catch (Exception e) { sla@5845: // Abort since we can't know if the test will work sla@5845: System.out sla@5845: .println("Test aborted since we are on MacOSX and the jmap tool is not signed."); sla@5845: return; sla@5845: } sla@5845: } sla@5845: sla@5845: // Small heap 22 megabytes, should create 1.0.1 file format sla@5845: testHProfFileFormat("-Xmx1g", 22 * M, HPROF_HEADER_1_0_1); sla@5845: sla@5845: /** sla@5845: * This test was deliberately commented out since the test system lacks sla@5845: * support to handle the requirements for this kind of heap size in a sla@5845: * good way. If or when it becomes possible to run this kind of tests in sla@5845: * the test environment the test should be enabled again. sla@5845: * */ sla@5845: // Large heap 2,2 gigabytes, should create 1.0.2 file format sla@5845: // testHProfFileFormat("-Xmx4g", 2 * G + 2 * M, HPROF_HEADER_1_0_2); sla@5845: } sla@5845: sla@5845: private static void testHProfFileFormat(String vmArgs, long heapSize, sla@5845: String expectedFormat) throws Exception, IOException, sla@5845: InterruptedException, FileNotFoundException { sla@5845: ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder( sla@5845: vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize)); sla@5845: procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); sla@5845: Process largeHeapProc = procBuilder.start(); sla@5845: sla@5845: try (Scanner largeHeapScanner = new Scanner( sla@5845: largeHeapProc.getInputStream());) { sla@5845: String pidstring = null; sla@5845: while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) { sla@5845: Thread.sleep(500); sla@5845: } sla@5845: int pid = Integer.parseInt(pidstring.substring(4, sla@5845: pidstring.length() - 1)); sla@5845: System.out.println("Extracted pid: " + pid); sla@5845: sla@5845: JDKToolLauncher jMapLauncher = JDKToolLauncher sla@5845: .create("jmap", false); sla@5845: jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-" sla@5845: + HEAP_DUMP_FILE_NAME); sla@5845: jMapLauncher.addToolArg(String.valueOf(pid)); sla@5845: sla@5845: ProcessBuilder jMapProcessBuilder = new ProcessBuilder( sla@5845: jMapLauncher.getCommand()); sla@5845: System.out.println("jmap command: " sla@5845: + Arrays.toString(jMapLauncher.getCommand())); sla@5845: sla@5845: Process jMapProcess = jMapProcessBuilder.start(); sla@5845: OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess); sla@5845: analyzer.shouldHaveExitValue(0); sla@5845: analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME); sla@5845: analyzer.shouldContain("Heap dump file created"); sla@5845: sla@5845: largeHeapProc.getOutputStream().write('\n'); sla@5845: sla@5845: File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME); sla@5845: Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found."); sla@5845: sla@5845: try (Reader reader = new BufferedReader(new FileReader(dumpFile))) { sla@5845: CharBuffer buf = CharBuffer.allocate(expectedFormat.length()); sla@5845: reader.read(buf); sla@5845: buf.clear(); sla@5845: Asserts.assertEQ(buf.toString(), expectedFormat, sla@5845: "Wrong file format. Expected '" + expectedFormat sla@5845: + "', but found '" + buf.toString() + "'"); sla@5845: } sla@5845: sla@5845: System.out.println("Success!"); sla@5845: sla@5845: } finally { sla@5845: largeHeapProc.destroyForcibly(); sla@5845: } sla@5845: } sla@5845: }