blob: 567ec99ce7b71e9edaa026f9b783da897bcd316f [file] [log] [blame]
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -08001
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07002/*
3 * Copyright (C) 2013 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "rosalloc_space.h"
19
20#include "rosalloc_space-inl.h"
21#include "gc/accounting/card_table.h"
22#include "gc/heap.h"
23#include "mirror/class-inl.h"
24#include "mirror/object-inl.h"
25#include "runtime.h"
26#include "thread.h"
27#include "thread_list.h"
28#include "utils.h"
Ian Rogers6fac4472014-02-25 17:01:10 -080029#include "valgrind_malloc_space-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070030
31namespace art {
32namespace gc {
33namespace space {
34
Ian Rogers6fac4472014-02-25 17:01:10 -080035static constexpr bool kPrefetchDuringRosAllocFreeList = true;
36
37template class ValgrindMallocSpace<RosAllocSpace, allocator::RosAlloc*>;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070038
39RosAllocSpace::RosAllocSpace(const std::string& name, MemMap* mem_map,
40 art::gc::allocator::RosAlloc* rosalloc, byte* begin, byte* end,
41 byte* limit, size_t growth_limit)
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -080042 : MallocSpace(name, mem_map, begin, end, limit, growth_limit), rosalloc_(rosalloc),
43 rosalloc_for_alloc_(rosalloc) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070044 CHECK(rosalloc != NULL);
45}
46
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080047RosAllocSpace* RosAllocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name,
Ian Rogers6fac4472014-02-25 17:01:10 -080048 size_t starting_size, size_t initial_size,
49 size_t growth_limit, size_t capacity,
50 bool low_memory_mode) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080051 DCHECK(mem_map != nullptr);
52 allocator::RosAlloc* rosalloc = CreateRosAlloc(mem_map->Begin(), starting_size, initial_size,
53 low_memory_mode);
54 if (rosalloc == NULL) {
55 LOG(ERROR) << "Failed to initialize rosalloc for alloc space (" << name << ")";
56 return NULL;
57 }
58
lzang1385de732014-02-21 14:15:01 +080059 // Protect memory beyond the starting size. MoreCore will add r/w permissions when necessory
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080060 byte* end = mem_map->Begin() + starting_size;
lzang1385de732014-02-21 14:15:01 +080061 if (capacity - starting_size > 0) {
62 CHECK_MEMORY_CALL(mprotect, (end, capacity - starting_size, PROT_NONE), name);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080063 }
64
65 // Everything is set so record in immutable structure and leave
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080066 byte* begin = mem_map->Begin();
67 if (RUNNING_ON_VALGRIND > 0) {
Ian Rogers6fac4472014-02-25 17:01:10 -080068 return new ValgrindMallocSpace<RosAllocSpace, allocator::RosAlloc*>(
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080069 name, mem_map, rosalloc, begin, end, begin + capacity, growth_limit, initial_size);
70 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -080071 return new RosAllocSpace(name, mem_map, rosalloc, begin, end, begin + capacity, growth_limit);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080072 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080073}
74
Ian Rogers6fac4472014-02-25 17:01:10 -080075RosAllocSpace* RosAllocSpace::Create(const std::string& name, size_t initial_size,
76 size_t growth_limit, size_t capacity, byte* requested_begin,
77 bool low_memory_mode) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070078 uint64_t start_time = 0;
79 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
80 start_time = NanoTime();
81 VLOG(startup) << "RosAllocSpace::Create entering " << name
82 << " initial_size=" << PrettySize(initial_size)
83 << " growth_limit=" << PrettySize(growth_limit)
84 << " capacity=" << PrettySize(capacity)
85 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
86 }
87
88 // Memory we promise to rosalloc before it asks for morecore.
89 // Note: making this value large means that large allocations are unlikely to succeed as rosalloc
90 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
91 // size of the large allocation) will be greater than the footprint limit.
92 size_t starting_size = kPageSize;
93 MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity,
94 requested_begin);
95 if (mem_map == NULL) {
96 LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size "
97 << PrettySize(capacity);
98 return NULL;
99 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700100
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800101 RosAllocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size,
102 growth_limit, capacity, low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700103 // We start out with only the initial size possibly containing objects.
104 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
105 LOG(INFO) << "RosAllocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time)
106 << " ) " << *space;
107 }
108 return space;
109}
110
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800111allocator::RosAlloc* RosAllocSpace::CreateRosAlloc(void* begin, size_t morecore_start, size_t initial_size,
112 bool low_memory_mode) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700113 // clear errno to allow PLOG on error
114 errno = 0;
115 // create rosalloc using our backing storage starting at begin and
116 // with a footprint of morecore_start. When morecore_start bytes of
117 // memory is exhaused morecore will be called.
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800118 allocator::RosAlloc* rosalloc = new art::gc::allocator::RosAlloc(
119 begin, morecore_start,
120 low_memory_mode ?
121 art::gc::allocator::RosAlloc::kPageReleaseModeAll :
122 art::gc::allocator::RosAlloc::kPageReleaseModeSizeAndEnd);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700123 if (rosalloc != NULL) {
124 rosalloc->SetFootprintLimit(initial_size);
125 } else {
126 PLOG(ERROR) << "RosAlloc::Create failed";
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800127 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700128 return rosalloc;
129}
130
Ian Rogers6fac4472014-02-25 17:01:10 -0800131mirror::Object* RosAllocSpace::AllocWithGrowth(Thread* self, size_t num_bytes,
132 size_t* bytes_allocated, size_t* usable_size) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700133 mirror::Object* result;
134 {
135 MutexLock mu(self, lock_);
136 // Grow as much as possible within the space.
137 size_t max_allowed = Capacity();
138 rosalloc_->SetFootprintLimit(max_allowed);
139 // Try the allocation.
Ian Rogers6fac4472014-02-25 17:01:10 -0800140 result = AllocCommon(self, num_bytes, bytes_allocated, usable_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700141 // Shrink back down as small as possible.
142 size_t footprint = rosalloc_->Footprint();
143 rosalloc_->SetFootprintLimit(footprint);
144 }
145 // Note RosAlloc zeroes memory internally.
146 // Return the new allocation or NULL.
147 CHECK(!kDebugSpaces || result == NULL || Contains(result));
148 return result;
149}
150
151MallocSpace* RosAllocSpace::CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
152 byte* begin, byte* end, byte* limit, size_t growth_limit) {
153 return new RosAllocSpace(name, mem_map, reinterpret_cast<allocator::RosAlloc*>(allocator),
154 begin, end, limit, growth_limit);
155}
156
157size_t RosAllocSpace::Free(Thread* self, mirror::Object* ptr) {
158 if (kDebugSpaces) {
159 CHECK(ptr != NULL);
160 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
161 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800162 const size_t bytes_freed = AllocationSizeNonvirtual(ptr, nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700163 if (kRecentFreeCount > 0) {
164 MutexLock mu(self, lock_);
165 RegisterRecentFree(ptr);
166 }
167 rosalloc_->Free(self, ptr);
168 return bytes_freed;
169}
170
171size_t RosAllocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
172 DCHECK(ptrs != NULL);
173
174 // Don't need the lock to calculate the size of the freed pointers.
175 size_t bytes_freed = 0;
176 for (size_t i = 0; i < num_ptrs; i++) {
177 mirror::Object* ptr = ptrs[i];
178 const size_t look_ahead = 8;
179 if (kPrefetchDuringRosAllocFreeList && i + look_ahead < num_ptrs) {
180 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]));
181 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800182 bytes_freed += AllocationSizeNonvirtual(ptr, nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700183 }
184
185 if (kRecentFreeCount > 0) {
186 MutexLock mu(self, lock_);
187 for (size_t i = 0; i < num_ptrs; i++) {
188 RegisterRecentFree(ptrs[i]);
189 }
190 }
191
192 if (kDebugSpaces) {
193 size_t num_broken_ptrs = 0;
194 for (size_t i = 0; i < num_ptrs; i++) {
195 if (!Contains(ptrs[i])) {
196 num_broken_ptrs++;
197 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
198 } else {
199 size_t size = rosalloc_->UsableSize(ptrs[i]);
200 memset(ptrs[i], 0xEF, size);
201 }
202 }
203 CHECK_EQ(num_broken_ptrs, 0u);
204 }
205
206 rosalloc_->BulkFree(self, reinterpret_cast<void**>(ptrs), num_ptrs);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700207 return bytes_freed;
208}
209
210// Callback from rosalloc when it needs to increase the footprint
211extern "C" void* art_heap_rosalloc_morecore(allocator::RosAlloc* rosalloc, intptr_t increment) {
212 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800213 RosAllocSpace* rosalloc_space = heap->GetRosAllocSpace();
214 DCHECK(rosalloc_space != nullptr);
215 DCHECK_EQ(rosalloc_space->GetRosAlloc(), rosalloc);
216 return rosalloc_space->MoreCore(increment);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700217}
218
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700219size_t RosAllocSpace::Trim() {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800220 {
221 MutexLock mu(Thread::Current(), lock_);
222 // Trim to release memory at the end of the space.
223 rosalloc_->Trim();
224 }
225 // Attempt to release pages if it does not release all empty pages.
226 if (!rosalloc_->DoesReleaseAllPages()) {
227 VLOG(heap) << "RosAllocSpace::Trim() ";
228 size_t reclaimed = 0;
229 InspectAllRosAlloc(DlmallocMadviseCallback, &reclaimed);
230 return reclaimed;
231 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700232 return 0;
233}
234
235void RosAllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
236 void* arg) {
237 InspectAllRosAlloc(callback, arg);
238 callback(NULL, NULL, 0, arg); // Indicate end of a space.
239}
240
241size_t RosAllocSpace::GetFootprint() {
242 MutexLock mu(Thread::Current(), lock_);
243 return rosalloc_->Footprint();
244}
245
246size_t RosAllocSpace::GetFootprintLimit() {
247 MutexLock mu(Thread::Current(), lock_);
248 return rosalloc_->FootprintLimit();
249}
250
251void RosAllocSpace::SetFootprintLimit(size_t new_size) {
252 MutexLock mu(Thread::Current(), lock_);
253 VLOG(heap) << "RosAllocSpace::SetFootprintLimit " << PrettySize(new_size);
254 // Compare against the actual footprint, rather than the Size(), because the heap may not have
255 // grown all the way to the allowed size yet.
256 size_t current_space_size = rosalloc_->Footprint();
257 if (new_size < current_space_size) {
258 // Don't let the space grow any more.
259 new_size = current_space_size;
260 }
261 rosalloc_->SetFootprintLimit(new_size);
262}
263
264uint64_t RosAllocSpace::GetBytesAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800265 size_t bytes_allocated = 0;
266 InspectAllRosAlloc(art::gc::allocator::RosAlloc::BytesAllocatedCallback, &bytes_allocated);
267 return bytes_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700268}
269
270uint64_t RosAllocSpace::GetObjectsAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800271 size_t objects_allocated = 0;
272 InspectAllRosAlloc(art::gc::allocator::RosAlloc::ObjectsAllocatedCallback, &objects_allocated);
273 return objects_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700274}
275
276void RosAllocSpace::InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
277 void* arg) NO_THREAD_SAFETY_ANALYSIS {
278 // TODO: NO_THREAD_SAFETY_ANALYSIS.
279 Thread* self = Thread::Current();
280 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
281 // The mutators are already suspended. For example, a call path
282 // from SignalCatcher::HandleSigQuit().
283 rosalloc_->InspectAll(callback, arg);
284 } else {
285 // The mutators are not suspended yet.
286 DCHECK(!Locks::mutator_lock_->IsSharedHeld(self));
287 ThreadList* tl = Runtime::Current()->GetThreadList();
288 tl->SuspendAll();
289 {
290 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
291 MutexLock mu2(self, *Locks::thread_list_lock_);
292 rosalloc_->InspectAll(callback, arg);
293 }
294 tl->ResumeAll();
295 }
296}
297
298void RosAllocSpace::RevokeThreadLocalBuffers(Thread* thread) {
299 rosalloc_->RevokeThreadLocalRuns(thread);
300}
301
302void RosAllocSpace::RevokeAllThreadLocalBuffers() {
303 rosalloc_->RevokeAllThreadLocalRuns();
304}
305
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800306void RosAllocSpace::Clear() {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800307 // TODO: Delete and create new mspace here.
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800308 madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED);
309 GetLiveBitmap()->Clear();
310 GetMarkBitmap()->Clear();
311}
312
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700313} // namespace space
314} // namespace gc
315} // namespace art