Skip to content

Commit 06a7b2c

Browse files
committed
dump heap when we see dangling threads for debugging
1 parent 172f082 commit 06a7b2c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ImportError;
4646
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
4747

48+
import java.io.IOException;
4849
import java.io.PrintWriter;
4950
import java.util.List;
5051
import java.util.logging.Level;
@@ -683,4 +684,21 @@ Object doIt(Object value) {
683684
}
684685
}
685686

687+
@Builtin(name = "dump_heap", minNumOfPositionalArgs = 0)
688+
@GenerateNodeFactory
689+
abstract static class DumpHeapNode extends PythonBuiltinNode {
690+
@Specialization
691+
String doit(VirtualFrame frame, @CachedContext(PythonLanguage.class) PythonContext context) {
692+
TruffleFile tempFile;
693+
try {
694+
tempFile = context.getEnv().createTempFile(context.getEnv().getCurrentWorkingDirectory(), "graalpython", ".hprof");
695+
tempFile.delete();
696+
} catch (IOException e) {
697+
throw raiseOSError(frame, e);
698+
}
699+
PythonUtils.dumpHeap(tempFile.getPath());
700+
return tempFile.getPath();
701+
}
702+
}
703+
686704
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/PythonUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,22 @@ public static void forceFullGC() {
297297
Runtime.getRuntime().freeMemory();
298298
}
299299

300+
@TruffleBoundary
301+
public static void dumpHeap(String path) {
302+
if (SERVER != null) {
303+
try {
304+
Class<?> mxBeanClass = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
305+
Object mxBean = ManagementFactory.newPlatformMXBeanProxy(SERVER,
306+
"com.sun.management:type=HotSpotDiagnostic",
307+
mxBeanClass);
308+
mxBeanClass.getMethod("dumpHeap", String.class, boolean.class).invoke(mxBean, path, true);
309+
} catch (Throwable e) {
310+
System.err.println("Cannot dump heap: " + e.getMessage());
311+
e.printStackTrace();
312+
}
313+
}
314+
}
315+
300316
/**
301317
* Get the existing or create a new {@link CallTarget} for the provided root node.
302318
*/

0 commit comments

Comments
 (0)