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