blob: 3b06f74d723237d450fe0fda47c5c0534cae033f [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;
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
Mathieu Chartier184e3222013-08-03 14:02:57 -070051template <typename Visitor>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070052inline size_t CardTable::Scan(ContinuousSpaceBitmap* bitmap, byte* scan_begin, byte* scan_end,
Mathieu Chartier0f72e412013-09-06 16:40:01 -070053 const Visitor& visitor, const byte minimum_age) const {
Zuo Wangf37a88b2014-07-10 04:26:41 -070054 DCHECK_GE(scan_begin, reinterpret_cast<byte*>(bitmap->HeapBegin()));
55 // scan_end is the byte after the last byte we scan.
56 DCHECK_LE(scan_end, reinterpret_cast<byte*>(bitmap->HeapLimit()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 byte* card_cur = CardFromAddr(scan_begin);
Mathieu Chartierd35326f2014-08-18 15:02:59 -070058 byte* 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.
64 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
65 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;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 }
70 ++card_cur;
71 }
72
73 byte* aligned_end = card_end -
74 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
75
Mathieu Chartier720ef762013-08-17 14:46:54 -070076 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070077 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
78 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070079 while (LIKELY(*word_cur == 0)) {
80 ++word_cur;
81 if (UNLIKELY(word_cur >= word_end)) {
82 goto exit_for;
83 }
84 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 uintptr_t start_word = *word_cur;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070088 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<byte*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070089 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
90 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070092 if (static_cast<byte>(start_word) >= minimum_age) {
93 auto* card = reinterpret_cast<byte*>(word_cur) + i;
94 DCHECK(*card == static_cast<byte>(start_word) || *card == kCardDirty)
95 << "card " << static_cast<size_t>(*card) << " word " << (start_word & 0xFF);
96 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Mathieu Chartier0f72e412013-09-06 16:40:01 -070097 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 }
99 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700100 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -0700103 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104
105 // Handle any unaligned cards at the end.
106 card_cur = reinterpret_cast<byte*>(word_end);
107 while (card_cur < card_end) {
108 if (*card_cur >= minimum_age) {
109 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700110 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
111 ++cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 }
113 ++card_cur;
114 }
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700115
116 return cards_scanned;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117}
118
119/*
120 * Visitor is expected to take in a card and return the new value. When a value is modified, the
121 * modify visitor is called.
122 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
123 * value.
124 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
125 * us to know which cards got cleared.
126 */
127template <typename Visitor, typename ModifiedVisitor>
128inline void CardTable::ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
129 const ModifiedVisitor& modified) {
130 byte* card_cur = CardFromAddr(scan_begin);
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800131 byte* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 CheckCardValid(card_cur);
133 CheckCardValid(card_end);
134
135 // Handle any unaligned cards at the start.
136 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
137 byte expected, new_value;
138 do {
139 expected = *card_cur;
140 new_value = visitor(expected);
141 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
142 if (expected != new_value) {
143 modified(card_cur, expected, new_value);
144 }
145 ++card_cur;
146 }
147
148 // Handle unaligned cards at the end.
149 while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) {
150 --card_end;
151 byte expected, new_value;
152 do {
153 expected = *card_end;
154 new_value = visitor(expected);
155 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
156 if (expected != new_value) {
Mathieu Chartier2ef33c32014-01-17 10:27:09 -0800157 modified(card_end, expected, new_value);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 }
159 }
160
161 // Now we have the words, we can process words in parallel.
162 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
163 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
Mathieu Chartierc2f4d022014-03-03 16:11:42 -0800164 // TODO: This is not big endian safe.
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800165 union {
166 uintptr_t expected_word;
167 uint8_t expected_bytes[sizeof(uintptr_t)];
168 };
169 union {
170 uintptr_t new_word;
171 uint8_t new_bytes[sizeof(uintptr_t)];
172 };
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173
174 // TODO: Parallelize.
175 while (word_cur < word_end) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800176 while (true) {
177 expected_word = *word_cur;
178 if (LIKELY(expected_word == 0)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 break;
180 }
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800181 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
182 new_bytes[i] = visitor(expected_bytes[i]);
183 }
Ian Rogers14611442014-07-08 22:55:18 -0700184 Atomic<uintptr_t>* atomic_word = reinterpret_cast<Atomic<uintptr_t>*>(word_cur);
185 if (LIKELY(atomic_word->CompareExchangeWeakRelaxed(expected_word, new_word))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier6b8c5032014-03-02 18:01:13 -0800187 const byte expected_byte = expected_bytes[i];
188 const byte new_byte = new_bytes[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 if (expected_byte != new_byte) {
190 modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
191 }
192 }
193 break;
194 }
195 }
196 ++word_cur;
197 }
198}
199
200inline void* CardTable::AddrFromCard(const byte *card_addr) const {
201 DCHECK(IsValidCard(card_addr))
202 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
203 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
204 << " end: " << reinterpret_cast<void*>(mem_map_->End());
205 uintptr_t offset = card_addr - biased_begin_;
206 return reinterpret_cast<void*>(offset << kCardShift);
207}
208
209inline byte* CardTable::CardFromAddr(const void *addr) const {
210 byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
211 // Sanity check the caller was asking for address covered by the card table
212 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
213 << " card_addr: " << reinterpret_cast<void*>(card_addr);
214 return card_addr;
215}
216
217inline void CardTable::CheckCardValid(byte* card) const {
218 DCHECK(IsValidCard(card))
219 << " card_addr: " << reinterpret_cast<const void*>(card)
220 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
221 << " end: " << reinterpret_cast<void*>(mem_map_->End());
222}
223
Ian Rogers1d54e732013-05-02 21:10:01 -0700224} // namespace accounting
225} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226} // namespace art
227
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700228#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_