blob: 885491fac074ce5ad3a4bb156f3cf6a839142d22 [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
25#include "UniquePtr.h"
26#include "globals.h"
27#include "logging.h"
28#include "mem_map.h"
29#include "utils.h"
30
31namespace art {
32
33class Object;
34
35class SpaceBitmap {
36 public:
37 static const size_t kAlignment = 8;
38
39 typedef void Callback(Object* obj, void* arg);
40
41 typedef void ScanCallback(Object* obj, void* finger, void* arg);
42
43 typedef void SweepCallback(size_t ptr_count, Object** ptrs, void* arg);
44
45 // Initialize a HeapBitmap so that it points to a bitmap large enough to cover a heap at
46 // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
47 static SpaceBitmap* Create(const std::string& name, byte* heap_begin, size_t heap_capacity);
48
49 ~SpaceBitmap();
50
51 // <offset> is the difference from .base to a pointer address.
52 // <index> is the index of .bits that contains the bit representing
53 // <offset>.
54 static size_t OffsetToIndex(size_t offset) {
55 return offset / kAlignment / kBitsPerWord;
56 }
57
58 static uintptr_t IndexToOffset(size_t index) {
59 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
60 }
61
62 // Pack the bits in backwards so they come out in address order when using CLZ.
63 static word OffsetToMask(uintptr_t offset_) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070064 return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070065 }
66
67 inline void Set(const Object* obj) {
68 Modify(obj, true);
69 }
70
71 inline void Clear(const Object* obj) {
72 Modify(obj, false);
73 }
74
75 void Clear();
76
77 inline bool Test(const Object* obj) const {
78 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
79 DCHECK(HasAddress(obj)) << obj;
80 DCHECK(bitmap_begin_ != NULL);
81 DCHECK_GE(addr, heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070082 const uintptr_t offset = addr - heap_begin_;
83 return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070084 }
85
Ian Rogers506de0c2012-09-17 15:39:06 -070086 // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
87 // even if a bit has not been set for it.
88 bool HasAddress(const void* obj) const {
89 // If obj < heap_begin_ then offset underflows to some very large value past the end of the
90 // bitmap.
91 const uintptr_t offset = (uintptr_t)obj - heap_begin_;
92 const size_t index = OffsetToIndex(offset);
93 return index < bitmap_size_ / kWordSize;
94 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070095
96 void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
97
98 class ClearVisitor {
99 public:
100 explicit ClearVisitor(SpaceBitmap* const bitmap)
101 : bitmap_(bitmap) {
102 }
103
104 void operator ()(Object* obj) const {
105 bitmap_->Clear(obj);
106 }
107 private:
108 SpaceBitmap* const bitmap_;
109 };
110
111 template <typename Visitor>
112 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
113 for (; visit_begin < visit_end; visit_begin += kAlignment ) {
114 visitor(reinterpret_cast<Object*>(visit_begin));
115 }
116 }
117
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700118 template <typename Visitor, typename FingerVisitor>
119 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end,
120 const Visitor& visitor, const FingerVisitor& finger_visitor) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700121 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700122 DCHECK_LT(visit_begin, visit_end);
123
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700124 const size_t word_span = kAlignment * kBitsPerWord; // Equals IndexToOffset(1).
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700125 const size_t bit_index_start = (visit_begin - heap_begin_) / kAlignment;
126 const size_t bit_index_end = (visit_end - heap_begin_ - 1) / kAlignment;
127
128 size_t word_start = bit_index_start / kBitsPerWord;
129 size_t word_end = bit_index_end / kBitsPerWord;
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700130 DCHECK_LT(word_end * kWordSize, Size());
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700131
132 // Trim off left_bits of left bits.
133 size_t edge_word = bitmap_begin_[word_start];
134
135 // Handle bits on the left first as a special case
136 size_t left_bits = bit_index_start & (kBitsPerWord - 1);
137 if (left_bits != 0) {
138 edge_word &= (1 << (kBitsPerWord - left_bits)) - 1;
139 }
140
141 // If word_start == word_end then handle this case at the same place we handle the right edge.
142 if (edge_word != 0 && word_start < word_end) {
143 uintptr_t ptr_base = IndexToOffset(word_start) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700144 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700145 do {
146 const size_t shift = CLZ(edge_word);
147 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
148 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700149 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700150 } while (edge_word != 0);
151 }
152 word_start++;
153
154 for (size_t i = word_start; i < word_end; i++) {
155 size_t w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700156 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700157 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700158 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700159 do {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700160 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700161 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
162 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700163 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700164 } while (w != 0);
165 }
166 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700167
168 // Handle the right edge, and also the left edge if both edges are on the same word.
169 size_t right_bits = bit_index_end & (kBitsPerWord - 1);
170
171 // If word_start == word_end then we need to use the word which we removed the left bits.
172 if (word_start <= word_end) {
173 edge_word = bitmap_begin_[word_end];
174 }
175
176 // Bits that we trim off the right.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700177 edge_word &= ~((static_cast<size_t>(kWordHighBitMask) >> right_bits) - 1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700178 uintptr_t ptr_base = IndexToOffset(word_end) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700179 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700180 while (edge_word != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700181 const size_t shift = CLZ(edge_word);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700182 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
183 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700184 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700185 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700186 }
187
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700188 void Walk(Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700189 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700190
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191 void InOrderWalk(Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700192 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700194
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700195 static void SweepWalk(const SpaceBitmap& live,
196 const SpaceBitmap& mark,
197 uintptr_t base, uintptr_t max,
198 SweepCallback* thunk, void* arg);
199
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700200 void CopyFrom(SpaceBitmap* source_bitmap);
201
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700202 // Starting address of our internal storage.
203 word* Begin() {
204 return bitmap_begin_;
205 }
206
207 // Size of our internal storage
208 size_t Size() const {
209 return bitmap_size_;
210 }
211
212 // Size in bytes of the memory that the bitmaps spans.
213 size_t HeapSize() const {
214 return IndexToOffset(Size() / kWordSize);
215 }
216
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700217 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700218 return heap_begin_;
219 }
220
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700221 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
222 uintptr_t HeapLimit() const {
223 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
224 }
225
226 // Set the max address which can covered by the bitmap.
227 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700228
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700229 std::string GetName() const;
230 void SetName(const std::string& name);
231
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700232 private:
233 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
234 // however, we document that this is expected on heap_end_
235 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin)
236 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700237 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700238 name_(name) {}
239
240 inline void Modify(const Object* obj, bool do_set) {
241 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
242 DCHECK_GE(addr, heap_begin_);
243 const uintptr_t offset = addr - heap_begin_;
244 const size_t index = OffsetToIndex(offset);
245 const word mask = OffsetToMask(offset);
246 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
247 if (do_set) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700248 bitmap_begin_[index] |= mask;
249 } else {
250 bitmap_begin_[index] &= ~mask;
251 }
252 }
253
254 // Backing storage for bitmap.
255 UniquePtr<MemMap> mem_map_;
256
257 // This bitmap itself, word sized for efficiency in scanning.
258 word* const bitmap_begin_;
259
260 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700261 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700262
263 // The base address of the heap, which corresponds to the word containing the first bit in the
264 // bitmap.
265 const uintptr_t heap_begin_;
266
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700267 // Name of this bitmap.
268 std::string name_;
269};
270
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700271// Like a bitmap except it keeps track of objects using sets.
272class SpaceSetMap {
273 public:
274 typedef std::set<const Object*> Objects;
275
276 bool IsEmpty() const {
277 return contained_.empty();
278 }
279
280 inline void Set(const Object* obj) {
281 contained_.insert(obj);
282 }
283
284 inline void Clear(const Object* obj) {
285 Objects::iterator found = contained_.find(obj);
286 if (found != contained_.end()) {
287 contained_.erase(found);
288 }
289 }
290
291 void Clear() {
292 contained_.clear();
293 }
294
295 inline bool Test(const Object* obj) const {
296 return contained_.find(obj) != contained_.end();
297 }
298
299 std::string GetName() const;
300 void SetName(const std::string& name);
301
302 void Walk(SpaceBitmap::Callback* callback, void* arg)
303 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
304
305 void CopyFrom(const SpaceSetMap& space_set);
306
307 template <typename Visitor>
308 void Visit(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS {
309 for (Objects::iterator it = contained_.begin(); it != contained_.end(); ++it) {
310 visitor(*it);
311 }
312 }
313
314 SpaceSetMap(const std::string& name);
315
316 Objects& GetObjects() {
317 return contained_;
318 }
319
320 private:
321 std::string name_;
322 Objects contained_;
323};
324
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700325std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
326
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700327} // namespace art
328
329#endif // ART_SRC_SPACE_BITMAP_H_