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