1 /*
   2  * Copyright (c) 2018, 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 /*
  25  * @test TestHeapDumpOnOutOfMemoryError
  26  * @summary Test verifies that -XX:HeapDumpOnOutOfMemoryError dump heap when OutOfMemory is thrown
  27  * @modules java.base/jdk.internal.misc
  28  * @library /test/lib
  29  * @run driver/timeout=240 TestHeapDumpOnOutOfMemoryError
  30  */
  31 
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.classloader.GeneratingClassLoader;
  34 import jdk.test.lib.hprof.HprofParser;
  35 import jdk.test.lib.process.ProcessTools;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 
  38 import java.io.File;
  39 
  40 public class TestHeapDumpOnOutOfMemoryError {
  41 
  42     public static String HEAP_OOME = "heap";
  43     public static String METASPACE_OOME = "metaspace";
  44 
  45     public static void main(String[] args) throws Exception {
  46         if (args.length == 1) {
  47              try {
  48                 if (args[0].equals(HEAP_OOME)) {
  49                     Object[] oa = new Object[Integer.MAX_VALUE];
  50                     for(int i = 0; i < oa.length; i++) {
  51                         oa[i] = new Object[Integer.MAX_VALUE];
  52                     }
  53                 } else {
  54                     GeneratingClassLoader loader = new GeneratingClassLoader();
  55                     for (int i = 0; ; i++) {
  56                         loader.loadClass(loader.getClassName(i));
  57                     }
  58                 }
  59                 throw new Error("OOME not triggered");
  60             } catch (OutOfMemoryError err) {
  61                 return;
  62             }
  63         }
  64         test(HEAP_OOME);
  65         test(METASPACE_OOME);
  66         testHeapDumpPath();
  67     }
  68 
  69     static void test(String type) throws Exception {
  70         String heapdumpFilename = type + ".hprof";
  71         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+HeapDumpOnOutOfMemoryError",
  72                 "-XX:HeapDumpPath=" + heapdumpFilename, "-XX:MaxMetaspaceSize=64m",
  73                 TestHeapDumpOnOutOfMemoryError.class.getName(), type);
  74 
  75         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  76         output.stdoutShouldNotBeEmpty();
  77         output.shouldContain("Dumping heap to " + type + ".hprof");
  78         File dump = new File(heapdumpFilename);
  79         Asserts.assertTrue(dump.exists() && dump.isFile(),
  80                 "Could not find dump file " + dump.getAbsolutePath());
  81 
  82         HprofParser.parse(new File(heapdumpFilename));
  83         System.out.println("PASSED");
  84     }
  85 
  86     static void testHeapDumpPath() throws Exception {
  87         String heapdumpPath = "dumps";
  88         File dumpDirectory = new File(heapdumpPath);
  89         dumpDirectory.mkdir();
  90         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+HeapDumpOnOutOfMemoryError",
  91                 "-XX:HeapDumpPath=" + heapdumpPath, "-Xmx256m", "-XX:MaxMetaspaceSize=64m",
  92                 TestHeapDumpOnOutOfMemoryError.class.getName(), HEAP_OOME);
  93 
  94         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  95         output.stdoutShouldNotBeEmpty();
  96         output.shouldContain("Dumping heap");
  97 
  98         Asserts.assertFalse(dumpDirectory.listFiles().length == 0,
  99                 "There is no dump files found in " + dumpDirectory );
 100 
 101         Asserts.assertTrue(dumpDirectory.listFiles().length == 1,
 102                 "There are unexpected files in " + dumpDirectory
 103                         + ": " + String.join(",", dumpDirectory.list()) +".");
 104 
 105         File dump = dumpDirectory.listFiles()[0];
 106         Asserts.assertTrue(dump.exists() && dump.isFile(),
 107                 "Could not find dump file " + dump.getAbsolutePath());
 108 
 109         HprofParser.parse(dump);
 110         System.out.println("PASSED");
 111     }
 112 
 113 }