Skip to content
Permalink
Browse files

8222001: JFR event for heap dumps written

Reviewed-by: mgronlun
  • Loading branch information
Erik Gahlin
Erik Gahlin committed Jan 29, 2020
1 parent f262092 commit a2bbf933d96dc4a911ac4b429519937d8dd83200
@@ -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>
@@ -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();
@@ -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>




@@ -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>



@@ -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);
}
}
@@ -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);
}

0 comments on commit a2bbf93

Please sign in to comment.