blob: 3c5688d5bd02d78edb9245553012cc81b9c0f452 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
David Sehr79e26072018-04-06 17:58:50 -070022#include "base/mem_map.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "mirror/object-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070026#include "mirror/object_array.h"
27
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070028namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
30namespace accounting {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070031
Andreas Gampe46ee31b2016-12-14 10:11:49 -080032using android::base::StringPrintf;
33
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070034template<size_t kAlignment>
Mathieu Chartier73d1e172014-04-11 17:53:48 -070035size_t SpaceBitmap<kAlignment>::ComputeBitmapSize(uint64_t capacity) {
Roland Levillain8f7ea9a2018-01-26 17:27:59 +000036 // Number of space (heap) bytes covered by one bitmap word.
37 // (Word size in bytes = `sizeof(intptr_t)`, which is expected to be
38 // 4 on a 32-bit architecture and 8 on a 64-bit one.)
Ian Rogers13735952014-10-08 12:43:28 -070039 const uint64_t kBytesCoveredPerWord = kAlignment * kBitsPerIntPtrT;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +000040 // Calculate the number of words required to cover a space (heap)
41 // having a size of `capacity` bytes.
Ian Rogers13735952014-10-08 12:43:28 -070042 return (RoundUp(capacity, kBytesCoveredPerWord) / kBytesCoveredPerWord) * sizeof(intptr_t);
Mathieu Chartier73d1e172014-04-11 17:53:48 -070043}
44
45template<size_t kAlignment>
Mathieu Chartierd39645e2015-06-09 17:50:29 -070046size_t SpaceBitmap<kAlignment>::ComputeHeapSize(uint64_t bitmap_bytes) {
47 return bitmap_bytes * kBitsPerByte * kAlignment;
48}
49
50template<size_t kAlignment>
Mathieu Chartier6f382012019-07-30 09:47:35 -070051SpaceBitmap<kAlignment> SpaceBitmap<kAlignment>::CreateFromMemMap(
Vladimir Markoc34bebf2018-08-16 16:12:49 +010052 const std::string& name, MemMap&& mem_map, uint8_t* heap_begin, size_t heap_capacity) {
53 CHECK(mem_map.IsValid());
54 uintptr_t* bitmap_begin = reinterpret_cast<uintptr_t*>(mem_map.Begin());
Mathieu Chartier73d1e172014-04-11 17:53:48 -070055 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Mathieu Chartier6f382012019-07-30 09:47:35 -070056 return { name, std::move(mem_map), bitmap_begin, bitmap_size, heap_begin, heap_capacity };
Mathieu Chartier31e89252013-08-28 11:29:12 -070057}
58
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070059template<size_t kAlignment>
Mathieu Chartier2a1513b2017-08-07 20:01:46 -070060SpaceBitmap<kAlignment>::SpaceBitmap(const std::string& name,
Vladimir Markoc34bebf2018-08-16 16:12:49 +010061 MemMap&& mem_map,
Mathieu Chartier2a1513b2017-08-07 20:01:46 -070062 uintptr_t* bitmap_begin,
63 size_t bitmap_size,
64 const void* heap_begin,
65 size_t heap_capacity)
Vladimir Markoc34bebf2018-08-16 16:12:49 +010066 : mem_map_(std::move(mem_map)),
Mathieu Chartierc381c362016-08-23 13:27:53 -070067 bitmap_begin_(reinterpret_cast<Atomic<uintptr_t>*>(bitmap_begin)),
68 bitmap_size_(bitmap_size),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070069 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartier2a1513b2017-08-07 20:01:46 -070070 heap_limit_(reinterpret_cast<uintptr_t>(heap_begin) + heap_capacity),
Mathieu Chartierbbd695c2014-04-16 09:48:48 -070071 name_(name) {
72 CHECK(bitmap_begin_ != nullptr);
73 CHECK_NE(bitmap_size, 0U);
74}
75
76template<size_t kAlignment>
Ian Rogers22d5e732014-07-15 22:23:51 -070077SpaceBitmap<kAlignment>::~SpaceBitmap() {}
78
79template<size_t kAlignment>
Mathieu Chartier6f382012019-07-30 09:47:35 -070080SpaceBitmap<kAlignment> SpaceBitmap<kAlignment>::Create(
Ian Rogers13735952014-10-08 12:43:28 -070081 const std::string& name, uint8_t* heap_begin, size_t heap_capacity) {
Roland Levillain8f7ea9a2018-01-26 17:27:59 +000082 // Round up since `heap_capacity` is not necessarily a multiple of `kAlignment * kBitsPerIntPtrT`
83 // (we represent one word as an `intptr_t`).
Mathieu Chartier73d1e172014-04-11 17:53:48 -070084 const size_t bitmap_size = ComputeBitmapSize(heap_capacity);
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +010086 MemMap mem_map = MemMap::MapAnonymous(name.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +010087 bitmap_size,
88 PROT_READ | PROT_WRITE,
Vladimir Marko11306592018-10-26 14:22:59 +010089 /*low_4gb=*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +010090 &error_msg);
91 if (UNLIKELY(!mem_map.IsValid())) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 LOG(ERROR) << "Failed to allocate bitmap " << name << ": " << error_msg;
Mathieu Chartier6f382012019-07-30 09:47:35 -070093 return SpaceBitmap<kAlignment>();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070094 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +010095 return CreateFromMemMap(name, std::move(mem_map), heap_begin, heap_capacity);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070096}
97
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070098template<size_t kAlignment>
99void SpaceBitmap<kAlignment>::SetHeapLimit(uintptr_t new_end) {
Roland Levillain14d90572015-07-16 10:52:26 +0100100 DCHECK_ALIGNED(new_end, kBitsPerIntPtrT * kAlignment);
Ian Rogers13735952014-10-08 12:43:28 -0700101 size_t new_size = OffsetToIndex(new_end - heap_begin_) * sizeof(intptr_t);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700102 if (new_size < bitmap_size_) {
103 bitmap_size_ = new_size;
104 }
Mathieu Chartier2a1513b2017-08-07 20:01:46 -0700105 heap_limit_ = new_end;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700106 // Not sure if doing this trim is necessary, since nothing past the end of the heap capacity
107 // should be marked.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700108}
109
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700110template<size_t kAlignment>
Ian Rogers576ca0c2014-06-06 15:58:22 -0700111std::string SpaceBitmap<kAlignment>::Dump() const {
112 return StringPrintf("%s: %p-%p", name_.c_str(), reinterpret_cast<void*>(HeapBegin()),
113 reinterpret_cast<void*>(HeapLimit()));
114}
115
116template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700117void SpaceBitmap<kAlignment>::Clear() {
Ian Rogersc5f17732014-06-05 20:48:42 -0700118 if (bitmap_begin_ != nullptr) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100119 mem_map_.MadviseDontNeedAndZero();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700120 }
121}
122
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700123template<size_t kAlignment>
Mathieu Chartier7ec38dc2016-10-07 15:24:46 -0700124void SpaceBitmap<kAlignment>::ClearRange(const mirror::Object* begin, const mirror::Object* end) {
125 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(begin) - heap_begin_;
126 uintptr_t end_offset = reinterpret_cast<uintptr_t>(end) - heap_begin_;
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000127 // Align begin and end to bitmap word boundaries.
Mathieu Chartier7ec38dc2016-10-07 15:24:46 -0700128 while (begin_offset < end_offset && OffsetBitIndex(begin_offset) != 0) {
129 Clear(reinterpret_cast<mirror::Object*>(heap_begin_ + begin_offset));
130 begin_offset += kAlignment;
131 }
132 while (begin_offset < end_offset && OffsetBitIndex(end_offset) != 0) {
133 end_offset -= kAlignment;
134 Clear(reinterpret_cast<mirror::Object*>(heap_begin_ + end_offset));
135 }
Roland Levillain8f7ea9a2018-01-26 17:27:59 +0000136 // Bitmap word boundaries.
Mathieu Chartier7ec38dc2016-10-07 15:24:46 -0700137 const uintptr_t start_index = OffsetToIndex(begin_offset);
138 const uintptr_t end_index = OffsetToIndex(end_offset);
Mathieu Chartier6e6078a2016-10-24 15:45:41 -0700139 ZeroAndReleasePages(reinterpret_cast<uint8_t*>(&bitmap_begin_[start_index]),
140 (end_index - start_index) * sizeof(*bitmap_begin_));
Mathieu Chartier7ec38dc2016-10-07 15:24:46 -0700141}
142
143template<size_t kAlignment>
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700144void SpaceBitmap<kAlignment>::CopyFrom(SpaceBitmap* source_bitmap) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700145 DCHECK_EQ(Size(), source_bitmap->Size());
Mathieu Chartierc381c362016-08-23 13:27:53 -0700146 const size_t count = source_bitmap->Size() / sizeof(intptr_t);
147 Atomic<uintptr_t>* const src = source_bitmap->Begin();
148 Atomic<uintptr_t>* const dest = Begin();
149 for (size_t i = 0; i < count; ++i) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000150 dest[i].store(src[i].load(std::memory_order_relaxed), std::memory_order_relaxed);
Mathieu Chartierc381c362016-08-23 13:27:53 -0700151 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700152}
153
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700154template<size_t kAlignment>
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700155void SpaceBitmap<kAlignment>::SweepWalk(const SpaceBitmap<kAlignment>& live_bitmap,
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700156 const SpaceBitmap<kAlignment>& mark_bitmap,
157 uintptr_t sweep_begin, uintptr_t sweep_end,
158 SpaceBitmap::SweepCallback* callback, void* arg) {
159 CHECK(live_bitmap.bitmap_begin_ != nullptr);
160 CHECK(mark_bitmap.bitmap_begin_ != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700161 CHECK_EQ(live_bitmap.heap_begin_, mark_bitmap.heap_begin_);
162 CHECK_EQ(live_bitmap.bitmap_size_, mark_bitmap.bitmap_size_);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700163 CHECK(callback != nullptr);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700164 CHECK_LE(sweep_begin, sweep_end);
165 CHECK_GE(sweep_begin, live_bitmap.heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700166
167 if (sweep_end <= sweep_begin) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700168 return;
169 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700170
Mathieu Chartier164f1332019-05-02 16:19:46 -0700171 size_t buffer_size = sizeof(intptr_t) * kBitsPerIntPtrT;
Mathieu Chartierc381c362016-08-23 13:27:53 -0700172 Atomic<uintptr_t>* live = live_bitmap.bitmap_begin_;
173 Atomic<uintptr_t>* mark = mark_bitmap.bitmap_begin_;
Mathieu Chartier164f1332019-05-02 16:19:46 -0700174 const size_t start = OffsetToIndex(sweep_begin - live_bitmap.heap_begin_);
175 const size_t end = OffsetToIndex(sweep_end - live_bitmap.heap_begin_ - 1);
176 CHECK_LT(end, live_bitmap.Size() / sizeof(intptr_t));
177
178 if (Runtime::Current()->IsRunningOnMemoryTool()) {
179 // For memory tool, make the buffer large enough to hold all allocations. This is done since
180 // we get the size of objects (and hence read the class) inside of the freeing logic. This can
181 // cause crashes for unloaded classes since the class may get zeroed out before it is read.
182 // See b/131542326
183 for (size_t i = start; i <= end; i++) {
184 uintptr_t garbage =
185 live[i].load(std::memory_order_relaxed) & ~mark[i].load(std::memory_order_relaxed);
186 buffer_size += POPCOUNT(garbage);
187 }
188 }
189 std::vector<mirror::Object*> pointer_buf(buffer_size);
190 mirror::Object** cur_pointer = &pointer_buf[0];
191 mirror::Object** pointer_end = cur_pointer + (buffer_size - kBitsPerIntPtrT);
192
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700193 for (size_t i = start; i <= end; i++) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000194 uintptr_t garbage =
195 live[i].load(std::memory_order_relaxed) & ~mark[i].load(std::memory_order_relaxed);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700196 if (UNLIKELY(garbage != 0)) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700197 uintptr_t ptr_base = IndexToOffset(i) + live_bitmap.heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700198 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700199 const size_t shift = CTZ(garbage);
Ian Rogers13735952014-10-08 12:43:28 -0700200 garbage ^= (static_cast<uintptr_t>(1)) << shift;
Mathieu Chartier164f1332019-05-02 16:19:46 -0700201 *cur_pointer++ = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700202 } while (garbage != 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700203 // Make sure that there are always enough slots available for an
204 // entire word of one bits.
Mathieu Chartier164f1332019-05-02 16:19:46 -0700205 if (cur_pointer >= pointer_end) {
206 (*callback)(cur_pointer - &pointer_buf[0], &pointer_buf[0], arg);
207 cur_pointer = &pointer_buf[0];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700208 }
209 }
210 }
Mathieu Chartier164f1332019-05-02 16:19:46 -0700211 if (cur_pointer > &pointer_buf[0]) {
212 (*callback)(cur_pointer - &pointer_buf[0], &pointer_buf[0], arg);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700213 }
214}
215
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700216template class SpaceBitmap<kObjectAlignment>;
217template class SpaceBitmap<kPageSize>;
218
Ian Rogers1d54e732013-05-02 21:10:01 -0700219} // namespace accounting
220} // namespace gc
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700221} // namespace art