blob: f0c4d0d7882e2792abef917785fe9ac53b67fbd7 [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);
157 uintptr_t expected_word;
158 uintptr_t new_word;
159
160 // TODO: Parallelize.
161 while (word_cur < word_end) {
162 while ((expected_word = *word_cur) != 0) {
163 new_word =
164 (visitor((expected_word >> 0) & 0xFF) << 0) |
165 (visitor((expected_word >> 8) & 0xFF) << 8) |
166 (visitor((expected_word >> 16) & 0xFF) << 16) |
167 (visitor((expected_word >> 24) & 0xFF) << 24);
168 if (new_word == expected_word) {
169 // No need to do a cas.
170 break;
171 }
172 if (LIKELY(android_atomic_cas(expected_word, new_word,
173 reinterpret_cast<int32_t*>(word_cur)) == 0)) {
174 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
175 const byte expected_byte = (expected_word >> (8 * i)) & 0xFF;
176 const byte new_byte = (new_word >> (8 * i)) & 0xFF;
177 if (expected_byte != new_byte) {
178 modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
179 }
180 }
181 break;
182 }
183 }
184 ++word_cur;
185 }
186}
187
188inline void* CardTable::AddrFromCard(const byte *card_addr) const {
189 DCHECK(IsValidCard(card_addr))
190 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
191 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
192 << " end: " << reinterpret_cast<void*>(mem_map_->End());
193 uintptr_t offset = card_addr - biased_begin_;
194 return reinterpret_cast<void*>(offset << kCardShift);
195}
196
197inline byte* CardTable::CardFromAddr(const void *addr) const {
198 byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
199 // Sanity check the caller was asking for address covered by the card table
200 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
201 << " card_addr: " << reinterpret_cast<void*>(card_addr);
202 return card_addr;
203}
204
205inline void CardTable::CheckCardValid(byte* card) const {
206 DCHECK(IsValidCard(card))
207 << " card_addr: " << reinterpret_cast<const void*>(card)
208 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
209 << " end: " << reinterpret_cast<void*>(mem_map_->End());
210}
211
Ian Rogers1d54e732013-05-02 21:10:01 -0700212} // namespace accounting
213} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214} // namespace art
215
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700216#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_