blob: 6ff53597e46d4fea32ac6ed44d7a81fb941d625b [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_
18#define ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Ian Rogers14611442014-07-08 22:55:18 -070020#include "atomic.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include "base/bit_utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/logging.h"
23#include "card_table.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "space_bitmap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026
27namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070028namespace gc {
29namespace accounting {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030
Ian Rogers13735952014-10-08 12:43:28 -070031static inline bool byte_cas(uint8_t old_value, uint8_t new_value, uint8_t* address) {
Ian Rogers14611442014-07-08 22:55:18 -070032#if defined(__i386__) || defined(__x86_64__)
Ian Rogers13735952014-10-08 12:43:28 -070033 Atomic<uint8_t>* byte_atomic = reinterpret_cast<Atomic<uint8_t>*>(address);
Ian Rogers14611442014-07-08 22:55:18 -070034 return byte_atomic->CompareExchangeWeakRelaxed(old_value, new_value);
35#else
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 // Little endian means most significant byte is on the left.
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080037 const size_t shift_in_bytes = reinterpret_cast<uintptr_t>(address) % sizeof(uintptr_t);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 // Align the address down.
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080039 address -= shift_in_bytes;
40 const size_t shift_in_bits = shift_in_bytes * kBitsPerByte;
Mathieu Chartieraa3c3e52014-08-05 15:59:43 -070041 Atomic<uintptr_t>* word_atomic = reinterpret_cast<Atomic<uintptr_t>*>(address);
Ian Rogers14611442014-07-08 22:55:18 -070042
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 // Word with the byte we are trying to cas cleared.
Mathieu Chartieraa3c3e52014-08-05 15:59:43 -070044 const uintptr_t cur_word = word_atomic->LoadRelaxed() &
45 ~(static_cast<uintptr_t>(0xFF) << shift_in_bits);
46 const uintptr_t old_word = cur_word | (static_cast<uintptr_t>(old_value) << shift_in_bits);
47 const uintptr_t new_word = cur_word | (static_cast<uintptr_t>(new_value) << shift_in_bits);
Ian Rogers14611442014-07-08 22:55:18 -070048 return word_atomic->CompareExchangeWeakRelaxed(old_word, new_word);
49#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050}
51
Lei Li727b2942015-01-15 11:26:34 +080052template <bool kClearCard, typename Visitor>
Mathieu Chartier6e6078a2016-10-24 15:45:41 -070053inline size_t CardTable::Scan(ContinuousSpaceBitmap* bitmap,
54 uint8_t* const scan_begin,
55 uint8_t* const scan_end,
56 const Visitor& visitor,
57 const uint8_t minimum_age) {
Ian Rogers13735952014-10-08 12:43:28 -070058 DCHECK_GE(scan_begin, reinterpret_cast<uint8_t*>(bitmap->HeapBegin()));
Zuo Wangf37a88b2014-07-10 04:26:41 -070059 // scan_end is the byte after the last byte we scan.
Ian Rogers13735952014-10-08 12:43:28 -070060 DCHECK_LE(scan_end, reinterpret_cast<uint8_t*>(bitmap->HeapLimit()));
Mathieu Chartier6e6078a2016-10-24 15:45:41 -070061 uint8_t* const card_begin = CardFromAddr(scan_begin);
62 uint8_t* const card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
63 uint8_t* card_cur = card_begin;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 CheckCardValid(card_cur);
65 CheckCardValid(card_end);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070066 size_t cards_scanned = 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067
68 // Handle any unaligned cards at the start.
Ian Rogers13735952014-10-08 12:43:28 -070069 while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 if (*card_cur >= minimum_age) {
71 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -070072 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
73 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
75 ++card_cur;
76 }
77
Ian Rogers13735952014-10-08 12:43:28 -070078 uint8_t* aligned_end = card_end -
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
80
Mathieu Chartier720ef762013-08-17 14:46:54 -070081 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070082 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
83 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070084 while (LIKELY(*word_cur == 0)) {
85 ++word_cur;
86 if (UNLIKELY(word_cur >= word_end)) {
87 goto exit_for;
88 }
89 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 uintptr_t start_word = *word_cur;
Ian Rogers13735952014-10-08 12:43:28 -070093 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<uint8_t*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070094 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
95 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Ian Rogers13735952014-10-08 12:43:28 -070097 if (static_cast<uint8_t>(start_word) >= minimum_age) {
98 auto* card = reinterpret_cast<uint8_t*>(word_cur) + i;
99 DCHECK(*card == static_cast<uint8_t>(start_word) || *card == kCardDirty)
100 << "card " << static_cast<size_t>(*card) << " intptr_t " << (start_word & 0xFF);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700101 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700102 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 }
104 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700105 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700108 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109
110 // Handle any unaligned cards at the end.
Ian Rogers13735952014-10-08 12:43:28 -0700111 card_cur = reinterpret_cast<uint8_t*>(word_end);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 while (card_cur < card_end) {
113 if (*card_cur >= minimum_age) {
114 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700115 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
116 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
118 ++card_cur;
119 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700120
Mathieu Chartier6e6078a2016-10-24 15:45:41 -0700121 if (kClearCard) {
122 ClearCardRange(scan_begin, scan_end);
123 }
124
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700125 return cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126}
127
128/*
129 * Visitor is expected to take in a card and return the new value. When a value is modified, the
130 * modify visitor is called.
131 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
132 * value.
133 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
134 * us to know which cards got cleared.
135 */
136template <typename Visitor, typename ModifiedVisitor>
Mathieu Chartier6e6078a2016-10-24 15:45:41 -0700137inline void CardTable::ModifyCardsAtomic(uint8_t* scan_begin,
138 uint8_t* scan_end,
139 const Visitor& visitor,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 const ModifiedVisitor& modified) {
Ian Rogers13735952014-10-08 12:43:28 -0700141 uint8_t* card_cur = CardFromAddr(scan_begin);
142 uint8_t* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 CheckCardValid(card_cur);
144 CheckCardValid(card_end);
145
146 // Handle any unaligned cards at the start.
Ian Rogers13735952014-10-08 12:43:28 -0700147 while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
148 uint8_t expected, new_value;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 do {
150 expected = *card_cur;
151 new_value = visitor(expected);
152 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
153 if (expected != new_value) {
154 modified(card_cur, expected, new_value);
155 }
156 ++card_cur;
157 }
158
159 // Handle unaligned cards at the end.
Ian Rogers13735952014-10-08 12:43:28 -0700160 while (!IsAligned<sizeof(intptr_t)>(card_end) && card_end > card_cur) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 --card_end;
Ian Rogers13735952014-10-08 12:43:28 -0700162 uint8_t expected, new_value;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 do {
164 expected = *card_end;
165 new_value = visitor(expected);
166 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
167 if (expected != new_value) {
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800168 modified(card_end, expected, new_value);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 }
170 }
171
172 // Now we have the words, we can process words in parallel.
173 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
174 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800175 // TODO: This is not big endian safe.
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800176 union {
177 uintptr_t expected_word;
178 uint8_t expected_bytes[sizeof(uintptr_t)];
179 };
180 union {
181 uintptr_t new_word;
182 uint8_t new_bytes[sizeof(uintptr_t)];
183 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184
185 // TODO: Parallelize.
186 while (word_cur < word_end) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800187 while (true) {
188 expected_word = *word_cur;
189 if (LIKELY(expected_word == 0)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 break;
191 }
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800192 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
193 new_bytes[i] = visitor(expected_bytes[i]);
194 }
Ian Rogers14611442014-07-08 22:55:18 -0700195 Atomic<uintptr_t>* atomic_word = reinterpret_cast<Atomic<uintptr_t>*>(word_cur);
196 if (LIKELY(atomic_word->CompareExchangeWeakRelaxed(expected_word, new_word))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700198 const uint8_t expected_byte = expected_bytes[i];
199 const uint8_t new_byte = new_bytes[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 if (expected_byte != new_byte) {
Ian Rogers13735952014-10-08 12:43:28 -0700201 modified(reinterpret_cast<uint8_t*>(word_cur) + i, expected_byte, new_byte);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 }
203 }
204 break;
205 }
206 }
207 ++word_cur;
208 }
209}
210
Ian Rogers13735952014-10-08 12:43:28 -0700211inline void* CardTable::AddrFromCard(const uint8_t *card_addr) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 DCHECK(IsValidCard(card_addr))
213 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
214 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
215 << " end: " << reinterpret_cast<void*>(mem_map_->End());
216 uintptr_t offset = card_addr - biased_begin_;
217 return reinterpret_cast<void*>(offset << kCardShift);
218}
219
Ian Rogers13735952014-10-08 12:43:28 -0700220inline uint8_t* CardTable::CardFromAddr(const void *addr) const {
221 uint8_t *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 // Sanity check the caller was asking for address covered by the card table
223 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
224 << " card_addr: " << reinterpret_cast<void*>(card_addr);
225 return card_addr;
226}
227
Vladimir Marko3481ba22015-04-13 12:22:36 +0100228inline bool CardTable::IsValidCard(const uint8_t* card_addr) const {
229 uint8_t* begin = mem_map_->Begin() + offset_;
230 uint8_t* end = mem_map_->End();
231 return card_addr >= begin && card_addr < end;
232}
233
Ian Rogers13735952014-10-08 12:43:28 -0700234inline void CardTable::CheckCardValid(uint8_t* card) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 DCHECK(IsValidCard(card))
236 << " card_addr: " << reinterpret_cast<const void*>(card)
237 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
238 << " end: " << reinterpret_cast<void*>(mem_map_->End());
239}
240
Ian Rogers1d54e732013-05-02 21:10:01 -0700241} // namespace accounting
242} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243} // namespace art
244
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700245#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_