blob: a88f3e475f1cc78abf5b33becd35950967e9e3d3 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_
18#define ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Ian Rogers719d1a32014-03-06 12:13:39 -080020#include "base/mutex.h"
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070021#include "gc_allocator.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "globals.h"
23#include "mem_map.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080024#include "object_callbacks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "UniquePtr.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070026
27#include <limits.h>
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070028#include <set>
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070029#include <stdint.h>
30#include <vector>
31
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070032namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070033
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace mirror {
Ian Rogers1d54e732013-05-02 21:10:01 -070035 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036} // namespace mirror
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070037
Ian Rogers1d54e732013-05-02 21:10:01 -070038namespace gc {
39namespace accounting {
40
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070041class SpaceBitmap {
42 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070043 // Alignment of objects within spaces.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070044 static const size_t kAlignment = 8;
45
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 typedef void ScanCallback(mirror::Object* obj, void* finger, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070047
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 typedef void SweepCallback(size_t ptr_count, mirror::Object** ptrs, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070049
Mathieu Chartier31e89252013-08-28 11:29:12 -070050 // Initialize a space bitmap so that it points to a bitmap large enough to cover a heap at
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070051 // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
52 static SpaceBitmap* Create(const std::string& name, byte* heap_begin, size_t heap_capacity);
53
Mathieu Chartier31e89252013-08-28 11:29:12 -070054 // Initialize a space bitmap using the provided mem_map as the live bits. Takes ownership of the
55 // mem map. The address range covered starts at heap_begin and is of size equal to heap_capacity.
56 // Objects are kAlignement-aligned.
57 static SpaceBitmap* CreateFromMemMap(const std::string& name, MemMap* mem_map,
58 byte* heap_begin, size_t heap_capacity);
59
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070060 ~SpaceBitmap();
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>.
65 static size_t OffsetToIndex(size_t offset) {
Ian Rogers1d54e732013-05-02 21:10:01 -070066 return offset / kAlignment / kBitsPerWord;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070067 }
68
69 static uintptr_t IndexToOffset(size_t index) {
70 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
71 }
72
Andreas Gampecb8aea42014-04-02 15:39:58 -070073 // Bits are packed in the obvious way.
74 static uword OffsetToMask(uintptr_t offset) {
75 return (static_cast<size_t>(1)) << ((offset / kAlignment) % kBitsPerWord);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070076 }
77
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 inline bool Set(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070079 return Modify(obj, true);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070080 }
81
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 inline bool Clear(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070083 return Modify(obj, false);
84 }
85
86 // Returns true if the object was previously marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 bool AtomicTestAndSet(const mirror::Object* obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070088
Ian Rogers1d54e732013-05-02 21:10:01 -070089 // Fill the bitmap with zeroes. Returns the bitmap's memory to the system as a side-effect.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070090 void Clear();
91
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 bool Test(const mirror::Object* obj) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070093
Ian Rogers506de0c2012-09-17 15:39:06 -070094 // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
95 // even if a bit has not been set for it.
96 bool HasAddress(const void* obj) const {
97 // If obj < heap_begin_ then offset underflows to some very large value past the end of the
98 // bitmap.
buzbeecbd6d442012-11-17 14:11:25 -080099 const uintptr_t offset = reinterpret_cast<uintptr_t>(obj) - heap_begin_;
Ian Rogers506de0c2012-09-17 15:39:06 -0700100 const size_t index = OffsetToIndex(offset);
101 return index < bitmap_size_ / kWordSize;
102 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800104 void VisitRange(uintptr_t base, uintptr_t max, ObjectCallback* callback, void* arg) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700105
106 class ClearVisitor {
107 public:
108 explicit ClearVisitor(SpaceBitmap* const bitmap)
109 : bitmap_(bitmap) {
110 }
111
Brian Carlstromdf629502013-07-17 22:39:56 -0700112 void operator()(mirror::Object* obj) const {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700113 bitmap_->Clear(obj);
114 }
115 private:
116 SpaceBitmap* const bitmap_;
117 };
118
119 template <typename Visitor>
120 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
Brian Carlstromdf629502013-07-17 22:39:56 -0700121 for (; visit_begin < visit_end; visit_begin += kAlignment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 visitor(reinterpret_cast<mirror::Object*>(visit_begin));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700123 }
124 }
125
Andreas Gampebe73e572014-04-03 10:46:42 -0700126 /**
127 * Visit the live objects in the range [visit_begin, visit_end).
128 */
Mathieu Chartier184e3222013-08-03 14:02:57 -0700129 template <typename Visitor>
130 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700133
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800134 void Walk(ObjectCallback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700135 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700136
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800137 void InOrderWalk(ObjectCallback* callback, void* arg)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139
Mathieu Chartier184e3222013-08-03 14:02:57 -0700140 static void SweepWalk(const SpaceBitmap& live, const SpaceBitmap& mark, uintptr_t base,
141 uintptr_t max, SweepCallback* thunk, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700142
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700143 void CopyFrom(SpaceBitmap* source_bitmap);
144
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700145 // Starting address of our internal storage.
Andreas Gampecb8aea42014-04-02 15:39:58 -0700146 uword* Begin() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700147 return bitmap_begin_;
148 }
149
150 // Size of our internal storage
151 size_t Size() const {
152 return bitmap_size_;
153 }
154
155 // Size in bytes of the memory that the bitmaps spans.
156 size_t HeapSize() const {
157 return IndexToOffset(Size() / kWordSize);
158 }
159
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700160 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700161 return heap_begin_;
162 }
163
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700164 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
165 uintptr_t HeapLimit() const {
166 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
167 }
168
169 // Set the max address which can covered by the bitmap.
170 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700171
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700172 std::string GetName() const;
173 void SetName(const std::string& name);
174
Ian Rogers1d54e732013-05-02 21:10:01 -0700175 std::string Dump() const;
176
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 const void* GetObjectWordAddress(const mirror::Object* obj) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700178 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
179 const uintptr_t offset = addr - heap_begin_;
180 const size_t index = OffsetToIndex(offset);
181 return &bitmap_begin_[index];
182 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700183
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700184 private:
185 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
186 // however, we document that this is expected on heap_end_
Andreas Gampecb8aea42014-04-02 15:39:58 -0700187 SpaceBitmap(const std::string& name, MemMap* mem_map, uword* bitmap_begin, size_t bitmap_size,
Mathieu Chartier184e3222013-08-03 14:02:57 -0700188 const void* heap_begin)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700189 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700190 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700191 name_(name) {}
192
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 bool Modify(const mirror::Object* obj, bool do_set);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700194
195 // Backing storage for bitmap.
196 UniquePtr<MemMap> mem_map_;
197
198 // This bitmap itself, word sized for efficiency in scanning.
Andreas Gampecb8aea42014-04-02 15:39:58 -0700199 uword* const bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700200
201 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700202 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700203
204 // The base address of the heap, which corresponds to the word containing the first bit in the
205 // bitmap.
206 const uintptr_t heap_begin_;
207
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700208 // Name of this bitmap.
209 std::string name_;
210};
211
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700212// Like a bitmap except it keeps track of objects using sets.
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800213class ObjectSet {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700214 public:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700215 typedef std::set<
216 const mirror::Object*, std::less<const mirror::Object*>,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700217 GcAllocator<const mirror::Object*> > Objects;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700218
219 bool IsEmpty() const {
220 return contained_.empty();
221 }
222
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 inline void Set(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700224 contained_.insert(obj);
225 }
226
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 inline void Clear(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700228 Objects::iterator found = contained_.find(obj);
229 if (found != contained_.end()) {
230 contained_.erase(found);
231 }
232 }
233
234 void Clear() {
235 contained_.clear();
236 }
237
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 inline bool Test(const mirror::Object* obj) const {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700239 return contained_.find(obj) != contained_.end();
240 }
241
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800242 const std::string& GetName() const {
243 return name_;
244 }
245
246 void SetName(const std::string& name) {
247 name_ = name;
248 }
249
250 void CopyFrom(const ObjectSet& space_set) {
251 contained_ = space_set.contained_;
252 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700253
Ian Rogers719d1a32014-03-06 12:13:39 -0800254 void Walk(ObjectCallback* callback, void* arg) SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700255
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700256 template <typename Visitor>
257 void Visit(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700258 for (const mirror::Object* obj : contained_) {
259 visitor(const_cast<mirror::Object*>(obj));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700260 }
261 }
262
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800263 explicit ObjectSet(const std::string& name) : name_(name) {}
264 ~ObjectSet() {}
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700265
266 Objects& GetObjects() {
267 return contained_;
268 }
269
270 private:
271 std::string name_;
272 Objects contained_;
273};
274
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700275std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
276
Ian Rogers1d54e732013-05-02 21:10:01 -0700277} // namespace accounting
278} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700279} // namespace art
280
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700281#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_