blob: d90c0901877f01064a99a87af314d5b91d2dbeac [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
17#include "heap_bitmap.h"
18
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/class-inl.h"
21#include "mirror/field-inl.h"
22#include "mirror/object-inl.h"
23#include "mirror/object_array-inl.h"
24#include "space_bitmap-inl.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070025#include "UniquePtr.h"
26#include "utils.h"
27
28namespace art {
29
Mathieu Chartier357e9be2012-08-01 11:00:14 -070030std::string SpaceBitmap::GetName() const {
31 return name_;
32}
33
34void SpaceBitmap::SetName(const std::string& name) {
35 name_ = name;
36}
37
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070038void SpaceSetMap::Walk(SpaceBitmap::Callback* callback, void* arg) {
39 for (Objects::iterator it = contained_.begin(); it != contained_.end(); ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040 callback(const_cast<mirror::Object*>(*it), arg);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070041 }
42}
43
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070044SpaceBitmap* SpaceBitmap::Create(const std::string& name, byte* heap_begin, size_t heap_capacity) {
45 CHECK(heap_begin != NULL);
46 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
47 size_t bitmap_size = OffsetToIndex(RoundUp(heap_capacity, kAlignment * kBitsPerWord)) * kWordSize;
48 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), NULL, bitmap_size, PROT_READ | PROT_WRITE));
49 if (mem_map.get() == NULL) {
50 LOG(ERROR) << "Failed to allocate bitmap " << name;
51 return NULL;
52 }
53 word* bitmap_begin = reinterpret_cast<word*>(mem_map->Begin());
54 return new SpaceBitmap(name, mem_map.release(), bitmap_begin, bitmap_size, heap_begin);
55}
56
57// Clean up any resources associated with the bitmap.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070058SpaceBitmap::~SpaceBitmap() {
59
60}
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070062void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
63 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
64 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070065 if (new_size < bitmap_size_) {
66 bitmap_size_ = new_size;
67 }
68 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
69 // should be marked.
70 // TODO: Fix this code is, it broken and causes rare heap corruption!
71 // mem_map_->Trim(reinterpret_cast<byte*>(heap_begin_ + bitmap_size_));
72}
73
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070074// Fill the bitmap with zeroes. Returns the bitmap's memory to the
75// system as a side-effect.
76void SpaceBitmap::Clear() {
77 if (bitmap_begin_ != NULL) {
78 // This returns the memory to the system. Successive page faults
79 // will return zeroed memory.
80 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
81 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070082 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070083 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070084 }
85}
86
Mathieu Chartier357e9be2012-08-01 11:00:14 -070087void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
88 DCHECK_EQ(Size(), source_bitmap->Size());
89 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
90}
91
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070092// Visits set bits in address order. The callback is not permitted to
93// change the bitmap bits or max during the traversal.
94void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
95 CHECK(bitmap_begin_ != NULL);
96 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070097
98 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
99 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700100 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700101 word w = bitmap_begin[i];
102 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700104 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700105 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700107 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700108 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700109 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700110 }
111 }
112}
113
114// Walk through the bitmaps in increasing address order, and find the
115// object pointers that correspond to garbage objects. Call
116// <callback> zero or more times with lists of these object pointers.
117//
118// The callback is not permitted to increase the max of either bitmap.
119void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
120 const SpaceBitmap& mark_bitmap,
121 uintptr_t sweep_begin, uintptr_t sweep_end,
122 SpaceBitmap::SweepCallback* callback, void* arg) {
123 CHECK(live_bitmap.bitmap_begin_ != NULL);
124 CHECK(mark_bitmap.bitmap_begin_ != NULL);
125 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
126 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
127 CHECK(callback != NULL);
128 CHECK_LE(sweep_begin, sweep_end);
129 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700130
131 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700132 return;
133 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700134
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700136 const size_t buffer_size = kWordSize * kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 mirror::Object* pointer_buf[buffer_size];
138 mirror::Object** pb = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700140 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
141 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700142 word* live = live_bitmap.bitmap_begin_;
143 word* mark = mark_bitmap.bitmap_begin_;
144 for (size_t i = start; i <= end; i++) {
145 word garbage = live[i] & ~mark[i];
146 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700147 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700148 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700149 const size_t shift = CLZ(garbage);
150 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700152 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700153 // Make sure that there are always enough slots available for an
154 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700155 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700156 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
157 pb = &pointer_buf[0];
158 }
159 }
160 }
161 if (pb > &pointer_buf[0]) {
162 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
163 }
164}
165
166} // namespace art
167
168// Support needed for in order traversal
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169#include "mirror/object.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700170#include "object_utils.h"
171
172namespace art {
173
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700175 void* arg);
176
177// Walk instance fields of the given Class. Separate function to allow recursion on the super
178// class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
180 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700182 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700184 if (super != NULL) {
185 WalkInstanceFields(visited, callback, obj, super, arg);
186 }
187 // Walk instance fields
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 mirror::ObjectArray<mirror::Field>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700189 if (fields != NULL) {
190 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 mirror::Field* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700192 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700193 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700195 if (value != NULL) {
196 WalkFieldsInOrder(visited, callback, value, arg);
197 }
198 }
199 }
200 }
201}
202
203// For an unvisited object, visit it then all its children found via fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700205 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700207 if (visited->Test(obj)) {
208 return;
209 }
210 // visit the object itself
211 (*callback)(obj, arg);
212 visited->Set(obj);
213 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700215 WalkInstanceFields(visited, callback, obj, klass, arg);
216 // Walk static fields of a Class
217 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 mirror::ObjectArray<mirror::Field>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700219 if (fields != NULL) {
220 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 mirror::Field* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700222 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700223 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700225 if (value != NULL) {
226 WalkFieldsInOrder(visited, callback, value, arg);
227 }
228 }
229 }
230 }
231 } else if (obj->IsObjectArray()) {
232 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700234 int32_t length = obj_array->GetLength();
235 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700237 if (value != NULL) {
238 WalkFieldsInOrder(visited, callback, value, arg);
239 }
240 }
241 }
242}
243
244// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
245// bits or max during the traversal.
246void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
247 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
248 reinterpret_cast<byte*>(heap_begin_),
249 IndexToOffset(bitmap_size_ / kWordSize)));
250 CHECK(bitmap_begin_ != NULL);
251 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700252 uintptr_t end = Size() / kWordSize;
253 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700254 word w = bitmap_begin_[i];
255 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700256 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
257 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700258 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700260 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700261 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700262 }
263 }
264 }
265}
266
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700267std::string SpaceSetMap::GetName() const {
268 return name_;
269}
270
271void SpaceSetMap::SetName(const std::string& name) {
272 name_ = name;
273}
274
275SpaceSetMap::SpaceSetMap(const std::string& name) : name_(name) {
276
277}
278
279void SpaceSetMap::CopyFrom(const SpaceSetMap& space_set) {
280 contained_ = space_set.contained_;
281}
282
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700283std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
284 return stream
285 << bitmap.GetName() << "["
286 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
287 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
288 << "]";
289 }
290
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700291} // namespace art