blob: 9c71841a556f0994852f8301cec42f67679b639e [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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080020#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/unix_file/fd_file.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "card_table.h"
Ian Rogers30fab402012-01-23 15:43:46 -080023#include "dlmalloc.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024#include "image.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/array.h"
26#include "mirror/abstract_method.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070027#include "os.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "runtime.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070029#include "space_bitmap.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "space_bitmap-inl.h"
31#include "thread.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080032#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070034
35namespace art {
36
Mathieu Chartierd22d5482012-11-06 17:14:12 -080037static const bool kPrefetchDuringDlMallocFreeList = true;
38
Mathieu Chartier8e9a1492012-10-04 12:25:40 -070039// Magic padding value that we use to check for buffer overruns.
40static const word kPaddingValue = 0xBAC0BAC0;
Ian Rogers30fab402012-01-23 15:43:46 -080041
Mathieu Chartier2fde5332012-09-14 14:51:54 -070042// TODO: Remove define macro
Ian Rogers30fab402012-01-23 15:43:46 -080043#define CHECK_MEMORY_CALL(call, args, what) \
44 do { \
45 int rc = call args; \
46 if (UNLIKELY(rc != 0)) { \
47 errno = rc; \
48 PLOG(FATAL) << # call << " failed for " << what; \
49 } \
50 } while (false)
51
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070052ImageSpace* Space::AsImageSpace() {
53 DCHECK_EQ(GetType(), kSpaceTypeImageSpace);
54 return down_cast<ImageSpace*>(down_cast<MemMapSpace*>(this));
55}
Mathieu Chartier2fde5332012-09-14 14:51:54 -070056
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070057DlMallocSpace* Space::AsAllocSpace() {
58 DCHECK_EQ(GetType(), kSpaceTypeAllocSpace);
59 return down_cast<DlMallocSpace*>(down_cast<MemMapSpace*>(this));
60}
61
62DlMallocSpace* Space::AsZygoteSpace() {
63 DCHECK_EQ(GetType(), kSpaceTypeZygoteSpace);
64 return down_cast<DlMallocSpace*>(down_cast<MemMapSpace*>(this));
65}
66
67LargeObjectSpace* Space::AsLargeObjectSpace() {
68 DCHECK_EQ(GetType(), kSpaceTypeLargeObjectSpace);
69 return reinterpret_cast<LargeObjectSpace*>(this);
Mathieu Chartier2fde5332012-09-14 14:51:54 -070070}
71
72ContinuousSpace::ContinuousSpace(const std::string& name, byte* begin, byte* end,
73 GcRetentionPolicy gc_retention_policy)
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070074 : name_(name), gc_retention_policy_(gc_retention_policy), begin_(begin), end_(end) {
75
76}
77
78DiscontinuousSpace::DiscontinuousSpace(const std::string& name,
79 GcRetentionPolicy gc_retention_policy)
80 : name_(name), gc_retention_policy_(gc_retention_policy) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -070081
82}
83
84MemMapSpace::MemMapSpace(const std::string& name, MemMap* mem_map, size_t initial_size,
85 GcRetentionPolicy gc_retention_policy)
86 : ContinuousSpace(name, mem_map->Begin(), mem_map->Begin() + initial_size, gc_retention_policy),
87 mem_map_(mem_map)
88{
89
90}
91
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070092size_t DlMallocSpace::bitmap_index_ = 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070093
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070094DlMallocSpace::DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin,
Mathieu Chartier2fde5332012-09-14 14:51:54 -070095 byte* end, size_t growth_limit)
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070096 : MemMapSpace(name, mem_map, end - begin, kGcRetentionPolicyAlwaysCollect),
Mathieu Chartier155dfe92012-10-09 14:24:49 -070097 num_bytes_allocated_(0), num_objects_allocated_(0), total_bytes_allocated_(0),
98 total_objects_allocated_(0), lock_("allocation space lock", kAllocSpaceLock), mspace_(mspace),
Ian Rogers15bf2d32012-08-28 17:33:04 -070099 growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700100 CHECK(mspace != NULL);
101
102 size_t bitmap_index = bitmap_index_++;
103
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700104 static const uintptr_t kGcCardSize = static_cast<uintptr_t>(CardTable::kCardSize);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700105 CHECK(reinterpret_cast<uintptr_t>(mem_map->Begin()) % kGcCardSize == 0);
106 CHECK(reinterpret_cast<uintptr_t>(mem_map->End()) % kGcCardSize == 0);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700107 live_bitmap_.reset(SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800108 StringPrintf("allocspace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700109 Begin(), Capacity()));
110 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
111
112 mark_bitmap_.reset(SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800113 StringPrintf("allocspace %s mark-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700114 Begin(), Capacity()));
115 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
116}
117
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700118DlMallocSpace* DlMallocSpace::Create(const std::string& name, size_t initial_size, size_t
119 growth_limit, size_t capacity, byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800120 // Memory we promise to dlmalloc before it asks for morecore.
121 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
122 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
123 // size of the large allocation) will be greater than the footprint limit.
124 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -0800125 uint64_t start_time = 0;
126 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
127 start_time = NanoTime();
128 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -0800129 << " initial_size=" << PrettySize(initial_size)
130 << " growth_limit=" << PrettySize(growth_limit)
131 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -0800132 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700133 }
Ian Rogers30fab402012-01-23 15:43:46 -0800134
135 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -0800136 if (starting_size > initial_size) {
137 initial_size = starting_size;
138 }
Ian Rogers30fab402012-01-23 15:43:46 -0800139 if (initial_size > growth_limit) {
140 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -0800141 << PrettySize(initial_size) << ") is larger than its capacity ("
142 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800143 return NULL;
144 }
145 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800146 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
147 << PrettySize(growth_limit) << ") is larger than the capacity ("
148 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800149 return NULL;
150 }
151
152 // Page align growth limit and capacity which will be used to manage mmapped storage
153 growth_limit = RoundUp(growth_limit, kPageSize);
154 capacity = RoundUp(capacity, kPageSize);
155
156 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin,
157 capacity, PROT_READ | PROT_WRITE));
158 if (mem_map.get() == NULL) {
159 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers3bb17a62012-01-27 23:56:44 -0800160 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -0800161 return NULL;
162 }
163
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700164 void* mspace = CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800165 if (mspace == NULL) {
166 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
167 return NULL;
168 }
169
Ian Rogers3bb17a62012-01-27 23:56:44 -0800170 // Protect memory beyond the initial size.
171 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800172 if (capacity - initial_size > 0) {
173 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
174 }
175
176 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700177 MemMap* mem_map_ptr = mem_map.release();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700178 DlMallocSpace* space = new DlMallocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end,
179 growth_limit);
Ian Rogers30fab402012-01-23 15:43:46 -0800180 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800181 LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time)
182 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800183 }
184 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700185}
186
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700187void* DlMallocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800188 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700189 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800190 // create mspace using our backing storage starting at begin and with a footprint of
191 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
192 // morecore_start bytes of memory is exhaused morecore will be called.
193 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700194 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800195 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800196 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700197 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800198 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700199 }
200 return msp;
201}
202
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700203void DlMallocSpace::SwapBitmaps() {
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700204 SpaceBitmap* temp_live_bitmap = live_bitmap_.release();
205 live_bitmap_.reset(mark_bitmap_.release());
206 mark_bitmap_.reset(temp_live_bitmap);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700207 // Swap names to get more descriptive diagnostics.
208 std::string temp_name = live_bitmap_->GetName();
209 live_bitmap_->SetName(mark_bitmap_->GetName());
210 mark_bitmap_->SetName(temp_name);
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700211}
212
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213mirror::Object* DlMallocSpace::AllocWithoutGrowthLocked(size_t num_bytes) {
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700214 if (kDebugSpaces) {
215 num_bytes += sizeof(word);
216 }
217
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 mirror::Object* result = reinterpret_cast<mirror::Object*>(mspace_calloc(mspace_, 1, num_bytes));
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700219 if (kDebugSpaces && result != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800220 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 << ") not in bounds of allocation space " << *this;
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700222 // Put a magic pattern before and after the allocation.
223 *reinterpret_cast<word*>(reinterpret_cast<byte*>(result) + AllocationSize(result)
224 - sizeof(word) - kChunkOverhead) = kPaddingValue;
jeffhaoc1160702011-10-27 15:48:45 -0700225 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700226 size_t allocation_size = AllocationSize(result);
227 num_bytes_allocated_ += allocation_size;
228 total_bytes_allocated_ += allocation_size;
229 ++total_objects_allocated_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700230 ++num_objects_allocated_;
Ian Rogers30fab402012-01-23 15:43:46 -0800231 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700232}
233
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234mirror::Object* DlMallocSpace::Alloc(Thread* self, size_t num_bytes) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700235 MutexLock mu(self, lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236 return AllocWithoutGrowthLocked(num_bytes);
237}
238
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700240 MutexLock mu(self, lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800241 // Grow as much as possible within the mspace.
242 size_t max_allowed = Capacity();
243 mspace_set_footprint_limit(mspace_, max_allowed);
244 // Try the allocation.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 mirror::Object* result = AllocWithoutGrowthLocked(num_bytes);
Ian Rogers30fab402012-01-23 15:43:46 -0800246 // Shrink back down as small as possible.
247 size_t footprint = mspace_footprint(mspace_);
248 mspace_set_footprint_limit(mspace_, footprint);
249 // Return the new allocation or NULL.
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700250 CHECK(!kDebugSpaces || result == NULL || Contains(result));
Ian Rogers30fab402012-01-23 15:43:46 -0800251 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700252}
253
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700254void DlMallocSpace::SetGrowthLimit(size_t growth_limit) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700255 growth_limit = RoundUp(growth_limit, kPageSize);
256 growth_limit_ = growth_limit;
257 if (Size() > growth_limit_) {
258 end_ = begin_ + growth_limit;
259 }
260}
261
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700262DlMallocSpace* DlMallocSpace::CreateZygoteSpace() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700263 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700264 DCHECK(IsAligned<CardTable::kCardSize>(begin_));
265 DCHECK(IsAligned<CardTable::kCardSize>(end_));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700266 DCHECK(IsAligned<kPageSize>(begin_));
267 DCHECK(IsAligned<kPageSize>(end_));
268 size_t size = RoundUp(Size(), kPageSize);
269 // Trim the heap so that we minimize the size of the Zygote space.
270 Trim();
271 // Trim our mem-map to free unused pages.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700272 GetMemMap()->UnMapAtEnd(end_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700273 // TODO: Not hardcode these in?
274 const size_t starting_size = kPageSize;
275 const size_t initial_size = 2 * MB;
276 // Remaining size is for the new alloc space.
277 const size_t growth_limit = growth_limit_ - size;
278 const size_t capacity = Capacity() - size;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700279 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_) << "\n"
280 << "End " << reinterpret_cast<const void*>(end_) << "\n"
281 << "Size " << size << "\n"
282 << "GrowthLimit " << growth_limit_ << "\n"
283 << "Capacity " << Capacity();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700284 SetGrowthLimit(RoundUp(size, kPageSize));
285 SetFootprintLimit(RoundUp(size, kPageSize));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700286 // FIXME: Do we need reference counted pointers here?
287 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700288 VLOG(heap) << "Creating new AllocSpace: ";
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700289 VLOG(heap) << "Size " << GetMemMap()->Size();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700290 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
291 VLOG(heap) << "Capacity " << PrettySize(capacity);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700292 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(GetName().c_str(), End(), capacity, PROT_READ | PROT_WRITE));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700293 void* mspace = CreateMallocSpace(end_, starting_size, initial_size);
294 // Protect memory beyond the initial size.
295 byte* end = mem_map->Begin() + starting_size;
296 if (capacity - initial_size > 0) {
297 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name_.c_str());
298 }
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700299 DlMallocSpace* alloc_space =
300 new DlMallocSpace(name_, mem_map.release(), mspace, end_, end, growth_limit);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700301 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
302 CHECK_EQ(live_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
303 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(End()));
304 CHECK_EQ(mark_bitmap_->HeapLimit(), reinterpret_cast<uintptr_t>(End()));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700305 name_ += "-zygote-transformed";
306 VLOG(heap) << "zygote space creation done";
307 return alloc_space;
308}
309
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310size_t DlMallocSpace::Free(Thread* self, mirror::Object* ptr) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700311 MutexLock mu(self, lock_);
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700312 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700313 CHECK(ptr != NULL);
314 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700315 CHECK_EQ(
316 *reinterpret_cast<word*>(reinterpret_cast<byte*>(ptr) + AllocationSize(ptr) -
317 sizeof(word) - kChunkOverhead), kPaddingValue);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700318 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800319 const size_t bytes_freed = InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700320 num_bytes_allocated_ -= bytes_freed;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700321 --num_objects_allocated_;
Ian Rogers30fab402012-01-23 15:43:46 -0800322 mspace_free(mspace_, ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700323 return bytes_freed;
Ian Rogers30fab402012-01-23 15:43:46 -0800324}
325
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326size_t DlMallocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800327 DCHECK(ptrs != NULL);
328
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700329 // Don't need the lock to calculate the size of the freed pointers.
330 size_t bytes_freed = 0;
331 for (size_t i = 0; i < num_ptrs; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 mirror::Object* ptr = ptrs[i];
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800333 const size_t look_ahead = 8;
334 if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) {
335 // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
336 __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
337 }
338 bytes_freed += InternalAllocationSize(ptr);
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700339 }
340
Mathieu Chartier8e9a1492012-10-04 12:25:40 -0700341 if (kDebugSpaces) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700342 size_t num_broken_ptrs = 0;
343 for (size_t i = 0; i < num_ptrs; i++) {
344 if (!Contains(ptrs[i])) {
345 num_broken_ptrs++;
346 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
347 } else {
348 size_t size = mspace_usable_size(ptrs[i]);
349 memset(ptrs[i], 0xEF, size);
350 }
Ian Rogers30fab402012-01-23 15:43:46 -0800351 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700352 CHECK_EQ(num_broken_ptrs, 0u);
Ian Rogers30fab402012-01-23 15:43:46 -0800353 }
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800354
355 {
356 MutexLock mu(self, lock_);
357 num_bytes_allocated_ -= bytes_freed;
358 num_objects_allocated_ -= num_ptrs;
359 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
360 return bytes_freed;
361 }
Ian Rogers30fab402012-01-23 15:43:46 -0800362}
363
364// Callback from dlmalloc when it needs to increase the footprint
365extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800366 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700367 DCHECK_EQ(heap->GetAllocSpace()->GetMspace(), mspace);
368 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800369}
370
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700371void* DlMallocSpace::MoreCore(intptr_t increment) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700372 lock_.AssertHeld(Thread::Current());
Ian Rogers30fab402012-01-23 15:43:46 -0800373 byte* original_end = end_;
374 if (increment != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800375 VLOG(heap) << "AllocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800376 byte* new_end = original_end + increment;
377 if (increment > 0) {
378#if DEBUG_SPACES
379 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
380 // by mspace_set_footprint_limit.
381 CHECK_LE(new_end, Begin() + Capacity());
382#endif
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700383 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800384 } else {
385#if DEBUG_SPACES
386 // Should never be asked for negative footprint (ie before begin)
387 CHECK_GT(original_end + increment, Begin());
388#endif
389 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800390 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
391 // expensive (note the same isn't true for giving permissions to a page as the protected
392 // page shouldn't be in a TLB). We should investigate performance impact of just
393 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
394 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800395 size_t size = -increment;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700396 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetName());
397 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetName());
Ian Rogers30fab402012-01-23 15:43:46 -0800398 }
399 // Update end_
400 end_ = new_end;
401 }
402 return original_end;
403}
404
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800405// Virtual functions can't get inlined.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406inline size_t DlMallocSpace::InternalAllocationSize(const mirror::Object* obj) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700407 return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) +
408 kChunkOverhead;
Ian Rogers30fab402012-01-23 15:43:46 -0800409}
410
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411size_t DlMallocSpace::AllocationSize(const mirror::Object* obj) {
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800412 return InternalAllocationSize(obj);
413}
414
Ian Rogers48931882013-01-22 14:35:16 -0800415void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
Brian Carlstromb18e77a2012-08-21 14:20:03 -0700416 // Is this chunk in use?
417 if (used_bytes != 0) {
418 return;
419 }
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700420 // Do we have any whole pages to give back?
421 start = reinterpret_cast<void*>(RoundUp(reinterpret_cast<uintptr_t>(start), kPageSize));
422 end = reinterpret_cast<void*>(RoundDown(reinterpret_cast<uintptr_t>(end), kPageSize));
423 if (end > start) {
424 size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start);
425 CHECK_MEMORY_CALL(madvise, (start, length, MADV_DONTNEED), "trim");
Ian Rogers48931882013-01-22 14:35:16 -0800426 size_t* reclaimed = reinterpret_cast<size_t*>(arg);
427 *reclaimed += length;
Ian Rogers30fab402012-01-23 15:43:46 -0800428 }
429}
430
Ian Rogers48931882013-01-22 14:35:16 -0800431size_t DlMallocSpace::Trim() {
Ian Rogers50b35e22012-10-04 10:09:15 -0700432 MutexLock mu(Thread::Current(), lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700433 // Trim to release memory at the end of the space.
434 mspace_trim(mspace_, 0);
435 // Visit space looking for page-sized holes to advise the kernel we don't need.
Ian Rogers48931882013-01-22 14:35:16 -0800436 size_t reclaimed = 0;
437 mspace_inspect_all(mspace_, MspaceMadviseCallback, &reclaimed);
438 return reclaimed;
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700439}
Ian Rogers30fab402012-01-23 15:43:46 -0800440
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700441void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
Ian Rogers30fab402012-01-23 15:43:46 -0800442 void* arg) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700443 MutexLock mu(Thread::Current(), lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800444 mspace_inspect_all(mspace_, callback, arg);
Ian Rogers15bf2d32012-08-28 17:33:04 -0700445 callback(NULL, NULL, 0, arg); // Indicate end of a space.
Ian Rogers30fab402012-01-23 15:43:46 -0800446}
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 Chartierb062fdd2012-07-03 09:51:48 -0700466size_t ImageSpace::bitmap_index_ = 0;
467
468ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map)
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700469 : MemMapSpace(name, mem_map, mem_map->Size(), kGcRetentionPolicyNeverCollect) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700470 const size_t bitmap_index = bitmap_index_++;
471 live_bitmap_.reset(SpaceBitmap::Create(
Ian Rogersa40307e2013-02-22 11:32:44 -0800472 StringPrintf("imagespace %s live-bitmap %d", name.c_str(), static_cast<int>(bitmap_index)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700473 Begin(), Capacity()));
474 DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index;
475}
476
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700477ImageSpace* ImageSpace::Create(const std::string& image_file_name) {
Brian Carlstrom5643b782012-02-05 12:32:53 -0800478 CHECK(!image_file_name.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800479
480 uint64_t start_time = 0;
481 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
482 start_time = NanoTime();
483 LOG(INFO) << "Space::CreateImageSpace entering" << " image_file_name=" << image_file_name;
484 }
485
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700486 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700487 if (file.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800488 LOG(ERROR) << "Failed to open " << image_file_name;
489 return NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700490 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700491 ImageHeader image_header;
492 bool success = file->ReadFully(&image_header, sizeof(image_header));
493 if (!success || !image_header.IsValid()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800494 LOG(ERROR) << "Invalid image header " << image_file_name;
495 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700496 }
Ian Rogers30fab402012-01-23 15:43:46 -0800497 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Elliott Hughes76160052012-12-12 16:31:20 -0800498 file->GetLength(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800499 // TODO: selectively PROT_EXEC stubs
500 PROT_READ | PROT_WRITE | PROT_EXEC,
501 MAP_PRIVATE | MAP_FIXED,
502 file->Fd(),
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800503 0,
504 false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700505 if (map.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800506 LOG(ERROR) << "Failed to map " << image_file_name;
507 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700508 }
Ian Rogers30fab402012-01-23 15:43:46 -0800509 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
510 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700511
Ian Rogers30fab402012-01-23 15:43:46 -0800512 Runtime* runtime = Runtime::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 mirror::Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray);
514 runtime->SetJniDlsymLookupStub(down_cast<mirror::ByteArray*>(jni_stub_array));
Brian Carlstrom16192862011-09-12 17:50:06 -0700515
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516 mirror::Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray);
517 runtime->SetAbstractMethodErrorStubArray(down_cast<mirror::ByteArray*>(ame_stub_array));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700518
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800519 mirror::Object* resolution_stub_array =
Ian Rogersfb6adba2012-03-04 21:51:51 -0800520 image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700521 runtime->SetResolutionStubArray(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522 down_cast<mirror::ByteArray*>(resolution_stub_array), Runtime::kStaticMethod);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700523 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700524 runtime->SetResolutionStubArray(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525 down_cast<mirror::ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700526
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
528 runtime->SetResolutionMethod(down_cast<mirror::AbstractMethod*>(resolution_method));
Ian Rogers19846512012-02-24 11:42:47 -0800529
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
531 runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kSaveAll);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700532 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kRefsOnly);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700534 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800535 runtime->SetCalleeSaveMethod(down_cast<mirror::AbstractMethod*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogersff1ed472011-09-20 13:46:24 -0700536
Ian Rogers30fab402012-01-23 15:43:46 -0800537 ImageSpace* space = new ImageSpace(image_file_name, map.release());
538 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800539 LOG(INFO) << "Space::CreateImageSpace exiting (" << PrettyDuration(NanoTime() - start_time)
540 << ") " << *space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700541 }
Ian Rogers30fab402012-01-23 15:43:46 -0800542 return space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700543}
544
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700545void ImageSpace::RecordImageAllocations(SpaceBitmap* live_bitmap) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800546 uint64_t start_time = 0;
547 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
548 LOG(INFO) << "ImageSpace::RecordImageAllocations entering";
549 start_time = NanoTime();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700550 }
Ian Rogers30fab402012-01-23 15:43:46 -0800551 DCHECK(!Runtime::Current()->IsStarted());
552 CHECK(live_bitmap != NULL);
553 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
554 byte* end = End();
555 while (current < end) {
556 DCHECK_ALIGNED(current, kObjectAlignment);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(current);
Ian Rogers30fab402012-01-23 15:43:46 -0800558 live_bitmap->Set(obj);
559 current += RoundUp(obj->SizeOf(), kObjectAlignment);
560 }
561 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800562 LOG(INFO) << "ImageSpace::RecordImageAllocations exiting ("
563 << PrettyDuration(NanoTime() - start_time) << ")";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700564 }
565}
566
Ian Rogers30fab402012-01-23 15:43:46 -0800567std::ostream& operator<<(std::ostream& os, const Space& space) {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700568 space.Dump(os);
Ian Rogers30fab402012-01-23 15:43:46 -0800569 return os;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700570}
571
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700572void DlMallocSpace::Dump(std::ostream& os) const {
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700573 os << GetType()
574 << "begin=" << reinterpret_cast<void*>(Begin())
575 << ",end=" << reinterpret_cast<void*>(End())
576 << ",size=" << PrettySize(Size()) << ",capacity=" << PrettySize(Capacity())
577 << ",name=\"" << GetName() << "\"]";
578}
579
580void ImageSpace::Dump(std::ostream& os) const {
581 os << GetType()
582 << "begin=" << reinterpret_cast<void*>(Begin())
583 << ",end=" << reinterpret_cast<void*>(End())
584 << ",size=" << PrettySize(Size())
585 << ",name=\"" << GetName() << "\"]";
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700586}
587
Carl Shapiro69759ea2011-07-21 18:13:35 -0700588} // namespace art