| 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 | { |
| Carl Shapiro | 603469a | 2010-05-20 20:22:31 -0700 | [diff] [blame] | 593 | return &gDvm.gcHeap->heapSource->allocBits; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Allocate the specified number of bytes from the heap. The |
| 598 | * allocation cursor points into a block of free storage. If the |
| 599 | * given allocation fits in the remaining space of the block, we |
| 600 | * advance the cursor and return a pointer to the free storage. If |
| 601 | * the allocation cannot fit in the current block but is smaller than |
| 602 | * a block we request a new block and allocate from it instead. If |
| 603 | * the allocation is larger than a block we must allocate from a span |
| 604 | * of contiguous blocks. |
| 605 | */ |
| 606 | void *dvmHeapSourceAlloc(size_t length) |
| 607 | { |
| 608 | HeapSource *heapSource; |
| 609 | unsigned char *addr; |
| 610 | size_t aligned, available, blocks; |
| 611 | |
| 612 | heapSource = gDvm.gcHeap->heapSource; |
| 613 | assert(heapSource != NULL); |
| 614 | assert(heapSource->allocPtr != NULL); |
| 615 | assert(heapSource->allocLimit != NULL); |
| 616 | |
| 617 | aligned = alignUp(length, ALLOC_ALIGNMENT); |
| 618 | available = heapSource->allocLimit - heapSource->allocPtr; |
| 619 | |
| 620 | /* Try allocating inside the current block. */ |
| 621 | if (aligned <= available) { |
| 622 | addr = heapSource->allocPtr; |
| 623 | heapSource->allocPtr += aligned; |
| 624 | heapSource->bytesAllocated += aligned; |
| 625 | dvmHeapBitmapSetObjectBit(&heapSource->allocBits, addr); |
| 626 | return addr; |
| 627 | } |
| 628 | |
| 629 | /* Try allocating in a new block. */ |
| 630 | if (aligned <= BLOCK_SIZE) { |
| 631 | addr = allocateBlocks(heapSource, 1); |
| 632 | if (addr != NULL) { |
| 633 | heapSource->allocLimit = addr + BLOCK_SIZE; |
| 634 | heapSource->allocPtr = addr + aligned; |
| 635 | heapSource->bytesAllocated += aligned; |
| 636 | dvmHeapBitmapSetObjectBit(&heapSource->allocBits, addr); |
| 637 | /* TODO(cshapiro): pad out the current block. */ |
| 638 | } |
| 639 | return addr; |
| 640 | } |
| 641 | |
| 642 | /* Try allocating in a span of blocks. */ |
| 643 | blocks = alignUp(aligned, BLOCK_SIZE) / BLOCK_SIZE; |
| 644 | |
| 645 | addr = allocateBlocks(heapSource, blocks); |
| 646 | /* Propagate failure upward. */ |
| 647 | if (addr != NULL) { |
| 648 | heapSource->bytesAllocated += aligned; |
| 649 | dvmHeapBitmapSetObjectBit(&heapSource->allocBits, addr); |
| 650 | /* TODO(cshapiro): pad out free space in the last block. */ |
| 651 | } |
| 652 | return addr; |
| 653 | } |
| 654 | |
| 655 | void *dvmHeapSourceAllocAndGrow(size_t size) |
| 656 | { |
| 657 | return dvmHeapSourceAlloc(size); |
| 658 | } |
| 659 | |
| 660 | /* TODO: refactor along with dvmHeapSourceAlloc */ |
| 661 | void *allocateGray(size_t size) |
| 662 | { |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 663 | HeapSource *heapSource; |
| 664 | void *addr; |
| 665 | size_t block; |
| 666 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 667 | /* TODO: add a check that we are in a GC. */ |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 668 | heapSource = gDvm.gcHeap->heapSource; |
| 669 | addr = dvmHeapSourceAlloc(size); |
| Carl Shapiro | 703a2f3 | 2010-05-12 23:11:37 -0700 | [diff] [blame] | 670 | assert(addr != NULL); |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 671 | block = addressToBlock(heapSource, (const u1 *)addr); |
| 672 | if (heapSource->queueHead == QUEUE_TAIL) { |
| 673 | /* |
| 674 | * Forcibly append the underlying block to the queue. This |
| 675 | * condition occurs when referents are transported following |
| 676 | * the initial trace. |
| 677 | */ |
| 678 | enqueueBlock(heapSource, block); |
| 679 | LOG_PROM("forced promoting block %zu %d @ %p", block, heapSource->blockSpace[block], addr); |
| 680 | } |
| 681 | return addr; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Returns true if the given address is within the heap and points to |
| 686 | * the header of a live object. |
| 687 | */ |
| 688 | bool dvmHeapSourceContains(const void *addr) |
| 689 | { |
| 690 | HeapSource *heapSource; |
| 691 | HeapBitmap *bitmap; |
| 692 | |
| 693 | heapSource = gDvm.gcHeap->heapSource; |
| 694 | bitmap = &heapSource->allocBits; |
| Carl Shapiro | dc1e4f1 | 2010-05-01 22:27:56 -0700 | [diff] [blame] | 695 | if (!dvmHeapBitmapCoversAddress(bitmap, addr)) { |
| 696 | return false; |
| 697 | } else { |
| 698 | return dvmHeapBitmapIsObjectBitSet(bitmap, addr); |
| 699 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | bool dvmHeapSourceGetPtrFlag(const void *ptr, enum HeapSourcePtrFlag flag) |
| 703 | { |
| 704 | assert(!"implemented"); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | size_t dvmHeapSourceChunkSize(const void *ptr) |
| 709 | { |
| 710 | assert(!"implemented"); |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | size_t dvmHeapSourceFootprint(void) |
| 715 | { |
| 716 | assert(!"implemented"); |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Returns the "ideal footprint" which appears to be the number of |
| 722 | * bytes currently committed to the heap. This starts out at the |
| 723 | * start size of the heap and grows toward the maximum size. |
| 724 | */ |
| 725 | size_t dvmHeapSourceGetIdealFootprint(void) |
| 726 | { |
| 727 | return gDvm.gcHeap->heapSource->currentSize; |
| 728 | } |
| 729 | |
| 730 | float dvmGetTargetHeapUtilization(void) |
| 731 | { |
| Carl Shapiro | 603469a | 2010-05-20 20:22:31 -0700 | [diff] [blame] | 732 | return 0.5f; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | void dvmSetTargetHeapUtilization(float newTarget) |
| 736 | { |
| Carl Shapiro | 603469a | 2010-05-20 20:22:31 -0700 | [diff] [blame] | 737 | assert(newTarget > 0.0f && newTarget < 1.0f); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | size_t dvmMinimumHeapSize(size_t size, bool set) |
| 741 | { |
| 742 | return gDvm.gcHeap->heapSource->minimumSize; |
| 743 | } |
| 744 | |
| 745 | /* |
| 746 | * Expands the size of the heap after a collection. At present we |
| 747 | * commit the pages for maximum size of the heap so this routine is |
| 748 | * just a no-op. Eventually, we will either allocate or commit pages |
| 749 | * on an as-need basis. |
| 750 | */ |
| 751 | void dvmHeapSourceGrowForUtilization(void) |
| 752 | { |
| 753 | /* do nothing */ |
| 754 | } |
| 755 | |
| 756 | void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen) |
| 757 | { |
| 758 | /* do nothing */ |
| 759 | } |
| 760 | |
| 761 | void dvmHeapSourceWalk(void (*callback)(const void *chunkptr, size_t chunklen, |
| 762 | const void *userptr, size_t userlen, |
| 763 | void *arg), |
| 764 | void *arg) |
| 765 | { |
| 766 | assert(!"implemented"); |
| 767 | } |
| 768 | |
| 769 | size_t dvmHeapSourceGetNumHeaps(void) |
| 770 | { |
| 771 | return 1; |
| 772 | } |
| 773 | |
| 774 | bool dvmTrackExternalAllocation(size_t n) |
| 775 | { |
| Carl Shapiro | 528f381 | 2010-05-18 14:16:26 -0700 | [diff] [blame] | 776 | /* do nothing */ |
| 777 | return true; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | void dvmTrackExternalFree(size_t n) |
| 781 | { |
| Carl Shapiro | 528f381 | 2010-05-18 14:16:26 -0700 | [diff] [blame] | 782 | /* do nothing */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | size_t dvmGetExternalBytesAllocated(void) |
| 786 | { |
| 787 | assert(!"implemented"); |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | void dvmHeapSourceFlip(void) |
| 792 | { |
| 793 | HeapSource *heapSource; |
| 794 | size_t i; |
| 795 | |
| 796 | heapSource = gDvm.gcHeap->heapSource; |
| 797 | |
| 798 | /* Reset the block queue. */ |
| 799 | heapSource->allocBlocks = 0; |
| 800 | heapSource->queueSize = 0; |
| 801 | heapSource->queueHead = QUEUE_TAIL; |
| 802 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 803 | /* TODO(cshapiro): pad the current (prev) block. */ |
| 804 | |
| 805 | heapSource->allocPtr = NULL; |
| 806 | heapSource->allocLimit = NULL; |
| 807 | |
| 808 | /* Whiten all allocated blocks. */ |
| 809 | for (i = 0; i < heapSource->totalBlocks; ++i) { |
| 810 | if (heapSource->blockSpace[i] == BLOCK_TO_SPACE) { |
| 811 | heapSource->blockSpace[i] = BLOCK_FROM_SPACE; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | static void room(size_t *alloc, size_t *avail, size_t *total) |
| 817 | { |
| 818 | HeapSource *heapSource; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 819 | |
| 820 | heapSource = gDvm.gcHeap->heapSource; |
| 821 | *total = heapSource->totalBlocks*BLOCK_SIZE; |
| 822 | *alloc = heapSource->allocBlocks*BLOCK_SIZE; |
| 823 | *avail = *total - *alloc; |
| 824 | } |
| 825 | |
| 826 | static bool isSpaceInternal(u1 *addr, int space) |
| 827 | { |
| 828 | HeapSource *heapSource; |
| 829 | u1 *base, *limit; |
| 830 | size_t offset; |
| 831 | char space2; |
| 832 | |
| 833 | heapSource = gDvm.gcHeap->heapSource; |
| 834 | base = heapSource->blockBase; |
| 835 | assert(addr >= base); |
| 836 | limit = heapSource->blockBase + heapSource->maximumSize; |
| 837 | assert(addr < limit); |
| 838 | offset = addr - base; |
| 839 | space2 = heapSource->blockSpace[offset >> BLOCK_SHIFT]; |
| 840 | return space == space2; |
| 841 | } |
| 842 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 843 | static bool fromSpaceContains(const void *addr) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 844 | { |
| 845 | return isSpaceInternal((u1 *)addr, BLOCK_FROM_SPACE); |
| 846 | } |
| 847 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 848 | static bool toSpaceContains(const void *addr) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 849 | { |
| 850 | return isSpaceInternal((u1 *)addr, BLOCK_TO_SPACE); |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * Notifies the collector that the object at the given address must |
| 855 | * remain stationary during the current collection. |
| 856 | */ |
| 857 | static void pinObject(const Object *obj) |
| 858 | { |
| 859 | promoteBlockByAddr(gDvm.gcHeap->heapSource, obj); |
| 860 | } |
| 861 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 862 | static size_t sumHeapBitmap(const HeapBitmap *bitmap) |
| 863 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 864 | size_t i, sum; |
| 865 | |
| 866 | sum = 0; |
| 867 | for (i = 0; i < bitmap->bitsLen >> 2; ++i) { |
| Carl Shapiro | 603469a | 2010-05-20 20:22:31 -0700 | [diff] [blame] | 868 | sum += CLZ(bitmap->bits[i]); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 869 | } |
| 870 | return sum; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * Miscellaneous functionality. |
| 875 | */ |
| 876 | |
| 877 | static int isForward(const void *addr) |
| 878 | { |
| 879 | return (uintptr_t)addr & 0x1; |
| 880 | } |
| 881 | |
| 882 | static void setForward(const void *toObj, void *fromObj) |
| 883 | { |
| 884 | *(unsigned long *)fromObj = (uintptr_t)toObj | 0x1; |
| 885 | } |
| 886 | |
| 887 | static void* getForward(const void *fromObj) |
| 888 | { |
| 889 | return (void *)((uintptr_t)fromObj & ~0x1); |
| 890 | } |
| 891 | |
| 892 | /* Beware, uses the same encoding as a forwarding pointers! */ |
| 893 | static int isPermanentString(const StringObject *obj) { |
| 894 | return (uintptr_t)obj & 0x1; |
| 895 | } |
| 896 | |
| 897 | static void* getPermanentString(const StringObject *obj) |
| 898 | { |
| 899 | return (void *)((uintptr_t)obj & ~0x1); |
| 900 | } |
| 901 | |
| 902 | |
| 903 | /* |
| 904 | * Scavenging and transporting routines follow. A transporter grays |
| 905 | * an object. A scavenger blackens an object. We define these |
| 906 | * routines for each fundamental object type. Dispatch is performed |
| 907 | * in scavengeObject. |
| 908 | */ |
| 909 | |
| 910 | /* |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 911 | * Class object scavenging. |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 912 | */ |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 913 | static void scavengeClassObject(ClassObject *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 914 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 915 | int i; |
| 916 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 917 | LOG_SCAV("scavengeClassObject(obj=%p)", obj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 918 | assert(obj != NULL); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 919 | assert(obj->obj.clazz != NULL); |
| 920 | assert(obj->obj.clazz->descriptor != NULL); |
| 921 | assert(!strcmp(obj->obj.clazz->descriptor, "Ljava/lang/Class;")); |
| 922 | assert(obj->descriptor != NULL); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 923 | LOG_SCAV("scavengeClassObject: descriptor='%s',vtableCount=%zu", |
| 924 | obj->descriptor, obj->vtableCount); |
| Carl Shapiro | 703a2f3 | 2010-05-12 23:11:37 -0700 | [diff] [blame] | 925 | /* Delegate class object and instance field scavenging. */ |
| 926 | scavengeDataObject((Object *)obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 927 | /* Scavenge the array element class object. */ |
| 928 | if (IS_CLASS_FLAG_SET(obj, CLASS_ISARRAY)) { |
| 929 | scavengeReference((Object **)(void *)&obj->elementClass); |
| 930 | } |
| 931 | /* Scavenge the superclass. */ |
| 932 | scavengeReference((Object **)(void *)&obj->super); |
| 933 | /* Scavenge the class loader. */ |
| 934 | scavengeReference(&obj->classLoader); |
| 935 | /* Scavenge static fields. */ |
| 936 | for (i = 0; i < obj->sfieldCount; ++i) { |
| 937 | char ch = obj->sfields[i].field.signature[0]; |
| 938 | if (ch == '[' || ch == 'L') { |
| 939 | scavengeReference((Object **)(void *)&obj->sfields[i].value.l); |
| 940 | } |
| 941 | } |
| 942 | /* Scavenge interface class objects. */ |
| 943 | for (i = 0; i < obj->interfaceCount; ++i) { |
| 944 | scavengeReference((Object **) &obj->interfaces[i]); |
| 945 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Array object scavenging. |
| 950 | */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 951 | static size_t scavengeArrayObject(ArrayObject *array) |
| 952 | { |
| 953 | size_t i, length; |
| 954 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 955 | LOG_SCAV("scavengeArrayObject(array=%p)", array); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 956 | /* Scavenge the class object. */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 957 | assert(toSpaceContains(array)); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 958 | assert(array != NULL); |
| 959 | assert(array->obj.clazz != NULL); |
| 960 | scavengeReference((Object **) array); |
| 961 | length = dvmArrayObjectSize(array); |
| 962 | /* Scavenge the array contents. */ |
| 963 | if (IS_CLASS_FLAG_SET(array->obj.clazz, CLASS_ISOBJECTARRAY)) { |
| 964 | Object **contents = (Object **)array->contents; |
| 965 | for (i = 0; i < array->length; ++i) { |
| 966 | scavengeReference(&contents[i]); |
| 967 | } |
| 968 | } |
| 969 | return length; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * Reference object scavenging. |
| 974 | */ |
| 975 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 976 | static int getReferenceFlags(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 977 | { |
| 978 | int flags; |
| 979 | |
| 980 | flags = CLASS_ISREFERENCE | |
| 981 | CLASS_ISWEAKREFERENCE | |
| 982 | CLASS_ISPHANTOMREFERENCE; |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 983 | return GET_CLASS_FLAG_GROUP(obj->clazz, flags); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 986 | static int isSoftReference(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 987 | { |
| 988 | return getReferenceFlags(obj) == CLASS_ISREFERENCE; |
| 989 | } |
| 990 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 991 | static int isWeakReference(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 992 | { |
| 993 | return getReferenceFlags(obj) & CLASS_ISWEAKREFERENCE; |
| 994 | } |
| 995 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 996 | static bool isPhantomReference(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 997 | { |
| 998 | return getReferenceFlags(obj) & CLASS_ISPHANTOMREFERENCE; |
| 999 | } |
| 1000 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1001 | /* |
| 1002 | * Returns true if the reference was registered with a reference queue |
| 1003 | * but has not yet been appended to it. |
| 1004 | */ |
| 1005 | static bool isReferenceEnqueuable(const Object *ref) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1006 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1007 | Object *queue, *queueNext; |
| 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 | queue = dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue); |
| 1010 | queueNext = dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext); |
| 1011 | if (queue == NULL || queueNext != NULL) { |
| 1012 | /* |
| 1013 | * There is no queue, or the reference has already |
| 1014 | * been enqueued. The Reference.enqueue() method |
| 1015 | * will do nothing even if we call it. |
| 1016 | */ |
| 1017 | return false; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1018 | } |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1019 | |
| 1020 | /* |
| 1021 | * We need to call enqueue(), but if we called it from |
| 1022 | * here we'd probably deadlock. Schedule a call. |
| 1023 | */ |
| 1024 | return true; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Schedules a reference to be appended to its reference queue. |
| 1029 | */ |
| 1030 | static void enqueueReference(const Object *ref) |
| 1031 | { |
| 1032 | LargeHeapRefTable **table; |
| 1033 | Object *op; |
| 1034 | |
| 1035 | assert(((uintptr_t)ref & 3) == 0); |
| 1036 | assert((WORKER_ENQUEUE & ~3) == 0); |
| 1037 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL); |
| 1038 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL); |
| 1039 | /* |
| 1040 | * Set the enqueue bit in the bottom of the pointer. Assumes that |
| 1041 | * objects are 8-byte aligned. |
| 1042 | * |
| 1043 | * Note that we are adding the *Reference* (which is by definition |
| 1044 | * already black at this point) to this list; we're not adding the |
| 1045 | * referent (which has already been cleared). |
| 1046 | */ |
| 1047 | table = &gDvm.gcHeap->referenceOperations; |
| 1048 | op = (Object *)((uintptr_t)ref | WORKER_ENQUEUE); |
| 1049 | if (!dvmHeapAddRefToLargeTable(table, op)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1050 | LOGE("no room for any more reference operations"); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1051 | dvmAbort(); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * Sets the referent field of a reference object to NULL. |
| 1057 | */ |
| 1058 | static void clearReference(Object *obj) |
| 1059 | { |
| 1060 | dvmSetFieldObject(obj, gDvm.offJavaLangRefReference_referent, NULL); |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Clears reference objects with white referents. |
| 1065 | */ |
| 1066 | void clearWhiteReferences(Object **list) |
| 1067 | { |
| 1068 | size_t referentOffset, vmDataOffset; |
| 1069 | bool doSignal; |
| 1070 | |
| 1071 | vmDataOffset = gDvm.offJavaLangRefReference_vmData; |
| 1072 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 1073 | doSignal = false; |
| 1074 | while (*list != NULL) { |
| 1075 | Object *ref = *list; |
| 1076 | JValue *field = dvmFieldPtr(ref, referentOffset); |
| 1077 | Object *referent = field->l; |
| 1078 | *list = dvmGetFieldObject(ref, vmDataOffset); |
| 1079 | assert(referent != NULL); |
| 1080 | if (isForward(referent->clazz)) { |
| 1081 | field->l = referent = getForward(referent->clazz); |
| 1082 | continue; |
| 1083 | } |
| 1084 | if (fromSpaceContains(referent)) { |
| 1085 | /* Referent is white, clear it. */ |
| 1086 | clearReference(ref); |
| 1087 | if (isReferenceEnqueuable(ref)) { |
| 1088 | enqueueReference(ref); |
| 1089 | doSignal = true; |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | /* |
| 1094 | * If we cleared a reference with a reference queue we must notify |
| 1095 | * the heap worker to append the reference. |
| 1096 | */ |
| 1097 | if (doSignal) { |
| 1098 | dvmSignalHeapWorker(false); |
| 1099 | } |
| 1100 | assert(*list == NULL); |
| 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | * Blackens referents subject to the soft reference preservation |
| 1105 | * policy. |
| 1106 | */ |
| 1107 | void preserveSoftReferences(Object **list) |
| 1108 | { |
| 1109 | Object *ref; |
| 1110 | Object *prev, *next; |
| 1111 | size_t referentOffset, vmDataOffset; |
| 1112 | unsigned counter; |
| 1113 | bool white; |
| 1114 | |
| 1115 | vmDataOffset = gDvm.offJavaLangRefReference_vmData; |
| 1116 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 1117 | counter = 0; |
| 1118 | prev = next = NULL; |
| 1119 | ref = *list; |
| 1120 | while (ref != NULL) { |
| 1121 | JValue *field = dvmFieldPtr(ref, referentOffset); |
| 1122 | Object *referent = field->l; |
| 1123 | next = dvmGetFieldObject(ref, vmDataOffset); |
| 1124 | assert(referent != NULL); |
| 1125 | if (isForward(referent->clazz)) { |
| 1126 | /* Referent is black. */ |
| 1127 | field->l = referent = getForward(referent->clazz); |
| 1128 | white = false; |
| 1129 | } else { |
| 1130 | white = fromSpaceContains(referent); |
| 1131 | } |
| 1132 | if (!white && ((++counter) & 1)) { |
| 1133 | /* Referent is white and biased toward saving, gray it. */ |
| 1134 | scavengeReference((Object **)(void *)&field->l); |
| 1135 | white = true; |
| 1136 | } |
| 1137 | if (white) { |
| 1138 | /* Referent is black, unlink it. */ |
| 1139 | if (prev != NULL) { |
| 1140 | dvmSetFieldObject(ref, vmDataOffset, NULL); |
| 1141 | dvmSetFieldObject(prev, vmDataOffset, next); |
| 1142 | } |
| 1143 | } else { |
| 1144 | /* Referent is white, skip over it. */ |
| 1145 | prev = ref; |
| 1146 | } |
| 1147 | ref = next; |
| 1148 | } |
| 1149 | /* |
| 1150 | * Restart the trace with the newly gray references added to the |
| 1151 | * root set. |
| 1152 | */ |
| 1153 | scavengeBlockQueue(); |
| 1154 | } |
| 1155 | |
| 1156 | void processFinalizableReferences(void) |
| 1157 | { |
| 1158 | HeapRefTable newPendingRefs; |
| 1159 | LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs; |
| 1160 | Object **ref; |
| 1161 | Object **lastRef; |
| 1162 | size_t totalPendCount; |
| 1163 | |
| 1164 | /* |
| 1165 | * All strongly, reachable objects are black. |
| 1166 | * Any white finalizable objects need to be finalized. |
| 1167 | */ |
| 1168 | |
| 1169 | /* Create a table that the new pending refs will |
| 1170 | * be added to. |
| 1171 | */ |
| 1172 | if (!dvmHeapInitHeapRefTable(&newPendingRefs, 128)) { |
| 1173 | //TODO: mark all finalizable refs and hope that |
| 1174 | // we can schedule them next time. Watch out, |
| 1175 | // because we may be expecting to free up space |
| 1176 | // by calling finalizers. |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1177 | LOG_REF("no room for pending finalizations\n"); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1178 | dvmAbort(); |
| 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | * Walk through finalizableRefs and move any white references to |
| 1183 | * the list of new pending refs. |
| 1184 | */ |
| 1185 | totalPendCount = 0; |
| 1186 | while (finRefs != NULL) { |
| 1187 | Object **gapRef; |
| 1188 | size_t newPendCount = 0; |
| 1189 | |
| 1190 | gapRef = ref = finRefs->refs.table; |
| 1191 | lastRef = finRefs->refs.nextEntry; |
| 1192 | while (ref < lastRef) { |
| 1193 | if (fromSpaceContains(*ref)) { |
| 1194 | if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) { |
| 1195 | //TODO: add the current table and allocate |
| 1196 | // a new, smaller one. |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1197 | LOG_REF("no room for any more pending finalizations: %zd\n", |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1198 | dvmHeapNumHeapRefTableEntries(&newPendingRefs)); |
| 1199 | dvmAbort(); |
| 1200 | } |
| 1201 | newPendCount++; |
| 1202 | } else { |
| 1203 | /* This ref is black, so will remain on finalizableRefs. |
| 1204 | */ |
| 1205 | if (newPendCount > 0) { |
| 1206 | /* Copy it up to fill the holes. |
| 1207 | */ |
| 1208 | *gapRef++ = *ref; |
| 1209 | } else { |
| 1210 | /* No holes yet; don't bother copying. |
| 1211 | */ |
| 1212 | gapRef++; |
| 1213 | } |
| 1214 | } |
| 1215 | ref++; |
| 1216 | } |
| 1217 | finRefs->refs.nextEntry = gapRef; |
| 1218 | //TODO: if the table is empty when we're done, free it. |
| 1219 | totalPendCount += newPendCount; |
| 1220 | finRefs = finRefs->next; |
| 1221 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1222 | LOG_REF("%zd finalizers triggered.\n", totalPendCount); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1223 | if (totalPendCount == 0) { |
| 1224 | /* No objects required finalization. |
| 1225 | * Free the empty temporary table. |
| 1226 | */ |
| 1227 | dvmClearReferenceTable(&newPendingRefs); |
| 1228 | return; |
| 1229 | } |
| 1230 | |
| 1231 | /* Add the new pending refs to the main list. |
| 1232 | */ |
| 1233 | if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs, |
| 1234 | &newPendingRefs)) |
| 1235 | { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1236 | LOG_REF("can't insert new pending finalizations\n"); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1237 | dvmAbort(); |
| 1238 | } |
| 1239 | |
| 1240 | //TODO: try compacting the main list with a memcpy loop |
| 1241 | |
| 1242 | /* Blacken the refs we just moved; we don't want them or their |
| 1243 | * children to get swept yet. |
| 1244 | */ |
| 1245 | ref = newPendingRefs.table; |
| 1246 | lastRef = newPendingRefs.nextEntry; |
| 1247 | assert(ref < lastRef); |
| 1248 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0); |
| 1249 | while (ref < lastRef) { |
| 1250 | scavengeReference(ref); |
| 1251 | ref++; |
| 1252 | } |
| 1253 | HPROF_CLEAR_GC_SCAN_STATE(); |
| 1254 | scavengeBlockQueue(); |
| 1255 | dvmSignalHeapWorker(false); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1258 | /* |
| 1259 | * If a reference points to from-space and has been forwarded, we snap |
| 1260 | * the pointer to its new to-space address. If the reference points |
| 1261 | * to an unforwarded from-space address we must enqueue the reference |
| 1262 | * for later processing. TODO: implement proper reference processing |
| 1263 | * and move the referent scavenging elsewhere. |
| 1264 | */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1265 | static void scavengeReferenceObject(Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1266 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1267 | Object *referent; |
| 1268 | Object **queue; |
| 1269 | size_t referentOffset, vmDataOffset; |
| 1270 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1271 | assert(obj != NULL); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1272 | LOG_SCAV("scavengeReferenceObject(obj=%p),'%s'", obj, obj->clazz->descriptor); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1273 | scavengeDataObject(obj); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1274 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 1275 | referent = dvmGetFieldObject(obj, referentOffset); |
| 1276 | if (referent == NULL || toSpaceContains(referent)) { |
| 1277 | return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1278 | } |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1279 | if (isSoftReference(obj)) { |
| 1280 | queue = &gDvm.gcHeap->softReferences; |
| 1281 | } else if (isWeakReference(obj)) { |
| 1282 | queue = &gDvm.gcHeap->weakReferences; |
| 1283 | } else { |
| 1284 | assert(isPhantomReference(obj)); |
| 1285 | queue = &gDvm.gcHeap->phantomReferences; |
| 1286 | } |
| 1287 | vmDataOffset = gDvm.offJavaLangRefReference_vmData; |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1288 | dvmSetFieldObject(obj, vmDataOffset, *queue); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1289 | *queue = obj; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1290 | LOG_SCAV("scavengeReferenceObject: enqueueing %p", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * Data object scavenging. |
| 1295 | */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1296 | static void scavengeDataObject(Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1297 | { |
| 1298 | ClassObject *clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1299 | int i; |
| 1300 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1301 | // LOG_SCAV("scavengeDataObject(obj=%p)", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1302 | assert(obj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1303 | assert(obj->clazz != NULL); |
| 1304 | assert(obj->clazz->objectSize != 0); |
| 1305 | assert(toSpaceContains(obj)); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1306 | /* Scavenge the class object. */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1307 | clazz = obj->clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1308 | scavengeReference((Object **) obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1309 | /* Scavenge instance fields. */ |
| 1310 | if (clazz->refOffsets != CLASS_WALK_SUPER) { |
| 1311 | size_t refOffsets = clazz->refOffsets; |
| 1312 | while (refOffsets != 0) { |
| 1313 | size_t rshift = CLZ(refOffsets); |
| 1314 | size_t offset = CLASS_OFFSET_FROM_CLZ(rshift); |
| 1315 | Object **ref = (Object **)((u1 *)obj + offset); |
| 1316 | scavengeReference(ref); |
| 1317 | refOffsets &= ~(CLASS_HIGH_BIT >> rshift); |
| 1318 | } |
| 1319 | } else { |
| 1320 | for (; clazz != NULL; clazz = clazz->super) { |
| 1321 | InstField *field = clazz->ifields; |
| 1322 | for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { |
| 1323 | size_t offset = field->byteOffset; |
| 1324 | Object **ref = (Object **)((u1 *)obj + offset); |
| 1325 | scavengeReference(ref); |
| 1326 | } |
| 1327 | } |
| 1328 | } |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | static Object *transportObject(const Object *fromObj) |
| 1332 | { |
| 1333 | Object *toObj; |
| 1334 | size_t allocSize, copySize; |
| 1335 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1336 | LOG_TRAN("transportObject(fromObj=%p) allocBlocks=%zu", |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1337 | fromObj, |
| 1338 | gDvm.gcHeap->heapSource->allocBlocks); |
| 1339 | assert(fromObj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1340 | assert(fromSpaceContains(fromObj)); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1341 | allocSize = copySize = objectSize(fromObj); |
| 1342 | if (LW_HASH_STATE(fromObj->lock) != LW_HASH_STATE_UNHASHED) { |
| 1343 | /* |
| 1344 | * The object has been hashed or hashed and moved. We must |
| 1345 | * reserve an additional word for a hash code. |
| 1346 | */ |
| 1347 | allocSize += sizeof(u4); |
| 1348 | } |
| 1349 | if (LW_HASH_STATE(fromObj->lock) == LW_HASH_STATE_HASHED_AND_MOVED) { |
| 1350 | /* |
| 1351 | * The object has its hash code allocated. Ensure the hash |
| 1352 | * code is copied along with the instance data. |
| 1353 | */ |
| 1354 | copySize += sizeof(u4); |
| 1355 | } |
| 1356 | /* TODO(cshapiro): don't copy, re-map large data objects. */ |
| 1357 | assert(copySize <= allocSize); |
| 1358 | toObj = allocateGray(allocSize); |
| 1359 | assert(toObj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1360 | assert(toSpaceContains(toObj)); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1361 | memcpy(toObj, fromObj, copySize); |
| 1362 | if (LW_HASH_STATE(fromObj->lock) == LW_HASH_STATE_HASHED) { |
| 1363 | /* |
| 1364 | * The object has had its hash code exposed. Append it to the |
| 1365 | * instance and set a bit so we know to look for it there. |
| 1366 | */ |
| 1367 | *(u4 *)(((char *)toObj) + copySize) = (u4)fromObj >> 3; |
| 1368 | toObj->lock |= LW_HASH_STATE_HASHED_AND_MOVED << LW_HASH_STATE_SHIFT; |
| 1369 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1370 | LOG_TRAN("transportObject: from %p/%zu to %p/%zu (%zu,%zu) %s", |
| 1371 | fromObj, addressToBlock(gDvm.gcHeap->heapSource,fromObj), |
| 1372 | toObj, addressToBlock(gDvm.gcHeap->heapSource,toObj), |
| 1373 | copySize, allocSize, copySize < allocSize ? "DIFFERENT" : ""); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1374 | return toObj; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | * Generic reference scavenging. |
| 1379 | */ |
| 1380 | |
| 1381 | /* |
| 1382 | * Given a reference to an object, the scavenge routine will gray the |
| 1383 | * reference. Any objects pointed to by the scavenger object will be |
| 1384 | * transported to new space and a forwarding pointer will be installed |
| 1385 | * in the header of the object. |
| 1386 | */ |
| 1387 | |
| 1388 | /* |
| 1389 | * Blacken the given pointer. If the pointer is in from space, it is |
| 1390 | * transported to new space. If the object has a forwarding pointer |
| 1391 | * installed it has already been transported and the referent is |
| 1392 | * snapped to the new address. |
| 1393 | */ |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1394 | static void scavengeReference(Object **obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1395 | { |
| 1396 | ClassObject *clazz; |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1397 | Object *fromObj, *toObj; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1398 | |
| 1399 | assert(obj); |
| 1400 | |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1401 | if (*obj == NULL) return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1402 | |
| 1403 | assert(dvmIsValidObject(*obj)); |
| 1404 | |
| 1405 | /* The entire block is black. */ |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1406 | if (toSpaceContains(*obj)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1407 | LOG_SCAV("scavengeReference skipping pinned object @ %p", *obj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1408 | return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1409 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1410 | LOG_SCAV("scavengeReference(*obj=%p)", *obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1411 | |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1412 | assert(fromSpaceContains(*obj)); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1413 | |
| 1414 | clazz = (*obj)->clazz; |
| 1415 | |
| 1416 | if (isForward(clazz)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1417 | // 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] | 1418 | *obj = (Object *)getForward(clazz); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1419 | return; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1420 | } |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1421 | fromObj = *obj; |
| 1422 | if (clazz == NULL) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1423 | // LOG_SCAV("scavangeReference %p has a NULL class object", fromObj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1424 | assert(!"implemented"); |
| 1425 | toObj = NULL; |
| 1426 | } else if (clazz == gDvm.unlinkedJavaLangClass) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1427 | // LOG_SCAV("scavangeReference %p is an unlinked class object", fromObj); |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1428 | assert(!"implemented"); |
| 1429 | toObj = NULL; |
| 1430 | } else { |
| 1431 | toObj = transportObject(fromObj); |
| 1432 | } |
| 1433 | setForward(toObj, fromObj); |
| 1434 | *obj = (Object *)toObj; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1437 | /* |
| 1438 | * Generic object scavenging. |
| 1439 | */ |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1440 | static void scavengeObject(Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1441 | { |
| 1442 | ClassObject *clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1443 | |
| 1444 | assert(obj != NULL); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1445 | assert(obj->clazz != NULL); |
| 1446 | assert(!((uintptr_t)obj->clazz & 0x1)); |
| 1447 | assert(obj->clazz != gDvm.unlinkedJavaLangClass); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1448 | clazz = obj->clazz; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1449 | if (clazz == gDvm.classJavaLangClass) { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1450 | scavengeClassObject((ClassObject *)obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1451 | } else if (IS_CLASS_FLAG_SET(clazz, CLASS_ISARRAY)) { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1452 | scavengeArrayObject((ArrayObject *)obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1453 | } else if (IS_CLASS_FLAG_SET(clazz, CLASS_ISREFERENCE)) { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1454 | scavengeReferenceObject(obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1455 | } else { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1456 | scavengeDataObject(obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1457 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * External root scavenging routines. |
| 1462 | */ |
| 1463 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1464 | static void pinHashTableEntries(HashTable *table) |
| 1465 | { |
| 1466 | HashEntry *entry; |
| 1467 | void *obj; |
| 1468 | int i; |
| 1469 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1470 | LOG_PIN(">>> pinHashTableEntries(table=%p)", table); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1471 | if (table == NULL) { |
| 1472 | return; |
| 1473 | } |
| 1474 | dvmHashTableLock(table); |
| 1475 | for (i = 0; i < table->tableSize; ++i) { |
| 1476 | entry = &table->pEntries[i]; |
| 1477 | obj = entry->data; |
| 1478 | if (obj == NULL || obj == HASH_TOMBSTONE) { |
| 1479 | continue; |
| 1480 | } |
| 1481 | pinObject(entry->data); |
| 1482 | } |
| 1483 | dvmHashTableUnlock(table); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1484 | LOG_PIN("<<< pinHashTableEntries(table=%p)", table); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | static void pinPrimitiveClasses(void) |
| 1488 | { |
| 1489 | size_t length; |
| 1490 | size_t i; |
| 1491 | |
| 1492 | length = ARRAYSIZE(gDvm.primitiveClass); |
| 1493 | for (i = 0; i < length; i++) { |
| 1494 | if (gDvm.primitiveClass[i] != NULL) { |
| 1495 | pinObject((Object *)gDvm.primitiveClass[i]); |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * Scavenge interned strings. Permanent interned strings will have |
| 1502 | * been pinned and are therefore ignored. Non-permanent strings that |
| 1503 | * have been forwarded are snapped. All other entries are removed. |
| 1504 | */ |
| 1505 | static void scavengeInternedStrings(void) |
| 1506 | { |
| 1507 | HashTable *table; |
| 1508 | HashEntry *entry; |
| 1509 | Object *obj; |
| 1510 | int i; |
| 1511 | |
| 1512 | table = gDvm.internedStrings; |
| 1513 | if (table == NULL) { |
| 1514 | return; |
| 1515 | } |
| 1516 | dvmHashTableLock(table); |
| 1517 | for (i = 0; i < table->tableSize; ++i) { |
| 1518 | entry = &table->pEntries[i]; |
| 1519 | obj = (Object *)entry->data; |
| 1520 | if (obj == NULL || obj == HASH_TOMBSTONE) { |
| 1521 | continue; |
| 1522 | } else if (!isPermanentString((StringObject *)obj)) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1523 | // LOG_SCAV("entry->data=%p", entry->data); |
| 1524 | LOG_SCAV(">>> string obj=%p", entry->data); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1525 | /* TODO(cshapiro): detach white string objects */ |
| 1526 | scavengeReference((Object **)(void *)&entry->data); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1527 | LOG_SCAV("<<< string obj=%p", entry->data); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | dvmHashTableUnlock(table); |
| 1531 | } |
| 1532 | |
| 1533 | static void pinInternedStrings(void) |
| 1534 | { |
| 1535 | HashTable *table; |
| 1536 | HashEntry *entry; |
| 1537 | Object *obj; |
| 1538 | int i; |
| 1539 | |
| 1540 | table = gDvm.internedStrings; |
| 1541 | if (table == NULL) { |
| 1542 | return; |
| 1543 | } |
| 1544 | dvmHashTableLock(table); |
| 1545 | for (i = 0; i < table->tableSize; ++i) { |
| 1546 | entry = &table->pEntries[i]; |
| 1547 | obj = (Object *)entry->data; |
| 1548 | if (obj == NULL || obj == HASH_TOMBSTONE) { |
| 1549 | continue; |
| 1550 | } else if (isPermanentString((StringObject *)obj)) { |
| 1551 | obj = (Object *)getPermanentString((StringObject*)obj); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1552 | LOG_PROM(">>> pin string obj=%p", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1553 | pinObject(obj); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1554 | LOG_PROM("<<< pin string obj=%p", obj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | dvmHashTableUnlock(table); |
| 1558 | } |
| 1559 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1560 | /* |
| 1561 | * At present, reference tables contain references that must not be |
| 1562 | * moved by the collector. Instead of scavenging each reference in |
| 1563 | * the table we pin each referenced object. |
| 1564 | */ |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1565 | static void pinReferenceTable(const ReferenceTable *table) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1566 | { |
| 1567 | Object **entry; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1568 | |
| 1569 | assert(table != NULL); |
| 1570 | assert(table->table != NULL); |
| 1571 | assert(table->nextEntry != NULL); |
| 1572 | for (entry = table->table; entry < table->nextEntry; ++entry) { |
| 1573 | assert(entry != NULL); |
| 1574 | assert(!isForward(*entry)); |
| 1575 | pinObject(*entry); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1579 | static void scavengeLargeHeapRefTable(LargeHeapRefTable *table, bool stripLowBits) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1580 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1581 | for (; table != NULL; table = table->next) { |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1582 | Object **ref = table->refs.table; |
| 1583 | for (; ref < table->refs.nextEntry; ++ref) { |
| 1584 | if (stripLowBits) { |
| 1585 | Object *obj = (Object *)((uintptr_t)*ref & ~3); |
| 1586 | scavengeReference(&obj); |
| 1587 | *ref = (Object *)((uintptr_t)obj | ((uintptr_t)*ref & 3)); |
| 1588 | } else { |
| 1589 | scavengeReference(ref); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1590 | } |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1595 | /* This code was copied from Thread.c */ |
| 1596 | static void scavengeThreadStack(Thread *thread) |
| 1597 | { |
| 1598 | const u4 *framePtr; |
| 1599 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1600 | bool first = true; |
| 1601 | #endif |
| 1602 | |
| 1603 | framePtr = (const u4 *)thread->curFrame; |
| 1604 | while (framePtr != NULL) { |
| 1605 | const StackSaveArea *saveArea; |
| 1606 | const Method *method; |
| 1607 | |
| 1608 | saveArea = SAVEAREA_FROM_FP(framePtr); |
| 1609 | method = saveArea->method; |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1610 | if (method != NULL && !dvmIsNativeMethod(method)) { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1611 | #ifdef COUNT_PRECISE_METHODS |
| 1612 | /* the GC is running, so no lock required */ |
| 1613 | if (dvmPointerSetAddEntry(gDvm.preciseMethods, method)) |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1614 | LOG_SCAV("PGC: added %s.%s %p\n", |
| 1615 | method->clazz->descriptor, method->name, method); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1616 | #endif |
| 1617 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1618 | /* |
| 1619 | * May also want to enable the memset() in the "invokeMethod" |
| 1620 | * goto target in the portable interpreter. That sets the stack |
| 1621 | * to a pattern that makes referring to uninitialized data |
| 1622 | * very obvious. |
| 1623 | */ |
| 1624 | |
| 1625 | if (first) { |
| 1626 | /* |
| 1627 | * First frame, isn't native, check the "alternate" saved PC |
| 1628 | * as a sanity check. |
| 1629 | * |
| 1630 | * It seems like we could check the second frame if the first |
| 1631 | * is native, since the PCs should be the same. It turns out |
| 1632 | * this doesn't always work. The problem is that we could |
| 1633 | * have calls in the sequence: |
| 1634 | * interp method #2 |
| 1635 | * native method |
| 1636 | * interp method #1 |
| 1637 | * |
| 1638 | * and then GC while in the native method after returning |
| 1639 | * from interp method #2. The currentPc on the stack is |
| 1640 | * for interp method #1, but thread->currentPc2 is still |
| 1641 | * set for the last thing interp method #2 did. |
| 1642 | * |
| 1643 | * This can also happen in normal execution: |
| 1644 | * - sget-object on not-yet-loaded class |
| 1645 | * - class init updates currentPc2 |
| 1646 | * - static field init is handled by parsing annotations; |
| 1647 | * static String init requires creation of a String object, |
| 1648 | * which can cause a GC |
| 1649 | * |
| 1650 | * Essentially, any pattern that involves executing |
| 1651 | * interpreted code and then causes an allocation without |
| 1652 | * executing instructions in the original method will hit |
| 1653 | * this. These are rare enough that the test still has |
| 1654 | * some value. |
| 1655 | */ |
| 1656 | if (saveArea->xtra.currentPc != thread->currentPc2) { |
| 1657 | LOGW("PGC: savedPC(%p) != current PC(%p), %s.%s ins=%p\n", |
| 1658 | saveArea->xtra.currentPc, thread->currentPc2, |
| 1659 | method->clazz->descriptor, method->name, method->insns); |
| 1660 | if (saveArea->xtra.currentPc != NULL) |
| 1661 | LOGE(" pc inst = 0x%04x\n", *saveArea->xtra.currentPc); |
| 1662 | if (thread->currentPc2 != NULL) |
| 1663 | LOGE(" pc2 inst = 0x%04x\n", *thread->currentPc2); |
| 1664 | dvmDumpThread(thread, false); |
| 1665 | } |
| 1666 | } else { |
| 1667 | /* |
| 1668 | * It's unusual, but not impossible, for a non-first frame |
| 1669 | * to be at something other than a method invocation. For |
| 1670 | * example, if we do a new-instance on a nonexistent class, |
| 1671 | * we'll have a lot of class loader activity on the stack |
| 1672 | * above the frame with the "new" operation. Could also |
| 1673 | * happen while we initialize a Throwable when an instruction |
| 1674 | * fails. |
| 1675 | * |
| 1676 | * So there's not much we can do here to verify the PC, |
| 1677 | * except to verify that it's a GC point. |
| 1678 | */ |
| 1679 | } |
| 1680 | assert(saveArea->xtra.currentPc != NULL); |
| 1681 | #endif |
| 1682 | |
| 1683 | const RegisterMap* pMap; |
| 1684 | const u1* regVector; |
| 1685 | int i; |
| 1686 | |
| 1687 | Method* nonConstMethod = (Method*) method; // quiet gcc |
| 1688 | pMap = dvmGetExpandedRegisterMap(nonConstMethod); |
| 1689 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1690 | //LOG_SCAV("PGC: %s.%s\n", method->clazz->descriptor, method->name); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1691 | |
| 1692 | if (pMap != NULL) { |
| 1693 | /* found map, get registers for this address */ |
| 1694 | int addr = saveArea->xtra.currentPc - method->insns; |
| 1695 | regVector = dvmRegisterMapGetLine(pMap, addr); |
| 1696 | /* |
| 1697 | if (regVector == NULL) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1698 | LOG_SCAV("PGC: map but no entry for %s.%s addr=0x%04x\n", |
| 1699 | method->clazz->descriptor, method->name, addr); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1700 | } else { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1701 | LOG_SCAV("PGC: found map for %s.%s 0x%04x (t=%d)\n", |
| 1702 | method->clazz->descriptor, method->name, addr, |
| 1703 | thread->threadId); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1704 | } |
| 1705 | */ |
| 1706 | } else { |
| 1707 | /* |
| 1708 | * No map found. If precise GC is disabled this is |
| 1709 | * expected -- we don't create pointers to the map data even |
| 1710 | * if it's present -- but if it's enabled it means we're |
| 1711 | * unexpectedly falling back on a conservative scan, so it's |
| 1712 | * worth yelling a little. |
| 1713 | */ |
| 1714 | if (gDvm.preciseGc) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1715 | 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] | 1716 | } |
| 1717 | regVector = NULL; |
| 1718 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1719 | if (regVector == NULL) { |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1720 | /* |
| 1721 | * There are no roots to scavenge. Skip over the entire frame. |
| 1722 | */ |
| 1723 | framePtr += method->registersSize; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1724 | } else { |
| 1725 | /* |
| 1726 | * Precise scan. v0 is at the lowest address on the |
| 1727 | * interpreted stack, and is the first bit in the register |
| 1728 | * vector, so we can walk through the register map and |
| 1729 | * memory in the same direction. |
| 1730 | * |
| 1731 | * A '1' bit indicates a live reference. |
| 1732 | */ |
| 1733 | u2 bits = 1 << 1; |
| 1734 | for (i = method->registersSize - 1; i >= 0; i--) { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1735 | u4 rval = *framePtr; |
| 1736 | |
| 1737 | bits >>= 1; |
| 1738 | if (bits == 1) { |
| 1739 | /* set bit 9 so we can tell when we're empty */ |
| 1740 | bits = *regVector++ | 0x0100; |
| 1741 | LOGVV("loaded bits: 0x%02x\n", bits & 0xff); |
| 1742 | } |
| 1743 | |
| 1744 | if (rval != 0 && (bits & 0x01) != 0) { |
| 1745 | /* |
| 1746 | * Non-null, register marked as live reference. This |
| 1747 | * should always be a valid object. |
| 1748 | */ |
| 1749 | #if WITH_EXTRA_GC_CHECKS > 0 |
| 1750 | if ((rval & 0x3) != 0 || !dvmIsValidObject((Object*) rval)) { |
| 1751 | /* this is very bad */ |
| 1752 | LOGE("PGC: invalid ref in reg %d: 0x%08x\n", |
| 1753 | method->registersSize-1 - i, rval); |
| 1754 | } else |
| 1755 | #endif |
| 1756 | { |
| 1757 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1758 | // LOG_SCAV("stack reference %u@%p", *framePtr, framePtr); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1759 | /* dvmMarkObjectNonNull((Object *)rval); */ |
| 1760 | scavengeReference((Object **) framePtr); |
| 1761 | } |
| 1762 | } else { |
| 1763 | /* |
| 1764 | * Null or non-reference, do nothing at all. |
| 1765 | */ |
| 1766 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1767 | if (dvmIsValidObject((Object*) rval)) { |
| 1768 | /* this is normal, but we feel chatty */ |
| 1769 | LOGD("PGC: ignoring valid ref in reg %d: 0x%08x\n", |
| 1770 | method->registersSize-1 - i, rval); |
| 1771 | } |
| 1772 | #endif |
| 1773 | } |
| 1774 | ++framePtr; |
| 1775 | } |
| 1776 | dvmReleaseRegisterMapLine(pMap, regVector); |
| 1777 | } |
| 1778 | } |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1779 | /* 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] | 1780 | * this is a native method and the registers are just the "ins", |
| 1781 | * copied from various registers in the caller's set. |
| 1782 | */ |
| 1783 | |
| 1784 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 1785 | first = false; |
| 1786 | #endif |
| 1787 | |
| 1788 | /* Don't fall into an infinite loop if things get corrupted. |
| 1789 | */ |
| 1790 | assert((uintptr_t)saveArea->prevFrame > (uintptr_t)framePtr || |
| 1791 | saveArea->prevFrame == NULL); |
| 1792 | framePtr = saveArea->prevFrame; |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | static void scavengeThread(Thread *thread) |
| 1797 | { |
| 1798 | assert(thread->status != THREAD_RUNNING || |
| 1799 | thread->isSuspended || |
| 1800 | thread == dvmThreadSelf()); |
| 1801 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1802 | // LOG_SCAV("scavengeThread(thread=%p)", thread); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1803 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1804 | // LOG_SCAV("Scavenging threadObj=%p", thread->threadObj); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1805 | scavengeReference(&thread->threadObj); |
| 1806 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1807 | // LOG_SCAV("Scavenging exception=%p", thread->exception); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1808 | scavengeReference(&thread->exception); |
| 1809 | |
| 1810 | scavengeThreadStack(thread); |
| 1811 | } |
| 1812 | |
| 1813 | static void scavengeThreadList(void) |
| 1814 | { |
| 1815 | Thread *thread; |
| 1816 | |
| 1817 | dvmLockThreadList(dvmThreadSelf()); |
| 1818 | thread = gDvm.threadList; |
| 1819 | while (thread) { |
| 1820 | scavengeThread(thread); |
| 1821 | thread = thread->next; |
| 1822 | } |
| 1823 | dvmUnlockThreadList(); |
| 1824 | } |
| 1825 | |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1826 | static void pinThreadStack(const Thread *thread) |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1827 | { |
| 1828 | const u4 *framePtr; |
| 1829 | const StackSaveArea *saveArea; |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1830 | Method *method; |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1831 | const char *shorty; |
| 1832 | Object *obj; |
| 1833 | int i; |
| 1834 | |
| 1835 | saveArea = NULL; |
| 1836 | framePtr = (const u4 *)thread->curFrame; |
| 1837 | for (; framePtr != NULL; framePtr = saveArea->prevFrame) { |
| 1838 | saveArea = SAVEAREA_FROM_FP(framePtr); |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1839 | method = (Method *)saveArea->method; |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1840 | if (method != NULL && dvmIsNativeMethod(method)) { |
| 1841 | /* |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1842 | * This is native method, pin its arguments. |
| 1843 | * |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 1844 | * For purposes of graying references, we don't need to do |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1845 | * anything here, because all of the native "ins" were copied |
| 1846 | * from registers in the caller's stack frame and won't be |
| 1847 | * changed (an interpreted method can freely use registers |
| 1848 | * with parameters like any other register, but natives don't |
| 1849 | * work that way). |
| 1850 | * |
| 1851 | * However, we need to ensure that references visible to |
| 1852 | * native methods don't move around. We can do a precise scan |
| 1853 | * of the arguments by examining the method signature. |
| 1854 | */ |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1855 | LOG_PIN("+++ native scan %s.%s\n", |
| 1856 | method->clazz->descriptor, method->name); |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1857 | assert(method->registersSize == method->insSize); |
| 1858 | if (!dvmIsStaticMethod(method)) { |
| 1859 | /* grab the "this" pointer */ |
| 1860 | obj = (Object *)*framePtr++; |
| 1861 | if (obj == NULL) { |
| 1862 | /* |
| 1863 | * This can happen for the "fake" entry frame inserted |
| 1864 | * for threads created outside the VM. There's no actual |
| 1865 | * call so there's no object. If we changed the fake |
| 1866 | * entry method to be declared "static" then this |
| 1867 | * situation should never occur. |
| 1868 | */ |
| 1869 | } else { |
| 1870 | assert(dvmIsValidObject(obj)); |
| 1871 | pinObject(obj); |
| 1872 | } |
| 1873 | } |
| 1874 | shorty = method->shorty+1; // skip return value |
| 1875 | for (i = method->registersSize - 1; i >= 0; i--, framePtr++) { |
| 1876 | switch (*shorty++) { |
| 1877 | case 'L': |
| 1878 | obj = (Object *)*framePtr; |
| 1879 | if (obj != NULL) { |
| 1880 | assert(dvmIsValidObject(obj)); |
| 1881 | pinObject(obj); |
| 1882 | } |
| 1883 | break; |
| 1884 | case 'D': |
| 1885 | case 'J': |
| 1886 | framePtr++; |
| 1887 | break; |
| 1888 | default: |
| 1889 | /* 32-bit non-reference value */ |
| 1890 | obj = (Object *)*framePtr; // debug, remove |
| 1891 | if (dvmIsValidObject(obj)) { // debug, remove |
| 1892 | /* if we see a lot of these, our scan might be off */ |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1893 | LOG_PIN("+++ did NOT pin obj %p\n", obj); |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1894 | } |
| 1895 | break; |
| 1896 | } |
| 1897 | } |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1898 | } else if (method != NULL && !dvmIsNativeMethod(method)) { |
| 1899 | const RegisterMap* pMap = dvmGetExpandedRegisterMap(method); |
| 1900 | const u1* regVector = NULL; |
| 1901 | |
| 1902 | LOGI("conservative : %s.%s\n", method->clazz->descriptor, method->name); |
| 1903 | |
| 1904 | if (pMap != NULL) { |
| 1905 | int addr = saveArea->xtra.currentPc - method->insns; |
| 1906 | regVector = dvmRegisterMapGetLine(pMap, addr); |
| 1907 | } |
| 1908 | if (regVector == NULL) { |
| 1909 | /* |
| 1910 | * No register info for this frame, conservatively pin. |
| 1911 | */ |
| 1912 | for (i = 0; i < method->registersSize; ++i) { |
| 1913 | u4 regValue = framePtr[i]; |
| 1914 | if (regValue != 0 && (regValue & 0x3) == 0 && dvmIsValidObject((Object *)regValue)) { |
| 1915 | pinObject((Object *)regValue); |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1919 | } |
| 1920 | /* |
| 1921 | * Don't fall into an infinite loop if things get corrupted. |
| 1922 | */ |
| 1923 | assert((uintptr_t)saveArea->prevFrame > (uintptr_t)framePtr || |
| 1924 | saveArea->prevFrame == NULL); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | static void pinThread(const Thread *thread) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1929 | { |
| 1930 | assert(thread != NULL); |
| 1931 | assert(thread->status != THREAD_RUNNING || |
| 1932 | thread->isSuspended || |
| 1933 | thread == dvmThreadSelf()); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1934 | LOG_PIN("pinThread(thread=%p)", thread); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1935 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1936 | LOG_PIN("Pin native method arguments"); |
| Carl Shapiro | 88b0035 | 2010-05-19 17:38:33 -0700 | [diff] [blame] | 1937 | pinThreadStack(thread); |
| Carl Shapiro | 583d64c | 2010-05-04 10:44:47 -0700 | [diff] [blame] | 1938 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1939 | LOG_PIN("Pin internalLocalRefTable"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1940 | pinReferenceTable(&thread->internalLocalRefTable); |
| 1941 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1942 | LOG_PIN("Pin jniLocalRefTable"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1943 | pinReferenceTable(&thread->jniLocalRefTable); |
| 1944 | |
| 1945 | /* Can the check be pushed into the promote routine? */ |
| 1946 | if (thread->jniMonitorRefTable.table) { |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1947 | LOG_PIN("Pin jniMonitorRefTable"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1948 | pinReferenceTable(&thread->jniMonitorRefTable); |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | static void pinThreadList(void) |
| 1953 | { |
| 1954 | Thread *thread; |
| 1955 | |
| 1956 | dvmLockThreadList(dvmThreadSelf()); |
| 1957 | thread = gDvm.threadList; |
| 1958 | while (thread) { |
| 1959 | pinThread(thread); |
| 1960 | thread = thread->next; |
| 1961 | } |
| 1962 | dvmUnlockThreadList(); |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * Heap block scavenging. |
| 1967 | */ |
| 1968 | |
| 1969 | /* |
| 1970 | * Scavenge objects in the current block. Scavenging terminates when |
| 1971 | * the pointer reaches the highest address in the block or when a run |
| 1972 | * of zero words that continues to the highest address is reached. |
| 1973 | */ |
| 1974 | static void scavengeBlock(HeapSource *heapSource, size_t block) |
| 1975 | { |
| 1976 | u1 *cursor; |
| 1977 | u1 *end; |
| 1978 | size_t size; |
| 1979 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1980 | LOG_SCAV("scavengeBlock(heapSource=%p,block=%zu)", heapSource, block); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1981 | |
| 1982 | assert(heapSource != NULL); |
| 1983 | assert(block < heapSource->totalBlocks); |
| 1984 | assert(heapSource->blockSpace[block] == BLOCK_TO_SPACE); |
| 1985 | |
| 1986 | cursor = blockToAddress(heapSource, block); |
| 1987 | end = cursor + BLOCK_SIZE; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 1988 | LOG_SCAV("scavengeBlock start=%p, end=%p", cursor, end); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1989 | |
| 1990 | /* Parse and scavenge the current block. */ |
| 1991 | size = 0; |
| 1992 | while (cursor < end) { |
| 1993 | u4 word = *(u4 *)cursor; |
| 1994 | if (word != 0) { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 1995 | scavengeObject((Object *)cursor); |
| 1996 | size = objectSize((Object *)cursor); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 1997 | size = alignUp(size, ALLOC_ALIGNMENT); |
| 1998 | cursor += size; |
| 1999 | } else if (word == 0 && cursor == (u1 *)gDvm.unlinkedJavaLangClass) { |
| 2000 | size = sizeof(ClassObject); |
| 2001 | cursor += size; |
| 2002 | } else { |
| 2003 | /* Check for padding. */ |
| 2004 | while (*(u4 *)cursor == 0) { |
| 2005 | cursor += 4; |
| 2006 | if (cursor == end) break; |
| 2007 | } |
| 2008 | /* Punt if something went wrong. */ |
| 2009 | assert(cursor == end); |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2014 | static size_t objectSize(const Object *obj) |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2015 | { |
| 2016 | size_t size; |
| 2017 | |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2018 | assert(obj != NULL); |
| 2019 | assert(obj->clazz != NULL); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2020 | if (obj->clazz == gDvm.classJavaLangClass || |
| 2021 | obj->clazz == gDvm.unlinkedJavaLangClass) { |
| 2022 | size = dvmClassObjectSize((ClassObject *)obj); |
| 2023 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
| 2024 | size = dvmArrayObjectSize((ArrayObject *)obj); |
| 2025 | } else { |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2026 | assert(obj->clazz->objectSize != 0); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2027 | size = obj->clazz->objectSize; |
| 2028 | } |
| Carl Shapiro | 2396fda | 2010-05-03 20:14:14 -0700 | [diff] [blame] | 2029 | if (LW_HASH_STATE(obj->lock) == LW_HASH_STATE_HASHED_AND_MOVED) { |
| 2030 | size += sizeof(u4); |
| 2031 | } |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2032 | return size; |
| 2033 | } |
| 2034 | |
| 2035 | static void verifyBlock(HeapSource *heapSource, size_t block) |
| 2036 | { |
| 2037 | u1 *cursor; |
| 2038 | u1 *end; |
| 2039 | size_t size; |
| 2040 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2041 | // LOG_VER("verifyBlock(heapSource=%p,block=%zu)", heapSource, block); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2042 | |
| 2043 | assert(heapSource != NULL); |
| 2044 | assert(block < heapSource->totalBlocks); |
| 2045 | assert(heapSource->blockSpace[block] == BLOCK_TO_SPACE); |
| 2046 | |
| 2047 | cursor = blockToAddress(heapSource, block); |
| 2048 | end = cursor + BLOCK_SIZE; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2049 | // LOG_VER("verifyBlock start=%p, end=%p", cursor, end); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2050 | |
| 2051 | /* Parse and scavenge the current block. */ |
| 2052 | size = 0; |
| 2053 | while (cursor < end) { |
| 2054 | u4 word = *(u4 *)cursor; |
| 2055 | if (word != 0) { |
| 2056 | dvmVerifyObject((Object *)cursor); |
| 2057 | size = objectSize((Object *)cursor); |
| 2058 | size = alignUp(size, ALLOC_ALIGNMENT); |
| 2059 | cursor += size; |
| 2060 | } else if (word == 0 && cursor == (u1 *)gDvm.unlinkedJavaLangClass) { |
| 2061 | size = sizeof(ClassObject); |
| 2062 | cursor += size; |
| 2063 | } else { |
| 2064 | /* Check for padding. */ |
| 2065 | while (*(unsigned long *)cursor == 0) { |
| 2066 | cursor += 4; |
| 2067 | if (cursor == end) break; |
| 2068 | } |
| 2069 | /* Punt if something went wrong. */ |
| 2070 | assert(cursor == end); |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | static void describeBlockQueue(const HeapSource *heapSource) |
| 2076 | { |
| 2077 | size_t block, count; |
| 2078 | char space; |
| 2079 | |
| 2080 | block = heapSource->queueHead; |
| 2081 | count = 0; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2082 | LOG_SCAV(">>> describeBlockQueue(heapSource=%p)", heapSource); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2083 | /* Count the number of blocks enqueued. */ |
| 2084 | while (block != QUEUE_TAIL) { |
| 2085 | block = heapSource->blockQueue[block]; |
| 2086 | ++count; |
| 2087 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2088 | LOG_SCAV("blockQueue %zu elements, enqueued %zu", |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2089 | count, heapSource->queueSize); |
| 2090 | block = heapSource->queueHead; |
| 2091 | while (block != QUEUE_TAIL) { |
| 2092 | space = heapSource->blockSpace[block]; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2093 | LOG_SCAV("block=%zu@%p,space=%zu", block, blockToAddress(heapSource,block), space); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2094 | block = heapSource->blockQueue[block]; |
| 2095 | } |
| 2096 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2097 | LOG_SCAV("<<< describeBlockQueue(heapSource=%p)", heapSource); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | /* |
| 2101 | * Blackens promoted objects. |
| 2102 | */ |
| 2103 | static void scavengeBlockQueue(void) |
| 2104 | { |
| 2105 | HeapSource *heapSource; |
| 2106 | size_t block; |
| 2107 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2108 | LOG_SCAV(">>> scavengeBlockQueue()"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2109 | heapSource = gDvm.gcHeap->heapSource; |
| 2110 | describeBlockQueue(heapSource); |
| 2111 | while (heapSource->queueHead != QUEUE_TAIL) { |
| 2112 | block = heapSource->queueHead; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2113 | LOG_SCAV("Dequeueing block %zu\n", block); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2114 | scavengeBlock(heapSource, block); |
| 2115 | heapSource->queueHead = heapSource->blockQueue[block]; |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2116 | LOG_SCAV("New queue head is %zu\n", heapSource->queueHead); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2117 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2118 | LOG_SCAV("<<< scavengeBlockQueue()"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * Scan the block list and verify all blocks that are marked as being |
| 2123 | * in new space. This should be parametrized so we can invoke this |
| 2124 | * routine outside of the context of a collection. |
| 2125 | */ |
| 2126 | static void verifyNewSpace(void) |
| 2127 | { |
| 2128 | HeapSource *heapSource; |
| 2129 | size_t i; |
| 2130 | size_t c0, c1, c2, c7; |
| 2131 | |
| 2132 | c0 = c1 = c2 = c7 = 0; |
| 2133 | heapSource = gDvm.gcHeap->heapSource; |
| 2134 | for (i = 0; i < heapSource->totalBlocks; ++i) { |
| 2135 | switch (heapSource->blockSpace[i]) { |
| 2136 | case BLOCK_FREE: ++c0; break; |
| 2137 | case BLOCK_TO_SPACE: ++c1; break; |
| 2138 | case BLOCK_FROM_SPACE: ++c2; break; |
| 2139 | case BLOCK_CONTINUED: ++c7; break; |
| 2140 | default: assert(!"reached"); |
| 2141 | } |
| 2142 | } |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2143 | LOG_VER("Block Demographics: " |
| 2144 | "Free=%zu,ToSpace=%zu,FromSpace=%zu,Continued=%zu", |
| 2145 | c0, c1, c2, c7); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2146 | for (i = 0; i < heapSource->totalBlocks; ++i) { |
| 2147 | if (heapSource->blockSpace[i] != BLOCK_TO_SPACE) { |
| 2148 | continue; |
| 2149 | } |
| 2150 | verifyBlock(heapSource, i); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | static void scavengeGlobals(void) |
| 2155 | { |
| 2156 | scavengeReference((Object **)(void *)&gDvm.classJavaLangClass); |
| 2157 | scavengeReference((Object **)(void *)&gDvm.classJavaLangClassArray); |
| 2158 | scavengeReference((Object **)(void *)&gDvm.classJavaLangError); |
| 2159 | scavengeReference((Object **)(void *)&gDvm.classJavaLangObject); |
| 2160 | scavengeReference((Object **)(void *)&gDvm.classJavaLangObjectArray); |
| 2161 | scavengeReference((Object **)(void *)&gDvm.classJavaLangRuntimeException); |
| 2162 | scavengeReference((Object **)(void *)&gDvm.classJavaLangString); |
| 2163 | scavengeReference((Object **)(void *)&gDvm.classJavaLangThread); |
| 2164 | scavengeReference((Object **)(void *)&gDvm.classJavaLangVMThread); |
| 2165 | scavengeReference((Object **)(void *)&gDvm.classJavaLangThreadGroup); |
| 2166 | scavengeReference((Object **)(void *)&gDvm.classJavaLangThrowable); |
| 2167 | scavengeReference((Object **)(void *)&gDvm.classJavaLangStackTraceElement); |
| 2168 | scavengeReference((Object **)(void *)&gDvm.classJavaLangStackTraceElementArray); |
| 2169 | scavengeReference((Object **)(void *)&gDvm.classJavaLangAnnotationAnnotationArray); |
| 2170 | scavengeReference((Object **)(void *)&gDvm.classJavaLangAnnotationAnnotationArrayArray); |
| 2171 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectAccessibleObject); |
| 2172 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectConstructor); |
| 2173 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectConstructorArray); |
| 2174 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectField); |
| 2175 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectFieldArray); |
| 2176 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectMethod); |
| 2177 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectMethodArray); |
| 2178 | scavengeReference((Object **)(void *)&gDvm.classJavaLangReflectProxy); |
| 2179 | scavengeReference((Object **)(void *)&gDvm.classJavaLangExceptionInInitializerError); |
| 2180 | scavengeReference((Object **)(void *)&gDvm.classJavaLangRefReference); |
| 2181 | scavengeReference((Object **)(void *)&gDvm.classJavaNioReadWriteDirectByteBuffer); |
| 2182 | scavengeReference((Object **)(void *)&gDvm.classJavaSecurityAccessController); |
| 2183 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyLangAnnotationAnnotationFactory); |
| 2184 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyLangAnnotationAnnotationMember); |
| 2185 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyLangAnnotationAnnotationMemberArray); |
| 2186 | scavengeReference((Object **)(void *)&gDvm.classOrgApacheHarmonyNioInternalDirectBuffer); |
| 2187 | scavengeReference((Object **)(void *)&gDvm.classArrayBoolean); |
| 2188 | scavengeReference((Object **)(void *)&gDvm.classArrayChar); |
| 2189 | scavengeReference((Object **)(void *)&gDvm.classArrayFloat); |
| 2190 | scavengeReference((Object **)(void *)&gDvm.classArrayDouble); |
| 2191 | scavengeReference((Object **)(void *)&gDvm.classArrayByte); |
| 2192 | scavengeReference((Object **)(void *)&gDvm.classArrayShort); |
| 2193 | scavengeReference((Object **)(void *)&gDvm.classArrayInt); |
| 2194 | scavengeReference((Object **)(void *)&gDvm.classArrayLong); |
| 2195 | } |
| 2196 | |
| 2197 | void describeHeap(void) |
| 2198 | { |
| 2199 | HeapSource *heapSource; |
| 2200 | |
| 2201 | heapSource = gDvm.gcHeap->heapSource; |
| 2202 | describeBlocks(heapSource); |
| 2203 | } |
| 2204 | |
| 2205 | /* |
| 2206 | * The collection interface. Collection has a few distinct phases. |
| 2207 | * The first is flipping AKA condemning AKA whitening the heap. The |
| 2208 | * second is to promote all objects which are pointed to by pinned or |
| 2209 | * ambiguous references. The third phase is tracing from the stacks, |
| 2210 | * registers and various globals. Lastly, a verification of the heap |
| 2211 | * is performed. The last phase should be optional. |
| 2212 | */ |
| 2213 | void dvmScavengeRoots(void) /* Needs a new name badly */ |
| 2214 | { |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2215 | GcHeap *gcHeap; |
| 2216 | |
| 2217 | { |
| 2218 | size_t alloc, unused, total; |
| 2219 | |
| 2220 | room(&alloc, &unused, &total); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2221 | LOG_SCAV("BEFORE GC: %zu alloc, %zu free, %zu total.", |
| 2222 | alloc, unused, total); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | gcHeap = gDvm.gcHeap; |
| 2226 | dvmHeapSourceFlip(); |
| 2227 | |
| 2228 | /* |
| 2229 | * Promote blocks with stationary objects. |
| 2230 | */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2231 | pinThreadList(); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2232 | pinReferenceTable(&gDvm.jniGlobalRefTable); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2233 | pinReferenceTable(&gDvm.jniPinRefTable); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2234 | pinReferenceTable(&gcHeap->nonCollectableRefs); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2235 | pinHashTableEntries(gDvm.loadedClasses); |
| Carl Shapiro | 427bf46 | 2010-06-04 00:03:18 -0700 | [diff] [blame^] | 2236 | pinHashTableEntries(gDvm.dbgRegistry); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2237 | pinPrimitiveClasses(); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2238 | pinInternedStrings(); |
| 2239 | |
| 2240 | // describeBlocks(gcHeap->heapSource); |
| 2241 | |
| 2242 | /* |
| 2243 | * Create first, open new-space page right here. |
| 2244 | */ |
| 2245 | |
| 2246 | /* Reset allocation to an unallocated block. */ |
| 2247 | gDvm.gcHeap->heapSource->allocPtr = allocateBlocks(gDvm.gcHeap->heapSource, 1); |
| 2248 | gDvm.gcHeap->heapSource->allocLimit = gDvm.gcHeap->heapSource->allocPtr + BLOCK_SIZE; |
| 2249 | /* |
| 2250 | * Hack: promote the empty block allocated above. If the |
| 2251 | * promotions that occurred above did not actually gray any |
| 2252 | * objects, the block queue may be empty. We must force a |
| 2253 | * promotion to be safe. |
| 2254 | */ |
| 2255 | promoteBlockByAddr(gDvm.gcHeap->heapSource, gDvm.gcHeap->heapSource->allocPtr); |
| 2256 | |
| 2257 | /* |
| 2258 | * Scavenge blocks and relocate movable objects. |
| 2259 | */ |
| 2260 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2261 | LOG_SCAV("Scavenging gDvm.threadList"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2262 | scavengeThreadList(); |
| 2263 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2264 | LOG_SCAV("Scavenging gDvm.gcHeap->referenceOperations"); |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 2265 | scavengeLargeHeapRefTable(gcHeap->referenceOperations, true); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2266 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2267 | LOG_SCAV("Scavenging gDvm.gcHeap->pendingFinalizationRefs"); |
| Carl Shapiro | 7800c09 | 2010-05-11 13:46:29 -0700 | [diff] [blame] | 2268 | scavengeLargeHeapRefTable(gcHeap->pendingFinalizationRefs, false); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2269 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2270 | LOG_SCAV("Scavenging random global stuff"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2271 | scavengeReference(&gDvm.outOfMemoryObj); |
| 2272 | scavengeReference(&gDvm.internalErrorObj); |
| 2273 | scavengeReference(&gDvm.noClassDefFoundErrorObj); |
| 2274 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2275 | // LOG_SCAV("Scavenging gDvm.internedString"); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2276 | scavengeInternedStrings(); |
| 2277 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2278 | LOG_SCAV("Root scavenge has completed."); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2279 | |
| 2280 | scavengeBlockQueue(); |
| 2281 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2282 | LOG_SCAV("Re-snap global class pointers."); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2283 | scavengeGlobals(); |
| 2284 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2285 | LOG_SCAV("New space scavenge has completed."); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2286 | |
| 2287 | /* |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2288 | * Process reference objects in strength order. |
| 2289 | */ |
| 2290 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2291 | LOG_REF("Processing soft references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2292 | preserveSoftReferences(&gDvm.gcHeap->softReferences); |
| 2293 | clearWhiteReferences(&gDvm.gcHeap->softReferences); |
| 2294 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2295 | LOG_REF("Processing weak references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2296 | clearWhiteReferences(&gDvm.gcHeap->weakReferences); |
| 2297 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2298 | LOG_REF("Finding finalizations..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2299 | processFinalizableReferences(); |
| 2300 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2301 | LOG_REF("Processing f-reachable soft references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2302 | clearWhiteReferences(&gDvm.gcHeap->softReferences); |
| 2303 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2304 | LOG_REF("Processing f-reachable weak references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2305 | clearWhiteReferences(&gDvm.gcHeap->weakReferences); |
| 2306 | |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2307 | LOG_REF("Processing phantom references..."); |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2308 | clearWhiteReferences(&gDvm.gcHeap->phantomReferences); |
| 2309 | |
| 2310 | /* |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2311 | * Verify the stack and heap. |
| 2312 | */ |
| Carl Shapiro | f571825 | 2010-05-11 20:55:13 -0700 | [diff] [blame] | 2313 | dvmVerifyRoots(); |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2314 | verifyNewSpace(); |
| 2315 | |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2316 | //describeBlocks(gcHeap->heapSource); |
| 2317 | |
| 2318 | clearFromSpace(gcHeap->heapSource); |
| 2319 | |
| 2320 | { |
| 2321 | size_t alloc, rem, total; |
| 2322 | |
| 2323 | room(&alloc, &rem, &total); |
| Carl Shapiro | 8bb533e | 2010-05-06 15:35:27 -0700 | [diff] [blame] | 2324 | 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] | 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | /* |
| 2329 | * Interface compatibility routines. |
| 2330 | */ |
| 2331 | |
| 2332 | void dvmClearWhiteRefs(Object **list) |
| 2333 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2334 | /* do nothing */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2335 | assert(*list == NULL); |
| 2336 | } |
| 2337 | |
| 2338 | void dvmHandleSoftRefs(Object **list) |
| 2339 | { |
| Carl Shapiro | 952e84a | 2010-05-06 14:35:29 -0700 | [diff] [blame] | 2340 | /* do nothing */ |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2341 | assert(*list == NULL); |
| 2342 | } |
| 2343 | |
| 2344 | bool dvmHeapBeginMarkStep(GcMode mode) |
| 2345 | { |
| 2346 | /* do nothing */ |
| 2347 | return true; |
| 2348 | } |
| 2349 | |
| 2350 | void dvmHeapFinishMarkStep(void) |
| 2351 | { |
| 2352 | /* do nothing */ |
| 2353 | } |
| 2354 | |
| 2355 | void dvmHeapMarkRootSet(void) |
| 2356 | { |
| 2357 | /* do nothing */ |
| 2358 | } |
| 2359 | |
| 2360 | void dvmHeapScanMarkedObjects(void) |
| 2361 | { |
| 2362 | dvmScavengeRoots(); |
| 2363 | } |
| 2364 | |
| 2365 | void dvmHeapScheduleFinalizations(void) |
| 2366 | { |
| 2367 | /* do nothing */ |
| 2368 | } |
| 2369 | |
| 2370 | void dvmHeapSweepUnmarkedObjects(GcMode mode, int *numFreed, size_t *sizeFreed) |
| 2371 | { |
| Carl Shapiro | 703a2f3 | 2010-05-12 23:11:37 -0700 | [diff] [blame] | 2372 | *numFreed = 0; |
| 2373 | *sizeFreed = 0; |
| Carl Shapiro | d28668c | 2010-04-15 16:10:00 -0700 | [diff] [blame] | 2374 | /* do nothing */ |
| 2375 | } |
| 2376 | |
| 2377 | void dvmMarkObjectNonNull(const Object *obj) |
| 2378 | { |
| 2379 | assert(!"implemented"); |
| 2380 | } |