blob: 9f853943ffb2e8ecae3522bc9f25d12ee53c6746 [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/memcheck.h>
24#include <valgrind.h>
25
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) {
50 void* obj_with_rdz = DlMallocSpace::AllocWithGrowth(self, num_bytes + (2 * kValgrindRedZoneBytes));
51 if (obj_with_rdz != NULL) {
52 //VALGRIND_MAKE_MEM_UNDEFINED();
53 mirror::Object* result = reinterpret_cast<mirror::Object*>(reinterpret_cast<byte*>(obj_with_rdz) +
54 kValgrindRedZoneBytes);
55 VALGRIND_MEMPOOL_ALLOC(GetMspace(), result, num_bytes);
56 LOG(INFO) << "AllocWithGrowth on " << self << " = " << obj_with_rdz
57 << " of size " << num_bytes;
58 return result;
59 } else {
60 return NULL;
61 }
62 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070063
Ian Rogers1d54e732013-05-02 21:10:01 -070064 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes) {
65 void* obj_with_rdz = DlMallocSpace::Alloc(self, num_bytes + (2 * kValgrindRedZoneBytes));
66 if (obj_with_rdz != NULL) {
67 mirror::Object* result = reinterpret_cast<mirror::Object*>(reinterpret_cast<byte*>(obj_with_rdz) +
68 kValgrindRedZoneBytes);
69 VALGRIND_MEMPOOL_ALLOC(GetMspace(), result, num_bytes);
70 LOG(INFO) << "Alloc on " << self << " = " << obj_with_rdz
71 << " of size " << num_bytes;
72 return result;
73 } else {
74 return NULL;
75 }
76 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -070077
Ian Rogers1d54e732013-05-02 21:10:01 -070078 virtual size_t AllocationSize(const mirror::Object* obj) {
79 const void* obj_after_rdz = reinterpret_cast<const void*>(obj);
80 size_t result = DlMallocSpace::AllocationSize(
81 reinterpret_cast<const mirror::Object*>(reinterpret_cast<const byte*>(obj_after_rdz) -
82 kValgrindRedZoneBytes));
83 return result - (2 * kValgrindRedZoneBytes);
84 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070085
Ian Rogers1d54e732013-05-02 21:10:01 -070086 virtual size_t Free(Thread* self, mirror::Object* ptr) {
87 void* obj_after_rdz = reinterpret_cast<void*>(ptr);
88 void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes;
89 LOG(INFO) << "Free on " << self << " of " << obj_with_rdz;
90 size_t freed = DlMallocSpace::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
91 VALGRIND_MEMPOOL_FREE(GetMspace(), obj_after_rdz);
92 return freed - (2 * kValgrindRedZoneBytes);
93 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070094
Ian Rogers1d54e732013-05-02 21:10:01 -070095 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
96 size_t freed = 0;
97 for (size_t i = 0; i < num_ptrs; i++) {
98 void* obj_after_rdz = reinterpret_cast<void*>(ptrs[i]);
99 void* obj_with_rdz = reinterpret_cast<byte*>(obj_after_rdz) - kValgrindRedZoneBytes;
100 LOG(INFO) << "FreeList on " << self << " of " << obj_with_rdz;
101 freed += DlMallocSpace::Free(self, reinterpret_cast<mirror::Object*>(obj_with_rdz));
102 VALGRIND_MEMPOOL_FREE(GetMspace(), obj_after_rdz);
103 }
104 return freed - (2 * kValgrindRedZoneBytes * num_ptrs);
105 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700106
Ian Rogers1d54e732013-05-02 21:10:01 -0700107 ValgrindDlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
108 byte* end, size_t growth_limit) :
109 DlMallocSpace(name, mem_map, mspace, begin, end, growth_limit) {
110 VALGRIND_CREATE_MEMPOOL(GetMspace(), kValgrindRedZoneBytes, true);
111 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700112
Ian Rogers1d54e732013-05-02 21:10:01 -0700113 virtual ~ValgrindDlMallocSpace() {
114 VALGRIND_DESTROY_MEMPOOL(GetMspace());
115 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700116
Ian Rogers1d54e732013-05-02 21:10:01 -0700117 private:
118 DISALLOW_COPY_AND_ASSIGN(ValgrindDlMallocSpace);
119};
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700120
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700121size_t DlMallocSpace::bitmap_index_ = 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700122
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700123DlMallocSpace::DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700124 byte* end, size_t growth_limit)
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700125 : MemMapSpace(name, mem_map, end - begin, kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700126 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
127 total_objects_allocated_(0), lock_("allocation space lock", kAllocSpaceLock), mspace_(mspace),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700128 growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700129 CHECK(mspace != NULL);
130
131 size_t bitmap_index = bitmap_index_++;
132
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
Brian Carlstrom42748892013-07-18 18:04:08 -0700134 CHECK_EQ(reinterpret_cast<uintptr_t>(mem_map->Begin()) % kGcCardSize, 0U);
135 CHECK_EQ(reinterpret_cast<uintptr_t>(mem_map->End()) % kGcCardSize, 0U);
Ian Rogers1d54e732013-05-02 21:10:01 -0700136 live_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800137 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700138 Begin(), Capacity()));
139 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
140
Ian Rogers1d54e732013-05-02 21:10:01 -0700141 mark_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800142 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700143 Begin(), Capacity()));
144 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
145}
146
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700147DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t
148 growth_limit, size_t capacity, byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800149 // Memory we promise to dlmalloc before it asks for morecore.
150 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
151 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
152 // size of the large allocation) will be greater than the footprint limit.
153 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -0800154 uint64_t start_time = 0;
155 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
156 start_time = NanoTime();
157 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -0800158 << " initial_size=" << PrettySize(initial_size)
159 << " growth_limit=" << PrettySize(growth_limit)
160 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -0800161 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700162 }
Ian Rogers30fab402012-01-23 15:43:46 -0800163
164 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -0800165 if (starting_size > initial_size) {
166 initial_size = starting_size;
167 }
Ian Rogers30fab402012-01-23 15:43:46 -0800168 if (initial_size > growth_limit) {
169 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -0800170 << PrettySize(initial_size) << ") is larger than its capacity ("
171 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800172 return NULL;
173 }
174 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800175 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
176 << PrettySize(growth_limit) << ") is larger than the capacity ("
177 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800178 return NULL;
179 }
180
181 // Page align growth limit and capacity which will be used to manage mmapped storage
182 growth_limit = RoundUp(growth_limit, kPageSize);
183 capacity = RoundUp(capacity, kPageSize);
184
185 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin,
186 capacity, PROT_READ | PROT_WRITE));
187 if (mem_map.get() == NULL) {
188 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers3bb17a62012-01-27 23:56:44 -0800189 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -0800190 return NULL;
191 }
192
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700193 void* mspace = CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800194 if (mspace == NULL) {
195 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
196 return NULL;
197 }
198
Ian Rogers3bb17a62012-01-27 23:56:44 -0800199 // Protect memory beyond the initial size.
200 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800201 if (capacity - initial_size > 0) {
202 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
203 }
204
205 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700206 MemMap* mem_map_ptr = mem_map.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700207 DlMallocSpace* space;
208 if (RUNNING_ON_VALGRIND > 0) {
209 space = new ValgrindDlMallocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end,
210 growth_limit);
211 } else {
212 space = new DlMallocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end, growth_limit);
213 }
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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245mirror::Object* DlMallocSpace::AllocWithoutGrowthLocked(size_t num_bytes) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 mirror::Object* result = reinterpret_cast<mirror::Object*>(mspace_calloc(mspace_, 1, num_bytes));
Ian Rogers1d54e732013-05-02 21:10:01 -0700247 if (result != NULL) {
248 if (kDebugSpaces) {
249 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
250 << ") not in bounds of allocation space " << *this;
251 }
252 size_t allocation_size = AllocationSize(result);
253 num_bytes_allocated_ += allocation_size;
254 total_bytes_allocated_ += allocation_size;
255 ++total_objects_allocated_;
256 ++num_objects_allocated_;
jeffhaoc1160702011-10-27 15:48:45 -0700257 }
Ian Rogers30fab402012-01-23 15:43:46 -0800258 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700259}
260
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261mirror::Object* DlMallocSpace::Alloc(Thread* self, size_t num_bytes) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700262 MutexLock mu(self, lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700263 return AllocWithoutGrowthLocked(num_bytes);
264}
265
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700267 MutexLock mu(self, lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800268 // Grow as much as possible within the mspace.
269 size_t max_allowed = Capacity();
270 mspace_set_footprint_limit(mspace_, max_allowed);
271 // Try the allocation.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 mirror::Object* result = AllocWithoutGrowthLocked(num_bytes);
Ian Rogers30fab402012-01-23 15:43:46 -0800273 // Shrink back down as small as possible.
274 size_t footprint = mspace_footprint(mspace_);
275 mspace_set_footprint_limit(mspace_, footprint);
276 // Return the new allocation or NULL.
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700277 CHECK(!kDebugSpaces || result == NULL || Contains(result));
Ian Rogers30fab402012-01-23 15:43:46 -0800278 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700279}
280
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700281void DlMallocSpace::SetGrowthLimit(size_t growth_limit) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700282 growth_limit = RoundUp(growth_limit, kPageSize);
283 growth_limit_ = growth_limit;
284 if (Size() > growth_limit_) {
285 end_ = begin_ + growth_limit;
286 }
287}
288
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700289DlMallocSpace* DlMallocSpace::CreateZygoteSpace() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700290 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
Ian Rogers1d54e732013-05-02 21:10:01 -0700291 DCHECK(IsAligned<accounting::CardTable::kCardSize>(begin_));
292 DCHECK(IsAligned<accounting::CardTable::kCardSize>(end_));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700293 DCHECK(IsAligned<kPageSize>(begin_));
294 DCHECK(IsAligned<kPageSize>(end_));
295 size_t size = RoundUp(Size(), kPageSize);
296 // Trim the heap so that we minimize the size of the Zygote space.
297 Trim();
298 // Trim our mem-map to free unused pages.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700299 GetMemMap()->UnMapAtEnd(end_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700300 // TODO: Not hardcode these in?
301 const size_t starting_size = kPageSize;
302 const size_t initial_size = 2 * MB;
303 // Remaining size is for the new alloc space.
304 const size_t growth_limit = growth_limit_ - size;
305 const size_t capacity = Capacity() - size;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700306 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n"
307 << "End " << reinterpret_cast<const void*>(end_) << "\n"
308 << "Size " << size << "\n"
309 << "GrowthLimit " << growth_limit_ << "\n"
310 << "Capacity " << Capacity();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700311 SetGrowthLimit(RoundUp(size, kPageSize));
312 SetFootprintLimit(RoundUp(size, kPageSize));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700313 // FIXME: Do we need reference counted pointers here?
314 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700315 VLOG(heap) << "Creating new AllocSpace: ";
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700316 VLOG(heap) << "Size " << GetMemMap()->Size();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700317 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
318 VLOG(heap) << "Capacity " << PrettySize(capacity);
Ian Rogers1d54e732013-05-02 21:10:01 -0700319 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(GetName(), End(), capacity, PROT_READ | PROT_WRITE));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700320 void* mspace = CreateMallocSpace(end_, starting_size, initial_size);
321 // Protect memory beyond the initial size.
322 byte* end = mem_map->Begin() + starting_size;
323 if (capacity - initial_size > 0) {
324 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name_.c_str());
325 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700326 DlMallocSpace* alloc_space =
327 new DlMallocSpace(name_, mem_map.release(), mspace, end_, end, growth_limit);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700328 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
329 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
330 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
331 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700332 name_ += "-zygote-transformed";
333 VLOG(heap) << "zygote space creation done";
334 return alloc_space;
335}
336
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700338 MutexLock mu(self, lock_);
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700339 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700340 CHECK(ptr != NULL);
341 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
342 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800343 const size_t bytes_freed = InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700344 num_bytes_allocated_ -= bytes_freed;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700345 --num_objects_allocated_;
Ian Rogers30fab402012-01-23 15:43:46 -0800346 mspace_free(mspace_, ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700347 return bytes_freed;
Ian Rogers30fab402012-01-23 15:43:46 -0800348}
349
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800351 DCHECK(ptrs != NULL);
352
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700353 // Don't need the lock to calculate the size of the freed pointers.
354 size_t bytes_freed = 0;
355 for (size_t i = 0; i < num_ptrs; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 mirror::Object* ptr = ptrs[i];
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800357 const size_t look_ahead = 8;
358 if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) {
359 // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
360 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
361 }
362 bytes_freed += InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700363 }
364
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700365 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700366 size_t num_broken_ptrs = 0;
367 for (size_t i = 0; i < num_ptrs; i++) {
368 if (!Contains(ptrs[i])) {
369 num_broken_ptrs++;
370 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
371 } else {
372 size_t size = mspace_usable_size(ptrs[i]);
373 memset(ptrs[i], 0xEF, size);
374 }
Ian Rogers30fab402012-01-23 15:43:46 -0800375 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700376 CHECK_EQ(num_broken_ptrs, 0u);
Ian Rogers30fab402012-01-23 15:43:46 -0800377 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800378
379 {
380 MutexLock mu(self, lock_);
381 num_bytes_allocated_ -= bytes_freed;
382 num_objects_allocated_ -= num_ptrs;
383 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
384 return bytes_freed;
385 }
Ian Rogers30fab402012-01-23 15:43:46 -0800386}
387
388// Callback from dlmalloc when it needs to increase the footprint
389extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800390 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700391 DCHECK_EQ(heap->GetAllocSpace()->GetMspace(), mspace);
392 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800393}
394
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700395void* DlMallocSpace::MoreCore(intptr_t increment) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700396 lock_.AssertHeld(Thread::Current());
Ian Rogers30fab402012-01-23 15:43:46 -0800397 byte* original_end = end_;
398 if (increment != 0) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700399 VLOG(heap) << "DlMallocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800400 byte* new_end = original_end + increment;
401 if (increment > 0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800402 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
403 // by mspace_set_footprint_limit.
404 CHECK_LE(new_end, Begin() + Capacity());
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700405 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800406 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800407 // Should never be asked for negative footprint (ie before begin)
408 CHECK_GT(original_end + increment, Begin());
Ian Rogers30fab402012-01-23 15:43:46 -0800409 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800410 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
411 // expensive (note the same isn't true for giving permissions to a page as the protected
412 // page shouldn't be in a TLB). We should investigate performance impact of just
413 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
414 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800415 size_t size = -increment;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700416 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName());
417 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800418 }
419 // Update end_
420 end_ = new_end;
421 }
422 return original_end;
423}
424
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800425// Virtual functions can't get inlined.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426inline size_t DlMallocSpace::InternalAllocationSize(const mirror::Object* obj) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700427 return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) +
428 kChunkOverhead;
Ian Rogers30fab402012-01-23 15:43:46 -0800429}
430
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431size_t DlMallocSpace::AllocationSize(const mirror::Object* obj) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800432 return InternalAllocationSize(obj);
433}
434
Ian Rogers48931882013-01-22 14:35:16 -0800435size_t DlMallocSpace::Trim() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700436 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700437 // Trim to release memory at the end of the space.
438 mspace_trim(mspace_, 0);
439 // Visit space looking for page-sized holes to advise the kernel we don't need.
Ian Rogers48931882013-01-22 14:35:16 -0800440 size_t reclaimed = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700441 mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed);
Ian Rogers48931882013-01-22 14:35:16 -0800442 return reclaimed;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700443}
Ian Rogers30fab402012-01-23 15:43:46 -0800444
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700445void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Ian Rogers30fab402012-01-23 15:43:46 -0800446 void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700447 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800448 mspace_inspect_all(mspace_, callback, arg);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700449 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800450}
451
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700452size_t DlMallocSpace::GetFootprintLimit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700453 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800454 return mspace_footprint_limit(mspace_);
455}
456
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700457void DlMallocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700458 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700459 VLOG(heap) << "DLMallocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800460 // Compare against the actual footprint, rather than the Size(), because the heap may not have
461 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800462 size_t current_space_size = mspace_footprint(mspace_);
463 if (new_size < current_space_size) {
464 // Don't let the space grow any more.
465 new_size = current_space_size;
466 }
467 mspace_set_footprint_limit(mspace_, new_size);
468}
469
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700470void DlMallocSpace::Dump(std::ostream& os) const {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700471 os << GetType()
Ian Rogers1d54e732013-05-02 21:10:01 -0700472 << " begin=" << reinterpret_cast<void*>(Begin())
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700473 << ",end=" << reinterpret_cast<void*>(End())
474 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
475 << ",name=\"" << GetName() << "\"]";
476}
477
Ian Rogers1d54e732013-05-02 21:10:01 -0700478} // namespace space
479} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700480} // namespace art