blob: 564168e2749173c363554ab6d5084f6d4027e989 [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
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
26namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace accounting {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029
30static inline bool byte_cas(byte old_value, byte new_value, byte* address) {
31 // Little endian means most significant byte is on the left.
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080032 const size_t shift_in_bytes = reinterpret_cast<uintptr_t>(address) % sizeof(uintptr_t);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033 // Align the address down.
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080034 address -= shift_in_bytes;
35 const size_t shift_in_bits = shift_in_bytes * kBitsPerByte;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 int32_t* word_address = reinterpret_cast<int32_t*>(address);
37 // Word with the byte we are trying to cas cleared.
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080038 const int32_t cur_word = *word_address & ~(0xFF << shift_in_bits);
39 const int32_t old_word = cur_word | (static_cast<int32_t>(old_value) << shift_in_bits);
40 const int32_t new_word = cur_word | (static_cast<int32_t>(new_value) << shift_in_bits);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 bool success = android_atomic_cas(old_word, new_word, word_address) == 0;
42 return success;
43}
44
Mathieu Chartier184e3222013-08-03 14:02:57 -070045template <typename Visitor>
Mathieu Chartier0f72e412013-09-06 16:40:01 -070046inline size_t CardTable::Scan(SpaceBitmap* bitmap, byte* scan_begin, byte* scan_end,
47 const Visitor& visitor, const byte minimum_age) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 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);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070054 size_t cards_scanned = 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055
56 // Handle any unaligned cards at the start.
57 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
58 if (*card_cur >= minimum_age) {
59 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -070060 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
61 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 }
63 ++card_cur;
64 }
65
66 byte* aligned_end = card_end -
67 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
68
Mathieu Chartier720ef762013-08-17 14:46:54 -070069 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070070 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
71 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070072 while (LIKELY(*word_cur == 0)) {
73 ++word_cur;
74 if (UNLIKELY(word_cur >= word_end)) {
75 goto exit_for;
76 }
77 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 uintptr_t start_word = *word_cur;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070081 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<byte*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070082 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
83 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070085 if (static_cast<byte>(start_word) >= minimum_age) {
86 auto* card = reinterpret_cast<byte*>(word_cur) + i;
87 DCHECK(*card == static_cast<byte>(start_word) || *card == kCardDirty)
88 << "card " << static_cast<size_t>(*card) << " word " << (start_word & 0xFF);
89 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070090 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 }
92 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070093 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -070096 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097
98 // Handle any unaligned cards at the end.
99 card_cur = reinterpret_cast<byte*>(word_end);
100 while (card_cur < card_end) {
101 if (*card_cur >= minimum_age) {
102 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700103 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
104 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 }
106 ++card_cur;
107 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700108
109 return cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110}
111
112/*
113 * Visitor is expected to take in a card and return the new value. When a value is modified, the
114 * modify visitor is called.
115 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
116 * value.
117 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
118 * us to know which cards got cleared.
119 */
120template <typename Visitor, typename ModifiedVisitor>
121inline void CardTable::ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
122 const ModifiedVisitor& modified) {
123 byte* card_cur = CardFromAddr(scan_begin);
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800124 byte* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 CheckCardValid(card_cur);
126 CheckCardValid(card_end);
127
128 // Handle any unaligned cards at the start.
129 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
130 byte expected, new_value;
131 do {
132 expected = *card_cur;
133 new_value = visitor(expected);
134 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
135 if (expected != new_value) {
136 modified(card_cur, expected, new_value);
137 }
138 ++card_cur;
139 }
140
141 // Handle unaligned cards at the end.
142 while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) {
143 --card_end;
144 byte expected, new_value;
145 do {
146 expected = *card_end;
147 new_value = visitor(expected);
148 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
149 if (expected != new_value) {
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800150 modified(card_end, expected, new_value);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 }
152 }
153
154 // Now we have the words, we can process words in parallel.
155 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
156 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800157 // TODO: This is not big endian safe.
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800158 union {
159 uintptr_t expected_word;
160 uint8_t expected_bytes[sizeof(uintptr_t)];
161 };
162 union {
163 uintptr_t new_word;
164 uint8_t new_bytes[sizeof(uintptr_t)];
165 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166
167 // TODO: Parallelize.
168 while (word_cur < word_end) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800169 while (true) {
170 expected_word = *word_cur;
171 if (LIKELY(expected_word == 0)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 break;
173 }
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800174 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
175 new_bytes[i] = visitor(expected_bytes[i]);
176 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 if (LIKELY(android_atomic_cas(expected_word, new_word,
178 reinterpret_cast<int32_t*>(word_cur)) == 0)) {
179 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800180 const byte expected_byte = expected_bytes[i];
181 const byte new_byte = new_bytes[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 if (expected_byte != new_byte) {
183 modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
184 }
185 }
186 break;
187 }
188 }
189 ++word_cur;
190 }
191}
192
193inline void* CardTable::AddrFromCard(const byte *card_addr) const {
194 DCHECK(IsValidCard(card_addr))
195 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
196 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
197 << " end: " << reinterpret_cast<void*>(mem_map_->End());
198 uintptr_t offset = card_addr - biased_begin_;
199 return reinterpret_cast<void*>(offset << kCardShift);
200}
201
202inline byte* CardTable::CardFromAddr(const void *addr) const {
203 byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
204 // Sanity check the caller was asking for address covered by the card table
205 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
206 << " card_addr: " << reinterpret_cast<void*>(card_addr);
207 return card_addr;
208}
209
210inline void CardTable::CheckCardValid(byte* card) const {
211 DCHECK(IsValidCard(card))
212 << " card_addr: " << reinterpret_cast<const void*>(card)
213 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
214 << " end: " << reinterpret_cast<void*>(mem_map_->End());
215}
216
Ian Rogers1d54e732013-05-02 21:10:01 -0700217} // namespace accounting
218} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219} // namespace art
220
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700221#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_