blob: 63b24ffb4e2c45bd2fca8e245f50504cb6976f4a [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"
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "mirror/art_field-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#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) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070048 for (const mirror::Object* obj : contained_) {
49 callback(const_cast<mirror::Object*>(obj), arg);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070050 }
51}
52
Mathieu Chartier31e89252013-08-28 11:29:12 -070053SpaceBitmap* SpaceBitmap::CreateFromMemMap(const std::string& name, MemMap* mem_map,
54 byte* heap_begin, size_t heap_capacity) {
55 CHECK(mem_map != nullptr);
56 word* bitmap_begin = reinterpret_cast<word*>(mem_map->Begin());
57 size_t bitmap_size = OffsetToIndex(RoundUp(heap_capacity, kAlignment * kBitsPerWord)) * kWordSize;
58 return new SpaceBitmap(name, mem_map, bitmap_begin, bitmap_size, heap_begin);
59}
60
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061SpaceBitmap* SpaceBitmap::Create(const std::string& name, byte* heap_begin, size_t heap_capacity) {
62 CHECK(heap_begin != NULL);
63 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
64 size_t bitmap_size = OffsetToIndex(RoundUp(heap_capacity, kAlignment * kBitsPerWord)) * kWordSize;
65 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), NULL, bitmap_size, PROT_READ | PROT_WRITE));
66 if (mem_map.get() == NULL) {
67 LOG(ERROR) << "Failed to allocate bitmap " << name;
68 return NULL;
69 }
Mathieu Chartier31e89252013-08-28 11:29:12 -070070 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070071}
72
73// Clean up any resources associated with the bitmap.
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070074SpaceBitmap::~SpaceBitmap() {}
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070075
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070076void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
77 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
78 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070079 if (new_size < bitmap_size_) {
80 bitmap_size_ = new_size;
81 }
82 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
83 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070084}
85
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070086void SpaceBitmap::Clear() {
87 if (bitmap_begin_ != NULL) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070088 // This returns the memory to the system. Successive page faults will return zeroed memory.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070089 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
90 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070091 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070092 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070093 }
94}
95
Mathieu Chartier357e9be2012-08-01 11:00:14 -070096void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
97 DCHECK_EQ(Size(), source_bitmap->Size());
98 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
99}
100
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700101// Visits set bits in address order. The callback is not permitted to
102// change the bitmap bits or max during the traversal.
103void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
104 CHECK(bitmap_begin_ != NULL);
105 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700106
107 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
108 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700109 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700110 word w = bitmap_begin[i];
111 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700112 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700113 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700114 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700116 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700117 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700118 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700119 }
120 }
121}
122
123// Walk through the bitmaps in increasing address order, and find the
124// object pointers that correspond to garbage objects. Call
125// <callback> zero or more times with lists of these object pointers.
126//
127// The callback is not permitted to increase the max of either bitmap.
128void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
129 const SpaceBitmap& mark_bitmap,
130 uintptr_t sweep_begin, uintptr_t sweep_end,
131 SpaceBitmap::SweepCallback* callback, void* arg) {
132 CHECK(live_bitmap.bitmap_begin_ != NULL);
133 CHECK(mark_bitmap.bitmap_begin_ != NULL);
134 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
135 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
136 CHECK(callback != NULL);
137 CHECK_LE(sweep_begin, sweep_end);
138 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700139
140 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700141 return;
142 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700143
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700145 const size_t buffer_size = kWordSize * kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 mirror::Object* pointer_buf[buffer_size];
147 mirror::Object** pb = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700148 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700149 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
150 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700151 word* live = live_bitmap.bitmap_begin_;
152 word* mark = mark_bitmap.bitmap_begin_;
153 for (size_t i = start; i <= end; i++) {
154 word garbage = live[i] & ~mark[i];
155 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700156 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700157 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700158 const size_t shift = CLZ(garbage);
159 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700161 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700162 // Make sure that there are always enough slots available for an
163 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700164 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700165 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
166 pb = &pointer_buf[0];
167 }
168 }
169 }
170 if (pb > &pointer_buf[0]) {
171 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
172 }
173}
174
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700176 void* arg);
177
178// Walk instance fields of the given Class. Separate function to allow recursion on the super
179// class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
181 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700183 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700185 if (super != NULL) {
186 WalkInstanceFields(visited, callback, obj, super, arg);
187 }
188 // Walk instance fields
Brian Carlstromea46f952013-07-30 01:26:50 -0700189 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700190 if (fields != NULL) {
191 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700192 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700193 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700194 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700196 if (value != NULL) {
197 WalkFieldsInOrder(visited, callback, value, arg);
198 }
199 }
200 }
201 }
202}
203
204// For an unvisited object, visit it then all its children found via fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700208 if (visited->Test(obj)) {
209 return;
210 }
211 // visit the object itself
212 (*callback)(obj, arg);
213 visited->Set(obj);
214 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700216 WalkInstanceFields(visited, callback, obj, klass, arg);
217 // Walk static fields of a Class
218 if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700219 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700220 if (fields != NULL) {
221 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700222 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700223 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700224 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700226 if (value != NULL) {
227 WalkFieldsInOrder(visited, callback, value, arg);
228 }
229 }
230 }
231 }
232 } else if (obj->IsObjectArray()) {
233 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700235 int32_t length = obj_array->GetLength();
236 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700238 if (value != NULL) {
239 WalkFieldsInOrder(visited, callback, value, arg);
240 }
241 }
242 }
243}
244
245// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
246// bits or max during the traversal.
247void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
248 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
249 reinterpret_cast<byte*>(heap_begin_),
250 IndexToOffset(bitmap_size_ / kWordSize)));
251 CHECK(bitmap_begin_ != NULL);
252 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700253 uintptr_t end = Size() / kWordSize;
254 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700255 word w = bitmap_begin_[i];
256 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700257 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
258 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700259 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700261 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700262 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700263 }
264 }
265 }
266}
267
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700268std::string SpaceSetMap::GetName() const {
269 return name_;
270}
271
272void SpaceSetMap::SetName(const std::string& name) {
273 name_ = name;
274}
275
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700276void SpaceSetMap::CopyFrom(const SpaceSetMap& space_set) {
277 contained_ = space_set.contained_;
278}
279
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700280std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
281 return stream
282 << bitmap.GetName() << "["
283 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
284 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
285 << "]";
Ian Rogers1d54e732013-05-02 21:10:01 -0700286}
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700287
Ian Rogers1d54e732013-05-02 21:10:01 -0700288} // namespace accounting
289} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700290} // namespace art