Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2011 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. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 17 | #ifndef ART_SRC_GC_CARDTABLE_H_ |
| 18 | #define ART_SRC_GC_CARDTABLE_H_ |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 19 | |
| 20 | #include "globals.h" |
| 21 | #include "logging.h" |
| 22 | #include "mem_map.h" |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 23 | #include "space_bitmap.h" |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 24 | #include "UniquePtr.h" |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 25 | #include "utils.h" |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 29 | class Heap; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 30 | class ContinuousSpace; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 31 | class SpaceBitmap; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 32 | class Object; |
| 33 | |
Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 34 | // Maintain a card table from the the write barrier. All writes of |
| 35 | // non-NULL values to heap addresses should go through an entry in |
| 36 | // WriteBarrier, and from there to here. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 37 | class CardTable { |
| 38 | public: |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 39 | static const size_t kCardShift = 7; |
| 40 | static const size_t kCardSize = (1 << kCardShift); |
| 41 | static const uint8_t kCardClean = 0x0; |
| 42 | static const uint8_t kCardDirty = 0x70; |
| 43 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 44 | static CardTable* Create(const byte* heap_begin, size_t heap_capacity); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 45 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 46 | // Set the card associated with the given address to GC_CARD_DIRTY. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 47 | void MarkCard(const void *addr) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 48 | byte* card_addr = CardFromAddr(addr); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 49 | *card_addr = kCardDirty; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 52 | // Is the object on a dirty card? |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 53 | bool IsDirty(const Object* obj) const { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 54 | return *CardFromAddr(obj) == kCardDirty; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 57 | // Visit and clear cards within memory range, only visits dirty cards. |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 58 | template <typename Visitor> |
| 59 | void VisitClear(const void* start, const void* end, const Visitor& visitor) { |
| 60 | byte* card_start = CardFromAddr(start); |
| 61 | byte* card_end = CardFromAddr(end); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 62 | for (byte* it = card_start; it != card_end; ++it) { |
| 63 | if (*it == kCardDirty) { |
| 64 | *it = kCardClean; |
| 65 | visitor(it); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 70 | // Returns a value that when added to a heap address >> GC_CARD_SHIFT will address the appropriate |
| 71 | // card table byte. For convenience this value is cached in every Thread |
| 72 | byte* GetBiasedBegin() const { |
| 73 | return biased_begin_; |
jeffhao | 39da035 | 2011-11-04 14:58:55 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 76 | /* |
| 77 | * Visitor is expected to take in a card and return the new value. When a value is modified, the |
| 78 | * modify visitor is called. |
| 79 | * visitor: The visitor which modifies the cards. Returns the new value for a card given an old |
| 80 | * value. |
| 81 | * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables |
| 82 | * us to know which cards got cleared. |
| 83 | */ |
| 84 | template <typename Visitor, typename ModifiedVisitor> |
| 85 | void ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor, |
| 86 | const ModifiedVisitor& modified = VoidFunctor()) { |
| 87 | byte* card_cur = CardFromAddr(scan_begin); |
| 88 | byte* card_end = CardFromAddr(scan_end); |
| 89 | CheckCardValid(card_cur); |
| 90 | CheckCardValid(card_end); |
| 91 | |
| 92 | // Handle any unaligned cards at the start. |
| 93 | while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) { |
| 94 | byte expected, new_value; |
| 95 | do { |
| 96 | expected = *card_cur; |
| 97 | new_value = visitor(expected); |
| 98 | } while (expected != new_value && UNLIKELY(byte_cas(expected, new_value, card_cur) != 0)); |
| 99 | if (expected != new_value) { |
| 100 | modified(card_cur, expected, new_value); |
| 101 | } |
| 102 | ++card_cur; |
| 103 | } |
| 104 | |
| 105 | // Handle unaligned cards at the end. |
| 106 | while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) { |
| 107 | --card_end; |
| 108 | byte expected, new_value; |
| 109 | do { |
| 110 | expected = *card_end; |
| 111 | new_value = visitor(expected); |
| 112 | } while (expected != new_value && UNLIKELY(byte_cas(expected, new_value, card_end) != 0)); |
| 113 | if (expected != new_value) { |
| 114 | modified(card_cur, expected, new_value); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Now we have the words, we can process words in parallel. |
| 119 | uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); |
| 120 | uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end); |
| 121 | uintptr_t expected_word; |
| 122 | uintptr_t new_word; |
| 123 | |
| 124 | // TODO: Parallelize. |
| 125 | while (word_cur < word_end) { |
| 126 | while ((expected_word = *word_cur) != 0) { |
| 127 | new_word = |
| 128 | (visitor((expected_word >> 0) & 0xFF) << 0) | |
| 129 | (visitor((expected_word >> 8) & 0xFF) << 8) | |
| 130 | (visitor((expected_word >> 16) & 0xFF) << 16) | |
| 131 | (visitor((expected_word >> 24) & 0xFF) << 24); |
| 132 | if (new_word == expected_word) { |
| 133 | // No need to do a cas. |
| 134 | break; |
| 135 | } |
| 136 | if (LIKELY(android_atomic_cas(expected_word, new_word, |
| 137 | reinterpret_cast<int32_t*>(word_cur)) == 0)) { |
| 138 | for (size_t i = 0; i < sizeof(uintptr_t); ++i) { |
| 139 | const byte expected_byte = (expected_word >> (8 * i)) & 0xFF; |
| 140 | const byte new_byte = (new_word >> (8 * i)) & 0xFF; |
| 141 | if (expected_byte != new_byte) { |
| 142 | modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte); |
| 143 | } |
| 144 | } |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | ++word_cur; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // For every dirty at least minumum age between begin and end invoke the visitor with the |
| 153 | // specified argument. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 154 | template <typename Visitor, typename FingerVisitor> |
| 155 | void Scan(SpaceBitmap* bitmap, byte* scan_begin, byte* scan_end, |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 156 | const Visitor& visitor, const FingerVisitor& finger_visitor, |
| 157 | const byte minimum_age = kCardDirty) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 158 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 159 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 160 | DCHECK(bitmap->HasAddress(scan_begin)); |
| 161 | DCHECK(bitmap->HasAddress(scan_end - 1)); // scan_end is the byte after the last byte we scan. |
| 162 | byte* card_cur = CardFromAddr(scan_begin); |
| 163 | byte* card_end = CardFromAddr(scan_end); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 164 | CheckCardValid(card_cur); |
| 165 | CheckCardValid(card_end); |
| 166 | |
| 167 | // Handle any unaligned cards at the start. |
| 168 | while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) { |
| 169 | if (*card_cur >= minimum_age) { |
| 170 | uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur)); |
| 171 | uintptr_t end = start + kCardSize; |
| 172 | bitmap->VisitMarkedRange(start, end, visitor, finger_visitor); |
| 173 | } |
| 174 | ++card_cur; |
| 175 | } |
| 176 | |
| 177 | byte* aligned_end = card_end - |
| 178 | (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1)); |
| 179 | |
| 180 | // Now we have the words, we can send these to be processed in parallel. |
| 181 | uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); |
| 182 | uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end); |
| 183 | |
| 184 | // TODO: Parallelize |
| 185 | while (word_cur < word_end) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 186 | // Find the first dirty card. |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 187 | while (*word_cur == 0 && word_cur < word_end) { |
| 188 | word_cur++; |
| 189 | } |
| 190 | if (word_cur >= word_end) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 191 | break; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 192 | } |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 193 | uintptr_t start_word = *word_cur; |
| 194 | for (size_t i = 0; i < sizeof(uintptr_t); ++i) { |
| 195 | if ((start_word & 0xFF) == minimum_age) { |
| 196 | byte* card = reinterpret_cast<byte*>(word_cur) + i; |
| 197 | DCHECK_EQ(*card, start_word & 0xFF); |
| 198 | uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card)); |
| 199 | uintptr_t end = start + kCardSize; |
| 200 | bitmap->VisitMarkedRange(start, end, visitor, finger_visitor); |
| 201 | } |
| 202 | start_word >>= 8; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 203 | } |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 204 | ++word_cur; |
| 205 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 206 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 207 | // Handle any unaligned cards at the end. |
| 208 | card_cur = reinterpret_cast<byte*>(word_end); |
| 209 | while (card_cur < card_end) { |
| 210 | if (*card_cur >= minimum_age) { |
| 211 | uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur)); |
| 212 | uintptr_t end = start + kCardSize; |
| 213 | bitmap->VisitMarkedRange(start, end, visitor, finger_visitor); |
| 214 | } |
| 215 | ++card_cur; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 218 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 219 | // Assertion used to check the given address is covered by the card table |
| 220 | void CheckAddrIsInCardTable(const byte* addr) const; |
| 221 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 222 | // Resets all of the bytes in the card table to clean. |
| 223 | void ClearCardTable(); |
| 224 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 225 | // Resets all of the bytes in the card table which do not map to the image space. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 226 | void ClearSpaceCards(ContinuousSpace* space); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 227 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 228 | // Returns the first address in the heap which maps to this card. |
| 229 | void* AddrFromCard(const byte *card_addr) const { |
| 230 | DCHECK(IsValidCard(card_addr)) |
| 231 | << " card_addr: " << reinterpret_cast<const void*>(card_addr) |
| 232 | << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_) |
| 233 | << " end: " << reinterpret_cast<void*>(mem_map_->End()); |
| 234 | uintptr_t offset = card_addr - biased_begin_; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 235 | return reinterpret_cast<void*>(offset << kCardShift); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 236 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 237 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 238 | // Returns the address of the relevant byte in the card table, given an address on the heap. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 239 | byte* CardFromAddr(const void *addr) const { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 240 | byte *card_addr = biased_begin_ + ((uintptr_t)addr >> kCardShift); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 241 | // Sanity check the caller was asking for address covered by the card table |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 242 | DCHECK(IsValidCard(card_addr)) << "addr: " << addr |
| 243 | << " card_addr: " << reinterpret_cast<void*>(card_addr); |
| 244 | return card_addr; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 247 | bool AddrIsInCardTable(const void* addr) const; |
| 248 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 249 | private: |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 250 | static int byte_cas(byte old_value, byte new_value, byte* address) { |
| 251 | // Little endian means most significant byte is on the left. |
| 252 | const size_t shift = reinterpret_cast<uintptr_t>(address) % sizeof(uintptr_t); |
| 253 | // Align the address down. |
| 254 | address -= shift; |
| 255 | int32_t* word_address = reinterpret_cast<int32_t*>(address); |
| 256 | // Word with the byte we are trying to cas cleared. |
| 257 | const int32_t cur_word = *word_address & ~(0xFF << shift); |
| 258 | const int32_t old_word = cur_word | (static_cast<int32_t>(old_value) << shift); |
| 259 | const int32_t new_word = cur_word | (static_cast<int32_t>(new_value) << shift); |
| 260 | return android_atomic_cas(old_word, new_word, word_address); |
| 261 | } |
| 262 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 263 | CardTable(MemMap* begin, byte* biased_begin, size_t offset); |
| 264 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 265 | // Returns true iff the card table address is within the bounds of the card table. |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 266 | bool IsValidCard(const byte* card_addr) const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 267 | byte* begin = mem_map_->Begin() + offset_; |
| 268 | byte* end = mem_map_->End(); |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 269 | return card_addr >= begin && card_addr < end; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame^] | 272 | void CheckCardValid(byte* card) const { |
| 273 | DCHECK(IsValidCard(card)) |
| 274 | << " card_addr: " << reinterpret_cast<const void*>(card) |
| 275 | << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_) |
| 276 | << " end: " << reinterpret_cast<void*>(mem_map_->End()); |
| 277 | } |
| 278 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 279 | // Verifies that all gray objects are on a dirty card. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 280 | void VerifyCardTable(); |
| 281 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 282 | // Mmapped pages for the card table |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 283 | UniquePtr<MemMap> mem_map_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 284 | // Value used to compute card table addresses from object addresses, see GetBiasedBegin |
| 285 | byte* const biased_begin_; |
| 286 | // Card table doesn't begin at the beginning of the mem_map_, instead it is displaced by offset |
| 287 | // to allow the byte value of biased_begin_ to equal GC_CARD_DIRTY |
| 288 | const size_t offset_; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | } // namespace art |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 292 | #endif // ART_SRC_GC_CARDTABLE_H_ |