blob: feb9565ccf6c06c0ed4a2f5a93e906176c973013 [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"
20#include "mem_map.h"
21#include "mirror/object-inl.h"
22#include "mirror/class.h"
23#include "mirror/art_field.h"
24#include "mirror/object_array.h"
25
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070026namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace accounting {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070029
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070030template<size_t kAlignment>
Mathieu Chartier73d1e172014-04-11 17:53:48 -070031size_t SpaceBitmap<kAlignment>::ComputeBitmapSize(uint64_t capacity) {
Ian Rogers13735952014-10-08 12:43:28 -070032 const uint64_t kBytesCoveredPerWord = kAlignment * kBitsPerIntPtrT;
33 return (RoundUp(capacity, kBytesCoveredPerWord) / kBytesCoveredPerWord) * sizeof(intptr_t);
Mathieu Chartier73d1e172014-04-11 17:53:48 -070034}
35
36template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070037SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::CreateFromMemMap(
Ian Rogers13735952014-10-08 12:43:28 -070038 const std::string& name, MemMap* mem_map, uint8_t* heap_begin, size_t heap_capacity) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070039 CHECK(mem_map != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -070040 uintptr_t* bitmap_begin = reinterpret_cast<uintptr_t*>(mem_map->Begin());
Mathieu Chartier73d1e172014-04-11 17:53:48 -070041 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Mathieu Chartier31e89252013-08-28 11:29:12 -070042 return new SpaceBitmap(name, mem_map, bitmap_begin, bitmap_size, heap_begin);
43}
44
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070045template<size_t kAlignment>
Ian Rogers13735952014-10-08 12:43:28 -070046SpaceBitmap<kAlignment>::SpaceBitmap(const std::string& name, MemMap* mem_map, uintptr_t* bitmap_begin,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070047 size_t bitmap_size, const void* heap_begin)
48 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
49 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
50 name_(name) {
51 CHECK(bitmap_begin_ != nullptr);
52 CHECK_NE(bitmap_size, 0U);
53}
54
55template<size_t kAlignment>
Ian Rogers22d5e732014-07-15 22:23:51 -070056SpaceBitmap<kAlignment>::~SpaceBitmap() {}
57
58template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070059SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::Create(
Ian Rogers13735952014-10-08 12:43:28 -070060 const std::string& name, uint8_t* heap_begin, size_t heap_capacity) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070061 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
Mathieu Chartier73d1e172014-04-11 17:53:48 -070062 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070063 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070064 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size,
Mathieu Chartier52e4b432014-06-10 11:22:31 -070065 PROT_READ | PROT_WRITE, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066 if (UNLIKELY(mem_map.get() == nullptr)) {
67 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070068 return nullptr;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070069 }
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
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070073template<size_t kAlignment>
74void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Ian Rogers13735952014-10-08 12:43:28 -070075 DCHECK(IsAligned<kBitsPerIntPtrT * kAlignment>(new_end));
76 size_t new_size = OffsetToIndex(new_end - heap_begin_) * sizeof(intptr_t);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070077 if (new_size < bitmap_size_) {
78 bitmap_size_ = new_size;
79 }
80 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
81 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070082}
83
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070084template<size_t kAlignment>
Ian Rogers576ca0c2014-06-06 15:58:22 -070085std::string SpaceBitmap<kAlignment>::Dump() const {
86 return StringPrintf("%s: %p-%p", name_.c_str(), reinterpret_cast<void*>(HeapBegin()),
87 reinterpret_cast<void*>(HeapLimit()));
88}
89
90template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070091void SpaceBitmap<kAlignment>::Clear() {
Ian Rogersc5f17732014-06-05 20:48:42 -070092 if (bitmap_begin_ != nullptr) {
93 mem_map_->MadviseDontNeedAndZero();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070094 }
95}
96
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070097template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070098void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070099 DCHECK_EQ(Size(), source_bitmap->Size());
Ian Rogers13735952014-10-08 12:43:28 -0700100 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / sizeof(intptr_t), Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700101}
102
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700103template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700104void SpaceBitmap<kAlignment>::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700105 CHECK(bitmap_begin_ != NULL);
106 CHECK(callback != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700107
108 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700109 uintptr_t* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700110 for (uintptr_t i = 0; i <= end; ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700111 uintptr_t w = bitmap_begin[i];
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700112 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700113 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700114 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700115 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700117 (*callback)(obj, arg);
Ian Rogers13735952014-10-08 12:43:28 -0700118 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700119 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700120 }
121 }
122}
123
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700124template<size_t kAlignment>
125void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700126 const SpaceBitmap<kAlignment>& mark_bitmap,
127 uintptr_t sweep_begin, uintptr_t sweep_end,
128 SpaceBitmap::SweepCallback* callback, void* arg) {
129 CHECK(live_bitmap.bitmap_begin_ != nullptr);
130 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700131 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
132 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
133 CHECK(callback != NULL);
134 CHECK_LE(sweep_begin, sweep_end);
135 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700136
137 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 return;
139 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700140
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Ian Rogers13735952014-10-08 12:43:28 -0700142 constexpr size_t buffer_size = sizeof(intptr_t) * kBitsPerIntPtrT;
Andreas Gampe262a0a32014-06-04 15:25:28 -0700143#ifdef __LP64__
144 // Heap-allocate for smaller stack frame.
145 std::unique_ptr<mirror::Object*[]> pointer_buf_ptr(new mirror::Object*[buffer_size]);
146 mirror::Object** pointer_buf = pointer_buf_ptr.get();
147#else
148 // Stack-allocate buffer as it's small enough.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 mirror::Object* pointer_buf[buffer_size];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700150#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 mirror::Object** pb = &pointer_buf[0];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700152
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700153 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700154 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700155 CHECK_LT(end, live_bitmap.Size() / sizeof(intptr_t));
156 uintptr_t* live = live_bitmap.bitmap_begin_;
157 uintptr_t* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700158 for (size_t i = start; i <= end; i++) {
Ian Rogers13735952014-10-08 12:43:28 -0700159 uintptr_t garbage = live[i] & ~mark[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700160 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700161 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700162 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700163 const size_t shift = CTZ(garbage);
Ian Rogers13735952014-10-08 12:43:28 -0700164 garbage ^= (static_cast<uintptr_t>(1)) << shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700166 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700167 // Make sure that there are always enough slots available for an
168 // entire word of one bits.
Ian Rogers13735952014-10-08 12:43:28 -0700169 if (pb >= &pointer_buf[buffer_size - kBitsPerIntPtrT]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700170 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
171 pb = &pointer_buf[0];
172 }
173 }
174 }
175 if (pb > &pointer_buf[0]) {
176 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
177 }
178}
179
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700180template<size_t kAlignment>
181void SpaceBitmap<kAlignment>::WalkInstanceFields(SpaceBitmap<kAlignment>* visited,
182 ObjectCallback* 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 Chartier61c5ebc2014-06-05 17:42:53 -0700195 if (!field->IsPrimitiveType()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 mirror::Object* value = field->GetObj(obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700197 if (value != NULL) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700198 WalkFieldsInOrder(visited, callback, value, arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700199 }
200 }
201 }
202 }
203}
204
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700205template<size_t kAlignment>
206void SpaceBitmap<kAlignment>::WalkFieldsInOrder(SpaceBitmap<kAlignment>* visited,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700207 ObjectCallback* callback, mirror::Object* obj,
208 void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700209 if (visited->Test(obj)) {
210 return;
211 }
212 // visit the object itself
213 (*callback)(obj, arg);
214 visited->Set(obj);
215 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700217 WalkInstanceFields(visited, callback, obj, klass, arg);
218 // Walk static fields of a Class
219 if (obj->IsClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700220 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetSFields();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700221 if (fields != NULL) {
222 for (int32_t i = 0; i < fields->GetLength(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700223 mirror::ArtField* field = fields->Get(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700224 if (!field->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
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700245template<size_t kAlignment>
246void SpaceBitmap<kAlignment>::InOrderWalk(ObjectCallback* callback, void* arg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700247 std::unique_ptr<SpaceBitmap<kAlignment>> visited(
Ian Rogers13735952014-10-08 12:43:28 -0700248 Create("bitmap for in-order walk", reinterpret_cast<uint8_t*>(heap_begin_),
249 IndexToOffset(bitmap_size_ / sizeof(intptr_t))));
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700250 CHECK(bitmap_begin_ != nullptr);
251 CHECK(callback != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700252 uintptr_t end = Size() / sizeof(intptr_t);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700253 for (uintptr_t i = 0; i < end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700254 // Need uint for unsigned shift.
Ian Rogers13735952014-10-08 12:43:28 -0700255 uintptr_t w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700256 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700257 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
258 while (w != 0) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700259 const size_t shift = CTZ(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);
Ian Rogers13735952014-10-08 12:43:28 -0700262 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700263 }
264 }
265 }
266}
267
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700268template class SpaceBitmap<kObjectAlignment>;
269template class SpaceBitmap<kPageSize>;
270
Ian Rogers1d54e732013-05-02 21:10:01 -0700271} // namespace accounting
272} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700273} // namespace art