| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Dalvik.h" |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 18 | #include "alloc/clz.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 19 | #include "alloc/HeapBitmap.h" |
| 20 | #include "alloc/HeapInternal.h" |
| 21 | #include "alloc/HeapSource.h" |
| 22 | #include "alloc/MarkSweep.h" |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 23 | #include "alloc/Visit.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 24 | #include <limits.h> // for ULONG_MAX |
| 25 | #include <sys/mman.h> // for madvise(), mmap() |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 26 | #include <errno.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 27 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | #define GC_LOG_TAG LOG_TAG "-gc" |
| 29 | |
| 30 | #if LOG_NDEBUG |
| 31 | #define LOGV_GC(...) ((void)0) |
| 32 | #define LOGD_GC(...) ((void)0) |
| 33 | #else |
| 34 | #define LOGV_GC(...) LOG(LOG_VERBOSE, GC_LOG_TAG, __VA_ARGS__) |
| 35 | #define LOGD_GC(...) LOG(LOG_DEBUG, GC_LOG_TAG, __VA_ARGS__) |
| 36 | #endif |
| 37 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 38 | #define LOGI_GC(...) LOG(LOG_INFO, GC_LOG_TAG, __VA_ARGS__) |
| 39 | #define LOGW_GC(...) LOG(LOG_WARN, GC_LOG_TAG, __VA_ARGS__) |
| 40 | #define LOGE_GC(...) LOG(LOG_ERROR, GC_LOG_TAG, __VA_ARGS__) |
| 41 | |
| 42 | #define LOG_SCAN(...) LOGV_GC("SCAN: " __VA_ARGS__) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 43 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | #define ALIGN_UP_TO_PAGE_SIZE(p) \ |
| Andy McFadden | 9651693 | 2009-10-28 17:39:02 -0700 | [diff] [blame] | 45 | (((size_t)(p) + (SYSTEM_PAGE_SIZE - 1)) & ~(SYSTEM_PAGE_SIZE - 1)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | |
| 47 | /* Do not cast the result of this to a boolean; the only set bit |
| 48 | * may be > 1<<8. |
| 49 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 50 | static inline long isMarked(const void *obj, const GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 51 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 52 | return dvmHeapBitmapIsObjectBitSet(ctx->bitmap, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static bool |
| 56 | createMarkStack(GcMarkStack *stack) |
| 57 | { |
| 58 | const Object **limit; |
| Carl Shapiro | 742c445 | 2010-07-13 18:28:13 -0700 | [diff] [blame] | 59 | const char *name; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 60 | size_t size; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 61 | |
| 62 | /* Create a stack big enough for the worst possible case, |
| 63 | * where the heap is perfectly full of the smallest object. |
| 64 | * TODO: be better about memory usage; use a smaller stack with |
| 65 | * overflow detection and recovery. |
| 66 | */ |
| 67 | size = dvmHeapSourceGetIdealFootprint() * sizeof(Object*) / |
| 68 | (sizeof(Object) + HEAP_SOURCE_CHUNK_OVERHEAD); |
| 69 | size = ALIGN_UP_TO_PAGE_SIZE(size); |
| Carl Shapiro | 742c445 | 2010-07-13 18:28:13 -0700 | [diff] [blame] | 70 | name = "dalvik-mark-stack"; |
| 71 | limit = dvmAllocRegion(size, PROT_READ | PROT_WRITE, name); |
| 72 | if (limit == NULL) { |
| 73 | LOGE_GC("Could not mmap %zd-byte ashmem region '%s'", size, name); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | return false; |
| 75 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 76 | stack->limit = limit; |
| 77 | stack->base = (const Object **)((uintptr_t)limit + size); |
| 78 | stack->top = stack->base; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | destroyMarkStack(GcMarkStack *stack) |
| 84 | { |
| 85 | munmap((char *)stack->limit, |
| 86 | (uintptr_t)stack->base - (uintptr_t)stack->limit); |
| 87 | memset(stack, 0, sizeof(*stack)); |
| 88 | } |
| 89 | |
| 90 | #define MARK_STACK_PUSH(stack, obj) \ |
| 91 | do { \ |
| 92 | *--(stack).top = (obj); \ |
| 93 | } while (false) |
| 94 | |
| 95 | bool |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 96 | dvmHeapBeginMarkStep(GcMode mode) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 97 | { |
| 98 | GcMarkContext *mc = &gDvm.gcHeap->markContext; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 99 | |
| 100 | if (!createMarkStack(&mc->stack)) { |
| 101 | return false; |
| 102 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 103 | mc->finger = NULL; |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 104 | mc->immuneLimit = dvmHeapSourceGetImmuneLimit(mode); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 108 | static long |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 109 | setAndReturnMarkBit(GcMarkContext *ctx, const void *obj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 111 | return dvmHeapBitmapSetAndReturnObjectBit(ctx->bitmap, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 114 | static void |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 115 | markObjectNonNull(const Object *obj, GcMarkContext *ctx, |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | bool checkFinger, bool forceStack) |
| 117 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 118 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 119 | assert(obj != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 120 | assert(dvmIsValidObject(obj)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | |
| Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 122 | if (obj < (Object *)ctx->immuneLimit) { |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 123 | assert(isMarked(obj, ctx)); |
| 124 | return; |
| 125 | } |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 126 | if (!setAndReturnMarkBit(ctx, obj)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | /* This object was not previously marked. |
| 128 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 129 | if (forceStack || (checkFinger && (void *)obj < ctx->finger)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 130 | /* This object will need to go on the mark stack. |
| 131 | */ |
| 132 | MARK_STACK_PUSH(ctx->stack, obj); |
| 133 | } |
| 134 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 135 | #if WITH_HPROF |
| 136 | if (gDvm.gcHeap->hprofContext != NULL) { |
| 137 | hprofMarkRootObject(gDvm.gcHeap->hprofContext, obj, 0); |
| 138 | } |
| 139 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /* Used to mark objects when recursing. Recursion is done by moving |
| 144 | * the finger across the bitmaps in address order and marking child |
| 145 | * objects. Any newly-marked objects whose addresses are lower than |
| 146 | * the finger won't be visited by the bitmap scan, so those objects |
| 147 | * need to be added to the mark stack. |
| 148 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 149 | static void markObject(const Object *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 150 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 151 | if (obj != NULL) { |
| 152 | markObjectNonNull(obj, ctx, true, false); |
| 153 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 156 | /* If the object hasn't already been marked, mark it and |
| 157 | * schedule it to be scanned for references. |
| 158 | * |
| 159 | * obj may not be NULL. The macro dvmMarkObject() should |
| 160 | * be used in situations where a reference may be NULL. |
| 161 | * |
| 162 | * This function may only be called when marking the root |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 163 | * set. When recursing, use the internal markObject(). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | */ |
| 165 | void |
| 166 | dvmMarkObjectNonNull(const Object *obj) |
| 167 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 168 | assert(obj != NULL); |
| 169 | markObjectNonNull(obj, &gDvm.gcHeap->markContext, false, false); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Mark the set of root objects. |
| 173 | * |
| 174 | * Things we need to scan: |
| 175 | * - System classes defined by root classloader |
| 176 | * - For each thread: |
| 177 | * - Interpreted stack, from top to "curFrame" |
| 178 | * - Dalvik registers (args + local vars) |
| 179 | * - JNI local references |
| 180 | * - Automatic VM local references (TrackedAlloc) |
| 181 | * - Associated Thread/VMThread object |
| 182 | * - ThreadGroups (could track & start with these instead of working |
| 183 | * upward from Threads) |
| 184 | * - Exception currently being thrown, if present |
| 185 | * - JNI global references |
| 186 | * - Interned string table |
| 187 | * - Primitive classes |
| 188 | * - Special objects |
| 189 | * - gDvm.outOfMemoryObj |
| 190 | * - Objects allocated with ALLOC_NO_GC |
| 191 | * - Objects pending finalization (but not yet finalized) |
| 192 | * - Objects in debugger object registry |
| 193 | * |
| 194 | * Don't need: |
| 195 | * - Native stack (for in-progress stuff in the VM) |
| 196 | * - The TrackedAlloc stuff watches all native VM references. |
| 197 | */ |
| 198 | void dvmHeapMarkRootSet() |
| 199 | { |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 200 | GcHeap *gcHeap = gDvm.gcHeap; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 201 | |
| 202 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_STICKY_CLASS, 0); |
| 203 | |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 204 | LOG_SCAN("immune objects"); |
| Barry Hayes | 425848f | 2010-05-04 13:32:12 -0700 | [diff] [blame] | 205 | dvmMarkImmuneObjects(gcHeap->markContext.immuneLimit); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 206 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 207 | LOG_SCAN("root class loader\n"); |
| 208 | dvmGcScanRootClassLoader(); |
| 209 | LOG_SCAN("primitive classes\n"); |
| 210 | dvmGcScanPrimitiveClasses(); |
| 211 | |
| 212 | /* dvmGcScanRootThreadGroups() sets a bunch of |
| 213 | * different scan states internally. |
| 214 | */ |
| 215 | HPROF_CLEAR_GC_SCAN_STATE(); |
| 216 | |
| 217 | LOG_SCAN("root thread groups\n"); |
| 218 | dvmGcScanRootThreadGroups(); |
| 219 | |
| 220 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_INTERNED_STRING, 0); |
| 221 | |
| 222 | LOG_SCAN("interned strings\n"); |
| 223 | dvmGcScanInternedStrings(); |
| 224 | |
| 225 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_JNI_GLOBAL, 0); |
| 226 | |
| 227 | LOG_SCAN("JNI global refs\n"); |
| 228 | dvmGcMarkJniGlobalRefs(); |
| 229 | |
| 230 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_REFERENCE_CLEANUP, 0); |
| 231 | |
| 232 | LOG_SCAN("pending reference operations\n"); |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 233 | dvmHeapMarkLargeTableRefs(gcHeap->referenceOperations); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 234 | |
| 235 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0); |
| 236 | |
| 237 | LOG_SCAN("pending finalizations\n"); |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 238 | dvmHeapMarkLargeTableRefs(gcHeap->pendingFinalizationRefs); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 239 | |
| 240 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_DEBUGGER, 0); |
| 241 | |
| 242 | LOG_SCAN("debugger refs\n"); |
| 243 | dvmGcMarkDebuggerRefs(); |
| 244 | |
| 245 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_VM_INTERNAL, 0); |
| 246 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 247 | /* Mark any special objects we have sitting around. |
| 248 | */ |
| 249 | LOG_SCAN("special objects\n"); |
| 250 | dvmMarkObjectNonNull(gDvm.outOfMemoryObj); |
| 251 | dvmMarkObjectNonNull(gDvm.internalErrorObj); |
| Andy McFadden | 7fc3ce8 | 2009-07-14 15:57:23 -0700 | [diff] [blame] | 252 | dvmMarkObjectNonNull(gDvm.noClassDefFoundErrorObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 253 | //TODO: scan object references sitting in gDvm; use pointer begin & end |
| 254 | |
| 255 | HPROF_CLEAR_GC_SCAN_STATE(); |
| 256 | } |
| 257 | |
| 258 | /* |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 259 | * Nothing past this point is allowed to use dvmMarkObject() or |
| 260 | * dvmMarkObjectNonNull(), which are for root-marking only. |
| 261 | * Scanning/recursion must use markObject(), which takes the finger |
| 262 | * into account. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 263 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 264 | #undef dvmMarkObject |
| 265 | #define dvmMarkObject __dont_use_dvmMarkObject__ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 266 | #define dvmMarkObjectNonNull __dont_use_dvmMarkObjectNonNull__ |
| 267 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 268 | /* |
| 269 | * Scans instance fields. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 270 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 271 | static void scanInstanceFields(const Object *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 272 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 273 | assert(obj != NULL); |
| 274 | assert(obj->clazz != NULL); |
| 275 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 276 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 277 | if (obj->clazz->refOffsets != CLASS_WALK_SUPER) { |
| 278 | unsigned int refOffsets = obj->clazz->refOffsets; |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 279 | while (refOffsets != 0) { |
| 280 | const int rshift = CLZ(refOffsets); |
| 281 | refOffsets &= ~(CLASS_HIGH_BIT >> rshift); |
| 282 | markObject(dvmGetFieldObject((Object*)obj, |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 283 | CLASS_OFFSET_FROM_CLZ(rshift)), ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 284 | } |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 285 | } else { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 286 | ClassObject *clazz; |
| 287 | int i; |
| 288 | for (clazz = obj->clazz; clazz != NULL; clazz = clazz->super) { |
| 289 | InstField *field = clazz->ifields; |
| 290 | for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { |
| 291 | void *addr = BYTE_OFFSET((Object *)obj, field->byteOffset); |
| 292 | markObject(((JValue *)addr)->l, ctx); |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 293 | } |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 294 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 298 | /* |
| 299 | * Scans the header, static field references, and interface |
| 300 | * pointers of a class object. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 301 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 302 | static void scanClassObject(const ClassObject *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 303 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 304 | int i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 305 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 306 | assert(obj != NULL); |
| 307 | assert(obj->obj.clazz == gDvm.classJavaLangClass); |
| 308 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 309 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 310 | markObject((Object *)obj->obj.clazz, ctx); |
| 311 | if (IS_CLASS_FLAG_SET(obj, CLASS_ISARRAY)) { |
| 312 | markObject((Object *)obj->elementClass, ctx); |
| 313 | } |
| Barry Hayes | c49db85 | 2010-05-14 13:43:34 -0700 | [diff] [blame] | 314 | /* Do super and the interfaces contain Objects and not dex idx values? */ |
| 315 | if (obj->status > CLASS_IDX) { |
| 316 | markObject((Object *)obj->super, ctx); |
| 317 | } |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 318 | markObject(obj->classLoader, ctx); |
| 319 | /* Scan static field references. */ |
| 320 | for (i = 0; i < obj->sfieldCount; ++i) { |
| 321 | char ch = obj->sfields[i].field.signature[0]; |
| 322 | if (ch == '[' || ch == 'L') { |
| 323 | markObject(obj->sfields[i].value.l, ctx); |
| 324 | } |
| 325 | } |
| 326 | /* Scan the instance fields. */ |
| 327 | scanInstanceFields((const Object *)obj, ctx); |
| 328 | /* Scan interface references. */ |
| Barry Hayes | c49db85 | 2010-05-14 13:43:34 -0700 | [diff] [blame] | 329 | if (obj->status > CLASS_IDX) { |
| 330 | for (i = 0; i < obj->interfaceCount; ++i) { |
| 331 | markObject((Object *)obj->interfaces[i], ctx); |
| 332 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 336 | /* |
| 337 | * Scans the header of all array objects. If the array object is |
| 338 | * specialized to a reference type, scans the array data as well. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 339 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 340 | static void scanArrayObject(const ArrayObject *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 341 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 342 | size_t i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 343 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 344 | assert(obj != NULL); |
| 345 | assert(obj->obj.clazz != NULL); |
| 346 | assert(ctx != NULL); |
| 347 | /* Scan the class object reference. */ |
| 348 | markObject((Object *)obj->obj.clazz, ctx); |
| 349 | if (IS_CLASS_FLAG_SET(obj->obj.clazz, CLASS_ISOBJECTARRAY)) { |
| 350 | /* Scan the array contents. */ |
| 351 | Object **contents = (Object **)obj->contents; |
| 352 | for (i = 0; i < obj->length; ++i) { |
| 353 | markObject(contents[i], ctx); |
| 354 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 355 | } |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 356 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 357 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 358 | /* |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 359 | * Returns class flags relating to Reference subclasses. |
| 360 | */ |
| 361 | static int referenceClassFlags(const Object *obj) |
| 362 | { |
| 363 | int flags = CLASS_ISREFERENCE | |
| 364 | CLASS_ISWEAKREFERENCE | |
| 365 | CLASS_ISPHANTOMREFERENCE; |
| 366 | return GET_CLASS_FLAG_GROUP(obj->clazz, flags); |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Returns true if the object derives from SoftReference. |
| 371 | */ |
| 372 | static bool isSoftReference(const Object *obj) |
| 373 | { |
| 374 | return referenceClassFlags(obj) == CLASS_ISREFERENCE; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Returns true if the object derives from WeakReference. |
| 379 | */ |
| 380 | static bool isWeakReference(const Object *obj) |
| 381 | { |
| 382 | return referenceClassFlags(obj) & CLASS_ISWEAKREFERENCE; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Returns true if the object derives from PhantomReference. |
| 387 | */ |
| 388 | static bool isPhantomReference(const Object *obj) |
| 389 | { |
| 390 | return referenceClassFlags(obj) & CLASS_ISPHANTOMREFERENCE; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Adds a reference to the tail of a circular queue of references. |
| 395 | */ |
| 396 | static void enqueuePendingReference(Object *ref, Object **list) |
| 397 | { |
| 398 | size_t offset; |
| 399 | |
| 400 | assert(ref != NULL); |
| 401 | assert(list != NULL); |
| 402 | offset = gDvm.offJavaLangRefReference_pendingNext; |
| 403 | if (*list == NULL) { |
| 404 | dvmSetFieldObject(ref, offset, ref); |
| 405 | *list = ref; |
| 406 | } else { |
| 407 | Object *head = dvmGetFieldObject(*list, offset); |
| 408 | dvmSetFieldObject(ref, offset, head); |
| 409 | dvmSetFieldObject(*list, offset, ref); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 414 | * Removes the reference at the head of a circular queue of |
| 415 | * references. |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 416 | */ |
| 417 | static Object *dequeuePendingReference(Object **list) |
| 418 | { |
| 419 | Object *ref, *head; |
| 420 | size_t offset; |
| 421 | |
| 422 | assert(list != NULL); |
| 423 | assert(*list != NULL); |
| 424 | offset = gDvm.offJavaLangRefReference_pendingNext; |
| 425 | head = dvmGetFieldObject(*list, offset); |
| 426 | if (*list == head) { |
| 427 | ref = *list; |
| 428 | *list = NULL; |
| 429 | } else { |
| 430 | Object *next = dvmGetFieldObject(head, offset); |
| 431 | dvmSetFieldObject(*list, offset, next); |
| 432 | ref = head; |
| 433 | } |
| 434 | dvmSetFieldObject(ref, offset, NULL); |
| 435 | return ref; |
| 436 | } |
| 437 | |
| 438 | /* |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 439 | * Process the "referent" field in a java.lang.ref.Reference. If the |
| 440 | * referent has not yet been marked, put it on the appropriate list in |
| 441 | * the gcHeap for later processing. |
| 442 | */ |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 443 | static void delayReferenceReferent(Object *obj, GcMarkContext *ctx) |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 444 | { |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 445 | GcHeap *gcHeap = gDvm.gcHeap; |
| 446 | Object *pending, *referent; |
| 447 | size_t pendingNextOffset, referentOffset; |
| 448 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 449 | assert(obj != NULL); |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 450 | assert(obj->clazz != NULL); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 451 | assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 452 | assert(ctx != NULL); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 453 | pendingNextOffset = gDvm.offJavaLangRefReference_pendingNext; |
| 454 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 455 | pending = dvmGetFieldObject(obj, pendingNextOffset); |
| 456 | referent = dvmGetFieldObject(obj, referentOffset); |
| 457 | if (pending == NULL && referent != NULL && !isMarked(referent, ctx)) { |
| 458 | Object **list = NULL; |
| 459 | if (isSoftReference(obj)) { |
| 460 | list = &gcHeap->softReferences; |
| 461 | } else if (isWeakReference(obj)) { |
| 462 | list = &gcHeap->weakReferences; |
| 463 | } else if (isPhantomReference(obj)) { |
| 464 | list = &gcHeap->phantomReferences; |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 465 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 466 | assert(list != NULL); |
| 467 | enqueuePendingReference(obj, list); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 468 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 471 | /* |
| 472 | * Scans the header and field references of a data object. |
| 473 | */ |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 474 | static void scanDataObject(DataObject *obj, GcMarkContext *ctx) |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 475 | { |
| 476 | assert(obj != NULL); |
| 477 | assert(obj->obj.clazz != NULL); |
| 478 | assert(ctx != NULL); |
| 479 | /* Scan the class object. */ |
| 480 | markObject((Object *)obj->obj.clazz, ctx); |
| 481 | /* Scan the instance fields. */ |
| 482 | scanInstanceFields((const Object *)obj, ctx); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 483 | if (IS_CLASS_FLAG_SET(obj->obj.clazz, CLASS_ISREFERENCE)) { |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 484 | delayReferenceReferent((Object *)obj, ctx); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Scans an object reference. Determines the type of the reference |
| 490 | * and dispatches to a specialized scanning routine. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 491 | */ |
| 492 | static void scanObject(const Object *obj, GcMarkContext *ctx) |
| 493 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 494 | assert(obj != NULL); |
| 495 | assert(ctx != NULL); |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 496 | assert(obj->clazz != NULL); |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 497 | #if WITH_HPROF |
| 498 | if (gDvm.gcHeap->hprofContext != NULL) { |
| 499 | hprofDumpHeapObject(gDvm.gcHeap->hprofContext, obj); |
| 500 | } |
| 501 | #endif |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 502 | /* Dispatch a type-specific scan routine. */ |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 503 | if (obj->clazz == gDvm.classJavaLangClass) { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 504 | scanClassObject((ClassObject *)obj, ctx); |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 505 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 506 | scanArrayObject((ArrayObject *)obj, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 507 | } else { |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 508 | scanDataObject((DataObject *)obj, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 509 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static void |
| 513 | processMarkStack(GcMarkContext *ctx) |
| 514 | { |
| 515 | const Object **const base = ctx->stack.base; |
| 516 | |
| 517 | /* Scan anything that's on the mark stack. |
| 518 | * We can't use the bitmaps anymore, so use |
| 519 | * a finger that points past the end of them. |
| 520 | */ |
| 521 | ctx->finger = (void *)ULONG_MAX; |
| 522 | while (ctx->stack.top != base) { |
| 523 | scanObject(*ctx->stack.top++, ctx); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | #ifndef NDEBUG |
| 528 | static uintptr_t gLastFinger = 0; |
| 529 | #endif |
| 530 | |
| 531 | static bool |
| 532 | scanBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg) |
| 533 | { |
| 534 | GcMarkContext *ctx = (GcMarkContext *)arg; |
| 535 | size_t i; |
| 536 | |
| 537 | #ifndef NDEBUG |
| 538 | assert((uintptr_t)finger >= gLastFinger); |
| 539 | gLastFinger = (uintptr_t)finger; |
| 540 | #endif |
| 541 | |
| 542 | ctx->finger = finger; |
| 543 | for (i = 0; i < numPtrs; i++) { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 544 | scanObject(*ptrs++, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | /* Given bitmaps with the root set marked, find and mark all |
| 551 | * reachable objects. When this returns, the entire set of |
| 552 | * live objects will be marked and the mark stack will be empty. |
| 553 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 554 | void dvmHeapScanMarkedObjects(void) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 555 | { |
| 556 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| 557 | |
| 558 | assert(ctx->finger == NULL); |
| 559 | |
| 560 | /* The bitmaps currently have bits set for the root set. |
| 561 | * Walk across the bitmaps and scan each object. |
| 562 | */ |
| 563 | #ifndef NDEBUG |
| 564 | gLastFinger = 0; |
| 565 | #endif |
| Carl Shapiro | cedcb70 | 2010-07-22 20:22:49 -0700 | [diff] [blame^] | 566 | dvmHeapBitmapWalk(ctx->bitmap, scanBitmapCallback, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 567 | |
| 568 | /* We've walked the mark bitmaps. Scan anything that's |
| 569 | * left on the mark stack. |
| 570 | */ |
| 571 | processMarkStack(ctx); |
| 572 | |
| 573 | LOG_SCAN("done with marked objects\n"); |
| 574 | } |
| 575 | |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 576 | /* |
| 577 | * Callback applied to each gray object to blacken it. |
| 578 | */ |
| 579 | static bool dirtyObjectCallback(size_t numPtrs, void **ptrs, |
| 580 | const void *finger, void *arg) |
| 581 | { |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 582 | size_t i; |
| 583 | |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 584 | for (i = 0; i < numPtrs; ++i) { |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 585 | scanObject(ptrs[i], arg); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 586 | } |
| 587 | return true; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Re-mark dirtied objects. Iterates through all blackened objects |
| 592 | * looking for references to white objects. |
| 593 | */ |
| 594 | void dvmMarkDirtyObjects(void) |
| 595 | { |
| 596 | HeapBitmap markBits[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| 597 | HeapBitmap liveBits[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| 598 | GcMarkContext *ctx; |
| 599 | size_t numBitmaps; |
| 600 | size_t i; |
| 601 | |
| 602 | ctx = &gDvm.gcHeap->markContext; |
| 603 | /* |
| Carl Shapiro | f586033 | 2010-06-28 23:02:08 -0700 | [diff] [blame] | 604 | * The finger must have been set to the maximum value to ensure |
| 605 | * that gray objects will be pushed onto the mark stack. |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 606 | */ |
| 607 | assert(ctx->finger == (void *)ULONG_MAX); |
| 608 | numBitmaps = dvmHeapSourceGetNumHeaps(); |
| 609 | dvmHeapSourceGetObjectBitmaps(liveBits, markBits, numBitmaps); |
| 610 | for (i = 0; i < numBitmaps; i++) { |
| 611 | dvmHeapBitmapWalk(&markBits[i], dirtyObjectCallback, ctx); |
| 612 | } |
| 613 | processMarkStack(ctx); |
| 614 | } |
| 615 | |
| Carl Shapiro | 34f5199 | 2010-07-09 17:55:41 -0700 | [diff] [blame] | 616 | /* |
| 617 | * Clear the referent field. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 618 | */ |
| Barry Hayes | 6930a11 | 2009-12-22 11:01:38 -0800 | [diff] [blame] | 619 | static void clearReference(Object *reference) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 620 | { |
| Carl Shapiro | 34f5199 | 2010-07-09 17:55:41 -0700 | [diff] [blame] | 621 | size_t offset = gDvm.offJavaLangRefReference_referent; |
| 622 | dvmSetFieldObject(reference, offset, NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 625 | /* |
| 626 | * Returns true if the reference was registered with a reference queue |
| 627 | * and has not yet been enqueued. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 628 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 629 | static bool isEnqueuable(const Object *reference) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 630 | { |
| Barry Hayes | 6930a11 | 2009-12-22 11:01:38 -0800 | [diff] [blame] | 631 | Object *queue = dvmGetFieldObject(reference, |
| 632 | gDvm.offJavaLangRefReference_queue); |
| 633 | Object *queueNext = dvmGetFieldObject(reference, |
| 634 | gDvm.offJavaLangRefReference_queueNext); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 635 | return queue != NULL && queueNext == NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 638 | /* |
| 639 | * Schedules a reference to be appended to its reference queue. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 640 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 641 | static void enqueueReference(Object *ref) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 642 | { |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 643 | assert(ref != NULL); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 644 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL); |
| 645 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL); |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 646 | if (!dvmHeapAddRefToLargeTable(&gDvm.gcHeap->referenceOperations, ref)) { |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 647 | LOGE_HEAP("enqueueReference(): no room for any more " |
| 648 | "reference operations\n"); |
| 649 | dvmAbort(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 653 | /* |
| 654 | * Walks the reference list marking any references subject to the |
| 655 | * reference clearing policy. References with a black referent are |
| 656 | * removed from the list. References with white referents biased |
| 657 | * toward saving are blackened and also removed from the list. |
| 658 | */ |
| 659 | void dvmHandleSoftRefs(Object **list) |
| 660 | { |
| 661 | GcMarkContext *markContext; |
| 662 | Object *ref, *referent; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 663 | Object *clear; |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 664 | size_t referentOffset; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 665 | size_t counter; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 666 | bool marked; |
| 667 | |
| 668 | markContext = &gDvm.gcHeap->markContext; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 669 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 670 | clear = NULL; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 671 | counter = 0; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 672 | while (*list != NULL) { |
| 673 | ref = dequeuePendingReference(list); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 674 | referent = dvmGetFieldObject(ref, referentOffset); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 675 | assert(referent != NULL); |
| 676 | marked = isMarked(referent, markContext); |
| 677 | if (!marked && ((++counter) & 1)) { |
| 678 | /* Referent is white and biased toward saving, mark it. */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 679 | markObject(referent, markContext); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 680 | marked = true; |
| 681 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 682 | if (!marked) { |
| 683 | /* Referent is white, queue it for clearing. */ |
| 684 | enqueuePendingReference(ref, &clear); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 685 | } |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 686 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 687 | *list = clear; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 688 | /* |
| 689 | * Restart the mark with the newly black references added to the |
| 690 | * root set. |
| 691 | */ |
| 692 | processMarkStack(markContext); |
| 693 | } |
| 694 | |
| 695 | /* |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 696 | * Unlink the reference list clearing references objects with white |
| 697 | * referents. Cleared references registered to a reference queue are |
| 698 | * scheduled for appending by the heap worker thread. |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 699 | */ |
| 700 | void dvmClearWhiteRefs(Object **list) |
| 701 | { |
| 702 | GcMarkContext *markContext; |
| 703 | Object *ref, *referent; |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 704 | size_t referentOffset; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 705 | bool doSignal; |
| 706 | |
| 707 | markContext = &gDvm.gcHeap->markContext; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 708 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 709 | doSignal = false; |
| 710 | while (*list != NULL) { |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 711 | ref = dequeuePendingReference(list); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 712 | referent = dvmGetFieldObject(ref, referentOffset); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 713 | assert(referent != NULL); |
| 714 | if (!isMarked(referent, markContext)) { |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 715 | /* Referent is white, clear it. */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 716 | clearReference(ref); |
| 717 | if (isEnqueuable(ref)) { |
| 718 | enqueueReference(ref); |
| 719 | doSignal = true; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | /* |
| 724 | * If we cleared a reference with a reference queue we must notify |
| 725 | * the heap worker to append the reference. |
| 726 | */ |
| 727 | if (doSignal) { |
| 728 | dvmSignalHeapWorker(false); |
| 729 | } |
| 730 | assert(*list == NULL); |
| 731 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 732 | |
| 733 | /* Find unreachable objects that need to be finalized, |
| 734 | * and schedule them for finalization. |
| 735 | */ |
| 736 | void dvmHeapScheduleFinalizations() |
| 737 | { |
| 738 | HeapRefTable newPendingRefs; |
| 739 | LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs; |
| 740 | Object **ref; |
| 741 | Object **lastRef; |
| 742 | size_t totalPendCount; |
| 743 | GcMarkContext *markContext = &gDvm.gcHeap->markContext; |
| 744 | |
| 745 | /* |
| 746 | * All reachable objects have been marked. |
| 747 | * Any unmarked finalizable objects need to be finalized. |
| 748 | */ |
| 749 | |
| 750 | /* Create a table that the new pending refs will |
| 751 | * be added to. |
| 752 | */ |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 753 | if (!dvmHeapInitHeapRefTable(&newPendingRefs)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 754 | //TODO: mark all finalizable refs and hope that |
| 755 | // we can schedule them next time. Watch out, |
| 756 | // because we may be expecting to free up space |
| 757 | // by calling finalizers. |
| 758 | LOGE_GC("dvmHeapScheduleFinalizations(): no room for " |
| 759 | "pending finalizations\n"); |
| 760 | dvmAbort(); |
| 761 | } |
| 762 | |
| 763 | /* Walk through finalizableRefs and move any unmarked references |
| 764 | * to the list of new pending refs. |
| 765 | */ |
| 766 | totalPendCount = 0; |
| 767 | while (finRefs != NULL) { |
| 768 | Object **gapRef; |
| 769 | size_t newPendCount = 0; |
| 770 | |
| 771 | gapRef = ref = finRefs->refs.table; |
| 772 | lastRef = finRefs->refs.nextEntry; |
| 773 | while (ref < lastRef) { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 774 | if (!isMarked(*ref, markContext)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 775 | if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) { |
| 776 | //TODO: add the current table and allocate |
| 777 | // a new, smaller one. |
| 778 | LOGE_GC("dvmHeapScheduleFinalizations(): " |
| 779 | "no room for any more pending finalizations: %zd\n", |
| 780 | dvmHeapNumHeapRefTableEntries(&newPendingRefs)); |
| 781 | dvmAbort(); |
| 782 | } |
| 783 | newPendCount++; |
| 784 | } else { |
| 785 | /* This ref is marked, so will remain on finalizableRefs. |
| 786 | */ |
| 787 | if (newPendCount > 0) { |
| 788 | /* Copy it up to fill the holes. |
| 789 | */ |
| 790 | *gapRef++ = *ref; |
| 791 | } else { |
| 792 | /* No holes yet; don't bother copying. |
| 793 | */ |
| 794 | gapRef++; |
| 795 | } |
| 796 | } |
| 797 | ref++; |
| 798 | } |
| 799 | finRefs->refs.nextEntry = gapRef; |
| 800 | //TODO: if the table is empty when we're done, free it. |
| 801 | totalPendCount += newPendCount; |
| 802 | finRefs = finRefs->next; |
| 803 | } |
| 804 | LOGD_GC("dvmHeapScheduleFinalizations(): %zd finalizers triggered.\n", |
| 805 | totalPendCount); |
| 806 | if (totalPendCount == 0) { |
| 807 | /* No objects required finalization. |
| 808 | * Free the empty temporary table. |
| 809 | */ |
| 810 | dvmClearReferenceTable(&newPendingRefs); |
| 811 | return; |
| 812 | } |
| 813 | |
| 814 | /* Add the new pending refs to the main list. |
| 815 | */ |
| 816 | if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs, |
| 817 | &newPendingRefs)) |
| 818 | { |
| 819 | LOGE_GC("dvmHeapScheduleFinalizations(): can't insert new " |
| 820 | "pending finalizations\n"); |
| 821 | dvmAbort(); |
| 822 | } |
| 823 | |
| 824 | //TODO: try compacting the main list with a memcpy loop |
| 825 | |
| 826 | /* Mark the refs we just moved; we don't want them or their |
| 827 | * children to get swept yet. |
| 828 | */ |
| 829 | ref = newPendingRefs.table; |
| 830 | lastRef = newPendingRefs.nextEntry; |
| 831 | assert(ref < lastRef); |
| 832 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0); |
| 833 | while (ref < lastRef) { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 834 | assert(*ref != NULL); |
| 835 | markObject(*ref, markContext); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 836 | ref++; |
| 837 | } |
| 838 | HPROF_CLEAR_GC_SCAN_STATE(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 839 | processMarkStack(markContext); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 840 | dvmSignalHeapWorker(false); |
| 841 | } |
| 842 | |
| 843 | void dvmHeapFinishMarkStep() |
| 844 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 845 | GcMarkContext *markContext; |
| 846 | |
| 847 | markContext = &gDvm.gcHeap->markContext; |
| 848 | |
| 849 | /* The sweep step freed every object that appeared in the |
| 850 | * HeapSource bitmaps that didn't appear in the mark bitmaps. |
| 851 | * The new state of the HeapSource is exactly the final |
| 852 | * mark bitmaps, so swap them in. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 853 | */ |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 854 | dvmHeapSourceSwapBitmaps(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 855 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 856 | /* Clean up everything else associated with the marking process. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 857 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 858 | destroyMarkStack(&markContext->stack); |
| 859 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 860 | markContext->finger = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 863 | static bool |
| 864 | sweepBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg) |
| 865 | { |
| 866 | const ClassObject *const classJavaLangClass = gDvm.classJavaLangClass; |
| Barry Hayes | 5cbb230 | 2010-02-02 14:07:37 -0800 | [diff] [blame] | 867 | const bool overwriteFree = gDvm.overwriteFree; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 868 | size_t i; |
| 869 | |
| 870 | for (i = 0; i < numPtrs; i++) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 871 | Object *obj; |
| 872 | |
| Barry Hayes | 04174be | 2010-07-21 11:51:37 -0700 | [diff] [blame] | 873 | obj = (Object *)ptrs[i]; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 874 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 875 | /* This assumes that java.lang.Class will never go away. |
| 876 | * If it can, and we were the last reference to it, it |
| 877 | * could have already been swept. However, even in that case, |
| 878 | * gDvm.classJavaLangClass should still have a useful |
| 879 | * value. |
| 880 | */ |
| 881 | if (obj->clazz == classJavaLangClass) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 882 | /* dvmFreeClassInnards() may have already been called, |
| 883 | * but it's safe to call on the same ClassObject twice. |
| 884 | */ |
| 885 | dvmFreeClassInnards((ClassObject *)obj); |
| 886 | } |
| 887 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 888 | /* Overwrite the to-be-freed object to make stale references |
| 889 | * more obvious. |
| 890 | */ |
| Barry Hayes | 5cbb230 | 2010-02-02 14:07:37 -0800 | [diff] [blame] | 891 | if (overwriteFree) { |
| Barry Hayes | 2e3c3e1 | 2010-02-22 09:39:10 -0800 | [diff] [blame] | 892 | int objlen; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 893 | ClassObject *clazz = obj->clazz; |
| Barry Hayes | 2e3c3e1 | 2010-02-22 09:39:10 -0800 | [diff] [blame] | 894 | objlen = dvmHeapSourceChunkSize(obj); |
| 895 | memset(obj, 0xa5, objlen); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 896 | obj->clazz = (ClassObject *)((uintptr_t)clazz ^ 0xffffffff); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 897 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 898 | } |
| Barry Hayes | dde8ab0 | 2009-05-20 12:10:36 -0700 | [diff] [blame] | 899 | // TODO: dvmHeapSourceFreeList has a loop, just like the above |
| 900 | // does. Consider collapsing the two loops to save overhead. |
| Barry Hayes | 04174be | 2010-07-21 11:51:37 -0700 | [diff] [blame] | 901 | dvmHeapSourceFreeList(numPtrs, ptrs); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 902 | |
| 903 | return true; |
| 904 | } |
| 905 | |
| Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 906 | /* Returns true if the given object is unmarked. Ignores the low bits |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 907 | * of the pointer because the intern table may set them. |
| 908 | */ |
| 909 | static int isUnmarkedObject(void *object) |
| 910 | { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 911 | return !isMarked((void *)((uintptr_t)object & ~(HB_OBJECT_ALIGNMENT-1)), |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 912 | &gDvm.gcHeap->markContext); |
| 913 | } |
| 914 | |
| 915 | /* Walk through the list of objects that haven't been |
| 916 | * marked and free them. |
| 917 | */ |
| 918 | void |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 919 | dvmHeapSweepUnmarkedObjects(GcMode mode, int *numFreed, size_t *sizeFreed) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 920 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 921 | HeapBitmap markBits[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| Carl Shapiro | d77f7fd | 2010-04-05 19:23:31 -0700 | [diff] [blame] | 922 | HeapBitmap liveBits[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 923 | size_t origObjectsAllocated; |
| 924 | size_t origBytesAllocated; |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 925 | size_t numBitmaps, numSweepBitmaps; |
| Barry Hayes | e168ebd | 2010-05-07 09:19:46 -0700 | [diff] [blame] | 926 | size_t i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 927 | |
| 928 | /* All reachable objects have been marked. |
| 929 | * Detach any unreachable interned strings before |
| 930 | * we sweep. |
| 931 | */ |
| 932 | dvmGcDetachDeadInternedStrings(isUnmarkedObject); |
| 933 | |
| 934 | /* Free any known objects that are not marked. |
| 935 | */ |
| 936 | origObjectsAllocated = dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0); |
| 937 | origBytesAllocated = dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0); |
| 938 | |
| Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 939 | dvmSweepMonitorList(&gDvm.monitorList, isUnmarkedObject); |
| 940 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 941 | numBitmaps = dvmHeapSourceGetNumHeaps(); |
| Carl Shapiro | d77f7fd | 2010-04-05 19:23:31 -0700 | [diff] [blame] | 942 | dvmHeapSourceGetObjectBitmaps(liveBits, markBits, numBitmaps); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 943 | if (mode == GC_PARTIAL) { |
| 944 | numSweepBitmaps = 1; |
| Carl Shapiro | d77f7fd | 2010-04-05 19:23:31 -0700 | [diff] [blame] | 945 | assert((uintptr_t)gDvm.gcHeap->markContext.immuneLimit == liveBits[0].base); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 946 | } else { |
| 947 | numSweepBitmaps = numBitmaps; |
| 948 | } |
| Barry Hayes | e168ebd | 2010-05-07 09:19:46 -0700 | [diff] [blame] | 949 | for (i = 0; i < numSweepBitmaps; i++) { |
| 950 | dvmHeapBitmapXorWalk(&markBits[i], &liveBits[i], |
| 951 | sweepBitmapCallback, NULL); |
| 952 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 953 | |
| 954 | *numFreed = origObjectsAllocated - |
| 955 | dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0); |
| 956 | *sizeFreed = origBytesAllocated - |
| 957 | dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0); |
| 958 | |
| 959 | #ifdef WITH_PROFILER |
| 960 | if (gDvm.allocProf.enabled) { |
| 961 | gDvm.allocProf.freeCount += *numFreed; |
| 962 | gDvm.allocProf.freeSize += *sizeFreed; |
| 963 | } |
| 964 | #endif |
| 965 | } |