blob: a4e6edadef0029bafcab859a8bd5f17ca47b66c4 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070016
Ian Rogers1d54e732013-05-02 21:10:01 -070017#include "dlmalloc_space.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070018
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070019#include "dlmalloc_space-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070020#include "gc/accounting/card_table.h"
21#include "gc/heap.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070022#include "mirror/class-inl.h"
Mathieu Chartier0f72e412013-09-06 16:40:01 -070023#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "thread.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070026#include "thread_list.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include <valgrind.h>
Mathieu Chartier75165d02013-09-12 14:00:31 -070030#include <memcheck/memcheck.h>
Ian Rogers1d54e732013-05-02 21:10:01 -070031
Carl Shapiro69759ea2011-07-21 18:13:35 -070032namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070033namespace gc {
34namespace space {
Ian Rogers30fab402012-01-23 15:43:46 -080035
Ian Rogers1d54e732013-05-02 21:10:01 -070036static const bool kPrefetchDuringDlMallocFreeList = true;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070037
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070038DlMallocSpace::DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070039 byte* end, byte* limit, size_t growth_limit)
40 : MallocSpace(name, mem_map, begin, end, limit, growth_limit),
Hiroshi Yamauchie48780b2013-12-17 17:19:53 -080041 mspace_(mspace), mspace_for_alloc_(mspace) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070042 CHECK(mspace != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070043}
44
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070045DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t growth_limit,
46 size_t capacity, byte* requested_begin) {
Ian Rogers30fab402012-01-23 15:43:46 -080047 uint64_t start_time = 0;
48 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
49 start_time = NanoTime();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070050 VLOG(startup) << "DlMallocSpace::Create entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -080051 << " initial_size=" << PrettySize(initial_size)
52 << " growth_limit=" << PrettySize(growth_limit)
53 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -080054 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -070055 }
Ian Rogers30fab402012-01-23 15:43:46 -080056
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070057 // Memory we promise to dlmalloc before it asks for morecore.
58 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
59 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
60 // size of the large allocation) will be greater than the footprint limit.
61 size_t starting_size = kPageSize;
62 MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity,
63 requested_begin);
64 if (mem_map == NULL) {
65 LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size "
66 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -080067 return NULL;
68 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070069 void* mspace = CreateMspace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -080070 if (mspace == NULL) {
71 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
72 return NULL;
73 }
74
Ian Rogers3bb17a62012-01-27 23:56:44 -080075 // Protect memory beyond the initial size.
76 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -080077 if (capacity - initial_size > 0) {
78 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
79 }
80
81 // Everything is set so record in immutable structure and leave
Ian Rogers1d54e732013-05-02 21:10:01 -070082 DlMallocSpace* space;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070083 byte* begin = mem_map->Begin();
Ian Rogers1d54e732013-05-02 21:10:01 -070084 if (RUNNING_ON_VALGRIND > 0) {
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -080085 space = new ValgrindMallocSpace<DlMallocSpace, void*>(
86 name, mem_map, mspace, begin, end, begin + capacity, growth_limit, initial_size);
Ian Rogers1d54e732013-05-02 21:10:01 -070087 } else {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070088 space = new DlMallocSpace(name, mem_map, mspace, begin, end, begin + capacity, growth_limit);
Ian Rogers1d54e732013-05-02 21:10:01 -070089 }
Mathieu Chartier590fee92013-09-13 13:46:47 -070090 // We start out with only the initial size possibly containing objects.
Ian Rogers30fab402012-01-23 15:43:46 -080091 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070092 LOG(INFO) << "DlMallocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time)
Ian Rogers3bb17a62012-01-27 23:56:44 -080093 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -080094 }
95 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -070096}
97
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070098void* DlMallocSpace::CreateMspace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -080099 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700100 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800101 // create mspace using our backing storage starting at begin and with a footprint of
102 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
103 // morecore_start bytes of memory is exhaused morecore will be called.
104 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700105 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800106 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800107 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800109 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700110 }
111 return msp;
112}
113
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700114mirror::Object* DlMallocSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
115 return AllocNonvirtual(self, num_bytes, bytes_allocated);
116}
117
118mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
119 mirror::Object* result;
120 {
121 MutexLock mu(self, lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700122 // Grow as much as possible within the space.
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700123 size_t max_allowed = Capacity();
124 mspace_set_footprint_limit(mspace_, max_allowed);
125 // Try the allocation.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700126 result = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated);
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700127 // Shrink back down as small as possible.
128 size_t footprint = mspace_footprint(mspace_);
129 mspace_set_footprint_limit(mspace_, footprint);
jeffhaoc1160702011-10-27 15:48:45 -0700130 }
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700131 if (result != NULL) {
132 // Zero freshly allocated memory, done while not holding the space's lock.
133 memset(result, 0, num_bytes);
134 }
Ian Rogers30fab402012-01-23 15:43:46 -0800135 // Return the new allocation or NULL.
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700136 CHECK(!kDebugSpaces || result == NULL || Contains(result));
Ian Rogers30fab402012-01-23 15:43:46 -0800137 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700138}
139
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700140MallocSpace* DlMallocSpace::CreateInstance(const std::string& name, MemMap* mem_map, void* allocator, byte* begin, byte* end,
141 byte* limit, size_t growth_limit) {
142 return new DlMallocSpace(name, mem_map, allocator, begin, end, limit, growth_limit);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700143}
144
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700146 MutexLock mu(self, lock_);
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700147 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700148 CHECK(ptr != NULL);
149 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
150 }
Hiroshi Yamauchie48780b2013-12-17 17:19:53 -0800151 const size_t bytes_freed = AllocationSizeNonvirtual(ptr);
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700152 if (kRecentFreeCount > 0) {
153 RegisterRecentFree(ptr);
154 }
Ian Rogers30fab402012-01-23 15:43:46 -0800155 mspace_free(mspace_, ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700156 return bytes_freed;
Ian Rogers30fab402012-01-23 15:43:46 -0800157}
158
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800160 DCHECK(ptrs != NULL);
161
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700162 // Don't need the lock to calculate the size of the freed pointers.
163 size_t bytes_freed = 0;
164 for (size_t i = 0; i < num_ptrs; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 mirror::Object* ptr = ptrs[i];
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800166 const size_t look_ahead = 8;
167 if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) {
168 // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
169 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
170 }
Hiroshi Yamauchie48780b2013-12-17 17:19:53 -0800171 bytes_freed += AllocationSizeNonvirtual(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700172 }
173
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700174 if (kRecentFreeCount > 0) {
175 MutexLock mu(self, lock_);
176 for (size_t i = 0; i < num_ptrs; i++) {
177 RegisterRecentFree(ptrs[i]);
178 }
179 }
180
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700181 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700182 size_t num_broken_ptrs = 0;
183 for (size_t i = 0; i < num_ptrs; i++) {
184 if (!Contains(ptrs[i])) {
185 num_broken_ptrs++;
186 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
187 } else {
188 size_t size = mspace_usable_size(ptrs[i]);
189 memset(ptrs[i], 0xEF, size);
190 }
Ian Rogers30fab402012-01-23 15:43:46 -0800191 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700192 CHECK_EQ(num_broken_ptrs, 0u);
Ian Rogers30fab402012-01-23 15:43:46 -0800193 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800194
195 {
196 MutexLock mu(self, lock_);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800197 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
198 return bytes_freed;
199 }
Ian Rogers30fab402012-01-23 15:43:46 -0800200}
201
202// Callback from dlmalloc when it needs to increase the footprint
203extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800204 Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700205 DCHECK(heap->GetNonMovingSpace()->IsDlMallocSpace());
206 DCHECK_EQ(heap->GetNonMovingSpace()->AsDlMallocSpace()->GetMspace(), mspace);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700207 return heap->GetNonMovingSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800208}
209
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210size_t DlMallocSpace::AllocationSize(const mirror::Object* obj) {
Hiroshi Yamauchie48780b2013-12-17 17:19:53 -0800211 return AllocationSizeNonvirtual(obj);
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800212}
213
Ian Rogers48931882013-01-22 14:35:16 -0800214size_t DlMallocSpace::Trim() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700215 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700216 // Trim to release memory at the end of the space.
217 mspace_trim(mspace_, 0);
218 // Visit space looking for page-sized holes to advise the kernel we don't need.
Ian Rogers48931882013-01-22 14:35:16 -0800219 size_t reclaimed = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700220 mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed);
Ian Rogers48931882013-01-22 14:35:16 -0800221 return reclaimed;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700222}
Ian Rogers30fab402012-01-23 15:43:46 -0800223
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700224void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Ian Rogers30fab402012-01-23 15:43:46 -0800225 void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700226 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800227 mspace_inspect_all(mspace_, callback, arg);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700228 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800229}
230
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700231size_t DlMallocSpace::GetFootprint() {
232 MutexLock mu(Thread::Current(), lock_);
233 return mspace_footprint(mspace_);
234}
235
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700236size_t DlMallocSpace::GetFootprintLimit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700237 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800238 return mspace_footprint_limit(mspace_);
239}
240
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700241void DlMallocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700242 MutexLock mu(Thread::Current(), lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700243 VLOG(heap) << "DlMallocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800244 // Compare against the actual footprint, rather than the Size(), because the heap may not have
245 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800246 size_t current_space_size = mspace_footprint(mspace_);
247 if (new_size < current_space_size) {
248 // Don't let the space grow any more.
249 new_size = current_space_size;
250 }
251 mspace_set_footprint_limit(mspace_, new_size);
252}
253
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700254uint64_t DlMallocSpace::GetBytesAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800255 MutexLock mu(Thread::Current(), lock_);
256 size_t bytes_allocated = 0;
257 mspace_inspect_all(mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated);
258 return bytes_allocated;
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700259}
260
261uint64_t DlMallocSpace::GetObjectsAllocated() {
Hiroshi Yamauchi4ce1f002013-11-18 14:49:09 -0800262 MutexLock mu(Thread::Current(), lock_);
263 size_t objects_allocated = 0;
264 mspace_inspect_all(mspace_, DlmallocObjectsAllocatedCallback, &objects_allocated);
265 return objects_allocated;
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700266}
267
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700268#ifndef NDEBUG
269void DlMallocSpace::CheckMoreCoreForPrecondition() {
270 lock_.AssertHeld(Thread::Current());
271}
272#endif
273
Ian Rogers1d54e732013-05-02 21:10:01 -0700274} // namespace space
275} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700276} // namespace art