blob: fb0f61b3b27bb24afb75bb508bd8d3cc7c0f82df [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
67 // Now we have the words, we can send these to be processed in parallel.
68 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
69 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
70
71 // TODO: Parallelize
72 while (word_cur < word_end) {
73 // Find the first dirty card.
74 while (*word_cur == 0 && word_cur < word_end) {
75 word_cur++;
76 }
77 if (word_cur >= word_end) {
78 break;
79 }
80 uintptr_t start_word = *word_cur;
81 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
82 if ((start_word & 0xFF) >= minimum_age) {
83 byte* card = reinterpret_cast<byte*>(word_cur) + i;
84 const byte card_byte = *card;
85 DCHECK(card_byte == (start_word & 0xFF) || card_byte == kCardDirty)
86 << "card " << static_cast<size_t>(card_byte) << " word " << (start_word & 0xFF);
87 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card));
88 uintptr_t end = start + kCardSize;
Mathieu Chartier184e3222013-08-03 14:02:57 -070089 bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 }
91 start_word >>= 8;
92 }
93 ++word_cur;
94 }
95
96 // Handle any unaligned cards at the end.
97 card_cur = reinterpret_cast<byte*>(word_end);
98 while (card_cur < card_end) {
99 if (*card_cur >= minimum_age) {
100 uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
101 uintptr_t end = start + kCardSize;
Mathieu Chartier184e3222013-08-03 14:02:57 -0700102 bitmap->VisitMarkedRange(start, end, visitor);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 }
104 ++card_cur;
105 }
106}
107
108/*
109 * Visitor is expected to take in a card and return the new value. When a value is modified, the
110 * modify visitor is called.
111 * visitor: The visitor which modifies the cards. Returns the new value for a card given an old
112 * value.
113 * modified: Whenever the visitor modifies a card, this visitor is called on the card. Enables
114 * us to know which cards got cleared.
115 */
116template <typename Visitor, typename ModifiedVisitor>
117inline void CardTable::ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
118 const ModifiedVisitor& modified) {
119 byte* card_cur = CardFromAddr(scan_begin);
120 byte* card_end = CardFromAddr(scan_end);
121 CheckCardValid(card_cur);
122 CheckCardValid(card_end);
123
124 // Handle any unaligned cards at the start.
125 while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
126 byte expected, new_value;
127 do {
128 expected = *card_cur;
129 new_value = visitor(expected);
130 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
131 if (expected != new_value) {
132 modified(card_cur, expected, new_value);
133 }
134 ++card_cur;
135 }
136
137 // Handle unaligned cards at the end.
138 while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) {
139 --card_end;
140 byte expected, new_value;
141 do {
142 expected = *card_end;
143 new_value = visitor(expected);
144 } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
145 if (expected != new_value) {
146 modified(card_cur, expected, new_value);
147 }
148 }
149
150 // Now we have the words, we can process words in parallel.
151 uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
152 uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
153 uintptr_t expected_word;
154 uintptr_t new_word;
155
156 // TODO: Parallelize.
157 while (word_cur < word_end) {
158 while ((expected_word = *word_cur) != 0) {
159 new_word =
160 (visitor((expected_word >> 0) & 0xFF) << 0) |
161 (visitor((expected_word >> 8) & 0xFF) << 8) |
162 (visitor((expected_word >> 16) & 0xFF) << 16) |
163 (visitor((expected_word >> 24) & 0xFF) << 24);
164 if (new_word == expected_word) {
165 // No need to do a cas.
166 break;
167 }
168 if (LIKELY(android_atomic_cas(expected_word, new_word,
169 reinterpret_cast<int32_t*>(word_cur)) == 0)) {
170 for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
171 const byte expected_byte = (expected_word >> (8 * i)) & 0xFF;
172 const byte new_byte = (new_word >> (8 * i)) & 0xFF;
173 if (expected_byte != new_byte) {
174 modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
175 }
176 }
177 break;
178 }
179 }
180 ++word_cur;
181 }
182}
183
184inline void* CardTable::AddrFromCard(const byte *card_addr) const {
185 DCHECK(IsValidCard(card_addr))
186 << " card_addr: " << reinterpret_cast<const void*>(card_addr)
187 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
188 << " end: " << reinterpret_cast<void*>(mem_map_->End());
189 uintptr_t offset = card_addr - biased_begin_;
190 return reinterpret_cast<void*>(offset << kCardShift);
191}
192
193inline byte* CardTable::CardFromAddr(const void *addr) const {
194 byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
195 // Sanity check the caller was asking for address covered by the card table
196 DCHECK(IsValidCard(card_addr)) << "addr: " << addr
197 << " card_addr: " << reinterpret_cast<void*>(card_addr);
198 return card_addr;
199}
200
201inline void CardTable::CheckCardValid(byte* card) const {
202 DCHECK(IsValidCard(card))
203 << " card_addr: " << reinterpret_cast<const void*>(card)
204 << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
205 << " end: " << reinterpret_cast<void*>(mem_map_->End());
206}
207
Ian Rogers1d54e732013-05-02 21:10:01 -0700208} // namespace accounting
209} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210} // namespace art
211
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700212#endif // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_