[github-197] Modify test class HeapDump to allow it work in different JDKs. Thanks to Marius Volkhart. This closes #197

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1883393 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2020-11-13 18:00:21 +00:00
parent ff0d031f10
commit 00b9048d59
1 changed files with 39 additions and 5 deletions

View File

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