blob: 19f1128963df8ed78180bf14cb8cf9ebabcb72c3 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#include "base/logging.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070018#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070019#include "heap_bitmap.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"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "object_utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "space_bitmap-inl.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070026#include "UniquePtr.h"
27#include "utils.h"
28
29namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070030namespace gc {
31namespace accounting {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070032
Mathieu Chartier357e9be2012-08-01 11:00:14 -070033std::string SpaceBitmap::GetName() const {
34 return name_;
35}
36
37void SpaceBitmap::SetName(const std::string& name) {
38 name_ = name;
39}
40
Ian Rogers1d54e732013-05-02 21:10:01 -070041std::string SpaceBitmap::Dump() const {
42 return StringPrintf("%s: %p-%p", name_.c_str(),
43 reinterpret_cast<void*>(HeapBegin()),
44 reinterpret_cast<void*>(HeapLimit()));
45}
46
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070047void SpaceSetMap::Walk(SpaceBitmap::Callback* callback, void* arg) {
48 for (Objects::iterator it = contained_.begin(); it != contained_.end(); ++it) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 callback(const_cast<mirror::Object*>(*it), arg);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070050 }
51}
52
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070053SpaceBitmap* SpaceBitmap::Create(const std::string& name, byte* heap_begin, size_t heap_capacity) {
54 CHECK(heap_begin != NULL);
55 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
56 size_t bitmap_size = OffsetToIndex(RoundUp(heap_capacity, kAlignment * kBitsPerWord)) * kWordSize;
57 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), NULL, bitmap_size, PROT_READ | PROT_WRITE));
58 if (mem_map.get() == NULL) {
59 LOG(ERROR) << "Failed to allocate bitmap " << name;
60 return NULL;
61 }
62 word* bitmap_begin = reinterpret_cast<word*>(mem_map->Begin());
63 return new SpaceBitmap(name, mem_map.release(), bitmap_begin, bitmap_size, heap_begin);
64}
65
66// Clean up any resources associated with the bitmap.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070067SpaceBitmap::~SpaceBitmap() {
68
69}
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070070
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070071void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
72 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
73 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070074 if (new_size < bitmap_size_) {
75 bitmap_size_ = new_size;
76 }
77 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
78 // should be marked.
79 // TODO: Fix this code is, it broken and causes rare heap corruption!
80 // mem_map_->Trim(reinterpret_cast<byte*>(heap_begin_ + bitmap_size_));
81}
82
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070083void SpaceBitmap::Clear() {
84 if (bitmap_begin_ != NULL) {
85 // This returns the memory to the system. Successive page faults
86 // will return zeroed memory.
87 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
88 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070089 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070090 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070091 }
92}
93
Mathieu Chartier357e9be2012-08-01 11:00:14 -070094void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
95 DCHECK_EQ(Size(), source_bitmap->Size());
96 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
97}
98
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070099// Visits set bits in address order. The callback is not permitted to
100// change the bitmap bits or max during the traversal.
101void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
102 CHECK(bitmap_begin_ != NULL);
103 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700104
105 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
106 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700107 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700108 word w = bitmap_begin[i];
109 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700110 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700111 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700112 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700114 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700115 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700116 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700117 }
118 }
119}
120
121// Walk through the bitmaps in increasing address order, and find the
122// object pointers that correspond to garbage objects. Call
123// <callback> zero or more times with lists of these object pointers.
124//
125// The callback is not permitted to increase the max of either bitmap.
126void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
127 const SpaceBitmap& mark_bitmap,
128 uintptr_t sweep_begin, uintptr_t sweep_end,
129 SpaceBitmap::SweepCallback* callback, void* arg) {
130 CHECK(live_bitmap.bitmap_begin_ != NULL);
131 CHECK(mark_bitmap.bitmap_begin_ != NULL);
132 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
133 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
134 CHECK(callback != NULL);
135 CHECK_LE(sweep_begin, sweep_end);
136 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700137
138 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139 return;
140 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700143 const size_t buffer_size = kWordSize * kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 mirror::Object* pointer_buf[buffer_size];
145 mirror::Object** pb = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700146 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700147 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
148 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700149 word* live = live_bitmap.bitmap_begin_;
150 word* mark = mark_bitmap.bitmap_begin_;
151 for (size_t i = start; i <= end; i++) {
152 word garbage = live[i] & ~mark[i];
153 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700154 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700155 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700156 const size_t shift = CLZ(garbage);
157 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700159 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700160 // Make sure that there are always enough slots available for an
161 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700162 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700163 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
164 pb = &pointer_buf[0];
165 }
166 }
167 }
168 if (pb > &pointer_buf[0]) {
169 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
170 }
171}
172
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700174 void* arg);
175
176// Walk instance fields of the given Class. Separate function to allow recursion on the super
177// class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
179 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700180 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700183 if (super != NULL) {
184 WalkInstanceFields(visited, callback, obj, super, arg);
185 }
186 // Walk instance fields
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 mirror::ObjectArray<mirror::Field>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700188 if (fields != NULL) {
189 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 mirror::Field* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700191 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700192 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700194 if (value != NULL) {
195 WalkFieldsInOrder(visited, callback, value, arg);
196 }
197 }
198 }
199 }
200}
201
202// For an unvisited object, visit it then all its children found via fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700206 if (visited->Test(obj)) {
207 return;
208 }
209 // visit the object itself
210 (*callback)(obj, arg);
211 visited->Set(obj);
212 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700214 WalkInstanceFields(visited, callback, obj, klass, arg);
215 // Walk static fields of a Class
216 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::ObjectArray<mirror::Field>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700218 if (fields != NULL) {
219 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 mirror::Field* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700221 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700222 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700224 if (value != NULL) {
225 WalkFieldsInOrder(visited, callback, value, arg);
226 }
227 }
228 }
229 }
230 } else if (obj->IsObjectArray()) {
231 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700233 int32_t length = obj_array->GetLength();
234 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700236 if (value != NULL) {
237 WalkFieldsInOrder(visited, callback, value, arg);
238 }
239 }
240 }
241}
242
243// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
244// bits or max during the traversal.
245void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
246 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
247 reinterpret_cast<byte*>(heap_begin_),
248 IndexToOffset(bitmap_size_ / kWordSize)));
249 CHECK(bitmap_begin_ != NULL);
250 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700251 uintptr_t end = Size() / kWordSize;
252 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700253 word w = bitmap_begin_[i];
254 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700255 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
256 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700257 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700259 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700260 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700261 }
262 }
263 }
264}
265
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700266std::string SpaceSetMap::GetName() const {
267 return name_;
268}
269
270void SpaceSetMap::SetName(const std::string& name) {
271 name_ = name;
272}
273
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700274void SpaceSetMap::CopyFrom(const SpaceSetMap& space_set) {
275 contained_ = space_set.contained_;
276}
277
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700278std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
279 return stream
280 << bitmap.GetName() << "["
281 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
282 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
283 << "]";
Ian Rogers1d54e732013-05-02 21:10:01 -0700284}
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700285
Ian Rogers1d54e732013-05-02 21:10:01 -0700286} // namespace accounting
287} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700288} // namespace art