blob: f5d3b47d495b23e429751692b45ff4782105a91d [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,
Mathieu Chartier52e4b432014-06-10 11:22:31 -070066 PROT_READ | PROT_WRITE, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070067 if (UNLIKELY(mem_map.get() == nullptr)) {
68 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070069 return nullptr;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070070 }
Mathieu Chartier31e89252013-08-28 11:29:12 -070071 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070072}
73
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070074template<size_t kAlignment>
75void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Ian Rogers13735952014-10-08 12:43:28 -070076 DCHECK(IsAligned<kBitsPerIntPtrT * kAlignment>(new_end));
77 size_t new_size = OffsetToIndex(new_end - heap_begin_) * sizeof(intptr_t);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070078 if (new_size < bitmap_size_) {
79 bitmap_size_ = new_size;
80 }
81 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
82 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070083}
84
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070085template<size_t kAlignment>
Ian Rogers576ca0c2014-06-06 15:58:22 -070086std::string SpaceBitmap<kAlignment>::Dump() const {
87 return StringPrintf("%s: %p-%p", name_.c_str(), reinterpret_cast<void*>(HeapBegin()),
88 reinterpret_cast<void*>(HeapLimit()));
89}
90
91template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070092void SpaceBitmap<kAlignment>::Clear() {
Ian Rogersc5f17732014-06-05 20:48:42 -070093 if (bitmap_begin_ != nullptr) {
94 mem_map_->MadviseDontNeedAndZero();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070095 }
96}
97
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070098template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070099void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700100 DCHECK_EQ(Size(), source_bitmap->Size());
Ian Rogers13735952014-10-08 12:43:28 -0700101 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / sizeof(intptr_t), Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700102}
103
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700104template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700105void SpaceBitmap<kAlignment>::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700106 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);
Ian Rogers13735952014-10-08 12:43:28 -0700110 uintptr_t* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700111 for (uintptr_t i = 0; i <= end; ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700112 uintptr_t w = bitmap_begin[i];
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700113 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 {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700116 const size_t shift = CTZ(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);
Ian Rogers13735952014-10-08 12:43:28 -0700119 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700120 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700121 }
122 }
123}
124
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700125template<size_t kAlignment>
126void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700127 const SpaceBitmap<kAlignment>& mark_bitmap,
128 uintptr_t sweep_begin, uintptr_t sweep_end,
129 SpaceBitmap::SweepCallback* callback, void* arg) {
130 CHECK(live_bitmap.bitmap_begin_ != nullptr);
131 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700132 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**?
Ian Rogers13735952014-10-08 12:43:28 -0700143 constexpr size_t buffer_size = sizeof(intptr_t) * kBitsPerIntPtrT;
Andreas Gampe262a0a32014-06-04 15:25:28 -0700144#ifdef __LP64__
145 // Heap-allocate for smaller stack frame.
146 std::unique_ptr<mirror::Object*[]> pointer_buf_ptr(new mirror::Object*[buffer_size]);
147 mirror::Object** pointer_buf = pointer_buf_ptr.get();
148#else
149 // Stack-allocate buffer as it's small enough.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 mirror::Object* pointer_buf[buffer_size];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700151#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 mirror::Object** pb = &pointer_buf[0];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700153
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700154 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700155 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700156 CHECK_LT(end, live_bitmap.Size() / sizeof(intptr_t));
157 uintptr_t* live = live_bitmap.bitmap_begin_;
158 uintptr_t* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700159 for (size_t i = start; i <= end; i++) {
Ian Rogers13735952014-10-08 12:43:28 -0700160 uintptr_t garbage = live[i] & ~mark[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700161 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700162 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700163 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700164 const size_t shift = CTZ(garbage);
Ian Rogers13735952014-10-08 12:43:28 -0700165 garbage ^= (static_cast<uintptr_t>(1)) << shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700167 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700168 // Make sure that there are always enough slots available for an
169 // entire word of one bits.
Ian Rogers13735952014-10-08 12:43:28 -0700170 if (pb >= &pointer_buf[buffer_size - kBitsPerIntPtrT]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700171 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
172 pb = &pointer_buf[0];
173 }
174 }
175 }
176 if (pb > &pointer_buf[0]) {
177 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
178 }
179}
180
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700181template<size_t kAlignment>
182void SpaceBitmap<kAlignment>::WalkInstanceFields(SpaceBitmap<kAlignment>* visited,
183 ObjectCallback* callback, mirror::Object* obj,
184 mirror::Class* klass, void* arg)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700186 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700188 if (super != NULL) {
189 WalkInstanceFields(visited, callback, obj, super, arg);
190 }
191 // Walk instance fields
Brian Carlstromea46f952013-07-30 01:26:50 -0700192 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700193 if (fields != NULL) {
194 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700195 mirror::ArtField* field = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700196 if (!field->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) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700199 WalkFieldsInOrder(visited, callback, value, arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700200 }
201 }
202 }
203 }
204}
205
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700206template<size_t kAlignment>
207void SpaceBitmap<kAlignment>::WalkFieldsInOrder(SpaceBitmap<kAlignment>* visited,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700208 ObjectCallback* callback, mirror::Object* obj,
209 void* arg) {
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 Chartier61c5ebc2014-06-05 17:42:53 -0700225 if (!field->IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 mirror::Object* value = field->GetObj(NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700227 if (value != NULL) {
228 WalkFieldsInOrder(visited, callback, value, arg);
229 }
230 }
231 }
232 }
233 } else if (obj->IsObjectArray()) {
234 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700236 int32_t length = obj_array->GetLength();
237 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 mirror::Object* value = obj_array->Get(i);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700239 if (value != NULL) {
240 WalkFieldsInOrder(visited, callback, value, arg);
241 }
242 }
243 }
244}
245
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700246template<size_t kAlignment>
247void SpaceBitmap<kAlignment>::InOrderWalk(ObjectCallback* callback, void* arg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700248 std::unique_ptr<SpaceBitmap<kAlignment>> visited(
Ian Rogers13735952014-10-08 12:43:28 -0700249 Create("bitmap for in-order walk", reinterpret_cast<uint8_t*>(heap_begin_),
250 IndexToOffset(bitmap_size_ / sizeof(intptr_t))));
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700251 CHECK(bitmap_begin_ != nullptr);
252 CHECK(callback != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700253 uintptr_t end = Size() / sizeof(intptr_t);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700254 for (uintptr_t i = 0; i < end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700255 // Need uint for unsigned shift.
Ian Rogers13735952014-10-08 12:43:28 -0700256 uintptr_t w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700257 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700258 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
259 while (w != 0) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700260 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700262 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Ian Rogers13735952014-10-08 12:43:28 -0700263 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700264 }
265 }
266 }
267}
268
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700269template class SpaceBitmap<kObjectAlignment>;
270template class SpaceBitmap<kPageSize>;
271
Ian Rogers1d54e732013-05-02 21:10:01 -0700272} // namespace accounting
273} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700274} // namespace art