From a2bbf933d96dc4a911ac4b429519937d8dd83200 Mon Sep 17 00:00:00 2001 From: Erik Gahlin Date: Wed, 29 Jan 2020 22:37:17 +0100 Subject: [PATCH] 8222001: JFR event for heap dumps written Reviewed-by: mgronlun --- src/hotspot/share/jfr/metadata/metadata.xml | 7 ++ src/hotspot/share/services/heapDumper.cpp | 13 +++ src/jdk.jfr/share/conf/jfr/default.jfc | 6 ++ src/jdk.jfr/share/conf/jfr/profile.jfc | 6 ++ .../jfr/event/diagnostics/TestHeapDump.java | 83 +++++++++++++++++++ test/lib/jdk/test/lib/jfr/EventNames.java | 5 +- 6 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 test/jdk/jdk/jfr/event/diagnostics/TestHeapDump.java diff --git a/src/hotspot/share/jfr/metadata/metadata.xml b/src/hotspot/share/jfr/metadata/metadata.xml index f69330dea2e..7c144df47c9 100644 --- a/src/hotspot/share/jfr/metadata/metadata.xml +++ b/src/hotspot/share/jfr/metadata/metadata.xml @@ -1023,6 +1023,13 @@ + + + + + + + diff --git a/src/hotspot/share/services/heapDumper.cpp b/src/hotspot/share/services/heapDumper.cpp index b8428f930de..ba88068cf62 100644 --- a/src/hotspot/share/services/heapDumper.cpp +++ b/src/hotspot/share/services/heapDumper.cpp @@ -32,6 +32,7 @@ #include "classfile/vmSymbols.hpp" #include "gc/shared/gcLocker.hpp" #include "gc/shared/gcVMOperations.hpp" +#include "jfr/jfrEvents.hpp" #include "memory/allocation.inline.hpp" #include "memory/resourceArea.hpp" #include "memory/universe.hpp" @@ -1952,6 +1953,9 @@ int HeapDumper::dump(const char* path, outputStream* out) { timer()->start(); } + // create JFR event + EventHeapDump event; + // create the dump writer. If the file can be opened then bail DumpWriter writer(path); if (writer.error() != NULL) { @@ -1976,6 +1980,15 @@ int HeapDumper::dump(const char* path, outputStream* out) { writer.close(); set_error(writer.error()); + // emit JFR event + if (error() == NULL) { + event.set_destination(path); + event.set_gcBeforeDump(_gc_before_heap_dump); + event.set_size(writer.bytes_written()); + event.set_onOutOfMemoryError(_oome); + event.commit(); + } + // print message in interactive case if (out != NULL) { timer()->stop(); diff --git a/src/jdk.jfr/share/conf/jfr/default.jfc b/src/jdk.jfr/share/conf/jfr/default.jfc index a15159b6154..6712e527127 100644 --- a/src/jdk.jfr/share/conf/jfr/default.jfc +++ b/src/jdk.jfr/share/conf/jfr/default.jfc @@ -708,6 +708,12 @@ false + + true + 0 ns + true + + diff --git a/src/jdk.jfr/share/conf/jfr/profile.jfc b/src/jdk.jfr/share/conf/jfr/profile.jfc index 9edecee0690..0a2b645442f 100644 --- a/src/jdk.jfr/share/conf/jfr/profile.jfc +++ b/src/jdk.jfr/share/conf/jfr/profile.jfc @@ -707,6 +707,12 @@ true true + + + true + 0 ns + true + diff --git a/test/jdk/jdk/jfr/event/diagnostics/TestHeapDump.java b/test/jdk/jdk/jfr/event/diagnostics/TestHeapDump.java new file mode 100644 index 00000000000..ae7bd5ab2b7 --- /dev/null +++ b/test/jdk/jdk/jfr/event/diagnostics/TestHeapDump.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.jfr.event.diagnostics; + +import java.lang.management.ManagementFactory; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import jdk.jfr.Recording; +import jdk.jfr.consumer.RecordedEvent; +import jdk.test.lib.jfr.EventNames; +import jdk.test.lib.jfr.Events; + +/** + * @test + * @key jfr + * @requires vm.hasJFR + * @library /test/lib + * @modules java.management + * @run main/othervm jdk.jfr.event.diagnostics.TestHeapDump + */ +public class TestHeapDump { + private final static String EVENT_NAME = EventNames.HeapDump; + + public static void main(String[] args) throws Exception { + + Path path = Paths.get("dump.hprof").toAbsolutePath(); + try (Recording r = new Recording()) { + r.enable(EVENT_NAME); + r.start(); + heapDump(path); + r.stop(); + List events = Events.fromRecording(r); + if (events.size() != 1) { + throw new Exception("Expected one event, got " + events.size()); + } + RecordedEvent e = events.get(0); + Events.assertField(e, "destination").equal(path.toString()); + Events.assertField(e, "gcBeforeDump").equal(true); + Events.assertField(e, "onOutOfMemoryError").equals(false); + Events.assertField(e, "size").equals(Files.size(path)); + System.out.println(e); + } + } + + private static void heapDump(Path path) throws Exception { + ObjectName objectName = new ObjectName("com.sun.management:type=HotSpotDiagnostic"); + MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); + Object[] parameters = new Object[2]; + parameters[0] = path.toString(); + parameters[1] = true; + String[] signature = new String[] { String.class.getName(), boolean.class.toString() }; + mbeanServer.invoke(objectName, "dumpHeap", parameters, signature); + } +} diff --git a/test/lib/jdk/test/lib/jfr/EventNames.java b/test/lib/jdk/test/lib/jfr/EventNames.java index 70ec8aed45f..e137f42f471 100644 --- a/test/lib/jdk/test/lib/jfr/EventNames.java +++ b/test/lib/jdk/test/lib/jfr/EventNames.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -197,6 +197,9 @@ public class EventNames { public static final String FlushMetadata = PREFIX + "FlushMetadata"; public static final String FlushTypeSet = PREFIX + "FlushTypeSet"; + // Diagnostics + public static final String HeapDump = PREFIX + "HeapDump"; + public static boolean isGcEvent(EventType et) { return et.getCategoryNames().contains(GC_CATEGORY); }