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.JDKToolLauncher;
  36 import jdk.test.lib.Platform;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import jdk.test.lib.process.ProcessTools;
  39 
  40 /*
  41  * @test
  42  * @bug 6313383
  43  * @key regression
  44  * @summary Regression test for hprof export issue due to large heaps (>2G)
  45  * @library /test/lib
  46  * @modules java.base/jdk.internal.misc
  47  *          java.compiler
  48  *          java.management/sun.management
  49  *          jdk.jvmstat/sun.jvmstat.monitor
  50  * @build JMapHProfLargeHeapProc
  51  * @run main JMapHProfLargeHeapTest
  52  */
  53 
  54 public class JMapHProfLargeHeapTest {
  55     private static final String HEAP_DUMP_FILE_NAME = "heap.bin";
  56     private static final String HPROF_HEADER_1_0_1 = "JAVA PROFILE 1.0.1";
  57     private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2";
  58     private static final long M = 1024L;
  59     private static final long G = 1024L * M;
  60 
  61     public static void main(String[] args) throws Exception {
  62         if (!Platform.shouldSAAttach()) {
  63             System.out.println("SA attach not expected to work - test skipped.");
  64             return;
  65         }
  66 
  67         // All heap dumps should create 1.0.2 file format
  68         // Hotspot internal heapdumper always use HPROF_HEADER_1_0_2 format,
  69         // but SA heapdumper still use HPROF_HEADER_1_0_1 for small heaps
  70         testHProfFileFormat("-Xmx1g", 22 * M, HPROF_HEADER_1_0_1);
  71 
  72         /**
  73          * This test was deliberately commented out since the test system lacks
  74          * support to handle the requirements for this kind of heap size in a
  75          * good way. If or when it becomes possible to run this kind of tests in
  76          * the test environment the test should be enabled again.
  77          * */
  78         // Large heap 2,2 gigabytes, should create 1.0.2 file format
  79         // testHProfFileFormat("-Xmx4g", 2 * G + 2 * M, HPROF_HEADER_1_0_2);
  80     }
  81 
  82     private static void testHProfFileFormat(String vmArgs, long heapSize,
  83             String expectedFormat) throws Exception, IOException,
  84             InterruptedException, FileNotFoundException {
  85         ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(
  86                 "--add-exports=java.management/sun.management=ALL-UNNAMED",
  87                 "--add-opens=java.management/sun.management=ALL-UNNAMED",
  88                 vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize));
  89         procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  90         Process largeHeapProc = procBuilder.start();
  91 
  92         try (Scanner largeHeapScanner = new Scanner(
  93                 largeHeapProc.getInputStream());) {
  94             String pidstring = null;
  95             if (!largeHeapScanner.hasNext()) {
  96                throw new RuntimeException ("Test failed: could not open largeHeapScanner.");
  97             }
  98             while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) {
  99                 Thread.sleep(500);
 100             }
 101             int pid = Integer.parseInt(pidstring.substring(4,
 102                     pidstring.length() - 1));
 103             System.out.println("Extracted pid: " + pid);
 104 
 105             JDKToolLauncher jMapLauncher = JDKToolLauncher
 106                     .createUsingTestJDK("jhsdb");
 107             jMapLauncher.addToolArg("jmap");
 108             jMapLauncher.addToolArg("--binaryheap");
 109             jMapLauncher.addToolArg("--pid");
 110             jMapLauncher.addToolArg(String.valueOf(pid));
 111 
 112             ProcessBuilder jMapProcessBuilder = new ProcessBuilder(
 113                     jMapLauncher.getCommand());
 114             System.out.println("jmap command: "
 115                     + Arrays.toString(jMapLauncher.getCommand()));
 116 
 117             Process jMapProcess = jMapProcessBuilder.start();
 118             OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess);
 119             analyzer.shouldHaveExitValue(0);
 120             analyzer.shouldContain(HEAP_DUMP_FILE_NAME);
 121 
 122             largeHeapProc.getOutputStream().write('\n');
 123 
 124             File dumpFile = new File(HEAP_DUMP_FILE_NAME);
 125             Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found.");
 126 
 127             try (Reader reader = new BufferedReader(new FileReader(dumpFile))) {
 128                 CharBuffer buf = CharBuffer.allocate(expectedFormat.length());
 129                 reader.read(buf);
 130                 buf.clear();
 131                 Asserts.assertEQ(buf.toString(), expectedFormat,
 132                         "Wrong file format. Expected '" + expectedFormat
 133                                 + "', but found '" + buf.toString() + "'");
 134             }
 135 
 136             System.out.println("Success!");
 137 
 138         } finally {
 139             largeHeapProc.destroyForcibly();
 140         }
 141     }
 142 }