blob: d539aa269a964d848c8f726c064113d597187bc3 [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"
17#include "gc/accounting/card_table.h"
18#include "gc/heap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "thread.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
Ian Rogers1d54e732013-05-02 21:10:01 -070023#include <valgrind.h>
Mathieu Chartier6875fd32013-08-02 11:17:31 -070024#include <../memcheck/memcheck.h>
Ian Rogers1d54e732013-05-02 21:10:01 -070025
Carl Shapiro69759ea2011-07-21 18:13:35 -070026namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace space {
Ian Rogers30fab402012-01-23 15:43:46 -080029
Mathieu Chartier2fde5332012-09-14 14:51:54 -070030// TODO: Remove define macro
Ian Rogers30fab402012-01-23 15:43:46 -080031#define CHECK_MEMORY_CALL(call, args, what) \
32 do { \
33 int rc = call args; \
34 if (UNLIKELY(rc != 0)) { \
35 errno = rc; \
36 PLOG(FATAL) << # call << " failed for " << what; \
37 } \
38 } while (false)
39
Ian Rogers1d54e732013-05-02 21:10:01 -070040static const bool kPrefetchDuringDlMallocFreeList = true;
Mathieu Chartier2fde5332012-09-14 14:51:54 -070041
Ian Rogers1d54e732013-05-02 21:10:01 -070042// Number of bytes to use as a red zone (rdz). A red zone of this size will be placed before and
43// after each allocation. 8 bytes provides long/double alignment.
44const size_t kValgrindRedZoneBytes = 8;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070045
Ian Rogers1d54e732013-05-02 21:10:01 -070046// A specialization of DlMallocSpace that provides information to valgrind wrt allocations.
47class ValgrindDlMallocSpace : public DlMallocSpace {
48 public:
49 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -070050 void* obj_with_rdz = DlMallocSpace::AllocWithGrowth(self, num_bytes + 2 * kValgrindRedZoneBytes);
51 if (obj_with_rdz == NULL) {
Ian Rogers1d54e732013-05-02 21:10:01 -070052 return NULL;
53 }
Mathieu Chartier6875fd32013-08-02 11:17:31 -070054 mirror::Object* result = reinterpret_cast<mirror::Object*>(
55 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
56 // Make redzones as no access.
57 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
58 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
59 return result;
Ian Rogers1d54e732013-05-02 21:10:01 -070060 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070061
Ian Rogers1d54e732013-05-02 21:10:01 -070062 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -070063 void* obj_with_rdz = DlMallocSpace::Alloc(self, num_bytes + 2 * kValgrindRedZoneBytes);
64 if (obj_with_rdz == NULL) {
65 return NULL;
Ian Rogers1d54e732013-05-02 21:10:01 -070066 }
Mathieu Chartier6875fd32013-08-02 11:17:31 -070067 mirror::Object* result = reinterpret_cast<mirror::Object*>(
68 reinterpret_cast<byte*>(obj_with_rdz) + kValgrindRedZoneBytes);
69 // Make redzones as no access.
70 VALGRIND_MAKE_MEM_NOACCESS(obj_with_rdz, kValgrindRedZoneBytes);
71 VALGRIND_MAKE_MEM_NOACCESS(reinterpret_cast<byte*>(result) + num_bytes, kValgrindRedZoneBytes);
72 return result;
Ian Rogers1d54e732013-05-02 21:10:01 -070073 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -070074
Ian Rogers1d54e732013-05-02 21:10:01 -070075 virtual size_t AllocationSize(const mirror::Object* obj) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -070076 size_t result = DlMallocSpace::AllocationSize(reinterpret_cast<const mirror::Object*>(
77 reinterpret_cast<const byte*>(obj) - kValgrindRedZoneBytes));
78 return result - 2 * kValgrindRedZoneBytes;
Ian Rogers1d54e732013-05-02 21:10:01 -070079 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070080
Ian Rogers1d54e732013-05-02 21:10:01 -070081 virtual size_t Free(Thread* self, mirror::Object* ptr) {
82 void* obj_after_rdz = reinterpret_cast<void*>(ptr);
83 void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes;
Mathieu Chartier6875fd32013-08-02 11:17:31 -070084 // Make redzones undefined.
85 size_t allocation_size = DlMallocSpace::AllocationSize(
86 reinterpret_cast<mirror::Object*>(obj_with_rdz));
87 VALGRIND_MAKE_MEM_UNDEFINED(obj_with_rdz, allocation_size);
Ian Rogers1d54e732013-05-02 21:10:01 -070088 size_t freed = DlMallocSpace::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
Mathieu Chartier6875fd32013-08-02 11:17:31 -070089 return freed - 2 * kValgrindRedZoneBytes;
Ian Rogers1d54e732013-05-02 21:10:01 -070090 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070091
Ian Rogers1d54e732013-05-02 21:10:01 -070092 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
93 size_t freed = 0;
94 for (size_t i = 0; i < num_ptrs; i++) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -070095 freed += Free(self, ptrs[i]);
Ian Rogers1d54e732013-05-02 21:10:01 -070096 }
Mathieu Chartier6875fd32013-08-02 11:17:31 -070097 return freed;
Ian Rogers1d54e732013-05-02 21:10:01 -070098 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -070099
Ian Rogers1d54e732013-05-02 21:10:01 -0700100 ValgrindDlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700101 byte* end, size_t growth_limit, size_t initial_size) :
Ian Rogers1d54e732013-05-02 21:10:01 -0700102 DlMallocSpace(name, mem_map, mspace, begin, end, growth_limit) {
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700103 VALGRIND_MAKE_MEM_UNDEFINED(mem_map->Begin() + initial_size, mem_map->Size() - initial_size);
Ian Rogers1d54e732013-05-02 21:10:01 -0700104 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700105
Ian Rogers1d54e732013-05-02 21:10:01 -0700106 virtual ~ValgrindDlMallocSpace() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700107 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700108
Ian Rogers1d54e732013-05-02 21:10:01 -0700109 private:
110 DISALLOW_COPY_AND_ASSIGN(ValgrindDlMallocSpace);
111};
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700112
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700113size_t DlMallocSpace::bitmap_index_ = 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700114
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700115DlMallocSpace::DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700116 byte* end, size_t growth_limit)
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700117 : MemMapSpace(name, mem_map, end - begin, kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700118 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
119 total_objects_allocated_(0), lock_("allocation space lock", kAllocSpaceLock), mspace_(mspace),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700120 growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700121 CHECK(mspace != NULL);
122
123 size_t bitmap_index = bitmap_index_++;
124
Ian Rogers1d54e732013-05-02 21:10:01 -0700125 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700126 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->Begin())));
127 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->End())));
Ian Rogers1d54e732013-05-02 21:10:01 -0700128 live_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800129 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700130 Begin(), Capacity()));
131 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
132
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 mark_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800134 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700135 Begin(), Capacity()));
136 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
137}
138
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700139DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t
140 growth_limit, size_t capacity, byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800141 // Memory we promise to dlmalloc before it asks for morecore.
142 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
143 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
144 // size of the large allocation) will be greater than the footprint limit.
145 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -0800146 uint64_t start_time = 0;
147 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
148 start_time = NanoTime();
149 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -0800150 << " initial_size=" << PrettySize(initial_size)
151 << " growth_limit=" << PrettySize(growth_limit)
152 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -0800153 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154 }
Ian Rogers30fab402012-01-23 15:43:46 -0800155
156 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -0800157 if (starting_size > initial_size) {
158 initial_size = starting_size;
159 }
Ian Rogers30fab402012-01-23 15:43:46 -0800160 if (initial_size > growth_limit) {
161 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -0800162 << PrettySize(initial_size) << ") is larger than its capacity ("
163 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800164 return NULL;
165 }
166 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800167 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
168 << PrettySize(growth_limit) << ") is larger than the capacity ("
169 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800170 return NULL;
171 }
172
173 // Page align growth limit and capacity which will be used to manage mmapped storage
174 growth_limit = RoundUp(growth_limit, kPageSize);
175 capacity = RoundUp(capacity, kPageSize);
176
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700177 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity,
178 PROT_READ | PROT_WRITE));
Ian Rogers30fab402012-01-23 15:43:46 -0800179 if (mem_map.get() == NULL) {
180 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers3bb17a62012-01-27 23:56:44 -0800181 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -0800182 return NULL;
183 }
184
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700185 void* mspace = CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800186 if (mspace == NULL) {
187 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
188 return NULL;
189 }
190
Ian Rogers3bb17a62012-01-27 23:56:44 -0800191 // Protect memory beyond the initial size.
192 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800193 if (capacity - initial_size > 0) {
194 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
195 }
196
197 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700198 MemMap* mem_map_ptr = mem_map.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700199 DlMallocSpace* space;
200 if (RUNNING_ON_VALGRIND > 0) {
201 space = new ValgrindDlMallocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end,
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700202 growth_limit, initial_size);
Ian Rogers1d54e732013-05-02 21:10:01 -0700203 } else {
204 space = new DlMallocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end, growth_limit);
205 }
Ian Rogers30fab402012-01-23 15:43:46 -0800206 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800207 LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time)
208 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800209 }
210 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211}
212
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700213void* DlMallocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800214 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700215 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800216 // create mspace using our backing storage starting at begin and with a footprint of
217 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
218 // morecore_start bytes of memory is exhaused morecore will be called.
219 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800221 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800222 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800224 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225 }
226 return msp;
227}
228
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700229void DlMallocSpace::SwapBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700230 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700231 // Swap names to get more descriptive diagnostics.
Ian Rogers1d54e732013-05-02 21:10:01 -0700232 std::string temp_name(live_bitmap_->GetName());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700233 live_bitmap_->SetName(mark_bitmap_->GetName());
234 mark_bitmap_->SetName(temp_name);
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700235}
236
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237mirror::Object* DlMallocSpace::AllocWithoutGrowthLocked(size_t num_bytes) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 mirror::Object* result = reinterpret_cast<mirror::Object*>(mspace_calloc(mspace_, 1, num_bytes));
Ian Rogers1d54e732013-05-02 21:10:01 -0700239 if (result != NULL) {
240 if (kDebugSpaces) {
241 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
242 << ") not in bounds of allocation space " << *this;
243 }
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700244 size_t allocation_size = InternalAllocationSize(result);
Ian Rogers1d54e732013-05-02 21:10:01 -0700245 num_bytes_allocated_ += allocation_size;
246 total_bytes_allocated_ += allocation_size;
247 ++total_objects_allocated_;
248 ++num_objects_allocated_;
jeffhaoc1160702011-10-27 15:48:45 -0700249 }
Ian Rogers30fab402012-01-23 15:43:46 -0800250 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700251}
252
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253mirror::Object* DlMallocSpace::Alloc(Thread* self, size_t num_bytes) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700254 MutexLock mu(self, lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700255 return AllocWithoutGrowthLocked(num_bytes);
256}
257
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700259 MutexLock mu(self, lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800260 // Grow as much as possible within the mspace.
261 size_t max_allowed = Capacity();
262 mspace_set_footprint_limit(mspace_, max_allowed);
263 // Try the allocation.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 mirror::Object* result = AllocWithoutGrowthLocked(num_bytes);
Ian Rogers30fab402012-01-23 15:43:46 -0800265 // Shrink back down as small as possible.
266 size_t footprint = mspace_footprint(mspace_);
267 mspace_set_footprint_limit(mspace_, footprint);
268 // Return the new allocation or NULL.
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700269 CHECK(!kDebugSpaces || result == NULL || Contains(result));
Ian Rogers30fab402012-01-23 15:43:46 -0800270 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700271}
272
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700273void DlMallocSpace::SetGrowthLimit(size_t growth_limit) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700274 growth_limit = RoundUp(growth_limit, kPageSize);
275 growth_limit_ = growth_limit;
276 if (Size() > growth_limit_) {
277 end_ = begin_ + growth_limit;
278 }
279}
280
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700281DlMallocSpace* DlMallocSpace::CreateZygoteSpace(const char* alloc_space_name) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700282 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
Ian Rogers1d54e732013-05-02 21:10:01 -0700283 DCHECK(IsAligned<accounting::CardTable::kCardSize>(begin_));
284 DCHECK(IsAligned<accounting::CardTable::kCardSize>(end_));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700285 DCHECK(IsAligned<kPageSize>(begin_));
286 DCHECK(IsAligned<kPageSize>(end_));
287 size_t size = RoundUp(Size(), kPageSize);
288 // Trim the heap so that we minimize the size of the Zygote space.
289 Trim();
290 // Trim our mem-map to free unused pages.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700291 GetMemMap()->UnMapAtEnd(end_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700292 // TODO: Not hardcode these in?
293 const size_t starting_size = kPageSize;
294 const size_t initial_size = 2 * MB;
295 // Remaining size is for the new alloc space.
296 const size_t growth_limit = growth_limit_ - size;
297 const size_t capacity = Capacity() - size;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700298 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n"
299 << "End " << reinterpret_cast<const void*>(end_) << "\n"
300 << "Size " << size << "\n"
301 << "GrowthLimit " << growth_limit_ << "\n"
302 << "Capacity " << Capacity();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700303 SetGrowthLimit(RoundUp(size, kPageSize));
304 SetFootprintLimit(RoundUp(size, kPageSize));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700305 // FIXME: Do we need reference counted pointers here?
306 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700307 VLOG(heap) << "Creating new AllocSpace: ";
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700308 VLOG(heap) << "Size " << GetMemMap()->Size();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700309 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
310 VLOG(heap) << "Capacity " << PrettySize(capacity);
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700311 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(alloc_space_name, End(), capacity, PROT_READ | PROT_WRITE));
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 =
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700319 new DlMallocSpace(alloc_space_name, mem_map.release(), mspace, end_, end, growth_limit);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700320 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
321 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
322 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
323 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700324 VLOG(heap) << "zygote space creation done";
325 return alloc_space;
326}
327
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700329 MutexLock mu(self, lock_);
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700330 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700331 CHECK(ptr != NULL);
332 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
333 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800334 const size_t bytes_freed = InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700335 num_bytes_allocated_ -= bytes_freed;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700336 --num_objects_allocated_;
Ian Rogers30fab402012-01-23 15:43:46 -0800337 mspace_free(mspace_, ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700338 return bytes_freed;
Ian Rogers30fab402012-01-23 15:43:46 -0800339}
340
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800342 DCHECK(ptrs != NULL);
343
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700344 // Don't need the lock to calculate the size of the freed pointers.
345 size_t bytes_freed = 0;
346 for (size_t i = 0; i < num_ptrs; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 mirror::Object* ptr = ptrs[i];
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800348 const size_t look_ahead = 8;
349 if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) {
350 // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
351 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
352 }
353 bytes_freed += InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700354 }
355
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700356 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700357 size_t num_broken_ptrs = 0;
358 for (size_t i = 0; i < num_ptrs; i++) {
359 if (!Contains(ptrs[i])) {
360 num_broken_ptrs++;
361 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
362 } else {
363 size_t size = mspace_usable_size(ptrs[i]);
364 memset(ptrs[i], 0xEF, size);
365 }
Ian Rogers30fab402012-01-23 15:43:46 -0800366 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700367 CHECK_EQ(num_broken_ptrs, 0u);
Ian Rogers30fab402012-01-23 15:43:46 -0800368 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800369
370 {
371 MutexLock mu(self, lock_);
372 num_bytes_allocated_ -= bytes_freed;
373 num_objects_allocated_ -= num_ptrs;
374 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
375 return bytes_freed;
376 }
Ian Rogers30fab402012-01-23 15:43:46 -0800377}
378
379// Callback from dlmalloc when it needs to increase the footprint
380extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800381 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700382 DCHECK_EQ(heap->GetAllocSpace()->GetMspace(), mspace);
383 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800384}
385
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700386void* DlMallocSpace::MoreCore(intptr_t increment) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700387 lock_.AssertHeld(Thread::Current());
Ian Rogers30fab402012-01-23 15:43:46 -0800388 byte* original_end = end_;
389 if (increment != 0) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700390 VLOG(heap) << "DlMallocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800391 byte* new_end = original_end + increment;
392 if (increment > 0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800393 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
394 // by mspace_set_footprint_limit.
395 CHECK_LE(new_end, Begin() + Capacity());
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700396 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800397 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800398 // Should never be asked for negative footprint (ie before begin)
399 CHECK_GT(original_end + increment, Begin());
Ian Rogers30fab402012-01-23 15:43:46 -0800400 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800401 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
402 // expensive (note the same isn't true for giving permissions to a page as the protected
403 // page shouldn't be in a TLB). We should investigate performance impact of just
404 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
405 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800406 size_t size = -increment;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700407 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName());
408 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800409 }
410 // Update end_
411 end_ = new_end;
412 }
413 return original_end;
414}
415
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800416// Virtual functions can't get inlined.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417inline size_t DlMallocSpace::InternalAllocationSize(const mirror::Object* obj) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700418 return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) +
419 kChunkOverhead;
Ian Rogers30fab402012-01-23 15:43:46 -0800420}
421
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422size_t DlMallocSpace::AllocationSize(const mirror::Object* obj) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800423 return InternalAllocationSize(obj);
424}
425
Ian Rogers48931882013-01-22 14:35:16 -0800426size_t DlMallocSpace::Trim() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700427 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700428 // Trim to release memory at the end of the space.
429 mspace_trim(mspace_, 0);
430 // Visit space looking for page-sized holes to advise the kernel we don't need.
Ian Rogers48931882013-01-22 14:35:16 -0800431 size_t reclaimed = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700432 mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed);
Ian Rogers48931882013-01-22 14:35:16 -0800433 return reclaimed;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700434}
Ian Rogers30fab402012-01-23 15:43:46 -0800435
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700436void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Ian Rogers30fab402012-01-23 15:43:46 -0800437 void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700438 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800439 mspace_inspect_all(mspace_, callback, arg);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700440 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800441}
442
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700443size_t DlMallocSpace::GetFootprint() {
444 MutexLock mu(Thread::Current(), lock_);
445 return mspace_footprint(mspace_);
446}
447
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700448size_t DlMallocSpace::GetFootprintLimit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700449 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800450 return mspace_footprint_limit(mspace_);
451}
452
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700453void DlMallocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700454 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700455 VLOG(heap) << "DLMallocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800456 // Compare against the actual footprint, rather than the Size(), because the heap may not have
457 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800458 size_t current_space_size = mspace_footprint(mspace_);
459 if (new_size < current_space_size) {
460 // Don't let the space grow any more.
461 new_size = current_space_size;
462 }
463 mspace_set_footprint_limit(mspace_, new_size);
464}
465
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700466void DlMallocSpace::Dump(std::ostream& os) const {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700467 os << GetType()
Ian Rogers1d54e732013-05-02 21:10:01 -0700468 << " begin=" << reinterpret_cast<void*>(Begin())
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700469 << ",end=" << reinterpret_cast<void*>(End())
470 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
471 << ",name=\"" << GetName() << "\"]";
472}
473
Ian Rogers1d54e732013-05-02 21:10:01 -0700474} // namespace space
475} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700476} // namespace art