From 1b9b4e4b89e1c682b6684ae5e2a637e4497a67e9 Mon Sep 17 00:00:00 2001 From: Barry Hayes Date: Mon, 4 Jan 2010 10:33:46 -0800 Subject: [PATCH] Percolate the reason for a GC up far enough to print out in logging messages. --- vm/alloc/Alloc.c | 2 +- vm/alloc/Heap.c | 23 +++++++++++++++-------- vm/alloc/Heap.h | 14 +++++++++++++- vm/alloc/HeapSource.c | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/vm/alloc/Alloc.c b/vm/alloc/Alloc.c index 1bbf6790a..d209e4a49 100644 --- a/vm/alloc/Alloc.c +++ b/vm/alloc/Alloc.c @@ -296,7 +296,7 @@ void dvmCollectGarbage(bool collectSoftReferences) dvmLockHeap(); LOGVV("Explicit GC\n"); - dvmCollectGarbageInternal(collectSoftReferences); + dvmCollectGarbageInternal(collectSoftReferences, GC_EXPLICIT); dvmUnlockHeap(); } diff --git a/vm/alloc/Heap.c b/vm/alloc/Heap.c index 07f0a20e2..5b3130d03 100644 --- a/vm/alloc/Heap.c +++ b/vm/alloc/Heap.c @@ -37,6 +37,13 @@ #define kNonCollectableRefDefault 16 #define kFinalizableRefDefault 128 +static const char* GcReasonStr[] = { + [GC_FOR_MALLOC] = "GC_FOR_MALLOC", + [GC_EXPLICIT] = "GC_EXPLICIT", + [GC_EXTERNAL_ALLOC] = "GC_EXTERNAL_ALLOC", + [GC_HPROF_DUMP_HEAP] = "GC_HPROF_DUMP_HEAP" +}; + /* * Initialize the GC heap. * @@ -326,7 +333,7 @@ static void gcForMalloc(bool collectSoftReferences) */ LOGD_HEAP("dvmMalloc initiating GC%s\n", collectSoftReferences ? "(collect SoftReferences)" : ""); - dvmCollectGarbageInternal(collectSoftReferences); + dvmCollectGarbageInternal(collectSoftReferences, GC_FOR_MALLOC); } /* Try as hard as possible to allocate some memory. @@ -720,7 +727,7 @@ size_t dvmObjectSizeInHeap(const Object *obj) * way to enforce this is to refuse to GC on an allocation made by the * JDWP thread -- we have to expand the heap or fail. */ -void dvmCollectGarbageInternal(bool collectSoftReferences) +void dvmCollectGarbageInternal(bool collectSoftReferences, enum GcReason reason) { GcHeap *gcHeap = gDvm.gcHeap; Object *softReferences; @@ -762,7 +769,7 @@ void dvmCollectGarbageInternal(bool collectSoftReferences) } gcHeap->gcStartTime = now; - LOGV_HEAP("GC starting -- suspending threads\n"); + LOGV_HEAP("%s starting -- suspending threads\n", GcReasonStr[reason]); dvmSuspendAllThreads(SUSPEND_FOR_GC); @@ -1039,11 +1046,11 @@ void dvmCollectGarbageInternal(bool collectSoftReferences) } gcElapsedTime = (dvmGetRelativeTimeUsec() - gcHeap->gcStartTime) / 1000; if (gcElapsedTime < 10000) { - LOGD("GC freed %d objects / %zd bytes in %dms\n", - numFreed, sizeFreed, (int)gcElapsedTime); + LOGD("%s freed %d objects / %zd bytes in %dms\n", + GcReasonStr[reason], numFreed, sizeFreed, (int)gcElapsedTime); } else { - LOGD("GC freed %d objects / %zd bytes in %d sec\n", - numFreed, sizeFreed, (int)(gcElapsedTime / 1000)); + LOGD("%s freed %d objects / %zd bytes in %d sec\n", + GcReasonStr[reason], numFreed, sizeFreed, (int)(gcElapsedTime / 1000)); } dvmLogGcStats(numFreed, sizeFreed, gcElapsedTime); @@ -1077,7 +1084,7 @@ int hprofDumpHeap(const char* fileName) gDvm.gcHeap->hprofDumpOnGc = true; gDvm.gcHeap->hprofFileName = fileName; - dvmCollectGarbageInternal(false); + dvmCollectGarbageInternal(false, GC_HPROF_DUMP_HEAP); result = gDvm.gcHeap->hprofResult; dvmUnlockMutex(&gDvm.gcHeapLock); diff --git a/vm/alloc/Heap.h b/vm/alloc/Heap.h index cc29c40c3..145173958 100644 --- a/vm/alloc/Heap.h +++ b/vm/alloc/Heap.h @@ -52,9 +52,21 @@ void dvmHeapShutdown(void); size_t dvmObjectSizeInHeap(const Object *obj); #endif +enum GcReason { + /* Not enough space for an "ordinary" Object to be allocated. */ + GC_FOR_MALLOC, + /* Explicit GC via Runtime.gc(), VMRuntime.gc(), or SIGUSR1. */ + GC_EXPLICIT, + /* GC to try to reduce heap footprint to allow more non-GC'ed memory. */ + GC_EXTERNAL_ALLOC, + /* GC to dump heap contents to a file, only used under WITH_HPROF */ + GC_HPROF_DUMP_HEAP +}; + /* * Run the garbage collector without doing any locking. */ -void dvmCollectGarbageInternal(bool collectSoftReferences); +void dvmCollectGarbageInternal(bool collectSoftReferences, + enum GcReason reason); #endif // _DALVIK_ALLOC_HEAP diff --git a/vm/alloc/HeapSource.c b/vm/alloc/HeapSource.c index 1511f3e26..440794167 100644 --- a/vm/alloc/HeapSource.c +++ b/vm/alloc/HeapSource.c @@ -1476,7 +1476,7 @@ gcForExternalAlloc(bool collectSoftReferences) } } #endif - dvmCollectGarbageInternal(collectSoftReferences); + dvmCollectGarbageInternal(collectSoftReferences, GC_EXTERNAL_ALLOC); } /* -- 2.11.0