Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/serviceability/sa/jmap-hprof/JMapHProfLargeHeapTest.java
32285 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedReader;24import java.io.File;25import java.io.FileNotFoundException;26import java.io.FileReader;27import java.io.IOException;28import java.io.Reader;29import java.nio.CharBuffer;30import java.util.Arrays;31import java.util.Scanner;3233import com.oracle.java.testlibrary.Asserts;34import com.oracle.java.testlibrary.JDKToolFinder;35import com.oracle.java.testlibrary.JDKToolLauncher;36import com.oracle.java.testlibrary.OutputAnalyzer;37import com.oracle.java.testlibrary.Platform;38import com.oracle.java.testlibrary.ProcessTools;3940/*41* @test42* @bug 631338343* @key regression44* @summary Regression test for hprof export issue due to large heaps (>2G)45* @library /testlibrary46* @build com.oracle.java.testlibrary.* JMapHProfLargeHeapProc47* @run main JMapHProfLargeHeapTest48*/4950public class JMapHProfLargeHeapTest {51private static final String HEAP_DUMP_FILE_NAME = "heap.hprof";52private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2";53private static final long M = 1024L;54private static final long G = 1024L * M;5556public static void main(String[] args) throws Exception {57// If we are on MacOSX, test if JMap tool is signed, otherwise return58// since test will fail with privilege error.59if (Platform.isOSX()) {60String jmapToolPath = JDKToolFinder.getTestJDKTool("jmap");61ProcessBuilder codesignProcessBuilder = new ProcessBuilder(62"codesign", "-v", jmapToolPath);63Process codesignProcess = codesignProcessBuilder.start();64OutputAnalyzer analyser = new OutputAnalyzer(codesignProcess);65try {66analyser.shouldNotContain("code object is not signed at all");67System.out.println("Signed jmap found at: " + jmapToolPath);68} catch (Exception e) {69// Abort since we can't know if the test will work70System.out71.println("Test aborted since we are on MacOSX and the jmap tool is not signed.");72return;73}74}7576// All heap dumps should create 1.0.2 file format77testHProfFileFormat("-Xmx1g", 22 * M, HPROF_HEADER_1_0_2);7879/**80* This test was deliberately commented out since the test system lacks81* support to handle the requirements for this kind of heap size in a82* good way. If or when it becomes possible to run this kind of tests in83* the test environment the test should be enabled again.84* */85// Large heap 2,2 gigabytes, should create 1.0.2 file format86// testHProfFileFormat("-Xmx4g", 2 * G + 2 * M, HPROF_HEADER_1_0_2);87}8889private static void testHProfFileFormat(String vmArgs, long heapSize,90String expectedFormat) throws Exception, IOException,91InterruptedException, FileNotFoundException {92ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(93vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize));94procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);95Process largeHeapProc = procBuilder.start();9697try (Scanner largeHeapScanner = new Scanner(98largeHeapProc.getInputStream());) {99String pidstring = null;100while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) {101Thread.sleep(500);102}103int pid = Integer.parseInt(pidstring.substring(4,104pidstring.length() - 1));105System.out.println("Extracted pid: " + pid);106107JDKToolLauncher jMapLauncher = JDKToolLauncher108.createUsingTestJDK("jmap");109jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-"110+ HEAP_DUMP_FILE_NAME);111jMapLauncher.addToolArg(String.valueOf(pid));112113ProcessBuilder jMapProcessBuilder = new ProcessBuilder(114jMapLauncher.getCommand());115System.out.println("jmap command: "116+ Arrays.toString(jMapLauncher.getCommand()));117118Process jMapProcess = jMapProcessBuilder.start();119OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess);120analyzer.shouldHaveExitValue(0);121analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME);122analyzer.shouldContain("Heap dump file created");123124largeHeapProc.getOutputStream().write('\n');125126File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME);127Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found.");128129try (Reader reader = new BufferedReader(new FileReader(dumpFile))) {130CharBuffer buf = CharBuffer.allocate(expectedFormat.length());131reader.read(buf);132buf.clear();133Asserts.assertEQ(buf.toString(), expectedFormat,134"Wrong file format. Expected '" + expectedFormat135+ "', but found '" + buf.toString() + "'");136}137138System.out.println("Success!");139140} finally {141largeHeapProc.destroyForcibly();142}143}144}145146147