blob: a080beedb023bba2b6ab1a50540e38cd10d458d2 [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 Chartierdb7f37d2014-01-10 11:09:06 -080047void ObjectSet::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;
Ian Rogers8d31bbd2013-10-13 10:44:14 -070065 std::string error_msg;
66 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), NULL, bitmap_size,
Ian Rogersef7d42f2014-01-06 12:55:46 -080067 PROT_READ | PROT_WRITE, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070068 if (UNLIKELY(mem_map.get() == nullptr)) {
69 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070070 return NULL;
71 }
Mathieu Chartier31e89252013-08-28 11:29:12 -070072 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070073}
74
75// Clean up any resources associated with the bitmap.
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070076SpaceBitmap::~SpaceBitmap() {}
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070077
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070078void SpaceBitmap::SetHeapLimit(uintptr_t new_end) {
79 DCHECK(IsAligned<kBitsPerWord * kAlignment>(new_end));
80 size_t new_size = OffsetToIndex(new_end - heap_begin_) * kWordSize;
Mathieu Chartiercc236d72012-07-20 10:29:05 -070081 if (new_size < bitmap_size_) {
82 bitmap_size_ = new_size;
83 }
84 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
85 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070086}
87
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070088void SpaceBitmap::Clear() {
89 if (bitmap_begin_ != NULL) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070090 // This returns the memory to the system. Successive page faults will return zeroed memory.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070091 int result = madvise(bitmap_begin_, bitmap_size_, MADV_DONTNEED);
92 if (result == -1) {
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070093 PLOG(FATAL) << "madvise failed";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070094 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070095 }
96}
97
Mathieu Chartier357e9be2012-08-01 11:00:14 -070098void SpaceBitmap::CopyFrom(SpaceBitmap* source_bitmap) {
99 DCHECK_EQ(Size(), source_bitmap->Size());
100 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / kWordSize, Begin());
101}
102
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103// Visits set bits in address order. The callback is not permitted to
104// change the bitmap bits or max during the traversal.
105void SpaceBitmap::Walk(SpaceBitmap::Callback* callback, void* arg) {
106 CHECK(bitmap_begin_ != NULL);
107 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700108
109 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
110 word* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700111 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700112 word w = bitmap_begin[i];
113 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700114 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700115 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700116 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700118 (*callback)(obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700119 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700120 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700121 }
122 }
123}
124
125// Walk through the bitmaps in increasing address order, and find the
126// object pointers that correspond to garbage objects. Call
127// <callback> zero or more times with lists of these object pointers.
128//
129// The callback is not permitted to increase the max of either bitmap.
130void SpaceBitmap::SweepWalk(const SpaceBitmap& live_bitmap,
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800131 const SpaceBitmap& mark_bitmap,
132 uintptr_t sweep_begin, uintptr_t sweep_end,
133 SpaceBitmap::SweepCallback* callback, void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700134 CHECK(live_bitmap.bitmap_begin_ != NULL);
135 CHECK(mark_bitmap.bitmap_begin_ != NULL);
136 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
137 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
138 CHECK(callback != NULL);
139 CHECK_LE(sweep_begin, sweep_end);
140 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700141
142 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700143 return;
144 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700145
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700147 const size_t buffer_size = kWordSize * kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 mirror::Object* pointer_buf[buffer_size];
149 mirror::Object** pb = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700150 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700151 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
152 CHECK_LT(end, live_bitmap.Size() / kWordSize);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700153 word* live = live_bitmap.bitmap_begin_;
154 word* mark = mark_bitmap.bitmap_begin_;
155 for (size_t i = start; i <= end; i++) {
156 word garbage = live[i] & ~mark[i];
157 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700158 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700159 do {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700160 const size_t shift = CLZ(garbage);
161 garbage ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700163 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700164 // Make sure that there are always enough slots available for an
165 // entire word of one bits.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700166 if (pb >= &pointer_buf[buffer_size - kBitsPerWord]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700167 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
168 pb = &pointer_buf[0];
169 }
170 }
171 }
172 if (pb > &pointer_buf[0]) {
173 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
174 }
175}
176
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700178 void* arg);
179
180// Walk instance fields of the given Class. Separate function to allow recursion on the super
181// class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182static void WalkInstanceFields(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
183 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700184 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700185 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700187 if (super != NULL) {
188 WalkInstanceFields(visited, callback, obj, super, arg);
189 }
190 // Walk instance fields
Brian Carlstromea46f952013-07-30 01:26:50 -0700191 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700192 if (fields != NULL) {
193 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700194 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700195 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700196 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700198 if (value != NULL) {
199 WalkFieldsInOrder(visited, callback, value, arg);
200 }
201 }
202 }
203 }
204}
205
206// For an unvisited object, visit it then all its children found via fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207static void WalkFieldsInOrder(SpaceBitmap* visited, SpaceBitmap::Callback* callback, mirror::Object* obj,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700209 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700210 if (visited->Test(obj)) {
211 return;
212 }
213 // visit the object itself
214 (*callback)(obj, arg);
215 visited->Set(obj);
216 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700218 WalkInstanceFields(visited, callback, obj, klass, arg);
219 // Walk static fields of a Class
220 if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700221 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700222 if (fields != NULL) {
223 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700224 mirror::ArtField* field = fields->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700225 FieldHelper fh(field);
Mathieu Chartier66f19252012-09-18 08:57:04 -0700226 if (!fh.IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700228 if (value != NULL) {
229 WalkFieldsInOrder(visited, callback, value, arg);
230 }
231 }
232 }
233 }
234 } else if (obj->IsObjectArray()) {
235 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700237 int32_t length = obj_array->GetLength();
238 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700240 if (value != NULL) {
241 WalkFieldsInOrder(visited, callback, value, arg);
242 }
243 }
244 }
245}
246
247// Visits set bits with an in order traversal. The callback is not permitted to change the bitmap
248// bits or max during the traversal.
249void SpaceBitmap::InOrderWalk(SpaceBitmap::Callback* callback, void* arg) {
250 UniquePtr<SpaceBitmap> visited(Create("bitmap for in-order walk",
251 reinterpret_cast<byte*>(heap_begin_),
252 IndexToOffset(bitmap_size_ / kWordSize)));
253 CHECK(bitmap_begin_ != NULL);
254 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700255 uintptr_t end = Size() / kWordSize;
256 for (uintptr_t i = 0; i < end; ++i) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700257 word w = bitmap_begin_[i];
258 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700259 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
260 while (w != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700261 const size_t shift = CLZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700263 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700264 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700265 }
266 }
267 }
268}
269
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700270std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap) {
271 return stream
272 << bitmap.GetName() << "["
273 << "begin=" << reinterpret_cast<const void*>(bitmap.HeapBegin())
274 << ",end=" << reinterpret_cast<const void*>(bitmap.HeapLimit())
275 << "]";
Ian Rogers1d54e732013-05-02 21:10:01 -0700276}
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700277
Ian Rogers1d54e732013-05-02 21:10:01 -0700278} // namespace accounting
279} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700280} // namespace art