test/serviceability/sa/jmap-hprof/JMapHProfLargeHeapTest.java

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6717
09f19d3de485
parent 0
f90c822e73f8
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import java.io.BufferedReader;
aoqi@0 25 import java.io.File;
aoqi@0 26 import java.io.FileNotFoundException;
aoqi@0 27 import java.io.FileReader;
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.io.Reader;
aoqi@0 30 import java.nio.CharBuffer;
aoqi@0 31 import java.util.Arrays;
aoqi@0 32 import java.util.Scanner;
aoqi@0 33
aoqi@0 34 import com.oracle.java.testlibrary.Asserts;
aoqi@0 35 import com.oracle.java.testlibrary.JDKToolFinder;
aoqi@0 36 import com.oracle.java.testlibrary.JDKToolLauncher;
aoqi@0 37 import com.oracle.java.testlibrary.OutputAnalyzer;
aoqi@0 38 import com.oracle.java.testlibrary.Platform;
aoqi@0 39 import com.oracle.java.testlibrary.ProcessTools;
aoqi@0 40
aoqi@0 41 /*
aoqi@0 42 * @test
aoqi@0 43 * @bug 6313383
aoqi@0 44 * @key regression
aoqi@0 45 * @summary Regression test for hprof export issue due to large heaps (>2G)
aoqi@0 46 * @library /testlibrary
aoqi@0 47 * @build com.oracle.java.testlibrary.* JMapHProfLargeHeapProc
aoqi@0 48 * @run main JMapHProfLargeHeapTest
aoqi@0 49 */
aoqi@0 50
aoqi@0 51 public class JMapHProfLargeHeapTest {
aoqi@0 52 private static final String HEAP_DUMP_FILE_NAME = "heap.hprof";
aoqi@0 53 private static final String HPROF_HEADER_1_0_1 = "JAVA PROFILE 1.0.1";
aoqi@0 54 private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2";
aoqi@0 55 private static final long M = 1024L;
aoqi@0 56 private static final long G = 1024L * M;
aoqi@0 57
aoqi@0 58 public static void main(String[] args) throws Exception {
aoqi@0 59 // If we are on MacOSX, test if JMap tool is signed, otherwise return
aoqi@0 60 // since test will fail with privilege error.
aoqi@0 61 if (Platform.isOSX()) {
aoqi@0 62 String jmapToolPath = JDKToolFinder.getTestJDKTool("jmap");
aoqi@0 63 ProcessBuilder codesignProcessBuilder = new ProcessBuilder(
aoqi@0 64 "codesign", "-v", jmapToolPath);
aoqi@0 65 Process codesignProcess = codesignProcessBuilder.start();
aoqi@0 66 OutputAnalyzer analyser = new OutputAnalyzer(codesignProcess);
aoqi@0 67 try {
aoqi@0 68 analyser.shouldNotContain("code object is not signed at all");
aoqi@0 69 System.out.println("Signed jmap found at: " + jmapToolPath);
aoqi@0 70 } catch (Exception e) {
aoqi@0 71 // Abort since we can't know if the test will work
aoqi@0 72 System.out
aoqi@0 73 .println("Test aborted since we are on MacOSX and the jmap tool is not signed.");
aoqi@0 74 return;
aoqi@0 75 }
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 // Small heap 22 megabytes, should create 1.0.1 file format
aoqi@0 79 testHProfFileFormat("-Xmx1g", 22 * M, HPROF_HEADER_1_0_1);
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * This test was deliberately commented out since the test system lacks
aoqi@0 83 * support to handle the requirements for this kind of heap size in a
aoqi@0 84 * good way. If or when it becomes possible to run this kind of tests in
aoqi@0 85 * the test environment the test should be enabled again.
aoqi@0 86 * */
aoqi@0 87 // Large heap 2,2 gigabytes, should create 1.0.2 file format
aoqi@0 88 // testHProfFileFormat("-Xmx4g", 2 * G + 2 * M, HPROF_HEADER_1_0_2);
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 private static void testHProfFileFormat(String vmArgs, long heapSize,
aoqi@0 92 String expectedFormat) throws Exception, IOException,
aoqi@0 93 InterruptedException, FileNotFoundException {
aoqi@0 94 ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(
aoqi@0 95 vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize));
aoqi@0 96 procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
aoqi@0 97 Process largeHeapProc = procBuilder.start();
aoqi@0 98
aoqi@0 99 try (Scanner largeHeapScanner = new Scanner(
aoqi@0 100 largeHeapProc.getInputStream());) {
aoqi@0 101 String pidstring = null;
aoqi@0 102 while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) {
aoqi@0 103 Thread.sleep(500);
aoqi@0 104 }
aoqi@0 105 int pid = Integer.parseInt(pidstring.substring(4,
aoqi@0 106 pidstring.length() - 1));
aoqi@0 107 System.out.println("Extracted pid: " + pid);
aoqi@0 108
aoqi@0 109 JDKToolLauncher jMapLauncher = JDKToolLauncher
aoqi@0 110 .createUsingTestJDK("jmap");
aoqi@0 111 jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-"
aoqi@0 112 + HEAP_DUMP_FILE_NAME);
aoqi@0 113 jMapLauncher.addToolArg(String.valueOf(pid));
aoqi@0 114
aoqi@0 115 ProcessBuilder jMapProcessBuilder = new ProcessBuilder(
aoqi@0 116 jMapLauncher.getCommand());
aoqi@0 117 System.out.println("jmap command: "
aoqi@0 118 + Arrays.toString(jMapLauncher.getCommand()));
aoqi@0 119
aoqi@0 120 Process jMapProcess = jMapProcessBuilder.start();
aoqi@0 121 OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess);
aoqi@0 122 analyzer.shouldHaveExitValue(0);
aoqi@0 123 analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME);
aoqi@0 124 analyzer.shouldContain("Heap dump file created");
aoqi@0 125
aoqi@0 126 largeHeapProc.getOutputStream().write('\n');
aoqi@0 127
aoqi@0 128 File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME);
aoqi@0 129 Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found.");
aoqi@0 130
aoqi@0 131 try (Reader reader = new BufferedReader(new FileReader(dumpFile))) {
aoqi@0 132 CharBuffer buf = CharBuffer.allocate(expectedFormat.length());
aoqi@0 133 reader.read(buf);
aoqi@0 134 buf.clear();
aoqi@0 135 Asserts.assertEQ(buf.toString(), expectedFormat,
aoqi@0 136 "Wrong file format. Expected '" + expectedFormat
aoqi@0 137 + "', but found '" + buf.toString() + "'");
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 System.out.println("Success!");
aoqi@0 141
aoqi@0 142 } finally {
aoqi@0 143 largeHeapProc.destroyForcibly();
aoqi@0 144 }
aoqi@0 145 }
aoqi@0 146 }

mercurial