blob: 24eca26c39aae7181c4eaaac0e39325f466da9d3 [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)
Mathieu Chartiercc236d72012-07-20 10:29:05 -070048 : Space(name, mem_map, begin, end, GCRP_ALWAYS_COLLECT), mspace_(mspace), growth_limit_(growth_limit) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070049 CHECK(mspace != NULL);
50
51 size_t bitmap_index = bitmap_index_++;
52
Mathieu Chartiercc236d72012-07-20 10:29:05 -070053 DCHECK(reinterpret_cast<uintptr_t>(mem_map->Begin()) % static_cast<uintptr_t>GC_CARD_SIZE == 0);
54 DCHECK(reinterpret_cast<uintptr_t>(mem_map->End()) % static_cast<uintptr_t>GC_CARD_SIZE == 0);
55
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070056 live_bitmap_.reset(SpaceBitmap::Create(
57 StringPrintf("allocspace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
58 Begin(), Capacity()));
59 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index;
60
61 mark_bitmap_.reset(SpaceBitmap::Create(
62 StringPrintf("allocspace-%s-mark-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
63 Begin(), Capacity()));
64 DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index;
65}
66
Ian Rogers30fab402012-01-23 15:43:46 -080067AllocSpace* Space::CreateAllocSpace(const std::string& name, size_t initial_size,
68 size_t growth_limit, size_t capacity,
69 byte* requested_begin) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080070 // Memory we promise to dlmalloc before it asks for morecore.
71 // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc
72 // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the
73 // size of the large allocation) will be greater than the footprint limit.
74 size_t starting_size = kPageSize;
Ian Rogers30fab402012-01-23 15:43:46 -080075 uint64_t start_time = 0;
76 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
77 start_time = NanoTime();
78 VLOG(startup) << "Space::CreateAllocSpace entering " << name
Ian Rogers3bb17a62012-01-27 23:56:44 -080079 << " initial_size=" << PrettySize(initial_size)
80 << " growth_limit=" << PrettySize(growth_limit)
81 << " capacity=" << PrettySize(capacity)
Ian Rogers30fab402012-01-23 15:43:46 -080082 << " requested_begin=" << reinterpret_cast<void*>(requested_begin);
Carl Shapiro69759ea2011-07-21 18:13:35 -070083 }
Ian Rogers30fab402012-01-23 15:43:46 -080084
85 // Sanity check arguments
Ian Rogers3bb17a62012-01-27 23:56:44 -080086 if (starting_size > initial_size) {
87 initial_size = starting_size;
88 }
Ian Rogers30fab402012-01-23 15:43:46 -080089 if (initial_size > growth_limit) {
90 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size ("
Ian Rogers3bb17a62012-01-27 23:56:44 -080091 << PrettySize(initial_size) << ") is larger than its capacity ("
92 << PrettySize(growth_limit) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -080093 return NULL;
94 }
95 if (growth_limit > capacity) {
Ian Rogers3bb17a62012-01-27 23:56:44 -080096 LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity ("
97 << PrettySize(growth_limit) << ") is larger than the capacity ("
98 << PrettySize(capacity) << ")";
Ian Rogers30fab402012-01-23 15:43:46 -080099 return NULL;
100 }
101
102 // Page align growth limit and capacity which will be used to manage mmapped storage
103 growth_limit = RoundUp(growth_limit, kPageSize);
104 capacity = RoundUp(capacity, kPageSize);
105
106 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin,
107 capacity, PROT_READ | PROT_WRITE));
108 if (mem_map.get() == NULL) {
109 LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size "
Ian Rogers3bb17a62012-01-27 23:56:44 -0800110 << PrettySize(capacity);
Ian Rogers30fab402012-01-23 15:43:46 -0800111 return NULL;
112 }
113
Ian Rogers3bb17a62012-01-27 23:56:44 -0800114 void* mspace = AllocSpace::CreateMallocSpace(mem_map->Begin(), starting_size, initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800115 if (mspace == NULL) {
116 LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")";
117 return NULL;
118 }
119
Ian Rogers3bb17a62012-01-27 23:56:44 -0800120 // Protect memory beyond the initial size.
121 byte* end = mem_map->Begin() + starting_size;
Ian Rogers30fab402012-01-23 15:43:46 -0800122 if (capacity - initial_size > 0) {
123 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name);
124 }
125
126 // Everything is set so record in immutable structure and leave
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700127 MemMap* mem_map_ptr = mem_map.release();
128 AllocSpace* space = new AllocSpace(name, mem_map_ptr, mspace, mem_map_ptr->Begin(), end, growth_limit);
Ian Rogers30fab402012-01-23 15:43:46 -0800129 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800130 LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time)
131 << " ) " << *space;
Ian Rogers30fab402012-01-23 15:43:46 -0800132 }
133 return space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700134}
135
Ian Rogers3bb17a62012-01-27 23:56:44 -0800136void* AllocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) {
Ian Rogers30fab402012-01-23 15:43:46 -0800137 // clear errno to allow PLOG on error
Carl Shapiro69759ea2011-07-21 18:13:35 -0700138 errno = 0;
Ian Rogers3bb17a62012-01-27 23:56:44 -0800139 // create mspace using our backing storage starting at begin and with a footprint of
140 // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When
141 // morecore_start bytes of memory is exhaused morecore will be called.
142 void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700143 if (msp != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800144 // Do not allow morecore requests to succeed beyond the initial size of the heap
Ian Rogers3bb17a62012-01-27 23:56:44 -0800145 mspace_set_footprint_limit(msp, initial_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700146 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800147 PLOG(ERROR) << "create_mspace_with_base failed";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700148 }
149 return msp;
150}
151
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700152void AllocSpace::SwapBitmaps() {
153 SpaceBitmap* temp_live_bitmap = live_bitmap_.release();
154 live_bitmap_.reset(mark_bitmap_.release());
155 mark_bitmap_.reset(temp_live_bitmap);
156}
157
Ian Rogers30fab402012-01-23 15:43:46 -0800158Object* AllocSpace::AllocWithoutGrowth(size_t num_bytes) {
159 Object* result = reinterpret_cast<Object*>(mspace_calloc(mspace_, 1, num_bytes));
160#if DEBUG_SPACES
161 if (result != NULL) {
162 CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result)
163 << ") not in bounds of heap " << *this;
jeffhaoc1160702011-10-27 15:48:45 -0700164 }
Ian Rogers30fab402012-01-23 15:43:46 -0800165#endif
166 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700167}
168
Ian Rogers30fab402012-01-23 15:43:46 -0800169Object* AllocSpace::AllocWithGrowth(size_t num_bytes) {
170 // Grow as much as possible within the mspace.
171 size_t max_allowed = Capacity();
172 mspace_set_footprint_limit(mspace_, max_allowed);
173 // Try the allocation.
174 void* ptr = AllocWithoutGrowth(num_bytes);
175 // Shrink back down as small as possible.
176 size_t footprint = mspace_footprint(mspace_);
177 mspace_set_footprint_limit(mspace_, footprint);
178 // Return the new allocation or NULL.
179 Object* result = reinterpret_cast<Object*>(ptr);
180 CHECK(result == NULL || Contains(result));
181 return result;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700182}
183
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700184AllocSpace* AllocSpace::CreateZygoteSpace() {
185 end_ = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(end_), kPageSize));
186 DCHECK(IsAligned<GC_CARD_SIZE>(begin_));
187 DCHECK(IsAligned<GC_CARD_SIZE>(end_));
188 DCHECK(IsAligned<kPageSize>(begin_));
189 DCHECK(IsAligned<kPageSize>(end_));
190 size_t size = RoundUp(Size(), kPageSize);
191 // Trim the heap so that we minimize the size of the Zygote space.
192 Trim();
193 // Trim our mem-map to free unused pages.
194 mem_map_->UnMapAtEnd(end_);
195 // TODO: Not hardcode these in?
196 const size_t starting_size = kPageSize;
197 const size_t initial_size = 2 * MB;
198 // Remaining size is for the new alloc space.
199 const size_t growth_limit = growth_limit_ - size;
200 const size_t capacity = Capacity() - size;
201 VLOG(heap) << "Begin " << reinterpret_cast<const void*>(begin_);
202 VLOG(heap) << "End " << reinterpret_cast<const void*>(end_);
203 VLOG(heap) << "Size " << size;
204 VLOG(heap) << "GrowthLimit " << growth_limit_;
205 VLOG(heap) << "Capacity " << Capacity();
206 growth_limit_ = RoundUp(size, kPageSize);
207 // FIXME: Do we need reference counted pointers here?
208 // Make the two spaces share the same mark bitmaps since the bitmaps span both of the spaces.
209 VLOG(heap) << "Creating new alloc space: ";
210 VLOG(heap) << "Size " << mem_map_->Size();
211 VLOG(heap) << "GrowthLimit " << PrettySize(growth_limit);
212 VLOG(heap) << "Capacity " << PrettySize(capacity);
213 UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name_.c_str(), end_, capacity, PROT_READ | PROT_WRITE));
214 void* mspace = CreateMallocSpace(end_, starting_size, initial_size);
215 // Protect memory beyond the initial size.
216 byte* end = mem_map->Begin() + starting_size;
217 if (capacity - initial_size > 0) {
218 CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name_.c_str());
219 }
220 AllocSpace* alloc_space = new AllocSpace(name_, mem_map.release(), mspace, end_, end, growth_limit);
221 live_bitmap_->Trim(Capacity()); // TODO - kPageSize?
222 mark_bitmap_->Trim(Capacity()); // TODO - kPageSize?
223 name_ += "-zygote-transformed";
224 VLOG(heap) << "zygote space creation done";
225 return alloc_space;
226}
227
Ian Rogers30fab402012-01-23 15:43:46 -0800228void AllocSpace::Free(Object* ptr) {
229#if DEBUG_SPACES
230 CHECK(ptr != NULL);
231 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
232#endif
233 mspace_free(mspace_, ptr);
234}
235
236void AllocSpace::FreeList(size_t num_ptrs, Object** ptrs) {
237#if DEBUG_SPACES
238 CHECK(ptrs != NULL);
239 size_t num_broken_ptrs = 0;
240 for (size_t i = 0; i < num_ptrs; i++) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700241 if (!Contains(ptrs[i])) {
Ian Rogers30fab402012-01-23 15:43:46 -0800242 num_broken_ptrs++;
243 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
244 }
245 }
246 CHECK_EQ(num_broken_ptrs, 0u);
247#endif
248 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
249}
250
251// Callback from dlmalloc when it needs to increase the footprint
252extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800253 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700254 if (heap->GetAllocSpace()->GetMspace() == mspace) {
255 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800256 } else {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700257 // Exhaustively search alloc spaces.
258 const Spaces& spaces = heap->GetSpaces();
259 // TODO: C++0x auto
260 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
261 if ((*cur)->IsAllocSpace()) {
262 AllocSpace* space = (*cur)->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800263 if (mspace == space->GetMspace()) {
264 return space->MoreCore(increment);
265 }
266 }
267 }
Ian Rogers30fab402012-01-23 15:43:46 -0800268 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700269
270 LOG(FATAL) << "Unexpected call to art_heap_morecore. mspace: " << mspace
271 << " increment: " << increment;
272 return NULL;
Ian Rogers30fab402012-01-23 15:43:46 -0800273}
274
275void* AllocSpace::MoreCore(intptr_t increment) {
276 byte* original_end = end_;
277 if (increment != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800278 VLOG(heap) << "AllocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800279 byte* new_end = original_end + increment;
280 if (increment > 0) {
281#if DEBUG_SPACES
282 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
283 // by mspace_set_footprint_limit.
284 CHECK_LE(new_end, Begin() + Capacity());
285#endif
286 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetSpaceName());
287 } else {
288#if DEBUG_SPACES
289 // Should never be asked for negative footprint (ie before begin)
290 CHECK_GT(original_end + increment, Begin());
291#endif
292 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800293 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
294 // expensive (note the same isn't true for giving permissions to a page as the protected
295 // page shouldn't be in a TLB). We should investigate performance impact of just
296 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
297 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800298 size_t size = -increment;
299 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetSpaceName());
300 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetSpaceName());
301 }
302 // Update end_
303 end_ = new_end;
304 }
305 return original_end;
306}
307
308size_t AllocSpace::AllocationSize(const Object* obj) {
309 return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) + kChunkOverhead;
310}
311
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700312void MspaceMadviseCallback(void* start, void* end, void* /*arg*/) {
313 // Do we have any whole pages to give back?
314 start = reinterpret_cast<void*>(RoundUp(reinterpret_cast<uintptr_t>(start), kPageSize));
315 end = reinterpret_cast<void*>(RoundDown(reinterpret_cast<uintptr_t>(end), kPageSize));
316 if (end > start) {
317 size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start);
318 CHECK_MEMORY_CALL(madvise, (start, length, MADV_DONTNEED), "trim");
Ian Rogers30fab402012-01-23 15:43:46 -0800319 }
320}
321
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700322void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
323 // Is this chunk in use?
324 if (used_bytes != 0) {
325 return;
326 }
327 return MspaceMadviseCallback(start, end, arg);
Ian Rogers30fab402012-01-23 15:43:46 -0800328}
329
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700330void AllocSpace::Trim() {
331 // Trim to release memory at the end of the space.
332 mspace_trim(mspace_, 0);
333 // Visit space looking for page-sized holes to advise the kernel we don't need.
334 mspace_inspect_all(mspace_, MspaceMadviseCallback, NULL);
335}
Ian Rogers30fab402012-01-23 15:43:46 -0800336
337void AllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
338 void* arg) {
339 mspace_inspect_all(mspace_, callback, arg);
340}
341
342size_t AllocSpace::GetFootprintLimit() {
343 return mspace_footprint_limit(mspace_);
344}
345
346void AllocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800347 VLOG(heap) << "AllocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800348 // Compare against the actual footprint, rather than the Size(), because the heap may not have
349 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800350 size_t current_space_size = mspace_footprint(mspace_);
351 if (new_size < current_space_size) {
352 // Don't let the space grow any more.
353 new_size = current_space_size;
354 }
355 mspace_set_footprint_limit(mspace_, new_size);
356}
357
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700358size_t ImageSpace::bitmap_index_ = 0;
359
360ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700361 : Space(name, mem_map, mem_map->Begin(), mem_map->End(), GCRP_NEVER_COLLECT) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700362 const size_t bitmap_index = bitmap_index_++;
363 live_bitmap_.reset(SpaceBitmap::Create(
364 StringPrintf("imagespace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
365 Begin(), Capacity()));
366 DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index;
367}
368
Ian Rogers30fab402012-01-23 15:43:46 -0800369ImageSpace* Space::CreateImageSpace(const std::string& image_file_name) {
Brian Carlstrom5643b782012-02-05 12:32:53 -0800370 CHECK(!image_file_name.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800371
372 uint64_t start_time = 0;
373 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
374 start_time = NanoTime();
375 LOG(INFO) << "Space::CreateImageSpace entering" << " image_file_name=" << image_file_name;
376 }
377
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700378 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700379 if (file.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800380 LOG(ERROR) << "Failed to open " << image_file_name;
381 return NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700382 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700383 ImageHeader image_header;
384 bool success = file->ReadFully(&image_header, sizeof(image_header));
385 if (!success || !image_header.IsValid()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800386 LOG(ERROR) << "Invalid image header " << image_file_name;
387 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700388 }
Ian Rogers30fab402012-01-23 15:43:46 -0800389 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800390 file->Length(),
391 // TODO: selectively PROT_EXEC stubs
392 PROT_READ | PROT_WRITE | PROT_EXEC,
393 MAP_PRIVATE | MAP_FIXED,
394 file->Fd(),
395 0));
Elliott Hughes90a33692011-08-30 13:27:07 -0700396 if (map.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800397 LOG(ERROR) << "Failed to map " << image_file_name;
398 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700399 }
Ian Rogers30fab402012-01-23 15:43:46 -0800400 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
401 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700402
Ian Rogers30fab402012-01-23 15:43:46 -0800403 Runtime* runtime = Runtime::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -0700404 Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray);
Ian Rogers169c9a72011-11-13 20:13:17 -0800405 runtime->SetJniDlsymLookupStub(down_cast<ByteArray*>(jni_stub_array));
Brian Carlstrom16192862011-09-12 17:50:06 -0700406
Brian Carlstrome24fa612011-09-29 00:53:55 -0700407 Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700408 runtime->SetAbstractMethodErrorStubArray(down_cast<ByteArray*>(ame_stub_array));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409
Ian Rogersfb6adba2012-03-04 21:51:51 -0800410 Object* resolution_stub_array =
411 image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700412 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700413 down_cast<ByteArray*>(resolution_stub_array), Runtime::kStaticMethod);
414 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700415 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700416 down_cast<ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700417
Ian Rogers19846512012-02-24 11:42:47 -0800418 Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
419 runtime->SetResolutionMethod(down_cast<Method*>(resolution_method));
420
Ian Rogersff1ed472011-09-20 13:46:24 -0700421 Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700422 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kSaveAll);
423 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
424 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsOnly);
425 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
426 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogersff1ed472011-09-20 13:46:24 -0700427
Ian Rogers30fab402012-01-23 15:43:46 -0800428 ImageSpace* space = new ImageSpace(image_file_name, map.release());
429 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800430 LOG(INFO) << "Space::CreateImageSpace exiting (" << PrettyDuration(NanoTime() - start_time)
431 << ") " << *space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700432 }
Ian Rogers30fab402012-01-23 15:43:46 -0800433 return space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700434}
435
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700436void ImageSpace::RecordImageAllocations(SpaceBitmap* live_bitmap) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800437 uint64_t start_time = 0;
438 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
439 LOG(INFO) << "ImageSpace::RecordImageAllocations entering";
440 start_time = NanoTime();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700441 }
Ian Rogers30fab402012-01-23 15:43:46 -0800442 DCHECK(!Runtime::Current()->IsStarted());
443 CHECK(live_bitmap != NULL);
444 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
445 byte* end = End();
446 while (current < end) {
447 DCHECK_ALIGNED(current, kObjectAlignment);
448 const Object* obj = reinterpret_cast<const Object*>(current);
449 live_bitmap->Set(obj);
450 current += RoundUp(obj->SizeOf(), kObjectAlignment);
451 }
452 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800453 LOG(INFO) << "ImageSpace::RecordImageAllocations exiting ("
454 << PrettyDuration(NanoTime() - start_time) << ")";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700455 }
456}
457
Ian Rogers30fab402012-01-23 15:43:46 -0800458std::ostream& operator<<(std::ostream& os, const Space& space) {
459 os << (space.IsImageSpace() ? "Image" : "Alloc") << "Space["
460 << "begin=" << reinterpret_cast<void*>(space.Begin())
461 << ",end=" << reinterpret_cast<void*>(space.End())
Ian Rogers3bb17a62012-01-27 23:56:44 -0800462 << ",size=" << PrettySize(space.Size()) << ",capacity=" << PrettySize(space.Capacity())
Ian Rogers30fab402012-01-23 15:43:46 -0800463 << ",name=\"" << space.GetSpaceName() << "\"]";
464 return os;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700465}
466
467} // namespace art