blob: 21db0e6f1dd2f480939397580da1411f4a4fab05 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Elliott Hughes5e71b522011-10-20 13:12:32 -070017#include "heap_bitmap.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
19#include <sys/mman.h>
20
Elliott Hughes90a33692011-08-30 13:27:07 -070021#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "logging.h"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070023#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070024
25namespace art {
26
Ian Rogers30fab402012-01-23 15:43:46 -080027HeapBitmap* HeapBitmap::Create(const char* name, byte* heap_begin, size_t heap_capacity) {
28 CHECK(heap_begin != NULL);
29 size_t bitmap_size = HB_OFFSET_TO_INDEX(heap_capacity) * kWordSize;
30 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name, NULL, bitmap_size, PROT_READ | PROT_WRITE));
31 if (mem_map.get() == NULL) {
32 LOG(ERROR) << "Failed to allocate bitmap " << name;
Carl Shapiro69759ea2011-07-21 18:13:35 -070033 return NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -070034 }
Ian Rogers30fab402012-01-23 15:43:46 -080035 word* bitmap_begin = reinterpret_cast<word*>(mem_map->Begin());
36 return new HeapBitmap(name, mem_map.release(), bitmap_begin, bitmap_size, heap_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -070037}
38
39// Clean up any resources associated with the bitmap.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040HeapBitmap::~HeapBitmap() {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
42// Fill the bitmap with zeroes. Returns the bitmap's memory to the
43// system as a side-effect.
44void HeapBitmap::Clear() {
Ian Rogers30fab402012-01-23 15:43:46 -080045 if (bitmap_begin_ != NULL) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070046 // This returns the memory to the system. Successive page faults
47 // will return zeroed memory.
Ian Rogers30fab402012-01-23 15:43:46 -080048 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
Carl Shapiro69759ea2011-07-21 18:13:35 -070049 if (result == -1) {
50 PLOG(WARNING) << "madvise failed";
51 }
Ian Rogers30fab402012-01-23 15:43:46 -080052 heap_end_ = heap_begin_ - 1;
Carl Shapiro69759ea2011-07-21 18:13:35 -070053 }
54}
55
Ian Rogers30fab402012-01-23 15:43:46 -080056// Return true iff <obj> is within the range of pointers that this bitmap could potentially cover,
57// even if a bit has not been set for it.
Carl Shapiro69759ea2011-07-21 18:13:35 -070058bool HeapBitmap::HasAddress(const void* obj) const {
59 if (obj != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -080060 const uintptr_t offset = (uintptr_t)obj - heap_begin_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070061 const size_t index = HB_OFFSET_TO_INDEX(offset);
Ian Rogers30fab402012-01-23 15:43:46 -080062 return index < bitmap_size_ / kWordSize;
Carl Shapiro69759ea2011-07-21 18:13:35 -070063 }
64 return false;
65}
66
Ian Rogers30fab402012-01-23 15:43:46 -080067void HeapBitmap::VisitRange(uintptr_t visit_begin, uintptr_t visit_end, Callback* visitor, void* arg) const {
68 size_t start = HB_OFFSET_TO_INDEX(visit_begin - heap_begin_);
69 size_t end = HB_OFFSET_TO_INDEX(visit_end - heap_begin_ - 1);
Ian Rogers5d76c432011-10-31 21:42:49 -070070 for (size_t i = start; i <= end; i++) {
Ian Rogers30fab402012-01-23 15:43:46 -080071 word w = bitmap_begin_[i];
Ian Rogers5d76c432011-10-31 21:42:49 -070072 if (w != 0) {
73 word high_bit = 1 << (kBitsPerWord - 1);
Ian Rogers30fab402012-01-23 15:43:46 -080074 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + heap_begin_;
Ian Rogers5d76c432011-10-31 21:42:49 -070075 while (w != 0) {
76 const int shift = CLZ(w);
77 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
78 (*visitor)(obj, arg);
79 w &= ~(high_bit >> shift);
80 }
81 }
82 }
83}
84
Carl Shapiro69759ea2011-07-21 18:13:35 -070085// Visits set bits in address order. The callback is not permitted to
86// change the bitmap bits or max during the traversal.
87void HeapBitmap::Walk(HeapBitmap::Callback* callback, void* arg) {
Ian Rogers30fab402012-01-23 15:43:46 -080088 CHECK(bitmap_begin_ != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070089 CHECK(callback != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -080090 uintptr_t end = HB_OFFSET_TO_INDEX(heap_end_ - heap_begin_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070091 for (uintptr_t i = 0; i <= end; ++i) {
Ian Rogers30fab402012-01-23 15:43:46 -080092 word w = bitmap_begin_[i];
Elliott Hughesb0663112011-10-19 18:16:37 -070093 if (UNLIKELY(w != 0)) {
94 word high_bit = 1 << (kBitsPerWord - 1);
Ian Rogers30fab402012-01-23 15:43:46 -080095 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + heap_begin_;
Elliott Hughesb0663112011-10-19 18:16:37 -070096 while (w != 0) {
97 const int shift = CLZ(w);
98 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
Carl Shapiro69759ea2011-07-21 18:13:35 -070099 (*callback)(obj, arg);
Elliott Hughesb0663112011-10-19 18:16:37 -0700100 w &= ~(high_bit >> shift);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101 }
102 }
103 }
104}
105
Ian Rogers30fab402012-01-23 15:43:46 -0800106// Similar to Walk but the callback routine is permitted to change the bitmap bits and end during
107// traversal. Used by the the root marking scan exclusively.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108//
Ian Rogers30fab402012-01-23 15:43:46 -0800109// The callback is invoked with a finger argument. The finger is a pointer to an address not yet
110// visited by the traversal. If the callback sets a bit for an address at or above the finger, this
111// address will be visited by the traversal. If the callback sets a bit for an address below the
112// finger, this address will not be visited (typiscally such an address would be placed on the
113// marking stack).
114void HeapBitmap::ScanWalk(uintptr_t scan_begin, uintptr_t scan_end, ScanCallback* callback, void* arg) {
115 CHECK(bitmap_begin_ != NULL);
Ian Rogers5d76c432011-10-31 21:42:49 -0700116 CHECK(callback != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -0800117 CHECK_LE(scan_begin, scan_end);
118 CHECK_GE(scan_begin, heap_begin_);
119 size_t start = HB_OFFSET_TO_INDEX(scan_begin - heap_begin_);
120 if (scan_end < heap_end_) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700121 // The end of the space we're looking at is before the current maximum bitmap PC, scan to that
122 // and don't recompute end on each iteration
Ian Rogers30fab402012-01-23 15:43:46 -0800123 size_t end = HB_OFFSET_TO_INDEX(scan_end - heap_begin_ - 1);
Ian Rogers5d76c432011-10-31 21:42:49 -0700124 for (size_t i = start; i <= end; i++) {
Ian Rogers30fab402012-01-23 15:43:46 -0800125 word w = bitmap_begin_[i];
Ian Rogers5d76c432011-10-31 21:42:49 -0700126 if (UNLIKELY(w != 0)) {
127 word high_bit = 1 << (kBitsPerWord - 1);
Ian Rogers30fab402012-01-23 15:43:46 -0800128 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + heap_begin_;
129 void* finger = reinterpret_cast<void*>(HB_INDEX_TO_OFFSET(i + 1) + heap_begin_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700130 while (w != 0) {
131 const int shift = CLZ(w);
132 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
133 (*callback)(obj, finger, arg);
134 w &= ~(high_bit >> shift);
135 }
136 }
137 }
138 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800139 size_t end = HB_OFFSET_TO_INDEX(heap_end_ - heap_begin_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700140 for (size_t i = start; i <= end; i++) {
Ian Rogers30fab402012-01-23 15:43:46 -0800141 word w = bitmap_begin_[i];
Ian Rogers5d76c432011-10-31 21:42:49 -0700142 if (UNLIKELY(w != 0)) {
143 word high_bit = 1 << (kBitsPerWord - 1);
Ian Rogers30fab402012-01-23 15:43:46 -0800144 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + heap_begin_;
145 void* finger = reinterpret_cast<void*>(HB_INDEX_TO_OFFSET(i + 1) + heap_begin_);
Ian Rogers5d76c432011-10-31 21:42:49 -0700146 while (w != 0) {
147 const int shift = CLZ(w);
148 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
149 (*callback)(obj, finger, arg);
150 w &= ~(high_bit >> shift);
151 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700152 }
Ian Rogers30fab402012-01-23 15:43:46 -0800153 // update 'end' in case callback modified bitmap
154 end = HB_OFFSET_TO_INDEX(heap_end_ - heap_begin_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155 }
156 }
157}
158
159// Walk through the bitmaps in increasing address order, and find the
160// object pointers that correspond to garbage objects. Call
161// <callback> zero or more times with lists of these object pointers.
162//
163// The callback is not permitted to increase the max of either bitmap.
164void HeapBitmap::SweepWalk(const HeapBitmap& live_bitmap,
165 const HeapBitmap& mark_bitmap,
Ian Rogers30fab402012-01-23 15:43:46 -0800166 uintptr_t sweep_begin, uintptr_t sweep_end,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700167 HeapBitmap::SweepCallback* callback, void* arg) {
Ian Rogers30fab402012-01-23 15:43:46 -0800168 CHECK(live_bitmap.bitmap_begin_ != NULL);
169 CHECK(mark_bitmap.bitmap_begin_ != NULL);
170 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
171 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700172 CHECK(callback != NULL);
Ian Rogers30fab402012-01-23 15:43:46 -0800173 CHECK_LE(sweep_begin, sweep_end);
174 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
175 sweep_end = std::min(sweep_end - 1, live_bitmap.heap_end_);
176 if (live_bitmap.heap_end_ < live_bitmap.heap_begin_) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177 // Easy case; both are obviously empty.
178 // TODO: this should never happen
179 return;
180 }
Ian Rogers30fab402012-01-23 15:43:46 -0800181 // TODO: rewrite the callbacks to accept a std::vector<Object*> rather than a Object**?
182 std::vector<Object*> pointer_buf(4 * kBitsPerWord);
183 Object** pb = &pointer_buf[0];
184 size_t start = HB_OFFSET_TO_INDEX(sweep_begin - live_bitmap.heap_begin_);
185 size_t end = HB_OFFSET_TO_INDEX(sweep_end - live_bitmap.heap_begin_);
186 word* live = live_bitmap.bitmap_begin_;
187 word* mark = mark_bitmap.bitmap_begin_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700188 for (size_t i = start; i <= end; i++) {
Elliott Hughesb0663112011-10-19 18:16:37 -0700189 word garbage = live[i] & ~mark[i];
190 if (UNLIKELY(garbage != 0)) {
191 word high_bit = 1 << (kBitsPerWord - 1);
Ian Rogers30fab402012-01-23 15:43:46 -0800192 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + live_bitmap.heap_begin_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700193 while (garbage != 0) {
194 int shift = CLZ(garbage);
195 garbage &= ~(high_bit >> shift);
Ian Rogers30fab402012-01-23 15:43:46 -0800196 *pb++ = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700197 }
198 // Make sure that there are always enough slots available for an
199 // entire word of one bits.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700200 if (pb >= &pointer_buf[pointer_buf.size() - kBitsPerWord]) {
201 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
202 pb = &pointer_buf[0];
Carl Shapiro69759ea2011-07-21 18:13:35 -0700203 }
204 }
205 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700206 if (pb > &pointer_buf[0]) {
207 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208 }
209}
210
211} // namespace art
Ian Rogers1351b672012-02-24 12:22:57 -0800212
213// Support needed for in order traversal
214#include "object.h"
215#include "object_utils.h"
216
217namespace art {
218
219static void WalkFieldsInOrder(HeapBitmap* visited, HeapBitmap::Callback* callback, Object* obj,
220 void* arg);
221
222// Walk instance fields of the given Class. Separate function to allow recursion on the super
223// class.
224static void WalkInstanceFields(HeapBitmap* visited, HeapBitmap::Callback* callback, Object* obj,
225 Class* klass, void* arg) {
226 // Visit fields of parent classes first.
227 Class* super = klass->GetSuperClass();
228 if (super != NULL) {
229 WalkInstanceFields(visited, callback, obj, super, arg);
230 }
231 // Walk instance fields
232 ObjectArray<Field>* fields = klass->GetIFields();
233 if (fields != NULL) {
234 for (int32_t i = 0; i < fields->GetLength(); i++) {
235 Field* field = fields->Get(i);
236 FieldHelper fh(field);
237 if (!fh.GetType()->IsPrimitive()) {
238 Object* value = field->GetObj(obj);
239 if (value != NULL) {
240 WalkFieldsInOrder(visited, callback, value, arg);
241 }
242 }
243 }
244 }
245}
246
247// For an unvisited object, visit it then all its children found via fields.
248static void WalkFieldsInOrder(HeapBitmap* visited, HeapBitmap::Callback* callback, Object* obj,
249 void* arg) {
250 if (visited->Test(obj)) {
251 return;
252 }
253 // visit the object itself
254 (*callback)(obj, arg);
255 visited->Set(obj);
256 // Walk instance fields of all objects
257 Class* klass = obj->GetClass();
258 WalkInstanceFields(visited, callback, obj, klass, arg);
259 // Walk static fields of a Class
260 if (obj->IsClass()) {
261 ObjectArray<Field>* fields = klass->GetSFields();
262 if (fields != NULL) {
263 for (int32_t i = 0; i < fields->GetLength(); i++) {
264 Field* field = fields->Get(i);
265 FieldHelper fh(field);
266 if (!fh.GetType()->IsPrimitive()) {
267 Object* value = field->GetObj(NULL);
268 if (value != NULL) {
269 WalkFieldsInOrder(visited, callback, value, arg);
270 }
271 }
272 }
273 }
274 } else if (obj->IsObjectArray()) {
275 // Walk elements of an object array
276 ObjectArray<Object>* obj_array = obj->AsObjectArray<Object>();
277 int32_t length = obj_array->GetLength();
278 for (int32_t i = 0; i < length; i++) {
279 Object* value = obj_array->Get(i);
280 if (value != NULL) {
281 WalkFieldsInOrder(visited, callback, value, arg);
282 }
283 }
284 }
285}
286
287// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
288// bits or max during the traversal.
289void HeapBitmap::InOrderWalk(HeapBitmap::Callback* callback, void* arg) {
290 UniquePtr<HeapBitmap> visited (Create("bitmap for in-order walk",
291 reinterpret_cast<byte*>(heap_begin_),
292 HB_INDEX_TO_OFFSET(bitmap_size_ / kWordSize)));
293 CHECK(bitmap_begin_ != NULL);
294 CHECK(callback != NULL);
295 uintptr_t end = HB_OFFSET_TO_INDEX(heap_end_ - heap_begin_);
296 for (uintptr_t i = 0; i <= end; ++i) {
297 word w = bitmap_begin_[i];
298 if (UNLIKELY(w != 0)) {
299 word high_bit = 1 << (kBitsPerWord - 1);
300 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + heap_begin_;
301 while (w != 0) {
302 const int shift = CLZ(w);
303 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
304 WalkFieldsInOrder(visited.get(), callback, obj, arg);
305 w &= ~(high_bit >> shift);
306 }
307 }
308 }
309}
310
311} // namespace art