blob: 8bd75d55c41ed56c6aef10fbe3c1d688c2dd17fe [file] [log] [blame]
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001/*
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_SRC_SPACE_BITMAP_H_
18#define ART_SRC_SPACE_BITMAP_H_
19
20#include <limits.h>
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070021#include <set>
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070022#include <stdint.h>
23#include <vector>
24
Mathieu Chartier02b6a782012-10-26 13:51:26 -070025#include "cutils/atomic.h"
26#include "cutils/atomic-inline.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070027#include "UniquePtr.h"
28#include "globals.h"
29#include "logging.h"
30#include "mem_map.h"
31#include "utils.h"
32
33namespace art {
34
35class Object;
36
37class SpaceBitmap {
38 public:
39 static const size_t kAlignment = 8;
40
41 typedef void Callback(Object* obj, void* arg);
42
43 typedef void ScanCallback(Object* obj, void* finger, void* arg);
44
45 typedef void SweepCallback(size_t ptr_count, Object** ptrs, void* arg);
46
47 // Initialize a HeapBitmap so that it points to a bitmap large enough to cover a heap at
48 // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
49 static SpaceBitmap* Create(const std::string& name, byte* heap_begin, size_t heap_capacity);
50
51 ~SpaceBitmap();
52
53 // <offset> is the difference from .base to a pointer address.
54 // <index> is the index of .bits that contains the bit representing
55 // <offset>.
56 static size_t OffsetToIndex(size_t offset) {
57 return offset / kAlignment / kBitsPerWord;
58 }
59
60 static uintptr_t IndexToOffset(size_t index) {
61 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
62 }
63
64 // Pack the bits in backwards so they come out in address order when using CLZ.
65 static word OffsetToMask(uintptr_t offset_) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070066 return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070067 }
68
Mathieu Chartier02b6a782012-10-26 13:51:26 -070069 inline bool Set(const Object* obj) {
70 return Modify(obj, true);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070071 }
72
Mathieu Chartier02b6a782012-10-26 13:51:26 -070073 inline bool Clear(const Object* obj) {
74 return Modify(obj, false);
75 }
76
77 // Returns true if the object was previously marked.
78 inline bool AtomicTestAndSet(const Object* obj) {
79 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
80 DCHECK_GE(addr, heap_begin_);
81 const uintptr_t offset = addr - heap_begin_;
82 const size_t index = OffsetToIndex(offset);
83 const word mask = OffsetToMask(offset);
84 word* const address = &bitmap_begin_[index];
85 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
86 word old_word;
87 do {
88 old_word = *address;
89 // Fast path: The bit is already set.
90 if ((old_word & mask) != 0) {
91 return true;
92 }
93 } while (UNLIKELY(android_atomic_cas(old_word, old_word | mask, address) != 0));
94 return false;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070095 }
96
97 void Clear();
98
99 inline bool Test(const Object* obj) const {
100 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
101 DCHECK(HasAddress(obj)) << obj;
102 DCHECK(bitmap_begin_ != NULL);
103 DCHECK_GE(addr, heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700104 const uintptr_t offset = addr - heap_begin_;
105 return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700106 }
107
Ian Rogers506de0c2012-09-17 15:39:06 -0700108 // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
109 // even if a bit has not been set for it.
110 bool HasAddress(const void* obj) const {
111 // If obj < heap_begin_ then offset underflows to some very large value past the end of the
112 // bitmap.
buzbeecbd6d442012-11-17 14:11:25 -0800113 const uintptr_t offset = reinterpret_cast<uintptr_t>(obj) - heap_begin_;
Ian Rogers506de0c2012-09-17 15:39:06 -0700114 const size_t index = OffsetToIndex(offset);
115 return index < bitmap_size_ / kWordSize;
116 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700117
118 void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
119
120 class ClearVisitor {
121 public:
122 explicit ClearVisitor(SpaceBitmap* const bitmap)
123 : bitmap_(bitmap) {
124 }
125
126 void operator ()(Object* obj) const {
127 bitmap_->Clear(obj);
128 }
129 private:
130 SpaceBitmap* const bitmap_;
131 };
132
133 template <typename Visitor>
134 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
135 for (; visit_begin < visit_end; visit_begin += kAlignment ) {
136 visitor(reinterpret_cast<Object*>(visit_begin));
137 }
138 }
139
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700140 template <typename Visitor, typename FingerVisitor>
141 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end,
142 const Visitor& visitor, const FingerVisitor& finger_visitor) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700144 DCHECK_LT(visit_begin, visit_end);
145
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700146 const size_t word_span = kAlignment * kBitsPerWord; // Equals IndexToOffset(1).
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700147 const size_t bit_index_start = (visit_begin - heap_begin_) / kAlignment;
148 const size_t bit_index_end = (visit_end - heap_begin_ - 1) / kAlignment;
149
150 size_t word_start = bit_index_start / kBitsPerWord;
151 size_t word_end = bit_index_end / kBitsPerWord;
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700152 DCHECK_LT(word_end * kWordSize, Size());
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700153
154 // Trim off left_bits of left bits.
155 size_t edge_word = bitmap_begin_[word_start];
156
157 // Handle bits on the left first as a special case
158 size_t left_bits = bit_index_start & (kBitsPerWord - 1);
159 if (left_bits != 0) {
160 edge_word &= (1 << (kBitsPerWord - left_bits)) - 1;
161 }
162
163 // If word_start == word_end then handle this case at the same place we handle the right edge.
164 if (edge_word != 0 && word_start < word_end) {
165 uintptr_t ptr_base = IndexToOffset(word_start) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700166 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700167 do {
168 const size_t shift = CLZ(edge_word);
169 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
170 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700171 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700172 } while (edge_word != 0);
173 }
174 word_start++;
175
176 for (size_t i = word_start; i < word_end; i++) {
177 size_t w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700178 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700179 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700180 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 do {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700182 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700183 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
184 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700185 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700186 } while (w != 0);
187 }
188 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700189
190 // Handle the right edge, and also the left edge if both edges are on the same word.
191 size_t right_bits = bit_index_end & (kBitsPerWord - 1);
192
193 // If word_start == word_end then we need to use the word which we removed the left bits.
194 if (word_start <= word_end) {
195 edge_word = bitmap_begin_[word_end];
196 }
197
198 // Bits that we trim off the right.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700199 edge_word &= ~((static_cast<size_t>(kWordHighBitMask) >> right_bits) - 1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700200 uintptr_t ptr_base = IndexToOffset(word_end) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700201 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700202 while (edge_word != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700203 const size_t shift = CLZ(edge_word);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700204 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
205 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700206 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700207 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700208 }
209
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700210 void Walk(Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700211 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700212
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 void InOrderWalk(Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700214 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700216
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700217 static void SweepWalk(const SpaceBitmap& live,
218 const SpaceBitmap& mark,
219 uintptr_t base, uintptr_t max,
220 SweepCallback* thunk, void* arg);
221
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700222 void CopyFrom(SpaceBitmap* source_bitmap);
223
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700224 // Starting address of our internal storage.
225 word* Begin() {
226 return bitmap_begin_;
227 }
228
229 // Size of our internal storage
230 size_t Size() const {
231 return bitmap_size_;
232 }
233
234 // Size in bytes of the memory that the bitmaps spans.
235 size_t HeapSize() const {
236 return IndexToOffset(Size() / kWordSize);
237 }
238
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700239 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700240 return heap_begin_;
241 }
242
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700243 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
244 uintptr_t HeapLimit() const {
245 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
246 }
247
248 // Set the max address which can covered by the bitmap.
249 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700250
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700251 std::string GetName() const;
252 void SetName(const std::string& name);
253
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700254 const void* GetObjectWordAddress(const Object* obj) const {
255 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
256 const uintptr_t offset = addr - heap_begin_;
257 const size_t index = OffsetToIndex(offset);
258 return &bitmap_begin_[index];
259 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700260 private:
261 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
262 // however, we document that this is expected on heap_end_
263 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin)
264 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700265 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700266 name_(name) {}
267
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700268 inline bool Modify(const Object* obj, bool do_set) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700269 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
270 DCHECK_GE(addr, heap_begin_);
271 const uintptr_t offset = addr - heap_begin_;
272 const size_t index = OffsetToIndex(offset);
273 const word mask = OffsetToMask(offset);
274 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700275 word* address = &bitmap_begin_[index];
276 word old_word = *address;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700277 if (do_set) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700278 *address = old_word | mask;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700279 } else {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700280 *address = old_word & ~mask;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700281 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700282 return (old_word & mask) != 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700283 }
284
285 // Backing storage for bitmap.
286 UniquePtr<MemMap> mem_map_;
287
288 // This bitmap itself, word sized for efficiency in scanning.
289 word* const bitmap_begin_;
290
291 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700292 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700293
294 // The base address of the heap, which corresponds to the word containing the first bit in the
295 // bitmap.
296 const uintptr_t heap_begin_;
297
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700298 // Name of this bitmap.
299 std::string name_;
300};
301
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700302// Like a bitmap except it keeps track of objects using sets.
303class SpaceSetMap {
304 public:
305 typedef std::set<const Object*> Objects;
306
307 bool IsEmpty() const {
308 return contained_.empty();
309 }
310
311 inline void Set(const Object* obj) {
312 contained_.insert(obj);
313 }
314
315 inline void Clear(const Object* obj) {
316 Objects::iterator found = contained_.find(obj);
317 if (found != contained_.end()) {
318 contained_.erase(found);
319 }
320 }
321
322 void Clear() {
323 contained_.clear();
324 }
325
326 inline bool Test(const Object* obj) const {
327 return contained_.find(obj) != contained_.end();
328 }
329
330 std::string GetName() const;
331 void SetName(const std::string& name);
332
333 void Walk(SpaceBitmap::Callback* callback, void* arg)
334 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
335
336 void CopyFrom(const SpaceSetMap& space_set);
337
338 template <typename Visitor>
339 void Visit(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS {
340 for (Objects::iterator it = contained_.begin(); it != contained_.end(); ++it) {
341 visitor(*it);
342 }
343 }
344
345 SpaceSetMap(const std::string& name);
346
347 Objects& GetObjects() {
348 return contained_;
349 }
350
351 private:
352 std::string name_;
353 Objects contained_;
354};
355
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700356std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
357
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700358} // namespace art
359
360#endif // ART_SRC_SPACE_BITMAP_H_