blob: 7914b6676968548e07df5d2d58501025bf8798d1 [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)
54 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
55 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
56 name_(name) {
57 CHECK(bitmap_begin_ != nullptr);
58 CHECK_NE(bitmap_size, 0U);
59}
60
61template<size_t kAlignment>
Ian Rogers22d5e732014-07-15 22:23:51 -070062SpaceBitmap<kAlignment>::~SpaceBitmap() {}
63
64template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070065SpaceBitmap<kAlignment>* SpaceBitmap<kAlignment>::Create(
Ian Rogers13735952014-10-08 12:43:28 -070066 const std::string& name, uint8_t* heap_begin, size_t heap_capacity) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070067 // Round up since heap_capacity is not necessarily a multiple of kAlignment * kBitsPerWord.
Mathieu Chartier73d1e172014-04-11 17:53:48 -070068 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070069 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -070070 std::unique_ptr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), nullptr, bitmap_size,
Vladimir Marko5c42c292015-02-25 12:02:49 +000071 PROT_READ | PROT_WRITE, false, false,
72 &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070073 if (UNLIKELY(mem_map.get() == nullptr)) {
74 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070075 return nullptr;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070076 }
Mathieu Chartier31e89252013-08-28 11:29:12 -070077 return CreateFromMemMap(name, mem_map.release(), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070078}
79
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070080template<size_t kAlignment>
81void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Roland Levillain14d90572015-07-16 10:52:26 +010082 DCHECK_ALIGNED(new_end, kBitsPerIntPtrT * kAlignment);
Ian Rogers13735952014-10-08 12:43:28 -070083 size_t new_size = OffsetToIndex(new_end - heap_begin_) * sizeof(intptr_t);
Mathieu Chartiercc236d72012-07-20 10:29:05 -070084 if (new_size < bitmap_size_) {
85 bitmap_size_ = new_size;
86 }
87 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
88 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -070089}
90
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070091template<size_t kAlignment>
Ian Rogers576ca0c2014-06-06 15:58:22 -070092std::string SpaceBitmap<kAlignment>::Dump() const {
93 return StringPrintf("%s: %p-%p", name_.c_str(), reinterpret_cast<void*>(HeapBegin()),
94 reinterpret_cast<void*>(HeapLimit()));
95}
96
97template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070098void SpaceBitmap<kAlignment>::Clear() {
Ian Rogersc5f17732014-06-05 20:48:42 -070099 if (bitmap_begin_ != nullptr) {
100 mem_map_->MadviseDontNeedAndZero();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700101 }
102}
103
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700104template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700105void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700106 DCHECK_EQ(Size(), source_bitmap->Size());
Ian Rogers13735952014-10-08 12:43:28 -0700107 std::copy(source_bitmap->Begin(), source_bitmap->Begin() + source_bitmap->Size() / sizeof(intptr_t), Begin());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700108}
109
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700110template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700111void SpaceBitmap<kAlignment>::Walk(ObjectCallback* callback, void* arg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 CHECK(bitmap_begin_ != nullptr);
113 CHECK(callback != nullptr);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700114
115 uintptr_t end = OffsetToIndex(HeapLimit() - heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700116 uintptr_t* bitmap_begin = bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700117 for (uintptr_t i = 0; i <= end; ++i) {
Ian Rogers13735952014-10-08 12:43:28 -0700118 uintptr_t w = bitmap_begin[i];
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700119 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700120 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700121 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700122 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700124 (*callback)(obj, arg);
Ian Rogers13735952014-10-08 12:43:28 -0700125 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700126 } while (w != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700127 }
128 }
129}
130
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700131template<size_t kAlignment>
132void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700133 const SpaceBitmap<kAlignment>& mark_bitmap,
134 uintptr_t sweep_begin, uintptr_t sweep_end,
135 SpaceBitmap::SweepCallback* callback, void* arg) {
136 CHECK(live_bitmap.bitmap_begin_ != nullptr);
137 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
139 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 CHECK(callback != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700141 CHECK_LE(sweep_begin, sweep_end);
142 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700143
144 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700145 return;
146 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700147
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 // TODO: rewrite the callbacks to accept a std::vector<mirror::Object*> rather than a mirror::Object**?
Ian Rogers13735952014-10-08 12:43:28 -0700149 constexpr size_t buffer_size = sizeof(intptr_t) * kBitsPerIntPtrT;
Andreas Gampe262a0a32014-06-04 15:25:28 -0700150#ifdef __LP64__
151 // Heap-allocate for smaller stack frame.
152 std::unique_ptr<mirror::Object*[]> pointer_buf_ptr(new mirror::Object*[buffer_size]);
153 mirror::Object** pointer_buf = pointer_buf_ptr.get();
154#else
155 // Stack-allocate buffer as it's small enough.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 mirror::Object* pointer_buf[buffer_size];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700157#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 mirror::Object** pb = &pointer_buf[0];
Andreas Gampe262a0a32014-06-04 15:25:28 -0700159
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700160 size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700161 size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
Ian Rogers13735952014-10-08 12:43:28 -0700162 CHECK_LT(end, live_bitmap.Size() / sizeof(intptr_t));
163 uintptr_t* live = live_bitmap.bitmap_begin_;
164 uintptr_t* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700165 for (size_t i = start; i <= end; i++) {
Ian Rogers13735952014-10-08 12:43:28 -0700166 uintptr_t garbage = live[i] & ~mark[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700167 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700168 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700169 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700170 const size_t shift = CTZ(garbage);
Ian Rogers13735952014-10-08 12:43:28 -0700171 garbage ^= (static_cast<uintptr_t>(1)) << shift;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 *pb++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700173 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700174 // Make sure that there are always enough slots available for an
175 // entire word of one bits.
Ian Rogers13735952014-10-08 12:43:28 -0700176 if (pb >= &pointer_buf[buffer_size - kBitsPerIntPtrT]) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700177 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
178 pb = &pointer_buf[0];
179 }
180 }
181 }
182 if (pb > &pointer_buf[0]) {
183 (*callback)(pb - &pointer_buf[0], &pointer_buf[0], arg);
184 }
185}
186
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700187template<size_t kAlignment>
188void SpaceBitmap<kAlignment>::WalkInstanceFields(SpaceBitmap<kAlignment>* visited,
189 ObjectCallback* callback, mirror::Object* obj,
190 mirror::Class* klass, void* arg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700191 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700192 // Visit fields of parent classes first.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 mirror::Class* super = klass->GetSuperClass();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700194 if (super != nullptr) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700195 WalkInstanceFields(visited, callback, obj, super, arg);
196 }
197 // Walk instance fields
Mathieu Chartierc7853442015-03-27 14:35:38 -0700198 auto* fields = klass->GetIFields();
199 for (size_t i = 0, count = klass->NumInstanceFields(); i < count; ++i) {
200 ArtField* field = &fields[i];
201 if (!field->IsPrimitiveType()) {
202 mirror::Object* value = field->GetObj(obj);
203 if (value != nullptr) {
204 WalkFieldsInOrder(visited, callback, value, arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700205 }
206 }
207 }
208}
209
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700210template<size_t kAlignment>
211void SpaceBitmap<kAlignment>::WalkFieldsInOrder(SpaceBitmap<kAlignment>* visited,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700212 ObjectCallback* callback, mirror::Object* obj,
213 void* arg) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700214 if (visited->Test(obj)) {
215 return;
216 }
217 // visit the object itself
218 (*callback)(obj, arg);
219 visited->Set(obj);
220 // Walk instance fields of all objects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 mirror::Class* klass = obj->GetClass();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700222 WalkInstanceFields(visited, callback, obj, klass, arg);
223 // Walk static fields of a Class
224 if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700225 auto* sfields = klass->GetSFields();
226 for (size_t i = 0, count = klass->NumStaticFields(); i < count; ++i) {
227 ArtField* field = &sfields[i];
228 if (!field->IsPrimitiveType()) {
229 mirror::Object* value = field->GetObj(nullptr);
230 if (value != nullptr) {
231 WalkFieldsInOrder(visited, callback, value, arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700232 }
233 }
234 }
235 } else if (obj->IsObjectArray()) {
236 // Walk elements of an object array
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700238 int32_t length = obj_array->GetLength();
239 for (int32_t i = 0; i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 mirror::Object* value = obj_array->Get(i);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700241 if (value != nullptr) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700242 WalkFieldsInOrder(visited, callback, value, arg);
243 }
244 }
245 }
246}
247
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700248template<size_t kAlignment>
249void SpaceBitmap<kAlignment>::InOrderWalk(ObjectCallback* callback, void* arg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700250 std::unique_ptr<SpaceBitmap<kAlignment>> visited(
Ian Rogers13735952014-10-08 12:43:28 -0700251 Create("bitmap for in-order walk", reinterpret_cast<uint8_t*>(heap_begin_),
252 IndexToOffset(bitmap_size_ / sizeof(intptr_t))));
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700253 CHECK(bitmap_begin_ != nullptr);
254 CHECK(callback != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700255 uintptr_t end = Size() / sizeof(intptr_t);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700256 for (uintptr_t i = 0; i < end; ++i) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700257 // Need uint for unsigned shift.
Ian Rogers13735952014-10-08 12:43:28 -0700258 uintptr_t w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700259 if (UNLIKELY(w != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700260 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
261 while (w != 0) {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700262 const size_t shift = CTZ(w);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700264 WalkFieldsInOrder(visited.get(), callback, obj, arg);
Ian Rogers13735952014-10-08 12:43:28 -0700265 w ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700266 }
267 }
268 }
269}
270
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700271template class SpaceBitmap<kObjectAlignment>;
272template class SpaceBitmap<kPageSize>;
273
Ian Rogers1d54e732013-05-02 21:10:01 -0700274} // namespace accounting
275} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700276} // namespace art