blob: a186f4c9224a107e30050ab99963da528f11032f [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 Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Mathieu Chartierec050072014-01-07 16:00:07 -080021#include "gc/accounting/card_table-inl.h"
22#include "gc/accounting/space_bitmap-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070023#include "gc/heap.h"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080024#include "gc/space/space-inl.h"
25#include "gc/space/zygote_space.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070026#include "mirror/class-inl.h"
27#include "mirror/object-inl.h"
28#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "handle_scope-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070030#include "thread.h"
31#include "thread_list.h"
32#include "utils.h"
33
34namespace art {
35namespace gc {
36namespace space {
37
Andreas Gampe46ee31b2016-12-14 10:11:49 -080038using android::base::StringPrintf;
39
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070040size_t MallocSpace::bitmap_index_ = 0;
41
42MallocSpace::MallocSpace(const std::string& name, MemMap* mem_map,
Ian Rogers13735952014-10-08 12:43:28 -070043 uint8_t* begin, uint8_t* end, uint8_t* limit, size_t growth_limit,
Mathieu Chartier31f44142014-04-08 14:40:03 -070044 bool create_bitmaps, bool can_move_objects, size_t starting_size,
45 size_t initial_size)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070046 : ContinuousMemMapAllocSpace(name, mem_map, begin, end, limit, kGcRetentionPolicyAlwaysCollect),
47 recent_free_pos_(0), lock_("allocation space lock", kAllocSpaceLock),
Mathieu Chartier31f44142014-04-08 14:40:03 -070048 growth_limit_(growth_limit), can_move_objects_(can_move_objects),
49 starting_size_(starting_size), initial_size_(initial_size) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080050 if (create_bitmaps) {
51 size_t bitmap_index = bitmap_index_++;
52 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
Roland Levillain14d90572015-07-16 10:52:26 +010053 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(mem_map->Begin()), kGcCardSize);
54 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(mem_map->End()), kGcCardSize);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070055 live_bitmap_.reset(accounting::ContinuousSpaceBitmap::Create(
Mathieu Chartiera1602f22014-01-13 17:19:19 -080056 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Hiroshi Yamauchi447a9142014-05-23 21:27:30 -070057 Begin(), NonGrowthLimitCapacity()));
Mathieu Chartier2796a162014-07-25 11:50:47 -070058 CHECK(live_bitmap_.get() != nullptr) << "could not create allocspace live bitmap #"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080059 << bitmap_index;
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070060 mark_bitmap_.reset(accounting::ContinuousSpaceBitmap::Create(
Mathieu Chartiera1602f22014-01-13 17:19:19 -080061 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Hiroshi Yamauchi447a9142014-05-23 21:27:30 -070062 Begin(), NonGrowthLimitCapacity()));
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010063 CHECK(mark_bitmap_.get() != nullptr) << "could not create allocspace mark bitmap #"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080064 << bitmap_index;
65 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070066 for (auto& freed : recent_freed_objects_) {
67 freed.first = nullptr;
68 freed.second = nullptr;
69 }
70}
71
72MemMap* MallocSpace::CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size,
Ian Rogers13735952014-10-08 12:43:28 -070073 size_t* growth_limit, size_t* capacity, uint8_t* requested_begin) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070074 // Sanity check arguments
75 if (starting_size > *initial_size) {
76 *initial_size = starting_size;
77 }
78 if (*initial_size > *growth_limit) {
79 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
80 << PrettySize(*initial_size) << ") is larger than its capacity ("
81 << PrettySize(*growth_limit) << ")";
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 return nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070083 }
84 if (*growth_limit > *capacity) {
85 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
86 << PrettySize(*growth_limit) << ") is larger than the capacity ("
87 << PrettySize(*capacity) << ")";
Mathieu Chartier2cebb242015-04-21 16:50:40 -070088 return nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070089 }
90
91 // Page align growth limit and capacity which will be used to manage mmapped storage
92 *growth_limit = RoundUp(*growth_limit, kPageSize);
93 *capacity = RoundUp(*capacity, kPageSize);
94
95 std::string error_msg;
96 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, *capacity,
Vladimir Marko5c42c292015-02-25 12:02:49 +000097 PROT_READ | PROT_WRITE, true, false, &error_msg);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080098 if (mem_map == nullptr) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070099 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
100 << PrettySize(*capacity) << ": " << error_msg;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700101 }
102 return mem_map;
103}
104
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700105mirror::Class* MallocSpace::FindRecentFreedObject(const mirror::Object* obj) {
106 size_t pos = recent_free_pos_;
107 // Start at the most recently freed object and work our way back since there may be duplicates
108 // caused by dlmalloc reusing memory.
109 if (kRecentFreeCount > 0) {
110 for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) {
111 pos = pos != 0 ? pos - 1 : kRecentFreeMask;
112 if (recent_freed_objects_[pos].first == obj) {
113 return recent_freed_objects_[pos].second;
114 }
115 }
116 }
117 return nullptr;
118}
119
120void MallocSpace::RegisterRecentFree(mirror::Object* ptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800121 // No verification since the object is dead.
122 recent_freed_objects_[recent_free_pos_] = std::make_pair(ptr, ptr->GetClass<kVerifyNone>());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700123 recent_free_pos_ = (recent_free_pos_ + 1) & kRecentFreeMask;
124}
125
126void MallocSpace::SetGrowthLimit(size_t growth_limit) {
127 growth_limit = RoundUp(growth_limit, kPageSize);
128 growth_limit_ = growth_limit;
129 if (Size() > growth_limit_) {
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700130 SetEnd(begin_ + growth_limit);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700131 }
132}
133
134void* MallocSpace::MoreCore(intptr_t increment) {
135 CheckMoreCoreForPrecondition();
Ian Rogers13735952014-10-08 12:43:28 -0700136 uint8_t* original_end = End();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700137 if (increment != 0) {
138 VLOG(heap) << "MallocSpace::MoreCore " << PrettySize(increment);
Ian Rogers13735952014-10-08 12:43:28 -0700139 uint8_t* new_end = original_end + increment;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700140 if (increment > 0) {
141 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
142 // by mspace_set_footprint_limit.
143 CHECK_LE(new_end, Begin() + Capacity());
144 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName());
145 } else {
146 // Should never be asked for negative footprint (ie before begin). Zero footprint is ok.
147 CHECK_GE(original_end + increment, Begin());
148 // Advise we don't need the pages and protect them
149 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
150 // expensive (note the same isn't true for giving permissions to a page as the protected
151 // page shouldn't be in a TLB). We should investigate performance impact of just
152 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
153 // likely just a useful debug feature.
154 size_t size = -increment;
155 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName());
156 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName());
157 }
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700158 // Update end_.
159 SetEnd(new_end);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700160 }
161 return original_end;
162}
163
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800164ZygoteSpace* MallocSpace::CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode,
165 MallocSpace** out_malloc_space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700166 // For RosAlloc, revoke thread local runs before creating a new
167 // alloc space so that we won't mix thread local runs from different
168 // alloc spaces.
169 RevokeAllThreadLocalBuffers();
Ian Rogers13735952014-10-08 12:43:28 -0700170 SetEnd(reinterpret_cast<uint8_t*>(RoundUp(reinterpret_cast<uintptr_t>(End()), kPageSize)));
Roland Levillain14d90572015-07-16 10:52:26 +0100171 DCHECK_ALIGNED(begin_, accounting::CardTable::kCardSize);
172 DCHECK_ALIGNED(End(), accounting::CardTable::kCardSize);
173 DCHECK_ALIGNED(begin_, kPageSize);
174 DCHECK_ALIGNED(End(), kPageSize);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700175 size_t size = RoundUp(Size(), kPageSize);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800176 // Trimming the heap should be done by the caller since we may have invalidated the accounting
177 // stored in between objects.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700178 // Remaining size is for the new alloc space.
179 const size_t growth_limit = growth_limit_ - size;
Lin Zangd0e0d4c2014-12-12 21:54:47 +0800180 // Use mem map limit in case error for clear growth limit.
181 const size_t capacity = NonGrowthLimitCapacity() - size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700182 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n"
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700183 << "End " << reinterpret_cast<const void*>(End()) << "\n"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700184 << "Size " << size << "\n"
185 << "GrowthLimit " << growth_limit_ << "\n"
186 << "Capacity " << Capacity();
187 SetGrowthLimit(RoundUp(size, kPageSize));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700188 // FIXME: Do we need reference counted pointers here?
189 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
190 VLOG(heap) << "Creating new AllocSpace: ";
191 VLOG(heap) << "Size " << GetMemMap()->Size();
192 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
193 VLOG(heap) << "Capacity " << PrettySize(capacity);
194 // Remap the tail.
195 std::string error_msg;
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700196 std::unique_ptr<MemMap> mem_map(GetMemMap()->RemapAtEnd(End(), alloc_space_name,
197 PROT_READ | PROT_WRITE, &error_msg));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700198 CHECK(mem_map.get() != nullptr) << error_msg;
Ian Rogersbe2a1df2014-07-10 00:56:36 -0700199 void* allocator = CreateAllocator(End(), starting_size_, initial_size_, capacity,
200 low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700201 // Protect memory beyond the initial size.
Ian Rogers13735952014-10-08 12:43:28 -0700202 uint8_t* end = mem_map->Begin() + starting_size_;
Mathieu Chartierc4d095b2014-04-15 12:01:58 -0700203 if (capacity > initial_size_) {
204 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size_, PROT_NONE), alloc_space_name);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700205 }
Andreas Gamped7576322014-10-24 22:13:45 -0700206 *out_malloc_space = CreateInstance(mem_map.release(), alloc_space_name, allocator, End(), end,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700207 limit_, growth_limit, CanMoveObjects());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700208 SetLimit(End());
209 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
210 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
211 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
212 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800213
214 // Create the actual zygote space.
215 ZygoteSpace* zygote_space = ZygoteSpace::Create("Zygote space", ReleaseMemMap(),
216 live_bitmap_.release(), mark_bitmap_.release());
217 if (UNLIKELY(zygote_space == nullptr)) {
218 VLOG(heap) << "Failed creating zygote space from space " << GetName();
219 } else {
220 VLOG(heap) << "zygote space creation done";
221 }
222 return zygote_space;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700223}
224
225void MallocSpace::Dump(std::ostream& os) const {
226 os << GetType()
Hiroshi Yamauchi447a9142014-05-23 21:27:30 -0700227 << " begin=" << reinterpret_cast<void*>(Begin())
228 << ",end=" << reinterpret_cast<void*>(End())
229 << ",limit=" << reinterpret_cast<void*>(Limit())
230 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
231 << ",non_growth_limit_capacity=" << PrettySize(NonGrowthLimitCapacity())
232 << ",name=\"" << GetName() << "\"]";
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700233}
234
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800235void MallocSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
Mathieu Chartierec050072014-01-07 16:00:07 -0800236 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800237 space::MallocSpace* space = context->space->AsMallocSpace();
Mathieu Chartierec050072014-01-07 16:00:07 -0800238 Thread* self = context->self;
239 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
240 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
241 // the bitmaps as an optimization.
242 if (!context->swap_bitmaps) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700243 accounting::ContinuousSpaceBitmap* bitmap = space->GetLiveBitmap();
Mathieu Chartierec050072014-01-07 16:00:07 -0800244 for (size_t i = 0; i < num_ptrs; ++i) {
245 bitmap->Clear(ptrs[i]);
246 }
247 }
248 // Use a bulk free, that merges consecutive objects before freeing or free per object?
249 // Documentation suggests better free performance with merging, but this may be at the expensive
250 // of allocation.
Mathieu Chartier10fb83a2014-06-15 15:15:43 -0700251 context->freed.objects += num_ptrs;
252 context->freed.bytes += space->FreeList(self, num_ptrs, ptrs);
Mathieu Chartierec050072014-01-07 16:00:07 -0800253}
254
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800255void MallocSpace::ClampGrowthLimit() {
256 size_t new_capacity = Capacity();
257 CHECK_LE(new_capacity, NonGrowthLimitCapacity());
258 GetLiveBitmap()->SetHeapSize(new_capacity);
259 GetMarkBitmap()->SetHeapSize(new_capacity);
Mathieu Chartierddac4232015-04-02 10:08:03 -0700260 if (temp_bitmap_.get() != nullptr) {
261 // If the bitmaps are clamped, then the temp bitmap is actually the mark bitmap.
262 temp_bitmap_->SetHeapSize(new_capacity);
263 }
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800264 GetMemMap()->SetSize(new_capacity);
265 limit_ = Begin() + new_capacity;
Mathieu Chartier379d09f2015-01-08 11:28:13 -0800266}
267
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700268} // namespace space
269} // namespace gc
270} // namespace art