blob: 281d9c234263dcead256b8b6b38f7d9eeff0eb67 [file] [log] [blame]
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001/*
2 * Copyright (C) 2013 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
17#include "malloc_space.h"
18
Andreas Gampe8764dc32019-01-07 15:20:12 -080019#include <ostream>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Andreas Gampe170331f2017-12-07 18:41:03 -080023#include "base/logging.h" // For VLOG
Andreas Gampe88dbad32018-06-26 19:54:12 -070024#include "base/mutex-inl.h"
David Sehrc431b9d2018-03-02 12:01:51 -080025#include "base/utils.h"
Mathieu Chartierec050072014-01-07 16:00:07 -080026#include "gc/accounting/card_table-inl.h"
27#include "gc/accounting/space_bitmap-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070028#include "gc/heap.h"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080029#include "gc/space/space-inl.h"
30#include "gc/space/zygote_space.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "handle_scope-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070032#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
34#include "runtime.h"
35#include "thread.h"
36#include "thread_list.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070037
38namespace art {
39namespace gc {
40namespace space {
41
Andreas Gampe46ee31b2016-12-14 10:11:49 -080042using android::base::StringPrintf;
43
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070044size_t MallocSpace::bitmap_index_ = 0;
45
Vladimir Markoc34bebf2018-08-16 16:12:49 +010046MallocSpace::MallocSpace(const std::string& name,
47 MemMap&& mem_map,
48 uint8_t* begin,
49 uint8_t* end,
50 uint8_t* limit,
51 size_t growth_limit,
52 bool create_bitmaps,
53 bool can_move_objects,
54 size_t starting_size,
Mathieu Chartier31f44142014-04-08 14:40:03 -070055 size_t initial_size)
Vladimir Markoc34bebf2018-08-16 16:12:49 +010056 : ContinuousMemMapAllocSpace(
57 name, std::move(mem_map), begin, end, limit, kGcRetentionPolicyAlwaysCollect),
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070058 recent_free_pos_(0), lock_("allocation space lock", kAllocSpaceLock),
Mathieu Chartier31f44142014-04-08 14:40:03 -070059 growth_limit_(growth_limit), can_move_objects_(can_move_objects),
60 starting_size_(starting_size), initial_size_(initial_size) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080061 if (create_bitmaps) {
62 size_t bitmap_index = bitmap_index_++;
63 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
Vladimir Markoc34bebf2018-08-16 16:12:49 +010064 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(mem_map_.Begin()), kGcCardSize);
65 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(mem_map_.End()), kGcCardSize);
Mathieu Chartier6f382012019-07-30 09:47:35 -070066 live_bitmap_ = accounting::ContinuousSpaceBitmap::Create(
Mathieu Chartiera1602f22014-01-13 17:19:19 -080067 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartier6f382012019-07-30 09:47:35 -070068 Begin(), NonGrowthLimitCapacity());
69 CHECK(live_bitmap_.IsValid()) << "could not create allocspace live bitmap #"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080070 << bitmap_index;
Mathieu Chartier6f382012019-07-30 09:47:35 -070071 mark_bitmap_ = accounting::ContinuousSpaceBitmap::Create(
Mathieu Chartiera1602f22014-01-13 17:19:19 -080072 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartier6f382012019-07-30 09:47:35 -070073 Begin(), NonGrowthLimitCapacity());
74 CHECK(mark_bitmap_.IsValid()) << "could not create allocspace mark bitmap #" << bitmap_index;
Mathieu Chartiera1602f22014-01-13 17:19:19 -080075 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070076 for (auto& freed : recent_freed_objects_) {
77 freed.first = nullptr;
78 freed.second = nullptr;
79 }
80}
81
Vladimir Markoc34bebf2018-08-16 16:12:49 +010082MemMap MallocSpace::CreateMemMap(const std::string& name,
83 size_t starting_size,
84 size_t* initial_size,
85 size_t* growth_limit,
Vladimir Marko11306592018-10-26 14:22:59 +010086 size_t* capacity) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070087 // Sanity check arguments
88 if (starting_size > *initial_size) {
89 *initial_size = starting_size;
90 }
91 if (*initial_size > *growth_limit) {
92 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
93 << PrettySize(*initial_size) << ") is larger than its capacity ("
94 << PrettySize(*growth_limit) << ")";
Vladimir Markoc34bebf2018-08-16 16:12:49 +010095 return MemMap::Invalid();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070096 }
97 if (*growth_limit > *capacity) {
98 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
99 << PrettySize(*growth_limit) << ") is larger than the capacity ("
100 << PrettySize(*capacity) << ")";
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100101 return MemMap::Invalid();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700102 }
103
104 // Page align growth limit and capacity which will be used to manage mmapped storage
105 *growth_limit = RoundUp(*growth_limit, kPageSize);
106 *capacity = RoundUp(*capacity, kPageSize);
107
108 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100109 MemMap mem_map = MemMap::MapAnonymous(name.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100110 *capacity,
111 PROT_READ | PROT_WRITE,
Vladimir Marko11306592018-10-26 14:22:59 +0100112 /*low_4gb=*/ true,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100113 &error_msg);
114 if (!mem_map.IsValid()) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700115 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
116 << PrettySize(*capacity) << ": " << error_msg;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700117 }
118 return mem_map;
119}
120
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700121mirror::Class* MallocSpace::FindRecentFreedObject(const mirror::Object* obj) {
122 size_t pos = recent_free_pos_;
123 // Start at the most recently freed object and work our way back since there may be duplicates
124 // caused by dlmalloc reusing memory.
125 if (kRecentFreeCount > 0) {
126 for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) {
127 pos = pos != 0 ? pos - 1 : kRecentFreeMask;
128 if (recent_freed_objects_[pos].first == obj) {
129 return recent_freed_objects_[pos].second;
130 }
131 }
132 }
133 return nullptr;
134}
135
136void MallocSpace::RegisterRecentFree(mirror::Object* ptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800137 // No verification since the object is dead.
138 recent_freed_objects_[recent_free_pos_] = std::make_pair(ptr, ptr->GetClass<kVerifyNone>());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700139 recent_free_pos_ = (recent_free_pos_ + 1) & kRecentFreeMask;
140}
141
142void MallocSpace::SetGrowthLimit(size_t growth_limit) {
143 growth_limit = RoundUp(growth_limit, kPageSize);
144 growth_limit_ = growth_limit;
145 if (Size() > growth_limit_) {
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700146 SetEnd(begin_ + growth_limit);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700147 }
148}
149
150void* MallocSpace::MoreCore(intptr_t increment) {
151 CheckMoreCoreForPrecondition();
Ian Rogers13735952014-10-08 12:43:28 -0700152 uint8_t* original_end = End();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700153 if (increment != 0) {
154 VLOG(heap) << "MallocSpace::MoreCore " << PrettySize(increment);
Ian Rogers13735952014-10-08 12:43:28 -0700155 uint8_t* new_end = original_end + increment;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700156 if (increment > 0) {
157 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
158 // by mspace_set_footprint_limit.
159 CHECK_LE(new_end, Begin() + Capacity());
Mathieu Chartier3425d022017-10-03 16:22:05 -0700160 CheckedCall(mprotect, GetName(), original_end, increment, PROT_READ | PROT_WRITE);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700161 } else {
162 // Should never be asked for negative footprint (ie before begin). Zero footprint is ok.
163 CHECK_GE(original_end + increment, Begin());
164 // Advise we don't need the pages and protect them
165 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
166 // expensive (note the same isn't true for giving permissions to a page as the protected
167 // page shouldn't be in a TLB). We should investigate performance impact of just
168 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
169 // likely just a useful debug feature.
170 size_t size = -increment;
Mathieu Chartier3425d022017-10-03 16:22:05 -0700171 CheckedCall(madvise, GetName(), new_end, size, MADV_DONTNEED);
172 CheckedCall(mprotect, GetName(), new_end, size, PROT_NONE);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700173 }
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700174 // Update end_.
175 SetEnd(new_end);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700176 }
177 return original_end;
178}
179
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800180ZygoteSpace* MallocSpace::CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode,
181 MallocSpace** out_malloc_space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700182 // For RosAlloc, revoke thread local runs before creating a new
183 // alloc space so that we won't mix thread local runs from different
184 // alloc spaces.
185 RevokeAllThreadLocalBuffers();
Ian Rogers13735952014-10-08 12:43:28 -0700186 SetEnd(reinterpret_cast<uint8_t*>(RoundUp(reinterpret_cast<uintptr_t>(End()), kPageSize)));
Roland Levillain14d90572015-07-16 10:52:26 +0100187 DCHECK_ALIGNED(begin_, accounting::CardTable::kCardSize);
188 DCHECK_ALIGNED(End(), accounting::CardTable::kCardSize);
189 DCHECK_ALIGNED(begin_, kPageSize);
190 DCHECK_ALIGNED(End(), kPageSize);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700191 size_t size = RoundUp(Size(), kPageSize);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800192 // Trimming the heap should be done by the caller since we may have invalidated the accounting
193 // stored in between objects.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700194 // Remaining size is for the new alloc space.
195 const size_t growth_limit = growth_limit_ - size;
Lin Zangd0e0d4c2014-12-12 21:54:47 +0800196 // Use mem map limit in case error for clear growth limit.
197 const size_t capacity = NonGrowthLimitCapacity() - size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700198 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n"
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700199 << "End " << reinterpret_cast<const void*>(End()) << "\n"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700200 << "Size " << size << "\n"
201 << "GrowthLimit " << growth_limit_ << "\n"
202 << "Capacity " << Capacity();
203 SetGrowthLimit(RoundUp(size, kPageSize));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700204 // FIXME: Do we need reference counted pointers here?
205 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
206 VLOG(heap) << "Creating new AllocSpace: ";
207 VLOG(heap) << "Size " << GetMemMap()->Size();
208 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
209 VLOG(heap) << "Capacity " << PrettySize(capacity);
Orion Hodsondbd05fe2017-08-10 11:41:35 +0100210 // Remap the tail.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700211 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100212 MemMap mem_map = GetMemMap()->RemapAtEnd(
213 End(), alloc_space_name, PROT_READ | PROT_WRITE, &error_msg);
214 CHECK(mem_map.IsValid()) << error_msg;
215 void* allocator =
216 CreateAllocator(End(), starting_size_, initial_size_, capacity, low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700217 // Protect memory beyond the initial size.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100218 uint8_t* end = mem_map.Begin() + starting_size_;
Mathieu Chartierc4d095b2014-04-15 12:01:58 -0700219 if (capacity > initial_size_) {
Mathieu Chartier3425d022017-10-03 16:22:05 -0700220 CheckedCall(mprotect, alloc_space_name, end, capacity - initial_size_, PROT_NONE);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700221 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100222 *out_malloc_space = CreateInstance(std::move(mem_map),
223 alloc_space_name,
224 allocator,
225 End(),
226 end,
227 limit_,
228 growth_limit,
229 CanMoveObjects());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700230 SetLimit(End());
Mathieu Chartier6f382012019-07-30 09:47:35 -0700231 live_bitmap_.SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
232 CHECK_EQ(live_bitmap_.HeapLimit(), reinterpret_cast<uintptr_t>(End()));
233 mark_bitmap_.SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
234 CHECK_EQ(mark_bitmap_.HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800235
236 // Create the actual zygote space.
Mathieu Chartier6f382012019-07-30 09:47:35 -0700237 ZygoteSpace* zygote_space = ZygoteSpace::Create("Zygote space",
238 ReleaseMemMap(),
239 std::move(live_bitmap_),
240 std::move(mark_bitmap_));
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800241 if (UNLIKELY(zygote_space == nullptr)) {
242 VLOG(heap) << "Failed creating zygote space from space " << GetName();
243 } else {
244 VLOG(heap) << "zygote space creation done";
245 }
246 return zygote_space;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700247}
248
249void MallocSpace::Dump(std::ostream& os) const {
250 os << GetType()
Hiroshi Yamauchi447a9142014-05-23 21:27:30 -0700251 << " begin=" << reinterpret_cast<void*>(Begin())
252 << ",end=" << reinterpret_cast<void*>(End())
253 << ",limit=" << reinterpret_cast<void*>(Limit())
254 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
255 << ",non_growth_limit_capacity=" << PrettySize(NonGrowthLimitCapacity())
256 << ",name=\"" << GetName() << "\"]";
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700257}
258
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800259void MallocSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
Mathieu Chartierec050072014-01-07 16:00:07 -0800260 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800261 space::MallocSpace* space = context->space->AsMallocSpace();
Mathieu Chartierec050072014-01-07 16:00:07 -0800262 Thread* self = context->self;
263 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
264 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
265 // the bitmaps as an optimization.
266 if (!context->swap_bitmaps) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700267 accounting::ContinuousSpaceBitmap* bitmap = space->GetLiveBitmap();
Mathieu Chartierec050072014-01-07 16:00:07 -0800268 for (size_t i = 0; i < num_ptrs; ++i) {
269 bitmap->Clear(ptrs[i]);
270 }
271 }
272 // Use a bulk free, that merges consecutive objects before freeing or free per object?
Roland Levillainef071322018-04-17 14:16:49 +0100273 // Documentation suggests better free performance with merging, but this may be at the expense
Mathieu Chartierec050072014-01-07 16:00:07 -0800274 // of allocation.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700275 context->freed.objects += num_ptrs;
276 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierec050072014-01-07 16:00:07 -0800277}
278
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800279void MallocSpace::ClampGrowthLimit() {
280 size_t new_capacity = Capacity();
281 CHECK_LE(new_capacity, NonGrowthLimitCapacity());
282 GetLiveBitmap()->SetHeapSize(new_capacity);
283 GetMarkBitmap()->SetHeapSize(new_capacity);
Mathieu Chartier6f382012019-07-30 09:47:35 -0700284 if (temp_bitmap_.IsValid()) {
Mathieu Chartierddac4232015-04-02 10:08:03 -0700285 // If the bitmaps are clamped, then the temp bitmap is actually the mark bitmap.
Mathieu Chartier6f382012019-07-30 09:47:35 -0700286 temp_bitmap_.SetHeapSize(new_capacity);
Mathieu Chartierddac4232015-04-02 10:08:03 -0700287 }
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800288 GetMemMap()->SetSize(new_capacity);
289 limit_ = Begin() + new_capacity;
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800290}
291
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700292} // namespace space
293} // namespace gc
294} // namespace art