|
|
@@ -18,8 +18,11 @@ package org.apache.poi.stress; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.lang.management.ManagementFactory; |
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
|
|
|
import com.sun.management.HotSpotDiagnosticMXBean; |
|
|
|
import org.apache.poi.ss.formula.eval.NotImplementedException; |
|
|
|
import org.apache.poi.util.SuppressForbidden; |
|
|
|
|
|
|
|
@SuppressForbidden("class only exists for manual tests in XSSFFileHandler") |
|
|
@@ -40,9 +43,18 @@ public class HeapDump { |
|
|
|
* only the live objects |
|
|
|
*/ |
|
|
|
public static void dumpHeap(String fileName, boolean live) throws IOException { |
|
|
|
// initialize hotspot diagnostic MBean |
|
|
|
initHotspotMBean(); |
|
|
|
hotspotMBean.dumpHeap(fileName, live); |
|
|
|
try { |
|
|
|
if (isIbmVm()) { |
|
|
|
dumpHeapJ9(fileName); |
|
|
|
} else { |
|
|
|
|
|
|
|
// initialize hotspot diagnostic MBean |
|
|
|
initHotspotMBean(); |
|
|
|
dumpHeapHotSpot(fileName, live); |
|
|
|
} |
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// initialize the hotspot diagnostic MBean field |
|
|
@@ -59,6 +71,28 @@ public class HeapDump { |
|
|
|
// get the hotspot diagnostic MBean from the platform MBean server |
|
|
|
private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException { |
|
|
|
return ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), |
|
|
|
HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); |
|
|
|
HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); |
|
|
|
} |
|
|
|
|
|
|
|
private static boolean isIbmVm() { |
|
|
|
try { |
|
|
|
Class.forName("com.ibm.jvm.Dump"); |
|
|
|
return true; |
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static void dumpHeapJ9(String fileName) throws ClassNotFoundException, NoSuchMethodException, |
|
|
|
InvocationTargetException, IllegalAccessException { |
|
|
|
Class<?> dump = Class.forName("com.ibm.jvm.Dump"); |
|
|
|
Method heapDumpToFile = dump.getMethod("heapDumpToFile", String.class); |
|
|
|
heapDumpToFile.invoke(dump, fileName); |
|
|
|
} |
|
|
|
|
|
|
|
private static void dumpHeapHotSpot(String fileName, boolean live) throws NoSuchMethodException, |
|
|
|
InvocationTargetException, IllegalAccessException { |
|
|
|
Method dumpHeap = hotspotMBean.getClass().getMethod("dumpHeap", String.class, boolean.class); |
|
|
|
dumpHeap.invoke(hotspotMBean, fileName, live); |
|
|
|
} |
|
|
|
} |
|
|
|
} |