blob: ad8d9884ecb8fed5490a7c47a705bb9627ebbbe9 [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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080017#include "space_bitmap-inl.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070018
Ian Rogers22d5e732014-07-15 22:23:51 -070019#include "base/stringprintf.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080020#include "dex_file-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070021#include "mem_map.h"
22#include "mirror/object-inl.h"
23#include "mirror/class.h"
24#include "mirror/art_field.h"
25#include "mirror/object_array.h"
26
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070027namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070028namespace gc {
29namespace accounting {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070030
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070031template<size_t kAlignment>
Mathieu Chartier73d1e172014-04-11 17:53:48 -070032size_t SpaceBitmap<kAlignment>::ComputeBitmapSize(uint64_t capacity) {
Ian Rogers13735952014-10-08 12:43:28 -070033 const uint64_t kBytesCoveredPerWord = kAlignment * kBitsPerIntPtrT;
34 return (RoundUp(capacity, kBytesCoveredPerWord) / kBytesCoveredPerWord) * sizeof(intptr_t);
Mathieu Chartier73d1e172014-04-11 17:53:48 -070035}
36
37template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070038SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::CreateFromMemMap(
Ian Rogers13735952014-10-08 12:43:28 -070039 const std::string& name, MemMap* mem_map, uint8_t* heap_begin, size_t heap_capacity) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070040 CHECK(mem_map != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -070041 uintptr_t* bitmap_begin = reinterpret_cast<uintptr_t*>(mem_map->Begin());
Mathieu Chartier73d1e172014-04-11 17:53:48 -070042 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Mathieu Chartier31e89252013-08-28 11:29:12 -070043 return new SpaceBitmap(name, mem_map, bitmap_begin, bitmap_size, heap_begin);
44}
45
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070046template<size_t kAlignment>
Ian Rogers13735952014-10-08 12:43:28 -070047SpaceBitmap<kAlignment>::SpaceBitmap(const std::string& name, MemMap* mem_map, uintptr_t* bitmap_begin,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070048 size_t bitmap_size, const void* heap_begin)
49 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
50 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
51 name_(name) {
52 CHECK(bitmap_begin_ != nullptr);
53 CHECK_NE(bitmap_size, 0U);
54}
55
56template<size_t kAlignment>
Ian Rogers22d5e732014-07-15 22:23:51 -070057SpaceBitmap<kAlignment>::~SpaceBitmap() {}
58
59template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070060SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::Create(
Ian Rogers13735952014-10-08 12:43:28 -070061 const std::string& name, uint8_t* heap_begin, size_t heap_capacity) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070062 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
Mathieu Chartier73d1e172014-04-11 17:53:48 -070063 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070064 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070065 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size,
Vladimir Marko5c42c292015-02-25 12:02:49 +000066 PROT_READ | PROT_WRITE, false, false,
67 &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 Chartierbbd695c2014-04-16 09:48:48 -070070 return nullptr;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070071 }
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
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070075template<size_t kAlignment>
76void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Ian Rogers13735952014-10-08 12:43:28 -070077 DCHECK(IsAligned<kBitsPerIntPtrT * kAlignment>(new_end));
78 size_t new_size = OffsetToIndex(new_end - heap_begin_) * sizeof(intptr_t);
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 Chartiera8e8f9c2014-04-09 14:51:05 -070086template<size_t kAlignment>
Ian Rogers576ca0c2014-06-06 15:58:22 -070087std::string SpaceBitmap<kAlignment>::Dump() const {
88 return StringPrintf("%s: %p-%p", name_.c_str(), reinterpret_cast<void*>(HeapBegin()),
89 reinterpret_cast<void*>(HeapLimit()));
90}
91
92template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070093void SpaceBitmap<kAlignment>::Clear() {
Ian Rogersc5f17732014-06-05 20:48:42 -070094 if (bitmap_begin_ != nullptr) {
95 mem_map_->MadviseDontNeedAndZero();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070096 }
97}
98
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070099template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700100void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700101 DCHECK_EQ(Size(), source_bitmap->Size());
Ian Rogers13735952014-10-08 12:43:28 -0700102 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / sizeof(intptr_t), Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700103}
104
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700105template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700106void SpaceBitmap<kAlignment>::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700107 CHECK(bitmap_begin_ != NULL);
108 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700109
110 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700111 uintptr_t* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700112 for (uintptr_t i = 0; i <= end; ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700113 uintptr_t w = bitmap_begin[i];
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700114 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700115 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700116 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700117 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700119 (*callback)(obj, arg);
Ian Rogers13735952014-10-08 12:43:28 -0700120 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700122 }
123 }
124}
125
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700126template<size_t kAlignment>
127void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700128 const SpaceBitmap<kAlignment>& mark_bitmap,
129 uintptr_t sweep_begin, uintptr_t sweep_end,
130 SpaceBitmap::SweepCallback* callback, void* arg) {
131 CHECK(live_bitmap.bitmap_begin_ != nullptr);
132 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700133 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
134 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
135 CHECK(callback != NULL);
136 CHECK_LE(sweep_begin, sweep_end);
137 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700138
139 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700140 return;
141 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700142
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Ian Rogers13735952014-10-08 12:43:28 -0700144 constexpr size_t buffer_size = sizeof(intptr_t) * kBitsPerIntPtrT;
Andreas Gampe262a0a32014-06-04 15:25:28 -0700145#ifdef __LP64__
146 // Heap-allocate for smaller stack frame.
147 std::unique_ptr<mirror::Object*[]> pointer_buf_ptr(new mirror::Object*[buffer_size]);
148 mirror::Object** pointer_buf = pointer_buf_ptr.get();
149#else
150 // Stack-allocate buffer as it's small enough.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 mirror::Object* pointer_buf[buffer_size];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700152#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 mirror::Object** pb = &pointer_buf[0];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700154
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700155 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700156 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700157 CHECK_LT(end, live_bitmap.Size() / sizeof(intptr_t));
158 uintptr_t* live = live_bitmap.bitmap_begin_;
159 uintptr_t* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700160 for (size_t i = start; i <= end; i++) {
Ian Rogers13735952014-10-08 12:43:28 -0700161 uintptr_t garbage = live[i] & ~mark[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700162 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700163 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700164 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700165 const size_t shift = CTZ(garbage);
Ian Rogers13735952014-10-08 12:43:28 -0700166 garbage ^= (static_cast<uintptr_t>(1)) << shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700168 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700169 // Make sure that there are always enough slots available for an
170 // entire word of one bits.
Ian Rogers13735952014-10-08 12:43:28 -0700171 if (pb >= &pointer_buf[buffer_size - kBitsPerIntPtrT]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700172 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
173 pb = &pointer_buf[0];
174 }
175 }
176 }
177 if (pb > &pointer_buf[0]) {
178 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
179 }
180}
181
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700182template<size_t kAlignment>
183void SpaceBitmap<kAlignment>::WalkInstanceFields(SpaceBitmap<kAlignment>* visited,
184 ObjectCallback* callback, mirror::Object* obj,
185 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700187 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700189 if (super != NULL) {
190 WalkInstanceFields(visited, callback, obj, super, arg);
191 }
192 // Walk instance fields
Brian Carlstromea46f952013-07-30 01:26:50 -0700193 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700194 if (fields != NULL) {
195 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700196 mirror::ArtField* field = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700197 if (!field->IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700199 if (value != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700200 WalkFieldsInOrder(visited, callback, value, arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700201 }
202 }
203 }
204 }
205}
206
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700207template<size_t kAlignment>
208void SpaceBitmap<kAlignment>::WalkFieldsInOrder(SpaceBitmap<kAlignment>* visited,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700209 ObjectCallback* callback, mirror::Object* obj,
210 void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700211 if (visited->Test(obj)) {
212 return;
213 }
214 // visit the object itself
215 (*callback)(obj, arg);
216 visited->Set(obj);
217 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700219 WalkInstanceFields(visited, callback, obj, klass, arg);
220 // Walk static fields of a Class
221 if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700222 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700223 if (fields != NULL) {
224 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700225 mirror::ArtField* field = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700226 if (!field->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
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700247template<size_t kAlignment>
248void SpaceBitmap<kAlignment>::InOrderWalk(ObjectCallback* callback, void* arg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700249 std::unique_ptr<SpaceBitmap<kAlignment>> visited(
Ian Rogers13735952014-10-08 12:43:28 -0700250 Create("bitmap for in-order walk", reinterpret_cast<uint8_t*>(heap_begin_),
251 IndexToOffset(bitmap_size_ / sizeof(intptr_t))));
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700252 CHECK(bitmap_begin_ != nullptr);
253 CHECK(callback != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700254 uintptr_t end = Size() / sizeof(intptr_t);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700255 for (uintptr_t i = 0; i < end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700256 // Need uint for unsigned shift.
Ian Rogers13735952014-10-08 12:43:28 -0700257 uintptr_t w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700258 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700259 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
260 while (w != 0) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700261 const size_t shift = CTZ(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);
Ian Rogers13735952014-10-08 12:43:28 -0700264 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700265 }
266 }
267 }
268}
269
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700270template class SpaceBitmap<kObjectAlignment>;
271template class SpaceBitmap<kPageSize>;
272
Ian Rogers1d54e732013-05-02 21:10:01 -0700273} // namespace accounting
274} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700275} // namespace art