blob: b936d93b4366a1b9456d6f644f992cad1a523337 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "base/logging.h"
22#include "card_table.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010023#include "mem_map.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "space_bitmap.h"
25#include "utils.h"
26
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>
Ian Rogers13735952014-10-08 12:43:28 -070053inline size_t CardTable::Scan(ContinuousSpaceBitmap* bitmap, uint8_t* scan_begin, uint8_t* scan_end,
54 const Visitor& visitor, const uint8_t minimum_age) const {
55 DCHECK_GE(scan_begin, reinterpret_cast<uint8_t*>(bitmap->HeapBegin()));
Zuo Wangf37a88b2014-07-10 04:26:41 -070056 // scan_end is the byte after the last byte we scan.
Ian Rogers13735952014-10-08 12:43:28 -070057 DCHECK_LE(scan_end, reinterpret_cast<uint8_t*>(bitmap->HeapLimit()));
58 uint8_t* card_cur = CardFromAddr(scan_begin);
59 uint8_t* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 CheckCardValid(card_cur);
61 CheckCardValid(card_end);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070062 size_t cards_scanned = 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063
64 // Handle any unaligned cards at the start.
Ian Rogers13735952014-10-08 12:43:28 -070065 while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 if (*card_cur >= minimum_age) {
67 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -070068 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
69 ++cards_scanned;
Lei Li727b2942015-01-15 11:26:34 +080070 if (kClearCard) {
71 *card_cur = 0;
72 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 }
74 ++card_cur;
75 }
76
Ian Rogers13735952014-10-08 12:43:28 -070077 uint8_t* aligned_end = card_end -
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
79
Mathieu Chartier720ef762013-08-17 14:46:54 -070080 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070081 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
82 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070083 while (LIKELY(*word_cur == 0)) {
84 ++word_cur;
85 if (UNLIKELY(word_cur >= word_end)) {
86 goto exit_for;
87 }
88 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 uintptr_t start_word = *word_cur;
Ian Rogers13735952014-10-08 12:43:28 -070092 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<uint8_t*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070093 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
94 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Ian Rogers13735952014-10-08 12:43:28 -070096 if (static_cast<uint8_t>(start_word) >= minimum_age) {
97 auto* card = reinterpret_cast<uint8_t*>(word_cur) + i;
98 DCHECK(*card == static_cast<uint8_t>(start_word) || *card == kCardDirty)
99 << "card " << static_cast<size_t>(*card) << " intptr_t " << (start_word & 0xFF);
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700100 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700101 ++cards_scanned;
Lei Li727b2942015-01-15 11:26:34 +0800102 if (kClearCard) {
103 *card = 0;
104 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 }
106 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700107 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700110 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111
112 // Handle any unaligned cards at the end.
Ian Rogers13735952014-10-08 12:43:28 -0700113 card_cur = reinterpret_cast<uint8_t*>(word_end);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 while (card_cur < card_end) {
115 if (*card_cur >= minimum_age) {
116 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700117 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
118 ++cards_scanned;
Lei Li727b2942015-01-15 11:26:34 +0800119 if (kClearCard) {
120 *card_cur = 0;
121 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 }
123 ++card_cur;
124 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700125
126 return cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127}
128
129/*
130 * Visitor is expected to take in a card and return the new value. When a value is modified, the
131 * modify visitor is called.
132 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
133 * value.
134 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
135 * us to know which cards got cleared.
136 */
137template <typename Visitor, typename ModifiedVisitor>
Ian Rogers13735952014-10-08 12:43:28 -0700138inline void CardTable::ModifyCardsAtomic(uint8_t* scan_begin, uint8_t* scan_end, const Visitor& visitor,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 const ModifiedVisitor& modified) {
Ian Rogers13735952014-10-08 12:43:28 -0700140 uint8_t* card_cur = CardFromAddr(scan_begin);
141 uint8_t* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 CheckCardValid(card_cur);
143 CheckCardValid(card_end);
144
145 // Handle any unaligned cards at the start.
Ian Rogers13735952014-10-08 12:43:28 -0700146 while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
147 uint8_t expected, new_value;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 do {
149 expected = *card_cur;
150 new_value = visitor(expected);
151 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
152 if (expected != new_value) {
153 modified(card_cur, expected, new_value);
154 }
155 ++card_cur;
156 }
157
158 // Handle unaligned cards at the end.
Ian Rogers13735952014-10-08 12:43:28 -0700159 while (!IsAligned<sizeof(intptr_t)>(card_end) && card_end > card_cur) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 --card_end;
Ian Rogers13735952014-10-08 12:43:28 -0700161 uint8_t expected, new_value;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 do {
163 expected = *card_end;
164 new_value = visitor(expected);
165 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
166 if (expected != new_value) {
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800167 modified(card_end, expected, new_value);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 }
169 }
170
171 // Now we have the words, we can process words in parallel.
172 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
173 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800174 // TODO: This is not big endian safe.
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800175 union {
176 uintptr_t expected_word;
177 uint8_t expected_bytes[sizeof(uintptr_t)];
178 };
179 union {
180 uintptr_t new_word;
181 uint8_t new_bytes[sizeof(uintptr_t)];
182 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183
184 // TODO: Parallelize.
185 while (word_cur < word_end) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800186 while (true) {
187 expected_word = *word_cur;
188 if (LIKELY(expected_word == 0)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 break;
190 }
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800191 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
192 new_bytes[i] = visitor(expected_bytes[i]);
193 }
Ian Rogers14611442014-07-08 22:55:18 -0700194 Atomic<uintptr_t>* atomic_word = reinterpret_cast<Atomic<uintptr_t>*>(word_cur);
195 if (LIKELY(atomic_word->CompareExchangeWeakRelaxed(expected_word, new_word))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700197 const uint8_t expected_byte = expected_bytes[i];
198 const uint8_t new_byte = new_bytes[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 if (expected_byte != new_byte) {
Ian Rogers13735952014-10-08 12:43:28 -0700200 modified(reinterpret_cast<uint8_t*>(word_cur) + i, expected_byte, new_byte);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 }
202 }
203 break;
204 }
205 }
206 ++word_cur;
207 }
208}
209
Ian Rogers13735952014-10-08 12:43:28 -0700210inline void* CardTable::AddrFromCard(const uint8_t *card_addr) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 DCHECK(IsValidCard(card_addr))
212 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
213 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
214 << " end: " << reinterpret_cast<void*>(mem_map_->End());
215 uintptr_t offset = card_addr - biased_begin_;
216 return reinterpret_cast<void*>(offset << kCardShift);
217}
218
Ian Rogers13735952014-10-08 12:43:28 -0700219inline uint8_t* CardTable::CardFromAddr(const void *addr) const {
220 uint8_t *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 // Sanity check the caller was asking for address covered by the card table
222 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
223 << " card_addr: " << reinterpret_cast<void*>(card_addr);
224 return card_addr;
225}
226
Vladimir Marko3481ba22015-04-13 12:22:36 +0100227inline bool CardTable::IsValidCard(const uint8_t* card_addr) const {
228 uint8_t* begin = mem_map_->Begin() + offset_;
229 uint8_t* end = mem_map_->End();
230 return card_addr >= begin && card_addr < end;
231}
232
Ian Rogers13735952014-10-08 12:43:28 -0700233inline void CardTable::CheckCardValid(uint8_t* card) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 DCHECK(IsValidCard(card))
235 << " card_addr: " << reinterpret_cast<const void*>(card)
236 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
237 << " end: " << reinterpret_cast<void*>(mem_map_->End());
238}
239
Ian Rogers1d54e732013-05-02 21:10:01 -0700240} // namespace accounting
241} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242} // namespace art
243
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700244#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_