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_H_
18 #define ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_
19 
20 #include <limits.h>
21 #include <stdint.h>
22 #include <memory>
23 #include <set>
24 #include <vector>
25 
26 #include "base/locks.h"
27 #include "base/mem_map.h"
28 #include "runtime_globals.h"
29 
30 namespace art {
31 
32 namespace mirror {
33 class Class;
34 class Object;
35 }  // namespace mirror
36 
37 namespace gc {
38 namespace accounting {
39 
40 template<size_t kAlignment>
41 class SpaceBitmap {
42  public:
43   typedef void ScanCallback(mirror::Object* obj, void* finger, void* arg);
44   typedef void SweepCallback(size_t ptr_count, mirror::Object** ptrs, void* arg);
45 
46   // Initialize a space bitmap so that it points to a bitmap large enough to cover a heap at
47   // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
48   static SpaceBitmap Create(const std::string& name, uint8_t* heap_begin, size_t heap_capacity);
49 
50   // Initialize a space bitmap using the provided mem_map as the live bits. Takes ownership of the
51   // mem map. The address range covered starts at heap_begin and is of size equal to heap_capacity.
52   // Objects are kAlignement-aligned.
53   static SpaceBitmap CreateFromMemMap(const std::string& name,
54                                       MemMap&& mem_map,
55                                       uint8_t* heap_begin,
56                                       size_t heap_capacity);
57 
58   ~SpaceBitmap();
59 
60   // Return the bitmap word index corresponding to memory offset (relative to
61   // `HeapBegin()`) `offset`.
62   // See also SpaceBitmap::OffsetBitIndex.
63   //
64   // <offset> is the difference from .base to a pointer address.
65   // <index> is the index of .bits that contains the bit representing
66   //         <offset>.
OffsetToIndex(size_t offset)67   static constexpr size_t OffsetToIndex(size_t offset) {
68     return offset / kAlignment / kBitsPerIntPtrT;
69   }
70 
71   // Return the memory offset (relative to `HeapBegin()`) corresponding to
72   // bitmap word index `index`.
73   template<typename T>
IndexToOffset(T index)74   static constexpr T IndexToOffset(T index) {
75     return static_cast<T>(index * kAlignment * kBitsPerIntPtrT);
76   }
77 
78   // Return the bit within the bitmap word index corresponding to
79   // memory offset (relative to `HeapBegin()`) `offset`.
80   // See also SpaceBitmap::OffsetToIndex.
OffsetBitIndex(uintptr_t offset)81   ALWAYS_INLINE static constexpr uintptr_t OffsetBitIndex(uintptr_t offset) {
82     return (offset / kAlignment) % kBitsPerIntPtrT;
83   }
84 
85   // Return the word-wide bit mask corresponding to `OffsetBitIndex(offset)`.
86   // Bits are packed in the obvious way.
OffsetToMask(uintptr_t offset)87   static constexpr uintptr_t OffsetToMask(uintptr_t offset) {
88     return static_cast<size_t>(1) << OffsetBitIndex(offset);
89   }
90 
91   // Set the bit corresponding to `obj` in the bitmap and return the previous value of that bit.
Set(const mirror::Object * obj)92   bool Set(const mirror::Object* obj) ALWAYS_INLINE {
93     return Modify<true>(obj);
94   }
95 
96   // Clear the bit corresponding to `obj` in the bitmap and return the previous value of that bit.
Clear(const mirror::Object * obj)97   bool Clear(const mirror::Object* obj) ALWAYS_INLINE {
98     return Modify<false>(obj);
99   }
100 
101   // Returns true if the object was previously marked.
102   bool AtomicTestAndSet(const mirror::Object* obj);
103 
104   // Fill the bitmap with zeroes.  Returns the bitmap's memory to the system as a side-effect.
105   void Clear();
106 
107   // Clear a range covered by the bitmap using madvise if possible.
108   void ClearRange(const mirror::Object* begin, const mirror::Object* end);
109 
110   // Test whether `obj` is part of the bitmap (i.e. return whether the bit
111   // corresponding to `obj` has been set in the bitmap).
112   //
113   // Precondition: `obj` is within the range of pointers that this bitmap could
114   // potentially cover (i.e. `this->HasAddress(obj)` is true)
115   bool Test(const mirror::Object* obj) const;
116 
117   // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
118   // even if a bit has not been set for it.
HasAddress(const void * obj)119   bool HasAddress(const void* obj) const {
120     // If obj < heap_begin_ then offset underflows to some very large value past the end of the
121     // bitmap.
122     const uintptr_t offset = reinterpret_cast<uintptr_t>(obj) - heap_begin_;
123     const size_t index = OffsetToIndex(offset);
124     return index < bitmap_size_ / sizeof(intptr_t);
125   }
126 
127   template <typename Visitor>
VisitRange(uintptr_t visit_begin,uintptr_t visit_end,const Visitor & visitor)128   void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
129     for (; visit_begin < visit_end; visit_begin += kAlignment) {
130       visitor(reinterpret_cast<mirror::Object*>(visit_begin));
131     }
132   }
133 
134   // Visit the live objects in the range [visit_begin, visit_end).
135   // TODO: Use lock annotations when clang is fixed.
136   // REQUIRES(Locks::heap_bitmap_lock_) REQUIRES_SHARED(Locks::mutator_lock_);
137   template <typename Visitor>
138   void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, Visitor&& visitor) const
139       NO_THREAD_SAFETY_ANALYSIS;
140 
141   // Visit all of the set bits in HeapBegin(), HeapLimit().
142   template <typename Visitor>
VisitAllMarked(Visitor && visitor)143   void VisitAllMarked(Visitor&& visitor) const {
144     VisitMarkedRange(HeapBegin(), HeapLimit(), visitor);
145   }
146 
147   // Visits set bits in address order.  The callback is not permitted to change the bitmap bits or
148   // max during the traversal.
149   template <typename Visitor>
150   void Walk(Visitor&& visitor)
151       REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
152 
153   // Walk through the bitmaps in increasing address order, and find the object pointers that
154   // correspond to garbage objects.  Call <callback> zero or more times with lists of these object
155   // pointers. The callback is not permitted to increase the max of either bitmap.
156   static void SweepWalk(const SpaceBitmap& live, const SpaceBitmap& mark, uintptr_t base,
157                         uintptr_t max, SweepCallback* thunk, void* arg);
158 
159   void CopyFrom(SpaceBitmap* source_bitmap);
160 
161   // Starting address of our internal storage.
Begin()162   Atomic<uintptr_t>* Begin() {
163     return bitmap_begin_;
164   }
165 
166   // Size of our internal storage
Size()167   size_t Size() const {
168     return bitmap_size_;
169   }
170 
171   // Size in bytes of the memory that the bitmaps spans.
HeapSize()172   uint64_t HeapSize() const {
173     return IndexToOffset<uint64_t>(Size() / sizeof(intptr_t));
174   }
175 
SetHeapSize(size_t bytes)176   void SetHeapSize(size_t bytes) {
177     // TODO: Un-map the end of the mem map.
178     heap_limit_ = heap_begin_ + bytes;
179     bitmap_size_ = OffsetToIndex(bytes) * sizeof(intptr_t);
180     CHECK_EQ(HeapSize(), bytes);
181   }
182 
HeapBegin()183   uintptr_t HeapBegin() const {
184     return heap_begin_;
185   }
186 
187   // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
HeapLimit()188   uint64_t HeapLimit() const {
189     return heap_limit_;
190   }
191 
192   // Set the max address which can covered by the bitmap.
193   void SetHeapLimit(uintptr_t new_end);
194 
GetName()195   std::string GetName() const {
196     return name_;
197   }
198 
SetName(const std::string & name)199   void SetName(const std::string& name) {
200     name_ = name;
201   }
202 
203   std::string Dump() const;
204 
205   // Helper function for computing bitmap size based on a 64 bit capacity.
206   static size_t ComputeBitmapSize(uint64_t capacity);
207   static size_t ComputeHeapSize(uint64_t bitmap_bytes);
208 
209   // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
210   // however, we document that this is expected on heap_end_
211 
212   SpaceBitmap() = default;
213   SpaceBitmap(SpaceBitmap&&) = default;
214   SpaceBitmap& operator=(SpaceBitmap&&) = default;
215 
IsValid()216   bool IsValid() const {
217     return bitmap_begin_ != nullptr;
218   }
219 
220   // Copy a view of the other bitmap without taking ownership of the underlying data.
CopyView(SpaceBitmap & other)221   void CopyView(SpaceBitmap& other) {
222     bitmap_begin_ = other.bitmap_begin_;
223     bitmap_size_ = other.bitmap_size_;
224     heap_begin_ = other.heap_begin_;
225     heap_limit_ = other.heap_limit_;
226     name_ = other.name_;
227   }
228 
229  private:
230   // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
231   // however, we document that this is expected on heap_end_
232   SpaceBitmap(const std::string& name,
233               MemMap&& mem_map,
234               uintptr_t* bitmap_begin,
235               size_t bitmap_size,
236               const void* heap_begin,
237               size_t heap_capacity);
238 
239   // Change the value of the bit corresponding to `obj` in the bitmap
240   // to `kSetBit` and return the previous value of that bit.
241   template<bool kSetBit>
242   bool Modify(const mirror::Object* obj);
243 
244   // Backing storage for bitmap.
245   MemMap mem_map_;
246 
247   // This bitmap itself, word sized for efficiency in scanning.
248   Atomic<uintptr_t>* bitmap_begin_ = nullptr;
249 
250   // Size of this bitmap.
251   size_t bitmap_size_ = 0u;
252 
253   // The start address of the memory covered by the bitmap, which corresponds to the word
254   // containing the first bit in the bitmap.
255   uintptr_t heap_begin_ = 0u;
256 
257   // The end address of the memory covered by the bitmap. This may not be on a word boundary.
258   uintptr_t heap_limit_ = 0u;
259 
260   // Name of this bitmap.
261   std::string name_;
262 };
263 
264 typedef SpaceBitmap<kObjectAlignment> ContinuousSpaceBitmap;
265 typedef SpaceBitmap<kLargeObjectAlignment> LargeObjectBitmap;
266 
267 template<size_t kAlignment>
268 std::ostream& operator << (std::ostream& stream, const SpaceBitmap<kAlignment>& bitmap);
269 
270 }  // namespace accounting
271 }  // namespace gc
272 }  // namespace art
273 
274 #endif  // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_
275