blob: 45ab214d9cfaac5562662069caf193b61776a6c8 [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.
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
Mathieu Chartier184e3222013-08-03 14:02:57 -070044template <typename Visitor>
Mathieu Chartier0f72e412013-09-06 16:40:01 -070045inline size_t CardTable::Scan(SpaceBitmap* bitmap, byte* scan_begin, byte* scan_end,
46 const Visitor& visitor, const byte minimum_age) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 DCHECK(bitmap->HasAddress(scan_begin));
48 DCHECK(bitmap->HasAddress(scan_end - 1)); // scan_end is the byte after the last byte we scan.
49 byte* card_cur = CardFromAddr(scan_begin);
50 byte* card_end = CardFromAddr(scan_end);
51 CheckCardValid(card_cur);
52 CheckCardValid(card_end);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070053 size_t cards_scanned = 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054
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));
Mathieu Chartier0f72e412013-09-06 16:40:01 -070059 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
60 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 }
62 ++card_cur;
63 }
64
65 byte* aligned_end = card_end -
66 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
67
Mathieu Chartier720ef762013-08-17 14:46:54 -070068 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070069 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
70 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070071 while (LIKELY(*word_cur == 0)) {
72 ++word_cur;
73 if (UNLIKELY(word_cur >= word_end)) {
74 goto exit_for;
75 }
76 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 uintptr_t start_word = *word_cur;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070080 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<byte*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070081 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
82 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070084 if (static_cast<byte>(start_word) >= minimum_age) {
85 auto* card = reinterpret_cast<byte*>(word_cur) + i;
86 DCHECK(*card == static_cast<byte>(start_word) || *card == kCardDirty)
87 << "card " << static_cast<size_t>(*card) << " word " << (start_word & 0xFF);
88 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070089 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 }
91 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070092 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -070095 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096
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));
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700102 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
103 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 }
105 ++card_cur;
106 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700107
108 return cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109}
110
111/*
112 * Visitor is expected to take in a card and return the new value. When a value is modified, the
113 * modify visitor is called.
114 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
115 * value.
116 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
117 * us to know which cards got cleared.
118 */
119template <typename Visitor, typename ModifiedVisitor>
120inline void CardTable::ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
121 const ModifiedVisitor& modified) {
122 byte* card_cur = CardFromAddr(scan_begin);
123 byte* card_end = CardFromAddr(scan_end);
124 CheckCardValid(card_cur);
125 CheckCardValid(card_end);
126
127 // Handle any unaligned cards at the start.
128 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
129 byte expected, new_value;
130 do {
131 expected = *card_cur;
132 new_value = visitor(expected);
133 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
134 if (expected != new_value) {
135 modified(card_cur, expected, new_value);
136 }
137 ++card_cur;
138 }
139
140 // Handle unaligned cards at the end.
141 while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) {
142 --card_end;
143 byte expected, new_value;
144 do {
145 expected = *card_end;
146 new_value = visitor(expected);
147 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
148 if (expected != new_value) {
149 modified(card_cur, expected, new_value);
150 }
151 }
152
153 // Now we have the words, we can process words in parallel.
154 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
155 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
156 uintptr_t expected_word;
157 uintptr_t new_word;
158
159 // TODO: Parallelize.
160 while (word_cur < word_end) {
161 while ((expected_word = *word_cur) != 0) {
162 new_word =
163 (visitor((expected_word >> 0) & 0xFF) << 0) |
164 (visitor((expected_word >> 8) & 0xFF) << 8) |
165 (visitor((expected_word >> 16) & 0xFF) << 16) |
166 (visitor((expected_word >> 24) & 0xFF) << 24);
167 if (new_word == expected_word) {
168 // No need to do a cas.
169 break;
170 }
171 if (LIKELY(android_atomic_cas(expected_word, new_word,
172 reinterpret_cast<int32_t*>(word_cur)) == 0)) {
173 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
174 const byte expected_byte = (expected_word >> (8 * i)) & 0xFF;
175 const byte new_byte = (new_word >> (8 * i)) & 0xFF;
176 if (expected_byte != new_byte) {
177 modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
178 }
179 }
180 break;
181 }
182 }
183 ++word_cur;
184 }
185}
186
187inline void* CardTable::AddrFromCard(const byte *card_addr) const {
188 DCHECK(IsValidCard(card_addr))
189 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
190 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
191 << " end: " << reinterpret_cast<void*>(mem_map_->End());
192 uintptr_t offset = card_addr - biased_begin_;
193 return reinterpret_cast<void*>(offset << kCardShift);
194}
195
196inline byte* CardTable::CardFromAddr(const void *addr) const {
197 byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
198 // Sanity check the caller was asking for address covered by the card table
199 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
200 << " card_addr: " << reinterpret_cast<void*>(card_addr);
201 return card_addr;
202}
203
204inline void CardTable::CheckCardValid(byte* card) const {
205 DCHECK(IsValidCard(card))
206 << " card_addr: " << reinterpret_cast<const void*>(card)
207 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
208 << " end: " << reinterpret_cast<void*>(mem_map_->End());
209}
210
Ian Rogers1d54e732013-05-02 21:10:01 -0700211} // namespace accounting
212} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213} // namespace art
214
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700215#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_