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