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