1 /*
   2  * Copyright (c) 2013, 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 com.oracle.java.testlibrary.Asserts;
  35 import com.oracle.java.testlibrary.JDKToolFinder;
  36 import com.oracle.java.testlibrary.JDKToolLauncher;
  37 import com.oracle.java.testlibrary.OutputAnalyzer;
  38 import com.oracle.java.testlibrary.Platform;
  39 import com.oracle.java.testlibrary.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  * @library /testlibrary
  47  * @build com.oracle.java.testlibrary.* JMapHProfLargeHeapProc
  48  * @run main JMapHProfLargeHeapTest
  49  */
  50 
  51 public class JMapHProfLargeHeapTest {
  52     private static final String HEAP_DUMP_FILE_NAME = "heap.hprof";
  53     private static final String HPROF_HEADER_1_0_1 = "JAVA PROFILE 1.0.1";
  54     private static final String HPROF_HEADER_1_0_2 = "JAVA PROFILE 1.0.2";
  55     private static final long M = 1024L;
  56     private static final long G = 1024L * M;
  57 
  58     public static void main(String[] args) throws Exception {
  59         // If we are on MacOSX, test if JMap tool is signed, otherwise return
  60         // since test will fail with privilege error.
  61         if (Platform.isOSX()) {
  62             String jmapToolPath = JDKToolFinder.getTestJDKTool("jmap");
  63             ProcessBuilder codesignProcessBuilder = new ProcessBuilder(
  64                     "codesign", "-v", jmapToolPath);
  65             Process codesignProcess = codesignProcessBuilder.start();
  66             OutputAnalyzer analyser = new OutputAnalyzer(codesignProcess);
  67             try {
  68                 analyser.shouldNotContain("code object is not signed at all");
  69                 System.out.println("Signed jmap found at: " + jmapToolPath);
  70             } catch (Exception e) {
  71                 // Abort since we can't know if the test will work
  72                 System.out
  73                         .println("Test aborted since we are on MacOSX and the jmap tool is not signed.");
  74                 return;
  75             }
  76         }
  77 
  78         // Small heap 22 megabytes, should create 1.0.1 file format
  79         testHProfFileFormat("-Xmx1g", 22 * M, HPROF_HEADER_1_0_1);
  80 
  81         /**
  82          * This test was deliberately commented out since the test system lacks
  83          * support to handle the requirements for this kind of heap size in a
  84          * good way. If or when it becomes possible to run this kind of tests in
  85          * the test environment the test should be enabled again.
  86          * */
  87         // Large heap 2,2 gigabytes, should create 1.0.2 file format
  88         // testHProfFileFormat("-Xmx4g", 2 * G + 2 * M, HPROF_HEADER_1_0_2);
  89     }
  90 
  91     private static void testHProfFileFormat(String vmArgs, long heapSize,
  92             String expectedFormat) throws Exception, IOException,
  93             InterruptedException, FileNotFoundException {
  94         ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(
  95                 vmArgs, "JMapHProfLargeHeapProc", String.valueOf(heapSize));
  96         procBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  97         Process largeHeapProc = procBuilder.start();
  98 
  99         try (Scanner largeHeapScanner = new Scanner(
 100                 largeHeapProc.getInputStream());) {
 101             String pidstring = null;
 102             while ((pidstring = largeHeapScanner.findInLine("PID\\[[0-9].*\\]")) == null) {
 103                 Thread.sleep(500);
 104             }
 105             int pid = Integer.parseInt(pidstring.substring(4,
 106                     pidstring.length() - 1));
 107             System.out.println("Extracted pid: " + pid);
 108 
 109             JDKToolLauncher jMapLauncher = JDKToolLauncher
 110                     .createUsingTestJDK("jmap");
 111             jMapLauncher.addToolArg("-dump:format=b,file=" + pid + "-"
 112                     + HEAP_DUMP_FILE_NAME);
 113             jMapLauncher.addToolArg(String.valueOf(pid));
 114 
 115             ProcessBuilder jMapProcessBuilder = new ProcessBuilder(
 116                     jMapLauncher.getCommand());
 117             System.out.println("jmap command: "
 118                     + Arrays.toString(jMapLauncher.getCommand()));
 119 
 120             Process jMapProcess = jMapProcessBuilder.start();
 121             OutputAnalyzer analyzer = new OutputAnalyzer(jMapProcess);
 122             analyzer.shouldHaveExitValue(0);
 123             analyzer.shouldContain(pid + "-" + HEAP_DUMP_FILE_NAME);
 124             analyzer.shouldContain("Heap dump file created");
 125 
 126             largeHeapProc.getOutputStream().write('\n');
 127 
 128             File dumpFile = new File(pid + "-" + HEAP_DUMP_FILE_NAME);
 129             Asserts.assertTrue(dumpFile.exists(), "Heap dump file not found.");
 130 
 131             try (Reader reader = new BufferedReader(new FileReader(dumpFile))) {
 132                 CharBuffer buf = CharBuffer.allocate(expectedFormat.length());
 133                 reader.read(buf);
 134                 buf.clear();
 135                 Asserts.assertEQ(buf.toString(), expectedFormat,
 136                         "Wrong file format. Expected '" + expectedFormat
 137                                 + "', but found '" + buf.toString() + "'");
 138             }
 139 
 140             System.out.println("Success!");
 141 
 142         } finally {
 143             largeHeapProc.destroyForcibly();
 144         }
 145     }
 146 }