blob: 83ad33e145edb2f1bd579d42737a2323a1ac0890 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "space_bitmap.h"
24#include "utils.h"
25
26namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace accounting {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
Ian Rogers13735952014-10-08 12:43:28 -070030static inline bool byte_cas(uint8_t old_value, uint8_t new_value, uint8_t* address) {
Ian Rogers14611442014-07-08 22:55:18 -070031#if defined(__i386__) || defined(__x86_64__)
Ian Rogers13735952014-10-08 12:43:28 -070032 Atomic<uint8_t>* byte_atomic = reinterpret_cast<Atomic<uint8_t>*>(address);
Ian Rogers14611442014-07-08 22:55:18 -070033 return byte_atomic->CompareExchangeWeakRelaxed(old_value, new_value);
34#else
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 // Little endian means most significant byte is on the left.
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080036 const size_t shift_in_bytes = reinterpret_cast<uintptr_t>(address) % sizeof(uintptr_t);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037 // Align the address down.
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080038 address -= shift_in_bytes;
39 const size_t shift_in_bits = shift_in_bytes * kBitsPerByte;
Mathieu Chartieraa3c3e52014-08-05 15:59:43 -070040 Atomic<uintptr_t>* word_atomic = reinterpret_cast<Atomic<uintptr_t>*>(address);
Ian Rogers14611442014-07-08 22:55:18 -070041
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 // Word with the byte we are trying to cas cleared.
Mathieu Chartieraa3c3e52014-08-05 15:59:43 -070043 const uintptr_t cur_word = word_atomic->LoadRelaxed() &
44 ~(static_cast<uintptr_t>(0xFF) << shift_in_bits);
45 const uintptr_t old_word = cur_word | (static_cast<uintptr_t>(old_value) << shift_in_bits);
46 const uintptr_t new_word = cur_word | (static_cast<uintptr_t>(new_value) << shift_in_bits);
Ian Rogers14611442014-07-08 22:55:18 -070047 return word_atomic->CompareExchangeWeakRelaxed(old_word, new_word);
48#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049}
50
Lei Li727b2942015-01-15 11:26:34 +080051template <bool kClearCard, typename Visitor>
Ian Rogers13735952014-10-08 12:43:28 -070052inline size_t CardTable::Scan(ContinuousSpaceBitmap* bitmap, uint8_t* scan_begin, uint8_t* scan_end,
53 const Visitor& visitor, const uint8_t minimum_age) const {
54 DCHECK_GE(scan_begin, reinterpret_cast<uint8_t*>(bitmap->HeapBegin()));
Zuo Wangf37a88b2014-07-10 04:26:41 -070055 // scan_end is the byte after the last byte we scan.
Ian Rogers13735952014-10-08 12:43:28 -070056 DCHECK_LE(scan_end, reinterpret_cast<uint8_t*>(bitmap->HeapLimit()));
57 uint8_t* card_cur = CardFromAddr(scan_begin);
58 uint8_t* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 CheckCardValid(card_cur);
60 CheckCardValid(card_end);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070061 size_t cards_scanned = 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062
63 // Handle any unaligned cards at the start.
Ian Rogers13735952014-10-08 12:43:28 -070064 while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 if (*card_cur >= minimum_age) {
66 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -070067 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
68 ++cards_scanned;
Lei Li727b2942015-01-15 11:26:34 +080069 if (kClearCard) {
70 *card_cur = 0;
71 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 }
73 ++card_cur;
74 }
75
Ian Rogers13735952014-10-08 12:43:28 -070076 uint8_t* aligned_end = card_end -
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
78
Mathieu Chartier720ef762013-08-17 14:46:54 -070079 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070080 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
81 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070082 while (LIKELY(*word_cur == 0)) {
83 ++word_cur;
84 if (UNLIKELY(word_cur >= word_end)) {
85 goto exit_for;
86 }
87 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 uintptr_t start_word = *word_cur;
Ian Rogers13735952014-10-08 12:43:28 -070091 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<uint8_t*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070092 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
93 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Ian Rogers13735952014-10-08 12:43:28 -070095 if (static_cast<uint8_t>(start_word) >= minimum_age) {
96 auto* card = reinterpret_cast<uint8_t*>(word_cur) + i;
97 DCHECK(*card == static_cast<uint8_t>(start_word) || *card == kCardDirty)
98 << "card " << static_cast<size_t>(*card) << " intptr_t " << (start_word & 0xFF);
Mathieu Chartier94c32c52013-08-09 11:14:04 -070099 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700100 ++cards_scanned;
Lei Li727b2942015-01-15 11:26:34 +0800101 if (kClearCard) {
102 *card = 0;
103 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 }
105 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700106 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700109 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110
111 // Handle any unaligned cards at the end.
Ian Rogers13735952014-10-08 12:43:28 -0700112 card_cur = reinterpret_cast<uint8_t*>(word_end);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 while (card_cur < card_end) {
114 if (*card_cur >= minimum_age) {
115 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700116 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
117 ++cards_scanned;
Lei Li727b2942015-01-15 11:26:34 +0800118 if (kClearCard) {
119 *card_cur = 0;
120 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 }
122 ++card_cur;
123 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700124
125 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>
Ian Rogers13735952014-10-08 12:43:28 -0700137inline void CardTable::ModifyCardsAtomic(uint8_t* scan_begin, uint8_t* scan_end, const Visitor& visitor,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 const ModifiedVisitor& modified) {
Ian Rogers13735952014-10-08 12:43:28 -0700139 uint8_t* card_cur = CardFromAddr(scan_begin);
140 uint8_t* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 CheckCardValid(card_cur);
142 CheckCardValid(card_end);
143
144 // Handle any unaligned cards at the start.
Ian Rogers13735952014-10-08 12:43:28 -0700145 while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
146 uint8_t expected, new_value;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147 do {
148 expected = *card_cur;
149 new_value = visitor(expected);
150 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
151 if (expected != new_value) {
152 modified(card_cur, expected, new_value);
153 }
154 ++card_cur;
155 }
156
157 // Handle unaligned cards at the end.
Ian Rogers13735952014-10-08 12:43:28 -0700158 while (!IsAligned<sizeof(intptr_t)>(card_end) && card_end > card_cur) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 --card_end;
Ian Rogers13735952014-10-08 12:43:28 -0700160 uint8_t expected, new_value;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 do {
162 expected = *card_end;
163 new_value = visitor(expected);
164 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
165 if (expected != new_value) {
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800166 modified(card_end, expected, new_value);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 }
168 }
169
170 // Now we have the words, we can process words in parallel.
171 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
172 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800173 // TODO: This is not big endian safe.
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800174 union {
175 uintptr_t expected_word;
176 uint8_t expected_bytes[sizeof(uintptr_t)];
177 };
178 union {
179 uintptr_t new_word;
180 uint8_t new_bytes[sizeof(uintptr_t)];
181 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182
183 // TODO: Parallelize.
184 while (word_cur < word_end) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800185 while (true) {
186 expected_word = *word_cur;
187 if (LIKELY(expected_word == 0)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 break;
189 }
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800190 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
191 new_bytes[i] = visitor(expected_bytes[i]);
192 }
Ian Rogers14611442014-07-08 22:55:18 -0700193 Atomic<uintptr_t>* atomic_word = reinterpret_cast<Atomic<uintptr_t>*>(word_cur);
194 if (LIKELY(atomic_word->CompareExchangeWeakRelaxed(expected_word, new_word))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700196 const uint8_t expected_byte = expected_bytes[i];
197 const uint8_t new_byte = new_bytes[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 if (expected_byte != new_byte) {
Ian Rogers13735952014-10-08 12:43:28 -0700199 modified(reinterpret_cast<uint8_t*>(word_cur) + i, expected_byte, new_byte);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 }
201 }
202 break;
203 }
204 }
205 ++word_cur;
206 }
207}
208
Ian Rogers13735952014-10-08 12:43:28 -0700209inline void* CardTable::AddrFromCard(const uint8_t *card_addr) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 DCHECK(IsValidCard(card_addr))
211 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
212 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
213 << " end: " << reinterpret_cast<void*>(mem_map_->End());
214 uintptr_t offset = card_addr - biased_begin_;
215 return reinterpret_cast<void*>(offset << kCardShift);
216}
217
Ian Rogers13735952014-10-08 12:43:28 -0700218inline uint8_t* CardTable::CardFromAddr(const void *addr) const {
219 uint8_t *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 // Sanity check the caller was asking for address covered by the card table
221 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
222 << " card_addr: " << reinterpret_cast<void*>(card_addr);
223 return card_addr;
224}
225
Ian Rogers13735952014-10-08 12:43:28 -0700226inline void CardTable::CheckCardValid(uint8_t* card) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 DCHECK(IsValidCard(card))
228 << " card_addr: " << reinterpret_cast<const void*>(card)
229 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
230 << " end: " << reinterpret_cast<void*>(mem_map_->End());
231}
232
Ian Rogers1d54e732013-05-02 21:10:01 -0700233} // namespace accounting
234} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235} // namespace art
236
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700237#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_