blob: a9440d3c1b2f8a332f836c6671ca452a2be4129f [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 Chartier6875fd32013-08-02 11:17: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 Chartier6875fd32013-08-02 11:17:31 -0700105 byte* end, size_t growth_limit, size_t initial_size) :
Ian Rogers1d54e732013-05-02 21:10:01 -0700106 DlMallocSpace(name, mem_map, mspace, begin, end, 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 Chartier2fde5332012-09-14 14:51:54 -0700120 byte* end, size_t growth_limit)
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700121 : MemMapSpace(name, mem_map, end - begin, kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700122 recent_free_pos_(0), num_bytes_allocated_(0), num_objects_allocated_(0),
123 total_bytes_allocated_(0), total_objects_allocated_(0),
124 lock_("allocation space lock", kAllocSpaceLock), mspace_(mspace),
Ian Rogers15bf2d32012-08-28 17:33:04 -0700125 growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700126 CHECK(mspace != NULL);
127
128 size_t bitmap_index = bitmap_index_++;
129
Ian Rogers1d54e732013-05-02 21:10:01 -0700130 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700131 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->Begin())));
132 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->End())));
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 live_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800134 StringPrintf("allocspace %s live-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 live bitmap #" << bitmap_index;
137
Ian Rogers1d54e732013-05-02 21:10:01 -0700138 mark_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800139 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700140 Begin(), Capacity()));
141 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700142
143 for (auto& freed : recent_freed_objects_) {
144 freed.first = nullptr;
145 freed.second = nullptr;
146 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700147}
148
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700149DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t
150 growth_limit, size_t capacity, byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800151 // Memory we promise to dlmalloc before it asks for morecore.
152 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
153 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
154 // size of the large allocation) will be greater than the footprint limit.
155 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -0800156 uint64_t start_time = 0;
157 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
158 start_time = NanoTime();
159 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -0800160 << " initial_size=" << PrettySize(initial_size)
161 << " growth_limit=" << PrettySize(growth_limit)
162 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -0800163 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700164 }
Ian Rogers30fab402012-01-23 15:43:46 -0800165
166 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -0800167 if (starting_size > initial_size) {
168 initial_size = starting_size;
169 }
Ian Rogers30fab402012-01-23 15:43:46 -0800170 if (initial_size > growth_limit) {
171 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -0800172 << PrettySize(initial_size) << ") is larger than its capacity ("
173 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800174 return NULL;
175 }
176 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800177 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
178 << PrettySize(growth_limit) << ") is larger than the capacity ("
179 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800180 return NULL;
181 }
182
183 // Page align growth limit and capacity which will be used to manage mmapped storage
184 growth_limit = RoundUp(growth_limit, kPageSize);
185 capacity = RoundUp(capacity, kPageSize);
186
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700187 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity,
188 PROT_READ | PROT_WRITE));
Ian Rogers30fab402012-01-23 15:43:46 -0800189 if (mem_map.get() == NULL) {
190 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers3bb17a62012-01-27 23:56:44 -0800191 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -0800192 return NULL;
193 }
194
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700195 void* mspace = CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800196 if (mspace == NULL) {
197 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
198 return NULL;
199 }
200
Ian Rogers3bb17a62012-01-27 23:56:44 -0800201 // Protect memory beyond the initial size.
202 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800203 if (capacity - initial_size > 0) {
204 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
205 }
206
207 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700208 MemMap* mem_map_ptr = mem_map.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700209 DlMallocSpace* space;
210 if (RUNNING_ON_VALGRIND > 0) {
211 space = new ValgrindDlMallocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end,
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700212 growth_limit, initial_size);
Ian Rogers1d54e732013-05-02 21:10:01 -0700213 } else {
214 space = new DlMallocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end, growth_limit);
215 }
Ian Rogers30fab402012-01-23 15:43:46 -0800216 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800217 LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time)
218 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800219 }
220 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221}
222
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700223void* DlMallocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800224 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800226 // create mspace using our backing storage starting at begin and with a footprint of
227 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
228 // morecore_start bytes of memory is exhaused morecore will be called.
229 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700230 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800231 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800232 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700233 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800234 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235 }
236 return msp;
237}
238
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700239void DlMallocSpace::SwapBitmaps() {
Ian Rogers1d54e732013-05-02 21:10:01 -0700240 live_bitmap_.swap(mark_bitmap_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700241 // Swap names to get more descriptive diagnostics.
Ian Rogers1d54e732013-05-02 21:10:01 -0700242 std::string temp_name(live_bitmap_->GetName());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700243 live_bitmap_->SetName(mark_bitmap_->GetName());
244 mark_bitmap_->SetName(temp_name);
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700245}
246
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700247mirror::Object* DlMallocSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
248 return AllocNonvirtual(self, num_bytes, bytes_allocated);
249}
250
251mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes, size_t* bytes_allocated) {
252 mirror::Object* result;
253 {
254 MutexLock mu(self, lock_);
255 // Grow as much as possible within the mspace.
256 size_t max_allowed = Capacity();
257 mspace_set_footprint_limit(mspace_, max_allowed);
258 // Try the allocation.
259 result = AllocWithoutGrowthLocked(num_bytes, bytes_allocated);
260 // Shrink back down as small as possible.
261 size_t footprint = mspace_footprint(mspace_);
262 mspace_set_footprint_limit(mspace_, footprint);
jeffhaoc1160702011-10-27 15:48:45 -0700263 }
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700264 if (result != NULL) {
265 // Zero freshly allocated memory, done while not holding the space's lock.
266 memset(result, 0, num_bytes);
267 }
Ian Rogers30fab402012-01-23 15:43:46 -0800268 // 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
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700328mirror::Class* DlMallocSpace::FindRecentFreedObject(const mirror::Object* obj) {
329 size_t pos = recent_free_pos_;
330 // Start at the most recently freed object and work our way back since there may be duplicates
331 // caused by dlmalloc reusing memory.
332 if (kRecentFreeCount > 0) {
333 for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) {
334 pos = pos != 0 ? pos - 1 : kRecentFreeMask;
335 if (recent_freed_objects_[pos].first == obj) {
336 return recent_freed_objects_[pos].second;
337 }
338 }
339 }
340 return nullptr;
341}
342
343void DlMallocSpace::RegisterRecentFree(mirror::Object* ptr) {
344 recent_freed_objects_[recent_free_pos_].first = ptr;
345 recent_freed_objects_[recent_free_pos_].second = ptr->GetClass();
346 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);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700356 num_bytes_allocated_ -= bytes_freed;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700357 --num_objects_allocated_;
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_);
403 num_bytes_allocated_ -= bytes_freed;
404 num_objects_allocated_ -= num_ptrs;
405 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 Chartier357e9be2012-08-01 11:00:14 -0700413 DCHECK_EQ(heap->GetAllocSpace()->GetMspace(), mspace);
414 return heap->GetAllocSpace()->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 Chartier1c23e1e2012-10-12 14:14:11 -0700483void DlMallocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700484 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700485 VLOG(heap) << "DLMallocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800486 // Compare against the actual footprint, rather than the Size(), because the heap may not have
487 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800488 size_t current_space_size = mspace_footprint(mspace_);
489 if (new_size < current_space_size) {
490 // Don't let the space grow any more.
491 new_size = current_space_size;
492 }
493 mspace_set_footprint_limit(mspace_, new_size);
494}
495
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700496void DlMallocSpace::Dump(std::ostream& os) const {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700497 os << GetType()
Ian Rogers1d54e732013-05-02 21:10:01 -0700498 << " begin=" << reinterpret_cast<void*>(Begin())
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700499 << ",end=" << reinterpret_cast<void*>(End())
500 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
501 << ",name=\"" << GetName() << "\"]";
502}
503
Ian Rogers1d54e732013-05-02 21:10:01 -0700504} // namespace space
505} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700506} // namespace art