blob: 02230e146de94c9df27fb939df7e453677db8716 [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.
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700209 VLOG(heap) << "Creating new AllocSpace: ";
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700210 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);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700221 live_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(end_));
222 CHECK(live_bitmap_->HeapLimit() == reinterpret_cast<uintptr_t>(end_));
223 mark_bitmap_->SetHeapLimit(reinterpret_cast<uintptr_t>(end_));
224 CHECK(mark_bitmap_->HeapLimit() == reinterpret_cast<uintptr_t>(end_));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700225 name_ += "-zygote-transformed";
226 VLOG(heap) << "zygote space creation done";
227 return alloc_space;
228}
229
Ian Rogers30fab402012-01-23 15:43:46 -0800230void AllocSpace::Free(Object* ptr) {
231#if DEBUG_SPACES
232 CHECK(ptr != NULL);
233 CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this;
234#endif
235 mspace_free(mspace_, ptr);
236}
237
238void AllocSpace::FreeList(size_t num_ptrs, Object** ptrs) {
239#if DEBUG_SPACES
240 CHECK(ptrs != NULL);
241 size_t num_broken_ptrs = 0;
242 for (size_t i = 0; i < num_ptrs; i++) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700243 if (!Contains(ptrs[i])) {
Ian Rogers30fab402012-01-23 15:43:46 -0800244 num_broken_ptrs++;
245 LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
246 }
247 }
248 CHECK_EQ(num_broken_ptrs, 0u);
249#endif
250 mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs);
251}
252
253// Callback from dlmalloc when it needs to increase the footprint
254extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800255 Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700256 if (heap->GetAllocSpace()->GetMspace() == mspace) {
257 return heap->GetAllocSpace()->MoreCore(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800258 } else {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700259 // Exhaustively search alloc spaces.
260 const Spaces& spaces = heap->GetSpaces();
261 // TODO: C++0x auto
262 for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) {
263 if ((*cur)->IsAllocSpace()) {
264 AllocSpace* space = (*cur)->AsAllocSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800265 if (mspace == space->GetMspace()) {
266 return space->MoreCore(increment);
267 }
268 }
269 }
Ian Rogers30fab402012-01-23 15:43:46 -0800270 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700271
272 LOG(FATAL) << "Unexpected call to art_heap_morecore. mspace: " << mspace
273 << " increment: " << increment;
274 return NULL;
Ian Rogers30fab402012-01-23 15:43:46 -0800275}
276
277void* AllocSpace::MoreCore(intptr_t increment) {
278 byte* original_end = end_;
279 if (increment != 0) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800280 VLOG(heap) << "AllocSpace::MoreCore " << PrettySize(increment);
Ian Rogers30fab402012-01-23 15:43:46 -0800281 byte* new_end = original_end + increment;
282 if (increment > 0) {
283#if DEBUG_SPACES
284 // Should never be asked to increase the allocation beyond the capacity of the space. Enforced
285 // by mspace_set_footprint_limit.
286 CHECK_LE(new_end, Begin() + Capacity());
287#endif
288 CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetSpaceName());
289 } else {
290#if DEBUG_SPACES
291 // Should never be asked for negative footprint (ie before begin)
292 CHECK_GT(original_end + increment, Begin());
293#endif
294 // Advise we don't need the pages and protect them
Ian Rogers3bb17a62012-01-27 23:56:44 -0800295 // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be
296 // expensive (note the same isn't true for giving permissions to a page as the protected
297 // page shouldn't be in a TLB). We should investigate performance impact of just
298 // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's
299 // likely just a useful debug feature.
Ian Rogers30fab402012-01-23 15:43:46 -0800300 size_t size = -increment;
301 CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetSpaceName());
302 CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetSpaceName());
303 }
304 // Update end_
305 end_ = new_end;
306 }
307 return original_end;
308}
309
310size_t AllocSpace::AllocationSize(const Object* obj) {
311 return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) + kChunkOverhead;
312}
313
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700314void MspaceMadviseCallback(void* start, void* end, void* /*arg*/) {
315 // Do we have any whole pages to give back?
316 start = reinterpret_cast<void*>(RoundUp(reinterpret_cast<uintptr_t>(start), kPageSize));
317 end = reinterpret_cast<void*>(RoundDown(reinterpret_cast<uintptr_t>(end), kPageSize));
318 if (end > start) {
319 size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start);
320 CHECK_MEMORY_CALL(madvise, (start, length, MADV_DONTNEED), "trim");
Ian Rogers30fab402012-01-23 15:43:46 -0800321 }
322}
323
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700324void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
325 // Is this chunk in use?
326 if (used_bytes != 0) {
327 return;
328 }
329 return MspaceMadviseCallback(start, end, arg);
Ian Rogers30fab402012-01-23 15:43:46 -0800330}
331
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700332void AllocSpace::Trim() {
333 // Trim to release memory at the end of the space.
334 mspace_trim(mspace_, 0);
335 // Visit space looking for page-sized holes to advise the kernel we don't need.
336 mspace_inspect_all(mspace_, MspaceMadviseCallback, NULL);
337}
Ian Rogers30fab402012-01-23 15:43:46 -0800338
339void AllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
340 void* arg) {
341 mspace_inspect_all(mspace_, callback, arg);
342}
343
344size_t AllocSpace::GetFootprintLimit() {
345 return mspace_footprint_limit(mspace_);
346}
347
348void AllocSpace::SetFootprintLimit(size_t new_size) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800349 VLOG(heap) << "AllocSpace::SetFootprintLimit " << PrettySize(new_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800350 // Compare against the actual footprint, rather than the Size(), because the heap may not have
351 // grown all the way to the allowed size yet.
Ian Rogers30fab402012-01-23 15:43:46 -0800352 size_t current_space_size = mspace_footprint(mspace_);
353 if (new_size < current_space_size) {
354 // Don't let the space grow any more.
355 new_size = current_space_size;
356 }
357 mspace_set_footprint_limit(mspace_, new_size);
358}
359
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700360size_t ImageSpace::bitmap_index_ = 0;
361
362ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700363 : Space(name, mem_map, mem_map->Begin(), mem_map->End(), GCRP_NEVER_COLLECT) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700364 const size_t bitmap_index = bitmap_index_++;
365 live_bitmap_.reset(SpaceBitmap::Create(
366 StringPrintf("imagespace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)),
367 Begin(), Capacity()));
368 DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index;
369}
370
Ian Rogers30fab402012-01-23 15:43:46 -0800371ImageSpace* Space::CreateImageSpace(const std::string& image_file_name) {
Brian Carlstrom5643b782012-02-05 12:32:53 -0800372 CHECK(!image_file_name.empty());
Ian Rogers30fab402012-01-23 15:43:46 -0800373
374 uint64_t start_time = 0;
375 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
376 start_time = NanoTime();
377 LOG(INFO) << "Space::CreateImageSpace entering" << " image_file_name=" << image_file_name;
378 }
379
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700380 UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700381 if (file.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800382 LOG(ERROR) << "Failed to open " << image_file_name;
383 return NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700384 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700385 ImageHeader image_header;
386 bool success = file->ReadFully(&image_header, sizeof(image_header));
387 if (!success || !image_header.IsValid()) {
Ian Rogers30fab402012-01-23 15:43:46 -0800388 LOG(ERROR) << "Invalid image header " << image_file_name;
389 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700390 }
Ian Rogers30fab402012-01-23 15:43:46 -0800391 UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Brian Carlstrom89521892011-12-07 22:05:07 -0800392 file->Length(),
393 // TODO: selectively PROT_EXEC stubs
394 PROT_READ | PROT_WRITE | PROT_EXEC,
395 MAP_PRIVATE | MAP_FIXED,
396 file->Fd(),
397 0));
Elliott Hughes90a33692011-08-30 13:27:07 -0700398 if (map.get() == NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800399 LOG(ERROR) << "Failed to map " << image_file_name;
400 return NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700401 }
Ian Rogers30fab402012-01-23 15:43:46 -0800402 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
403 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700404
Ian Rogers30fab402012-01-23 15:43:46 -0800405 Runtime* runtime = Runtime::Current();
Brian Carlstrom16192862011-09-12 17:50:06 -0700406 Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray);
Ian Rogers169c9a72011-11-13 20:13:17 -0800407 runtime->SetJniDlsymLookupStub(down_cast<ByteArray*>(jni_stub_array));
Brian Carlstrom16192862011-09-12 17:50:06 -0700408
Brian Carlstrome24fa612011-09-29 00:53:55 -0700409 Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700410 runtime->SetAbstractMethodErrorStubArray(down_cast<ByteArray*>(ame_stub_array));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700411
Ian Rogersfb6adba2012-03-04 21:51:51 -0800412 Object* resolution_stub_array =
413 image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700414 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700415 down_cast<ByteArray*>(resolution_stub_array), Runtime::kStaticMethod);
416 resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700417 runtime->SetResolutionStubArray(
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700418 down_cast<ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod);
Ian Rogersad25ac52011-10-04 19:13:33 -0700419
Ian Rogers19846512012-02-24 11:42:47 -0800420 Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
421 runtime->SetResolutionMethod(down_cast<Method*>(resolution_method));
422
Ian Rogersff1ed472011-09-20 13:46:24 -0700423 Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700424 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kSaveAll);
425 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
426 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsOnly);
427 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
428 runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsAndArgs);
Ian Rogersff1ed472011-09-20 13:46:24 -0700429
Ian Rogers30fab402012-01-23 15:43:46 -0800430 ImageSpace* space = new ImageSpace(image_file_name, map.release());
431 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800432 LOG(INFO) << "Space::CreateImageSpace exiting (" << PrettyDuration(NanoTime() - start_time)
433 << ") " << *space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700434 }
Ian Rogers30fab402012-01-23 15:43:46 -0800435 return space;
Ian Rogers5d76c432011-10-31 21:42:49 -0700436}
437
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700438void ImageSpace::RecordImageAllocations(SpaceBitmap* live_bitmap) const {
Ian Rogers30fab402012-01-23 15:43:46 -0800439 uint64_t start_time = 0;
440 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
441 LOG(INFO) << "ImageSpace::RecordImageAllocations entering";
442 start_time = NanoTime();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700443 }
Ian Rogers30fab402012-01-23 15:43:46 -0800444 DCHECK(!Runtime::Current()->IsStarted());
445 CHECK(live_bitmap != NULL);
446 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
447 byte* end = End();
448 while (current < end) {
449 DCHECK_ALIGNED(current, kObjectAlignment);
450 const Object* obj = reinterpret_cast<const Object*>(current);
451 live_bitmap->Set(obj);
452 current += RoundUp(obj->SizeOf(), kObjectAlignment);
453 }
454 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800455 LOG(INFO) << "ImageSpace::RecordImageAllocations exiting ("
456 << PrettyDuration(NanoTime() - start_time) << ")";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700457 }
458}
459
Ian Rogers30fab402012-01-23 15:43:46 -0800460std::ostream& operator<<(std::ostream& os, const Space& space) {
461 os << (space.IsImageSpace() ? "Image" : "Alloc") << "Space["
462 << "begin=" << reinterpret_cast<void*>(space.Begin())
463 << ",end=" << reinterpret_cast<void*>(space.End())
Ian Rogers3bb17a62012-01-27 23:56:44 -0800464 << ",size=" << PrettySize(space.Size()) << ",capacity=" << PrettySize(space.Capacity())
Ian Rogers30fab402012-01-23 15:43:46 -0800465 << ",name=\"" << space.GetSpaceName() << "\"]";
466 return os;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700467}
468
469} // namespace art