changeset 57870:ea066e5bdfd6

8222001: JFR event for heap dumps written Reviewed-by: mgronlun
author egahlin
date Wed, 29 Jan 2020 22:37:17 +0100
parents c7d4f2849dbf
children db30f31b9a8e
files src/hotspot/share/jfr/metadata/metadata.xml src/hotspot/share/services/heapDumper.cpp src/jdk.jfr/share/conf/jfr/default.jfc src/jdk.jfr/share/conf/jfr/profile.jfc test/jdk/jdk/jfr/event/diagnostics/TestHeapDump.java test/lib/jdk/test/lib/jfr/EventNames.java
diffstat 6 files changed, 119 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/jfr/metadata/metadata.xml	Wed Jan 29 15:48:22 2020 -0500
+++ b/src/hotspot/share/jfr/metadata/metadata.xml	Wed Jan 29 22:37:17 2020 +0100
@@ -1023,6 +1023,13 @@
     <Field type="ulong" contentType="bytes" name="size" label="Size Written" />
   </Event>
 
+  <Event name="HeapDump" category="Java Virtual Machine, Diagnostics" label="Heap Dump" stackTrace="true" thread="true">
+    <Field type="string" name="destination" label="Destination" />
+    <Field type="long" name="size" label="Size" />
+    <Field type="boolean" name="gcBeforeDump" label="GC Before Dump" />
+    <Field type="boolean" name="onOutOfMemoryError" label="On Out of Memory Error" />
+  </Event>
+
   <Type name="DeoptimizationReason" label="Deoptimization Reason">
     <Field type="string" name="reason" label="Reason" />
   </Type>
--- a/src/hotspot/share/services/heapDumper.cpp	Wed Jan 29 15:48:22 2020 -0500
+++ b/src/hotspot/share/services/heapDumper.cpp	Wed Jan 29 22:37:17 2020 +0100
@@ -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 @@
     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 @@
   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();
--- a/src/jdk.jfr/share/conf/jfr/default.jfc	Wed Jan 29 15:48:22 2020 -0500
+++ b/src/jdk.jfr/share/conf/jfr/default.jfc	Wed Jan 29 22:37:17 2020 +0100
@@ -708,6 +708,12 @@
       <setting name="stackTrace">false</setting>
     </event>
 
+    <event name="jdk.HeapDump">
+      <setting name="enabled">true</setting>
+      <setting name="threshold">0 ns</setting>
+      <setting name="stackTrace">true</setting>  
+    </event>
+
 
 
 
--- a/src/jdk.jfr/share/conf/jfr/profile.jfc	Wed Jan 29 15:48:22 2020 -0500
+++ b/src/jdk.jfr/share/conf/jfr/profile.jfc	Wed Jan 29 22:37:17 2020 +0100
@@ -707,6 +707,12 @@
       <setting name="enabled">true</setting>
       <setting name="stackTrace">true</setting>
     </event>
+	
+    <event name="jdk.HeapDump">
+      <setting name="enabled">true</setting>
+      <setting name="threshold">0 ns</setting>
+      <setting name="stackTrace">true</setting>
+    </event>
 
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jdk/jfr/event/diagnostics/TestHeapDump.java	Wed Jan 29 22:37:17 2020 +0100
@@ -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<RecordedEvent> 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);
+    }
+}
--- a/test/lib/jdk/test/lib/jfr/EventNames.java	Wed Jan 29 15:48:22 2020 -0500
+++ b/test/lib/jdk/test/lib/jfr/EventNames.java	Wed Jan 29 22:37:17 2020 +0100
@@ -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 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);
     }