| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 <errno.h> |
| 18 | #include <limits.h> |
| 19 | #include <sys/mman.h> |
| 20 | |
| 21 | #include "Dalvik.h" |
| 22 | #include "alloc/Heap.h" |
| 23 | #include "alloc/HeapBitmap.h" |
| 24 | #include "alloc/HeapInternal.h" |
| 25 | #include "alloc/HeapSource.h" |
| 26 | #include "alloc/Verify.h" |
| 27 | #include "alloc/clz.h" |
| 28 | |
| 29 | /* |
| 30 | * A "mostly copying", generational, garbage collector. |
| 31 | * |
| 32 | * TODO: we allocate our own contiguous tract of page frames to back |
| 33 | * object allocations. To cooperate with other heaps active in the |
| 34 | * virtual machine we need to move the responsibility of allocating |
| 35 | * pages someplace outside of this code. |
| 36 | * |
| 37 | * The other major data structures that maintain the state of the heap |
| 38 | * are the block space table and the block queue. |
| 39 | * |
| 40 | * The block space table records the state of a block. We must track |
| 41 | * whether a block is: |
| 42 | * |
| 43 | * - Free or allocated in some space. |
| 44 | * |
| 45 | * - If the block holds part of a large object allocation, whether the |
| 46 | * block is the initial or a continued block of the allocation. |
| 47 | * |
| 48 | * - Whether the block is pinned, that is to say whether at least one |
| 49 | * object in the block must remain stationary. Only needed during a |
| 50 | * GC. |
| 51 | * |
| 52 | * - Which space the object belongs to. At present this means |
| 53 | * from-space or to-space. |
| 54 | * |
| 55 | * The block queue is used during garbage collection. Unlike Cheney's |
| 56 | * algorithm, from-space and to-space are not contiguous. Therefore, |
| 57 | * one cannot maintain the state of the copy with just two pointers. |
| 58 | * The block queue exists to thread lists of blocks from the various |
| 59 | * spaces together. |
| 60 | * |
| 61 | * Additionally, we record the free space frontier of the heap, as |
| 62 | * well as the address of the first object within a block, which is |
| 63 | * required to copy objects following a large object (not currently |
| 64 | * implemented). This is stored in the heap source structure. This |
| 65 | * should be moved elsewhere to support in-line allocations from Java |
| 66 | * threads. |
| 67 | * |
| 68 | * Allocation requests are satisfied by reserving storage from one or |
| 69 | * more contiguous blocks. Objects that are small enough to fit |
| 70 | * inside a block are packed together within a block. Objects that |
| 71 | * are larger than a block are allocated from contiguous sequences of |
| 72 | * blocks. When half the available blocks are filled, a garbage |
| 73 | * collection occurs. We "flip" spaces (exchange from- and to-space), |
| 74 | * copy live objects into to space, and perform pointer adjustment. |
| 75 | * |
| 76 | * Copying is made more complicated by the requirement that some |
| 77 | * objects must not be moved. This property is known as "pinning". |
| 78 | * These objects must be dealt with specially. We use Bartlett's |
| 79 | * scheme; blocks containing such objects are grayed (promoted) at the |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 80 | * start of a garbage collection. By virtue of this trick, tracing |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 81 | * from the roots proceeds as usual but all objects on those pages are |
| 82 | * considered promoted and therefore not moved. |
| 83 | * |
| 84 | * TODO: there is sufficient information within the garbage collector |
| 85 | * to implement Attardi's scheme for evacuating unpinned objects from |
| 86 | * a page that is otherwise pinned. This would eliminate false |
| 87 | * retention caused by the large pinning granularity. |
| 88 | * |
| 89 | * We need a scheme for medium and large objects. Ignore that for |
| 90 | * now, we can return to this later. |
| 91 | * |
| 92 | * Eventually we need to worry about promoting objects out of the |
| 93 | * copy-collected heap (tenuring) into a less volatile space. Copying |
| 94 | * may not always be the best policy for such spaces. We should |
| 95 | * consider a variant of mark, sweep, compact. |
| 96 | * |
| 97 | * The block scheme allows us to use VM page faults to maintain a |
| 98 | * write barrier. Consider having a special leaf state for a page. |
| 99 | * |
| 100 | * Bibliography: |
| 101 | * |
| 102 | * C. J. Cheney. 1970. A non-recursive list compacting |
| 103 | * algorithm. CACM. 13-11 pp677--678. |
| 104 | * |
| 105 | * Joel F. Bartlett. 1988. Compacting Garbage Collection with |
| 106 | * Ambiguous Roots. Digital Equipment Corporation. |
| 107 | * |
| 108 | * Joel F. Bartlett. 1989. Mostly-Copying Garbage Collection Picks Up |
| 109 | * Generations and C++. Digital Equipment Corporation. |
| 110 | * |
| 111 | * G. May Yip. 1991. Incremental, Generational Mostly-Copying Garbage |
| 112 | * Collection in Uncooperative Environments. Digital Equipment |
| 113 | * Corporation. |
| 114 | * |
| 115 | * Giuseppe Attardi, Tito Flagella. 1994. A Customisable Memory |
| 116 | * Management Framework. TR-94-010 |
| 117 | * |
| 118 | * Giuseppe Attardi, Tito Flagella, Pietro Iglio. 1998. A customisable |
| 119 | * memory management framework for C++. Software -- Practice and |
| 120 | * Experience. 28(11), 1143-1183. |
| 121 | * |
| 122 | */ |
| 123 | |
| 124 | #define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0])) |
| 125 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 126 | #if 0 |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 127 | #define LOG_ALLOC LOGI |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 128 | #define LOG_PIN LOGI |
| 129 | #define LOG_PROM LOGI |
| 130 | #define LOG_REF LOGI |
| 131 | #define LOG_SCAV LOGI |
| 132 | #define LOG_TRAN LOGI |
| 133 | #define LOG_VER LOGI |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 134 | #else |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 135 | #define LOG_ALLOC(...) ((void)0) |
| 136 | #define LOG_PIN(...) ((void)0) |
| 137 | #define LOG_PROM(...) ((void)0) |
| 138 | #define LOG_REF(...) ((void)0) |
| 139 | #define LOG_SCAV(...) ((void)0) |
| 140 | #define LOG_TRAN(...) ((void)0) |
| 141 | #define LOG_VER(...) ((void)0) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 142 | #endif |
| 143 | |
| 144 | static void enqueueBlock(HeapSource *heapSource, size_t block); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 145 | static void scavengeReference(Object **obj); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 146 | static bool toSpaceContains(const void *addr); |
| 147 | static bool fromSpaceContains(const void *addr); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 148 | static size_t sumHeapBitmap(const HeapBitmap *bitmap); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 149 | static size_t objectSize(const Object *obj); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 150 | static void scavengeDataObject(Object *obj); |
| 151 | static void scavengeBlockQueue(void); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * We use 512-byte blocks. |
| 155 | */ |
| 156 | enum { BLOCK_SHIFT = 9 }; |
| 157 | enum { BLOCK_SIZE = 1 << BLOCK_SHIFT }; |
| 158 | |
| 159 | /* |
| 160 | * Space identifiers, stored into the blockSpace array. |
| 161 | */ |
| 162 | enum { |
| 163 | BLOCK_FREE = 0, |
| 164 | BLOCK_FROM_SPACE = 1, |
| 165 | BLOCK_TO_SPACE = 2, |
| 166 | BLOCK_CONTINUED = 7 |
| 167 | }; |
| 168 | |
| 169 | /* |
| 170 | * Alignment for all allocations, in bytes. |
| 171 | */ |
| 172 | enum { ALLOC_ALIGNMENT = 8 }; |
| 173 | |
| 174 | /* |
| 175 | * Sentinel value for the queue end. |
| 176 | */ |
| 177 | #define QUEUE_TAIL (~(size_t)0) |
| 178 | |
| 179 | struct HeapSource { |
| 180 | |
| 181 | /* The base address of backing store. */ |
| 182 | u1 *blockBase; |
| 183 | |
| 184 | /* Total number of blocks available for allocation. */ |
| 185 | size_t totalBlocks; |
| 186 | size_t allocBlocks; |
| 187 | |
| 188 | /* |
| 189 | * The scavenger work queue. Implemented as an array of index |
| 190 | * values into the queue. |
| 191 | */ |
| 192 | size_t *blockQueue; |
| 193 | |
| 194 | /* |
| 195 | * Base and limit blocks. Basically the shifted start address of |
| 196 | * the block. We convert blocks to a relative number when |
| 197 | * indexing in the block queue. TODO: make the block queue base |
| 198 | * relative rather than the index into the block queue. |
| 199 | */ |
| 200 | size_t baseBlock, limitBlock; |
| 201 | |
| 202 | size_t queueHead; |
| 203 | size_t queueTail; |
| 204 | size_t queueSize; |
| 205 | |
| 206 | /* The space of the current block 0 (free), 1 or 2. */ |
| 207 | char *blockSpace; |
| 208 | |
| 209 | /* Start of free space in the current block. */ |
| 210 | u1 *allocPtr; |
| 211 | /* Exclusive limit of free space in the current block. */ |
| 212 | u1 *allocLimit; |
| 213 | |
| 214 | HeapBitmap allocBits; |
| 215 | |
| 216 | /* |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 217 | * The starting size of the heap. This value is the same as the |
| 218 | * value provided to the -Xms flag. |
| 219 | */ |
| 220 | size_t minimumSize; |
| 221 | |
| 222 | /* |
| 223 | * The maximum size of the heap. This value is the same as the |
| 224 | * -Xmx flag. |
| 225 | */ |
| 226 | size_t maximumSize; |
| 227 | |
| 228 | /* |
| 229 | * The current, committed size of the heap. At present, this is |
| 230 | * equivalent to the maximumSize. |
| 231 | */ |
| 232 | size_t currentSize; |
| 233 | |
| 234 | size_t bytesAllocated; |
| 235 | }; |
| 236 | |
| 237 | static unsigned long alignDown(unsigned long x, unsigned long n) |
| 238 | { |
| 239 | return x & -n; |
| 240 | } |
| 241 | |
| 242 | static unsigned long alignUp(unsigned long x, unsigned long n) |
| 243 | { |
| 244 | return alignDown(x + (n - 1), n); |
| 245 | } |
| 246 | |
| 247 | static void describeBlocks(const HeapSource *heapSource) |
| 248 | { |
| 249 | size_t i; |
| 250 | |
| 251 | for (i = 0; i < heapSource->totalBlocks; ++i) { |
| 252 | if ((i % 32) == 0) putchar('\n'); |
| 253 | printf("%d ", heapSource->blockSpace[i]); |
| 254 | } |
| 255 | putchar('\n'); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Virtual memory interface. |
| 260 | */ |
| 261 | |
| 262 | static void *virtualAlloc(size_t length) |
| 263 | { |
| 264 | void *addr; |
| 265 | int flags, prot; |
| 266 | |
| 267 | flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 268 | prot = PROT_READ | PROT_WRITE; |
| 269 | addr = mmap(NULL, length, prot, flags, -1, 0); |
| 270 | if (addr == MAP_FAILED) { |
| 271 | LOGE_HEAP("mmap: %s", strerror(errno)); |
| 272 | addr = NULL; |
| 273 | } |
| 274 | return addr; |
| 275 | } |
| 276 | |
| 277 | static void virtualFree(void *addr, size_t length) |
| 278 | { |
| 279 | int res; |
| 280 | |
| 281 | assert(addr != NULL); |
| 282 | assert((uintptr_t)addr % SYSTEM_PAGE_SIZE == 0); |
| 283 | res = munmap(addr, length); |
| 284 | if (res == -1) { |
| 285 | LOGE_HEAP("munmap: %s", strerror(errno)); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | static int isValidAddress(const HeapSource *heapSource, const u1 *addr) |
| 290 | { |
| 291 | size_t block; |
| 292 | |
| 293 | block = (uintptr_t)addr >> BLOCK_SHIFT; |
| 294 | return heapSource->baseBlock <= block && |
| 295 | heapSource->limitBlock > block; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Iterate over the block map looking for a contiguous run of free |
| 300 | * blocks. |
| 301 | */ |
| 302 | static void *allocateBlocks(HeapSource *heapSource, size_t blocks) |
| 303 | { |
| 304 | void *addr; |
| 305 | size_t allocBlocks, totalBlocks; |
| 306 | size_t i, j; |
| 307 | |
| 308 | allocBlocks = heapSource->allocBlocks; |
| 309 | totalBlocks = heapSource->totalBlocks; |
| 310 | /* Check underflow. */ |
| 311 | assert(blocks != 0); |
| 312 | /* Check overflow. */ |
| 313 | if (allocBlocks + blocks > totalBlocks / 2) { |
| 314 | return NULL; |
| 315 | } |
| 316 | /* Scan block map. */ |
| 317 | for (i = 0; i < totalBlocks; ++i) { |
| 318 | /* Check fit. */ |
| 319 | for (j = 0; j < blocks; ++j) { /* runs over totalBlocks */ |
| 320 | if (heapSource->blockSpace[i+j] != BLOCK_FREE) { |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | /* No fit? */ |
| 325 | if (j != blocks) { |
| 326 | i += j; |
| 327 | continue; |
| 328 | } |
| 329 | /* Fit, allocate. */ |
| 330 | heapSource->blockSpace[i] = BLOCK_TO_SPACE; /* why to-space? */ |
| 331 | for (j = 1; j < blocks; ++j) { |
| 332 | heapSource->blockSpace[i+j] = BLOCK_CONTINUED; |
| 333 | } |
| 334 | heapSource->allocBlocks += blocks; |
| 335 | addr = &heapSource->blockBase[i*BLOCK_SIZE]; |
| 336 | memset(addr, 0, blocks*BLOCK_SIZE); |
| 337 | /* Collecting? */ |
| 338 | if (heapSource->queueHead != QUEUE_TAIL) { |
| 339 | LOG_ALLOC("allocateBlocks allocBlocks=%zu,block#=%zu", heapSource->allocBlocks, i); |
| 340 | /* |
| 341 | * This allocated was on behalf of the transporter when it |
| 342 | * shaded a white object gray. We enqueue the block so |
| 343 | * the scavenger can further shade the gray objects black. |
| 344 | */ |
| 345 | enqueueBlock(heapSource, i); |
| 346 | } |
| 347 | |
| 348 | return addr; |
| 349 | } |
| 350 | /* Insufficient space, fail. */ |
| 351 | LOGE("Insufficient space, %zu blocks, %zu blocks allocated and %zu bytes allocated", |
| 352 | heapSource->totalBlocks, |
| 353 | heapSource->allocBlocks, |
| 354 | heapSource->bytesAllocated); |
| 355 | return NULL; |
| 356 | } |
| 357 | |
| 358 | /* Converts an absolute address to a relative block number. */ |
| 359 | static size_t addressToBlock(const HeapSource *heapSource, const void *addr) |
| 360 | { |
| 361 | assert(heapSource != NULL); |
| 362 | assert(isValidAddress(heapSource, addr)); |
| 363 | return (((uintptr_t)addr) >> BLOCK_SHIFT) - heapSource->baseBlock; |
| 364 | } |
| 365 | |
| 366 | /* Converts a relative block number to an absolute address. */ |
| 367 | static u1 *blockToAddress(const HeapSource *heapSource, size_t block) |
| 368 | { |
| 369 | u1 *addr; |
| 370 | |
| 371 | addr = (u1 *) (((uintptr_t) heapSource->baseBlock + block) * BLOCK_SIZE); |
| 372 | assert(isValidAddress(heapSource, addr)); |
| 373 | return addr; |
| 374 | } |
| 375 | |
| 376 | static void clearBlock(HeapSource *heapSource, size_t block) |
| 377 | { |
| 378 | u1 *addr; |
| 379 | size_t i; |
| 380 | |
| 381 | assert(heapSource != NULL); |
| 382 | assert(block < heapSource->totalBlocks); |
| 383 | addr = heapSource->blockBase + block*BLOCK_SIZE; |
| 384 | memset(addr, 0xCC, BLOCK_SIZE); |
| 385 | for (i = 0; i < BLOCK_SIZE; i += 8) { |
| 386 | dvmHeapBitmapClearObjectBit(&heapSource->allocBits, addr + i); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static void clearFromSpace(HeapSource *heapSource) |
| 391 | { |
| 392 | size_t i, count; |
| 393 | |
| 394 | assert(heapSource != NULL); |
| 395 | i = count = 0; |
| 396 | while (i < heapSource->totalBlocks) { |
| 397 | if (heapSource->blockSpace[i] != BLOCK_FROM_SPACE) { |
| 398 | ++i; |
| 399 | continue; |
| 400 | } |
| 401 | heapSource->blockSpace[i] = BLOCK_FREE; |
| 402 | clearBlock(heapSource, i); |
| 403 | ++i; |
| 404 | ++count; |
| 405 | while (i < heapSource->totalBlocks && |
| 406 | heapSource->blockSpace[i] == BLOCK_CONTINUED) { |
| 407 | heapSource->blockSpace[i] = BLOCK_FREE; |
| 408 | clearBlock(heapSource, i); |
| 409 | ++i; |
| 410 | ++count; |
| 411 | } |
| 412 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 413 | LOG_SCAV("freed %zu blocks (%zu bytes)", count, count*BLOCK_SIZE); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Appends the given block to the block queue. The block queue is |
| 418 | * processed in-order by the scavenger. |
| 419 | */ |
| 420 | static void enqueueBlock(HeapSource *heapSource, size_t block) |
| 421 | { |
| 422 | assert(heapSource != NULL); |
| 423 | assert(block < heapSource->totalBlocks); |
| 424 | if (heapSource->queueHead != QUEUE_TAIL) { |
| 425 | heapSource->blockQueue[heapSource->queueTail] = block; |
| 426 | } else { |
| 427 | heapSource->queueHead = block; |
| 428 | } |
| 429 | heapSource->blockQueue[block] = QUEUE_TAIL; |
| 430 | heapSource->queueTail = block; |
| 431 | ++heapSource->queueSize; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Grays all objects within the block corresponding to the given |
| 436 | * address. |
| 437 | */ |
| 438 | static void promoteBlockByAddr(HeapSource *heapSource, const void *addr) |
| 439 | { |
| 440 | size_t block; |
| 441 | |
| 442 | block = addressToBlock(heapSource, (const u1 *)addr); |
| 443 | if (heapSource->blockSpace[block] != BLOCK_TO_SPACE) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 444 | // LOG_PROM("promoting block %zu %d @ %p", block, heapSource->blockSpace[block], obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 445 | heapSource->blockSpace[block] = BLOCK_TO_SPACE; |
| 446 | enqueueBlock(heapSource, block); |
| 447 | /* TODO(cshapiro): count continued blocks?*/ |
| 448 | heapSource->allocBlocks += 1; |
| 449 | } else { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 450 | // LOG_PROM("NOT promoting block %zu %d @ %p", block, heapSource->blockSpace[block], obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | GcHeap *dvmHeapSourceStartup(size_t startSize, size_t absoluteMaxSize) |
| 455 | { |
| 456 | GcHeap* gcHeap; |
| 457 | HeapSource *heapSource; |
| 458 | |
| 459 | assert(startSize <= absoluteMaxSize); |
| 460 | |
| 461 | heapSource = malloc(sizeof(*heapSource)); |
| 462 | assert(heapSource != NULL); |
| 463 | memset(heapSource, 0, sizeof(*heapSource)); |
| 464 | |
| 465 | heapSource->minimumSize = alignUp(startSize, BLOCK_SIZE); |
| 466 | heapSource->maximumSize = alignUp(absoluteMaxSize, BLOCK_SIZE); |
| 467 | |
| 468 | heapSource->currentSize = heapSource->maximumSize; |
| 469 | |
| 470 | /* Allocate underlying storage for blocks. */ |
| 471 | heapSource->blockBase = virtualAlloc(heapSource->maximumSize); |
| 472 | assert(heapSource->blockBase != NULL); |
| 473 | heapSource->baseBlock = (uintptr_t) heapSource->blockBase >> BLOCK_SHIFT; |
| 474 | heapSource->limitBlock = ((uintptr_t) heapSource->blockBase + heapSource->maximumSize) >> BLOCK_SHIFT; |
| 475 | |
| 476 | heapSource->allocBlocks = 0; |
| 477 | heapSource->totalBlocks = (heapSource->limitBlock - heapSource->baseBlock); |
| 478 | |
| 479 | assert(heapSource->totalBlocks = heapSource->maximumSize / BLOCK_SIZE); |
| 480 | |
| 481 | { |
| 482 | size_t size = sizeof(heapSource->blockQueue[0]); |
| 483 | heapSource->blockQueue = malloc(heapSource->totalBlocks*size); |
| 484 | assert(heapSource->blockQueue != NULL); |
| 485 | memset(heapSource->blockQueue, 0xCC, heapSource->totalBlocks*size); |
| 486 | heapSource->queueHead = QUEUE_TAIL; |
| 487 | } |
| 488 | |
| 489 | /* Byte indicating space residence or free status of block. */ |
| 490 | { |
| 491 | size_t size = sizeof(heapSource->blockSpace[0]); |
| 492 | heapSource->blockSpace = malloc(heapSource->totalBlocks*size); |
| 493 | assert(heapSource->blockSpace != NULL); |
| 494 | memset(heapSource->blockSpace, 0, heapSource->totalBlocks*size); |
| 495 | } |
| 496 | |
| 497 | dvmHeapBitmapInit(&heapSource->allocBits, |
| 498 | heapSource->blockBase, |
| 499 | heapSource->maximumSize, |
| 500 | "blockBase"); |
| 501 | |
| 502 | /* Initialize allocation pointers. */ |
| 503 | heapSource->allocPtr = allocateBlocks(heapSource, 1); |
| 504 | heapSource->allocLimit = heapSource->allocPtr + BLOCK_SIZE; |
| 505 | |
| 506 | gcHeap = malloc(sizeof(*gcHeap)); |
| 507 | assert(gcHeap != NULL); |
| 508 | memset(gcHeap, 0, sizeof(*gcHeap)); |
| 509 | gcHeap->heapSource = heapSource; |
| 510 | |
| 511 | return gcHeap; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Perform any required heap initializations after forking from the |
| 516 | * zygote process. This is a no-op for the time being. Eventually |
| 517 | * this will demarcate the shared region of the heap. |
| 518 | */ |
| 519 | bool dvmHeapSourceStartupAfterZygote(void) |
| 520 | { |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | bool dvmHeapSourceStartupBeforeFork(void) |
| 525 | { |
| 526 | assert(!"implemented"); |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | void dvmHeapSourceShutdown(GcHeap **gcHeap) |
| 531 | { |
| 532 | if (*gcHeap == NULL || (*gcHeap)->heapSource == NULL) |
| 533 | return; |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 534 | free((*gcHeap)->heapSource->blockQueue); |
| 535 | free((*gcHeap)->heapSource->blockSpace); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 536 | virtualFree((*gcHeap)->heapSource->blockBase, |
| 537 | (*gcHeap)->heapSource->maximumSize); |
| 538 | free((*gcHeap)->heapSource); |
| 539 | (*gcHeap)->heapSource = NULL; |
| 540 | free(*gcHeap); |
| 541 | *gcHeap = NULL; |
| 542 | } |
| 543 | |
| 544 | size_t dvmHeapSourceGetValue(enum HeapSourceValueSpec spec, |
| 545 | size_t perHeapStats[], |
| 546 | size_t arrayLen) |
| 547 | { |
| 548 | HeapSource *heapSource; |
| 549 | size_t value; |
| 550 | |
| 551 | heapSource = gDvm.gcHeap->heapSource; |
| 552 | switch (spec) { |
| 553 | case HS_EXTERNAL_BYTES_ALLOCATED: |
| 554 | value = 0; |
| 555 | break; |
| 556 | case HS_EXTERNAL_LIMIT: |
| 557 | value = 0; |
| 558 | break; |
| 559 | case HS_FOOTPRINT: |
| 560 | value = heapSource->maximumSize; |
| 561 | break; |
| 562 | case HS_ALLOWED_FOOTPRINT: |
| 563 | value = heapSource->maximumSize; |
| 564 | break; |
| 565 | case HS_BYTES_ALLOCATED: |
| 566 | value = heapSource->bytesAllocated; |
| 567 | break; |
| 568 | case HS_OBJECTS_ALLOCATED: |
| 569 | value = sumHeapBitmap(&heapSource->allocBits); |
| 570 | break; |
| 571 | default: |
| 572 | assert(!"implemented"); |
| 573 | value = 0; |
| 574 | } |
| 575 | if (perHeapStats) { |
| 576 | *perHeapStats = value; |
| 577 | } |
| 578 | return value; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Performs a shallow copy of the allocation bitmap into the given |
| 583 | * vector of heap bitmaps. |
| 584 | */ |
| 585 | void dvmHeapSourceGetObjectBitmaps(HeapBitmap objBits[], HeapBitmap markBits[], |
| 586 | size_t numHeaps) |
| 587 | { |
| 588 | assert(!"implemented"); |
| 589 | } |
| 590 | |
| 591 | HeapBitmap *dvmHeapSourceGetLiveBits(void) |
| 592 | { |
| 593 | assert(!"implemented"); |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Allocate the specified number of bytes from the heap. The |
| 599 | * allocation cursor points into a block of free storage. If the |
| 600 | * given allocation fits in the remaining space of the block, we |
| 601 | * advance the cursor and return a pointer to the free storage. If |
| 602 | * the allocation cannot fit in the current block but is smaller than |
| 603 | * a block we request a new block and allocate from it instead. If |
| 604 | * the allocation is larger than a block we must allocate from a span |
| 605 | * of contiguous blocks. |
| 606 | */ |
| 607 | void *dvmHeapSourceAlloc(size_t length) |
| 608 | { |
| 609 | HeapSource *heapSource; |
| 610 | unsigned char *addr; |
| 611 | size_t aligned, available, blocks; |
| 612 | |
| 613 | heapSource = gDvm.gcHeap->heapSource; |
| 614 | assert(heapSource != NULL); |
| 615 | assert(heapSource->allocPtr != NULL); |
| 616 | assert(heapSource->allocLimit != NULL); |
| 617 | |
| 618 | aligned = alignUp(length, ALLOC_ALIGNMENT); |
| 619 | available = heapSource->allocLimit - heapSource->allocPtr; |
| 620 | |
| 621 | /* Try allocating inside the current block. */ |
| 622 | if (aligned <= available) { |
| 623 | addr = heapSource->allocPtr; |
| 624 | heapSource->allocPtr += aligned; |
| 625 | heapSource->bytesAllocated += aligned; |
| 626 | dvmHeapBitmapSetObjectBit(&heapSource->allocBits, addr); |
| 627 | return addr; |
| 628 | } |
| 629 | |
| 630 | /* Try allocating in a new block. */ |
| 631 | if (aligned <= BLOCK_SIZE) { |
| 632 | addr = allocateBlocks(heapSource, 1); |
| 633 | if (addr != NULL) { |
| 634 | heapSource->allocLimit = addr + BLOCK_SIZE; |
| 635 | heapSource->allocPtr = addr + aligned; |
| 636 | heapSource->bytesAllocated += aligned; |
| 637 | dvmHeapBitmapSetObjectBit(&heapSource->allocBits, addr); |
| 638 | /* TODO(cshapiro): pad out the current block. */ |
| 639 | } |
| 640 | return addr; |
| 641 | } |
| 642 | |
| 643 | /* Try allocating in a span of blocks. */ |
| 644 | blocks = alignUp(aligned, BLOCK_SIZE) / BLOCK_SIZE; |
| 645 | |
| 646 | addr = allocateBlocks(heapSource, blocks); |
| 647 | /* Propagate failure upward. */ |
| 648 | if (addr != NULL) { |
| 649 | heapSource->bytesAllocated += aligned; |
| 650 | dvmHeapBitmapSetObjectBit(&heapSource->allocBits, addr); |
| 651 | /* TODO(cshapiro): pad out free space in the last block. */ |
| 652 | } |
| 653 | return addr; |
| 654 | } |
| 655 | |
| 656 | void *dvmHeapSourceAllocAndGrow(size_t size) |
| 657 | { |
| 658 | return dvmHeapSourceAlloc(size); |
| 659 | } |
| 660 | |
| 661 | /* TODO: refactor along with dvmHeapSourceAlloc */ |
| 662 | void *allocateGray(size_t size) |
| 663 | { |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 664 | HeapSource *heapSource; |
| 665 | void *addr; |
| 666 | size_t block; |
| 667 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 668 | /* TODO: add a check that we are in a GC. */ |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 669 | heapSource = gDvm.gcHeap->heapSource; |
| 670 | addr = dvmHeapSourceAlloc(size); |
| Carl Shapiro | 703a2f3 | 2010-05-12 23:11:37 -0700 | [diff] [blame] | 671 | assert(addr != NULL); |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 672 | block = addressToBlock(heapSource, (const u1 *)addr); |
| 673 | if (heapSource->queueHead == QUEUE_TAIL) { |
| 674 | /* |
| 675 | * Forcibly append the underlying block to the queue. This |
| 676 | * condition occurs when referents are transported following |
| 677 | * the initial trace. |
| 678 | */ |
| 679 | enqueueBlock(heapSource, block); |
| 680 | LOG_PROM("forced promoting block %zu %d @ %p", block, heapSource->blockSpace[block], addr); |
| 681 | } |
| 682 | return addr; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Returns true if the given address is within the heap and points to |
| 687 | * the header of a live object. |
| 688 | */ |
| 689 | bool dvmHeapSourceContains(const void *addr) |
| 690 | { |
| 691 | HeapSource *heapSource; |
| 692 | HeapBitmap *bitmap; |
| 693 | |
| 694 | heapSource = gDvm.gcHeap->heapSource; |
| 695 | bitmap = &heapSource->allocBits; |
| Carl Shapiro | dc1e4f1 | 2010-05-01 22:27:56 -0700 | [diff] [blame] | 696 | if (!dvmHeapBitmapCoversAddress(bitmap, addr)) { |
| 697 | return false; |
| 698 | } else { |
| 699 | return dvmHeapBitmapIsObjectBitSet(bitmap, addr); |
| 700 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag) |
| 704 | { |
| 705 | assert(!"implemented"); |
| 706 | return false; |
| 707 | } |
| 708 | |
| 709 | size_t dvmHeapSourceChunkSize(const void *ptr) |
| 710 | { |
| 711 | assert(!"implemented"); |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | size_t dvmHeapSourceFootprint(void) |
| 716 | { |
| 717 | assert(!"implemented"); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Returns the "ideal footprint" which appears to be the number of |
| 723 | * bytes currently committed to the heap. This starts out at the |
| 724 | * start size of the heap and grows toward the maximum size. |
| 725 | */ |
| 726 | size_t dvmHeapSourceGetIdealFootprint(void) |
| 727 | { |
| 728 | return gDvm.gcHeap->heapSource->currentSize; |
| 729 | } |
| 730 | |
| 731 | float dvmGetTargetHeapUtilization(void) |
| 732 | { |
| 733 | assert(!"implemented"); |
| 734 | return 0.0f; |
| 735 | } |
| 736 | |
| 737 | void dvmSetTargetHeapUtilization(float newTarget) |
| 738 | { |
| 739 | assert(!"implemented"); |
| 740 | } |
| 741 | |
| 742 | size_t dvmMinimumHeapSize(size_t size, bool set) |
| 743 | { |
| 744 | return gDvm.gcHeap->heapSource->minimumSize; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Expands the size of the heap after a collection. At present we |
| 749 | * commit the pages for maximum size of the heap so this routine is |
| 750 | * just a no-op. Eventually, we will either allocate or commit pages |
| 751 | * on an as-need basis. |
| 752 | */ |
| 753 | void dvmHeapSourceGrowForUtilization(void) |
| 754 | { |
| 755 | /* do nothing */ |
| 756 | } |
| 757 | |
| 758 | void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen) |
| 759 | { |
| 760 | /* do nothing */ |
| 761 | } |
| 762 | |
| 763 | void dvmHeapSourceWalk(void (*callback)(const void *chunkptr, size_t chunklen, |
| 764 | const void *userptr, size_t userlen, |
| 765 | void *arg), |
| 766 | void *arg) |
| 767 | { |
| 768 | assert(!"implemented"); |
| 769 | } |
| 770 | |
| 771 | size_t dvmHeapSourceGetNumHeaps(void) |
| 772 | { |
| 773 | return 1; |
| 774 | } |
| 775 | |
| 776 | bool dvmTrackExternalAllocation(size_t n) |
| 777 | { |
| Carl Shapiro | 528f381 | 2010-05-18 14:16:26 -0700 | [diff] [blame] | 778 | /* do nothing */ |
| 779 | return true; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void dvmTrackExternalFree(size_t n) |
| 783 | { |
| Carl Shapiro | 528f381 | 2010-05-18 14:16:26 -0700 | [diff] [blame] | 784 | /* do nothing */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | size_t dvmGetExternalBytesAllocated(void) |
| 788 | { |
| 789 | assert(!"implemented"); |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | void dvmHeapSourceFlip(void) |
| 794 | { |
| 795 | HeapSource *heapSource; |
| 796 | size_t i; |
| 797 | |
| 798 | heapSource = gDvm.gcHeap->heapSource; |
| 799 | |
| 800 | /* Reset the block queue. */ |
| 801 | heapSource->allocBlocks = 0; |
| 802 | heapSource->queueSize = 0; |
| 803 | heapSource->queueHead = QUEUE_TAIL; |
| 804 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 805 | /* TODO(cshapiro): pad the current (prev) block. */ |
| 806 | |
| 807 | heapSource->allocPtr = NULL; |
| 808 | heapSource->allocLimit = NULL; |
| 809 | |
| 810 | /* Whiten all allocated blocks. */ |
| 811 | for (i = 0; i < heapSource->totalBlocks; ++i) { |
| 812 | if (heapSource->blockSpace[i] == BLOCK_TO_SPACE) { |
| 813 | heapSource->blockSpace[i] = BLOCK_FROM_SPACE; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | static void room(size_t *alloc, size_t *avail, size_t *total) |
| 819 | { |
| 820 | HeapSource *heapSource; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 821 | |
| 822 | heapSource = gDvm.gcHeap->heapSource; |
| 823 | *total = heapSource->totalBlocks*BLOCK_SIZE; |
| 824 | *alloc = heapSource->allocBlocks*BLOCK_SIZE; |
| 825 | *avail = *total - *alloc; |
| 826 | } |
| 827 | |
| 828 | static bool isSpaceInternal(u1 *addr, int space) |
| 829 | { |
| 830 | HeapSource *heapSource; |
| 831 | u1 *base, *limit; |
| 832 | size_t offset; |
| 833 | char space2; |
| 834 | |
| 835 | heapSource = gDvm.gcHeap->heapSource; |
| 836 | base = heapSource->blockBase; |
| 837 | assert(addr >= base); |
| 838 | limit = heapSource->blockBase + heapSource->maximumSize; |
| 839 | assert(addr < limit); |
| 840 | offset = addr - base; |
| 841 | space2 = heapSource->blockSpace[offset >> BLOCK_SHIFT]; |
| 842 | return space == space2; |
| 843 | } |
| 844 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 845 | static bool fromSpaceContains(const void *addr) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 846 | { |
| 847 | return isSpaceInternal((u1 *)addr, BLOCK_FROM_SPACE); |
| 848 | } |
| 849 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 850 | static bool toSpaceContains(const void *addr) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 851 | { |
| 852 | return isSpaceInternal((u1 *)addr, BLOCK_TO_SPACE); |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Notifies the collector that the object at the given address must |
| 857 | * remain stationary during the current collection. |
| 858 | */ |
| 859 | static void pinObject(const Object *obj) |
| 860 | { |
| 861 | promoteBlockByAddr(gDvm.gcHeap->heapSource, obj); |
| 862 | } |
| 863 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 864 | static size_t sumHeapBitmap(const HeapBitmap *bitmap) |
| 865 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 866 | size_t i, sum; |
| 867 | |
| 868 | sum = 0; |
| 869 | for (i = 0; i < bitmap->bitsLen >> 2; ++i) { |
| 870 | sum += dvmClzImpl(bitmap->bits[i]); |
| 871 | } |
| 872 | return sum; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Miscellaneous functionality. |
| 877 | */ |
| 878 | |
| 879 | static int isForward(const void *addr) |
| 880 | { |
| 881 | return (uintptr_t)addr & 0x1; |
| 882 | } |
| 883 | |
| 884 | static void setForward(const void *toObj, void *fromObj) |
| 885 | { |
| 886 | *(unsigned long *)fromObj = (uintptr_t)toObj | 0x1; |
| 887 | } |
| 888 | |
| 889 | static void* getForward(const void *fromObj) |
| 890 | { |
| 891 | return (void *)((uintptr_t)fromObj & ~0x1); |
| 892 | } |
| 893 | |
| 894 | /* Beware, uses the same encoding as a forwarding pointers! */ |
| 895 | static int isPermanentString(const StringObject *obj) { |
| 896 | return (uintptr_t)obj & 0x1; |
| 897 | } |
| 898 | |
| 899 | static void* getPermanentString(const StringObject *obj) |
| 900 | { |
| 901 | return (void *)((uintptr_t)obj & ~0x1); |
| 902 | } |
| 903 | |
| 904 | |
| 905 | /* |
| 906 | * Scavenging and transporting routines follow. A transporter grays |
| 907 | * an object. A scavenger blackens an object. We define these |
| 908 | * routines for each fundamental object type. Dispatch is performed |
| 909 | * in scavengeObject. |
| 910 | */ |
| 911 | |
| 912 | /* |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 913 | * Class object scavenging. |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 914 | */ |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 915 | static void scavengeClassObject(ClassObject *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 916 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 917 | int i; |
| 918 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 919 | LOG_SCAV("scavengeClassObject(obj=%p)", obj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 920 | assert(obj != NULL); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 921 | assert(obj->obj.clazz != NULL); |
| 922 | assert(obj->obj.clazz->descriptor != NULL); |
| 923 | assert(!strcmp(obj->obj.clazz->descriptor, "Ljava/lang/Class;")); |
| 924 | assert(obj->descriptor != NULL); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 925 | LOG_SCAV("scavengeClassObject: descriptor='%s',vtableCount=%zu", |
| 926 | obj->descriptor, obj->vtableCount); |
| Carl Shapiro | 703a2f3 | 2010-05-12 23:11:37 -0700 | [diff] [blame] | 927 | /* Delegate class object and instance field scavenging. */ |
| 928 | scavengeDataObject((Object *)obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 929 | /* Scavenge the array element class object. */ |
| 930 | if (IS_CLASS_FLAG_SET(obj, CLASS_ISARRAY)) { |
| 931 | scavengeReference((Object **)(void *)&obj->elementClass); |
| 932 | } |
| 933 | /* Scavenge the superclass. */ |
| 934 | scavengeReference((Object **)(void *)&obj->super); |
| 935 | /* Scavenge the class loader. */ |
| 936 | scavengeReference(&obj->classLoader); |
| 937 | /* Scavenge static fields. */ |
| 938 | for (i = 0; i < obj->sfieldCount; ++i) { |
| 939 | char ch = obj->sfields[i].field.signature[0]; |
| 940 | if (ch == '[' || ch == 'L') { |
| 941 | scavengeReference((Object **)(void *)&obj->sfields[i].value.l); |
| 942 | } |
| 943 | } |
| 944 | /* Scavenge interface class objects. */ |
| 945 | for (i = 0; i < obj->interfaceCount; ++i) { |
| 946 | scavengeReference((Object **) &obj->interfaces[i]); |
| 947 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Array object scavenging. |
| 952 | */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 953 | static size_t scavengeArrayObject(ArrayObject *array) |
| 954 | { |
| 955 | size_t i, length; |
| 956 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 957 | LOG_SCAV("scavengeArrayObject(array=%p)", array); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 958 | /* Scavenge the class object. */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 959 | assert(toSpaceContains(array)); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 960 | assert(array != NULL); |
| 961 | assert(array->obj.clazz != NULL); |
| 962 | scavengeReference((Object **) array); |
| 963 | length = dvmArrayObjectSize(array); |
| 964 | /* Scavenge the array contents. */ |
| 965 | if (IS_CLASS_FLAG_SET(array->obj.clazz, CLASS_ISOBJECTARRAY)) { |
| 966 | Object **contents = (Object **)array->contents; |
| 967 | for (i = 0; i < array->length; ++i) { |
| 968 | scavengeReference(&contents[i]); |
| 969 | } |
| 970 | } |
| 971 | return length; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * Reference object scavenging. |
| 976 | */ |
| 977 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 978 | static int getReferenceFlags(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 979 | { |
| 980 | int flags; |
| 981 | |
| 982 | flags = CLASS_ISREFERENCE | |
| 983 | CLASS_ISWEAKREFERENCE | |
| 984 | CLASS_ISPHANTOMREFERENCE; |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 985 | return GET_CLASS_FLAG_GROUP(obj->clazz, flags); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 988 | static int isSoftReference(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 989 | { |
| 990 | return getReferenceFlags(obj) == CLASS_ISREFERENCE; |
| 991 | } |
| 992 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 993 | static int isWeakReference(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 994 | { |
| 995 | return getReferenceFlags(obj) & CLASS_ISWEAKREFERENCE; |
| 996 | } |
| 997 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 998 | static bool isPhantomReference(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 999 | { |
| 1000 | return getReferenceFlags(obj) & CLASS_ISPHANTOMREFERENCE; |
| 1001 | } |
| 1002 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1003 | /* |
| 1004 | * Returns true if the reference was registered with a reference queue |
| 1005 | * but has not yet been appended to it. |
| 1006 | */ |
| 1007 | static bool isReferenceEnqueuable(const Object *ref) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1008 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1009 | Object *queue, *queueNext; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1010 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1011 | queue = dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue); |
| 1012 | queueNext = dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext); |
| 1013 | if (queue == NULL || queueNext != NULL) { |
| 1014 | /* |
| 1015 | * There is no queue, or the reference has already |
| 1016 | * been enqueued. The Reference.enqueue() method |
| 1017 | * will do nothing even if we call it. |
| 1018 | */ |
| 1019 | return false; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1020 | } |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1021 | |
| 1022 | /* |
| 1023 | * We need to call enqueue(), but if we called it from |
| 1024 | * here we'd probably deadlock. Schedule a call. |
| 1025 | */ |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Schedules a reference to be appended to its reference queue. |
| 1031 | */ |
| 1032 | static void enqueueReference(const Object *ref) |
| 1033 | { |
| 1034 | LargeHeapRefTable **table; |
| 1035 | Object *op; |
| 1036 | |
| 1037 | assert(((uintptr_t)ref & 3) == 0); |
| 1038 | assert((WORKER_ENQUEUE & ~3) == 0); |
| 1039 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL); |
| 1040 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL); |
| 1041 | /* |
| 1042 | * Set the enqueue bit in the bottom of the pointer. Assumes that |
| 1043 | * objects are 8-byte aligned. |
| 1044 | * |
| 1045 | * Note that we are adding the *Reference* (which is by definition |
| 1046 | * already black at this point) to this list; we're not adding the |
| 1047 | * referent (which has already been cleared). |
| 1048 | */ |
| 1049 | table = &gDvm.gcHeap->referenceOperations; |
| 1050 | op = (Object *)((uintptr_t)ref | WORKER_ENQUEUE); |
| 1051 | if (!dvmHeapAddRefToLargeTable(table, op)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1052 | LOGE("no room for any more reference operations"); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1053 | dvmAbort(); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /* |
| 1058 | * Sets the referent field of a reference object to NULL. |
| 1059 | */ |
| 1060 | static void clearReference(Object *obj) |
| 1061 | { |
| 1062 | dvmSetFieldObject(obj, gDvm.offJavaLangRefReference_referent, NULL); |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Clears reference objects with white referents. |
| 1067 | */ |
| 1068 | void clearWhiteReferences(Object **list) |
| 1069 | { |
| 1070 | size_t referentOffset, vmDataOffset; |
| 1071 | bool doSignal; |
| 1072 | |
| 1073 | vmDataOffset = gDvm.offJavaLangRefReference_vmData; |
| 1074 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 1075 | doSignal = false; |
| 1076 | while (*list != NULL) { |
| 1077 | Object *ref = *list; |
| 1078 | JValue *field = dvmFieldPtr(ref, referentOffset); |
| 1079 | Object *referent = field->l; |
| 1080 | *list = dvmGetFieldObject(ref, vmDataOffset); |
| 1081 | assert(referent != NULL); |
| 1082 | if (isForward(referent->clazz)) { |
| 1083 | field->l = referent = getForward(referent->clazz); |
| 1084 | continue; |
| 1085 | } |
| 1086 | if (fromSpaceContains(referent)) { |
| 1087 | /* Referent is white, clear it. */ |
| 1088 | clearReference(ref); |
| 1089 | if (isReferenceEnqueuable(ref)) { |
| 1090 | enqueueReference(ref); |
| 1091 | doSignal = true; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | /* |
| 1096 | * If we cleared a reference with a reference queue we must notify |
| 1097 | * the heap worker to append the reference. |
| 1098 | */ |
| 1099 | if (doSignal) { |
| 1100 | dvmSignalHeapWorker(false); |
| 1101 | } |
| 1102 | assert(*list == NULL); |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * Blackens referents subject to the soft reference preservation |
| 1107 | * policy. |
| 1108 | */ |
| 1109 | void preserveSoftReferences(Object **list) |
| 1110 | { |
| 1111 | Object *ref; |
| 1112 | Object *prev, *next; |
| 1113 | size_t referentOffset, vmDataOffset; |
| 1114 | unsigned counter; |
| 1115 | bool white; |
| 1116 | |
| 1117 | vmDataOffset = gDvm.offJavaLangRefReference_vmData; |
| 1118 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 1119 | counter = 0; |
| 1120 | prev = next = NULL; |
| 1121 | ref = *list; |
| 1122 | while (ref != NULL) { |
| 1123 | JValue *field = dvmFieldPtr(ref, referentOffset); |
| 1124 | Object *referent = field->l; |
| 1125 | next = dvmGetFieldObject(ref, vmDataOffset); |
| 1126 | assert(referent != NULL); |
| 1127 | if (isForward(referent->clazz)) { |
| 1128 | /* Referent is black. */ |
| 1129 | field->l = referent = getForward(referent->clazz); |
| 1130 | white = false; |
| 1131 | } else { |
| 1132 | white = fromSpaceContains(referent); |
| 1133 | } |
| 1134 | if (!white && ((++counter) & 1)) { |
| 1135 | /* Referent is white and biased toward saving, gray it. */ |
| 1136 | scavengeReference((Object **)(void *)&field->l); |
| 1137 | white = true; |
| 1138 | } |
| 1139 | if (white) { |
| 1140 | /* Referent is black, unlink it. */ |
| 1141 | if (prev != NULL) { |
| 1142 | dvmSetFieldObject(ref, vmDataOffset, NULL); |
| 1143 | dvmSetFieldObject(prev, vmDataOffset, next); |
| 1144 | } |
| 1145 | } else { |
| 1146 | /* Referent is white, skip over it. */ |
| 1147 | prev = ref; |
| 1148 | } |
| 1149 | ref = next; |
| 1150 | } |
| 1151 | /* |
| 1152 | * Restart the trace with the newly gray references added to the |
| 1153 | * root set. |
| 1154 | */ |
| 1155 | scavengeBlockQueue(); |
| 1156 | } |
| 1157 | |
| 1158 | void processFinalizableReferences(void) |
| 1159 | { |
| 1160 | HeapRefTable newPendingRefs; |
| 1161 | LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs; |
| 1162 | Object **ref; |
| 1163 | Object **lastRef; |
| 1164 | size_t totalPendCount; |
| 1165 | |
| 1166 | /* |
| 1167 | * All strongly, reachable objects are black. |
| 1168 | * Any white finalizable objects need to be finalized. |
| 1169 | */ |
| 1170 | |
| 1171 | /* Create a table that the new pending refs will |
| 1172 | * be added to. |
| 1173 | */ |
| 1174 | if (!dvmHeapInitHeapRefTable(&newPendingRefs, 128)) { |
| 1175 | //TODO: mark all finalizable refs and hope that |
| 1176 | // we can schedule them next time. Watch out, |
| 1177 | // because we may be expecting to free up space |
| 1178 | // by calling finalizers. |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1179 | LOG_REF("no room for pending finalizations\n"); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1180 | dvmAbort(); |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Walk through finalizableRefs and move any white references to |
| 1185 | * the list of new pending refs. |
| 1186 | */ |
| 1187 | totalPendCount = 0; |
| 1188 | while (finRefs != NULL) { |
| 1189 | Object **gapRef; |
| 1190 | size_t newPendCount = 0; |
| 1191 | |
| 1192 | gapRef = ref = finRefs->refs.table; |
| 1193 | lastRef = finRefs->refs.nextEntry; |
| 1194 | while (ref < lastRef) { |
| 1195 | if (fromSpaceContains(*ref)) { |
| 1196 | if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) { |
| 1197 | //TODO: add the current table and allocate |
| 1198 | // a new, smaller one. |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1199 | LOG_REF("no room for any more pending finalizations: %zd\n", |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1200 | dvmHeapNumHeapRefTableEntries(&newPendingRefs)); |
| 1201 | dvmAbort(); |
| 1202 | } |
| 1203 | newPendCount++; |
| 1204 | } else { |
| 1205 | /* This ref is black, so will remain on finalizableRefs. |
| 1206 | */ |
| 1207 | if (newPendCount > 0) { |
| 1208 | /* Copy it up to fill the holes. |
| 1209 | */ |
| 1210 | *gapRef++ = *ref; |
| 1211 | } else { |
| 1212 | /* No holes yet; don't bother copying. |
| 1213 | */ |
| 1214 | gapRef++; |
| 1215 | } |
| 1216 | } |
| 1217 | ref++; |
| 1218 | } |
| 1219 | finRefs->refs.nextEntry = gapRef; |
| 1220 | //TODO: if the table is empty when we're done, free it. |
| 1221 | totalPendCount += newPendCount; |
| 1222 | finRefs = finRefs->next; |
| 1223 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1224 | LOG_REF("%zd finalizers triggered.\n", totalPendCount); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1225 | if (totalPendCount == 0) { |
| 1226 | /* No objects required finalization. |
| 1227 | * Free the empty temporary table. |
| 1228 | */ |
| 1229 | dvmClearReferenceTable(&newPendingRefs); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | /* Add the new pending refs to the main list. |
| 1234 | */ |
| 1235 | if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs, |
| 1236 | &newPendingRefs)) |
| 1237 | { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1238 | LOG_REF("can't insert new pending finalizations\n"); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1239 | dvmAbort(); |
| 1240 | } |
| 1241 | |
| 1242 | //TODO: try compacting the main list with a memcpy loop |
| 1243 | |
| 1244 | /* Blacken the refs we just moved; we don't want them or their |
| 1245 | * children to get swept yet. |
| 1246 | */ |
| 1247 | ref = newPendingRefs.table; |
| 1248 | lastRef = newPendingRefs.nextEntry; |
| 1249 | assert(ref < lastRef); |
| 1250 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0); |
| 1251 | while (ref < lastRef) { |
| 1252 | scavengeReference(ref); |
| 1253 | ref++; |
| 1254 | } |
| 1255 | HPROF_CLEAR_GC_SCAN_STATE(); |
| 1256 | scavengeBlockQueue(); |
| 1257 | dvmSignalHeapWorker(false); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1260 | /* |
| 1261 | * If a reference points to from-space and has been forwarded, we snap |
| 1262 | * the pointer to its new to-space address. If the reference points |
| 1263 | * to an unforwarded from-space address we must enqueue the reference |
| 1264 | * for later processing. TODO: implement proper reference processing |
| 1265 | * and move the referent scavenging elsewhere. |
| 1266 | */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1267 | static void scavengeReferenceObject(Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1268 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1269 | Object *referent; |
| 1270 | Object **queue; |
| 1271 | size_t referentOffset, vmDataOffset; |
| 1272 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1273 | assert(obj != NULL); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1274 | LOG_SCAV("scavengeReferenceObject(obj=%p),'%s'", obj, obj->clazz->descriptor); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1275 | scavengeDataObject(obj); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1276 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 1277 | referent = dvmGetFieldObject(obj, referentOffset); |
| 1278 | if (referent == NULL || toSpaceContains(referent)) { |
| 1279 | return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1280 | } |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1281 | if (isSoftReference(obj)) { |
| 1282 | queue = &gDvm.gcHeap->softReferences; |
| 1283 | } else if (isWeakReference(obj)) { |
| 1284 | queue = &gDvm.gcHeap->weakReferences; |
| 1285 | } else { |
| 1286 | assert(isPhantomReference(obj)); |
| 1287 | queue = &gDvm.gcHeap->phantomReferences; |
| 1288 | } |
| 1289 | vmDataOffset = gDvm.offJavaLangRefReference_vmData; |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1290 | dvmSetFieldObject(obj, vmDataOffset, *queue); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1291 | *queue = obj; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1292 | LOG_SCAV("scavengeReferenceObject: enqueueing %p", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Data object scavenging. |
| 1297 | */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1298 | static void scavengeDataObject(Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1299 | { |
| 1300 | ClassObject *clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1301 | int i; |
| 1302 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1303 | // LOG_SCAV("scavengeDataObject(obj=%p)", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1304 | assert(obj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1305 | assert(obj->clazz != NULL); |
| 1306 | assert(obj->clazz->objectSize != 0); |
| 1307 | assert(toSpaceContains(obj)); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1308 | /* Scavenge the class object. */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1309 | clazz = obj->clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1310 | scavengeReference((Object **) obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1311 | /* Scavenge instance fields. */ |
| 1312 | if (clazz->refOffsets != CLASS_WALK_SUPER) { |
| 1313 | size_t refOffsets = clazz->refOffsets; |
| 1314 | while (refOffsets != 0) { |
| 1315 | size_t rshift = CLZ(refOffsets); |
| 1316 | size_t offset = CLASS_OFFSET_FROM_CLZ(rshift); |
| 1317 | Object **ref = (Object **)((u1 *)obj + offset); |
| 1318 | scavengeReference(ref); |
| 1319 | refOffsets &= ~(CLASS_HIGH_BIT >> rshift); |
| 1320 | } |
| 1321 | } else { |
| 1322 | for (; clazz != NULL; clazz = clazz->super) { |
| 1323 | InstField *field = clazz->ifields; |
| 1324 | for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { |
| 1325 | size_t offset = field->byteOffset; |
| 1326 | Object **ref = (Object **)((u1 *)obj + offset); |
| 1327 | scavengeReference(ref); |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | static Object *transportObject(const Object *fromObj) |
| 1334 | { |
| 1335 | Object *toObj; |
| 1336 | size_t allocSize, copySize; |
| 1337 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1338 | LOG_TRAN("transportObject(fromObj=%p) allocBlocks=%zu", |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1339 | fromObj, |
| 1340 | gDvm.gcHeap->heapSource->allocBlocks); |
| 1341 | assert(fromObj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1342 | assert(fromSpaceContains(fromObj)); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1343 | allocSize = copySize = objectSize(fromObj); |
| 1344 | if (LW_HASH_STATE(fromObj->lock) != LW_HASH_STATE_UNHASHED) { |
| 1345 | /* |
| 1346 | * The object has been hashed or hashed and moved. We must |
| 1347 | * reserve an additional word for a hash code. |
| 1348 | */ |
| 1349 | allocSize += sizeof(u4); |
| 1350 | } |
| 1351 | if (LW_HASH_STATE(fromObj->lock) == LW_HASH_STATE_HASHED_AND_MOVED) { |
| 1352 | /* |
| 1353 | * The object has its hash code allocated. Ensure the hash |
| 1354 | * code is copied along with the instance data. |
| 1355 | */ |
| 1356 | copySize += sizeof(u4); |
| 1357 | } |
| 1358 | /* TODO(cshapiro): don't copy, re-map large data objects. */ |
| 1359 | assert(copySize <= allocSize); |
| 1360 | toObj = allocateGray(allocSize); |
| 1361 | assert(toObj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1362 | assert(toSpaceContains(toObj)); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1363 | memcpy(toObj, fromObj, copySize); |
| 1364 | if (LW_HASH_STATE(fromObj->lock) == LW_HASH_STATE_HASHED) { |
| 1365 | /* |
| 1366 | * The object has had its hash code exposed. Append it to the |
| 1367 | * instance and set a bit so we know to look for it there. |
| 1368 | */ |
| 1369 | *(u4 *)(((char *)toObj) + copySize) = (u4)fromObj >> 3; |
| 1370 | toObj->lock |= LW_HASH_STATE_HASHED_AND_MOVED << LW_HASH_STATE_SHIFT; |
| 1371 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1372 | LOG_TRAN("transportObject: from %p/%zu to %p/%zu (%zu,%zu) %s", |
| 1373 | fromObj, addressToBlock(gDvm.gcHeap->heapSource,fromObj), |
| 1374 | toObj, addressToBlock(gDvm.gcHeap->heapSource,toObj), |
| 1375 | copySize, allocSize, copySize < allocSize ? "DIFFERENT" : ""); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1376 | return toObj; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * Generic reference scavenging. |
| 1381 | */ |
| 1382 | |
| 1383 | /* |
| 1384 | * Given a reference to an object, the scavenge routine will gray the |
| 1385 | * reference. Any objects pointed to by the scavenger object will be |
| 1386 | * transported to new space and a forwarding pointer will be installed |
| 1387 | * in the header of the object. |
| 1388 | */ |
| 1389 | |
| 1390 | /* |
| 1391 | * Blacken the given pointer. If the pointer is in from space, it is |
| 1392 | * transported to new space. If the object has a forwarding pointer |
| 1393 | * installed it has already been transported and the referent is |
| 1394 | * snapped to the new address. |
| 1395 | */ |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1396 | static void scavengeReference(Object **obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1397 | { |
| 1398 | ClassObject *clazz; |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1399 | Object *fromObj, *toObj; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1400 | |
| 1401 | assert(obj); |
| 1402 | |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1403 | if (*obj == NULL) return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1404 | |
| 1405 | assert(dvmIsValidObject(*obj)); |
| 1406 | |
| 1407 | /* The entire block is black. */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1408 | if (toSpaceContains(*obj)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1409 | LOG_SCAV("scavengeReference skipping pinned object @ %p", *obj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1410 | return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1411 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1412 | LOG_SCAV("scavengeReference(*obj=%p)", *obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1413 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1414 | assert(fromSpaceContains(*obj)); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1415 | |
| 1416 | clazz = (*obj)->clazz; |
| 1417 | |
| 1418 | if (isForward(clazz)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1419 | // LOG_SCAV("forwarding %p @ %p to %p", *obj, obj, (void *)((uintptr_t)clazz & ~0x1)); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1420 | *obj = (Object *)getForward(clazz); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1421 | return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1422 | } |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1423 | fromObj = *obj; |
| 1424 | if (clazz == NULL) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1425 | // LOG_SCAV("scavangeReference %p has a NULL class object", fromObj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1426 | assert(!"implemented"); |
| 1427 | toObj = NULL; |
| 1428 | } else if (clazz == gDvm.unlinkedJavaLangClass) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1429 | // LOG_SCAV("scavangeReference %p is an unlinked class object", fromObj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1430 | assert(!"implemented"); |
| 1431 | toObj = NULL; |
| 1432 | } else { |
| 1433 | toObj = transportObject(fromObj); |
| 1434 | } |
| 1435 | setForward(toObj, fromObj); |
| 1436 | *obj = (Object *)toObj; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1439 | /* |
| 1440 | * Generic object scavenging. |
| 1441 | */ |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1442 | static void scavengeObject(Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1443 | { |
| 1444 | ClassObject *clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1445 | |
| 1446 | assert(obj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1447 | assert(obj->clazz != NULL); |
| 1448 | assert(!((uintptr_t)obj->clazz & 0x1)); |
| 1449 | assert(obj->clazz != gDvm.unlinkedJavaLangClass); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1450 | clazz = obj->clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1451 | if (clazz == gDvm.classJavaLangClass) { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1452 | scavengeClassObject((ClassObject *)obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1453 | } else if (IS_CLASS_FLAG_SET(clazz, CLASS_ISARRAY)) { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1454 | scavengeArrayObject((ArrayObject *)obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1455 | } else if (IS_CLASS_FLAG_SET(clazz, CLASS_ISREFERENCE)) { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1456 | scavengeReferenceObject(obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1457 | } else { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1458 | scavengeDataObject(obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1459 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /* |
| 1463 | * External root scavenging routines. |
| 1464 | */ |
| 1465 | |
| 1466 | static void scavengeHashTable(HashTable *table) |
| 1467 | { |
| 1468 | HashEntry *entry; |
| 1469 | void *obj; |
| 1470 | int i; |
| 1471 | |
| 1472 | if (table == NULL) { |
| 1473 | return; |
| 1474 | } |
| 1475 | dvmHashTableLock(table); |
| 1476 | for (i = 0; i < table->tableSize; ++i) { |
| 1477 | entry = &table->pEntries[i]; |
| 1478 | obj = entry->data; |
| 1479 | if (obj == NULL || obj == HASH_TOMBSTONE) { |
| 1480 | continue; |
| 1481 | } |
| 1482 | scavengeReference((Object **)(void *)&entry->data); |
| 1483 | } |
| 1484 | dvmHashTableUnlock(table); |
| 1485 | } |
| 1486 | |
| 1487 | static void pinHashTableEntries(HashTable *table) |
| 1488 | { |
| 1489 | HashEntry *entry; |
| 1490 | void *obj; |
| 1491 | int i; |
| 1492 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1493 | LOG_PIN(">>> pinHashTableEntries(table=%p)", table); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1494 | if (table == NULL) { |
| 1495 | return; |
| 1496 | } |
| 1497 | dvmHashTableLock(table); |
| 1498 | for (i = 0; i < table->tableSize; ++i) { |
| 1499 | entry = &table->pEntries[i]; |
| 1500 | obj = entry->data; |
| 1501 | if (obj == NULL || obj == HASH_TOMBSTONE) { |
| 1502 | continue; |
| 1503 | } |
| 1504 | pinObject(entry->data); |
| 1505 | } |
| 1506 | dvmHashTableUnlock(table); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1507 | LOG_PIN("<<< pinHashTableEntries(table=%p)", table); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | static void pinPrimitiveClasses(void) |
| 1511 | { |
| 1512 | size_t length; |
| 1513 | size_t i; |
| 1514 | |
| 1515 | length = ARRAYSIZE(gDvm.primitiveClass); |
| 1516 | for (i = 0; i < length; i++) { |
| 1517 | if (gDvm.primitiveClass[i] != NULL) { |
| 1518 | pinObject((Object *)gDvm.primitiveClass[i]); |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | * Scavenge interned strings. Permanent interned strings will have |
| 1525 | * been pinned and are therefore ignored. Non-permanent strings that |
| 1526 | * have been forwarded are snapped. All other entries are removed. |
| 1527 | */ |
| 1528 | static void scavengeInternedStrings(void) |
| 1529 | { |
| 1530 | HashTable *table; |
| 1531 | HashEntry *entry; |
| 1532 | Object *obj; |
| 1533 | int i; |
| 1534 | |
| 1535 | table = gDvm.internedStrings; |
| 1536 | if (table == NULL) { |
| 1537 | return; |
| 1538 | } |
| 1539 | dvmHashTableLock(table); |
| 1540 | for (i = 0; i < table->tableSize; ++i) { |
| 1541 | entry = &table->pEntries[i]; |
| 1542 | obj = (Object *)entry->data; |
| 1543 | if (obj == NULL || obj == HASH_TOMBSTONE) { |
| 1544 | continue; |
| 1545 | } else if (!isPermanentString((StringObject *)obj)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1546 | // LOG_SCAV("entry->data=%p", entry->data); |
| 1547 | LOG_SCAV(">>> string obj=%p", entry->data); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1548 | /* TODO(cshapiro): detach white string objects */ |
| 1549 | scavengeReference((Object **)(void *)&entry->data); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1550 | LOG_SCAV("<<< string obj=%p", entry->data); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | dvmHashTableUnlock(table); |
| 1554 | } |
| 1555 | |
| 1556 | static void pinInternedStrings(void) |
| 1557 | { |
| 1558 | HashTable *table; |
| 1559 | HashEntry *entry; |
| 1560 | Object *obj; |
| 1561 | int i; |
| 1562 | |
| 1563 | table = gDvm.internedStrings; |
| 1564 | if (table == NULL) { |
| 1565 | return; |
| 1566 | } |
| 1567 | dvmHashTableLock(table); |
| 1568 | for (i = 0; i < table->tableSize; ++i) { |
| 1569 | entry = &table->pEntries[i]; |
| 1570 | obj = (Object *)entry->data; |
| 1571 | if (obj == NULL || obj == HASH_TOMBSTONE) { |
| 1572 | continue; |
| 1573 | } else if (isPermanentString((StringObject *)obj)) { |
| 1574 | obj = (Object *)getPermanentString((StringObject*)obj); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1575 | LOG_PROM(">>> pin string obj=%p", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1576 | pinObject(obj); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1577 | LOG_PROM("<<< pin string obj=%p", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1578 | } |
| 1579 | } |
| 1580 | dvmHashTableUnlock(table); |
| 1581 | } |
| 1582 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1583 | /* |
| 1584 | * At present, reference tables contain references that must not be |
| 1585 | * moved by the collector. Instead of scavenging each reference in |
| 1586 | * the table we pin each referenced object. |
| 1587 | */ |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1588 | static void pinReferenceTable(const ReferenceTable *table) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1589 | { |
| 1590 | Object **entry; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1591 | |
| 1592 | assert(table != NULL); |
| 1593 | assert(table->table != NULL); |
| 1594 | assert(table->nextEntry != NULL); |
| 1595 | for (entry = table->table; entry < table->nextEntry; ++entry) { |
| 1596 | assert(entry != NULL); |
| 1597 | assert(!isForward(*entry)); |
| 1598 | pinObject(*entry); |
| 1599 | } |
| 1600 | } |
| 1601 | |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1602 | static void scavengeLargeHeapRefTable(LargeHeapRefTable *table, bool stripLowBits) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1603 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1604 | for (; table != NULL; table = table->next) { |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1605 | Object **ref = table->refs.table; |
| 1606 | for (; ref < table->refs.nextEntry; ++ref) { |
| 1607 | if (stripLowBits) { |
| 1608 | Object *obj = (Object *)((uintptr_t)*ref & ~3); |
| 1609 | scavengeReference(&obj); |
| 1610 | *ref = (Object *)((uintptr_t)obj | ((uintptr_t)*ref & 3)); |
| 1611 | } else { |
| 1612 | scavengeReference(ref); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1613 | } |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1618 | /* This code was copied from Thread.c */ |
| 1619 | static void scavengeThreadStack(Thread *thread) |
| 1620 | { |
| 1621 | const u4 *framePtr; |
| 1622 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1623 | bool first = true; |
| 1624 | #endif |
| 1625 | |
| 1626 | framePtr = (const u4 *)thread->curFrame; |
| 1627 | while (framePtr != NULL) { |
| 1628 | const StackSaveArea *saveArea; |
| 1629 | const Method *method; |
| 1630 | |
| 1631 | saveArea = SAVEAREA_FROM_FP(framePtr); |
| 1632 | method = saveArea->method; |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1633 | if (method != NULL && !dvmIsNativeMethod(method)) { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1634 | #ifdef COUNT_PRECISE_METHODS |
| 1635 | /* the GC is running, so no lock required */ |
| 1636 | if (dvmPointerSetAddEntry(gDvm.preciseMethods, method)) |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1637 | LOG_SCAV("PGC: added %s.%s %p\n", |
| 1638 | method->clazz->descriptor, method->name, method); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1639 | #endif |
| 1640 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1641 | /* |
| 1642 | * May also want to enable the memset() in the "invokeMethod" |
| 1643 | * goto target in the portable interpreter. That sets the stack |
| 1644 | * to a pattern that makes referring to uninitialized data |
| 1645 | * very obvious. |
| 1646 | */ |
| 1647 | |
| 1648 | if (first) { |
| 1649 | /* |
| 1650 | * First frame, isn't native, check the "alternate" saved PC |
| 1651 | * as a sanity check. |
| 1652 | * |
| 1653 | * It seems like we could check the second frame if the first |
| 1654 | * is native, since the PCs should be the same. It turns out |
| 1655 | * this doesn't always work. The problem is that we could |
| 1656 | * have calls in the sequence: |
| 1657 | * interp method #2 |
| 1658 | * native method |
| 1659 | * interp method #1 |
| 1660 | * |
| 1661 | * and then GC while in the native method after returning |
| 1662 | * from interp method #2. The currentPc on the stack is |
| 1663 | * for interp method #1, but thread->currentPc2 is still |
| 1664 | * set for the last thing interp method #2 did. |
| 1665 | * |
| 1666 | * This can also happen in normal execution: |
| 1667 | * - sget-object on not-yet-loaded class |
| 1668 | * - class init updates currentPc2 |
| 1669 | * - static field init is handled by parsing annotations; |
| 1670 | * static String init requires creation of a String object, |
| 1671 | * which can cause a GC |
| 1672 | * |
| 1673 | * Essentially, any pattern that involves executing |
| 1674 | * interpreted code and then causes an allocation without |
| 1675 | * executing instructions in the original method will hit |
| 1676 | * this. These are rare enough that the test still has |
| 1677 | * some value. |
| 1678 | */ |
| 1679 | if (saveArea->xtra.currentPc != thread->currentPc2) { |
| 1680 | LOGW("PGC: savedPC(%p) != current PC(%p), %s.%s ins=%p\n", |
| 1681 | saveArea->xtra.currentPc, thread->currentPc2, |
| 1682 | method->clazz->descriptor, method->name, method->insns); |
| 1683 | if (saveArea->xtra.currentPc != NULL) |
| 1684 | LOGE(" pc inst = 0x%04x\n", *saveArea->xtra.currentPc); |
| 1685 | if (thread->currentPc2 != NULL) |
| 1686 | LOGE(" pc2 inst = 0x%04x\n", *thread->currentPc2); |
| 1687 | dvmDumpThread(thread, false); |
| 1688 | } |
| 1689 | } else { |
| 1690 | /* |
| 1691 | * It's unusual, but not impossible, for a non-first frame |
| 1692 | * to be at something other than a method invocation. For |
| 1693 | * example, if we do a new-instance on a nonexistent class, |
| 1694 | * we'll have a lot of class loader activity on the stack |
| 1695 | * above the frame with the "new" operation. Could also |
| 1696 | * happen while we initialize a Throwable when an instruction |
| 1697 | * fails. |
| 1698 | * |
| 1699 | * So there's not much we can do here to verify the PC, |
| 1700 | * except to verify that it's a GC point. |
| 1701 | */ |
| 1702 | } |
| 1703 | assert(saveArea->xtra.currentPc != NULL); |
| 1704 | #endif |
| 1705 | |
| 1706 | const RegisterMap* pMap; |
| 1707 | const u1* regVector; |
| 1708 | int i; |
| 1709 | |
| 1710 | Method* nonConstMethod = (Method*) method; // quiet gcc |
| 1711 | pMap = dvmGetExpandedRegisterMap(nonConstMethod); |
| 1712 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1713 | //LOG_SCAV("PGC: %s.%s\n", method->clazz->descriptor, method->name); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1714 | |
| 1715 | if (pMap != NULL) { |
| 1716 | /* found map, get registers for this address */ |
| 1717 | int addr = saveArea->xtra.currentPc - method->insns; |
| 1718 | regVector = dvmRegisterMapGetLine(pMap, addr); |
| 1719 | /* |
| 1720 | if (regVector == NULL) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1721 | LOG_SCAV("PGC: map but no entry for %s.%s addr=0x%04x\n", |
| 1722 | method->clazz->descriptor, method->name, addr); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1723 | } else { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1724 | LOG_SCAV("PGC: found map for %s.%s 0x%04x (t=%d)\n", |
| 1725 | method->clazz->descriptor, method->name, addr, |
| 1726 | thread->threadId); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1727 | } |
| 1728 | */ |
| 1729 | } else { |
| 1730 | /* |
| 1731 | * No map found. If precise GC is disabled this is |
| 1732 | * expected -- we don't create pointers to the map data even |
| 1733 | * if it's present -- but if it's enabled it means we're |
| 1734 | * unexpectedly falling back on a conservative scan, so it's |
| 1735 | * worth yelling a little. |
| 1736 | */ |
| 1737 | if (gDvm.preciseGc) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1738 | LOG_SCAV("PGC: no map for %s.%s\n", method->clazz->descriptor, method->name); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1739 | } |
| 1740 | regVector = NULL; |
| 1741 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1742 | if (regVector == NULL) { |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1743 | /* |
| 1744 | * There are no roots to scavenge. Skip over the entire frame. |
| 1745 | */ |
| 1746 | framePtr += method->registersSize; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1747 | } else { |
| 1748 | /* |
| 1749 | * Precise scan. v0 is at the lowest address on the |
| 1750 | * interpreted stack, and is the first bit in the register |
| 1751 | * vector, so we can walk through the register map and |
| 1752 | * memory in the same direction. |
| 1753 | * |
| 1754 | * A '1' bit indicates a live reference. |
| 1755 | */ |
| 1756 | u2 bits = 1 << 1; |
| 1757 | for (i = method->registersSize - 1; i >= 0; i--) { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1758 | u4 rval = *framePtr; |
| 1759 | |
| 1760 | bits >>= 1; |
| 1761 | if (bits == 1) { |
| 1762 | /* set bit 9 so we can tell when we're empty */ |
| 1763 | bits = *regVector++ | 0x0100; |
| 1764 | LOGVV("loaded bits: 0x%02x\n", bits & 0xff); |
| 1765 | } |
| 1766 | |
| 1767 | if (rval != 0 && (bits & 0x01) != 0) { |
| 1768 | /* |
| 1769 | * Non-null, register marked as live reference. This |
| 1770 | * should always be a valid object. |
| 1771 | */ |
| 1772 | #if WITH_EXTRA_GC_CHECKS > 0 |
| 1773 | if ((rval & 0x3) != 0 || !dvmIsValidObject((Object*) rval)) { |
| 1774 | /* this is very bad */ |
| 1775 | LOGE("PGC: invalid ref in reg %d: 0x%08x\n", |
| 1776 | method->registersSize-1 - i, rval); |
| 1777 | } else |
| 1778 | #endif |
| 1779 | { |
| 1780 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1781 | // LOG_SCAV("stack reference %u@%p", *framePtr, framePtr); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1782 | /* dvmMarkObjectNonNull((Object *)rval); */ |
| 1783 | scavengeReference((Object **) framePtr); |
| 1784 | } |
| 1785 | } else { |
| 1786 | /* |
| 1787 | * Null or non-reference, do nothing at all. |
| 1788 | */ |
| 1789 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1790 | if (dvmIsValidObject((Object*) rval)) { |
| 1791 | /* this is normal, but we feel chatty */ |
| 1792 | LOGD("PGC: ignoring valid ref in reg %d: 0x%08x\n", |
| 1793 | method->registersSize-1 - i, rval); |
| 1794 | } |
| 1795 | #endif |
| 1796 | } |
| 1797 | ++framePtr; |
| 1798 | } |
| 1799 | dvmReleaseRegisterMapLine(pMap, regVector); |
| 1800 | } |
| 1801 | } |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1802 | /* else this is a break frame and there is nothing to gray, or |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1803 | * this is a native method and the registers are just the "ins", |
| 1804 | * copied from various registers in the caller's set. |
| 1805 | */ |
| 1806 | |
| 1807 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1808 | first = false; |
| 1809 | #endif |
| 1810 | |
| 1811 | /* Don't fall into an infinite loop if things get corrupted. |
| 1812 | */ |
| 1813 | assert((uintptr_t)saveArea->prevFrame > (uintptr_t)framePtr || |
| 1814 | saveArea->prevFrame == NULL); |
| 1815 | framePtr = saveArea->prevFrame; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | static void scavengeThread(Thread *thread) |
| 1820 | { |
| 1821 | assert(thread->status != THREAD_RUNNING || |
| 1822 | thread->isSuspended || |
| 1823 | thread == dvmThreadSelf()); |
| 1824 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1825 | // LOG_SCAV("scavengeThread(thread=%p)", thread); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1826 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1827 | // LOG_SCAV("Scavenging threadObj=%p", thread->threadObj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1828 | scavengeReference(&thread->threadObj); |
| 1829 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1830 | // LOG_SCAV("Scavenging exception=%p", thread->exception); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1831 | scavengeReference(&thread->exception); |
| 1832 | |
| 1833 | scavengeThreadStack(thread); |
| 1834 | } |
| 1835 | |
| 1836 | static void scavengeThreadList(void) |
| 1837 | { |
| 1838 | Thread *thread; |
| 1839 | |
| 1840 | dvmLockThreadList(dvmThreadSelf()); |
| 1841 | thread = gDvm.threadList; |
| 1842 | while (thread) { |
| 1843 | scavengeThread(thread); |
| 1844 | thread = thread->next; |
| 1845 | } |
| 1846 | dvmUnlockThreadList(); |
| 1847 | } |
| 1848 | |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1849 | static void pinThreadStack(const Thread *thread) |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1850 | { |
| 1851 | const u4 *framePtr; |
| 1852 | const StackSaveArea *saveArea; |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1853 | Method *method; |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1854 | const char *shorty; |
| 1855 | Object *obj; |
| 1856 | int i; |
| 1857 | |
| 1858 | saveArea = NULL; |
| 1859 | framePtr = (const u4 *)thread->curFrame; |
| 1860 | for (; framePtr != NULL; framePtr = saveArea->prevFrame) { |
| 1861 | saveArea = SAVEAREA_FROM_FP(framePtr); |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1862 | method = (Method *)saveArea->method; |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1863 | if (method != NULL && dvmIsNativeMethod(method)) { |
| 1864 | /* |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1865 | * This is native method, pin its arguments. |
| 1866 | * |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1867 | * For purposes of graying references, we don't need to do |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1868 | * anything here, because all of the native "ins" were copied |
| 1869 | * from registers in the caller's stack frame and won't be |
| 1870 | * changed (an interpreted method can freely use registers |
| 1871 | * with parameters like any other register, but natives don't |
| 1872 | * work that way). |
| 1873 | * |
| 1874 | * However, we need to ensure that references visible to |
| 1875 | * native methods don't move around. We can do a precise scan |
| 1876 | * of the arguments by examining the method signature. |
| 1877 | */ |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1878 | LOG_PIN("+++ native scan %s.%s\n", |
| 1879 | method->clazz->descriptor, method->name); |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1880 | assert(method->registersSize == method->insSize); |
| 1881 | if (!dvmIsStaticMethod(method)) { |
| 1882 | /* grab the "this" pointer */ |
| 1883 | obj = (Object *)*framePtr++; |
| 1884 | if (obj == NULL) { |
| 1885 | /* |
| 1886 | * This can happen for the "fake" entry frame inserted |
| 1887 | * for threads created outside the VM. There's no actual |
| 1888 | * call so there's no object. If we changed the fake |
| 1889 | * entry method to be declared "static" then this |
| 1890 | * situation should never occur. |
| 1891 | */ |
| 1892 | } else { |
| 1893 | assert(dvmIsValidObject(obj)); |
| 1894 | pinObject(obj); |
| 1895 | } |
| 1896 | } |
| 1897 | shorty = method->shorty+1; // skip return value |
| 1898 | for (i = method->registersSize - 1; i >= 0; i--, framePtr++) { |
| 1899 | switch (*shorty++) { |
| 1900 | case 'L': |
| 1901 | obj = (Object *)*framePtr; |
| 1902 | if (obj != NULL) { |
| 1903 | assert(dvmIsValidObject(obj)); |
| 1904 | pinObject(obj); |
| 1905 | } |
| 1906 | break; |
| 1907 | case 'D': |
| 1908 | case 'J': |
| 1909 | framePtr++; |
| 1910 | break; |
| 1911 | default: |
| 1912 | /* 32-bit non-reference value */ |
| 1913 | obj = (Object *)*framePtr; // debug, remove |
| 1914 | if (dvmIsValidObject(obj)) { // debug, remove |
| 1915 | /* if we see a lot of these, our scan might be off */ |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1916 | LOG_PIN("+++ did NOT pin obj %p\n", obj); |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1917 | } |
| 1918 | break; |
| 1919 | } |
| 1920 | } |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1921 | } else if (method != NULL && !dvmIsNativeMethod(method)) { |
| 1922 | const RegisterMap* pMap = dvmGetExpandedRegisterMap(method); |
| 1923 | const u1* regVector = NULL; |
| 1924 | |
| 1925 | LOGI("conservative : %s.%s\n", method->clazz->descriptor, method->name); |
| 1926 | |
| 1927 | if (pMap != NULL) { |
| 1928 | int addr = saveArea->xtra.currentPc - method->insns; |
| 1929 | regVector = dvmRegisterMapGetLine(pMap, addr); |
| 1930 | } |
| 1931 | if (regVector == NULL) { |
| 1932 | /* |
| 1933 | * No register info for this frame, conservatively pin. |
| 1934 | */ |
| 1935 | for (i = 0; i < method->registersSize; ++i) { |
| 1936 | u4 regValue = framePtr[i]; |
| 1937 | if (regValue != 0 && (regValue & 0x3) == 0 && dvmIsValidObject((Object *)regValue)) { |
| 1938 | pinObject((Object *)regValue); |
| 1939 | } |
| 1940 | } |
| 1941 | } |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1942 | } |
| 1943 | /* |
| 1944 | * Don't fall into an infinite loop if things get corrupted. |
| 1945 | */ |
| 1946 | assert((uintptr_t)saveArea->prevFrame > (uintptr_t)framePtr || |
| 1947 | saveArea->prevFrame == NULL); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | static void pinThread(const Thread *thread) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1952 | { |
| 1953 | assert(thread != NULL); |
| 1954 | assert(thread->status != THREAD_RUNNING || |
| 1955 | thread->isSuspended || |
| 1956 | thread == dvmThreadSelf()); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1957 | LOG_PIN("pinThread(thread=%p)", thread); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1958 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1959 | LOG_PIN("Pin native method arguments"); |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1960 | pinThreadStack(thread); |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1961 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1962 | LOG_PIN("Pin internalLocalRefTable"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1963 | pinReferenceTable(&thread->internalLocalRefTable); |
| 1964 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1965 | LOG_PIN("Pin jniLocalRefTable"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1966 | pinReferenceTable(&thread->jniLocalRefTable); |
| 1967 | |
| 1968 | /* Can the check be pushed into the promote routine? */ |
| 1969 | if (thread->jniMonitorRefTable.table) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1970 | LOG_PIN("Pin jniMonitorRefTable"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1971 | pinReferenceTable(&thread->jniMonitorRefTable); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | static void pinThreadList(void) |
| 1976 | { |
| 1977 | Thread *thread; |
| 1978 | |
| 1979 | dvmLockThreadList(dvmThreadSelf()); |
| 1980 | thread = gDvm.threadList; |
| 1981 | while (thread) { |
| 1982 | pinThread(thread); |
| 1983 | thread = thread->next; |
| 1984 | } |
| 1985 | dvmUnlockThreadList(); |
| 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | * Heap block scavenging. |
| 1990 | */ |
| 1991 | |
| 1992 | /* |
| 1993 | * Scavenge objects in the current block. Scavenging terminates when |
| 1994 | * the pointer reaches the highest address in the block or when a run |
| 1995 | * of zero words that continues to the highest address is reached. |
| 1996 | */ |
| 1997 | static void scavengeBlock(HeapSource *heapSource, size_t block) |
| 1998 | { |
| 1999 | u1 *cursor; |
| 2000 | u1 *end; |
| 2001 | size_t size; |
| 2002 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2003 | LOG_SCAV("scavengeBlock(heapSource=%p,block=%zu)", heapSource, block); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2004 | |
| 2005 | assert(heapSource != NULL); |
| 2006 | assert(block < heapSource->totalBlocks); |
| 2007 | assert(heapSource->blockSpace[block] == BLOCK_TO_SPACE); |
| 2008 | |
| 2009 | cursor = blockToAddress(heapSource, block); |
| 2010 | end = cursor + BLOCK_SIZE; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2011 | LOG_SCAV("scavengeBlock start=%p, end=%p", cursor, end); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2012 | |
| 2013 | /* Parse and scavenge the current block. */ |
| 2014 | size = 0; |
| 2015 | while (cursor < end) { |
| 2016 | u4 word = *(u4 *)cursor; |
| 2017 | if (word != 0) { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2018 | scavengeObject((Object *)cursor); |
| 2019 | size = objectSize((Object *)cursor); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2020 | size = alignUp(size, ALLOC_ALIGNMENT); |
| 2021 | cursor += size; |
| 2022 | } else if (word == 0 && cursor == (u1 *)gDvm.unlinkedJavaLangClass) { |
| 2023 | size = sizeof(ClassObject); |
| 2024 | cursor += size; |
| 2025 | } else { |
| 2026 | /* Check for padding. */ |
| 2027 | while (*(u4 *)cursor == 0) { |
| 2028 | cursor += 4; |
| 2029 | if (cursor == end) break; |
| 2030 | } |
| 2031 | /* Punt if something went wrong. */ |
| 2032 | assert(cursor == end); |
| 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2037 | static size_t objectSize(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2038 | { |
| 2039 | size_t size; |
| 2040 | |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2041 | assert(obj != NULL); |
| 2042 | assert(obj->clazz != NULL); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2043 | if (obj->clazz == gDvm.classJavaLangClass || |
| 2044 | obj->clazz == gDvm.unlinkedJavaLangClass) { |
| 2045 | size = dvmClassObjectSize((ClassObject *)obj); |
| 2046 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
| 2047 | size = dvmArrayObjectSize((ArrayObject *)obj); |
| 2048 | } else { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2049 | assert(obj->clazz->objectSize != 0); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2050 | size = obj->clazz->objectSize; |
| 2051 | } |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2052 | if (LW_HASH_STATE(obj->lock) == LW_HASH_STATE_HASHED_AND_MOVED) { |
| 2053 | size += sizeof(u4); |
| 2054 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2055 | return size; |
| 2056 | } |
| 2057 | |
| 2058 | static void verifyBlock(HeapSource *heapSource, size_t block) |
| 2059 | { |
| 2060 | u1 *cursor; |
| 2061 | u1 *end; |
| 2062 | size_t size; |
| 2063 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2064 | // LOG_VER("verifyBlock(heapSource=%p,block=%zu)", heapSource, block); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2065 | |
| 2066 | assert(heapSource != NULL); |
| 2067 | assert(block < heapSource->totalBlocks); |
| 2068 | assert(heapSource->blockSpace[block] == BLOCK_TO_SPACE); |
| 2069 | |
| 2070 | cursor = blockToAddress(heapSource, block); |
| 2071 | end = cursor + BLOCK_SIZE; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2072 | // LOG_VER("verifyBlock start=%p, end=%p", cursor, end); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2073 | |
| 2074 | /* Parse and scavenge the current block. */ |
| 2075 | size = 0; |
| 2076 | while (cursor < end) { |
| 2077 | u4 word = *(u4 *)cursor; |
| 2078 | if (word != 0) { |
| 2079 | dvmVerifyObject((Object *)cursor); |
| 2080 | size = objectSize((Object *)cursor); |
| 2081 | size = alignUp(size, ALLOC_ALIGNMENT); |
| 2082 | cursor += size; |
| 2083 | } else if (word == 0 && cursor == (u1 *)gDvm.unlinkedJavaLangClass) { |
| 2084 | size = sizeof(ClassObject); |
| 2085 | cursor += size; |
| 2086 | } else { |
| 2087 | /* Check for padding. */ |
| 2088 | while (*(unsigned long *)cursor == 0) { |
| 2089 | cursor += 4; |
| 2090 | if (cursor == end) break; |
| 2091 | } |
| 2092 | /* Punt if something went wrong. */ |
| 2093 | assert(cursor == end); |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | static void describeBlockQueue(const HeapSource *heapSource) |
| 2099 | { |
| 2100 | size_t block, count; |
| 2101 | char space; |
| 2102 | |
| 2103 | block = heapSource->queueHead; |
| 2104 | count = 0; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2105 | LOG_SCAV(">>> describeBlockQueue(heapSource=%p)", heapSource); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2106 | /* Count the number of blocks enqueued. */ |
| 2107 | while (block != QUEUE_TAIL) { |
| 2108 | block = heapSource->blockQueue[block]; |
| 2109 | ++count; |
| 2110 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2111 | LOG_SCAV("blockQueue %zu elements, enqueued %zu", |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2112 | count, heapSource->queueSize); |
| 2113 | block = heapSource->queueHead; |
| 2114 | while (block != QUEUE_TAIL) { |
| 2115 | space = heapSource->blockSpace[block]; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2116 | LOG_SCAV("block=%zu@%p,space=%zu", block, blockToAddress(heapSource,block), space); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2117 | block = heapSource->blockQueue[block]; |
| 2118 | } |
| 2119 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2120 | LOG_SCAV("<<< describeBlockQueue(heapSource=%p)", heapSource); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | * Blackens promoted objects. |
| 2125 | */ |
| 2126 | static void scavengeBlockQueue(void) |
| 2127 | { |
| 2128 | HeapSource *heapSource; |
| 2129 | size_t block; |
| 2130 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2131 | LOG_SCAV(">>> scavengeBlockQueue()"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2132 | heapSource = gDvm.gcHeap->heapSource; |
| 2133 | describeBlockQueue(heapSource); |
| 2134 | while (heapSource->queueHead != QUEUE_TAIL) { |
| 2135 | block = heapSource->queueHead; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2136 | LOG_SCAV("Dequeueing block %zu\n", block); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2137 | scavengeBlock(heapSource, block); |
| 2138 | heapSource->queueHead = heapSource->blockQueue[block]; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2139 | LOG_SCAV("New queue head is %zu\n", heapSource->queueHead); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2140 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2141 | LOG_SCAV("<<< scavengeBlockQueue()"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | /* |
| 2145 | * Scan the block list and verify all blocks that are marked as being |
| 2146 | * in new space. This should be parametrized so we can invoke this |
| 2147 | * routine outside of the context of a collection. |
| 2148 | */ |
| 2149 | static void verifyNewSpace(void) |
| 2150 | { |
| 2151 | HeapSource *heapSource; |
| 2152 | size_t i; |
| 2153 | size_t c0, c1, c2, c7; |
| 2154 | |
| 2155 | c0 = c1 = c2 = c7 = 0; |
| 2156 | heapSource = gDvm.gcHeap->heapSource; |
| 2157 | for (i = 0; i < heapSource->totalBlocks; ++i) { |
| 2158 | switch (heapSource->blockSpace[i]) { |
| 2159 | case BLOCK_FREE: ++c0; break; |
| 2160 | case BLOCK_TO_SPACE: ++c1; break; |
| 2161 | case BLOCK_FROM_SPACE: ++c2; break; |
| 2162 | case BLOCK_CONTINUED: ++c7; break; |
| 2163 | default: assert(!"reached"); |
| 2164 | } |
| 2165 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2166 | LOG_VER("Block Demographics: " |
| 2167 | "Free=%zu,ToSpace=%zu,FromSpace=%zu,Continued=%zu", |
| 2168 | c0, c1, c2, c7); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2169 | for (i = 0; i < heapSource->totalBlocks; ++i) { |
| 2170 | if (heapSource->blockSpace[i] != BLOCK_TO_SPACE) { |
| 2171 | continue; |
| 2172 | } |
| 2173 | verifyBlock(heapSource, i); |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | static void scavengeGlobals(void) |
| 2178 | { |
| 2179 | scavengeReference((Object **)(void *)&gDvm.classJavaLangClass); |
| 2180 | scavengeReference((Object **)(void *)&gDvm.classJavaLangClassArray); |
| 2181 | scavengeReference((Object **)(void *)&gDvm.classJavaLangError); |
| 2182 | scavengeReference((Object **)(void *)&gDvm.classJavaLangObject); |
| 2183 | scavengeReference((Object **)(void *)&gDvm.classJavaLangObjectArray); |
| 2184 | scavengeReference((Object **)(void *)&gDvm.classJavaLangRuntimeException); |
| 2185 | scavengeReference((Object **)(void *)&gDvm.classJavaLangString); |
| 2186 | scavengeReference((Object **)(void *)&gDvm.classJavaLangThread); |
| 2187 | scavengeReference((Object **)(void *)&gDvm.classJavaLangVMThread); |
| 2188 | scavengeReference((Object **)(void *)&gDvm.classJavaLangThreadGroup); |
| 2189 | scavengeReference((Object **)(void *)&gDvm.classJavaLangThrowable); |
| 2190 | scavengeReference((Object **)(void *)&gDvm.classJavaLangStackTraceElement); |
| 2191 | scavengeReference((Object **)(void *)&gDvm.classJavaLangStackTraceElementArray); |
| 2192 | scavengeReference((Object **)(void *)&gDvm.classJavaLangAnnotationAnnotationArray); |
| 2193 | scavengeReference((Object **)(void *)&gDvm.classJavaLangAnnotationAnnotationArrayArray); |
| 2194 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectAccessibleObject); |
| 2195 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectConstructor); |
| 2196 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectConstructorArray); |
| 2197 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectField); |
| 2198 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectFieldArray); |
| 2199 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectMethod); |
| 2200 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectMethodArray); |
| 2201 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectProxy); |
| 2202 | scavengeReference((Object **)(void *)&gDvm.classJavaLangExceptionInInitializerError); |
| 2203 | scavengeReference((Object **)(void *)&gDvm.classJavaLangRefReference); |
| 2204 | scavengeReference((Object **)(void *)&gDvm.classJavaNioReadWriteDirectByteBuffer); |
| 2205 | scavengeReference((Object **)(void *)&gDvm.classJavaSecurityAccessController); |
| 2206 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyLangAnnotationAnnotationFactory); |
| 2207 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyLangAnnotationAnnotationMember); |
| 2208 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyLangAnnotationAnnotationMemberArray); |
| 2209 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyNioInternalDirectBuffer); |
| 2210 | scavengeReference((Object **)(void *)&gDvm.classArrayBoolean); |
| 2211 | scavengeReference((Object **)(void *)&gDvm.classArrayChar); |
| 2212 | scavengeReference((Object **)(void *)&gDvm.classArrayFloat); |
| 2213 | scavengeReference((Object **)(void *)&gDvm.classArrayDouble); |
| 2214 | scavengeReference((Object **)(void *)&gDvm.classArrayByte); |
| 2215 | scavengeReference((Object **)(void *)&gDvm.classArrayShort); |
| 2216 | scavengeReference((Object **)(void *)&gDvm.classArrayInt); |
| 2217 | scavengeReference((Object **)(void *)&gDvm.classArrayLong); |
| 2218 | } |
| 2219 | |
| 2220 | void describeHeap(void) |
| 2221 | { |
| 2222 | HeapSource *heapSource; |
| 2223 | |
| 2224 | heapSource = gDvm.gcHeap->heapSource; |
| 2225 | describeBlocks(heapSource); |
| 2226 | } |
| 2227 | |
| 2228 | /* |
| 2229 | * The collection interface. Collection has a few distinct phases. |
| 2230 | * The first is flipping AKA condemning AKA whitening the heap. The |
| 2231 | * second is to promote all objects which are pointed to by pinned or |
| 2232 | * ambiguous references. The third phase is tracing from the stacks, |
| 2233 | * registers and various globals. Lastly, a verification of the heap |
| 2234 | * is performed. The last phase should be optional. |
| 2235 | */ |
| 2236 | void dvmScavengeRoots(void) /* Needs a new name badly */ |
| 2237 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2238 | GcHeap *gcHeap; |
| 2239 | |
| 2240 | { |
| 2241 | size_t alloc, unused, total; |
| 2242 | |
| 2243 | room(&alloc, &unused, &total); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2244 | LOG_SCAV("BEFORE GC: %zu alloc, %zu free, %zu total.", |
| 2245 | alloc, unused, total); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | gcHeap = gDvm.gcHeap; |
| 2249 | dvmHeapSourceFlip(); |
| 2250 | |
| 2251 | /* |
| 2252 | * Promote blocks with stationary objects. |
| 2253 | */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2254 | pinThreadList(); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2255 | pinReferenceTable(&gDvm.jniGlobalRefTable); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2256 | pinReferenceTable(&gDvm.jniPinRefTable); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2257 | pinReferenceTable(&gcHeap->nonCollectableRefs); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2258 | pinHashTableEntries(gDvm.loadedClasses); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2259 | pinPrimitiveClasses(); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2260 | pinInternedStrings(); |
| 2261 | |
| 2262 | // describeBlocks(gcHeap->heapSource); |
| 2263 | |
| 2264 | /* |
| 2265 | * Create first, open new-space page right here. |
| 2266 | */ |
| 2267 | |
| 2268 | /* Reset allocation to an unallocated block. */ |
| 2269 | gDvm.gcHeap->heapSource->allocPtr = allocateBlocks(gDvm.gcHeap->heapSource, 1); |
| 2270 | gDvm.gcHeap->heapSource->allocLimit = gDvm.gcHeap->heapSource->allocPtr + BLOCK_SIZE; |
| 2271 | /* |
| 2272 | * Hack: promote the empty block allocated above. If the |
| 2273 | * promotions that occurred above did not actually gray any |
| 2274 | * objects, the block queue may be empty. We must force a |
| 2275 | * promotion to be safe. |
| 2276 | */ |
| 2277 | promoteBlockByAddr(gDvm.gcHeap->heapSource, gDvm.gcHeap->heapSource->allocPtr); |
| 2278 | |
| 2279 | /* |
| 2280 | * Scavenge blocks and relocate movable objects. |
| 2281 | */ |
| 2282 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2283 | LOG_SCAV("Scavenging gDvm.threadList"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2284 | scavengeThreadList(); |
| 2285 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2286 | LOG_SCAV("Scavenging gDvm.gcHeap->referenceOperations"); |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 2287 | scavengeLargeHeapRefTable(gcHeap->referenceOperations, true); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2288 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2289 | LOG_SCAV("Scavenging gDvm.gcHeap->pendingFinalizationRefs"); |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 2290 | scavengeLargeHeapRefTable(gcHeap->pendingFinalizationRefs, false); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2291 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2292 | LOG_SCAV("Scavenging random global stuff"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2293 | scavengeReference(&gDvm.outOfMemoryObj); |
| 2294 | scavengeReference(&gDvm.internalErrorObj); |
| 2295 | scavengeReference(&gDvm.noClassDefFoundErrorObj); |
| 2296 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2297 | LOG_SCAV("Scavenging gDvm.dbgRegistry"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2298 | scavengeHashTable(gDvm.dbgRegistry); |
| 2299 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2300 | // LOG_SCAV("Scavenging gDvm.internedString"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2301 | scavengeInternedStrings(); |
| 2302 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2303 | LOG_SCAV("Root scavenge has completed."); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2304 | |
| 2305 | scavengeBlockQueue(); |
| 2306 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2307 | LOG_SCAV("Re-snap global class pointers."); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2308 | scavengeGlobals(); |
| 2309 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2310 | LOG_SCAV("New space scavenge has completed."); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2311 | |
| 2312 | /* |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2313 | * Process reference objects in strength order. |
| 2314 | */ |
| 2315 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2316 | LOG_REF("Processing soft references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2317 | preserveSoftReferences(&gDvm.gcHeap->softReferences); |
| 2318 | clearWhiteReferences(&gDvm.gcHeap->softReferences); |
| 2319 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2320 | LOG_REF("Processing weak references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2321 | clearWhiteReferences(&gDvm.gcHeap->weakReferences); |
| 2322 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2323 | LOG_REF("Finding finalizations..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2324 | processFinalizableReferences(); |
| 2325 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2326 | LOG_REF("Processing f-reachable soft references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2327 | clearWhiteReferences(&gDvm.gcHeap->softReferences); |
| 2328 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2329 | LOG_REF("Processing f-reachable weak references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2330 | clearWhiteReferences(&gDvm.gcHeap->weakReferences); |
| 2331 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2332 | LOG_REF("Processing phantom references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2333 | clearWhiteReferences(&gDvm.gcHeap->phantomReferences); |
| 2334 | |
| 2335 | /* |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2336 | * Verify the stack and heap. |
| 2337 | */ |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 2338 | dvmVerifyRoots(); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2339 | verifyNewSpace(); |
| 2340 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2341 | //describeBlocks(gcHeap->heapSource); |
| 2342 | |
| 2343 | clearFromSpace(gcHeap->heapSource); |
| 2344 | |
| 2345 | { |
| 2346 | size_t alloc, rem, total; |
| 2347 | |
| 2348 | room(&alloc, &rem, &total); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2349 | LOG_SCAV("AFTER GC: %zu alloc, %zu free, %zu total.", alloc, rem, total); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | /* |
| 2354 | * Interface compatibility routines. |
| 2355 | */ |
| 2356 | |
| 2357 | void dvmClearWhiteRefs(Object **list) |
| 2358 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2359 | /* do nothing */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2360 | assert(*list == NULL); |
| 2361 | } |
| 2362 | |
| 2363 | void dvmHandleSoftRefs(Object **list) |
| 2364 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2365 | /* do nothing */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2366 | assert(*list == NULL); |
| 2367 | } |
| 2368 | |
| 2369 | bool dvmHeapBeginMarkStep(GcMode mode) |
| 2370 | { |
| 2371 | /* do nothing */ |
| 2372 | return true; |
| 2373 | } |
| 2374 | |
| 2375 | void dvmHeapFinishMarkStep(void) |
| 2376 | { |
| 2377 | /* do nothing */ |
| 2378 | } |
| 2379 | |
| 2380 | void dvmHeapMarkRootSet(void) |
| 2381 | { |
| 2382 | /* do nothing */ |
| 2383 | } |
| 2384 | |
| 2385 | void dvmHeapScanMarkedObjects(void) |
| 2386 | { |
| 2387 | dvmScavengeRoots(); |
| 2388 | } |
| 2389 | |
| 2390 | void dvmHeapScheduleFinalizations(void) |
| 2391 | { |
| 2392 | /* do nothing */ |
| 2393 | } |
| 2394 | |
| 2395 | void dvmHeapSweepUnmarkedObjects(GcMode mode, int *numFreed, size_t *sizeFreed) |
| 2396 | { |
| Carl Shapiro | 703a2f3 | 2010-05-12 23:11:37 -0700 | [diff] [blame] | 2397 | *numFreed = 0; |
| 2398 | *sizeFreed = 0; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2399 | /* do nothing */ |
| 2400 | } |
| 2401 | |
| 2402 | void dvmMarkObjectNonNull(const Object *obj) |
| 2403 | { |
| 2404 | assert(!"implemented"); |
| 2405 | } |