Hiroshi Yamauchi | 7cb7bbc | 2013-11-18 17:27:37 -0800 | [diff] [blame] | 1 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "rosalloc_space.h" |
| 19 | |
| 20 | #include "rosalloc_space-inl.h" |
| 21 | #include "gc/accounting/card_table.h" |
| 22 | #include "gc/heap.h" |
| 23 | #include "mirror/class-inl.h" |
| 24 | #include "mirror/object-inl.h" |
| 25 | #include "runtime.h" |
| 26 | #include "thread.h" |
| 27 | #include "thread_list.h" |
| 28 | #include "utils.h" |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 29 | #include "valgrind_malloc_space-inl.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | namespace gc { |
| 33 | namespace space { |
| 34 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 35 | static constexpr bool kPrefetchDuringRosAllocFreeList = true; |
| 36 | |
| 37 | template class ValgrindMallocSpace<RosAllocSpace, allocator::RosAlloc*>; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 38 | |
| 39 | RosAllocSpace::RosAllocSpace(const std::string& name, MemMap* mem_map, |
| 40 | art::gc::allocator::RosAlloc* rosalloc, byte* begin, byte* end, |
| 41 | byte* limit, size_t growth_limit) |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 42 | : MallocSpace(name, mem_map, begin, end, limit, growth_limit), rosalloc_(rosalloc), |
| 43 | rosalloc_for_alloc_(rosalloc) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 44 | CHECK(rosalloc != NULL); |
| 45 | } |
| 46 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 47 | RosAllocSpace* RosAllocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 48 | size_t starting_size, size_t initial_size, |
| 49 | size_t growth_limit, size_t capacity, |
| 50 | bool low_memory_mode) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 51 | DCHECK(mem_map != nullptr); |
| 52 | allocator::RosAlloc* rosalloc = CreateRosAlloc(mem_map->Begin(), starting_size, initial_size, |
| 53 | low_memory_mode); |
| 54 | if (rosalloc == NULL) { |
| 55 | LOG(ERROR) << "Failed to initialize rosalloc for alloc space (" << name << ")"; |
| 56 | return NULL; |
| 57 | } |
| 58 | |
lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 59 | // Protect memory beyond the starting size. MoreCore will add r/w permissions when necessory |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 60 | byte* end = mem_map->Begin() + starting_size; |
lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 61 | if (capacity - starting_size > 0) { |
| 62 | CHECK_MEMORY_CALL(mprotect, (end, capacity - starting_size, PROT_NONE), name); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Everything is set so record in immutable structure and leave |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 66 | byte* begin = mem_map->Begin(); |
| 67 | if (RUNNING_ON_VALGRIND > 0) { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 68 | return new ValgrindMallocSpace<RosAllocSpace, allocator::RosAlloc*>( |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 69 | name, mem_map, rosalloc, begin, end, begin + capacity, growth_limit, initial_size); |
| 70 | } else { |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 71 | return new RosAllocSpace(name, mem_map, rosalloc, begin, end, begin + capacity, growth_limit); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 72 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 75 | RosAllocSpace* RosAllocSpace::Create(const std::string& name, size_t initial_size, |
| 76 | size_t growth_limit, size_t capacity, byte* requested_begin, |
| 77 | bool low_memory_mode) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 78 | uint64_t start_time = 0; |
| 79 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 80 | start_time = NanoTime(); |
| 81 | VLOG(startup) << "RosAllocSpace::Create entering " << name |
| 82 | << " initial_size=" << PrettySize(initial_size) |
| 83 | << " growth_limit=" << PrettySize(growth_limit) |
| 84 | << " capacity=" << PrettySize(capacity) |
| 85 | << " requested_begin=" << reinterpret_cast<void*>(requested_begin); |
| 86 | } |
| 87 | |
| 88 | // Memory we promise to rosalloc before it asks for morecore. |
| 89 | // Note: making this value large means that large allocations are unlikely to succeed as rosalloc |
| 90 | // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the |
| 91 | // size of the large allocation) will be greater than the footprint limit. |
| 92 | size_t starting_size = kPageSize; |
| 93 | MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity, |
| 94 | requested_begin); |
| 95 | if (mem_map == NULL) { |
| 96 | LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size " |
| 97 | << PrettySize(capacity); |
| 98 | return NULL; |
| 99 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 100 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 101 | RosAllocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size, |
| 102 | growth_limit, capacity, low_memory_mode); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 103 | // We start out with only the initial size possibly containing objects. |
| 104 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 105 | LOG(INFO) << "RosAllocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time) |
| 106 | << " ) " << *space; |
| 107 | } |
| 108 | return space; |
| 109 | } |
| 110 | |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 111 | allocator::RosAlloc* RosAllocSpace::CreateRosAlloc(void* begin, size_t morecore_start, size_t initial_size, |
| 112 | bool low_memory_mode) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 113 | // clear errno to allow PLOG on error |
| 114 | errno = 0; |
| 115 | // create rosalloc using our backing storage starting at begin and |
| 116 | // with a footprint of morecore_start. When morecore_start bytes of |
| 117 | // memory is exhaused morecore will be called. |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 118 | allocator::RosAlloc* rosalloc = new art::gc::allocator::RosAlloc( |
| 119 | begin, morecore_start, |
| 120 | low_memory_mode ? |
| 121 | art::gc::allocator::RosAlloc::kPageReleaseModeAll : |
| 122 | art::gc::allocator::RosAlloc::kPageReleaseModeSizeAndEnd); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 123 | if (rosalloc != NULL) { |
| 124 | rosalloc->SetFootprintLimit(initial_size); |
| 125 | } else { |
| 126 | PLOG(ERROR) << "RosAlloc::Create failed"; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 127 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 128 | return rosalloc; |
| 129 | } |
| 130 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 131 | mirror::Object* RosAllocSpace::AllocWithGrowth(Thread* self, size_t num_bytes, |
| 132 | size_t* bytes_allocated, size_t* usable_size) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 133 | mirror::Object* result; |
| 134 | { |
| 135 | MutexLock mu(self, lock_); |
| 136 | // Grow as much as possible within the space. |
| 137 | size_t max_allowed = Capacity(); |
| 138 | rosalloc_->SetFootprintLimit(max_allowed); |
| 139 | // Try the allocation. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 140 | result = AllocCommon(self, num_bytes, bytes_allocated, usable_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 141 | // Shrink back down as small as possible. |
| 142 | size_t footprint = rosalloc_->Footprint(); |
| 143 | rosalloc_->SetFootprintLimit(footprint); |
| 144 | } |
| 145 | // Note RosAlloc zeroes memory internally. |
| 146 | // Return the new allocation or NULL. |
| 147 | CHECK(!kDebugSpaces || result == NULL || Contains(result)); |
| 148 | return result; |
| 149 | } |
| 150 | |
| 151 | MallocSpace* RosAllocSpace::CreateInstance(const std::string& name, MemMap* mem_map, void* allocator, |
| 152 | byte* begin, byte* end, byte* limit, size_t growth_limit) { |
| 153 | return new RosAllocSpace(name, mem_map, reinterpret_cast<allocator::RosAlloc*>(allocator), |
| 154 | begin, end, limit, growth_limit); |
| 155 | } |
| 156 | |
| 157 | size_t RosAllocSpace::Free(Thread* self, mirror::Object* ptr) { |
| 158 | if (kDebugSpaces) { |
| 159 | CHECK(ptr != NULL); |
| 160 | CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this; |
| 161 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 162 | const size_t bytes_freed = AllocationSizeNonvirtual(ptr, nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 163 | if (kRecentFreeCount > 0) { |
| 164 | MutexLock mu(self, lock_); |
| 165 | RegisterRecentFree(ptr); |
| 166 | } |
| 167 | rosalloc_->Free(self, ptr); |
| 168 | return bytes_freed; |
| 169 | } |
| 170 | |
| 171 | size_t RosAllocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) { |
| 172 | DCHECK(ptrs != NULL); |
| 173 | |
| 174 | // Don't need the lock to calculate the size of the freed pointers. |
| 175 | size_t bytes_freed = 0; |
| 176 | for (size_t i = 0; i < num_ptrs; i++) { |
| 177 | mirror::Object* ptr = ptrs[i]; |
| 178 | const size_t look_ahead = 8; |
| 179 | if (kPrefetchDuringRosAllocFreeList && i + look_ahead < num_ptrs) { |
| 180 | __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead])); |
| 181 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 182 | bytes_freed += AllocationSizeNonvirtual(ptr, nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (kRecentFreeCount > 0) { |
| 186 | MutexLock mu(self, lock_); |
| 187 | for (size_t i = 0; i < num_ptrs; i++) { |
| 188 | RegisterRecentFree(ptrs[i]); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (kDebugSpaces) { |
| 193 | size_t num_broken_ptrs = 0; |
| 194 | for (size_t i = 0; i < num_ptrs; i++) { |
| 195 | if (!Contains(ptrs[i])) { |
| 196 | num_broken_ptrs++; |
| 197 | LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this; |
| 198 | } else { |
| 199 | size_t size = rosalloc_->UsableSize(ptrs[i]); |
| 200 | memset(ptrs[i], 0xEF, size); |
| 201 | } |
| 202 | } |
| 203 | CHECK_EQ(num_broken_ptrs, 0u); |
| 204 | } |
| 205 | |
| 206 | rosalloc_->BulkFree(self, reinterpret_cast<void**>(ptrs), num_ptrs); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 207 | return bytes_freed; |
| 208 | } |
| 209 | |
| 210 | // Callback from rosalloc when it needs to increase the footprint |
| 211 | extern "C" void* art_heap_rosalloc_morecore(allocator::RosAlloc* rosalloc, intptr_t increment) { |
| 212 | Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 213 | RosAllocSpace* rosalloc_space = heap->GetRosAllocSpace(); |
| 214 | DCHECK(rosalloc_space != nullptr); |
| 215 | DCHECK_EQ(rosalloc_space->GetRosAlloc(), rosalloc); |
| 216 | return rosalloc_space->MoreCore(increment); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 219 | size_t RosAllocSpace::Trim() { |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 220 | { |
| 221 | MutexLock mu(Thread::Current(), lock_); |
| 222 | // Trim to release memory at the end of the space. |
| 223 | rosalloc_->Trim(); |
| 224 | } |
| 225 | // Attempt to release pages if it does not release all empty pages. |
| 226 | if (!rosalloc_->DoesReleaseAllPages()) { |
| 227 | VLOG(heap) << "RosAllocSpace::Trim() "; |
| 228 | size_t reclaimed = 0; |
| 229 | InspectAllRosAlloc(DlmallocMadviseCallback, &reclaimed); |
| 230 | return reclaimed; |
| 231 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | void RosAllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 236 | void* arg) { |
| 237 | InspectAllRosAlloc(callback, arg); |
| 238 | callback(NULL, NULL, 0, arg); // Indicate end of a space. |
| 239 | } |
| 240 | |
| 241 | size_t RosAllocSpace::GetFootprint() { |
| 242 | MutexLock mu(Thread::Current(), lock_); |
| 243 | return rosalloc_->Footprint(); |
| 244 | } |
| 245 | |
| 246 | size_t RosAllocSpace::GetFootprintLimit() { |
| 247 | MutexLock mu(Thread::Current(), lock_); |
| 248 | return rosalloc_->FootprintLimit(); |
| 249 | } |
| 250 | |
| 251 | void RosAllocSpace::SetFootprintLimit(size_t new_size) { |
| 252 | MutexLock mu(Thread::Current(), lock_); |
| 253 | VLOG(heap) << "RosAllocSpace::SetFootprintLimit " << PrettySize(new_size); |
| 254 | // Compare against the actual footprint, rather than the Size(), because the heap may not have |
| 255 | // grown all the way to the allowed size yet. |
| 256 | size_t current_space_size = rosalloc_->Footprint(); |
| 257 | if (new_size < current_space_size) { |
| 258 | // Don't let the space grow any more. |
| 259 | new_size = current_space_size; |
| 260 | } |
| 261 | rosalloc_->SetFootprintLimit(new_size); |
| 262 | } |
| 263 | |
| 264 | uint64_t RosAllocSpace::GetBytesAllocated() { |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 265 | size_t bytes_allocated = 0; |
| 266 | InspectAllRosAlloc(art::gc::allocator::RosAlloc::BytesAllocatedCallback, &bytes_allocated); |
| 267 | return bytes_allocated; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | uint64_t RosAllocSpace::GetObjectsAllocated() { |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 271 | size_t objects_allocated = 0; |
| 272 | InspectAllRosAlloc(art::gc::allocator::RosAlloc::ObjectsAllocatedCallback, &objects_allocated); |
| 273 | return objects_allocated; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void RosAllocSpace::InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 277 | void* arg) NO_THREAD_SAFETY_ANALYSIS { |
| 278 | // TODO: NO_THREAD_SAFETY_ANALYSIS. |
| 279 | Thread* self = Thread::Current(); |
| 280 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 281 | // The mutators are already suspended. For example, a call path |
| 282 | // from SignalCatcher::HandleSigQuit(). |
| 283 | rosalloc_->InspectAll(callback, arg); |
| 284 | } else { |
| 285 | // The mutators are not suspended yet. |
| 286 | DCHECK(!Locks::mutator_lock_->IsSharedHeld(self)); |
| 287 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
| 288 | tl->SuspendAll(); |
| 289 | { |
| 290 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 291 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 292 | rosalloc_->InspectAll(callback, arg); |
| 293 | } |
| 294 | tl->ResumeAll(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void RosAllocSpace::RevokeThreadLocalBuffers(Thread* thread) { |
| 299 | rosalloc_->RevokeThreadLocalRuns(thread); |
| 300 | } |
| 301 | |
| 302 | void RosAllocSpace::RevokeAllThreadLocalBuffers() { |
| 303 | rosalloc_->RevokeAllThreadLocalRuns(); |
| 304 | } |
| 305 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 306 | void RosAllocSpace::Clear() { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 307 | // TODO: Delete and create new mspace here. |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 308 | madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED); |
| 309 | GetLiveBitmap()->Clear(); |
| 310 | GetMarkBitmap()->Clear(); |
| 311 | } |
| 312 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 313 | } // namespace space |
| 314 | } // namespace gc |
| 315 | } // namespace art |