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 TestJmapCore 26 * @summary Test verifies that jhsdb jmap could generate heap dump from core when heap is full 27 * @requires vm.hasSA 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 import jdk.test.lib.Utils; 41 import jtreg.SkippedException; 42 43 import java.io.File; 44 45 public class TestJmapCore { 46 static final String pidSeparator = ":KILLED_PID"; 47 48 public static final String HEAP_OOME = "heap"; 49 public static final String METASPACE_OOME = "metaspace"; 50 51 52 public static void main(String[] args) throws Throwable { 53 if (args.length == 1) { 54 // If 1 argument is set prints pid so main process could find corefile 55 System.out.println(ProcessHandle.current().pid() + pidSeparator); 56 try { 57 if (args[0].equals(HEAP_OOME)) { 58 Object[] oa = new Object[Integer.MAX_VALUE / 2]; 59 for(int i = 0; i < oa.length; i++) { 60 oa[i] = new Object[Integer.MAX_VALUE / 2]; 61 } 62 } else { 63 GeneratingClassLoader loader = new GeneratingClassLoader(); 64 for (int i = 0; ; i++) { 65 loader.loadClass(loader.getClassName(i)); 66 } 67 } 68 throw new Error("OOME not triggered"); 69 } catch (OutOfMemoryError err) { 70 return; 71 } 72 } 73 test(args[1]); 74 } 75 76 // Test tries to run java with ulimit unlimited if it is possible 77 static boolean useDefaultUlimit() { 78 if (Platform.isWindows()) { 79 return true; 80 } 81 try { 82 OutputAnalyzer output = ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && ulimit -c"); 83 return !(output.getExitValue() == 0 && output.getStdout().contains("unlimited")); 84 } catch (Throwable t) { 85 return true; 86 } 87 } 88 89 static void test(String type) throws Throwable { 90 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-XX:+CreateCoredumpOnCrash", 91 "-Xmx512m", "-XX:MaxMetaspaceSize=64m", "-XX:+CrashOnOutOfMemoryError", 92 TestJmapCore.class.getName(), type); 93 94 boolean useDefaultUlimit = useDefaultUlimit(); 95 System.out.println("Run test with ulimit: " + (useDefaultUlimit ? "default" : "unlimited")); 96 OutputAnalyzer output = useDefaultUlimit 97 ? ProcessTools.executeProcess(pb) 98 : ProcessTools.executeProcess("sh", "-c", "ulimit -c unlimited && " 99 + ProcessTools.getCommandLine(pb)); 100 File core; 101 String pattern = Platform.isWindows() ? "mdmp" : "core"; 102 File[] cores = new File(".").listFiles((dir, name) -> name.contains(pattern)); 103 if (cores.length == 0) { 104 // /cores/core.$pid might be generated on macosx by default 105 String pid = output.firstMatch("^(\\d+)" + pidSeparator, 1); 106 core = new File("cores/core." + pid); 107 if (!core.exists()) { 108 throw new SkippedException("Has not been able to find coredump"); 109 } 110 } else { 111 Asserts.assertTrue(cores.length == 1, 112 "There are unexpected files containing core " 113 + ": " + String.join(",", new File(".").list()) + "."); 114 core = cores[0]; 115 } 116 System.out.println("Found corefile: " + core.getAbsolutePath()); 117 118 File dumpFile = new File("heap.hprof"); 119 JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb"); 120 launcher.addToolArg("jmap"); 121 launcher.addToolArg("--binaryheap"); 122 launcher.addToolArg("--dumpfile=" + dumpFile); 123 launcher.addToolArg("--exe"); 124 launcher.addToolArg(JDKToolFinder.getTestJDKTool("java")); 125 launcher.addToolArg("--core"); 126 launcher.addToolArg(core.getPath()); 127 128 ProcessBuilder jhsdpb = new ProcessBuilder(); 129 jhsdpb.command(launcher.getCommand()); 130 Process jhsdb = jhsdpb.start(); 131 OutputAnalyzer out = new OutputAnalyzer(jhsdb); 132 133 jhsdb.waitFor(); 134 135 System.out.println(out.getStdout()); 136 System.err.println(out.getStderr()); 137 138 if (dumpFile.exists() && dumpFile.isFile()) { 139 HprofParser.parse(dumpFile); 140 } else { 141 boolean ZGCUsed = false; 142 143 for (String opt: Utils.getFilteredTestJavaOpts()) { 144 if (opt.contains("+UseZGC")) { 145 ZGCUsed = true; 146 break; 147 } 148 } 149 150 if (!ZGCUsed) { 151 throw new RuntimeException( 152 "Could not find dump file " + dumpFile.getAbsolutePath()); 153 } 154 } 155 156 System.out.println("PASSED"); 157 } 158 }