blob: 8a5e33a403138ec7db661f9cd8097913c31bf0dd [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 */
Ian Rogers1d54e732013-05-02 21:10:01 -070016#include "dlmalloc_space.h"
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070017#include "dlmalloc_space-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070018#include "gc/accounting/card_table.h"
19#include "gc/heap.h"
Mathieu Chartier0f72e412013-09-06 16:40:01 -070020#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "thread.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070024
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include <valgrind.h>
Mathieu Chartier75165d02013-09-12 14:00:31 -070026#include <memcheck/memcheck.h>
Ian Rogers1d54e732013-05-02 21:10:01 -070027
Carl Shapiro69759ea2011-07-21 18:13:35 -070028namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
30namespace space {
Ian Rogers30fab402012-01-23 15:43:46 -080031
Mathieu Chartier2fde5332012-09-14 14:51:54 -070032// TODO: Remove define macro
Ian Rogers30fab402012-01-23 15:43:46 -080033#define CHECK_MEMORY_CALL(call, args, what) \
34 do { \
35 int rc = call args; \
36 if (UNLIKELY(rc != 0)) { \
37 errno = rc; \
38 PLOG(FATAL) << # call << " failed for " << what; \
39 } \
40 } while (false)
41
Ian Rogers1d54e732013-05-02 21:10:01 -070042static const bool kPrefetchDuringDlMallocFreeList = true;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070043
Ian Rogers1d54e732013-05-02 21:10:01 -070044// Number of bytes to use as a red zone (rdz). A red zone of this size will be placed before and
45// after each allocation. 8 bytes provides long/double alignment.
46const size_t kValgrindRedZoneBytes = 8;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070047
Ian Rogers1d54e732013-05-02 21:10:01 -070048// A specialization of DlMallocSpace that provides information to valgrind wrt allocations.
49class ValgrindDlMallocSpace : public DlMallocSpace {
50 public:
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070051 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
52 void* obj_with_rdz = DlMallocSpace::AllocWithGrowth(self, num_bytes + 2 * kValgrindRedZoneBytes,
53 bytes_allocated);
Mathieu Chartier6875fd32013-08-02 11:17:31 -070054 if (obj_with_rdz == NULL) {
Ian Rogers1d54e732013-05-02 21:10:01 -070055 return NULL;
56 }
Mathieu Chartier6875fd32013-08-02 11:17:31 -070057 mirror::Object* result = reinterpret_cast<mirror::Object*>(
58 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
59 // Make redzones as no access.
60 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
61 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
62 return result;
Ian Rogers1d54e732013-05-02 21:10:01 -070063 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070064
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -070065 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
66 void* obj_with_rdz = DlMallocSpace::Alloc(self, num_bytes + 2 * kValgrindRedZoneBytes,
67 bytes_allocated);
Mathieu Chartier6875fd32013-08-02 11:17:31 -070068 if (obj_with_rdz == NULL) {
69 return NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -070070 }
Mathieu Chartier6875fd32013-08-02 11:17:31 -070071 mirror::Object* result = reinterpret_cast<mirror::Object*>(
72 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
73 // Make redzones as no access.
74 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
75 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
76 return result;
Ian Rogers1d54e732013-05-02 21:10:01 -070077 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -070078
Ian Rogers1d54e732013-05-02 21:10:01 -070079 virtual size_t AllocationSize(const mirror::Object* obj) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -070080 size_t result = DlMallocSpace::AllocationSize(reinterpret_cast<const mirror::Object*>(
81 reinterpret_cast<const byte*>(obj) - kValgrindRedZoneBytes));
82 return result - 2 * kValgrindRedZoneBytes;
Ian Rogers1d54e732013-05-02 21:10:01 -070083 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070084
Ian Rogers1d54e732013-05-02 21:10:01 -070085 virtual size_t Free(Thread* self, mirror::Object* ptr) {
86 void* obj_after_rdz = reinterpret_cast<void*>(ptr);
87 void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes;
Mathieu Chartier6875fd32013-08-02 11:17:31 -070088 // Make redzones undefined.
89 size_t allocation_size = DlMallocSpace::AllocationSize(
90 reinterpret_cast<mirror::Object*>(obj_with_rdz));
91 VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, allocation_size);
Ian Rogers1d54e732013-05-02 21:10:01 -070092 size_t freed = DlMallocSpace::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
Mathieu Chartier6875fd32013-08-02 11:17:31 -070093 return freed - 2 * kValgrindRedZoneBytes;
Ian Rogers1d54e732013-05-02 21:10:01 -070094 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070095
Ian Rogers1d54e732013-05-02 21:10:01 -070096 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
97 size_t freed = 0;
98 for (size_t i = 0; i < num_ptrs; i++) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -070099 freed += Free(self, ptrs[i]);
Ian Rogers1d54e732013-05-02 21:10:01 -0700100 }
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700101 return freed;
Ian Rogers1d54e732013-05-02 21:10:01 -0700102 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700103
Ian Rogers1d54e732013-05-02 21:10:01 -0700104 ValgrindDlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700105 byte* end, byte* limit, size_t growth_limit, size_t initial_size) :
106 DlMallocSpace(name, mem_map, mspace, begin, end, limit, growth_limit) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700107 VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size, mem_map->Size() - initial_size);
Ian Rogers1d54e732013-05-02 21:10:01 -0700108 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700109
Ian Rogers1d54e732013-05-02 21:10:01 -0700110 virtual ~ValgrindDlMallocSpace() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700111 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700112
Ian Rogers1d54e732013-05-02 21:10:01 -0700113 private:
114 DISALLOW_COPY_AND_ASSIGN(ValgrindDlMallocSpace);
115};
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700116
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700117size_t DlMallocSpace::bitmap_index_ = 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700118
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700119DlMallocSpace::DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700120 byte* end, byte* limit, size_t growth_limit)
121 : ContinuousMemMapAllocSpace(name, mem_map, begin, end, limit, kGcRetentionPolicyAlwaysCollect),
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700122 recent_free_pos_(0), total_bytes_freed_(0), total_objects_freed_(0),
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700123 lock_("allocation space lock", kAllocSpaceLock), mspace_(mspace),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700124 growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700125 CHECK(mspace != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700126 size_t bitmap_index = bitmap_index_++;
Ian Rogers1d54e732013-05-02 21:10:01 -0700127 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700128 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->Begin())));
129 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->End())));
Ian Rogers1d54e732013-05-02 21:10:01 -0700130 live_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800131 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700132 Begin(), Capacity()));
133 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
Ian Rogers1d54e732013-05-02 21:10:01 -0700134 mark_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800135 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700136 Begin(), Capacity()));
137 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700138 for (auto& freed : recent_freed_objects_) {
139 freed.first = nullptr;
140 freed.second = nullptr;
141 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700142}
143
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700144DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t
145 growth_limit, size_t capacity, byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800146 // Memory we promise to dlmalloc before it asks for morecore.
147 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
148 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
149 // size of the large allocation) will be greater than the footprint limit.
150 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -0800151 uint64_t start_time = 0;
152 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
153 start_time = NanoTime();
154 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -0800155 << " initial_size=" << PrettySize(initial_size)
156 << " growth_limit=" << PrettySize(growth_limit)
157 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -0800158 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700159 }
Ian Rogers30fab402012-01-23 15:43:46 -0800160
161 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -0800162 if (starting_size > initial_size) {
163 initial_size = starting_size;
164 }
Ian Rogers30fab402012-01-23 15:43:46 -0800165 if (initial_size > growth_limit) {
166 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -0800167 << PrettySize(initial_size) << ") is larger than its capacity ("
168 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800169 return NULL;
170 }
171 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800172 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
173 << PrettySize(growth_limit) << ") is larger than the capacity ("
174 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800175 return NULL;
176 }
177
178 // Page align growth limit and capacity which will be used to manage mmapped storage
179 growth_limit = RoundUp(growth_limit, kPageSize);
180 capacity = RoundUp(capacity, kPageSize);
181
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700182 std::string error_msg;
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700183 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700184 PROT_READ | PROT_WRITE, &error_msg));
Ian Rogers30fab402012-01-23 15:43:46 -0800185 if (mem_map.get() == NULL) {
186 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700187 << PrettySize(capacity) << ": " << error_msg;
Ian Rogers30fab402012-01-23 15:43:46 -0800188 return NULL;
189 }
190
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700191 void* mspace = CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800192 if (mspace == NULL) {
193 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
194 return NULL;
195 }
196
Ian Rogers3bb17a62012-01-27 23:56:44 -0800197 // Protect memory beyond the initial size.
198 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800199 if (capacity - initial_size > 0) {
200 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
201 }
202
203 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700204 MemMap* mem_map_ptr = mem_map.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700205 DlMallocSpace* space;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700206 byte* begin = mem_map_ptr->Begin();
Ian Rogers1d54e732013-05-02 21:10:01 -0700207 if (RUNNING_ON_VALGRIND > 0) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700208 space = new ValgrindDlMallocSpace(name, mem_map_ptr, mspace, begin, end, begin + capacity,
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700209 growth_limit, initial_size);
Ian Rogers1d54e732013-05-02 21:10:01 -0700210 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700211 space = new DlMallocSpace(name, mem_map_ptr, mspace, begin, end, begin + capacity, growth_limit);
Ian Rogers1d54e732013-05-02 21:10:01 -0700212 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700213 // We start out with only the initial size possibly containing objects.
Ian Rogers30fab402012-01-23 15:43:46 -0800214 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800215 LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time)
216 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800217 }
218 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700219}
220
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700221void* DlMallocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800222 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800224 // create mspace using our backing storage starting at begin and with a footprint of
225 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
226 // morecore_start bytes of memory is exhaused morecore will be called.
227 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800229 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800230 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700231 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800232 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700233 }
234 return msp;
235}
236
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700237void DlMallocSpace::SwapBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700238 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700239 // Swap names to get more descriptive diagnostics.
Ian Rogers1d54e732013-05-02 21:10:01 -0700240 std::string temp_name(live_bitmap_->GetName());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700241 live_bitmap_->SetName(mark_bitmap_->GetName());
242 mark_bitmap_->SetName(temp_name);
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700243}
244
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700245mirror::Object* DlMallocSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
246 return AllocNonvirtual(self, num_bytes, bytes_allocated);
247}
248
249mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
250 mirror::Object* result;
251 {
252 MutexLock mu(self, lock_);
253 // Grow as much as possible within the mspace.
254 size_t max_allowed = Capacity();
255 mspace_set_footprint_limit(mspace_, max_allowed);
256 // Try the allocation.
257 result = AllocWithoutGrowthLocked(num_bytes, bytes_allocated);
258 // Shrink back down as small as possible.
259 size_t footprint = mspace_footprint(mspace_);
260 mspace_set_footprint_limit(mspace_, footprint);
jeffhaoc1160702011-10-27 15:48:45 -0700261 }
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700262 if (result != NULL) {
263 // Zero freshly allocated memory, done while not holding the space's lock.
264 memset(result, 0, num_bytes);
265 }
Ian Rogers30fab402012-01-23 15:43:46 -0800266 // Return the new allocation or NULL.
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700267 CHECK(!kDebugSpaces || result == NULL || Contains(result));
Ian Rogers30fab402012-01-23 15:43:46 -0800268 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700269}
270
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700271void DlMallocSpace::SetGrowthLimit(size_t growth_limit) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700272 growth_limit = RoundUp(growth_limit, kPageSize);
273 growth_limit_ = growth_limit;
274 if (Size() > growth_limit_) {
275 end_ = begin_ + growth_limit;
276 }
277}
278
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700279DlMallocSpace* DlMallocSpace::CreateZygoteSpace(const char* alloc_space_name) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700280 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
Ian Rogers1d54e732013-05-02 21:10:01 -0700281 DCHECK(IsAligned<accounting::CardTable::kCardSize>(begin_));
282 DCHECK(IsAligned<accounting::CardTable::kCardSize>(end_));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700283 DCHECK(IsAligned<kPageSize>(begin_));
284 DCHECK(IsAligned<kPageSize>(end_));
285 size_t size = RoundUp(Size(), kPageSize);
286 // Trim the heap so that we minimize the size of the Zygote space.
287 Trim();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700288 // TODO: Not hardcode these in?
289 const size_t starting_size = kPageSize;
290 const size_t initial_size = 2 * MB;
291 // Remaining size is for the new alloc space.
292 const size_t growth_limit = growth_limit_ - size;
293 const size_t capacity = Capacity() - size;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700294 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n"
295 << "End " << reinterpret_cast<const void*>(end_) << "\n"
296 << "Size " << size << "\n"
297 << "GrowthLimit " << growth_limit_ << "\n"
298 << "Capacity " << Capacity();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700299 SetGrowthLimit(RoundUp(size, kPageSize));
300 SetFootprintLimit(RoundUp(size, kPageSize));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700301 // FIXME: Do we need reference counted pointers here?
302 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700303 VLOG(heap) << "Creating new AllocSpace: ";
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700304 VLOG(heap) << "Size " << GetMemMap()->Size();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700305 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
306 VLOG(heap) << "Capacity " << PrettySize(capacity);
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700307 // Remap the tail.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700308 std::string error_msg;
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -0700309 UniquePtr<MemMap> mem_map(GetMemMap()->RemapAtEnd(end_, alloc_space_name,
310 PROT_READ | PROT_WRITE, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700311 CHECK(mem_map.get() != nullptr) << error_msg;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700312 void* mspace = CreateMallocSpace(end_, starting_size, initial_size);
313 // Protect memory beyond the initial size.
314 byte* end = mem_map->Begin() + starting_size;
315 if (capacity - initial_size > 0) {
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700316 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), alloc_space_name);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700317 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700318 DlMallocSpace* alloc_space =
Mathieu Chartier590fee92013-09-13 13:46:47 -0700319 new DlMallocSpace(alloc_space_name, mem_map.release(), mspace, end_, end, limit_,
320 growth_limit);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700321 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
322 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
323 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
324 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700325 VLOG(heap) << "zygote space creation done";
326 return alloc_space;
327}
328
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700329mirror::Class* DlMallocSpace::FindRecentFreedObject(const mirror::Object* obj) {
330 size_t pos = recent_free_pos_;
331 // Start at the most recently freed object and work our way back since there may be duplicates
332 // caused by dlmalloc reusing memory.
333 if (kRecentFreeCount > 0) {
334 for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) {
335 pos = pos != 0 ? pos - 1 : kRecentFreeMask;
336 if (recent_freed_objects_[pos].first == obj) {
337 return recent_freed_objects_[pos].second;
338 }
339 }
340 }
341 return nullptr;
342}
343
344void DlMallocSpace::RegisterRecentFree(mirror::Object* ptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700345 recent_freed_objects_[recent_free_pos_] = std::make_pair(ptr, ptr->GetClass());
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700346 recent_free_pos_ = (recent_free_pos_ + 1) & kRecentFreeMask;
347}
348
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700350 MutexLock mu(self, lock_);
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700351 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700352 CHECK(ptr != NULL);
353 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
354 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800355 const size_t bytes_freed = InternalAllocationSize(ptr);
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700356 total_bytes_freed_ += bytes_freed;
357 ++total_objects_freed_;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700358 if (kRecentFreeCount > 0) {
359 RegisterRecentFree(ptr);
360 }
Ian Rogers30fab402012-01-23 15:43:46 -0800361 mspace_free(mspace_, ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700362 return bytes_freed;
Ian Rogers30fab402012-01-23 15:43:46 -0800363}
364
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800366 DCHECK(ptrs != NULL);
367
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700368 // Don't need the lock to calculate the size of the freed pointers.
369 size_t bytes_freed = 0;
370 for (size_t i = 0; i < num_ptrs; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 mirror::Object* ptr = ptrs[i];
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800372 const size_t look_ahead = 8;
373 if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) {
374 // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
375 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
376 }
377 bytes_freed += InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700378 }
379
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700380 if (kRecentFreeCount > 0) {
381 MutexLock mu(self, lock_);
382 for (size_t i = 0; i < num_ptrs; i++) {
383 RegisterRecentFree(ptrs[i]);
384 }
385 }
386
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700387 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700388 size_t num_broken_ptrs = 0;
389 for (size_t i = 0; i < num_ptrs; i++) {
390 if (!Contains(ptrs[i])) {
391 num_broken_ptrs++;
392 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
393 } else {
394 size_t size = mspace_usable_size(ptrs[i]);
395 memset(ptrs[i], 0xEF, size);
396 }
Ian Rogers30fab402012-01-23 15:43:46 -0800397 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700398 CHECK_EQ(num_broken_ptrs, 0u);
Ian Rogers30fab402012-01-23 15:43:46 -0800399 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800400
401 {
402 MutexLock mu(self, lock_);
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700403 total_bytes_freed_ += bytes_freed;
404 total_objects_freed_ += num_ptrs;
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800405 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
406 return bytes_freed;
407 }
Ian Rogers30fab402012-01-23 15:43:46 -0800408}
409
410// Callback from dlmalloc when it needs to increase the footprint
411extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800412 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700413 DCHECK_EQ(heap->GetNonMovingSpace()->GetMspace(), mspace);
414 return heap->GetNonMovingSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800415}
416
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700417void* DlMallocSpace::MoreCore(intptr_t increment) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700418 lock_.AssertHeld(Thread::Current());
Ian Rogers30fab402012-01-23 15:43:46 -0800419 byte* original_end = end_;
420 if (increment != 0) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700421 VLOG(heap) << "DlMallocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800422 byte* new_end = original_end + increment;
423 if (increment > 0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800424 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
425 // by mspace_set_footprint_limit.
426 CHECK_LE(new_end, Begin() + Capacity());
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700427 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800428 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800429 // Should never be asked for negative footprint (ie before begin)
430 CHECK_GT(original_end + increment, Begin());
Ian Rogers30fab402012-01-23 15:43:46 -0800431 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800432 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
433 // expensive (note the same isn't true for giving permissions to a page as the protected
434 // page shouldn't be in a TLB). We should investigate performance impact of just
435 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
436 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800437 size_t size = -increment;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700438 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName());
439 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800440 }
441 // Update end_
442 end_ = new_end;
443 }
444 return original_end;
445}
446
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800447// Virtual functions can't get inlined.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448inline size_t DlMallocSpace::InternalAllocationSize(const mirror::Object* obj) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700449 return AllocationSizeNonvirtual(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800450}
451
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800452size_t DlMallocSpace::AllocationSize(const mirror::Object* obj) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800453 return InternalAllocationSize(obj);
454}
455
Ian Rogers48931882013-01-22 14:35:16 -0800456size_t DlMallocSpace::Trim() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700457 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700458 // Trim to release memory at the end of the space.
459 mspace_trim(mspace_, 0);
460 // Visit space looking for page-sized holes to advise the kernel we don't need.
Ian Rogers48931882013-01-22 14:35:16 -0800461 size_t reclaimed = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700462 mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed);
Ian Rogers48931882013-01-22 14:35:16 -0800463 return reclaimed;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700464}
Ian Rogers30fab402012-01-23 15:43:46 -0800465
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700466void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Ian Rogers30fab402012-01-23 15:43:46 -0800467 void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700468 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800469 mspace_inspect_all(mspace_, callback, arg);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700470 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800471}
472
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700473size_t DlMallocSpace::GetFootprint() {
474 MutexLock mu(Thread::Current(), lock_);
475 return mspace_footprint(mspace_);
476}
477
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700478size_t DlMallocSpace::GetFootprintLimit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700479 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800480 return mspace_footprint_limit(mspace_);
481}
482
Mathieu Chartier590fee92013-09-13 13:46:47 -0700483// Returns the old mark bitmap.
484accounting::SpaceBitmap* DlMallocSpace::BindLiveToMarkBitmap() {
485 accounting::SpaceBitmap* live_bitmap = GetLiveBitmap();
486 accounting::SpaceBitmap* mark_bitmap = mark_bitmap_.release();
487 temp_bitmap_.reset(mark_bitmap);
488 mark_bitmap_.reset(live_bitmap);
489 return mark_bitmap;
490}
491
492bool DlMallocSpace::HasBoundBitmaps() const {
493 return temp_bitmap_.get() != nullptr;
494}
495
496void DlMallocSpace::UnBindBitmaps() {
497 CHECK(HasBoundBitmaps());
498 // At this point, the temp_bitmap holds our old mark bitmap.
499 accounting::SpaceBitmap* new_bitmap = temp_bitmap_.release();
500 CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get());
501 mark_bitmap_.reset(new_bitmap);
502 DCHECK(temp_bitmap_.get() == NULL);
503}
504
505
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700506void DlMallocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700507 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700508 VLOG(heap) << "DLMallocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800509 // Compare against the actual footprint, rather than the Size(), because the heap may not have
510 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800511 size_t current_space_size = mspace_footprint(mspace_);
512 if (new_size < current_space_size) {
513 // Don't let the space grow any more.
514 new_size = current_space_size;
515 }
516 mspace_set_footprint_limit(mspace_, new_size);
517}
518
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700519void DlMallocSpace::Dump(std::ostream& os) const {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700520 os << GetType()
Ian Rogers1d54e732013-05-02 21:10:01 -0700521 << " begin=" << reinterpret_cast<void*>(Begin())
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700522 << ",end=" << reinterpret_cast<void*>(End())
523 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
524 << ",name=\"" << GetName() << "\"]";
525}
526
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700527uint64_t DlMallocSpace::GetBytesAllocated() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700528 if (mspace_ != nullptr) {
529 MutexLock mu(Thread::Current(), lock_);
530 size_t bytes_allocated = 0;
531 mspace_inspect_all(mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated);
532 return bytes_allocated;
533 } else {
534 return Size();
535 }
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700536}
537
538uint64_t DlMallocSpace::GetObjectsAllocated() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700539 if (mspace_ != nullptr) {
540 MutexLock mu(Thread::Current(), lock_);
541 size_t objects_allocated = 0;
542 mspace_inspect_all(mspace_, DlmallocObjectsAllocatedCallback, &objects_allocated);
543 return objects_allocated;
544 } else {
545 return 0;
546 }
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700547}
548
Ian Rogers1d54e732013-05-02 21:10:01 -0700549} // namespace space
550} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700551} // namespace art