blob: 674c2621a00dffbf3fb783fc2730897e55639d05 [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
51 // Initialize a HeapBitmap so that it points to a bitmap large enough to cover a heap at
52 // 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
55 ~SpaceBitmap();
56
57 // <offset> is the difference from .base to a pointer address.
58 // <index> is the index of .bits that contains the bit representing
59 // <offset>.
60 static size_t OffsetToIndex(size_t offset) {
Ian Rogers1d54e732013-05-02 21:10:01 -070061 return offset / kAlignment / kBitsPerWord;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070062 }
63
64 static uintptr_t IndexToOffset(size_t index) {
65 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
66 }
67
68 // Pack the bits in backwards so they come out in address order when using CLZ.
69 static word OffsetToMask(uintptr_t offset_) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070070 return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070071 }
72
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 inline bool Set(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070074 return Modify(obj, true);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070075 }
76
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 inline bool Clear(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070078 return Modify(obj, false);
79 }
80
81 // Returns true if the object was previously marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 bool AtomicTestAndSet(const mirror::Object* obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070083
Ian Rogers1d54e732013-05-02 21:10:01 -070084 // Fill the bitmap with zeroes. Returns the bitmap's memory to the system as a side-effect.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070085 void Clear();
86
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 bool Test(const mirror::Object* obj) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070088
Ian Rogers506de0c2012-09-17 15:39:06 -070089 // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
90 // even if a bit has not been set for it.
91 bool HasAddress(const void* obj) const {
92 // If obj < heap_begin_ then offset underflows to some very large value past the end of the
93 // bitmap.
buzbeecbd6d442012-11-17 14:11:25 -080094 const uintptr_t offset = reinterpret_cast<uintptr_t>(obj) - heap_begin_;
Ian Rogers506de0c2012-09-17 15:39:06 -070095 const size_t index = OffsetToIndex(offset);
96 return index < bitmap_size_ / kWordSize;
97 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070098
99 void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
100
101 class ClearVisitor {
102 public:
103 explicit ClearVisitor(SpaceBitmap* const bitmap)
104 : bitmap_(bitmap) {
105 }
106
Brian Carlstromdf629502013-07-17 22:39:56 -0700107 void operator()(mirror::Object* obj) const {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700108 bitmap_->Clear(obj);
109 }
110 private:
111 SpaceBitmap* const bitmap_;
112 };
113
114 template <typename Visitor>
115 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
Brian Carlstromdf629502013-07-17 22:39:56 -0700116 for (; visit_begin < visit_end; visit_begin += kAlignment) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 visitor(reinterpret_cast<mirror::Object*>(visit_begin));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700118 }
119 }
120
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121 template <typename Visitor, typename FingerVisitor>
122 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end,
123 const Visitor& visitor, const FingerVisitor& finger_visitor) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700126
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700127 void Walk(Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700129
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 void InOrderWalk(Callback* callback, void* arg)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700132
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700133 static void SweepWalk(const SpaceBitmap& live,
134 const SpaceBitmap& mark,
135 uintptr_t base, uintptr_t max,
136 SweepCallback* thunk, void* arg);
137
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700138 void CopyFrom(SpaceBitmap* source_bitmap);
139
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700140 // Starting address of our internal storage.
141 word* Begin() {
142 return bitmap_begin_;
143 }
144
145 // Size of our internal storage
146 size_t Size() const {
147 return bitmap_size_;
148 }
149
150 // Size in bytes of the memory that the bitmaps spans.
151 size_t HeapSize() const {
152 return IndexToOffset(Size() / kWordSize);
153 }
154
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700155 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700156 return heap_begin_;
157 }
158
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700159 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
160 uintptr_t HeapLimit() const {
161 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
162 }
163
164 // Set the max address which can covered by the bitmap.
165 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700166
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700167 std::string GetName() const;
168 void SetName(const std::string& name);
169
Ian Rogers1d54e732013-05-02 21:10:01 -0700170 std::string Dump() const;
171
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 const void* GetObjectWordAddress(const mirror::Object* obj) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700173 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
174 const uintptr_t offset = addr - heap_begin_;
175 const size_t index = OffsetToIndex(offset);
176 return &bitmap_begin_[index];
177 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700178
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700179 private:
180 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
181 // however, we document that this is expected on heap_end_
182 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin)
183 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700184 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700185 name_(name) {}
186
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 bool Modify(const mirror::Object* obj, bool do_set);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700188
189 // Backing storage for bitmap.
190 UniquePtr<MemMap> mem_map_;
191
192 // This bitmap itself, word sized for efficiency in scanning.
193 word* const bitmap_begin_;
194
195 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700196 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700197
198 // The base address of the heap, which corresponds to the word containing the first bit in the
199 // bitmap.
200 const uintptr_t heap_begin_;
201
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700202 // Name of this bitmap.
203 std::string name_;
204};
205
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700206// Like a bitmap except it keeps track of objects using sets.
207class SpaceSetMap {
208 public:
Mathieu Chartier0a9dc052013-07-25 11:01:28 -0700209 typedef std::set<
210 const mirror::Object*, std::less<const mirror::Object*>,
211 GCAllocator<const mirror::Object*> > Objects;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700212
213 bool IsEmpty() const {
214 return contained_.empty();
215 }
216
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 inline void Set(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700218 contained_.insert(obj);
219 }
220
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 inline void Clear(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700222 Objects::iterator found = contained_.find(obj);
223 if (found != contained_.end()) {
224 contained_.erase(found);
225 }
226 }
227
228 void Clear() {
229 contained_.clear();
230 }
231
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 inline bool Test(const mirror::Object* obj) const {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700233 return contained_.find(obj) != contained_.end();
234 }
235
236 std::string GetName() const;
237 void SetName(const std::string& name);
238
239 void Walk(SpaceBitmap::Callback* callback, void* arg)
240 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
241
242 void CopyFrom(const SpaceSetMap& space_set);
243
244 template <typename Visitor>
245 void Visit(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS {
246 for (Objects::iterator it = contained_.begin(); it != contained_.end(); ++it) {
247 visitor(*it);
248 }
249 }
250
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700251 explicit SpaceSetMap(const std::string& name) : name_(name) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700252 ~SpaceSetMap() {}
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700253
254 Objects& GetObjects() {
255 return contained_;
256 }
257
258 private:
259 std::string name_;
260 Objects contained_;
261};
262
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700263std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
264
Ian Rogers1d54e732013-05-02 21:10:01 -0700265} // namespace accounting
266} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700267} // namespace art
268
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700269#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_