Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 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_SRC_SPACE_BITMAP_H_ |
| 18 | #define ART_SRC_SPACE_BITMAP_H_ |
| 19 | |
| 20 | #include <limits.h> |
| 21 | #include <stdint.h> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "UniquePtr.h" |
| 25 | #include "globals.h" |
| 26 | #include "logging.h" |
| 27 | #include "mem_map.h" |
| 28 | #include "utils.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | class Object; |
| 33 | |
| 34 | class SpaceBitmap { |
| 35 | public: |
| 36 | static const size_t kAlignment = 8; |
| 37 | |
| 38 | typedef void Callback(Object* obj, void* arg); |
| 39 | |
| 40 | typedef void ScanCallback(Object* obj, void* finger, void* arg); |
| 41 | |
| 42 | typedef void SweepCallback(size_t ptr_count, Object** ptrs, void* arg); |
| 43 | |
| 44 | // Initialize a HeapBitmap so that it points to a bitmap large enough to cover a heap at |
| 45 | // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned. |
| 46 | static SpaceBitmap* Create(const std::string& name, byte* heap_begin, size_t heap_capacity); |
| 47 | |
| 48 | ~SpaceBitmap(); |
| 49 | |
| 50 | // <offset> is the difference from .base to a pointer address. |
| 51 | // <index> is the index of .bits that contains the bit representing |
| 52 | // <offset>. |
| 53 | static size_t OffsetToIndex(size_t offset) { |
| 54 | return offset / kAlignment / kBitsPerWord; |
| 55 | } |
| 56 | |
| 57 | static uintptr_t IndexToOffset(size_t index) { |
| 58 | return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord); |
| 59 | } |
| 60 | |
| 61 | // Pack the bits in backwards so they come out in address order when using CLZ. |
| 62 | static word OffsetToMask(uintptr_t offset_) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 63 | return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | inline void Set(const Object* obj) { |
| 67 | Modify(obj, true); |
| 68 | } |
| 69 | |
| 70 | inline void Clear(const Object* obj) { |
| 71 | Modify(obj, false); |
| 72 | } |
| 73 | |
| 74 | void Clear(); |
| 75 | |
| 76 | inline bool Test(const Object* obj) const { |
| 77 | uintptr_t addr = reinterpret_cast<uintptr_t>(obj); |
| 78 | DCHECK(HasAddress(obj)) << obj; |
| 79 | DCHECK(bitmap_begin_ != NULL); |
| 80 | DCHECK_GE(addr, heap_begin_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 81 | const uintptr_t offset = addr - heap_begin_; |
| 82 | return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Ian Rogers | 506de0c | 2012-09-17 15:39:06 -0700 | [diff] [blame^] | 85 | // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover, |
| 86 | // even if a bit has not been set for it. |
| 87 | bool HasAddress(const void* obj) const { |
| 88 | // If obj < heap_begin_ then offset underflows to some very large value past the end of the |
| 89 | // bitmap. |
| 90 | const uintptr_t offset = (uintptr_t)obj - heap_begin_; |
| 91 | const size_t index = OffsetToIndex(offset); |
| 92 | return index < bitmap_size_ / kWordSize; |
| 93 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 94 | |
| 95 | void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const; |
| 96 | |
| 97 | class ClearVisitor { |
| 98 | public: |
| 99 | explicit ClearVisitor(SpaceBitmap* const bitmap) |
| 100 | : bitmap_(bitmap) { |
| 101 | } |
| 102 | |
| 103 | void operator ()(Object* obj) const { |
| 104 | bitmap_->Clear(obj); |
| 105 | } |
| 106 | private: |
| 107 | SpaceBitmap* const bitmap_; |
| 108 | }; |
| 109 | |
| 110 | template <typename Visitor> |
| 111 | void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const { |
| 112 | for (; visit_begin < visit_end; visit_begin += kAlignment ) { |
| 113 | visitor(reinterpret_cast<Object*>(visit_begin)); |
| 114 | } |
| 115 | } |
| 116 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 117 | template <typename Visitor, typename FingerVisitor> |
| 118 | void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, |
| 119 | const Visitor& visitor, const FingerVisitor& finger_visitor) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 120 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 121 | DCHECK_LT(visit_begin, visit_end); |
| 122 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 123 | const size_t word_span = kAlignment * kBitsPerWord; // Equals IndexToOffset(1). |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 124 | const size_t bit_index_start = (visit_begin - heap_begin_) / kAlignment; |
| 125 | const size_t bit_index_end = (visit_end - heap_begin_ - 1) / kAlignment; |
| 126 | |
| 127 | size_t word_start = bit_index_start / kBitsPerWord; |
| 128 | size_t word_end = bit_index_end / kBitsPerWord; |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 129 | DCHECK_LT(word_end * kWordSize, Size()); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 130 | |
| 131 | // Trim off left_bits of left bits. |
| 132 | size_t edge_word = bitmap_begin_[word_start]; |
| 133 | |
| 134 | // Handle bits on the left first as a special case |
| 135 | size_t left_bits = bit_index_start & (kBitsPerWord - 1); |
| 136 | if (left_bits != 0) { |
| 137 | edge_word &= (1 << (kBitsPerWord - left_bits)) - 1; |
| 138 | } |
| 139 | |
| 140 | // If word_start == word_end then handle this case at the same place we handle the right edge. |
| 141 | if (edge_word != 0 && word_start < word_end) { |
| 142 | uintptr_t ptr_base = IndexToOffset(word_start) + heap_begin_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 143 | finger_visitor(reinterpret_cast<void*>(ptr_base + word_span)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 144 | do { |
| 145 | const size_t shift = CLZ(edge_word); |
| 146 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 147 | visitor(obj); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 148 | edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 149 | } while (edge_word != 0); |
| 150 | } |
| 151 | word_start++; |
| 152 | |
| 153 | for (size_t i = word_start; i < word_end; i++) { |
| 154 | size_t w = bitmap_begin_[i]; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 155 | if (w != 0) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 156 | uintptr_t ptr_base = IndexToOffset(i) + heap_begin_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 157 | finger_visitor(reinterpret_cast<void*>(ptr_base + word_span)); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 158 | do { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 159 | const size_t shift = CLZ(w); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 160 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 161 | visitor(obj); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 162 | w ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 163 | } while (w != 0); |
| 164 | } |
| 165 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 166 | |
| 167 | // Handle the right edge, and also the left edge if both edges are on the same word. |
| 168 | size_t right_bits = bit_index_end & (kBitsPerWord - 1); |
| 169 | |
| 170 | // If word_start == word_end then we need to use the word which we removed the left bits. |
| 171 | if (word_start <= word_end) { |
| 172 | edge_word = bitmap_begin_[word_end]; |
| 173 | } |
| 174 | |
| 175 | // Bits that we trim off the right. |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 176 | edge_word &= ~((static_cast<size_t>(kWordHighBitMask) >> right_bits) - 1); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 177 | uintptr_t ptr_base = IndexToOffset(word_end) + heap_begin_; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 178 | finger_visitor(reinterpret_cast<void*>(ptr_base + word_span)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 179 | while (edge_word != 0) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 180 | const size_t shift = CLZ(edge_word); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 181 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 182 | visitor(obj); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 183 | edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 184 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 187 | void Walk(Callback* callback, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 188 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 189 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 190 | void InOrderWalk(Callback* callback, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 191 | SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 192 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 193 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 194 | static void SweepWalk(const SpaceBitmap& live, |
| 195 | const SpaceBitmap& mark, |
| 196 | uintptr_t base, uintptr_t max, |
| 197 | SweepCallback* thunk, void* arg); |
| 198 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 199 | void CopyFrom(SpaceBitmap* source_bitmap); |
| 200 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 201 | // Starting address of our internal storage. |
| 202 | word* Begin() { |
| 203 | return bitmap_begin_; |
| 204 | } |
| 205 | |
| 206 | // Size of our internal storage |
| 207 | size_t Size() const { |
| 208 | return bitmap_size_; |
| 209 | } |
| 210 | |
| 211 | // Size in bytes of the memory that the bitmaps spans. |
| 212 | size_t HeapSize() const { |
| 213 | return IndexToOffset(Size() / kWordSize); |
| 214 | } |
| 215 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 216 | uintptr_t HeapBegin() const { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 217 | return heap_begin_; |
| 218 | } |
| 219 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 220 | // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()). |
| 221 | uintptr_t HeapLimit() const { |
| 222 | return HeapBegin() + static_cast<uintptr_t>(HeapSize()); |
| 223 | } |
| 224 | |
| 225 | // Set the max address which can covered by the bitmap. |
| 226 | void SetHeapLimit(uintptr_t new_end); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 227 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 228 | std::string GetName() const; |
| 229 | void SetName(const std::string& name); |
| 230 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 231 | private: |
| 232 | // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1, |
| 233 | // however, we document that this is expected on heap_end_ |
| 234 | SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin) |
| 235 | : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size), |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 236 | heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)), |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 237 | name_(name) {} |
| 238 | |
| 239 | inline void Modify(const Object* obj, bool do_set) { |
| 240 | uintptr_t addr = reinterpret_cast<uintptr_t>(obj); |
| 241 | DCHECK_GE(addr, heap_begin_); |
| 242 | const uintptr_t offset = addr - heap_begin_; |
| 243 | const size_t index = OffsetToIndex(offset); |
| 244 | const word mask = OffsetToMask(offset); |
| 245 | DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_; |
| 246 | if (do_set) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 247 | bitmap_begin_[index] |= mask; |
| 248 | } else { |
| 249 | bitmap_begin_[index] &= ~mask; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Backing storage for bitmap. |
| 254 | UniquePtr<MemMap> mem_map_; |
| 255 | |
| 256 | // This bitmap itself, word sized for efficiency in scanning. |
| 257 | word* const bitmap_begin_; |
| 258 | |
| 259 | // Size of this bitmap. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 260 | size_t bitmap_size_; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 261 | |
| 262 | // The base address of the heap, which corresponds to the word containing the first bit in the |
| 263 | // bitmap. |
| 264 | const uintptr_t heap_begin_; |
| 265 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 266 | // Name of this bitmap. |
| 267 | std::string name_; |
| 268 | }; |
| 269 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 270 | std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap); |
| 271 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 272 | } // namespace art |
| 273 | |
| 274 | #endif // ART_SRC_SPACE_BITMAP_H_ |