Skip to content

Instantly share code, notes, and snippets.

@Byteflux
Created June 16, 2019 16:17
Show Gist options
  • Save Byteflux/9ea5d01acdeedc101570fa56c2775eef to your computer and use it in GitHub Desktop.
Save Byteflux/9ea5d01acdeedc101570fa56c2775eef to your computer and use it in GitHub Desktop.
diff --git a/src/main/java/com/destroystokyo/paper/PaperCommand.java b/src/main/java/com/destroystokyo/paper/PaperCommand.java
index b5f318c00..f3c73b0de 100644
--- a/src/main/java/com/destroystokyo/paper/PaperCommand.java
+++ b/src/main/java/com/destroystokyo/paper/PaperCommand.java
@@ -215,11 +215,12 @@ public class PaperCommand extends Command {
}
private void dumpHeap(CommandSender sender) {
- File file = new File(new File(new File("."), "dumps"),
- "heap-dump-" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss").format(LocalDateTime.now()) + "-server.hprof");
- Command.broadcastCommandMessage(sender, ChatColor.YELLOW + "Writing JVM heap data to " + file);
- if (CraftServer.dumpHeap(file)) {
- Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Heap dump complete");
+ java.nio.file.Path dir = java.nio.file.Paths.get("dumps");
+ String name = "heap-dump-" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss").format(LocalDateTime.now());
+ Command.broadcastCommandMessage(sender, ChatColor.YELLOW + "Writing JVM heap data to " + dir);
+ java.nio.file.Path file = CraftServer.dumpHeap(dir, name);
+ if (file != null) {
+ Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Heap dump saved to " + file);
} else {
Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed to write heap dump, see sever log for details");
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 4413066ee..e0bcd7e5c 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -1959,22 +1959,31 @@ public final class CraftServer implements Server {
// Paper start
@SuppressWarnings({"rawtypes", "unchecked"})
- public static boolean dumpHeap(File file) {
+ public static java.nio.file.Path dumpHeap(java.nio.file.Path dir, String name) {
try {
- if (file.getParentFile() != null) {
- file.getParentFile().mkdirs();
- }
+ java.nio.file.Files.createDirectories(dir);
- Class clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
javax.management.MBeanServer server = java.lang.management.ManagementFactory.getPlatformMBeanServer();
- Object hotspotMBean = java.lang.management.ManagementFactory.newPlatformMXBeanProxy(server, "com.sun.management:type=HotSpotDiagnostic", clazz);
- java.lang.reflect.Method m = clazz.getMethod("dumpHeap", String.class, boolean.class);
- m.invoke(hotspotMBean, file.getPath(), true);
- return true;
+ java.nio.file.Path file;
+
+ try {
+ Class clazz = Class.forName("openj9.lang.management.OpenJ9DiagnosticsMXBean");
+ Object openj9Mbean = java.lang.management.ManagementFactory.newPlatformMXBeanProxy(server, "openj9.lang.management:type=OpenJ9Diagnostics", clazz);
+ java.lang.reflect.Method m = clazz.getMethod("triggerDumpToFile", String.class, String.class);
+ file = dir.resolve(name + ".phd");
+ m.invoke(openj9Mbean, "heap", file.toString());
+ } catch (ClassNotFoundException e) {
+ Class clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
+ Object hotspotMBean = java.lang.management.ManagementFactory.newPlatformMXBeanProxy(server, "com.sun.management:type=HotSpotDiagnostic", clazz);
+ java.lang.reflect.Method m = clazz.getMethod("dumpHeap", String.class, boolean.class);
+ file = dir.resolve(name + ".hprof");
+ m.invoke(hotspotMBean, file.toString(), true);
+ }
+ return file;
} catch (Throwable t) {
- Bukkit.getLogger().severe("Could not write heap to " + file);
+ Bukkit.getLogger().severe("Could not write heap to " + dir);
t.printStackTrace();
- return false;
+ return null;
}
}
// Paper end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment