1 /*
   2  * Copyright (c) 2017, 2019, 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.util.ArrayList;
  25 import java.util.List;
  26 import java.io.File;
  27 import java.nio.file.Files;
  28 import java.io.IOException;
  29 import java.io.BufferedInputStream;
  30 import java.util.stream.Collectors;
  31 import java.io.FileInputStream;
  32 
  33 import sun.jvm.hotspot.HotSpotAgent;
  34 import sun.jvm.hotspot.debugger.*;
  35 
  36 import jdk.test.lib.apps.LingeredApp;
  37 import jdk.test.lib.JDKToolLauncher;
  38 import jdk.test.lib.JDKToolFinder;
  39 import jdk.test.lib.Platform;
  40 import jdk.test.lib.process.ProcessTools;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 import jdk.test.lib.Utils;
  43 import jdk.test.lib.Asserts;
  44 
  45 /**
  46  * @test
  47  * @library /test/lib
  48  * @bug 8171084
  49  * @requires vm.hasSAandCanAttach & (vm.bits == "64" & os.maxMemory > 8g)
  50  * @requires vm.gc != "Shenandoah"
  51  * @modules java.base/jdk.internal.misc
  52  *          jdk.hotspot.agent/sun.jvm.hotspot
  53  *          jdk.hotspot.agent/sun.jvm.hotspot.utilities
  54  *          jdk.hotspot.agent/sun.jvm.hotspot.oops
  55  *          jdk.hotspot.agent/sun.jvm.hotspot.debugger
  56  * @run main/timeout=1800/othervm -Xmx8g TestHeapDumpForLargeArray
  57  */
  58 
  59 public class TestHeapDumpForLargeArray {
  60 
  61     private static LingeredAppWithLargeArray theApp = null;
  62 
  63     private static void attachAndDump(String heapDumpFileName,
  64                                       long lingeredAppPid) throws Exception {
  65 
  66         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");
  67         launcher.addToolArg("jmap");
  68         launcher.addToolArg("--binaryheap");
  69         launcher.addToolArg("--dumpfile");
  70         launcher.addToolArg(heapDumpFileName);
  71         launcher.addToolArg("--pid");
  72         launcher.addToolArg(Long.toString(lingeredAppPid));
  73 
  74         ProcessBuilder processBuilder = new ProcessBuilder();
  75         processBuilder.command(launcher.getCommand());
  76         System.out.println(
  77             processBuilder.command().stream().collect(Collectors.joining(" ")));
  78 
  79         OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
  80         SAOutput.shouldHaveExitValue(0);
  81         SAOutput.shouldNotContain("Heap segment size overflow");
  82         SAOutput.shouldContain("truncating to");
  83         SAOutput.shouldContain("heap written to");
  84         SAOutput.shouldContain(heapDumpFileName);
  85         System.out.println(SAOutput.getOutput());
  86 
  87     }
  88 
  89     public static void main (String... args) throws Exception {
  90 
  91         String heapDumpFileName = "LargeArrayHeapDump.bin";
  92 
  93         File heapDumpFile = new File(heapDumpFileName);
  94         if (heapDumpFile.exists()) {
  95             heapDumpFile.delete();
  96         }
  97 
  98         try {
  99             // Need to add the default arguments first to have explicit
 100             // -Xmx8g last, otherwise test will fail if default
 101             // arguments contain a smaller -Xmx.
 102             List<String> vmArgs = new ArrayList<String>();
 103             vmArgs.addAll(Utils.getVmOptions());
 104             vmArgs.add("-XX:+UsePerfData");
 105             vmArgs.add("-Xmx8g");
 106 
 107             theApp = new LingeredAppWithLargeArray();
 108             LingeredApp.startApp(vmArgs, theApp);
 109             attachAndDump(heapDumpFileName, theApp.getPid());
 110         } finally {
 111             LingeredApp.stopApp(theApp);
 112             heapDumpFile.delete();
 113         }
 114     }
 115 }