blob: a828d91d8d0118e45ba9ec88da513c2ee787f0ad [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 Hughes90a33692011-08-30 13:27:07 -070019#include "UniquePtr.h"
Ian Rogers30fab402012-01-23 15:43:46 -080020#include "dlmalloc.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070021#include "file.h"
22#include "image.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "logging.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070024#include "os.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070025#include "space_bitmap.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070026#include "stl_util.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29namespace art {
30
Ian Rogers30fab402012-01-23 15:43:46 -080031#ifndef NDEBUG
32#define DEBUG_SPACES 1
33#endif
34
35#define CHECK_MEMORY_CALL(call, args, what) \
36 do { \
37 int rc = call args; \
38 if (UNLIKELY(rc != 0)) { \
39 errno = rc; \
40 PLOG(FATAL) << # call << " failed for " << what; \
41 } \
42 } while (false)
43
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070044size_t AllocSpace::bitmap_index_ = 0;
45
Mathieu Chartiercc236d72012-07-20 10:29:05 -070046AllocSpace::AllocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070047 size_t growth_limit)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 : Space(name, mem_map, begin, end, GCRP_ALWAYS_COLLECT), lock_("allocation space lock"),
49 mspace_(mspace), growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070050 CHECK(mspace != NULL);
51
52 size_t bitmap_index = bitmap_index_++;
53
Mathieu Chartiercc236d72012-07-20 10:29:05 -070054 DCHECK(reinterpret_cast<uintptr_t>(mem_map->Begin()) % static_cast<uintptr_t>GC_CARD_SIZE == 0);
55 DCHECK(reinterpret_cast<uintptr_t>(mem_map->End()) % static_cast<uintptr_t>GC_CARD_SIZE == 0);
56
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070057 live_bitmap_.reset(SpaceBitmap::Create(
58 StringPrintf("allocspace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
59 Begin(), Capacity()));
60 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
61
62 mark_bitmap_.reset(SpaceBitmap::Create(
63 StringPrintf("allocspace-%s-mark-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
64 Begin(), Capacity()));
65 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
66}
67
Ian Rogers30fab402012-01-23 15:43:46 -080068AllocSpace* Space::CreateAllocSpace(const std::string& name, size_t initial_size,
69 size_t growth_limit, size_t capacity,
70 byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080071 // Memory we promise to dlmalloc before it asks for morecore.
72 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
73 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
74 // size of the large allocation) will be greater than the footprint limit.
75 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -080076 uint64_t start_time = 0;
77 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
78 start_time = NanoTime();
79 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -080080 << " initial_size=" << PrettySize(initial_size)
81 << " growth_limit=" << PrettySize(growth_limit)
82 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -080083 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -070084 }
Ian Rogers30fab402012-01-23 15:43:46 -080085
86 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -080087 if (starting_size > initial_size) {
88 initial_size = starting_size;
89 }
Ian Rogers30fab402012-01-23 15:43:46 -080090 if (initial_size > growth_limit) {
91 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -080092 << PrettySize(initial_size) << ") is larger than its capacity ("
93 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -080094 return NULL;
95 }
96 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080097 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
98 << PrettySize(growth_limit) << ") is larger than the capacity ("
99 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -0800100 return NULL;
101 }
102
103 // Page align growth limit and capacity which will be used to manage mmapped storage
104 growth_limit = RoundUp(growth_limit, kPageSize);
105 capacity = RoundUp(capacity, kPageSize);
106
107 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin,
108 capacity, PROT_READ | PROT_WRITE));
109 if (mem_map.get() == NULL) {
110 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers3bb17a62012-01-27 23:56:44 -0800111 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -0800112 return NULL;
113 }
114
Ian Rogers3bb17a62012-01-27 23:56:44 -0800115 void* mspace = AllocSpace::CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800116 if (mspace == NULL) {
117 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
118 return NULL;
119 }
120
Ian Rogers3bb17a62012-01-27 23:56:44 -0800121 // Protect memory beyond the initial size.
122 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800123 if (capacity - initial_size > 0) {
124 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
125 }
126
127 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700128 MemMap* mem_map_ptr = mem_map.release();
129 AllocSpace* space = new AllocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end, growth_limit);
Ian Rogers30fab402012-01-23 15:43:46 -0800130 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800131 LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time)
132 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800133 }
134 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700135}
136
Ian Rogers3bb17a62012-01-27 23:56:44 -0800137void* AllocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800138 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700139 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800140 // create mspace using our backing storage starting at begin and with a footprint of
141 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
142 // morecore_start bytes of memory is exhaused morecore will be called.
143 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700144 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800145 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800146 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800148 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700149 }
150 return msp;
151}
152
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700153void AllocSpace::SwapBitmaps() {
154 SpaceBitmap* temp_live_bitmap = live_bitmap_.release();
155 live_bitmap_.reset(mark_bitmap_.release());
156 mark_bitmap_.reset(temp_live_bitmap);
157}
158
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159Object* AllocSpace::AllocWithoutGrowthLocked(size_t num_bytes) {
Ian Rogers30fab402012-01-23 15:43:46 -0800160 Object* result = reinterpret_cast<Object*>(mspace_calloc(mspace_, 1, num_bytes));
161#if DEBUG_SPACES
162 if (result != NULL) {
163 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 << ") not in bounds of allocation space " << *this;
jeffhaoc1160702011-10-27 15:48:45 -0700165 }
Ian Rogers30fab402012-01-23 15:43:46 -0800166#endif
167 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700168}
169
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170Object* AllocSpace::AllocWithoutGrowth(size_t num_bytes) {
171 MutexLock mu(lock_);
172 return AllocWithoutGrowthLocked(num_bytes);
173}
174
Ian Rogers30fab402012-01-23 15:43:46 -0800175Object* AllocSpace::AllocWithGrowth(size_t num_bytes) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800177 // Grow as much as possible within the mspace.
178 size_t max_allowed = Capacity();
179 mspace_set_footprint_limit(mspace_, max_allowed);
180 // Try the allocation.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700181 void* ptr = AllocWithoutGrowthLocked(num_bytes);
Ian Rogers30fab402012-01-23 15:43:46 -0800182 // Shrink back down as small as possible.
183 size_t footprint = mspace_footprint(mspace_);
184 mspace_set_footprint_limit(mspace_, footprint);
185 // Return the new allocation or NULL.
186 Object* result = reinterpret_cast<Object*>(ptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187#if DEBUG_SPACES
Ian Rogers30fab402012-01-23 15:43:46 -0800188 CHECK(result == NULL || Contains(result));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700189#endif
Ian Rogers30fab402012-01-23 15:43:46 -0800190 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700191}
192
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700193AllocSpace* AllocSpace::CreateZygoteSpace() {
194 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
195 DCHECK(IsAligned<GC_CARD_SIZE>(begin_));
196 DCHECK(IsAligned<GC_CARD_SIZE>(end_));
197 DCHECK(IsAligned<kPageSize>(begin_));
198 DCHECK(IsAligned<kPageSize>(end_));
199 size_t size = RoundUp(Size(), kPageSize);
200 // Trim the heap so that we minimize the size of the Zygote space.
201 Trim();
202 // Trim our mem-map to free unused pages.
203 mem_map_->UnMapAtEnd(end_);
204 // TODO: Not hardcode these in?
205 const size_t starting_size = kPageSize;
206 const size_t initial_size = 2 * MB;
207 // Remaining size is for the new alloc space.
208 const size_t growth_limit = growth_limit_ - size;
209 const size_t capacity = Capacity() - size;
210 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_);
211 VLOG(heap) << "End " << reinterpret_cast<const void*>(end_);
212 VLOG(heap) << "Size " << size;
213 VLOG(heap) << "GrowthLimit " << growth_limit_;
214 VLOG(heap) << "Capacity " << Capacity();
215 growth_limit_ = RoundUp(size, kPageSize);
216 // FIXME: Do we need reference counted pointers here?
217 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700218 VLOG(heap) << "Creating new AllocSpace: ";
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700219 VLOG(heap) << "Size " << mem_map_->Size();
220 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
221 VLOG(heap) << "Capacity " << PrettySize(capacity);
222 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name_.c_str(), end_, capacity, PROT_READ | PROT_WRITE));
223 void* mspace = CreateMallocSpace(end_, starting_size, initial_size);
224 // Protect memory beyond the initial size.
225 byte* end = mem_map->Begin() + starting_size;
226 if (capacity - initial_size > 0) {
227 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name_.c_str());
228 }
229 AllocSpace* alloc_space = new AllocSpace(name_, mem_map.release(), mspace, end_, end, growth_limit);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700230 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(end_));
231 CHECK(live_bitmap_->HeapLimit() == reinterpret_cast<uintptr_t>(end_));
232 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(end_));
233 CHECK(mark_bitmap_->HeapLimit() == reinterpret_cast<uintptr_t>(end_));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700234 name_ += "-zygote-transformed";
235 VLOG(heap) << "zygote space creation done";
236 return alloc_space;
237}
238
Ian Rogers30fab402012-01-23 15:43:46 -0800239void AllocSpace::Free(Object* ptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800241#if DEBUG_SPACES
242 CHECK(ptr != NULL);
243 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
244#endif
245 mspace_free(mspace_, ptr);
246}
247
248void AllocSpace::FreeList(size_t num_ptrs, Object** ptrs) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800250#if DEBUG_SPACES
251 CHECK(ptrs != NULL);
252 size_t num_broken_ptrs = 0;
253 for (size_t i = 0; i < num_ptrs; i++) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700254 if (!Contains(ptrs[i])) {
Ian Rogers30fab402012-01-23 15:43:46 -0800255 num_broken_ptrs++;
256 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
257 }
258 }
259 CHECK_EQ(num_broken_ptrs, 0u);
260#endif
261 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
262}
263
264// Callback from dlmalloc when it needs to increase the footprint
265extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800266 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700267 if (heap->GetAllocSpace()->GetMspace() == mspace) {
268 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800269 } else {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700270 // Exhaustively search alloc spaces.
271 const Spaces& spaces = heap->GetSpaces();
272 // TODO: C++0x auto
273 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
274 if ((*cur)->IsAllocSpace()) {
275 AllocSpace* space = (*cur)->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800276 if (mspace == space->GetMspace()) {
277 return space->MoreCore(increment);
278 }
279 }
280 }
Ian Rogers30fab402012-01-23 15:43:46 -0800281 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700282
283 LOG(FATAL) << "Unexpected call to art_heap_morecore. mspace: " << mspace
284 << " increment: " << increment;
285 return NULL;
Ian Rogers30fab402012-01-23 15:43:46 -0800286}
287
288void* AllocSpace::MoreCore(intptr_t increment) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 lock_.AssertHeld();
Ian Rogers30fab402012-01-23 15:43:46 -0800290 byte* original_end = end_;
291 if (increment != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800292 VLOG(heap) << "AllocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800293 byte* new_end = original_end + increment;
294 if (increment > 0) {
295#if DEBUG_SPACES
296 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
297 // by mspace_set_footprint_limit.
298 CHECK_LE(new_end, Begin() + Capacity());
299#endif
300 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetSpaceName());
301 } else {
302#if DEBUG_SPACES
303 // Should never be asked for negative footprint (ie before begin)
304 CHECK_GT(original_end + increment, Begin());
305#endif
306 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800307 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
308 // expensive (note the same isn't true for giving permissions to a page as the protected
309 // page shouldn't be in a TLB). We should investigate performance impact of just
310 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
311 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800312 size_t size = -increment;
313 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetSpaceName());
314 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetSpaceName());
315 }
316 // Update end_
317 end_ = new_end;
318 }
319 return original_end;
320}
321
322size_t AllocSpace::AllocationSize(const Object* obj) {
323 return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) + kChunkOverhead;
324}
325
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700326void MspaceMadviseCallback(void* start, void* end, void* /*arg*/) {
327 // Do we have any whole pages to give back?
328 start = reinterpret_cast<void*>(RoundUp(reinterpret_cast<uintptr_t>(start), kPageSize));
329 end = reinterpret_cast<void*>(RoundDown(reinterpret_cast<uintptr_t>(end), kPageSize));
330 if (end > start) {
331 size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start);
332 CHECK_MEMORY_CALL(madvise, (start, length, MADV_DONTNEED), "trim");
Ian Rogers30fab402012-01-23 15:43:46 -0800333 }
334}
335
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700336void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
337 // Is this chunk in use?
338 if (used_bytes != 0) {
339 return;
340 }
341 return MspaceMadviseCallback(start, end, arg);
Ian Rogers30fab402012-01-23 15:43:46 -0800342}
343
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700344void AllocSpace::Trim() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 MutexLock mu(lock_);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700346 // Trim to release memory at the end of the space.
347 mspace_trim(mspace_, 0);
348 // Visit space looking for page-sized holes to advise the kernel we don't need.
349 mspace_inspect_all(mspace_, MspaceMadviseCallback, NULL);
350}
Ian Rogers30fab402012-01-23 15:43:46 -0800351
352void AllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
353 void* arg) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800355 mspace_inspect_all(mspace_, callback, arg);
356}
357
358size_t AllocSpace::GetFootprintLimit() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 MutexLock mu(lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800360 return mspace_footprint_limit(mspace_);
361}
362
363void AllocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700364 MutexLock mu(lock_);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800365 VLOG(heap) << "AllocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800366 // Compare against the actual footprint, rather than the Size(), because the heap may not have
367 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800368 size_t current_space_size = mspace_footprint(mspace_);
369 if (new_size < current_space_size) {
370 // Don't let the space grow any more.
371 new_size = current_space_size;
372 }
373 mspace_set_footprint_limit(mspace_, new_size);
374}
375
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700376size_t ImageSpace::bitmap_index_ = 0;
377
378ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700379 : Space(name, mem_map, mem_map->Begin(), mem_map->End(), GCRP_NEVER_COLLECT) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700380 const size_t bitmap_index = bitmap_index_++;
381 live_bitmap_.reset(SpaceBitmap::Create(
382 StringPrintf("imagespace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
383 Begin(), Capacity()));
384 DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index;
385}
386
Ian Rogers30fab402012-01-23 15:43:46 -0800387ImageSpace* Space::CreateImageSpace(const std::string& image_file_name) {
Brian Carlstrom5643b782012-02-05 12:32:53 -0800388 CHECK(!image_file_name.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800389
390 uint64_t start_time = 0;
391 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
392 start_time = NanoTime();
393 LOG(INFO) << "Space::CreateImageSpace entering" << " image_file_name=" << image_file_name;
394 }
395
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700396 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700397 if (file.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800398 LOG(ERROR) << "Failed to open " << image_file_name;
399 return NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700400 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700401 ImageHeader image_header;
402 bool success = file->ReadFully(&image_header, sizeof(image_header));
403 if (!success || !image_header.IsValid()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800404 LOG(ERROR) << "Invalid image header " << image_file_name;
405 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700406 }
Ian Rogers30fab402012-01-23 15:43:46 -0800407 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800408 file->Length(),
409 // TODO: selectively PROT_EXEC stubs
410 PROT_READ | PROT_WRITE | PROT_EXEC,
411 MAP_PRIVATE | MAP_FIXED,
412 file->Fd(),
413 0));
Elliott Hughes90a33692011-08-30 13:27:07 -0700414 if (map.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800415 LOG(ERROR) << "Failed to map " << image_file_name;
416 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700417 }
Ian Rogers30fab402012-01-23 15:43:46 -0800418 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
419 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700420
Ian Rogers30fab402012-01-23 15:43:46 -0800421 Runtime* runtime = Runtime::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -0700422 Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray);
Ian Rogers169c9a72011-11-13 20:13:17 -0800423 runtime->SetJniDlsymLookupStub(down_cast<ByteArray*>(jni_stub_array));
Brian Carlstrom16192862011-09-12 17:50:06 -0700424
Brian Carlstrome24fa612011-09-29 00:53:55 -0700425 Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700426 runtime->SetAbstractMethodErrorStubArray(down_cast<ByteArray*>(ame_stub_array));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700427
Ian Rogersfb6adba2012-03-04 21:51:51 -0800428 Object* resolution_stub_array =
429 image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700430 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700431 down_cast<ByteArray*>(resolution_stub_array), Runtime::kStaticMethod);
432 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700433 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700434 down_cast<ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700435
Ian Rogers19846512012-02-24 11:42:47 -0800436 Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
437 runtime->SetResolutionMethod(down_cast<Method*>(resolution_method));
438
Ian Rogersff1ed472011-09-20 13:46:24 -0700439 Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700440 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kSaveAll);
441 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
442 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsOnly);
443 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
444 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogersff1ed472011-09-20 13:46:24 -0700445
Ian Rogers30fab402012-01-23 15:43:46 -0800446 ImageSpace* space = new ImageSpace(image_file_name, map.release());
447 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800448 LOG(INFO) << "Space::CreateImageSpace exiting (" << PrettyDuration(NanoTime() - start_time)
449 << ") " << *space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700450 }
Ian Rogers30fab402012-01-23 15:43:46 -0800451 return space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700452}
453
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700454void ImageSpace::RecordImageAllocations(SpaceBitmap* live_bitmap) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800455 uint64_t start_time = 0;
456 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
457 LOG(INFO) << "ImageSpace::RecordImageAllocations entering";
458 start_time = NanoTime();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700459 }
Ian Rogers30fab402012-01-23 15:43:46 -0800460 DCHECK(!Runtime::Current()->IsStarted());
461 CHECK(live_bitmap != NULL);
462 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
463 byte* end = End();
464 while (current < end) {
465 DCHECK_ALIGNED(current, kObjectAlignment);
466 const Object* obj = reinterpret_cast<const Object*>(current);
467 live_bitmap->Set(obj);
468 current += RoundUp(obj->SizeOf(), kObjectAlignment);
469 }
470 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800471 LOG(INFO) << "ImageSpace::RecordImageAllocations exiting ("
472 << PrettyDuration(NanoTime() - start_time) << ")";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700473 }
474}
475
Ian Rogers30fab402012-01-23 15:43:46 -0800476std::ostream& operator<<(std::ostream& os, const Space& space) {
477 os << (space.IsImageSpace() ? "Image" : "Alloc") << "Space["
478 << "begin=" << reinterpret_cast<void*>(space.Begin())
479 << ",end=" << reinterpret_cast<void*>(space.End())
Ian Rogers3bb17a62012-01-27 23:56:44 -0800480 << ",size=" << PrettySize(space.Size()) << ",capacity=" << PrettySize(space.Capacity())
Ian Rogers30fab402012-01-23 15:43:46 -0800481 << ",name=\"" << space.GetSpaceName() << "\"]";
482 return os;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700483}
484
485} // namespace art