Allow heap dump requests with a FileDescriptor arg.
Part of a larger change to allow "am" to initiate hprof heap dumps.
The original implementation wrote data to a temp file, because we
compute the second part first while doing GC work. Now all paths
work like DDMS, writing all data to a memstream and then copying it
out to a file at the very end.
Also, fix a potential fd leak on failure in the profiling code.
Bug 2759474.
Change-Id: I0f838cbd9948a23088f08463c3008be7198d76e7
diff --git a/vm/alloc/Heap.c b/vm/alloc/Heap.c
index 80b5030..474818c 100644
--- a/vm/alloc/Heap.c
+++ b/vm/alloc/Heap.c
@@ -720,7 +720,7 @@
gcHeap->hprofFileName = nameBuf;
}
gcHeap->hprofContext = hprofStartup(gcHeap->hprofFileName,
- gcHeap->hprofDirectToDdms);
+ gcHeap->hprofFd, gcHeap->hprofDirectToDdms);
if (gcHeap->hprofContext != NULL) {
hprofStartHeapDump(gcHeap->hprofContext);
}
@@ -929,11 +929,18 @@
/*
* Perform garbage collection, writing heap information to the specified file.
*
+ * If "fd" is >= 0, the output will be written to that file descriptor.
+ * Otherwise, "fileName" is used to create an output file.
+ *
* If "fileName" is NULL, a suitable name will be generated automatically.
+ * (TODO: remove this when the SIGUSR1 feature goes away)
+ *
+ * If "directToDdms" is set, the other arguments are ignored, and data is
+ * sent directly to DDMS.
*
* Returns 0 on success, or an error code on failure.
*/
-int hprofDumpHeap(const char* fileName, bool directToDdms)
+int hprofDumpHeap(const char* fileName, int fd, bool directToDdms)
{
int result;
@@ -941,6 +948,7 @@
gDvm.gcHeap->hprofDumpOnGc = true;
gDvm.gcHeap->hprofFileName = fileName;
+ gDvm.gcHeap->hprofFd = fd;
gDvm.gcHeap->hprofDirectToDdms = directToDdms;
dvmCollectGarbageInternal(false, GC_HPROF_DUMP_HEAP);
result = gDvm.gcHeap->hprofResult;