blob: 6edc067cc72fe9ade5ba34df112c3ef401869e16 [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.
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070067SpaceBitmap::~SpaceBitmap() {}
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070068
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070069void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
70 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
71 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070072 if (new_size < bitmap_size_) {
73 bitmap_size_ = new_size;
74 }
75 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
76 // should be marked.
77 // TODO: Fix this code is, it broken and causes rare heap corruption!
78 // mem_map_->Trim(reinterpret_cast<byte*>(heap_begin_ + bitmap_size_));
79}
80
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070081void SpaceBitmap::Clear() {
82 if (bitmap_begin_ != NULL) {
83 // This returns the memory to the system. Successive page faults
84 // will return zeroed memory.
85 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
86 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070087 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070088 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070089 }
90}
91
Mathieu Chartier357e9be2012-08-01 11:00:14 -070092void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
93 DCHECK_EQ(Size(), source_bitmap->Size());
94 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
95}
96
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070097// Visits set bits in address order. The callback is not permitted to
98// change the bitmap bits or max during the traversal.
99void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
100 CHECK(bitmap_begin_ != NULL);
101 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700102
103 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
104 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700105 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700106 word w = bitmap_begin[i];
107 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700108 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700109 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700110 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700112 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700113 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700114 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700115 }
116 }
117}
118
119// Walk through the bitmaps in increasing address order, and find the
120// object pointers that correspond to garbage objects. Call
121// <callback> zero or more times with lists of these object pointers.
122//
123// The callback is not permitted to increase the max of either bitmap.
124void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
125 const SpaceBitmap& mark_bitmap,
126 uintptr_t sweep_begin, uintptr_t sweep_end,
127 SpaceBitmap::SweepCallback* callback, void* arg) {
128 CHECK(live_bitmap.bitmap_begin_ != NULL);
129 CHECK(mark_bitmap.bitmap_begin_ != NULL);
130 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
131 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
132 CHECK(callback != NULL);
133 CHECK_LE(sweep_begin, sweep_end);
134 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700135
136 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700137 return;
138 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700141 const size_t buffer_size = kWordSize * kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 mirror::Object* pointer_buf[buffer_size];
143 mirror::Object** pb = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700144 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700145 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
146 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700147 word* live = live_bitmap.bitmap_begin_;
148 word* mark = mark_bitmap.bitmap_begin_;
149 for (size_t i = start; i <= end; i++) {
150 word garbage = live[i] & ~mark[i];
151 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700152 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700153 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700154 const size_t shift = CLZ(garbage);
155 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700157 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700158 // Make sure that there are always enough slots available for an
159 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700160 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700161 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
162 pb = &pointer_buf[0];
163 }
164 }
165 }
166 if (pb > &pointer_buf[0]) {
167 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
168 }
169}
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700172 void* arg);
173
174// Walk instance fields of the given Class. Separate function to allow recursion on the super
175// class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
177 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700179 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 if (super != NULL) {
182 WalkInstanceFields(visited, callback, obj, super, arg);
183 }
184 // Walk instance fields
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 mirror::ObjectArray<mirror::Field>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700186 if (fields != NULL) {
187 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 mirror::Field* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700189 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700190 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700192 if (value != NULL) {
193 WalkFieldsInOrder(visited, callback, value, arg);
194 }
195 }
196 }
197 }
198}
199
200// For an unvisited object, visit it then all its children found via fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700203 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700204 if (visited->Test(obj)) {
205 return;
206 }
207 // visit the object itself
208 (*callback)(obj, arg);
209 visited->Set(obj);
210 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700212 WalkInstanceFields(visited, callback, obj, klass, arg);
213 // Walk static fields of a Class
214 if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 mirror::ObjectArray<mirror::Field>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700216 if (fields != NULL) {
217 for (int32_t i = 0; i < fields->GetLength(); i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 mirror::Field* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700219 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700220 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700222 if (value != NULL) {
223 WalkFieldsInOrder(visited, callback, value, arg);
224 }
225 }
226 }
227 }
228 } else if (obj->IsObjectArray()) {
229 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700231 int32_t length = obj_array->GetLength();
232 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700234 if (value != NULL) {
235 WalkFieldsInOrder(visited, callback, value, arg);
236 }
237 }
238 }
239}
240
241// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
242// bits or max during the traversal.
243void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
244 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
245 reinterpret_cast<byte*>(heap_begin_),
246 IndexToOffset(bitmap_size_ / kWordSize)));
247 CHECK(bitmap_begin_ != NULL);
248 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700249 uintptr_t end = Size() / kWordSize;
250 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700251 word w = bitmap_begin_[i];
252 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700253 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
254 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700255 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700257 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700258 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700259 }
260 }
261 }
262}
263
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700264std::string SpaceSetMap::GetName() const {
265 return name_;
266}
267
268void SpaceSetMap::SetName(const std::string& name) {
269 name_ = name;
270}
271
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700272void SpaceSetMap::CopyFrom(const SpaceSetMap& space_set) {
273 contained_ = space_set.contained_;
274}
275
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700276std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
277 return stream
278 << bitmap.GetName() << "["
279 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
280 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
281 << "]";
Ian Rogers1d54e732013-05-02 21:10:01 -0700282}
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700283
Ian Rogers1d54e732013-05-02 21:10:01 -0700284} // namespace accounting
285} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700286} // namespace art