blob: 2d6cde52afcc402e08e2bc522696068716f0c672 [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
20#include "locks.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"
24#include "UniquePtr.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070025
26#include <limits.h>
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070027#include <set>
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070028#include <stdint.h>
29#include <vector>
30
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070031namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070032
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace mirror {
Ian Rogers1d54e732013-05-02 21:10:01 -070034 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035} // namespace mirror
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070036
Ian Rogers1d54e732013-05-02 21:10:01 -070037namespace gc {
38namespace accounting {
39
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070040class SpaceBitmap {
41 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070042 // Alignment of objects within spaces.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070043 static const size_t kAlignment = 8;
44
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 typedef void Callback(mirror::Object* obj, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070046
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 typedef void ScanCallback(mirror::Object* obj, void* finger, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070048
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 typedef void SweepCallback(size_t ptr_count, mirror::Object** ptrs, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070050
Mathieu Chartier31e89252013-08-28 11:29:12 -070051 // 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 -070052 // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
53 static SpaceBitmap* Create(const std::string& name, byte* heap_begin, size_t heap_capacity);
54
Mathieu Chartier31e89252013-08-28 11:29:12 -070055 // Initialize a space bitmap using the provided mem_map as the live bits. Takes ownership of the
56 // mem map. The address range covered starts at heap_begin and is of size equal to heap_capacity.
57 // Objects are kAlignement-aligned.
58 static SpaceBitmap* CreateFromMemMap(const std::string& name, MemMap* mem_map,
59 byte* heap_begin, size_t heap_capacity);
60
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061 ~SpaceBitmap();
62
63 // <offset> is the difference from .base to a pointer address.
64 // <index> is the index of .bits that contains the bit representing
65 // <offset>.
66 static size_t OffsetToIndex(size_t offset) {
Ian Rogers1d54e732013-05-02 21:10:01 -070067 return offset / kAlignment / kBitsPerWord;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070068 }
69
70 static uintptr_t IndexToOffset(size_t index) {
71 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
72 }
73
74 // Pack the bits in backwards so they come out in address order when using CLZ.
75 static word OffsetToMask(uintptr_t offset_) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070076 return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070077 }
78
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079 inline bool Set(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070080 return Modify(obj, true);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070081 }
82
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 inline bool Clear(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070084 return Modify(obj, false);
85 }
86
87 // Returns true if the object was previously marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 bool AtomicTestAndSet(const mirror::Object* obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070089
Ian Rogers1d54e732013-05-02 21:10:01 -070090 // Fill the bitmap with zeroes. Returns the bitmap's memory to the system as a side-effect.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070091 void Clear();
92
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 bool Test(const mirror::Object* obj) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070094
Ian Rogers506de0c2012-09-17 15:39:06 -070095 // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
96 // even if a bit has not been set for it.
97 bool HasAddress(const void* obj) const {
98 // If obj < heap_begin_ then offset underflows to some very large value past the end of the
99 // bitmap.
buzbeecbd6d442012-11-17 14:11:25 -0800100 const uintptr_t offset = reinterpret_cast<uintptr_t>(obj) - heap_begin_;
Ian Rogers506de0c2012-09-17 15:39:06 -0700101 const size_t index = OffsetToIndex(offset);
102 return index < bitmap_size_ / kWordSize;
103 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700104
105 void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
106
107 class ClearVisitor {
108 public:
109 explicit ClearVisitor(SpaceBitmap* const bitmap)
110 : bitmap_(bitmap) {
111 }
112
Brian Carlstromdf629502013-07-17 22:39:56 -0700113 void operator()(mirror::Object* obj) const {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700114 bitmap_->Clear(obj);
115 }
116 private:
117 SpaceBitmap* const bitmap_;
118 };
119
120 template <typename Visitor>
121 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
Brian Carlstromdf629502013-07-17 22:39:56 -0700122 for (; visit_begin < visit_end; visit_begin += kAlignment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 visitor(reinterpret_cast<mirror::Object*>(visit_begin));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700124 }
125 }
126
Mathieu Chartier184e3222013-08-03 14:02:57 -0700127 template <typename Visitor>
128 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
130 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700131
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700132 void Walk(Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700133 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700134
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 void InOrderWalk(Callback* callback, void* arg)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700137
Mathieu Chartier184e3222013-08-03 14:02:57 -0700138 static void SweepWalk(const SpaceBitmap& live, const SpaceBitmap& mark, uintptr_t base,
139 uintptr_t max, SweepCallback* thunk, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700140
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700141 void CopyFrom(SpaceBitmap* source_bitmap);
142
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700143 // Starting address of our internal storage.
144 word* Begin() {
145 return bitmap_begin_;
146 }
147
148 // Size of our internal storage
149 size_t Size() const {
150 return bitmap_size_;
151 }
152
153 // Size in bytes of the memory that the bitmaps spans.
154 size_t HeapSize() const {
155 return IndexToOffset(Size() / kWordSize);
156 }
157
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700158 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700159 return heap_begin_;
160 }
161
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700162 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
163 uintptr_t HeapLimit() const {
164 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
165 }
166
167 // Set the max address which can covered by the bitmap.
168 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700169
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700170 std::string GetName() const;
171 void SetName(const std::string& name);
172
Ian Rogers1d54e732013-05-02 21:10:01 -0700173 std::string Dump() const;
174
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 const void* GetObjectWordAddress(const mirror::Object* obj) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700176 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
177 const uintptr_t offset = addr - heap_begin_;
178 const size_t index = OffsetToIndex(offset);
179 return &bitmap_begin_[index];
180 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700181
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700182 private:
183 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
184 // however, we document that this is expected on heap_end_
Mathieu Chartier184e3222013-08-03 14:02:57 -0700185 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size,
186 const void* heap_begin)
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700187 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700188 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700189 name_(name) {}
190
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 bool Modify(const mirror::Object* obj, bool do_set);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700192
193 // Backing storage for bitmap.
194 UniquePtr<MemMap> mem_map_;
195
196 // This bitmap itself, word sized for efficiency in scanning.
197 word* const bitmap_begin_;
198
199 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700200 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700201
202 // The base address of the heap, which corresponds to the word containing the first bit in the
203 // bitmap.
204 const uintptr_t heap_begin_;
205
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700206 // Name of this bitmap.
207 std::string name_;
208};
209
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700210// Like a bitmap except it keeps track of objects using sets.
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800211class ObjectSet {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700212 public:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700213 typedef std::set<
214 const mirror::Object*, std::less<const mirror::Object*>,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 GcAllocator<const mirror::Object*> > Objects;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700216
217 bool IsEmpty() const {
218 return contained_.empty();
219 }
220
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 inline void Set(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700222 contained_.insert(obj);
223 }
224
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 inline void Clear(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700226 Objects::iterator found = contained_.find(obj);
227 if (found != contained_.end()) {
228 contained_.erase(found);
229 }
230 }
231
232 void Clear() {
233 contained_.clear();
234 }
235
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 inline bool Test(const mirror::Object* obj) const {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700237 return contained_.find(obj) != contained_.end();
238 }
239
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800240 const std::string& GetName() const {
241 return name_;
242 }
243
244 void SetName(const std::string& name) {
245 name_ = name;
246 }
247
248 void CopyFrom(const ObjectSet& space_set) {
249 contained_ = space_set.contained_;
250 }
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700251
252 void Walk(SpaceBitmap::Callback* callback, void* arg)
253 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
254
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700255 template <typename Visitor>
256 void Visit(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartier11409ae2013-09-23 11:49:36 -0700257 for (const mirror::Object* obj : contained_) {
258 visitor(const_cast<mirror::Object*>(obj));
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700259 }
260 }
261
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -0800262 explicit ObjectSet(const std::string& name) : name_(name) {}
263 ~ObjectSet() {}
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700264
265 Objects& GetObjects() {
266 return contained_;
267 }
268
269 private:
270 std::string name_;
271 Objects contained_;
272};
273
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700274std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
275
Ian Rogers1d54e732013-05-02 21:10:01 -0700276} // namespace accounting
277} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700278} // namespace art
279
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700280#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_