|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2025, NTT DATA. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +package jdk.jfr.event.oldobject; |
| 26 | + |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.concurrent.atomic.AtomicLong; |
| 32 | +import java.util.concurrent.atomic.AtomicReference; |
| 33 | +import jdk.jfr.consumer.EventStream; |
| 34 | +import jdk.jfr.consumer.RecordingFile; |
| 35 | + |
| 36 | +import jdk.test.lib.Asserts; |
| 37 | +import jdk.test.lib.process.OutputAnalyzer; |
| 38 | +import jdk.test.lib.process.ProcessTools; |
| 39 | + |
| 40 | +/** |
| 41 | +* @test |
| 42 | +* @bug 8364090 |
| 43 | +* @summary Tests Dump reason and OldObjectSample events at OOME. |
| 44 | +* @requires vm.flagless |
| 45 | +* @requires vm.hasJFR |
| 46 | +* @library /test/lib |
| 47 | +* @run main/othervm jdk.jfr.event.oldobject.TestEmergencyDumpAtOOM |
| 48 | +*/ |
| 49 | +public class TestEmergencyDumpAtOOM { |
| 50 | + |
| 51 | + public static List<String> DEFAULT_LEAKER_ARGS = List.of( |
| 52 | + "-Xmx64m", |
| 53 | + "-XX:TLABSize=2k", |
| 54 | + "-XX:StartFlightRecording:dumponexit=true,filename=oom.jfr", |
| 55 | + Leaker.class.getName() |
| 56 | + ); |
| 57 | + |
| 58 | + public static class Leaker { |
| 59 | + public static void main(String... args) { |
| 60 | + List<byte[]> list = new ArrayList<>(); |
| 61 | + while (true) { |
| 62 | + list.add(new byte[1024]); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static void test(boolean shouldCrash) throws Exception { |
| 68 | + List<String> args = new ArrayList<>(DEFAULT_LEAKER_ARGS); |
| 69 | + if (shouldCrash) { |
| 70 | + args.add(0, "-XX:+CrashOnOutOfMemoryError"); |
| 71 | + } |
| 72 | + |
| 73 | + while (true) { |
| 74 | + Process p = ProcessTools.createTestJavaProcessBuilder(args).start(); |
| 75 | + p.waitFor(); |
| 76 | + OutputAnalyzer output = new OutputAnalyzer(p); |
| 77 | + if (!output.contains("java.lang.OutOfMemoryError")) { |
| 78 | + throw new RuntimeException("OutOfMemoryError did not happen."); |
| 79 | + } |
| 80 | + |
| 81 | + // Check recording file |
| 82 | + String jfrFileName = shouldCrash ? String.format("hs_err_pid%d.jfr", p.pid()) : "oom.jfr"; |
| 83 | + Path jfrPath = Path.of(jfrFileName); |
| 84 | + Asserts.assertTrue(Files.exists(jfrPath), "No jfr recording file " + jfrFileName + " exists"); |
| 85 | + |
| 86 | + // Check events |
| 87 | + AtomicLong oldObjects = new AtomicLong(); |
| 88 | + AtomicReference<String> shutdownReason = new AtomicReference<>(); |
| 89 | + AtomicReference<String> dumpReason = new AtomicReference<>(); |
| 90 | + try (EventStream stream = EventStream.openFile(jfrPath)) { |
| 91 | + stream.onEvent("jdk.OldObjectSample", e -> oldObjects.incrementAndGet()); |
| 92 | + stream.onEvent("jdk.Shutdown", e -> shutdownReason.set(e.getString("reason"))); |
| 93 | + stream.onEvent("jdk.DumpReason", e -> dumpReason.set(e.getString("reason"))); |
| 94 | + stream.start(); |
| 95 | + } |
| 96 | + |
| 97 | + // Check OldObjectSample events |
| 98 | + if (oldObjects.get() > 0L) { |
| 99 | + if (shouldCrash) { |
| 100 | + Asserts.assertEquals("VM Error", shutdownReason.get()); |
| 101 | + Asserts.assertEquals("Out of Memory", dumpReason.get()); |
| 102 | + } else { |
| 103 | + Asserts.assertEquals("No remaining non-daemon Java threads", shutdownReason.get()); |
| 104 | + } |
| 105 | + // Passed this test |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + System.out.println("Could not find OldObjectSample events. Retrying."); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public static void main(String... args) throws Exception { |
| 114 | + test(true); |
| 115 | + test(false); |
| 116 | + } |
| 117 | +} |
0 commit comments