blob: 012267bc1486f7a6f3304cf3e59251004ac8d863 [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)
Mathieu Chartier661974a2014-01-09 11:23:53 -080042 : MallocSpace(name, mem_map, begin, end, limit, growth_limit), rosalloc_(rosalloc) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070043 CHECK(rosalloc != NULL);
44}
45
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080046RosAllocSpace* RosAllocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name,
Ian Rogersa55cf412014-02-27 00:31:26 -080047 size_t starting_size, size_t initial_size,
48 size_t growth_limit, size_t capacity,
49 bool low_memory_mode) {
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080050 DCHECK(mem_map != nullptr);
51 allocator::RosAlloc* rosalloc = CreateRosAlloc(mem_map->Begin(), starting_size, initial_size,
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080052 capacity, low_memory_mode);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080053 if (rosalloc == NULL) {
54 LOG(ERROR) << "Failed to initialize rosalloc for alloc space (" << name << ")";
55 return NULL;
56 }
57
lzang1385de732014-02-21 14:15:01 +080058 // Protect memory beyond the starting size. MoreCore will add r/w permissions when necessory
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080059 byte* end = mem_map->Begin() + starting_size;
lzang1385de732014-02-21 14:15:01 +080060 if (capacity - starting_size > 0) {
61 CHECK_MEMORY_CALL(mprotect, (end, capacity - starting_size, PROT_NONE), name);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080062 }
63
64 // Everything is set so record in immutable structure and leave
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080065 byte* begin = mem_map->Begin();
Mathieu Chartier661974a2014-01-09 11:23:53 -080066 // TODO: Fix RosAllocSpace to support valgrind. There is currently some issues with
67 // AllocationSize caused by redzones. b/12944686
Mathieu Chartier36bf2162014-03-20 15:40:37 -070068 if (false && Runtime::Current()->GetHeap()->RunningOnValgrind()) {
Ian Rogers6fac4472014-02-25 17:01:10 -080069 return new ValgrindMallocSpace<RosAllocSpace, allocator::RosAlloc*>(
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080070 name, mem_map, rosalloc, begin, end, begin + capacity, growth_limit, initial_size);
71 } else {
Ian Rogers6fac4472014-02-25 17:01:10 -080072 return new RosAllocSpace(name, mem_map, rosalloc, begin, end, begin + capacity, growth_limit);
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080073 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080074}
75
Mathieu Chartier661974a2014-01-09 11:23:53 -080076RosAllocSpace::~RosAllocSpace() {
77 delete rosalloc_;
78}
79
Ian Rogers6fac4472014-02-25 17:01:10 -080080RosAllocSpace* RosAllocSpace::Create(const std::string& name, size_t initial_size,
Ian Rogersa55cf412014-02-27 00:31:26 -080081 size_t growth_limit, size_t capacity, byte* requested_begin,
82 bool low_memory_mode) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070083 uint64_t start_time = 0;
84 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
85 start_time = NanoTime();
86 VLOG(startup) << "RosAllocSpace::Create entering " << name
87 << " initial_size=" << PrettySize(initial_size)
88 << " growth_limit=" << PrettySize(growth_limit)
89 << " capacity=" << PrettySize(capacity)
90 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
91 }
92
93 // Memory we promise to rosalloc before it asks for morecore.
94 // Note: making this value large means that large allocations are unlikely to succeed as rosalloc
95 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
96 // size of the large allocation) will be greater than the footprint limit.
Hiroshi Yamauchi5ccd4982014-03-11 12:19:04 -070097 size_t starting_size = Heap::kDefaultStartingSize;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070098 MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity,
99 requested_begin);
100 if (mem_map == NULL) {
101 LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size "
102 << PrettySize(capacity);
103 return NULL;
104 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700105
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800106 RosAllocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size,
107 growth_limit, capacity, low_memory_mode);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700108 // We start out with only the initial size possibly containing objects.
109 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
110 LOG(INFO) << "RosAllocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time)
111 << " ) " << *space;
112 }
113 return space;
114}
115
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800116allocator::RosAlloc* RosAllocSpace::CreateRosAlloc(void* begin, size_t morecore_start, size_t initial_size,
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800117 size_t maximum_size, bool low_memory_mode) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700118 // clear errno to allow PLOG on error
119 errno = 0;
120 // create rosalloc using our backing storage starting at begin and
121 // with a footprint of morecore_start. When morecore_start bytes of
122 // memory is exhaused morecore will be called.
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800123 allocator::RosAlloc* rosalloc = new art::gc::allocator::RosAlloc(
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800124 begin, morecore_start, maximum_size,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800125 low_memory_mode ?
126 art::gc::allocator::RosAlloc::kPageReleaseModeAll :
127 art::gc::allocator::RosAlloc::kPageReleaseModeSizeAndEnd);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700128 if (rosalloc != NULL) {
129 rosalloc->SetFootprintLimit(initial_size);
130 } else {
131 PLOG(ERROR) << "RosAlloc::Create failed";
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800132 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700133 return rosalloc;
134}
135
Ian Rogers6fac4472014-02-25 17:01:10 -0800136mirror::Object* RosAllocSpace::AllocWithGrowth(Thread* self, size_t num_bytes,
137 size_t* bytes_allocated, size_t* usable_size) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700138 mirror::Object* result;
139 {
140 MutexLock mu(self, lock_);
141 // Grow as much as possible within the space.
142 size_t max_allowed = Capacity();
143 rosalloc_->SetFootprintLimit(max_allowed);
144 // Try the allocation.
Ian Rogers6fac4472014-02-25 17:01:10 -0800145 result = AllocCommon(self, num_bytes, bytes_allocated, usable_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700146 // Shrink back down as small as possible.
147 size_t footprint = rosalloc_->Footprint();
148 rosalloc_->SetFootprintLimit(footprint);
149 }
150 // Note RosAlloc zeroes memory internally.
151 // Return the new allocation or NULL.
152 CHECK(!kDebugSpaces || result == NULL || Contains(result));
153 return result;
154}
155
156MallocSpace* RosAllocSpace::CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
157 byte* begin, byte* end, byte* limit, size_t growth_limit) {
158 return new RosAllocSpace(name, mem_map, reinterpret_cast<allocator::RosAlloc*>(allocator),
159 begin, end, limit, growth_limit);
160}
161
162size_t RosAllocSpace::Free(Thread* self, mirror::Object* ptr) {
163 if (kDebugSpaces) {
164 CHECK(ptr != NULL);
165 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
166 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800167 const size_t bytes_freed = AllocationSizeNonvirtual(ptr, nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700168 if (kRecentFreeCount > 0) {
169 MutexLock mu(self, lock_);
170 RegisterRecentFree(ptr);
171 }
172 rosalloc_->Free(self, ptr);
173 return bytes_freed;
174}
175
176size_t RosAllocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
177 DCHECK(ptrs != NULL);
178
179 // Don't need the lock to calculate the size of the freed pointers.
180 size_t bytes_freed = 0;
181 for (size_t i = 0; i < num_ptrs; i++) {
182 mirror::Object* ptr = ptrs[i];
183 const size_t look_ahead = 8;
184 if (kPrefetchDuringRosAllocFreeList && i + look_ahead < num_ptrs) {
185 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]));
186 }
Ian Rogers6fac4472014-02-25 17:01:10 -0800187 bytes_freed += AllocationSizeNonvirtual(ptr, nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700188 }
189
190 if (kRecentFreeCount > 0) {
191 MutexLock mu(self, lock_);
192 for (size_t i = 0; i < num_ptrs; i++) {
193 RegisterRecentFree(ptrs[i]);
194 }
195 }
196
197 if (kDebugSpaces) {
198 size_t num_broken_ptrs = 0;
199 for (size_t i = 0; i < num_ptrs; i++) {
200 if (!Contains(ptrs[i])) {
201 num_broken_ptrs++;
202 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
203 } else {
204 size_t size = rosalloc_->UsableSize(ptrs[i]);
205 memset(ptrs[i], 0xEF, size);
206 }
207 }
208 CHECK_EQ(num_broken_ptrs, 0u);
209 }
210
211 rosalloc_->BulkFree(self, reinterpret_cast<void**>(ptrs), num_ptrs);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700212 return bytes_freed;
213}
214
215// Callback from rosalloc when it needs to increase the footprint
216extern "C" void* art_heap_rosalloc_morecore(allocator::RosAlloc* rosalloc, intptr_t increment) {
217 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800218 RosAllocSpace* rosalloc_space = heap->GetRosAllocSpace();
219 DCHECK(rosalloc_space != nullptr);
220 DCHECK_EQ(rosalloc_space->GetRosAlloc(), rosalloc);
221 return rosalloc_space->MoreCore(increment);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700222}
223
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700224size_t RosAllocSpace::Trim() {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800225 {
226 MutexLock mu(Thread::Current(), lock_);
227 // Trim to release memory at the end of the space.
228 rosalloc_->Trim();
229 }
230 // Attempt to release pages if it does not release all empty pages.
231 if (!rosalloc_->DoesReleaseAllPages()) {
232 VLOG(heap) << "RosAllocSpace::Trim() ";
233 size_t reclaimed = 0;
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700234 InspectAllRosAlloc(DlmallocMadviseCallback, &reclaimed, false);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800235 return reclaimed;
236 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700237 return 0;
238}
239
240void RosAllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
241 void* arg) {
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700242 InspectAllRosAlloc(callback, arg, true);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700243}
244
245size_t RosAllocSpace::GetFootprint() {
246 MutexLock mu(Thread::Current(), lock_);
247 return rosalloc_->Footprint();
248}
249
250size_t RosAllocSpace::GetFootprintLimit() {
251 MutexLock mu(Thread::Current(), lock_);
252 return rosalloc_->FootprintLimit();
253}
254
255void RosAllocSpace::SetFootprintLimit(size_t new_size) {
256 MutexLock mu(Thread::Current(), lock_);
257 VLOG(heap) << "RosAllocSpace::SetFootprintLimit " << PrettySize(new_size);
258 // Compare against the actual footprint, rather than the Size(), because the heap may not have
259 // grown all the way to the allowed size yet.
260 size_t current_space_size = rosalloc_->Footprint();
261 if (new_size < current_space_size) {
262 // Don't let the space grow any more.
263 new_size = current_space_size;
264 }
265 rosalloc_->SetFootprintLimit(new_size);
266}
267
268uint64_t RosAllocSpace::GetBytesAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800269 size_t bytes_allocated = 0;
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700270 InspectAllRosAlloc(art::gc::allocator::RosAlloc::BytesAllocatedCallback, &bytes_allocated, false);
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800271 return bytes_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700272}
273
274uint64_t RosAllocSpace::GetObjectsAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800275 size_t objects_allocated = 0;
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700276 InspectAllRosAlloc(art::gc::allocator::RosAlloc::ObjectsAllocatedCallback, &objects_allocated, false);
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800277 return objects_allocated;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700278}
279
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700280void RosAllocSpace::InspectAllRosAllocWithSuspendAll(
281 void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
282 void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS {
283 // TODO: NO_THREAD_SAFETY_ANALYSIS.
284 Thread* self = Thread::Current();
285 ThreadList* tl = Runtime::Current()->GetThreadList();
286 tl->SuspendAll();
287 {
288 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
289 MutexLock mu2(self, *Locks::thread_list_lock_);
290 rosalloc_->InspectAll(callback, arg);
291 if (do_null_callback_at_end) {
292 callback(NULL, NULL, 0, arg); // Indicate end of a space.
293 }
294 }
295 tl->ResumeAll();
296}
297
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700298void RosAllocSpace::InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700299 void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700300 // TODO: NO_THREAD_SAFETY_ANALYSIS.
301 Thread* self = Thread::Current();
302 if (Locks::mutator_lock_->IsExclusiveHeld(self)) {
303 // The mutators are already suspended. For example, a call path
304 // from SignalCatcher::HandleSigQuit().
305 rosalloc_->InspectAll(callback, arg);
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700306 if (do_null_callback_at_end) {
307 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700308 }
Hiroshi Yamauchi1cd53db2014-03-28 15:26:48 -0700309 } else if (Locks::mutator_lock_->IsSharedHeld(self)) {
310 // The mutators are not suspended yet and we have a shared access
311 // to the mutator lock. Temporarily release the shared access by
312 // transitioning to the suspend state, and suspend the mutators.
313 self->TransitionFromRunnableToSuspended(kSuspended);
314 InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end);
315 self->TransitionFromSuspendedToRunnable();
316 Locks::mutator_lock_->AssertSharedHeld(self);
317 } else {
318 // The mutators are not suspended yet. Suspend the mutators.
319 InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700320 }
321}
322
323void RosAllocSpace::RevokeThreadLocalBuffers(Thread* thread) {
324 rosalloc_->RevokeThreadLocalRuns(thread);
325}
326
327void RosAllocSpace::RevokeAllThreadLocalBuffers() {
328 rosalloc_->RevokeAllThreadLocalRuns();
329}
330
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700331void RosAllocSpace::AssertAllThreadLocalBuffersAreRevoked() {
332 if (kIsDebugBuild) {
333 rosalloc_->AssertAllThreadLocalRunsAreRevoked();
334 }
335}
336
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800337void RosAllocSpace::Clear() {
338 madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED);
339 GetLiveBitmap()->Clear();
340 GetMarkBitmap()->Clear();
341}
342
Mathieu Chartier15d34022014-02-26 17:16:38 -0800343void RosAllocSpace::Reset() {
344 // TODO: Delete and create new mspace here.
345}
346
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700347} // namespace space
348} // namespace gc
349} // namespace art