blob: 3df02ed443dfc5a82c6141dd40d241eafd1ea09c [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070020#include "base/stringprintf.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080021#include "dex_file-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070022#include "mem_map.h"
23#include "mirror/object-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "mirror/class-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070025#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 Chartierd39645e2015-06-09 17:50:29 -070038size_t SpaceBitmap<kAlignment>::ComputeHeapSize(uint64_t bitmap_bytes) {
39 return bitmap_bytes * kBitsPerByte * kAlignment;
40}
41
42template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070043SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::CreateFromMemMap(
Ian Rogers13735952014-10-08 12:43:28 -070044 const std::string& name, MemMap* mem_map, uint8_t* heap_begin, size_t heap_capacity) {
Mathieu Chartier31e89252013-08-28 11:29:12 -070045 CHECK(mem_map != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -070046 uintptr_t* bitmap_begin = reinterpret_cast<uintptr_t*>(mem_map->Begin());
Mathieu Chartier73d1e172014-04-11 17:53:48 -070047 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Mathieu Chartier31e89252013-08-28 11:29:12 -070048 return new SpaceBitmap(name, mem_map, bitmap_begin, bitmap_size, heap_begin);
49}
50
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070051template<size_t kAlignment>
Ian Rogers13735952014-10-08 12:43:28 -070052SpaceBitmap<kAlignment>::SpaceBitmap(const std::string& name, MemMap* mem_map, uintptr_t* bitmap_begin,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070053 size_t bitmap_size, const void* heap_begin)
Mathieu Chartierc381c362016-08-23 13:27:53 -070054 : mem_map_(mem_map),
55 bitmap_begin_(reinterpret_cast<Atomic<uintptr_t>*>(bitmap_begin)),
56 bitmap_size_(bitmap_size),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070057 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
58 name_(name) {
59 CHECK(bitmap_begin_ != nullptr);
60 CHECK_NE(bitmap_size, 0U);
61}
62
63template<size_t kAlignment>
Ian Rogers22d5e732014-07-15 22:23:51 -070064SpaceBitmap<kAlignment>::~SpaceBitmap() {}
65
66template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070067SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::Create(
Ian Rogers13735952014-10-08 12:43:28 -070068 const std::string& name, uint8_t* heap_begin, size_t heap_capacity) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070069 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
Mathieu Chartier73d1e172014-04-11 17:53:48 -070070 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070071 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070072 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size,
Vladimir Marko5c42c292015-02-25 12:02:49 +000073 PROT_READ | PROT_WRITE, false, false,
74 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070075 if (UNLIKELY(mem_map.get() == nullptr)) {
76 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070077 return nullptr;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070078 }
Mathieu Chartier31e89252013-08-28 11:29:12 -070079 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070080}
81
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070082template<size_t kAlignment>
83void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Roland Levillain14d90572015-07-16 10:52:26 +010084 DCHECK_ALIGNED(new_end, kBitsPerIntPtrT * kAlignment);
Ian Rogers13735952014-10-08 12:43:28 -070085 size_t new_size = OffsetToIndex(new_end - heap_begin_) * sizeof(intptr_t);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070086 if (new_size < bitmap_size_) {
87 bitmap_size_ = new_size;
88 }
89 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
90 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070091}
92
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070093template<size_t kAlignment>
Ian Rogers576ca0c2014-06-06 15:58:22 -070094std::string SpaceBitmap<kAlignment>::Dump() const {
95 return StringPrintf("%s: %p-%p", name_.c_str(), reinterpret_cast<void*>(HeapBegin()),
96 reinterpret_cast<void*>(HeapLimit()));
97}
98
99template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700100void SpaceBitmap<kAlignment>::Clear() {
Ian Rogersc5f17732014-06-05 20:48:42 -0700101 if (bitmap_begin_ != nullptr) {
102 mem_map_->MadviseDontNeedAndZero();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700103 }
104}
105
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700106template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700107void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700108 DCHECK_EQ(Size(), source_bitmap->Size());
Mathieu Chartierc381c362016-08-23 13:27:53 -0700109 const size_t count = source_bitmap->Size() / sizeof(intptr_t);
110 Atomic<uintptr_t>* const src = source_bitmap->Begin();
111 Atomic<uintptr_t>* const dest = Begin();
112 for (size_t i = 0; i < count; ++i) {
113 dest[i].StoreRelaxed(src[i].LoadRelaxed());
114 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700115}
116
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700117template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700118void SpaceBitmap<kAlignment>::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 CHECK(bitmap_begin_ != nullptr);
120 CHECK(callback != nullptr);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121
122 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700123 Atomic<uintptr_t>* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700124 for (uintptr_t i = 0; i <= end; ++i) {
Mathieu Chartierc381c362016-08-23 13:27:53 -0700125 uintptr_t w = bitmap_begin[i].LoadRelaxed();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700126 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700127 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700128 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700129 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700131 (*callback)(obj, arg);
Ian Rogers13735952014-10-08 12:43:28 -0700132 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700133 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700134 }
135 }
136}
137
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700138template<size_t kAlignment>
139void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700140 const SpaceBitmap<kAlignment>& mark_bitmap,
141 uintptr_t sweep_begin, uintptr_t sweep_end,
142 SpaceBitmap::SweepCallback* callback, void* arg) {
143 CHECK(live_bitmap.bitmap_begin_ != nullptr);
144 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700145 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
146 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700147 CHECK(callback != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700148 CHECK_LE(sweep_begin, sweep_end);
149 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700150
151 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700152 return;
153 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700154
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Ian Rogers13735952014-10-08 12:43:28 -0700156 constexpr size_t buffer_size = sizeof(intptr_t) * kBitsPerIntPtrT;
Andreas Gampe262a0a32014-06-04 15:25:28 -0700157#ifdef __LP64__
158 // Heap-allocate for smaller stack frame.
159 std::unique_ptr<mirror::Object*[]> pointer_buf_ptr(new mirror::Object*[buffer_size]);
160 mirror::Object** pointer_buf = pointer_buf_ptr.get();
161#else
162 // Stack-allocate buffer as it's small enough.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 mirror::Object* pointer_buf[buffer_size];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700164#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 mirror::Object** pb = &pointer_buf[0];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700166
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700167 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700168 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700169 CHECK_LT(end, live_bitmap.Size() / sizeof(intptr_t));
Mathieu Chartierc381c362016-08-23 13:27:53 -0700170 Atomic<uintptr_t>* live = live_bitmap.bitmap_begin_;
171 Atomic<uintptr_t>* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700172 for (size_t i = start; i <= end; i++) {
Mathieu Chartierc381c362016-08-23 13:27:53 -0700173 uintptr_t garbage = live[i].LoadRelaxed() & ~mark[i].LoadRelaxed();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700174 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700175 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700176 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700177 const size_t shift = CTZ(garbage);
Ian Rogers13735952014-10-08 12:43:28 -0700178 garbage ^= (static_cast<uintptr_t>(1)) << shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700180 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 // Make sure that there are always enough slots available for an
182 // entire word of one bits.
Ian Rogers13735952014-10-08 12:43:28 -0700183 if (pb >= &pointer_buf[buffer_size - kBitsPerIntPtrT]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700184 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
185 pb = &pointer_buf[0];
186 }
187 }
188 }
189 if (pb > &pointer_buf[0]) {
190 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
191 }
192}
193
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700194template<size_t kAlignment>
195void SpaceBitmap<kAlignment>::WalkInstanceFields(SpaceBitmap<kAlignment>* visited,
196 ObjectCallback* callback, mirror::Object* obj,
197 mirror::Class* klass, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700199 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700201 if (super != nullptr) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700202 WalkInstanceFields(visited, callback, obj, super, arg);
203 }
204 // Walk instance fields
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700205 for (ArtField& field : klass->GetIFields()) {
206 if (!field.IsPrimitiveType()) {
207 mirror::Object* value = field.GetObj(obj);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700208 if (value != nullptr) {
209 WalkFieldsInOrder(visited, callback, value, arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700210 }
211 }
212 }
213}
214
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700215template<size_t kAlignment>
216void SpaceBitmap<kAlignment>::WalkFieldsInOrder(SpaceBitmap<kAlignment>* visited,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700217 ObjectCallback* callback, mirror::Object* obj,
218 void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700219 if (visited->Test(obj)) {
220 return;
221 }
222 // visit the object itself
223 (*callback)(obj, arg);
224 visited->Set(obj);
225 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700227 WalkInstanceFields(visited, callback, obj, klass, arg);
228 // Walk static fields of a Class
229 if (obj->IsClass()) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700230 for (ArtField& field : klass->GetSFields()) {
231 if (!field.IsPrimitiveType()) {
232 mirror::Object* value = field.GetObj(nullptr);
Mathieu Chartierc7853442015-03-27 14:35:38 -0700233 if (value != nullptr) {
234 WalkFieldsInOrder(visited, callback, value, arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700235 }
236 }
237 }
238 } else if (obj->IsObjectArray()) {
239 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700241 int32_t length = obj_array->GetLength();
242 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 mirror::Object* value = obj_array->Get(i);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700244 if (value != nullptr) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700245 WalkFieldsInOrder(visited, callback, value, arg);
246 }
247 }
248 }
249}
250
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700251template<size_t kAlignment>
252void SpaceBitmap<kAlignment>::InOrderWalk(ObjectCallback* callback, void* arg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700253 std::unique_ptr<SpaceBitmap<kAlignment>> visited(
Ian Rogers13735952014-10-08 12:43:28 -0700254 Create("bitmap for in-order walk", reinterpret_cast<uint8_t*>(heap_begin_),
255 IndexToOffset(bitmap_size_ / sizeof(intptr_t))));
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700256 CHECK(bitmap_begin_ != nullptr);
257 CHECK(callback != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700258 uintptr_t end = Size() / sizeof(intptr_t);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700259 for (uintptr_t i = 0; i < end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700260 // Need uint for unsigned shift.
Mathieu Chartierc381c362016-08-23 13:27:53 -0700261 uintptr_t w = bitmap_begin_[i].LoadRelaxed();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700262 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700263 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
264 while (w != 0) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700265 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700267 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Ian Rogers13735952014-10-08 12:43:28 -0700268 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700269 }
270 }
271 }
272}
273
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700274template class SpaceBitmap<kObjectAlignment>;
275template class SpaceBitmap<kPageSize>;
276
Ian Rogers1d54e732013-05-02 21:10:01 -0700277} // namespace accounting
278} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700279} // namespace art