1 /*
2 * Copyright (C) 2008 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
17 #ifndef ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_INL_H_
18 #define ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_INL_H_
19
20 #include "space_bitmap.h"
21
22 #include <memory>
23
24 #include <android-base/logging.h>
25
26 #include "base/atomic.h"
27 #include "base/bit_utils.h"
28
29 namespace art HIDDEN {
30 namespace gc {
31 namespace accounting {
32
33 template<size_t kAlignment>
AtomicTestAndSet(const mirror::Object * obj)34 inline bool SpaceBitmap<kAlignment>::AtomicTestAndSet(const mirror::Object* obj) {
35 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
36 DCHECK_GE(addr, heap_begin_);
37 const uintptr_t offset = addr - heap_begin_;
38 const size_t index = OffsetToIndex(offset);
39 const uintptr_t mask = OffsetToMask(offset);
40 Atomic<uintptr_t>* atomic_entry = &bitmap_begin_[index];
41 DCHECK_LT(index, bitmap_size_ / sizeof(intptr_t)) << " bitmap_size_ = " << bitmap_size_;
42 uintptr_t old_word;
43 do {
44 old_word = atomic_entry->load(std::memory_order_relaxed);
45 // Fast path: The bit is already set.
46 if ((old_word & mask) != 0) {
47 DCHECK(Test(obj));
48 return true;
49 }
50 } while (!atomic_entry->CompareAndSetWeakRelaxed(old_word, old_word | mask));
51 DCHECK(Test(obj));
52 return false;
53 }
54
55 template<size_t kAlignment>
Test(const mirror::Object * obj)56 inline bool SpaceBitmap<kAlignment>::Test(const mirror::Object* obj) const {
57 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
58 DCHECK(HasAddress(obj)) << obj;
59 DCHECK(bitmap_begin_ != nullptr);
60 DCHECK_GE(addr, heap_begin_);
61 const uintptr_t offset = addr - heap_begin_;
62 size_t index = OffsetToIndex(offset);
63 return (bitmap_begin_[index].load(std::memory_order_relaxed) & OffsetToMask(offset)) != 0;
64 }
65
66 template<size_t kAlignment>
FindPrecedingObject(uintptr_t visit_begin,uintptr_t visit_end)67 inline mirror::Object* SpaceBitmap<kAlignment>::FindPrecedingObject(uintptr_t visit_begin,
68 uintptr_t visit_end) const {
69 // Covers [visit_end, visit_begin].
70 visit_end = std::max(heap_begin_, visit_end);
71 DCHECK_LE(visit_end, visit_begin);
72 DCHECK_LT(visit_begin, HeapLimit());
73
74 const uintptr_t offset_start = visit_begin - heap_begin_;
75 const uintptr_t offset_end = visit_end - heap_begin_;
76 uintptr_t index_start = OffsetToIndex(offset_start);
77 const uintptr_t index_end = OffsetToIndex(offset_end);
78
79 // Start with the right edge
80 uintptr_t word = bitmap_begin_[index_start].load(std::memory_order_relaxed);
81 // visit_begin could be the first word of the object we are looking for.
82 const uintptr_t right_edge_mask = OffsetToMask(offset_start);
83 word &= right_edge_mask | (right_edge_mask - 1);
84 while (index_start > index_end) {
85 if (word != 0) {
86 const uintptr_t ptr_base = IndexToOffset(index_start) + heap_begin_;
87 size_t pos_leading_set_bit = kBitsPerIntPtrT - CLZ(word) - 1;
88 return reinterpret_cast<mirror::Object*>(ptr_base + pos_leading_set_bit * kAlignment);
89 }
90 word = bitmap_begin_[--index_start].load(std::memory_order_relaxed);
91 }
92
93 word &= ~(OffsetToMask(offset_end) - 1);
94 if (word != 0) {
95 const uintptr_t ptr_base = IndexToOffset(index_end) + heap_begin_;
96 size_t pos_leading_set_bit = kBitsPerIntPtrT - CLZ(word) - 1;
97 return reinterpret_cast<mirror::Object*>(ptr_base + pos_leading_set_bit * kAlignment);
98 } else {
99 return nullptr;
100 }
101 }
102
103 template<size_t kAlignment>
104 template<bool kVisitOnce, typename Visitor>
VisitMarkedRange(uintptr_t visit_begin,uintptr_t visit_end,Visitor && visitor)105 inline void SpaceBitmap<kAlignment>::VisitMarkedRange(uintptr_t visit_begin,
106 uintptr_t visit_end,
107 Visitor&& visitor) const {
108 DCHECK_LE(visit_begin, visit_end);
109 #if 0
110 for (uintptr_t i = visit_begin; i < visit_end; i += kAlignment) {
111 mirror::Object* obj = reinterpret_cast<mirror::Object*>(i);
112 if (Test(obj)) {
113 visitor(obj);
114 }
115 }
116 #else
117 DCHECK_LE(heap_begin_, visit_begin);
118 DCHECK_LE(visit_end, HeapLimit());
119
120 const uintptr_t offset_start = visit_begin - heap_begin_;
121 const uintptr_t offset_end = visit_end - heap_begin_;
122
123 const uintptr_t index_start = OffsetToIndex(offset_start);
124 const uintptr_t index_end = OffsetToIndex(offset_end);
125
126 const size_t bit_start = (offset_start / kAlignment) % kBitsPerIntPtrT;
127 const size_t bit_end = (offset_end / kAlignment) % kBitsPerIntPtrT;
128
129 // Index(begin) ... Index(end)
130 // [xxxxx???][........][????yyyy]
131 // ^ ^
132 // | #---- Bit of visit_end
133 // #---- Bit of visit_begin
134 //
135
136 // Left edge.
137 uintptr_t left_edge = bitmap_begin_[index_start];
138 // Mark of lower bits that are not in range.
139 left_edge &= ~((static_cast<uintptr_t>(1) << bit_start) - 1);
140
141 // Right edge. Either unique, or left_edge.
142 uintptr_t right_edge;
143
144 if (index_start < index_end) {
145 // Left edge != right edge.
146
147 // Traverse left edge.
148 if (left_edge != 0) {
149 const uintptr_t ptr_base = IndexToOffset(index_start) + heap_begin_;
150 do {
151 const size_t shift = CTZ(left_edge);
152 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
153 visitor(obj);
154 if (kVisitOnce) {
155 return;
156 }
157 left_edge ^= (static_cast<uintptr_t>(1)) << shift;
158 } while (left_edge != 0);
159 }
160
161 // Traverse the middle, full part.
162 for (size_t i = index_start + 1; i < index_end; ++i) {
163 uintptr_t w = bitmap_begin_[i].load(std::memory_order_relaxed);
164 if (w != 0) {
165 const uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
166 // Iterate on the bits set in word `w`, from the least to the most significant bit.
167 do {
168 const size_t shift = CTZ(w);
169 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
170 visitor(obj);
171 if (kVisitOnce) {
172 return;
173 }
174 w ^= (static_cast<uintptr_t>(1)) << shift;
175 } while (w != 0);
176 }
177 }
178
179 // Right edge is unique.
180 // But maybe we don't have anything to do: visit_end starts in a new word...
181 if (bit_end == 0) {
182 // Do not read memory, as it could be after the end of the bitmap.
183 right_edge = 0;
184 } else {
185 right_edge = bitmap_begin_[index_end];
186 }
187 } else {
188 // Right edge = left edge.
189 right_edge = left_edge;
190 }
191
192 // Right edge handling.
193 right_edge &= ((static_cast<uintptr_t>(1) << bit_end) - 1);
194 if (right_edge != 0) {
195 const uintptr_t ptr_base = IndexToOffset(index_end) + heap_begin_;
196 // Iterate on the bits set in word `right_edge`, from the least to the most significant bit.
197 do {
198 const size_t shift = CTZ(right_edge);
199 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
200 visitor(obj);
201 if (kVisitOnce) {
202 return;
203 }
204 right_edge ^= (static_cast<uintptr_t>(1)) << shift;
205 } while (right_edge != 0);
206 }
207 #endif
208 }
209
210 template<size_t kAlignment>
211 template<typename Visitor>
Walk(Visitor && visitor)212 void SpaceBitmap<kAlignment>::Walk(Visitor&& visitor) {
213 CHECK(bitmap_begin_ != nullptr);
214
215 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
216 Atomic<uintptr_t>* bitmap_begin = bitmap_begin_;
217 for (uintptr_t i = 0; i <= end; ++i) {
218 uintptr_t w = bitmap_begin[i].load(std::memory_order_relaxed);
219 if (w != 0) {
220 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
221 do {
222 const size_t shift = CTZ(w);
223 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
224 visitor(obj);
225 w ^= (static_cast<uintptr_t>(1)) << shift;
226 } while (w != 0);
227 }
228 }
229 }
230
231 template<size_t kAlignment>
232 template<bool kSetBit>
Modify(const mirror::Object * obj)233 inline bool SpaceBitmap<kAlignment>::Modify(const mirror::Object* obj) {
234 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
235 DCHECK_GE(addr, heap_begin_);
236 DCHECK(HasAddress(obj)) << obj;
237 const uintptr_t offset = addr - heap_begin_;
238 const size_t index = OffsetToIndex(offset);
239 const uintptr_t mask = OffsetToMask(offset);
240 DCHECK_LT(index, bitmap_size_ / sizeof(intptr_t)) << " bitmap_size_ = " << bitmap_size_;
241 Atomic<uintptr_t>* atomic_entry = &bitmap_begin_[index];
242 uintptr_t old_word = atomic_entry->load(std::memory_order_relaxed);
243 if (kSetBit) {
244 // Check the bit before setting the word incase we are trying to mark a read only bitmap
245 // like an image space bitmap. This bitmap is mapped as read only and will fault if we
246 // attempt to change any words. Since all of the objects are marked, this will never
247 // occur if we check before setting the bit. This also prevents dirty pages that would
248 // occur if the bitmap was read write and we did not check the bit.
249 if ((old_word & mask) == 0) {
250 atomic_entry->store(old_word | mask, std::memory_order_relaxed);
251 }
252 } else {
253 atomic_entry->store(old_word & ~mask, std::memory_order_relaxed);
254 }
255 DCHECK_EQ(Test(obj), kSetBit);
256 return (old_word & mask) != 0;
257 }
258
259 template<size_t kAlignment>
260 inline std::ostream& operator << (std::ostream& stream, const SpaceBitmap<kAlignment>& bitmap) {
261 return stream
262 << bitmap.GetName() << "["
263 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
264 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
265 << "]";
266 }
267
268 } // namespace accounting
269 } // namespace gc
270 } // namespace art
271
272 #endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_INL_H_
273