blob: dac043efbbbae59a3e7d57fa0a806255b7d4ca1e [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
Mathieu Chartierec050072014-01-07 16:00:07 -080019#include "gc/accounting/card_table-inl.h"
20#include "gc/accounting/space_bitmap-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070021#include "gc/heap.h"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080022#include "gc/space/space-inl.h"
23#include "gc/space/zygote_space.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070024#include "mirror/class-inl.h"
25#include "mirror/object-inl.h"
26#include "runtime.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080027#include "sirt_ref-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070028#include "thread.h"
29#include "thread_list.h"
30#include "utils.h"
31
32namespace art {
33namespace gc {
34namespace space {
35
36size_t MallocSpace::bitmap_index_ = 0;
37
38MallocSpace::MallocSpace(const std::string& name, MemMap* mem_map,
Mathieu Chartiera1602f22014-01-13 17:19:19 -080039 byte* begin, byte* end, byte* limit, size_t growth_limit,
40 bool create_bitmaps)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070041 : ContinuousMemMapAllocSpace(name, mem_map, begin, end, limit, kGcRetentionPolicyAlwaysCollect),
42 recent_free_pos_(0), lock_("allocation space lock", kAllocSpaceLock),
43 growth_limit_(growth_limit) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080044 if (create_bitmaps) {
45 size_t bitmap_index = bitmap_index_++;
46 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
47 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->Begin())));
48 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->End())));
49 live_bitmap_.reset(accounting::SpaceBitmap::Create(
50 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
51 Begin(), Capacity()));
52 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #"
53 << bitmap_index;
54 mark_bitmap_.reset(accounting::SpaceBitmap::Create(
55 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
56 Begin(), Capacity()));
57 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #"
58 << bitmap_index;
59 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070060 for (auto& freed : recent_freed_objects_) {
61 freed.first = nullptr;
62 freed.second = nullptr;
63 }
64}
65
66MemMap* MallocSpace::CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size,
67 size_t* growth_limit, size_t* capacity, byte* requested_begin) {
68 // Sanity check arguments
69 if (starting_size > *initial_size) {
70 *initial_size = starting_size;
71 }
72 if (*initial_size > *growth_limit) {
73 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
74 << PrettySize(*initial_size) << ") is larger than its capacity ("
75 << PrettySize(*growth_limit) << ")";
76 return NULL;
77 }
78 if (*growth_limit > *capacity) {
79 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
80 << PrettySize(*growth_limit) << ") is larger than the capacity ("
81 << PrettySize(*capacity) << ")";
82 return NULL;
83 }
84
85 // Page align growth limit and capacity which will be used to manage mmapped storage
86 *growth_limit = RoundUp(*growth_limit, kPageSize);
87 *capacity = RoundUp(*capacity, kPageSize);
88
89 std::string error_msg;
90 MemMap* mem_map = MemMap::MapAnonymous(name.c_str(), requested_begin, *capacity,
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 PROT_READ | PROT_WRITE, true, &error_msg);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080092 if (mem_map == nullptr) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070093 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
94 << PrettySize(*capacity) << ": " << error_msg;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070095 }
96 return mem_map;
97}
98
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070099mirror::Class* MallocSpace::FindRecentFreedObject(const mirror::Object* obj) {
100 size_t pos = recent_free_pos_;
101 // Start at the most recently freed object and work our way back since there may be duplicates
102 // caused by dlmalloc reusing memory.
103 if (kRecentFreeCount > 0) {
104 for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) {
105 pos = pos != 0 ? pos - 1 : kRecentFreeMask;
106 if (recent_freed_objects_[pos].first == obj) {
107 return recent_freed_objects_[pos].second;
108 }
109 }
110 }
111 return nullptr;
112}
113
114void MallocSpace::RegisterRecentFree(mirror::Object* ptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800115 // No verification since the object is dead.
116 recent_freed_objects_[recent_free_pos_] = std::make_pair(ptr, ptr->GetClass<kVerifyNone>());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700117 recent_free_pos_ = (recent_free_pos_ + 1) & kRecentFreeMask;
118}
119
120void MallocSpace::SetGrowthLimit(size_t growth_limit) {
121 growth_limit = RoundUp(growth_limit, kPageSize);
122 growth_limit_ = growth_limit;
123 if (Size() > growth_limit_) {
124 end_ = begin_ + growth_limit;
125 }
126}
127
128void* MallocSpace::MoreCore(intptr_t increment) {
129 CheckMoreCoreForPrecondition();
130 byte* original_end = end_;
131 if (increment != 0) {
132 VLOG(heap) << "MallocSpace::MoreCore " << PrettySize(increment);
133 byte* new_end = original_end + increment;
134 if (increment > 0) {
135 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
136 // by mspace_set_footprint_limit.
137 CHECK_LE(new_end, Begin() + Capacity());
138 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName());
139 } else {
140 // Should never be asked for negative footprint (ie before begin). Zero footprint is ok.
141 CHECK_GE(original_end + increment, Begin());
142 // Advise we don't need the pages and protect them
143 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
144 // expensive (note the same isn't true for giving permissions to a page as the protected
145 // page shouldn't be in a TLB). We should investigate performance impact of just
146 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
147 // likely just a useful debug feature.
148 size_t size = -increment;
149 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName());
150 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName());
151 }
152 // Update end_
153 end_ = new_end;
154 }
155 return original_end;
156}
157
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800158ZygoteSpace* MallocSpace::CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode,
159 MallocSpace** out_malloc_space) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700160 // For RosAlloc, revoke thread local runs before creating a new
161 // alloc space so that we won't mix thread local runs from different
162 // alloc spaces.
163 RevokeAllThreadLocalBuffers();
164 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
165 DCHECK(IsAligned<accounting::CardTable::kCardSize>(begin_));
166 DCHECK(IsAligned<accounting::CardTable::kCardSize>(end_));
167 DCHECK(IsAligned<kPageSize>(begin_));
168 DCHECK(IsAligned<kPageSize>(end_));
169 size_t size = RoundUp(Size(), kPageSize);
Mathieu Chartier85a43c02014-01-07 17:59:00 -0800170 // Trimming the heap should be done by the caller since we may have invalidated the accounting
171 // stored in between objects.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700172 // Remaining size is for the new alloc space.
173 const size_t growth_limit = growth_limit_ - size;
174 const size_t capacity = Capacity() - size;
175 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n"
176 << "End " << reinterpret_cast<const void*>(end_) << "\n"
177 << "Size " << size << "\n"
178 << "GrowthLimit " << growth_limit_ << "\n"
179 << "Capacity " << Capacity();
180 SetGrowthLimit(RoundUp(size, kPageSize));
181 SetFootprintLimit(RoundUp(size, kPageSize));
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800182
183 // TODO: Not hardcode these in?
184 const size_t starting_size = kPageSize;
185 const size_t initial_size = 2 * MB;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700186 // FIXME: Do we need reference counted pointers here?
187 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
188 VLOG(heap) << "Creating new AllocSpace: ";
189 VLOG(heap) << "Size " << GetMemMap()->Size();
190 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
191 VLOG(heap) << "Capacity " << PrettySize(capacity);
192 // Remap the tail.
193 std::string error_msg;
194 UniquePtr<MemMap> mem_map(GetMemMap()->RemapAtEnd(end_, alloc_space_name,
195 PROT_READ | PROT_WRITE, &error_msg));
196 CHECK(mem_map.get() != nullptr) << error_msg;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800197 void* allocator = CreateAllocator(end_, starting_size, initial_size, capacity, low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700198 // Protect memory beyond the initial size.
199 byte* end = mem_map->Begin() + starting_size;
200 if (capacity - initial_size > 0) {
201 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), alloc_space_name);
202 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800203 *out_malloc_space = CreateInstance(alloc_space_name, mem_map.release(), allocator, end_, end,
204 limit_, growth_limit);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700205 SetLimit(End());
206 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
207 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
208 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
209 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800210
211 // Create the actual zygote space.
212 ZygoteSpace* zygote_space = ZygoteSpace::Create("Zygote space", ReleaseMemMap(),
213 live_bitmap_.release(), mark_bitmap_.release());
214 if (UNLIKELY(zygote_space == nullptr)) {
215 VLOG(heap) << "Failed creating zygote space from space " << GetName();
216 } else {
217 VLOG(heap) << "zygote space creation done";
218 }
219 return zygote_space;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700220}
221
222void MallocSpace::Dump(std::ostream& os) const {
223 os << GetType()
224 << " begin=" << reinterpret_cast<void*>(Begin())
225 << ",end=" << reinterpret_cast<void*>(End())
226 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
227 << ",name=\"" << GetName() << "\"]";
228}
229
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800230void MallocSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
Mathieu Chartierec050072014-01-07 16:00:07 -0800231 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800232 DCHECK(context->space->IsMallocSpace());
233 space::MallocSpace* space = context->space->AsMallocSpace();
Mathieu Chartierec050072014-01-07 16:00:07 -0800234 Thread* self = context->self;
235 Locks::heap_bitmap_lock_->AssertExclusiveHeld(self);
236 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
237 // the bitmaps as an optimization.
238 if (!context->swap_bitmaps) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800239 accounting::SpaceBitmap* bitmap = space->GetLiveBitmap();
Mathieu Chartierec050072014-01-07 16:00:07 -0800240 for (size_t i = 0; i < num_ptrs; ++i) {
241 bitmap->Clear(ptrs[i]);
242 }
243 }
244 // Use a bulk free, that merges consecutive objects before freeing or free per object?
245 // Documentation suggests better free performance with merging, but this may be at the expensive
246 // of allocation.
247 context->freed_objects += num_ptrs;
248 context->freed_bytes += space->FreeList(self, num_ptrs, ptrs);
249}
250
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700251} // namespace space
252} // namespace gc
253} // namespace art