Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "space.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 19 | #include "UniquePtr.h" |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 20 | #include "dlmalloc.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 21 | #include "file.h" |
| 22 | #include "image.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "logging.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 24 | #include "os.h" |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 25 | #include "stl_util.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 26 | #include "utils.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 30 | #ifndef NDEBUG |
| 31 | #define DEBUG_SPACES 1 |
| 32 | #endif |
| 33 | |
| 34 | #define CHECK_MEMORY_CALL(call, args, what) \ |
| 35 | do { \ |
| 36 | int rc = call args; \ |
| 37 | if (UNLIKELY(rc != 0)) { \ |
| 38 | errno = rc; \ |
| 39 | PLOG(FATAL) << # call << " failed for " << what; \ |
| 40 | } \ |
| 41 | } while (false) |
| 42 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 43 | size_t AllocSpace::bitmap_index_ = 0; |
| 44 | |
| 45 | AllocSpace::AllocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* end, |
| 46 | size_t growth_limit) |
| 47 | : Space(name, mem_map, end), mspace_(mspace), growth_limit_(growth_limit) { |
| 48 | CHECK(mspace != NULL); |
| 49 | |
| 50 | size_t bitmap_index = bitmap_index_++; |
| 51 | |
| 52 | live_bitmap_.reset(SpaceBitmap::Create( |
| 53 | StringPrintf("allocspace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)), |
| 54 | Begin(), Capacity())); |
| 55 | DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace live bitmap #" << bitmap_index; |
| 56 | |
| 57 | mark_bitmap_.reset(SpaceBitmap::Create( |
| 58 | StringPrintf("allocspace-%s-mark-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)), |
| 59 | Begin(), Capacity())); |
| 60 | DCHECK(live_bitmap_.get() != NULL) << "could not create allocspace mark bitmap #" << bitmap_index; |
| 61 | } |
| 62 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 63 | AllocSpace* Space::CreateAllocSpace(const std::string& name, size_t initial_size, |
| 64 | size_t growth_limit, size_t capacity, |
| 65 | byte* requested_begin) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 66 | // Memory we promise to dlmalloc before it asks for morecore. |
| 67 | // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc |
| 68 | // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the |
| 69 | // size of the large allocation) will be greater than the footprint limit. |
| 70 | size_t starting_size = kPageSize; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 71 | uint64_t start_time = 0; |
| 72 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 73 | start_time = NanoTime(); |
| 74 | VLOG(startup) << "Space::CreateAllocSpace entering " << name |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 75 | << " initial_size=" << PrettySize(initial_size) |
| 76 | << " growth_limit=" << PrettySize(growth_limit) |
| 77 | << " capacity=" << PrettySize(capacity) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 78 | << " requested_begin=" << reinterpret_cast<void*>(requested_begin); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 79 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 80 | |
| 81 | // Sanity check arguments |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 82 | if (starting_size > initial_size) { |
| 83 | initial_size = starting_size; |
| 84 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 85 | if (initial_size > growth_limit) { |
| 86 | LOG(ERROR) << "Failed to create alloc space (" << name << ") where the initial size (" |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 87 | << PrettySize(initial_size) << ") is larger than its capacity (" |
| 88 | << PrettySize(growth_limit) << ")"; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 89 | return NULL; |
| 90 | } |
| 91 | if (growth_limit > capacity) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 92 | LOG(ERROR) << "Failed to create alloc space (" << name << ") where the growth limit capacity (" |
| 93 | << PrettySize(growth_limit) << ") is larger than the capacity (" |
| 94 | << PrettySize(capacity) << ")"; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | // Page align growth limit and capacity which will be used to manage mmapped storage |
| 99 | growth_limit = RoundUp(growth_limit, kPageSize); |
| 100 | capacity = RoundUp(capacity, kPageSize); |
| 101 | |
| 102 | UniquePtr<MemMap> mem_map(MemMap::MapAnonymous(name.c_str(), requested_begin, |
| 103 | capacity, PROT_READ | PROT_WRITE)); |
| 104 | if (mem_map.get() == NULL) { |
| 105 | LOG(ERROR) << "Failed to allocate pages for alloc space (" << name << ") of size " |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 106 | << PrettySize(capacity); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 110 | void* mspace = AllocSpace::CreateMallocSpace(mem_map->Begin(), starting_size, initial_size); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 111 | if (mspace == NULL) { |
| 112 | LOG(ERROR) << "Failed to initialize mspace for alloc space (" << name << ")"; |
| 113 | return NULL; |
| 114 | } |
| 115 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 116 | // Protect memory beyond the initial size. |
| 117 | byte* end = mem_map->Begin() + starting_size; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 118 | if (capacity - initial_size > 0) { |
| 119 | CHECK_MEMORY_CALL(mprotect, (end, capacity - initial_size, PROT_NONE), name); |
| 120 | } |
| 121 | |
| 122 | // Everything is set so record in immutable structure and leave |
| 123 | AllocSpace* space = new AllocSpace(name, mem_map.release(), mspace, end, growth_limit); |
| 124 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 125 | LOG(INFO) << "Space::CreateAllocSpace exiting (" << PrettyDuration(NanoTime() - start_time) |
| 126 | << " ) " << *space; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 127 | } |
| 128 | return space; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 131 | void* AllocSpace::CreateMallocSpace(void* begin, size_t morecore_start, size_t initial_size) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 132 | // clear errno to allow PLOG on error |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 133 | errno = 0; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 134 | // create mspace using our backing storage starting at begin and with a footprint of |
| 135 | // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When |
| 136 | // morecore_start bytes of memory is exhaused morecore will be called. |
| 137 | void* msp = create_mspace_with_base(begin, morecore_start, false /*locked*/); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 138 | if (msp != NULL) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 139 | // Do not allow morecore requests to succeed beyond the initial size of the heap |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 140 | mspace_set_footprint_limit(msp, initial_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 141 | } else { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 142 | PLOG(ERROR) << "create_mspace_with_base failed"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 143 | } |
| 144 | return msp; |
| 145 | } |
| 146 | |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame^] | 147 | void AllocSpace::SwapBitmaps() { |
| 148 | SpaceBitmap* temp_live_bitmap = live_bitmap_.release(); |
| 149 | live_bitmap_.reset(mark_bitmap_.release()); |
| 150 | mark_bitmap_.reset(temp_live_bitmap); |
| 151 | } |
| 152 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 153 | Object* AllocSpace::AllocWithoutGrowth(size_t num_bytes) { |
| 154 | Object* result = reinterpret_cast<Object*>(mspace_calloc(mspace_, 1, num_bytes)); |
| 155 | #if DEBUG_SPACES |
| 156 | if (result != NULL) { |
| 157 | CHECK(Contains(result)) << "Allocation (" << reinterpret_cast<void*>(result) |
| 158 | << ") not in bounds of heap " << *this; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 159 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 160 | #endif |
| 161 | return result; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 164 | Object* AllocSpace::AllocWithGrowth(size_t num_bytes) { |
| 165 | // Grow as much as possible within the mspace. |
| 166 | size_t max_allowed = Capacity(); |
| 167 | mspace_set_footprint_limit(mspace_, max_allowed); |
| 168 | // Try the allocation. |
| 169 | void* ptr = AllocWithoutGrowth(num_bytes); |
| 170 | // Shrink back down as small as possible. |
| 171 | size_t footprint = mspace_footprint(mspace_); |
| 172 | mspace_set_footprint_limit(mspace_, footprint); |
| 173 | // Return the new allocation or NULL. |
| 174 | Object* result = reinterpret_cast<Object*>(ptr); |
| 175 | CHECK(result == NULL || Contains(result)); |
| 176 | return result; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 179 | void AllocSpace::Free(Object* ptr) { |
| 180 | #if DEBUG_SPACES |
| 181 | CHECK(ptr != NULL); |
| 182 | CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this; |
| 183 | #endif |
| 184 | mspace_free(mspace_, ptr); |
| 185 | } |
| 186 | |
| 187 | void AllocSpace::FreeList(size_t num_ptrs, Object** ptrs) { |
| 188 | #if DEBUG_SPACES |
| 189 | CHECK(ptrs != NULL); |
| 190 | size_t num_broken_ptrs = 0; |
| 191 | for (size_t i = 0; i < num_ptrs; i++) { |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 192 | if (!Contains(ptrs[i])) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 193 | num_broken_ptrs++; |
| 194 | LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this; |
| 195 | } |
| 196 | } |
| 197 | CHECK_EQ(num_broken_ptrs, 0u); |
| 198 | #endif |
| 199 | mspace_bulk_free(mspace_, reinterpret_cast<void**>(ptrs), num_ptrs); |
| 200 | } |
| 201 | |
| 202 | // Callback from dlmalloc when it needs to increase the footprint |
| 203 | extern "C" void* art_heap_morecore(void* mspace, intptr_t increment) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 204 | Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 205 | if (heap->GetAllocSpace()->GetMspace() == mspace) { |
| 206 | return heap->GetAllocSpace()->MoreCore(increment); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 207 | } else { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 208 | // Exhaustively search alloc spaces. |
| 209 | const Spaces& spaces = heap->GetSpaces(); |
| 210 | // TODO: C++0x auto |
| 211 | for (Spaces::const_iterator cur = spaces.begin(); cur != spaces.end(); ++cur) { |
| 212 | if ((*cur)->IsAllocSpace()) { |
| 213 | AllocSpace* space = (*cur)->AsAllocSpace(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 214 | if (mspace == space->GetMspace()) { |
| 215 | return space->MoreCore(increment); |
| 216 | } |
| 217 | } |
| 218 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 219 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 220 | |
| 221 | LOG(FATAL) << "Unexpected call to art_heap_morecore. mspace: " << mspace |
| 222 | << " increment: " << increment; |
| 223 | return NULL; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void* AllocSpace::MoreCore(intptr_t increment) { |
| 227 | byte* original_end = end_; |
| 228 | if (increment != 0) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 229 | VLOG(heap) << "AllocSpace::MoreCore " << PrettySize(increment); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 230 | byte* new_end = original_end + increment; |
| 231 | if (increment > 0) { |
| 232 | #if DEBUG_SPACES |
| 233 | // Should never be asked to increase the allocation beyond the capacity of the space. Enforced |
| 234 | // by mspace_set_footprint_limit. |
| 235 | CHECK_LE(new_end, Begin() + Capacity()); |
| 236 | #endif |
| 237 | CHECK_MEMORY_CALL(mprotect, (original_end, increment, PROT_READ | PROT_WRITE), GetSpaceName()); |
| 238 | } else { |
| 239 | #if DEBUG_SPACES |
| 240 | // Should never be asked for negative footprint (ie before begin) |
| 241 | CHECK_GT(original_end + increment, Begin()); |
| 242 | #endif |
| 243 | // Advise we don't need the pages and protect them |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 244 | // TODO: by removing permissions to the pages we may be causing TLB shoot-down which can be |
| 245 | // expensive (note the same isn't true for giving permissions to a page as the protected |
| 246 | // page shouldn't be in a TLB). We should investigate performance impact of just |
| 247 | // removing ignoring the memory protection change here and in Space::CreateAllocSpace. It's |
| 248 | // likely just a useful debug feature. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 249 | size_t size = -increment; |
| 250 | CHECK_MEMORY_CALL(madvise, (new_end, size, MADV_DONTNEED), GetSpaceName()); |
| 251 | CHECK_MEMORY_CALL(mprotect, (new_end, size, PROT_NONE), GetSpaceName()); |
| 252 | } |
| 253 | // Update end_ |
| 254 | end_ = new_end; |
| 255 | } |
| 256 | return original_end; |
| 257 | } |
| 258 | |
| 259 | size_t AllocSpace::AllocationSize(const Object* obj) { |
| 260 | return mspace_usable_size(const_cast<void*>(reinterpret_cast<const void*>(obj))) + kChunkOverhead; |
| 261 | } |
| 262 | |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 263 | void MspaceMadviseCallback(void* start, void* end, void* /*arg*/) { |
| 264 | // Do we have any whole pages to give back? |
| 265 | start = reinterpret_cast<void*>(RoundUp(reinterpret_cast<uintptr_t>(start), kPageSize)); |
| 266 | end = reinterpret_cast<void*>(RoundDown(reinterpret_cast<uintptr_t>(end), kPageSize)); |
| 267 | if (end > start) { |
| 268 | size_t length = reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start); |
| 269 | CHECK_MEMORY_CALL(madvise, (start, length, MADV_DONTNEED), "trim"); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 273 | void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 274 | // Is this chunk in use? |
| 275 | if (used_bytes != 0) { |
| 276 | return; |
| 277 | } |
| 278 | return MspaceMadviseCallback(start, end, arg); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 281 | void AllocSpace::Trim() { |
| 282 | // Trim to release memory at the end of the space. |
| 283 | mspace_trim(mspace_, 0); |
| 284 | // Visit space looking for page-sized holes to advise the kernel we don't need. |
| 285 | mspace_inspect_all(mspace_, MspaceMadviseCallback, NULL); |
| 286 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 287 | |
| 288 | void AllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 289 | void* arg) { |
| 290 | mspace_inspect_all(mspace_, callback, arg); |
| 291 | } |
| 292 | |
| 293 | size_t AllocSpace::GetFootprintLimit() { |
| 294 | return mspace_footprint_limit(mspace_); |
| 295 | } |
| 296 | |
| 297 | void AllocSpace::SetFootprintLimit(size_t new_size) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 298 | VLOG(heap) << "AllocSpace::SetFootprintLimit " << PrettySize(new_size); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 299 | // Compare against the actual footprint, rather than the Size(), because the heap may not have |
| 300 | // grown all the way to the allowed size yet. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 301 | size_t current_space_size = mspace_footprint(mspace_); |
| 302 | if (new_size < current_space_size) { |
| 303 | // Don't let the space grow any more. |
| 304 | new_size = current_space_size; |
| 305 | } |
| 306 | mspace_set_footprint_limit(mspace_, new_size); |
| 307 | } |
| 308 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 309 | size_t ImageSpace::bitmap_index_ = 0; |
| 310 | |
| 311 | ImageSpace::ImageSpace(const std::string& name, MemMap* mem_map) |
| 312 | : Space(name, mem_map, mem_map->End()) { |
| 313 | const size_t bitmap_index = bitmap_index_++; |
| 314 | live_bitmap_.reset(SpaceBitmap::Create( |
| 315 | StringPrintf("imagespace-%s-live-bitmap-%d", name.c_str(), static_cast<int>(bitmap_index)), |
| 316 | Begin(), Capacity())); |
| 317 | DCHECK(live_bitmap_.get() != NULL) << "could not create imagespace live bitmap #" << bitmap_index; |
| 318 | } |
| 319 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 320 | ImageSpace* Space::CreateImageSpace(const std::string& image_file_name) { |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 321 | CHECK(!image_file_name.empty()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 322 | |
| 323 | uint64_t start_time = 0; |
| 324 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 325 | start_time = NanoTime(); |
| 326 | LOG(INFO) << "Space::CreateImageSpace entering" << " image_file_name=" << image_file_name; |
| 327 | } |
| 328 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 329 | UniquePtr<File> file(OS::OpenFile(image_file_name.c_str(), false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 330 | if (file.get() == NULL) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 331 | LOG(ERROR) << "Failed to open " << image_file_name; |
| 332 | return NULL; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 333 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 334 | ImageHeader image_header; |
| 335 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 336 | if (!success || !image_header.IsValid()) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 337 | LOG(ERROR) << "Invalid image header " << image_file_name; |
| 338 | return NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 339 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 340 | UniquePtr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(), |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 341 | file->Length(), |
| 342 | // TODO: selectively PROT_EXEC stubs |
| 343 | PROT_READ | PROT_WRITE | PROT_EXEC, |
| 344 | MAP_PRIVATE | MAP_FIXED, |
| 345 | file->Fd(), |
| 346 | 0)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 347 | if (map.get() == NULL) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 348 | LOG(ERROR) << "Failed to map " << image_file_name; |
| 349 | return NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 350 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 351 | CHECK_EQ(image_header.GetImageBegin(), map->Begin()); |
| 352 | DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader))); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 353 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 354 | Runtime* runtime = Runtime::Current(); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 355 | Object* jni_stub_array = image_header.GetImageRoot(ImageHeader::kJniStubArray); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 356 | runtime->SetJniDlsymLookupStub(down_cast<ByteArray*>(jni_stub_array)); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 357 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 358 | Object* ame_stub_array = image_header.GetImageRoot(ImageHeader::kAbstractMethodErrorStubArray); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 359 | runtime->SetAbstractMethodErrorStubArray(down_cast<ByteArray*>(ame_stub_array)); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 360 | |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 361 | Object* resolution_stub_array = |
| 362 | image_header.GetImageRoot(ImageHeader::kStaticResolutionStubArray); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 363 | runtime->SetResolutionStubArray( |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 364 | down_cast<ByteArray*>(resolution_stub_array), Runtime::kStaticMethod); |
| 365 | resolution_stub_array = image_header.GetImageRoot(ImageHeader::kUnknownMethodResolutionStubArray); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 366 | runtime->SetResolutionStubArray( |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 367 | down_cast<ByteArray*>(resolution_stub_array), Runtime::kUnknownMethod); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 368 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 369 | Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod); |
| 370 | runtime->SetResolutionMethod(down_cast<Method*>(resolution_method)); |
| 371 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 372 | Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 373 | runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kSaveAll); |
| 374 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod); |
| 375 | runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsOnly); |
| 376 | callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod); |
| 377 | runtime->SetCalleeSaveMethod(down_cast<Method*>(callee_save_method), Runtime::kRefsAndArgs); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 378 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 379 | ImageSpace* space = new ImageSpace(image_file_name, map.release()); |
| 380 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 381 | LOG(INFO) << "Space::CreateImageSpace exiting (" << PrettyDuration(NanoTime() - start_time) |
| 382 | << ") " << *space; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 383 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 384 | return space; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 387 | void ImageSpace::RecordImageAllocations(SpaceBitmap* live_bitmap) const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 388 | uint64_t start_time = 0; |
| 389 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 390 | LOG(INFO) << "ImageSpace::RecordImageAllocations entering"; |
| 391 | start_time = NanoTime(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 392 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 393 | DCHECK(!Runtime::Current()->IsStarted()); |
| 394 | CHECK(live_bitmap != NULL); |
| 395 | byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
| 396 | byte* end = End(); |
| 397 | while (current < end) { |
| 398 | DCHECK_ALIGNED(current, kObjectAlignment); |
| 399 | const Object* obj = reinterpret_cast<const Object*>(current); |
| 400 | live_bitmap->Set(obj); |
| 401 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 402 | } |
| 403 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 404 | LOG(INFO) << "ImageSpace::RecordImageAllocations exiting (" |
| 405 | << PrettyDuration(NanoTime() - start_time) << ")"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 409 | std::ostream& operator<<(std::ostream& os, const Space& space) { |
| 410 | os << (space.IsImageSpace() ? "Image" : "Alloc") << "Space[" |
| 411 | << "begin=" << reinterpret_cast<void*>(space.Begin()) |
| 412 | << ",end=" << reinterpret_cast<void*>(space.End()) |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 413 | << ",size=" << PrettySize(space.Size()) << ",capacity=" << PrettySize(space.Capacity()) |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 414 | << ",name=\"" << space.GetSpaceName() << "\"]"; |
| 415 | return os; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | } // namespace art |