Heap.c revision 6343bd071555458d0cb071f2eaf15b59b36771bf
1f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 2f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 3f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 4f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * you may not use this file except in compliance with the License. 6f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * You may obtain a copy of the License at 7f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 8f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 10f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * See the License for the specific language governing permissions and 14f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * limitations under the License. 15f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 16f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 17f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Garbage-collecting memory allocator. 18f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 19f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "Dalvik.h" 20f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/HeapTable.h" 21f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/Heap.h" 22f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/HeapInternal.h" 23f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/DdmHeap.h" 24f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/HeapSource.h" 25f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "alloc/MarkSweep.h" 26f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 27f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include "utils/threads.h" // need Android thread priorities 28f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#define kInvalidPriority 10000 29f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 305a2056ca8c7e7c0ed9f51d68cedbee59cc936685San Mehat#include <cutils/sched_policy.h> 315a2056ca8c7e7c0ed9f51d68cedbee59cc936685San Mehat 32f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <sys/time.h> 33f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <sys/resource.h> 34f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <limits.h> 35f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#include <errno.h> 36f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 37f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#define kNonCollectableRefDefault 16 38f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#define kFinalizableRefDefault 128 39f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 401b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayesstatic const char* GcReasonStr[] = { 411b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_FOR_MALLOC] = "GC_FOR_MALLOC", 421b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_EXPLICIT] = "GC_EXPLICIT", 431b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_EXTERNAL_ALLOC] = "GC_EXTERNAL_ALLOC", 441b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes [GC_HPROF_DUMP_HEAP] = "GC_HPROF_DUMP_HEAP" 451b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes}; 461b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes 47f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 48f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Initialize the GC heap. 49f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 50f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Returns true if successful, false otherwise. 51f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 52f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectbool dvmHeapStartup() 53f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 54f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap; 55f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 56f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if defined(WITH_ALLOC_LIMITS) 57f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.checkAllocLimits = false; 58f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocationLimit = -1; 59f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 60f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 61f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap = dvmHeapSourceStartup(gDvm.heapSizeStart, gDvm.heapSizeMax); 62f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap == NULL) { 63f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return false; 64f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 65f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->heapWorkerCurrentObject = NULL; 66f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->heapWorkerCurrentMethod = NULL; 67f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->heapWorkerInterpStartTime = 0LL; 68f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceCollectionState = SR_COLLECT_NONE; 69f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceHeapSizeThreshold = gDvm.heapSizeStart; 70f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmHpifWhen = 0; 71f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmHpsgWhen = 0; 72f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmHpsgWhat = 0; 73f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmNhsgWhen = 0; 74f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->ddmNhsgWhat = 0; 75f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 76f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofDumpOnGc = false; 77f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofContext = NULL; 78f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 79f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 80f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* This needs to be set before we call dvmHeapInitHeapRefTable(). 81f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 82f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap = gcHeap; 83f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 84f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Set up the table we'll use for ALLOC_NO_GC. 85f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 86f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (!dvmHeapInitHeapRefTable(&gcHeap->nonCollectableRefs, 87f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project kNonCollectableRefDefault)) 88f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project { 89f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGE_HEAP("Can't allocate GC_NO_ALLOC table\n"); 90f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project goto fail; 91f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 92f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 93f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Set up the lists and lock we'll use for finalizable 94f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * and reference objects. 95f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 96f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmInitMutex(&gDvm.heapWorkerListLock); 97f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->finalizableRefs = NULL; 98f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->pendingFinalizationRefs = NULL; 99f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->referenceOperations = NULL; 100f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 101f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Initialize the HeapWorker locks and other state 102f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * that the GC uses. 103f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 104f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmInitializeHeapWorkerState(); 105f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 106f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return true; 107f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 108f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectfail: 109a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapSourceShutdown(&gcHeap); 110f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return false; 111f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 112f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 113c8e06c8dba30f01d3cf6506c553041c340b312afCarl Shapirovoid dvmHeapStartupAfterZygote() 114f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 115f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Update our idea of the last GC start time so that we 116f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * don't use the last time that Zygote happened to GC. 117f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 118f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap->gcStartTime = dvmGetRelativeTimeUsec(); 119f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 120f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 121f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmHeapShutdown() 122f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 123f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: make sure we're locked 124f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.gcHeap != NULL) { 125f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Tables are allocated on the native heap; 126f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * they need to be cleaned up explicitly. 127f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The process may stick around, so we don't 128f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * want to leak any native memory. 129f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 130a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapFreeHeapRefTable(&gDvm.gcHeap->nonCollectableRefs); 131f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 132a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapFreeLargeTable(gDvm.gcHeap->finalizableRefs); 133a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro gDvm.gcHeap->finalizableRefs = NULL; 134f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 135a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapFreeLargeTable(gDvm.gcHeap->pendingFinalizationRefs); 136a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro gDvm.gcHeap->pendingFinalizationRefs = NULL; 137f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 138a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapFreeLargeTable(gDvm.gcHeap->referenceOperations); 139a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro gDvm.gcHeap->referenceOperations = NULL; 140f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 141f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Destroy the heap. Any outstanding pointers 142f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * will point to unmapped memory (unless/until 143a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro * someone else maps it). This frees gDvm.gcHeap 144f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * as a side-effect. 145f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 146a199eb70871a8c142a723d76b1b08939286a3199Carl Shapiro dvmHeapSourceShutdown(&gDvm.gcHeap); 147f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 148f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 149f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 150f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 151f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * We've been asked to allocate something we can't, e.g. an array so 1526da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * large that (length * elementWidth) is larger than 2^31. 153f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 1546da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * _The Java Programming Language_, 4th edition, says, "you can be sure 1556da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * that all SoftReferences to softly reachable objects will be cleared 1566da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * before an OutOfMemoryError is thrown." 1576da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * 1586da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * It's unclear whether that holds for all situations where an OOM can 1596da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * be thrown, or just in the context of an allocation that fails due 1606da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * to lack of heap space. For simplicity we just throw the exception. 1616da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * 1626da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden * (OOM due to actually running out of space is handled elsewhere.) 163f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 164f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmThrowBadAllocException(const char* msg) 165f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 1666da743bfe7c60ad873df72a3b6412137c4ac71dcAndy McFadden dvmThrowException("Ljava/lang/OutOfMemoryError;", msg); 167f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 168f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 169f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 170f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Grab the lock, but put ourselves into THREAD_VMWAIT if it looks like 171f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we're going to have to wait on the mutex. 172f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 173f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectbool dvmLockHeap() 174f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 175f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (pthread_mutex_trylock(&gDvm.gcHeapLock) != 0) { 176f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread *self; 177f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project ThreadStatus oldStatus; 178f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int cc; 179f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 180f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self = dvmThreadSelf(); 181f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 182f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project oldStatus = dvmChangeStatus(self, THREAD_VMWAIT); 183f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 18499409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project LOGI("ODD: waiting on heap lock, no self\n"); 185f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project oldStatus = -1; // shut up gcc 186f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 187f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 188f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project cc = pthread_mutex_lock(&gDvm.gcHeapLock); 189f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(cc == 0); 190f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 191f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 192f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmChangeStatus(self, oldStatus); 193f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 194f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 195f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 196f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return true; 197f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 198f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 199f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmUnlockHeap() 200f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 201f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.gcHeapLock); 202f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 203f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 204f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Pop an object from the list of pending finalizations and 205f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * reference clears/enqueues, and return the object. 206f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The caller must call dvmReleaseTrackedAlloc() 207f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * on the object when finished. 208f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 209f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Typically only called by the heap worker thread. 210f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 211f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source ProjectObject *dvmGetNextHeapWorkerObject(HeapWorkerOperation *op) 212f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 213f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object *obj; 214f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap = gDvm.gcHeap; 215f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 216f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(op != NULL); 217f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 218f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project obj = NULL; 219f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 220f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.heapWorkerListLock); 221f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 222f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* We must handle reference operations before finalizations. 223f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If: 224f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * a) Someone subclasses WeakReference and overrides clear() 225f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * b) A reference of this type is the last reference to 226f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * a finalizable object 227f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * then we need to guarantee that the overridden clear() is called 228f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * on the reference before finalize() is called on the referent. 229f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Both of these operations will always be scheduled at the same 230f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * time, so handling reference operations first will guarantee 231f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * the required order. 232f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 233f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project obj = dvmHeapGetNextObjectFromLargeTable(&gcHeap->referenceOperations); 234f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (obj != NULL) { 235f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project uintptr_t workBits; 236f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 2376930a11fd75071fa2f04fcfe11c322e2518cf503Barry Hayes workBits = (uintptr_t)obj & WORKER_ENQUEUE; 238f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(workBits != 0); 2396930a11fd75071fa2f04fcfe11c322e2518cf503Barry Hayes obj = (Object *)((uintptr_t)obj & ~WORKER_ENQUEUE); 240f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 241f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *op = workBits; 242f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 243f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project obj = dvmHeapGetNextObjectFromLargeTable( 244f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project &gcHeap->pendingFinalizationRefs); 245f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (obj != NULL) { 246f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project *op = WORKER_FINALIZE; 247f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 248f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 249f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 250f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (obj != NULL) { 251f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't let the GC collect the object until the 252f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * worker thread is done with it. 253f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 254f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This call is safe; it uses thread-local storage 255f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * and doesn't acquire any locks. 256f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 257f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAddTrackedAlloc(obj, NULL); 258f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 259f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 260f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.heapWorkerListLock); 261f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 262f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return obj; 263f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 264f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 265f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Used for a heap size change hysteresis to avoid collecting 266f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * SoftReferences when the heap only grows by a small amount. 267f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 268f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#define SOFT_REFERENCE_GROWTH_SLACK (128 * 1024) 269f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 270f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Whenever the effective heap size may have changed, 271f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * this function must be called. 272f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 273f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmHeapSizeChanged() 274f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 275f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap = gDvm.gcHeap; 276f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t currentHeapSize; 277f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 278f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project currentHeapSize = dvmHeapSourceGetIdealFootprint(); 279f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 280f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* See if the heap size has changed enough that we should care 281f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * about it. 282f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 283f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (currentHeapSize <= gcHeap->softReferenceHeapSizeThreshold - 284f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 4 * SOFT_REFERENCE_GROWTH_SLACK) 285f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project { 286f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The heap has shrunk enough that we'll use this as a new 287f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * threshold. Since we're doing better on space, there's 288f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * no need to collect any SoftReferences. 289f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 290f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This is 4x the growth hysteresis because we don't want 291f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * to snap down so easily after a shrink. If we just cleared 292f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * up a bunch of SoftReferences, we don't want to disallow 293f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * any new ones from being created. 294f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * TODO: determine if the 4x is important, needed, or even good 295f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 296f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceHeapSizeThreshold = currentHeapSize; 297f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceCollectionState = SR_COLLECT_NONE; 298f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else if (currentHeapSize >= gcHeap->softReferenceHeapSizeThreshold + 299f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project SOFT_REFERENCE_GROWTH_SLACK) 300f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project { 301f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The heap has grown enough to warrant collecting SoftReferences. 302f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 303f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceHeapSizeThreshold = currentHeapSize; 304f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceCollectionState = SR_COLLECT_SOME; 305f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 306f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 307f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 308f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 309f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Do a full garbage collection, which may grow the 310f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * heap as a side-effect if the live set is large. 311f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 312f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectstatic void gcForMalloc(bool collectSoftReferences) 313f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 314f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 315f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocProf.enabled) { 316f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 317f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.gcCount++; 318f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 319f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.gcCount++; 320f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 321f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 322f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 323f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* This may adjust the soft limit as a side-effect. 324f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 325f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("dvmMalloc initiating GC%s\n", 326f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project collectSoftReferences ? "(collect SoftReferences)" : ""); 3271b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes dvmCollectGarbageInternal(collectSoftReferences, GC_FOR_MALLOC); 328f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 329f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 330f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Try as hard as possible to allocate some memory. 331f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 3326343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapirostatic void *tryMalloc(size_t size) 333f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 3346343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro void *ptr; 335f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 336f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't try too hard if there's no way the allocation is 337f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * going to succeed. We have to collect SoftReferences before 338f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * throwing an OOME, though. 339f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 340f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (size >= gDvm.heapSizeMax) { 341f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGW_HEAP("dvmMalloc(%zu/0x%08zx): " 342f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "someone's allocating a huge buffer\n", size, size); 3436343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = NULL; 344f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project goto collect_soft_refs; 345f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 346f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 347f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: figure out better heuristics 348f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// There will be a lot of churn if someone allocates a bunch of 349f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// big objects in a row, and we hit the frag case each time. 350f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// A full GC for each. 351f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// Maybe we grow the heap in bigger leaps 352f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// Maybe we skip the GC if the size is large and we did one recently 353f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// (number of allocations ago) (watch for thread effects) 354f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// DeflateTest allocs a bunch of ~128k buffers w/in 0-5 allocs of each other 355f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// (or, at least, there are only 0-5 objects swept each time) 356f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 3576343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAlloc(size); 3586343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 3596343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 360f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 361f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 362f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The allocation failed. Free up some space by doing 363f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * a full garbage collection. This may grow the heap 364f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * if the live set is sufficiently large. 365f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 366f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcForMalloc(false); 3676343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAlloc(size); 3686343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 3696343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 370f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 371f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 372f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Even that didn't work; this is an exceptional state. 373f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Try harder, growing the heap if necessary. 374f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 3756343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAllocAndGrow(size); 376f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSizeChanged(); 3776343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 378f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t newHeapSize; 379f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 380f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project newHeapSize = dvmHeapSourceGetIdealFootprint(); 381f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: may want to grow a little bit more so that the amount of free 382f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// space is equal to the old free space + the utilization slop for 383f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project// the new allocation. 384f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI_HEAP("Grow heap (frag case) to " 385f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "%zu.%03zuMB for %zu-byte allocation\n", 386f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project FRACTIONAL_MB(newHeapSize), size); 3876343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 388f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 389f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 390f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Most allocations should have succeeded by now, so the heap 391f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * is really full, really fragmented, or the requested size is 392f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * really big. Do another GC, collecting SoftReferences this 393f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * time. The VM spec requires that all SoftReferences have 394f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * been collected and cleared before throwing an OOME. 395f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 396f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: wait for the finalizers from the previous GC to finish 397f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectcollect_soft_refs: 398f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI_HEAP("Forcing collection of SoftReferences for %zu-byte allocation\n", 399f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size); 400f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcForMalloc(true); 4016343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = dvmHeapSourceAllocAndGrow(size); 402f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSizeChanged(); 4036343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 4046343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return ptr; 405f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 406f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: maybe wait for finalizers and try one last time 407f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 408f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGE_HEAP("Out of memory on a %zd-byte allocation.\n", size); 409f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: tell the HeapSource to dump its state 410f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDumpThread(dvmThreadSelf(), false); 411f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 412f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return NULL; 413f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 414f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 415f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Throw an OutOfMemoryError if there's a thread to attach it to. 416f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Avoid recursing. 417f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 418f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The caller must not be holding the heap lock, or else the allocations 419f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in dvmThrowException() will deadlock. 420f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 421f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectstatic void throwOOME() 422f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 423f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread *self; 424f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 425f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((self = dvmThreadSelf()) != NULL) { 426f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* If the current (failing) dvmMalloc() happened as part of thread 427f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * creation/attachment before the thread became part of the root set, 428f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we can't rely on the thread-local trackedAlloc table, so 429f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we can't keep track of a real allocated OOME object. But, since 430f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * the thread is in the process of being created, it won't have 431f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * a useful stack anyway, so we may as well make things easier 432f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * by throwing the (stackless) pre-built OOME. 433f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 434f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (dvmIsOnThreadList(self) && !self->throwingOOME) { 435f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Let ourselves know that we tried to throw an OOM 436f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * error in the normal way in case we run out of 437f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * memory trying to allocate it inside dvmThrowException(). 438f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 439f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->throwingOOME = true; 440f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 441f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't include a description string; 442f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * one fewer allocation. 443f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 444f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmThrowException("Ljava/lang/OutOfMemoryError;", NULL); 445f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 446f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 447f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This thread has already tried to throw an OutOfMemoryError, 448f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * which probably means that we're running out of memory 449f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * while recursively trying to throw. 450f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 451f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * To avoid any more allocation attempts, "throw" a pre-built 452f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * OutOfMemoryError object (which won't have a useful stack trace). 453f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 454f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Note that since this call can't possibly allocate anything, 455f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we don't care about the state of self->throwingOOME 456f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * (which will usually already be set). 457f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 458f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmSetException(self, gDvm.outOfMemoryObj); 459f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 460f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* We're done with the possible recursion. 461f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 462f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->throwingOOME = false; 463f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 464f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 465f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 466f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 467f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Allocate storage on the GC heap. We guarantee 8-byte alignment. 468f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 469f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The new storage is zeroed out. 470f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 471f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Note that, in rare cases, this could get called while a GC is in 472f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * progress. If a non-VM thread tries to attach itself through JNI, 473f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * it will need to allocate some objects. If this becomes annoying to 474f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * deal with, we can block it at the source, but holding the allocation 475f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * mutex should be enough. 476f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 477f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * In rare circumstances (JNI AttachCurrentThread) we can be called 478f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * from a non-VM thread. 479f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 480f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * We implement ALLOC_NO_GC by maintaining an internal list of objects 481f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * that should not be collected. This requires no actual flag storage in 482f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * the object itself, which is good, but makes flag queries expensive. 483f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 484f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Use ALLOC_DONT_TRACK when we either don't want to track an allocation 485f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * (because it's being done for the interpreter "new" operation and will 486f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * be part of the root set immediately) or we can't (because this allocation 487f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * is for a brand new thread). 488f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 489f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Returns NULL and throws an exception on failure. 490f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 491f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * TODO: don't do a GC if the debugger thinks all threads are suspended 492f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 493f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid* dvmMalloc(size_t size, int flags) 494f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 495f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap = gDvm.gcHeap; 496f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project void *ptr; 497f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 498f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if 0 499f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* handy for spotting large allocations */ 500f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (size >= 100000) { 501f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI("dvmMalloc(%d):\n", size); 502f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDumpThread(dvmThreadSelf(), false); 503f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 504f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 505f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 506f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if defined(WITH_ALLOC_LIMITS) 507f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 508f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * See if they've exceeded the allocation limit for this thread. 509f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 510f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * A limit value of -1 means "no limit". 511f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 512f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This is enabled at compile time because it requires us to do a 513f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * TLS lookup for the Thread pointer. This has enough of a performance 514f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * impact that we don't want to do it if we don't have to. (Now that 515f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we're using gDvm.checkAllocLimits we may want to reconsider this, 516f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * but it's probably still best to just compile the check out of 517f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * production code -- one less thing to hit on every allocation.) 518f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 519f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.checkAllocLimits) { 520f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 521f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 522f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int count = self->allocLimit; 523f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (count > 0) { 524f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocLimit--; 525f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else if (count == 0) { 526f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* fail! */ 527f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(!gDvm.initializing); 528f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocLimit = -1; 529f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmThrowException("Ldalvik/system/AllocationLimitError;", 530f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "thread allocation limit exceeded"); 531f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return NULL; 532f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 533f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 534f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 535f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 536f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocationLimit >= 0) { 537f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project assert(!gDvm.initializing); 538f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocationLimit = -1; 539f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmThrowException("Ldalvik/system/AllocationLimitError;", 540f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "global allocation limit exceeded"); 541f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return NULL; 542f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 543f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 544f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 545f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockHeap(); 546f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 547f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Try as hard as possible to allocate some memory. 548f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 5496343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro ptr = tryMalloc(size); 5506343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (ptr != NULL) { 551f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* We've got the memory. 552f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 553f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((flags & ALLOC_FINALIZABLE) != 0) { 554f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* This object is an instance of a class that 555f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * overrides finalize(). Add it to the finalizable list. 556f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 557f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Note that until DVM_OBJECT_INIT() is called on this 558f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * object, its clazz will be NULL. Since the object is 559f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in this table, it will be scanned as part of the root 560f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * set. scanObject() explicitly deals with the NULL clazz. 561f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 562f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (!dvmHeapAddRefToLargeTable(&gcHeap->finalizableRefs, 5636343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro (Object *)ptr)) 564f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project { 565f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGE_HEAP("dvmMalloc(): no room for any more " 566f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "finalizable objects\n"); 567f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAbort(); 568f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 569f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 570f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 571f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The caller may not want us to collect this object. 572f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If not, throw it in the nonCollectableRefs table, which 573f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * will be added to the root set when we GC. 574f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 575f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Note that until DVM_OBJECT_INIT() is called on this 576f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * object, its clazz will be NULL. Since the object is 577f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in this table, it will be scanned as part of the root 578f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * set. scanObject() explicitly deals with the NULL clazz. 579f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 580f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((flags & ALLOC_NO_GC) != 0) { 581f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (!dvmHeapAddToHeapRefTable(&gcHeap->nonCollectableRefs, ptr)) { 582f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGE_HEAP("dvmMalloc(): no room for any more " 583f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "ALLOC_NO_GC objects: %zd\n", 584f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapNumHeapRefTableEntries( 585f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project &gcHeap->nonCollectableRefs)); 586f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAbort(); 587f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 588f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 589f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 590f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 591f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocProf.enabled) { 592f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 593f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.allocCount++; 594f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.allocSize += size; 595f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 596f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.allocCount++; 597f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.allocSize += size; 598f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 599f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 600f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 601f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 602f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The allocation failed. 603f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 604f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 605f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 606f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.allocProf.enabled) { 607f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Thread* self = dvmThreadSelf(); 608f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.failedAllocCount++; 609f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.allocProf.failedAllocSize += size; 610f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (self != NULL) { 611f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.failedAllocCount++; 612f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project self->allocProf.failedAllocSize += size; 613f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 614f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 615f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 616f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 617f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 618f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockHeap(); 619f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 620f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (ptr != NULL) { 621f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* 622f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If this block is immediately GCable, and they haven't asked us not 623f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * to track it, add it to the internal tracking list. 624f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 625f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If there's no "self" yet, we can't track it. Calls made before 626f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * the Thread exists should use ALLOC_NO_GC. 627f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 628f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((flags & (ALLOC_DONT_TRACK | ALLOC_NO_GC)) == 0) { 629f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAddTrackedAlloc(ptr, NULL); 630f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 631f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 632c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng /* 633f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The allocation failed; throw an OutOfMemoryError. 634f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 635f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project throwOOME(); 636f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 637f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 638f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return ptr; 639f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 640f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 641f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 642f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Returns true iff <obj> points to a valid allocated object. 643f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 644f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectbool dvmIsValidObject(const Object* obj) 645f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 646f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't bother if it's NULL or not 8-byte aligned. 647f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 6486343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro if (obj != NULL && ((uintptr_t)obj & (8-1)) == 0) { 649f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Even if the heap isn't locked, this shouldn't return 650f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * any false negatives. The only mutation that could 651f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * be happening is allocation, which means that another 652f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * thread could be in the middle of a read-modify-write 653f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * to add a new bit for a new object. However, that 654f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * RMW will have completed by the time any other thread 655f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * could possibly see the new pointer, so there is no 656f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * danger of dvmIsValidObject() being called on a valid 657f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * pointer whose bit isn't set. 658f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 659f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Freeing will only happen during the sweep phase, which 660f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * only happens while the heap is locked. 661f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 6626343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return dvmHeapSourceContains(obj); 663f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 664f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return false; 665f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 666f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 667f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 668f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Clear flags that were passed into dvmMalloc() et al. 669f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * e.g., ALLOC_NO_GC, ALLOC_DONT_TRACK. 670f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 671f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmClearAllocFlags(Object *obj, int mask) 672f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 673f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((mask & ALLOC_NO_GC) != 0) { 674f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockHeap(); 675f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (dvmIsValidObject(obj)) { 676f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (!dvmHeapRemoveFromHeapRefTable(&gDvm.gcHeap->nonCollectableRefs, 677f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project obj)) 678f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project { 679f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGE_HEAP("dvmMalloc(): failed to remove ALLOC_NO_GC bit from " 680f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project "object 0x%08x\n", (uintptr_t)obj); 681f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAbort(); 682f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 683f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: shrink if the table is very empty 684f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 685f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockHeap(); 686f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 687f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 688f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if ((mask & ALLOC_DONT_TRACK) != 0) { 689f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmReleaseTrackedAlloc(obj, NULL); 690f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 691f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 692f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 693f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectsize_t dvmObjectSizeInHeap(const Object *obj) 694f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 6956343bd071555458d0cb071f2eaf15b59b36771bfCarl Shapiro return dvmHeapSourceChunkSize(obj); 696f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 697f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 698f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 699f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Initiate garbage collection. 700f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 701f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * NOTES: 702f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * - If we don't hold gDvm.threadListLock, it's possible for a thread to 703f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * be added to the thread list while we work. The thread should NOT 704f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * start executing, so this is only interesting when we start chasing 705f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * thread stacks. (Before we do so, grab the lock.) 706f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 707f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * We are not allowed to GC when the debugger has suspended the VM, which 708f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * is awkward because debugger requests can cause allocations. The easiest 709f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * way to enforce this is to refuse to GC on an allocation made by the 710f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * JDWP thread -- we have to expand the heap or fail. 711f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 7121b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayesvoid dvmCollectGarbageInternal(bool collectSoftReferences, enum GcReason reason) 713f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 714f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project GcHeap *gcHeap = gDvm.gcHeap; 715f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object *softReferences; 716f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object *weakReferences; 717f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project Object *phantomReferences; 718f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 719f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project u8 now; 720f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project s8 timeSinceLastGc; 721f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project s8 gcElapsedTime; 722f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int numFreed; 723f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t sizeFreed; 724f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 725f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DVM_TRACK_HEAP_MARKING 726f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Since weak and soft references are always cleared, 727f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * they don't require any marking. 728f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * (Soft are lumped into strong when they aren't cleared.) 729f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 730f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t strongMarkCount = 0; 731f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t strongMarkSize = 0; 732f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t finalizeMarkCount = 0; 733f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t finalizeMarkSize = 0; 734f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t phantomMarkCount = 0; 735f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project size_t phantomMarkSize = 0; 736f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 737f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 738f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* The heap lock must be held. 739f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 740f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 741f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->gcRunning) { 742f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGW_HEAP("Attempted recursive GC\n"); 743f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project return; 744f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 745f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->gcRunning = true; 746f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project now = dvmGetRelativeTimeUsec(); 747f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->gcStartTime != 0) { 748f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project timeSinceLastGc = (now - gcHeap->gcStartTime) / 1000; 749f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 750f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project timeSinceLastGc = 0; 751f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 752f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->gcStartTime = now; 753f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 7541b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes LOGV_HEAP("%s starting -- suspending threads\n", GcReasonStr[reason]); 755f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 756f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmSuspendAllThreads(SUSPEND_FOR_GC); 757f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 758f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Get the priority (the "nice" value) of the current thread. The 759f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * getpriority() call can legitimately return -1, so we have to 760f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * explicitly test errno. 761f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 762f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project errno = 0; 763f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int oldThreadPriority = kInvalidPriority; 764f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project int priorityResult = getpriority(PRIO_PROCESS, 0); 765f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (errno != 0) { 766f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI_HEAP("getpriority(self) failed: %s\n", strerror(errno)); 767f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else if (priorityResult > ANDROID_PRIORITY_NORMAL) { 768f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Current value is numerically greater than "normal", which 769f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in backward UNIX terms means lower priority. 770f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 771256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat 7723e371e2ebbeeefe176ba1357e6ff241bd13a711eSan Mehat if (priorityResult >= ANDROID_PRIORITY_BACKGROUND) { 7735a2056ca8c7e7c0ed9f51d68cedbee59cc936685San Mehat set_sched_policy(dvmGetSysThreadId(), SP_FOREGROUND); 774256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat } 775256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat 776f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL) != 0) { 777f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI_HEAP("Unable to elevate priority from %d to %d\n", 778f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project priorityResult, ANDROID_PRIORITY_NORMAL); 779f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 780f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* priority elevated; save value so we can restore it later */ 781f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Elevating priority from %d to %d\n", 782f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project priorityResult, ANDROID_PRIORITY_NORMAL); 783f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project oldThreadPriority = priorityResult; 784f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 785f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 786f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 787f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Wait for the HeapWorker thread to block. 788f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * (It may also already be suspended in interp code, 789f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in which case it's not holding heapWorkerLock.) 790f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 791f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.heapWorkerLock); 792f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 793f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Make sure that the HeapWorker thread hasn't become 794f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * wedged inside interp code. If it has, this call will 795f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * print a message and abort the VM. 796f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 797f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmAssertHeapWorkerThreadRunning(); 798f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 799f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Lock the pendingFinalizationRefs list. 800f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 801f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Acquire the lock after suspending so the finalizer 802f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * thread can't block in the RUNNING state while 803f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we try to suspend. 804f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 805f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.heapWorkerListLock); 806f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 807f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 808f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmMethodTraceGCBegin(); 809f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 810f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 811f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 812f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 813f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* Set DUMP_HEAP_ON_DDMS_UPDATE to 1 to enable heap dumps 814f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * whenever DDMS requests a heap update (HPIF chunk). 815f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * The output files will appear in /data/misc, which must 816f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * already exist. 817f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * You must define "WITH_HPROF := true" in your buildspec.mk 818f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * and recompile libdvm for this to work. 819f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 820f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * To enable stack traces for each allocation, define 821f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * "WITH_HPROF_STACK := true" in buildspec.mk. This option slows down 822f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * allocations and also requires 8 additional bytes per object on the 823f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * GC heap. 824f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 825f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#define DUMP_HEAP_ON_DDMS_UPDATE 0 826f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DUMP_HEAP_ON_DDMS_UPDATE 827f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofDumpOnGc |= (gcHeap->ddmHpifWhen != 0); 828f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 829f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 830f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofDumpOnGc) { 831f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project char nameBuf[128]; 832f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 83399409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project gcHeap->hprofResult = -1; 83499409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project 835f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofFileName == NULL) { 836f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* no filename was provided; invent one */ 837f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project sprintf(nameBuf, "/data/misc/heap-dump-tm%d-pid%d.hprof", 838f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project (int) time(NULL), (int) getpid()); 839f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofFileName = nameBuf; 840f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 8416bf992c9d51f1e12aa37fe4c791c156402a9b79bAndy McFadden gcHeap->hprofContext = hprofStartup(gcHeap->hprofFileName, 8426bf992c9d51f1e12aa37fe4c791c156402a9b79bAndy McFadden gcHeap->hprofDirectToDdms); 843f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofContext != NULL) { 844f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project hprofStartHeapDump(gcHeap->hprofContext); 845f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 846f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofDumpOnGc = false; 847f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofFileName = NULL; 848f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 849f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 850f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 851f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (timeSinceLastGc < 10000) { 852f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("GC! (%dms since last GC)\n", 853f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project (int)timeSinceLastGc); 854f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 855f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("GC! (%d sec since last GC)\n", 856f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project (int)(timeSinceLastGc / 1000)); 857f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 858f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DVM_TRACK_HEAP_MARKING 859f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markCount = 0; 860f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markSize = 0; 861f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 862f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 863f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Set up the marking context. 864f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 86599409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project if (!dvmHeapBeginMarkStep()) { 86699409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project LOGE_HEAP("dvmHeapBeginMarkStep failed; aborting\n"); 86799409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project dvmAbort(); 86899409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project } 869f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 870f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Mark the set of objects that are strongly reachable from the roots. 871f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 872f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Marking..."); 873f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapMarkRootSet(); 874f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 875f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* dvmHeapScanMarkedObjects() will build the lists of known 876f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * instances of the Reference classes. 877f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 878f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferences = NULL; 879f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->weakReferences = NULL; 880f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->phantomReferences = NULL; 881f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 882f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Make sure that we don't hard-mark the referents of Reference 883f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * objects by default. 884f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 885f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markAllReferents = false; 886f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 887f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Don't mark SoftReferences if our caller wants us to collect them. 888f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This has to be set before calling dvmHeapScanMarkedObjects(). 889f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 890f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (collectSoftReferences) { 891f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceCollectionState = SR_COLLECT_ALL; 892f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 893f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 894f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Recursively mark any objects that marked objects point to strongly. 895f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If we're not collecting soft references, soft-reachable 896f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * objects will also be marked. 897f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 898f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Recursing..."); 899f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapScanMarkedObjects(); 900f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DVM_TRACK_HEAP_MARKING 901f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project strongMarkCount = gcHeap->markCount; 902f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project strongMarkSize = gcHeap->markSize; 903f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markCount = 0; 904f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markSize = 0; 905f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 906f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 907f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Latch these so that the other calls to dvmHeapScanMarkedObjects() don't 908f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * mess with them. 909f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 910f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project softReferences = gcHeap->softReferences; 911f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project weakReferences = gcHeap->weakReferences; 912f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project phantomReferences = gcHeap->phantomReferences; 913f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 914f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* All strongly-reachable objects have now been marked. 915f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 916f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->softReferenceCollectionState != SR_COLLECT_NONE) { 917f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Handling soft references..."); 918f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapHandleReferences(softReferences, REF_SOFT); 919f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project // markCount always zero 920f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 921f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Now that we've tried collecting SoftReferences, 922f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * fall back to not collecting them. If the heap 923f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * grows, we will start collecting again. 924f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 925f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->softReferenceCollectionState = SR_COLLECT_NONE; 926f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } // else dvmHeapScanMarkedObjects() already marked the soft-reachable set 927f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Handling weak references..."); 928f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapHandleReferences(weakReferences, REF_WEAK); 929f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project // markCount always zero 930f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 931f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Once all weak-reachable objects have been taken 932f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * care of, any remaining unmarked objects can be finalized. 933f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 934f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Finding finalizations..."); 935f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapScheduleFinalizations(); 936f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DVM_TRACK_HEAP_MARKING 937f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project finalizeMarkCount = gcHeap->markCount; 938f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project finalizeMarkSize = gcHeap->markSize; 939f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markCount = 0; 940f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markSize = 0; 941f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 942f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 943f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Any remaining objects that are not pending finalization 944f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * could be phantom-reachable. This will mark any phantom-reachable 945f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * objects, as well as enqueue their references. 946f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 947f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Handling phantom references..."); 948f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapHandleReferences(phantomReferences, REF_PHANTOM); 949f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DVM_TRACK_HEAP_MARKING 950f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project phantomMarkCount = gcHeap->markCount; 951f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project phantomMarkSize = gcHeap->markSize; 952f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markCount = 0; 953f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->markSize = 0; 954f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 955f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 956f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: take care of JNI weak global references 957f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 958f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if DVM_TRACK_HEAP_MARKING 959f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGI_HEAP("Marked objects: %dB strong, %dB final, %dB phantom\n", 960f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project strongMarkSize, finalizeMarkSize, phantomMarkSize); 961f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 962f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 963f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_DEADLOCK_PREDICTION 964f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDumpMonitorInfo("before sweep"); 965f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 966f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Sweeping..."); 967f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSweepUnmarkedObjects(&numFreed, &sizeFreed); 968f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_DEADLOCK_PREDICTION 969f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDumpMonitorInfo("after sweep"); 970f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 971f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 972f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Cleaning up..."); 973f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapFinishMarkStep(); 974f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 975f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Done."); 976f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 977f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Now's a good time to adjust the heap size, since 978f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * we know what our utilization is. 979f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 980f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * This doesn't actually resize any memory; 981f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * it just lets the heap grow more when necessary. 982f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 983f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSourceGrowForUtilization(); 984f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmHeapSizeChanged(); 985f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 986f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 987f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->hprofContext != NULL) { 988f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project hprofFinishHeapDump(gcHeap->hprofContext); 989f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project//TODO: write a HEAP_SUMMARY record 99099409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project if (hprofShutdown(gcHeap->hprofContext)) 99199409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project gcHeap->hprofResult = 0; /* indicate success */ 992f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->hprofContext = NULL; 993f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 994f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 995f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 996f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project /* Now that we've freed up the GC heap, return any large 997f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * free chunks back to the system. They'll get paged back 998f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * in the next time they're used. Don't do it immediately, 999f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * though; if the process is still allocating a bunch of 1000f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * memory, we'll be taking a ton of page faults that we don't 1001f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * necessarily need to. 1002f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 1003f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Cancel any old scheduled trims, and schedule a new one. 1004f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 1005f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmScheduleHeapSourceTrim(5); // in seconds 1006f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1007f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#ifdef WITH_PROFILER 1008f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmMethodTraceGCEnd(); 1009f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 1010f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGV_HEAP("GC finished -- resuming threads\n"); 1011f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1012f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcHeap->gcRunning = false; 1013f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1014f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.heapWorkerListLock); 1015f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.heapWorkerLock); 1016f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1017c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng#if defined(WITH_JIT) 1018c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng extern void dvmCompilerPerformSafePointChecks(void); 1019c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng 1020c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng /* 1021c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * Patching a chaining cell is very cheap as it only updates 4 words. It's 1022c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * the overhead of stopping all threads and synchronizing the I/D cache 1023c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * that makes it expensive. 1024c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * 1025c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * Therefore we batch those work orders in a queue and go through them 1026c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng * when threads are suspended for GC. 1027c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng */ 1028c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng dvmCompilerPerformSafePointChecks(); 1029c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng#endif 1030c3b92b26df6416d3179e865adccb283ee4170ab1Ben Cheng 1031f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmResumeAllThreads(SUSPEND_FOR_GC); 1032f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (oldThreadPriority != kInvalidPriority) { 1033f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (setpriority(PRIO_PROCESS, 0, oldThreadPriority) != 0) { 1034f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGW_HEAP("Unable to reset priority to %d: %s\n", 1035f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project oldThreadPriority, strerror(errno)); 1036f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } else { 1037f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Reset priority to %d\n", oldThreadPriority); 1038f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 1039256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat 10403e371e2ebbeeefe176ba1357e6ff241bd13a711eSan Mehat if (oldThreadPriority >= ANDROID_PRIORITY_BACKGROUND) { 10415a2056ca8c7e7c0ed9f51d68cedbee59cc936685San Mehat set_sched_policy(dvmGetSysThreadId(), SP_BACKGROUND); 1042256fc159a267859c18e11e1d15fd7d97a59757c6San Mehat } 1043f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 1044f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gcElapsedTime = (dvmGetRelativeTimeUsec() - gcHeap->gcStartTime) / 1000; 10453136413fae48cee04095236a945e41782d6d10bcBarry Hayes LOGD("%s freed %d objects / %zd bytes in %dms\n", 10463136413fae48cee04095236a945e41782d6d10bcBarry Hayes GcReasonStr[reason], numFreed, sizeFreed, (int)gcElapsedTime); 1047f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLogGcStats(numFreed, sizeFreed, gcElapsedTime); 1048f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1049f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->ddmHpifWhen != 0) { 1050f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Sending VM heap info to DDM\n"); 1051f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDdmSendHeapInfo(gcHeap->ddmHpifWhen, false); 1052f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 1053f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->ddmHpsgWhen != 0) { 1054f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Dumping VM heap to DDM\n"); 1055f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDdmSendHeapSegments(false, false); 1056f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 1057f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gcHeap->ddmNhsgWhen != 0) { 1058f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project LOGD_HEAP("Dumping native heap to DDM\n"); 1059f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmDdmSendHeapSegments(false, true); 1060f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 1061f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 1062f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1063f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#if WITH_HPROF 1064f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project/* 1065f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * Perform garbage collection, writing heap information to the specified file. 1066f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * 1067f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project * If "fileName" is NULL, a suitable name will be generated automatically. 106899409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project * 106999409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project * Returns 0 on success, or an error code on failure. 1070f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project */ 10716bf992c9d51f1e12aa37fe4c791c156402a9b79bAndy McFaddenint hprofDumpHeap(const char* fileName, bool directToDdms) 1072f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 107399409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project int result; 107499409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project 1075f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmLockMutex(&gDvm.gcHeapLock); 1076f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1077f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap->hprofDumpOnGc = true; 1078f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project gDvm.gcHeap->hprofFileName = fileName; 10796bf992c9d51f1e12aa37fe4c791c156402a9b79bAndy McFadden gDvm.gcHeap->hprofDirectToDdms = directToDdms; 10801b9b4e4b89e1c682b6684ae5e2a637e4497a67e9Barry Hayes dvmCollectGarbageInternal(false, GC_HPROF_DUMP_HEAP); 108199409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project result = gDvm.gcHeap->hprofResult; 1082f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1083f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project dvmUnlockMutex(&gDvm.gcHeapLock); 108499409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project 108599409883d9c4c0ffb49b070ce307bb33a9dfe9f1The Android Open Source Project return result; 1086f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 1087f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project 1088f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Projectvoid dvmHeapSetHprofGcScanState(hprof_heap_tag_t state, u4 threadSerialNumber) 1089f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project{ 1090f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project if (gDvm.gcHeap->hprofContext != NULL) { 1091f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project hprofSetGcScanState(gDvm.gcHeap->hprofContext, state, 1092f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project threadSerialNumber); 1093f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project } 1094f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project} 1095f6c387128427e121477c1b32ad35cdcaa5101ba3The Android Open Source Project#endif 1096