blob: 46b9363b9a4be1eed875d0915a661c74d981aae3 [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
30static inline bool byte_cas(byte old_value, byte new_value, byte* address) {
Ian Rogers14611442014-07-08 22:55:18 -070031#if defined(__i386__) || defined(__x86_64__)
32 Atomic<byte>* byte_atomic = reinterpret_cast<Atomic<byte>*>(address);
33 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;
Ian Rogers14611442014-07-08 22:55:18 -070040 AtomicInteger* word_atomic = reinterpret_cast<AtomicInteger*>(address);
41
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 // Word with the byte we are trying to cas cleared.
Ian Rogers14611442014-07-08 22:55:18 -070043 const int32_t cur_word = word_atomic->LoadRelaxed() & ~(0xFF << shift_in_bits);
Brian Carlstrombb11f4b2013-12-18 17:42:42 -080044 const int32_t old_word = cur_word | (static_cast<int32_t>(old_value) << shift_in_bits);
45 const int32_t new_word = cur_word | (static_cast<int32_t>(new_value) << shift_in_bits);
Ian Rogers14611442014-07-08 22:55:18 -070046 return word_atomic->CompareExchangeWeakRelaxed(old_word, new_word);
47#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048}
49
Mathieu Chartier184e3222013-08-03 14:02:57 -070050template <typename Visitor>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070051inline size_t CardTable::Scan(ContinuousSpaceBitmap* bitmap, byte* scan_begin, byte* scan_end,
Mathieu Chartier0f72e412013-09-06 16:40:01 -070052 const Visitor& visitor, const byte minimum_age) const {
Zuo Wangf37a88b2014-07-10 04:26:41 -070053 DCHECK_GE(scan_begin, reinterpret_cast<byte*>(bitmap->HeapBegin()));
54 // scan_end is the byte after the last byte we scan.
55 DCHECK_LE(scan_end, reinterpret_cast<byte*>(bitmap->HeapLimit()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 byte* card_cur = CardFromAddr(scan_begin);
57 byte* card_end = CardFromAddr(scan_end);
58 CheckCardValid(card_cur);
59 CheckCardValid(card_end);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070060 size_t cards_scanned = 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061
62 // Handle any unaligned cards at the start.
63 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
64 if (*card_cur >= minimum_age) {
65 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -070066 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
67 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 }
69 ++card_cur;
70 }
71
72 byte* aligned_end = card_end -
73 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
74
Mathieu Chartier720ef762013-08-17 14:46:54 -070075 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070076 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
77 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070078 while (LIKELY(*word_cur == 0)) {
79 ++word_cur;
80 if (UNLIKELY(word_cur >= word_end)) {
81 goto exit_for;
82 }
83 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 uintptr_t start_word = *word_cur;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070087 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<byte*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070088 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
89 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070091 if (static_cast<byte>(start_word) >= minimum_age) {
92 auto* card = reinterpret_cast<byte*>(word_cur) + i;
93 DCHECK(*card == static_cast<byte>(start_word) || *card == kCardDirty)
94 << "card " << static_cast<size_t>(*card) << " word " << (start_word & 0xFF);
95 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070096 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 }
98 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070099 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700102 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103
104 // Handle any unaligned cards at the end.
105 card_cur = reinterpret_cast<byte*>(word_end);
106 while (card_cur < card_end) {
107 if (*card_cur >= minimum_age) {
108 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700109 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
110 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 }
112 ++card_cur;
113 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700114
115 return cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116}
117
118/*
119 * Visitor is expected to take in a card and return the new value. When a value is modified, the
120 * modify visitor is called.
121 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
122 * value.
123 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
124 * us to know which cards got cleared.
125 */
126template <typename Visitor, typename ModifiedVisitor>
127inline void CardTable::ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
128 const ModifiedVisitor& modified) {
129 byte* card_cur = CardFromAddr(scan_begin);
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800130 byte* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 CheckCardValid(card_cur);
132 CheckCardValid(card_end);
133
134 // Handle any unaligned cards at the start.
135 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
136 byte expected, new_value;
137 do {
138 expected = *card_cur;
139 new_value = visitor(expected);
140 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
141 if (expected != new_value) {
142 modified(card_cur, expected, new_value);
143 }
144 ++card_cur;
145 }
146
147 // Handle unaligned cards at the end.
148 while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) {
149 --card_end;
150 byte expected, new_value;
151 do {
152 expected = *card_end;
153 new_value = visitor(expected);
154 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
155 if (expected != new_value) {
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800156 modified(card_end, expected, new_value);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 }
158 }
159
160 // Now we have the words, we can process words in parallel.
161 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
162 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800163 // TODO: This is not big endian safe.
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800164 union {
165 uintptr_t expected_word;
166 uint8_t expected_bytes[sizeof(uintptr_t)];
167 };
168 union {
169 uintptr_t new_word;
170 uint8_t new_bytes[sizeof(uintptr_t)];
171 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172
173 // TODO: Parallelize.
174 while (word_cur < word_end) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800175 while (true) {
176 expected_word = *word_cur;
177 if (LIKELY(expected_word == 0)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 break;
179 }
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800180 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
181 new_bytes[i] = visitor(expected_bytes[i]);
182 }
Ian Rogers14611442014-07-08 22:55:18 -0700183 Atomic<uintptr_t>* atomic_word = reinterpret_cast<Atomic<uintptr_t>*>(word_cur);
184 if (LIKELY(atomic_word->CompareExchangeWeakRelaxed(expected_word, new_word))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800186 const byte expected_byte = expected_bytes[i];
187 const byte new_byte = new_bytes[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 if (expected_byte != new_byte) {
189 modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
190 }
191 }
192 break;
193 }
194 }
195 ++word_cur;
196 }
197}
198
199inline void* CardTable::AddrFromCard(const byte *card_addr) const {
200 DCHECK(IsValidCard(card_addr))
201 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
202 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
203 << " end: " << reinterpret_cast<void*>(mem_map_->End());
204 uintptr_t offset = card_addr - biased_begin_;
205 return reinterpret_cast<void*>(offset << kCardShift);
206}
207
208inline byte* CardTable::CardFromAddr(const void *addr) const {
209 byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
210 // Sanity check the caller was asking for address covered by the card table
211 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
212 << " card_addr: " << reinterpret_cast<void*>(card_addr);
213 return card_addr;
214}
215
216inline void CardTable::CheckCardValid(byte* card) const {
217 DCHECK(IsValidCard(card))
218 << " card_addr: " << reinterpret_cast<const void*>(card)
219 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
220 << " end: " << reinterpret_cast<void*>(mem_map_->End());
221}
222
Ian Rogers1d54e732013-05-02 21:10:01 -0700223} // namespace accounting
224} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225} // namespace art
226
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700227#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_