blob: bf4c1ed9af80bb4d45f25d8aac6404d68f9f7b4d [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"
21#include "globals.h"
22#include "mem_map.h"
23#include "UniquePtr.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070024
25#include <limits.h>
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070026#include <set>
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070027#include <stdint.h>
28#include <vector>
29
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070030namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070031
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032namespace mirror {
Ian Rogers1d54e732013-05-02 21:10:01 -070033 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034} // namespace mirror
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070035
Ian Rogers1d54e732013-05-02 21:10:01 -070036namespace gc {
37namespace accounting {
38
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070039class SpaceBitmap {
40 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070041 // Alignment of objects within spaces.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070042 static const size_t kAlignment = 8;
43
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044 typedef void Callback(mirror::Object* obj, void* arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070045
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
50 // Initialize a HeapBitmap so that it points to a bitmap large enough to cover a heap at
51 // 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
54 ~SpaceBitmap();
55
56 // <offset> is the difference from .base to a pointer address.
57 // <index> is the index of .bits that contains the bit representing
58 // <offset>.
59 static size_t OffsetToIndex(size_t offset) {
Ian Rogers1d54e732013-05-02 21:10:01 -070060 return offset / kAlignment / kBitsPerWord;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061 }
62
63 static uintptr_t IndexToOffset(size_t index) {
64 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
65 }
66
67 // Pack the bits in backwards so they come out in address order when using CLZ.
68 static word OffsetToMask(uintptr_t offset_) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070069 return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070070 }
71
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 inline bool Set(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070073 return Modify(obj, true);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070074 }
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 inline bool Clear(const mirror::Object* obj) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070077 return Modify(obj, false);
78 }
79
80 // Returns true if the object was previously marked.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 bool AtomicTestAndSet(const mirror::Object* obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070082
Ian Rogers1d54e732013-05-02 21:10:01 -070083 // Fill the bitmap with zeroes. Returns the bitmap's memory to the system as a side-effect.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070084 void Clear();
85
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 bool Test(const mirror::Object* obj) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070087
Ian Rogers506de0c2012-09-17 15:39:06 -070088 // Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
89 // even if a bit has not been set for it.
90 bool HasAddress(const void* obj) const {
91 // If obj < heap_begin_ then offset underflows to some very large value past the end of the
92 // bitmap.
buzbeecbd6d442012-11-17 14:11:25 -080093 const uintptr_t offset = reinterpret_cast<uintptr_t>(obj) - heap_begin_;
Ian Rogers506de0c2012-09-17 15:39:06 -070094 const size_t index = OffsetToIndex(offset);
95 return index < bitmap_size_ / kWordSize;
96 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070097
98 void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
99
100 class ClearVisitor {
101 public:
102 explicit ClearVisitor(SpaceBitmap* const bitmap)
103 : bitmap_(bitmap) {
104 }
105
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 void operator ()(mirror::Object* obj) const {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700107 bitmap_->Clear(obj);
108 }
109 private:
110 SpaceBitmap* const bitmap_;
111 };
112
113 template <typename Visitor>
114 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
115 for (; visit_begin < visit_end; visit_begin += kAlignment ) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 visitor(reinterpret_cast<mirror::Object*>(visit_begin));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700117 }
118 }
119
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700120 template <typename Visitor, typename FingerVisitor>
121 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end,
122 const Visitor& visitor, const FingerVisitor& finger_visitor) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700125
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700126 void Walk(Callback* callback, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700128
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 void InOrderWalk(Callback* callback, void* arg)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_, Locks::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700131
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700132 static void SweepWalk(const SpaceBitmap& live,
133 const SpaceBitmap& mark,
134 uintptr_t base, uintptr_t max,
135 SweepCallback* thunk, void* arg);
136
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700137 void CopyFrom(SpaceBitmap* source_bitmap);
138
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700139 // Starting address of our internal storage.
140 word* Begin() {
141 return bitmap_begin_;
142 }
143
144 // Size of our internal storage
145 size_t Size() const {
146 return bitmap_size_;
147 }
148
149 // Size in bytes of the memory that the bitmaps spans.
150 size_t HeapSize() const {
151 return IndexToOffset(Size() / kWordSize);
152 }
153
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700154 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700155 return heap_begin_;
156 }
157
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700158 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
159 uintptr_t HeapLimit() const {
160 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
161 }
162
163 // Set the max address which can covered by the bitmap.
164 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700165
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700166 std::string GetName() const;
167 void SetName(const std::string& name);
168
Ian Rogers1d54e732013-05-02 21:10:01 -0700169 std::string Dump() const;
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 const void* GetObjectWordAddress(const mirror::Object* obj) const {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700172 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
173 const uintptr_t offset = addr - heap_begin_;
174 const size_t index = OffsetToIndex(offset);
175 return &bitmap_begin_[index];
176 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700177
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700178 private:
179 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
180 // however, we document that this is expected on heap_end_
181 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin)
182 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700183 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700184 name_(name) {}
185
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 bool Modify(const mirror::Object* obj, bool do_set);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700187
188 // Backing storage for bitmap.
189 UniquePtr<MemMap> mem_map_;
190
191 // This bitmap itself, word sized for efficiency in scanning.
192 word* const bitmap_begin_;
193
194 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700195 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700196
197 // The base address of the heap, which corresponds to the word containing the first bit in the
198 // bitmap.
199 const uintptr_t heap_begin_;
200
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700201 // Name of this bitmap.
202 std::string name_;
203};
204
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700205// Like a bitmap except it keeps track of objects using sets.
206class SpaceSetMap {
207 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800208 typedef std::set<const mirror::Object*> Objects;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700209
210 bool IsEmpty() const {
211 return contained_.empty();
212 }
213
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 inline void Set(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700215 contained_.insert(obj);
216 }
217
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 inline void Clear(const mirror::Object* obj) {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700219 Objects::iterator found = contained_.find(obj);
220 if (found != contained_.end()) {
221 contained_.erase(found);
222 }
223 }
224
225 void Clear() {
226 contained_.clear();
227 }
228
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 inline bool Test(const mirror::Object* obj) const {
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700230 return contained_.find(obj) != contained_.end();
231 }
232
233 std::string GetName() const;
234 void SetName(const std::string& name);
235
236 void Walk(SpaceBitmap::Callback* callback, void* arg)
237 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
238
239 void CopyFrom(const SpaceSetMap& space_set);
240
241 template <typename Visitor>
242 void Visit(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS {
243 for (Objects::iterator it = contained_.begin(); it != contained_.end(); ++it) {
244 visitor(*it);
245 }
246 }
247
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700248 explicit SpaceSetMap(const std::string& name) : name_(name) {}
Ian Rogers1d54e732013-05-02 21:10:01 -0700249 ~SpaceSetMap() {}
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700250
251 Objects& GetObjects() {
252 return contained_;
253 }
254
255 private:
256 std::string name_;
257 Objects contained_;
258};
259
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700260std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
261
Ian Rogers1d54e732013-05-02 21:10:01 -0700262} // namespace accounting
263} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700264} // namespace art
265
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700266#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_H_