< prev index next >

test/hotspot/jtreg/serviceability/sa/TestJmapCore.java

Print this page




   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 /*
  25  * @test TestJmapCore
  26  * @summary Test verifies that jhsdb jmap could generate heap dump from core
  27  * @modules java.base/jdk.internal.misc
  28  * @library /test/lib
  29  * @run driver TestJmapCore
  30  */
  31 
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.JDKToolFinder;
  34 import jdk.test.lib.JDKToolLauncher;
  35 import jdk.test.lib.Platform;
  36 import jdk.test.lib.classloader.GeneratingClassLoader;
  37 import jdk.test.lib.hprof.HprofParser;
  38 import jdk.test.lib.process.ProcessTools;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 
  41 import java.io.File;
  42 import java.util.Arrays;
  43 import java.util.stream.Collectors;
  44 
  45 public class TestJmapCore {
  46     static final String pidSeparator = ":KILLED_PID";
  47 




  48     public static void main(String[] args) throws Throwable {
  49         if (args.length == 1) {
  50             // If any arguments are set prints pid so main process coud find corefile
  51             System.out.println(ProcessHandle.current().pid() + pidSeparator);
  52             try {

  53                 Object[] oa = new Object[Integer.MAX_VALUE / 2];
  54                 for(int i = 0; i < oa.length; i++) {
  55                     oa[i] = new Object[Integer.MAX_VALUE / 2];
  56                 }






  57                 throw new Error("OOME not triggered");
  58             } catch (OutOfMemoryError err) {
  59                 return;
  60             }
  61         }
  62         test();
  63     }
  64 
  65     // Test tries to run java with ulimit unlimited if it is possible
  66     static boolean useDefaultUlimit() {
  67         if (Platform.isWindows()) {
  68             return true;
  69         }
  70         try {
  71             OutputAnalyzer output = ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && ulimit -c");
  72             return !(output.getExitValue() == 0 && output.getStdout().contains("unlimited"));
  73         } catch (Throwable t) {
  74             return true;
  75         }
  76     }
  77 
  78     static void test() throws Throwable {
  79         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-XX:+CreateCoredumpOnCrash",
  80                 "-XX:+CrashOnOutOfMemoryError", "-XX:-TransmitErrorReport",
  81                 TestJmapCore.class.getName(), "throwOOME");
  82 
  83         boolean useDefaultUlimit = useDefaultUlimit();
  84         System.out.println("Run test with ulimit: " + (useDefaultUlimit ? "default" : "unlimited"));
  85         OutputAnalyzer output = useDefaultUlimit
  86             ? ProcessTools.executeProcess(pb)
  87             : ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && "
  88                     + ProcessTools.getCommandLine(pb));
  89         File core;
  90         String pattern = Platform.isWindows() ? "mdmp" : "core";
  91         File[] cores = new File(".").listFiles((dir, name) -> name.contains(pattern));
  92         if (cores.length == 0) {
  93             // /cores/core.$pid might be generated on macosx by default
  94             String pid = output.firstMatch("^(\\d+)" + pidSeparator, 1);
  95             core = new File("cores/core." + pid);
  96             if (!core.exists()) {
  97                 System.out.println("Has not been able to find coredump. Test skipped.");
  98                 return;
  99             }
 100         } else {
 101             Asserts.assertTrue(cores.length == 1,




   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 /*
  25  * @test TestJmapCore
  26  * @summary Test verifies that jhsdb jmap could generate heap dump from core when heap is full
  27  * @modules java.base/jdk.internal.misc
  28  * @library /test/lib
  29  * @run driver/timeout=240 TestJmapCore run heap
  30  */
  31 
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.JDKToolFinder;
  34 import jdk.test.lib.JDKToolLauncher;
  35 import jdk.test.lib.Platform;
  36 import jdk.test.lib.classloader.GeneratingClassLoader;
  37 import jdk.test.lib.hprof.HprofParser;
  38 import jdk.test.lib.process.ProcessTools;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 
  41 import java.io.File;


  42 
  43 public class TestJmapCore {
  44     static final String pidSeparator = ":KILLED_PID";
  45 
  46     public static String HEAP_OOME = "heap";
  47     public static String METASPACE_OOME = "metaspace";
  48 
  49 
  50     public static void main(String[] args) throws Throwable {
  51         if (args.length == 1) {
  52             // If 1 argument is set prints pid so main process could find corefile
  53             System.out.println(ProcessHandle.current().pid() + pidSeparator);
  54             try {
  55                 if (args[0].equals(HEAP_OOME)) {
  56                     Object[] oa = new Object[Integer.MAX_VALUE / 2];
  57                     for(int i = 0; i < oa.length; i++) {
  58                         oa[i] = new Object[Integer.MAX_VALUE / 2];
  59                     }
  60                 } else {
  61                     GeneratingClassLoader loader = new GeneratingClassLoader();
  62                     for (int i = 0; ; i++) {
  63                         loader.loadClass(loader.getClassName(i));
  64                     }
  65                 }
  66                 throw new Error("OOME not triggered");
  67             } catch (OutOfMemoryError err) {
  68                 return;
  69             }
  70         }
  71         test(args[1]);
  72     }
  73 
  74     // Test tries to run java with ulimit unlimited if it is possible
  75     static boolean useDefaultUlimit() {
  76         if (Platform.isWindows()) {
  77             return true;
  78         }
  79         try {
  80             OutputAnalyzer output = ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && ulimit -c");
  81             return !(output.getExitValue() == 0 && output.getStdout().contains("unlimited"));
  82         } catch (Throwable t) {
  83             return true;
  84         }
  85     }
  86 
  87     static void test(String type) throws Throwable {
  88         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-XX:+CreateCoredumpOnCrash",
  89                 "-XX:MaxMetaspaceSize=64m", "-XX:+CrashOnOutOfMemoryError", "-XX:-TransmitErrorReport",
  90                 TestJmapCore.class.getName(), type);
  91 
  92         boolean useDefaultUlimit = useDefaultUlimit();
  93         System.out.println("Run test with ulimit: " + (useDefaultUlimit ? "default" : "unlimited"));
  94         OutputAnalyzer output = useDefaultUlimit
  95             ? ProcessTools.executeProcess(pb)
  96             : ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && "
  97                     + ProcessTools.getCommandLine(pb));
  98         File core;
  99         String pattern = Platform.isWindows() ? "mdmp" : "core";
 100         File[] cores = new File(".").listFiles((dir, name) -> name.contains(pattern));
 101         if (cores.length == 0) {
 102             // /cores/core.$pid might be generated on macosx by default
 103             String pid = output.firstMatch("^(\\d+)" + pidSeparator, 1);
 104             core = new File("cores/core." + pid);
 105             if (!core.exists()) {
 106                 System.out.println("Has not been able to find coredump. Test skipped.");
 107                 return;
 108             }
 109         } else {
 110             Asserts.assertTrue(cores.length == 1,


< prev index next >