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