blob: 8c13d79be6eb03537fcd2006fb8bdbe7873e8c11 [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 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),
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);
126
127 size_t bitmap_index = bitmap_index_++;
128
Ian Rogers1d54e732013-05-02 21:10:01 -0700129 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(accounting::CardTable::kCardSize);
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700130 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->Begin())));
131 CHECK(IsAligned<kGcCardSize>(reinterpret_cast<uintptr_t>(mem_map->End())));
Ian Rogers1d54e732013-05-02 21:10:01 -0700132 live_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800133 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700134 Begin(), Capacity()));
135 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
136
Ian Rogers1d54e732013-05-02 21:10:01 -0700137 mark_bitmap_.reset(accounting::SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800138 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700139 Begin(), Capacity()));
140 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700141
142 for (auto& freed : recent_freed_objects_) {
143 freed.first = nullptr;
144 freed.second = nullptr;
145 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700146}
147
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700148DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t
149 growth_limit, size_t capacity, byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800150 // Memory we promise to dlmalloc before it asks for morecore.
151 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
152 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
153 // size of the large allocation) will be greater than the footprint limit.
154 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -0800155 uint64_t start_time = 0;
156 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
157 start_time = NanoTime();
158 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -0800159 << " initial_size=" << PrettySize(initial_size)
160 << " growth_limit=" << PrettySize(growth_limit)
161 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -0800162 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163 }
Ian Rogers30fab402012-01-23 15:43:46 -0800164
165 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -0800166 if (starting_size > initial_size) {
167 initial_size = starting_size;
168 }
Ian Rogers30fab402012-01-23 15:43:46 -0800169 if (initial_size > growth_limit) {
170 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -0800171 << PrettySize(initial_size) << ") is larger than its capacity ("
172 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800173 return NULL;
174 }
175 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800176 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
177 << PrettySize(growth_limit) << ") is larger than the capacity ("
178 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800179 return NULL;
180 }
181
182 // Page align growth limit and capacity which will be used to manage mmapped storage
183 growth_limit = RoundUp(growth_limit, kPageSize);
184 capacity = RoundUp(capacity, kPageSize);
185
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700186 std::string error_msg;
Mathieu Chartier6875fd32013-08-02 11:17:31 -0700187 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, capacity,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700188 PROT_READ | PROT_WRITE, &error_msg));
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 Rogers8d31bbd2013-10-13 10:44:14 -0700191 << PrettySize(capacity) << ": " << error_msg;
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);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700311 std::string error_msg;
312 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(alloc_space_name, End(), capacity,
313 PROT_READ | PROT_WRITE, &error_msg));
314 CHECK(mem_map.get() != nullptr) << error_msg;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700315 void* mspace = CreateMallocSpace(end_, starting_size, initial_size);
316 // Protect memory beyond the initial size.
317 byte* end = mem_map->Begin() + starting_size;
318 if (capacity - initial_size > 0) {
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700319 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), alloc_space_name);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700320 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700321 DlMallocSpace* alloc_space =
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700322 new DlMallocSpace(alloc_space_name, mem_map.release(), mspace, end_, end, growth_limit);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700323 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
324 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
325 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
326 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700327 VLOG(heap) << "zygote space creation done";
328 return alloc_space;
329}
330
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700331mirror::Class* DlMallocSpace::FindRecentFreedObject(const mirror::Object* obj) {
332 size_t pos = recent_free_pos_;
333 // Start at the most recently freed object and work our way back since there may be duplicates
334 // caused by dlmalloc reusing memory.
335 if (kRecentFreeCount > 0) {
336 for (size_t i = 0; i + 1 < kRecentFreeCount + 1; ++i) {
337 pos = pos != 0 ? pos - 1 : kRecentFreeMask;
338 if (recent_freed_objects_[pos].first == obj) {
339 return recent_freed_objects_[pos].second;
340 }
341 }
342 }
343 return nullptr;
344}
345
346void DlMallocSpace::RegisterRecentFree(mirror::Object* ptr) {
347 recent_freed_objects_[recent_free_pos_].first = ptr;
348 recent_freed_objects_[recent_free_pos_].second = ptr->GetClass();
349 recent_free_pos_ = (recent_free_pos_ + 1) & kRecentFreeMask;
350}
351
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700353 MutexLock mu(self, lock_);
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700354 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700355 CHECK(ptr != NULL);
356 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
357 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800358 const size_t bytes_freed = InternalAllocationSize(ptr);
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700359 total_bytes_freed_ += bytes_freed;
360 ++total_objects_freed_;
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700361 if (kRecentFreeCount > 0) {
362 RegisterRecentFree(ptr);
363 }
Ian Rogers30fab402012-01-23 15:43:46 -0800364 mspace_free(mspace_, ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700365 return bytes_freed;
Ian Rogers30fab402012-01-23 15:43:46 -0800366}
367
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800369 DCHECK(ptrs != NULL);
370
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700371 // Don't need the lock to calculate the size of the freed pointers.
372 size_t bytes_freed = 0;
373 for (size_t i = 0; i < num_ptrs; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 mirror::Object* ptr = ptrs[i];
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800375 const size_t look_ahead = 8;
376 if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) {
377 // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
378 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
379 }
380 bytes_freed += InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700381 }
382
Mathieu Chartier0f72e412013-09-06 16:40:01 -0700383 if (kRecentFreeCount > 0) {
384 MutexLock mu(self, lock_);
385 for (size_t i = 0; i < num_ptrs; i++) {
386 RegisterRecentFree(ptrs[i]);
387 }
388 }
389
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700390 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700391 size_t num_broken_ptrs = 0;
392 for (size_t i = 0; i < num_ptrs; i++) {
393 if (!Contains(ptrs[i])) {
394 num_broken_ptrs++;
395 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
396 } else {
397 size_t size = mspace_usable_size(ptrs[i]);
398 memset(ptrs[i], 0xEF, size);
399 }
Ian Rogers30fab402012-01-23 15:43:46 -0800400 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700401 CHECK_EQ(num_broken_ptrs, 0u);
Ian Rogers30fab402012-01-23 15:43:46 -0800402 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800403
404 {
405 MutexLock mu(self, lock_);
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700406 total_bytes_freed_ += bytes_freed;
407 total_objects_freed_ += num_ptrs;
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800408 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
409 return bytes_freed;
410 }
Ian Rogers30fab402012-01-23 15:43:46 -0800411}
412
413// Callback from dlmalloc when it needs to increase the footprint
414extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800415 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700416 DCHECK_EQ(heap->GetAllocSpace()->GetMspace(), mspace);
417 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800418}
419
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700420void* DlMallocSpace::MoreCore(intptr_t increment) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700421 lock_.AssertHeld(Thread::Current());
Ian Rogers30fab402012-01-23 15:43:46 -0800422 byte* original_end = end_;
423 if (increment != 0) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700424 VLOG(heap) << "DlMallocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800425 byte* new_end = original_end + increment;
426 if (increment > 0) {
Ian Rogers30fab402012-01-23 15:43:46 -0800427 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
428 // by mspace_set_footprint_limit.
429 CHECK_LE(new_end, Begin() + Capacity());
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700430 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800431 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800432 // Should never be asked for negative footprint (ie before begin)
433 CHECK_GT(original_end + increment, Begin());
Ian Rogers30fab402012-01-23 15:43:46 -0800434 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800435 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
436 // expensive (note the same isn't true for giving permissions to a page as the protected
437 // page shouldn't be in a TLB). We should investigate performance impact of just
438 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
439 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800440 size_t size = -increment;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700441 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName());
442 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800443 }
444 // Update end_
445 end_ = new_end;
446 }
447 return original_end;
448}
449
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800450// Virtual functions can't get inlined.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800451inline size_t DlMallocSpace::InternalAllocationSize(const mirror::Object* obj) {
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700452 return AllocationSizeNonvirtual(obj);
Ian Rogers30fab402012-01-23 15:43:46 -0800453}
454
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455size_t DlMallocSpace::AllocationSize(const mirror::Object* obj) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800456 return InternalAllocationSize(obj);
457}
458
Ian Rogers48931882013-01-22 14:35:16 -0800459size_t DlMallocSpace::Trim() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700460 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700461 // Trim to release memory at the end of the space.
462 mspace_trim(mspace_, 0);
463 // Visit space looking for page-sized holes to advise the kernel we don't need.
Ian Rogers48931882013-01-22 14:35:16 -0800464 size_t reclaimed = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700465 mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed);
Ian Rogers48931882013-01-22 14:35:16 -0800466 return reclaimed;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700467}
Ian Rogers30fab402012-01-23 15:43:46 -0800468
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700469void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Ian Rogers30fab402012-01-23 15:43:46 -0800470 void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700471 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800472 mspace_inspect_all(mspace_, callback, arg);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700473 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800474}
475
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700476size_t DlMallocSpace::GetFootprint() {
477 MutexLock mu(Thread::Current(), lock_);
478 return mspace_footprint(mspace_);
479}
480
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700481size_t DlMallocSpace::GetFootprintLimit() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700482 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800483 return mspace_footprint_limit(mspace_);
484}
485
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700486void DlMallocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700487 MutexLock mu(Thread::Current(), lock_);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700488 VLOG(heap) << "DLMallocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800489 // Compare against the actual footprint, rather than the Size(), because the heap may not have
490 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800491 size_t current_space_size = mspace_footprint(mspace_);
492 if (new_size < current_space_size) {
493 // Don't let the space grow any more.
494 new_size = current_space_size;
495 }
496 mspace_set_footprint_limit(mspace_, new_size);
497}
498
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700499void DlMallocSpace::Dump(std::ostream& os) const {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700500 os << GetType()
Ian Rogers1d54e732013-05-02 21:10:01 -0700501 << " begin=" << reinterpret_cast<void*>(Begin())
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700502 << ",end=" << reinterpret_cast<void*>(End())
503 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
504 << ",name=\"" << GetName() << "\"]";
505}
506
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700507uint64_t DlMallocSpace::GetBytesAllocated() {
508 MutexLock mu(Thread::Current(), lock_);
509 size_t bytes_allocated = 0;
510 mspace_inspect_all(mspace_, DlmallocBytesAllocatedCallback, &bytes_allocated);
511 return bytes_allocated;
512}
513
514uint64_t DlMallocSpace::GetObjectsAllocated() {
515 MutexLock mu(Thread::Current(), lock_);
516 size_t objects_allocated = 0;
517 mspace_inspect_all(mspace_, DlmallocObjectsAllocatedCallback, &objects_allocated);
518 return objects_allocated;
519}
520
Ian Rogers1d54e732013-05-02 21:10:01 -0700521} // namespace space
522} // namespace gc
Carl Shapiro69759ea2011-07-21 18:13:35 -0700523} // namespace art