Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "card_table.h" |
| 18 | |
| 19 | #include <sys/mman.h> /* for PROT_* */ |
| 20 | |
| 21 | #include "heap.h" |
| 22 | #include "heap_bitmap.h" |
| 23 | #include "logging.h" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 24 | #include "runtime.h" |
Elliott Hughes | f0605a0 | 2012-01-13 20:12:02 -0800 | [diff] [blame] | 25 | #include "utils.h" |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | /* |
| 29 | * Maintain a card table from the write barrier. All writes of |
| 30 | * non-NULL values to heap addresses should go through an entry in |
| 31 | * WriteBarrier, and from there to here. |
| 32 | * |
| 33 | * The heap is divided into "cards" of GC_CARD_SIZE bytes, as |
| 34 | * determined by GC_CARD_SHIFT. The card table contains one byte of |
| 35 | * data per card, to be used by the GC. The value of the byte will be |
| 36 | * one of GC_CARD_CLEAN or GC_CARD_DIRTY. |
| 37 | * |
| 38 | * After any store of a non-NULL object pointer into a heap object, |
| 39 | * code is obliged to mark the card dirty. The setters in |
| 40 | * object.h [such as SetFieldObject] do this for you. The |
| 41 | * compiler also contains code to mark cards as dirty. |
| 42 | * |
| 43 | * The card table's base [the "biased card table"] gets set to a |
| 44 | * rather strange value. In order to keep the JIT from having to |
| 45 | * fabricate or load GC_DIRTY_CARD to store into the card table, |
| 46 | * biased base is within the mmap allocation at a point where its low |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 47 | * byte is equal to GC_DIRTY_CARD. See CardTable::Create for details. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 50 | CardTable* CardTable::Create(const byte* heap_begin, size_t heap_capacity) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 51 | /* Set up the card table */ |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 52 | size_t capacity = heap_capacity / GC_CARD_SIZE; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 53 | /* Allocate an extra 256 bytes to allow fixed low-byte of base */ |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 54 | UniquePtr<MemMap> mem_map(MemMap::MapAnonymous("dalvik-card-table", NULL, |
| 55 | capacity + 256, PROT_READ | PROT_WRITE)); |
| 56 | if (mem_map.get() == NULL) { |
Elliott Hughes | f0605a0 | 2012-01-13 20:12:02 -0800 | [diff] [blame] | 57 | std::string maps; |
| 58 | ReadFileToString("/proc/self/maps", &maps); |
| 59 | LOG(FATAL) << "couldn't allocate card table\n" << maps; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 60 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 61 | // All zeros is the correct initial value; all clean. Anonymous mmaps are initialized to zero, we |
| 62 | // don't clear the card table to avoid unnecessary pages being allocated |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 63 | CHECK_EQ(GC_CARD_CLEAN, 0); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 64 | |
| 65 | byte* cardtable_begin = mem_map->Begin(); |
| 66 | CHECK(cardtable_begin != NULL); |
| 67 | |
| 68 | // We allocated up to a bytes worth of extra space to allow biased_begin's byte value to equal |
| 69 | // GC_CARD_DIRTY, compute a offset value to make this the case |
| 70 | size_t offset = 0; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 71 | byte* biased_begin = reinterpret_cast<byte*>(reinterpret_cast<uintptr_t>(cardtable_begin) - |
| 72 | (reinterpret_cast<uintptr_t>(heap_begin) >> GC_CARD_SHIFT)); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 73 | if (((uintptr_t)biased_begin & 0xff) != GC_CARD_DIRTY) { |
| 74 | int delta = GC_CARD_DIRTY - (reinterpret_cast<int>(biased_begin) & 0xff); |
| 75 | offset = delta + (delta < 0 ? 0x100 : 0); |
| 76 | biased_begin += offset; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 77 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 78 | CHECK_EQ(reinterpret_cast<int>(biased_begin) & 0xff, GC_CARD_DIRTY); |
| 79 | |
| 80 | return new CardTable(mem_map.release(), biased_begin, offset); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void CardTable::ClearCardTable() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 84 | // TODO: clear just the range of the table that has been modified |
| 85 | memset(mem_map_->Begin(), GC_CARD_CLEAN, mem_map_->Size()); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 88 | void CardTable::CheckAddrIsInCardTable(const byte* addr) const { |
| 89 | byte* cardAddr = biased_begin_ + ((uintptr_t)addr >> GC_CARD_SHIFT); |
| 90 | if (!IsValidCard(cardAddr)) { |
| 91 | byte* begin = mem_map_->Begin() + offset_; |
| 92 | byte* end = mem_map_->End(); |
| 93 | LOG(FATAL) << "Cardtable - begin: " << reinterpret_cast<void*>(begin) |
| 94 | << " end: " << reinterpret_cast<void*>(end) |
| 95 | << " addr: " << reinterpret_cast<const void*>(addr) |
| 96 | << " cardAddr: " << reinterpret_cast<void*>(cardAddr); |
| 97 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 100 | void CardTable::Scan(byte* heap_begin, byte* heap_end, Callback* visitor, void* arg) const { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 101 | Heap* heap = Runtime::Current()->GetHeap(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 102 | byte* card_cur = CardFromAddr(heap_begin); |
| 103 | byte* card_end = CardFromAddr(heap_end); |
| 104 | while (card_cur < card_end) { |
| 105 | while (card_cur < card_end && *card_cur == GC_CARD_CLEAN) { |
| 106 | card_cur++; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 107 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 108 | byte* run_start = card_cur; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 109 | size_t run = 0; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 110 | while (card_cur < card_end && *card_cur == GC_CARD_DIRTY) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 111 | run++; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 112 | card_cur++; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 113 | } |
| 114 | if (run > 0) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 115 | byte* run_end = &card_cur[run]; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 116 | heap->GetLiveBits()->VisitRange(reinterpret_cast<uintptr_t>(AddrFromCard(run_start)), |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 117 | reinterpret_cast<uintptr_t>(AddrFromCard(run_end)), |
| 118 | visitor, arg); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 123 | void CardTable::VerifyCardTable() { |
| 124 | UNIMPLEMENTED(WARNING) << "Card table verification"; |
| 125 | } |
| 126 | |
| 127 | } // namespace art |