blob: 3c4b674fcd496f82b1f9b9aa8853802bcfe29285 [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
Mathieu Chartier0a9dc052013-07-25 11:01:28 -070020#include "gc_allocator.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "globals.h"
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080022#include "locks.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#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
73 // Pack the bits in backwards so they come out in address order when using CLZ.
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 static word OffsetToMask(uintptr_t offset) {
75 return static_cast<uintptr_t>(kWordHighBitMask) >> ((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
Mathieu Chartier184e3222013-08-03 14:02:57 -0700126 template <typename Visitor>
127 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700130
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800131 void Walk(ObjectCallback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700133
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800134 void InOrderWalk(ObjectCallback* callback, void* arg)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700136
Mathieu Chartier184e3222013-08-03 14:02:57 -0700137 static void SweepWalk(const SpaceBitmap& live, const SpaceBitmap& mark, uintptr_t base,
138 uintptr_t max, SweepCallback* thunk, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700140 void CopyFrom(SpaceBitmap* source_bitmap);
141
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700142 // Starting address of our internal storage.
143 word* Begin() {
144 return bitmap_begin_;
145 }
146
147 // Size of our internal storage
148 size_t Size() const {
149 return bitmap_size_;
150 }
151
152 // Size in bytes of the memory that the bitmaps spans.
153 size_t HeapSize() const {
154 return IndexToOffset(Size() / kWordSize);
155 }
156
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700157 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700158 return heap_begin_;
159 }
160
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700161 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
162 uintptr_t HeapLimit() const {
163 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
164 }
165
166 // Set the max address which can covered by the bitmap.
167 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700168
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700169 std::string GetName() const;
170 void SetName(const std::string& name);
171
Ian Rogers1d54e732013-05-02 21:10:01 -0700172 std::string Dump() const;
173
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 const void* GetObjectWordAddress(const mirror::Object* obj) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700175 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
176 const uintptr_t offset = addr - heap_begin_;
177 const size_t index = OffsetToIndex(offset);
178 return &bitmap_begin_[index];
179 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700180
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 private:
182 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
183 // however, we document that this is expected on heap_end_
Mathieu Chartier184e3222013-08-03 14:02:57 -0700184 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size,
185 const void* heap_begin)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700186 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700187 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700188 name_(name) {}
189
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 bool Modify(const mirror::Object* obj, bool do_set);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700191
192 // Backing storage for bitmap.
193 UniquePtr<MemMap> mem_map_;
194
195 // This bitmap itself, word sized for efficiency in scanning.
196 word* const bitmap_begin_;
197
198 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700199 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700200
201 // The base address of the heap, which corresponds to the word containing the first bit in the
202 // bitmap.
203 const uintptr_t heap_begin_;
204
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700205 // Name of this bitmap.
206 std::string name_;
207};
208
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700209// Like a bitmap except it keeps track of objects using sets.
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800210class ObjectSet {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700211 public:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700212 typedef std::set<
213 const mirror::Object*, std::less<const mirror::Object*>,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700214 GcAllocator<const mirror::Object*> > Objects;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700215
216 bool IsEmpty() const {
217 return contained_.empty();
218 }
219
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 inline void Set(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700221 contained_.insert(obj);
222 }
223
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 inline void Clear(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700225 Objects::iterator found = contained_.find(obj);
226 if (found != contained_.end()) {
227 contained_.erase(found);
228 }
229 }
230
231 void Clear() {
232 contained_.clear();
233 }
234
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 inline bool Test(const mirror::Object* obj) const {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700236 return contained_.find(obj) != contained_.end();
237 }
238
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800239 const std::string& GetName() const {
240 return name_;
241 }
242
243 void SetName(const std::string& name) {
244 name_ = name;
245 }
246
247 void CopyFrom(const ObjectSet& space_set) {
248 contained_ = space_set.contained_;
249 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700250
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800251 void Walk(ObjectCallback* callback, void* arg)
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700252 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
253
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700254 template <typename Visitor>
255 void Visit(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700256 for (const mirror::Object* obj : contained_) {
257 visitor(const_cast<mirror::Object*>(obj));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700258 }
259 }
260
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800261 explicit ObjectSet(const std::string& name) : name_(name) {}
262 ~ObjectSet() {}
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700263
264 Objects& GetObjects() {
265 return contained_;
266 }
267
268 private:
269 std::string name_;
270 Objects contained_;
271};
272
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700273std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
274
Ian Rogers1d54e732013-05-02 21:10:01 -0700275} // namespace accounting
276} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700277} // namespace art
278
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700279#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_