Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_ |
| 18 | #define ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "base/logging.h" |
| 21 | #include "card_table.h" |
| 22 | #include "cutils/atomic-inline.h" |
| 23 | #include "space_bitmap.h" |
| 24 | #include "utils.h" |
| 25 | |
| 26 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 27 | namespace gc { |
| 28 | namespace accounting { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | |
| 30 | static inline bool byte_cas(byte old_value, byte new_value, byte* address) { |
| 31 | // Little endian means most significant byte is on the left. |
| 32 | const size_t shift = reinterpret_cast<uintptr_t>(address) % sizeof(uintptr_t); |
| 33 | // Align the address down. |
| 34 | address -= shift; |
| 35 | int32_t* word_address = reinterpret_cast<int32_t*>(address); |
| 36 | // Word with the byte we are trying to cas cleared. |
| 37 | const int32_t cur_word = *word_address & ~(0xFF << shift); |
| 38 | const int32_t old_word = cur_word | (static_cast<int32_t>(old_value) << shift); |
| 39 | const int32_t new_word = cur_word | (static_cast<int32_t>(new_value) << shift); |
| 40 | bool success = android_atomic_cas(old_word, new_word, word_address) == 0; |
| 41 | return success; |
| 42 | } |
| 43 | |
| 44 | template <typename Visitor, typename FingerVisitor> |
| 45 | inline void CardTable::Scan(SpaceBitmap* bitmap, byte* scan_begin, byte* scan_end, |
| 46 | const Visitor& visitor, const FingerVisitor& finger_visitor, |
| 47 | const byte minimum_age) const { |
| 48 | DCHECK(bitmap->HasAddress(scan_begin)); |
| 49 | DCHECK(bitmap->HasAddress(scan_end - 1)); // scan_end is the byte after the last byte we scan. |
| 50 | byte* card_cur = CardFromAddr(scan_begin); |
| 51 | byte* card_end = CardFromAddr(scan_end); |
| 52 | CheckCardValid(card_cur); |
| 53 | CheckCardValid(card_end); |
| 54 | |
| 55 | // Handle any unaligned cards at the start. |
| 56 | while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) { |
| 57 | if (*card_cur >= minimum_age) { |
| 58 | uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur)); |
| 59 | uintptr_t end = start + kCardSize; |
| 60 | bitmap->VisitMarkedRange(start, end, visitor, finger_visitor); |
| 61 | } |
| 62 | ++card_cur; |
| 63 | } |
| 64 | |
| 65 | byte* aligned_end = card_end - |
| 66 | (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1)); |
| 67 | |
| 68 | // Now we have the words, we can send these to be processed in parallel. |
| 69 | uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); |
| 70 | uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end); |
| 71 | |
| 72 | // TODO: Parallelize |
| 73 | while (word_cur < word_end) { |
| 74 | // Find the first dirty card. |
| 75 | while (*word_cur == 0 && word_cur < word_end) { |
| 76 | word_cur++; |
| 77 | } |
| 78 | if (word_cur >= word_end) { |
| 79 | break; |
| 80 | } |
| 81 | uintptr_t start_word = *word_cur; |
| 82 | for (size_t i = 0; i < sizeof(uintptr_t); ++i) { |
| 83 | if ((start_word & 0xFF) >= minimum_age) { |
| 84 | byte* card = reinterpret_cast<byte*>(word_cur) + i; |
| 85 | const byte card_byte = *card; |
| 86 | DCHECK(card_byte == (start_word & 0xFF) || card_byte == kCardDirty) |
| 87 | << "card " << static_cast<size_t>(card_byte) << " word " << (start_word & 0xFF); |
| 88 | uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card)); |
| 89 | uintptr_t end = start + kCardSize; |
| 90 | bitmap->VisitMarkedRange(start, end, visitor, finger_visitor); |
| 91 | } |
| 92 | start_word >>= 8; |
| 93 | } |
| 94 | ++word_cur; |
| 95 | } |
| 96 | |
| 97 | // Handle any unaligned cards at the end. |
| 98 | card_cur = reinterpret_cast<byte*>(word_end); |
| 99 | while (card_cur < card_end) { |
| 100 | if (*card_cur >= minimum_age) { |
| 101 | uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur)); |
| 102 | uintptr_t end = start + kCardSize; |
| 103 | bitmap->VisitMarkedRange(start, end, visitor, finger_visitor); |
| 104 | } |
| 105 | ++card_cur; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Visitor is expected to take in a card and return the new value. When a value is modified, the |
| 111 | * modify visitor is called. |
| 112 | * visitor: The visitor which modifies the cards. Returns the new value for a card given an old |
| 113 | * value. |
| 114 | * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables |
| 115 | * us to know which cards got cleared. |
| 116 | */ |
| 117 | template <typename Visitor, typename ModifiedVisitor> |
| 118 | inline void CardTable::ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor, |
| 119 | const ModifiedVisitor& modified) { |
| 120 | byte* card_cur = CardFromAddr(scan_begin); |
| 121 | byte* card_end = CardFromAddr(scan_end); |
| 122 | CheckCardValid(card_cur); |
| 123 | CheckCardValid(card_end); |
| 124 | |
| 125 | // Handle any unaligned cards at the start. |
| 126 | while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) { |
| 127 | byte expected, new_value; |
| 128 | do { |
| 129 | expected = *card_cur; |
| 130 | new_value = visitor(expected); |
| 131 | } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur))); |
| 132 | if (expected != new_value) { |
| 133 | modified(card_cur, expected, new_value); |
| 134 | } |
| 135 | ++card_cur; |
| 136 | } |
| 137 | |
| 138 | // Handle unaligned cards at the end. |
| 139 | while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) { |
| 140 | --card_end; |
| 141 | byte expected, new_value; |
| 142 | do { |
| 143 | expected = *card_end; |
| 144 | new_value = visitor(expected); |
| 145 | } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end))); |
| 146 | if (expected != new_value) { |
| 147 | modified(card_cur, expected, new_value); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Now we have the words, we can process words in parallel. |
| 152 | uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); |
| 153 | uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end); |
| 154 | uintptr_t expected_word; |
| 155 | uintptr_t new_word; |
| 156 | |
| 157 | // TODO: Parallelize. |
| 158 | while (word_cur < word_end) { |
| 159 | while ((expected_word = *word_cur) != 0) { |
| 160 | new_word = |
| 161 | (visitor((expected_word >> 0) & 0xFF) << 0) | |
| 162 | (visitor((expected_word >> 8) & 0xFF) << 8) | |
| 163 | (visitor((expected_word >> 16) & 0xFF) << 16) | |
| 164 | (visitor((expected_word >> 24) & 0xFF) << 24); |
| 165 | if (new_word == expected_word) { |
| 166 | // No need to do a cas. |
| 167 | break; |
| 168 | } |
| 169 | if (LIKELY(android_atomic_cas(expected_word, new_word, |
| 170 | reinterpret_cast<int32_t*>(word_cur)) == 0)) { |
| 171 | for (size_t i = 0; i < sizeof(uintptr_t); ++i) { |
| 172 | const byte expected_byte = (expected_word >> (8 * i)) & 0xFF; |
| 173 | const byte new_byte = (new_word >> (8 * i)) & 0xFF; |
| 174 | if (expected_byte != new_byte) { |
| 175 | modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte); |
| 176 | } |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | ++word_cur; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | inline void* CardTable::AddrFromCard(const byte *card_addr) const { |
| 186 | DCHECK(IsValidCard(card_addr)) |
| 187 | << " card_addr: " << reinterpret_cast<const void*>(card_addr) |
| 188 | << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_) |
| 189 | << " end: " << reinterpret_cast<void*>(mem_map_->End()); |
| 190 | uintptr_t offset = card_addr - biased_begin_; |
| 191 | return reinterpret_cast<void*>(offset << kCardShift); |
| 192 | } |
| 193 | |
| 194 | inline byte* CardTable::CardFromAddr(const void *addr) const { |
| 195 | byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift); |
| 196 | // Sanity check the caller was asking for address covered by the card table |
| 197 | DCHECK(IsValidCard(card_addr)) << "addr: " << addr |
| 198 | << " card_addr: " << reinterpret_cast<void*>(card_addr); |
| 199 | return card_addr; |
| 200 | } |
| 201 | |
| 202 | inline void CardTable::CheckCardValid(byte* card) const { |
| 203 | DCHECK(IsValidCard(card)) |
| 204 | << " card_addr: " << reinterpret_cast<const void*>(card) |
| 205 | << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_) |
| 206 | << " end: " << reinterpret_cast<void*>(mem_map_->End()); |
| 207 | } |
| 208 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 209 | } // namespace accounting |
| 210 | } // namespace gc |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 211 | } // namespace art |
| 212 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 213 | #endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_ |