blob: 0cfbd6fa9e039775e754e3ac7d4a1bdf73a15ba0 [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>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045inline void CardTable::Scan(SpaceBitmap* bitmap, byte* scan_begin, byte* scan_end,
Mathieu Chartier184e3222013-08-03 14:02:57 -070046 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);
53
54 // Handle any unaligned cards at the start.
55 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
56 if (*card_cur >= minimum_age) {
57 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
58 uintptr_t end = start + kCardSize;
Mathieu Chartier184e3222013-08-03 14:02:57 -070059 bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 }
61 ++card_cur;
62 }
63
64 byte* aligned_end = card_end -
65 (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
66
Mathieu Chartier720ef762013-08-17 14:46:54 -070067 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
Mathieu Chartierc4621982013-09-16 19:43:47 -070068 for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
69 ++word_cur) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070070 while (LIKELY(*word_cur == 0)) {
71 ++word_cur;
72 if (UNLIKELY(word_cur >= word_end)) {
73 goto exit_for;
74 }
75 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 // Find the first dirty card.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 uintptr_t start_word = *word_cur;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070079 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<byte*>(word_cur)));
Mathieu Chartierc4621982013-09-16 19:43:47 -070080 // TODO: Investigate if processing continuous runs of dirty cards with a single bitmap visit is
81 // more efficient.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
Mathieu Chartier94c32c52013-08-09 11:14:04 -070083 if (static_cast<byte>(start_word) >= minimum_age) {
84 auto* card = reinterpret_cast<byte*>(word_cur) + i;
85 DCHECK(*card == static_cast<byte>(start_word) || *card == kCardDirty)
86 << "card " << static_cast<size_t>(*card) << " word " << (start_word & 0xFF);
87 bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 }
89 start_word >>= 8;
Mathieu Chartier94c32c52013-08-09 11:14:04 -070090 start += kCardSize;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 }
Mathieu Chartier94c32c52013-08-09 11:14:04 -070093 exit_for:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
95 // Handle any unaligned cards at the end.
96 card_cur = reinterpret_cast<byte*>(word_end);
97 while (card_cur < card_end) {
98 if (*card_cur >= minimum_age) {
99 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
100 uintptr_t end = start + kCardSize;
Mathieu Chartier184e3222013-08-03 14:02:57 -0700101 bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 }
103 ++card_cur;
104 }
105}
106
107/*
108 * Visitor is expected to take in a card and return the new value. When a value is modified, the
109 * modify visitor is called.
110 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
111 * value.
112 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
113 * us to know which cards got cleared.
114 */
115template <typename Visitor, typename ModifiedVisitor>
116inline void CardTable::ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
117 const ModifiedVisitor& modified) {
118 byte* card_cur = CardFromAddr(scan_begin);
119 byte* card_end = CardFromAddr(scan_end);
120 CheckCardValid(card_cur);
121 CheckCardValid(card_end);
122
123 // Handle any unaligned cards at the start.
124 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
125 byte expected, new_value;
126 do {
127 expected = *card_cur;
128 new_value = visitor(expected);
129 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
130 if (expected != new_value) {
131 modified(card_cur, expected, new_value);
132 }
133 ++card_cur;
134 }
135
136 // Handle unaligned cards at the end.
137 while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) {
138 --card_end;
139 byte expected, new_value;
140 do {
141 expected = *card_end;
142 new_value = visitor(expected);
143 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
144 if (expected != new_value) {
145 modified(card_cur, expected, new_value);
146 }
147 }
148
149 // Now we have the words, we can process words in parallel.
150 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
151 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
152 uintptr_t expected_word;
153 uintptr_t new_word;
154
155 // TODO: Parallelize.
156 while (word_cur < word_end) {
157 while ((expected_word = *word_cur) != 0) {
158 new_word =
159 (visitor((expected_word >> 0) & 0xFF) << 0) |
160 (visitor((expected_word >> 8) & 0xFF) << 8) |
161 (visitor((expected_word >> 16) & 0xFF) << 16) |
162 (visitor((expected_word >> 24) & 0xFF) << 24);
163 if (new_word == expected_word) {
164 // No need to do a cas.
165 break;
166 }
167 if (LIKELY(android_atomic_cas(expected_word, new_word,
168 reinterpret_cast<int32_t*>(word_cur)) == 0)) {
169 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
170 const byte expected_byte = (expected_word >> (8 * i)) & 0xFF;
171 const byte new_byte = (new_word >> (8 * i)) & 0xFF;
172 if (expected_byte != new_byte) {
173 modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
174 }
175 }
176 break;
177 }
178 }
179 ++word_cur;
180 }
181}
182
183inline void* CardTable::AddrFromCard(const byte *card_addr) const {
184 DCHECK(IsValidCard(card_addr))
185 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
186 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
187 << " end: " << reinterpret_cast<void*>(mem_map_->End());
188 uintptr_t offset = card_addr - biased_begin_;
189 return reinterpret_cast<void*>(offset << kCardShift);
190}
191
192inline byte* CardTable::CardFromAddr(const void *addr) const {
193 byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
194 // Sanity check the caller was asking for address covered by the card table
195 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
196 << " card_addr: " << reinterpret_cast<void*>(card_addr);
197 return card_addr;
198}
199
200inline void CardTable::CheckCardValid(byte* card) const {
201 DCHECK(IsValidCard(card))
202 << " card_addr: " << reinterpret_cast<const void*>(card)
203 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
204 << " end: " << reinterpret_cast<void*>(mem_map_->End());
205}
206
Ian Rogers1d54e732013-05-02 21:10:01 -0700207} // namespace accounting
208} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209} // namespace art
210
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700211#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_