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 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 18 | #include "rosalloc_space-inl.h" |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 19 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 21 | #include "cutils/trace.h" |
| 22 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 23 | #include "gc/accounting/card_table.h" |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 24 | #include "gc/accounting/space_bitmap-inl.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 25 | #include "gc/heap.h" |
| 26 | #include "mirror/class-inl.h" |
| 27 | #include "mirror/object-inl.h" |
| 28 | #include "runtime.h" |
| 29 | #include "thread.h" |
| 30 | #include "thread_list.h" |
| 31 | #include "utils.h" |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 32 | #include "valgrind_malloc_space-inl.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | namespace gc { |
| 36 | namespace space { |
| 37 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 38 | static constexpr bool kPrefetchDuringRosAllocFreeList = false; |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 39 | static constexpr size_t kPrefetchLookAhead = 8; |
| 40 | // Use this only for verification, it is not safe to use since the class of the object may have |
| 41 | // been freed. |
| 42 | static constexpr bool kVerifyFreedBytes = false; |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 43 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 44 | // TODO: Fix |
| 45 | // template class ValgrindMallocSpace<RosAllocSpace, allocator::RosAlloc*>; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 46 | |
| 47 | RosAllocSpace::RosAllocSpace(const std::string& name, MemMap* mem_map, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 48 | art::gc::allocator::RosAlloc* rosalloc, uint8_t* begin, uint8_t* end, |
| 49 | uint8_t* limit, size_t growth_limit, bool can_move_objects, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 50 | size_t starting_size, size_t initial_size, bool low_memory_mode) |
| 51 | : MallocSpace(name, mem_map, begin, end, limit, growth_limit, true, can_move_objects, |
| 52 | starting_size, initial_size), |
| 53 | rosalloc_(rosalloc), low_memory_mode_(low_memory_mode) { |
| 54 | CHECK(rosalloc != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 57 | RosAllocSpace* RosAllocSpace::CreateFromMemMap(MemMap* mem_map, const std::string& name, |
Ian Rogers | a55cf41 | 2014-02-27 00:31:26 -0800 | [diff] [blame] | 58 | size_t starting_size, size_t initial_size, |
| 59 | size_t growth_limit, size_t capacity, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 60 | bool low_memory_mode, bool can_move_objects) { |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 61 | DCHECK(mem_map != nullptr); |
| 62 | allocator::RosAlloc* rosalloc = CreateRosAlloc(mem_map->Begin(), starting_size, initial_size, |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 63 | capacity, low_memory_mode); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 64 | if (rosalloc == NULL) { |
| 65 | LOG(ERROR) << "Failed to initialize rosalloc for alloc space (" << name << ")"; |
| 66 | return NULL; |
| 67 | } |
| 68 | |
lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 69 | // Protect memory beyond the starting size. MoreCore will add r/w permissions when necessory |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 70 | uint8_t* end = mem_map->Begin() + starting_size; |
lzang1 | 385de73 | 2014-02-21 14:15:01 +0800 | [diff] [blame] | 71 | if (capacity - starting_size > 0) { |
| 72 | CHECK_MEMORY_CALL(mprotect, (end, capacity - starting_size, PROT_NONE), name); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Everything is set so record in immutable structure and leave |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 76 | uint8_t* begin = mem_map->Begin(); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 77 | // TODO: Fix RosAllocSpace to support valgrind. There is currently some issues with |
| 78 | // AllocationSize caused by redzones. b/12944686 |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 79 | if (Runtime::Current()->RunningOnValgrind()) { |
| 80 | UNIMPLEMENTED(FATAL); |
| 81 | UNREACHABLE(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 82 | } else { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 83 | return new RosAllocSpace(name, mem_map, rosalloc, begin, end, begin + capacity, growth_limit, |
| 84 | can_move_objects, starting_size, initial_size, low_memory_mode); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 85 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 88 | RosAllocSpace::~RosAllocSpace() { |
| 89 | delete rosalloc_; |
| 90 | } |
| 91 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 92 | RosAllocSpace* RosAllocSpace::Create(const std::string& name, size_t initial_size, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 93 | size_t growth_limit, size_t capacity, uint8_t* requested_begin, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 94 | bool low_memory_mode, bool can_move_objects) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 95 | uint64_t start_time = 0; |
| 96 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 97 | start_time = NanoTime(); |
| 98 | VLOG(startup) << "RosAllocSpace::Create entering " << name |
| 99 | << " initial_size=" << PrettySize(initial_size) |
| 100 | << " growth_limit=" << PrettySize(growth_limit) |
| 101 | << " capacity=" << PrettySize(capacity) |
| 102 | << " requested_begin=" << reinterpret_cast<void*>(requested_begin); |
| 103 | } |
| 104 | |
| 105 | // Memory we promise to rosalloc before it asks for morecore. |
| 106 | // Note: making this value large means that large allocations are unlikely to succeed as rosalloc |
| 107 | // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the |
| 108 | // size of the large allocation) will be greater than the footprint limit. |
Hiroshi Yamauchi | 5ccd498 | 2014-03-11 12:19:04 -0700 | [diff] [blame] | 109 | size_t starting_size = Heap::kDefaultStartingSize; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 110 | MemMap* mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity, |
| 111 | requested_begin); |
| 112 | if (mem_map == NULL) { |
| 113 | LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size " |
| 114 | << PrettySize(capacity); |
| 115 | return NULL; |
| 116 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 117 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 118 | RosAllocSpace* space = CreateFromMemMap(mem_map, name, starting_size, initial_size, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 119 | growth_limit, capacity, low_memory_mode, |
| 120 | can_move_objects); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 121 | // We start out with only the initial size possibly containing objects. |
| 122 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 123 | LOG(INFO) << "RosAllocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time) |
| 124 | << " ) " << *space; |
| 125 | } |
| 126 | return space; |
| 127 | } |
| 128 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 129 | allocator::RosAlloc* RosAllocSpace::CreateRosAlloc(void* begin, size_t morecore_start, |
| 130 | size_t initial_size, |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 131 | size_t maximum_size, bool low_memory_mode) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 132 | // clear errno to allow PLOG on error |
| 133 | errno = 0; |
| 134 | // create rosalloc using our backing storage starting at begin and |
| 135 | // with a footprint of morecore_start. When morecore_start bytes of |
| 136 | // memory is exhaused morecore will be called. |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 137 | allocator::RosAlloc* rosalloc = new art::gc::allocator::RosAlloc( |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 138 | begin, morecore_start, maximum_size, |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 139 | low_memory_mode ? |
| 140 | art::gc::allocator::RosAlloc::kPageReleaseModeAll : |
| 141 | art::gc::allocator::RosAlloc::kPageReleaseModeSizeAndEnd); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 142 | if (rosalloc != NULL) { |
| 143 | rosalloc->SetFootprintLimit(initial_size); |
| 144 | } else { |
| 145 | PLOG(ERROR) << "RosAlloc::Create failed"; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 146 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 147 | return rosalloc; |
| 148 | } |
| 149 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 150 | mirror::Object* RosAllocSpace::AllocWithGrowth(Thread* self, size_t num_bytes, |
| 151 | size_t* bytes_allocated, size_t* usable_size) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 152 | mirror::Object* result; |
| 153 | { |
| 154 | MutexLock mu(self, lock_); |
| 155 | // Grow as much as possible within the space. |
| 156 | size_t max_allowed = Capacity(); |
| 157 | rosalloc_->SetFootprintLimit(max_allowed); |
| 158 | // Try the allocation. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 159 | result = AllocCommon(self, num_bytes, bytes_allocated, usable_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 160 | // Shrink back down as small as possible. |
| 161 | size_t footprint = rosalloc_->Footprint(); |
| 162 | rosalloc_->SetFootprintLimit(footprint); |
| 163 | } |
| 164 | // Note RosAlloc zeroes memory internally. |
| 165 | // Return the new allocation or NULL. |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 166 | CHECK(!kDebugSpaces || result == nullptr || Contains(result)); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 167 | return result; |
| 168 | } |
| 169 | |
| 170 | MallocSpace* RosAllocSpace::CreateInstance(const std::string& name, MemMap* mem_map, void* allocator, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 171 | uint8_t* begin, uint8_t* end, uint8_t* limit, size_t growth_limit, |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 172 | bool can_move_objects) { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 173 | return new RosAllocSpace(name, mem_map, reinterpret_cast<allocator::RosAlloc*>(allocator), |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 174 | begin, end, limit, growth_limit, can_move_objects, starting_size_, |
| 175 | initial_size_, low_memory_mode_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | size_t RosAllocSpace::Free(Thread* self, mirror::Object* ptr) { |
| 179 | if (kDebugSpaces) { |
| 180 | CHECK(ptr != NULL); |
| 181 | CHECK(Contains(ptr)) << "Free (" << ptr << ") not in bounds of heap " << *this; |
| 182 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 183 | if (kRecentFreeCount > 0) { |
| 184 | MutexLock mu(self, lock_); |
| 185 | RegisterRecentFree(ptr); |
| 186 | } |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 187 | return rosalloc_->Free(self, ptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | size_t RosAllocSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) { |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 191 | DCHECK(ptrs != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 192 | |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 193 | size_t verify_bytes = 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 194 | for (size_t i = 0; i < num_ptrs; i++) { |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 195 | if (kPrefetchDuringRosAllocFreeList && i + kPrefetchLookAhead < num_ptrs) { |
| 196 | __builtin_prefetch(reinterpret_cast<char*>(ptrs[i + kPrefetchLookAhead])); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 197 | } |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 198 | if (kVerifyFreedBytes) { |
| 199 | verify_bytes += AllocationSizeNonvirtual(ptrs[i], nullptr); |
| 200 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (kRecentFreeCount > 0) { |
| 204 | MutexLock mu(self, lock_); |
| 205 | for (size_t i = 0; i < num_ptrs; i++) { |
| 206 | RegisterRecentFree(ptrs[i]); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (kDebugSpaces) { |
| 211 | size_t num_broken_ptrs = 0; |
| 212 | for (size_t i = 0; i < num_ptrs; i++) { |
| 213 | if (!Contains(ptrs[i])) { |
| 214 | num_broken_ptrs++; |
| 215 | LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this; |
| 216 | } else { |
| 217 | size_t size = rosalloc_->UsableSize(ptrs[i]); |
| 218 | memset(ptrs[i], 0xEF, size); |
| 219 | } |
| 220 | } |
| 221 | CHECK_EQ(num_broken_ptrs, 0u); |
| 222 | } |
| 223 | |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 224 | const size_t bytes_freed = rosalloc_->BulkFree(self, reinterpret_cast<void**>(ptrs), num_ptrs); |
| 225 | if (kVerifyFreedBytes) { |
| 226 | CHECK_EQ(verify_bytes, bytes_freed); |
| 227 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 228 | return bytes_freed; |
| 229 | } |
| 230 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 231 | size_t RosAllocSpace::Trim() { |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 232 | VLOG(heap) << "RosAllocSpace::Trim() "; |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 233 | { |
| 234 | MutexLock mu(Thread::Current(), lock_); |
| 235 | // Trim to release memory at the end of the space. |
| 236 | rosalloc_->Trim(); |
| 237 | } |
| 238 | // Attempt to release pages if it does not release all empty pages. |
| 239 | if (!rosalloc_->DoesReleaseAllPages()) { |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 240 | return rosalloc_->ReleasePages(); |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 241 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | void RosAllocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 246 | void* arg) { |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 247 | InspectAllRosAlloc(callback, arg, true); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | size_t RosAllocSpace::GetFootprint() { |
| 251 | MutexLock mu(Thread::Current(), lock_); |
| 252 | return rosalloc_->Footprint(); |
| 253 | } |
| 254 | |
| 255 | size_t RosAllocSpace::GetFootprintLimit() { |
| 256 | MutexLock mu(Thread::Current(), lock_); |
| 257 | return rosalloc_->FootprintLimit(); |
| 258 | } |
| 259 | |
| 260 | void RosAllocSpace::SetFootprintLimit(size_t new_size) { |
| 261 | MutexLock mu(Thread::Current(), lock_); |
| 262 | VLOG(heap) << "RosAllocSpace::SetFootprintLimit " << PrettySize(new_size); |
| 263 | // Compare against the actual footprint, rather than the Size(), because the heap may not have |
| 264 | // grown all the way to the allowed size yet. |
| 265 | size_t current_space_size = rosalloc_->Footprint(); |
| 266 | if (new_size < current_space_size) { |
| 267 | // Don't let the space grow any more. |
| 268 | new_size = current_space_size; |
| 269 | } |
| 270 | rosalloc_->SetFootprintLimit(new_size); |
| 271 | } |
| 272 | |
| 273 | uint64_t RosAllocSpace::GetBytesAllocated() { |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 274 | size_t bytes_allocated = 0; |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 275 | InspectAllRosAlloc(art::gc::allocator::RosAlloc::BytesAllocatedCallback, &bytes_allocated, false); |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 276 | return bytes_allocated; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | uint64_t RosAllocSpace::GetObjectsAllocated() { |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 280 | size_t objects_allocated = 0; |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 281 | InspectAllRosAlloc(art::gc::allocator::RosAlloc::ObjectsAllocatedCallback, &objects_allocated, false); |
Hiroshi Yamauchi | 4ce1f00 | 2013-11-18 14:49:09 -0800 | [diff] [blame] | 282 | return objects_allocated; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 285 | void RosAllocSpace::InspectAllRosAllocWithSuspendAll( |
| 286 | void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 287 | void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS { |
| 288 | // TODO: NO_THREAD_SAFETY_ANALYSIS. |
| 289 | Thread* self = Thread::Current(); |
| 290 | ThreadList* tl = Runtime::Current()->GetThreadList(); |
| 291 | tl->SuspendAll(); |
| 292 | { |
| 293 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 294 | MutexLock mu2(self, *Locks::thread_list_lock_); |
| 295 | rosalloc_->InspectAll(callback, arg); |
| 296 | if (do_null_callback_at_end) { |
| 297 | callback(NULL, NULL, 0, arg); // Indicate end of a space. |
| 298 | } |
| 299 | } |
| 300 | tl->ResumeAll(); |
| 301 | } |
| 302 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 303 | void RosAllocSpace::InspectAllRosAlloc(void (*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 304 | void* arg, bool do_null_callback_at_end) NO_THREAD_SAFETY_ANALYSIS { |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 305 | // TODO: NO_THREAD_SAFETY_ANALYSIS. |
| 306 | Thread* self = Thread::Current(); |
| 307 | if (Locks::mutator_lock_->IsExclusiveHeld(self)) { |
| 308 | // The mutators are already suspended. For example, a call path |
| 309 | // from SignalCatcher::HandleSigQuit(). |
| 310 | rosalloc_->InspectAll(callback, arg); |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 311 | if (do_null_callback_at_end) { |
| 312 | callback(NULL, NULL, 0, arg); // Indicate end of a space. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 313 | } |
Hiroshi Yamauchi | 1cd53db | 2014-03-28 15:26:48 -0700 | [diff] [blame] | 314 | } else if (Locks::mutator_lock_->IsSharedHeld(self)) { |
| 315 | // The mutators are not suspended yet and we have a shared access |
| 316 | // to the mutator lock. Temporarily release the shared access by |
| 317 | // transitioning to the suspend state, and suspend the mutators. |
| 318 | self->TransitionFromRunnableToSuspended(kSuspended); |
| 319 | InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end); |
| 320 | self->TransitionFromSuspendedToRunnable(); |
| 321 | Locks::mutator_lock_->AssertSharedHeld(self); |
| 322 | } else { |
| 323 | // The mutators are not suspended yet. Suspend the mutators. |
| 324 | InspectAllRosAllocWithSuspendAll(callback, arg, do_null_callback_at_end); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | void RosAllocSpace::RevokeThreadLocalBuffers(Thread* thread) { |
| 329 | rosalloc_->RevokeThreadLocalRuns(thread); |
| 330 | } |
| 331 | |
| 332 | void RosAllocSpace::RevokeAllThreadLocalBuffers() { |
| 333 | rosalloc_->RevokeAllThreadLocalRuns(); |
| 334 | } |
| 335 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 336 | void RosAllocSpace::AssertThreadLocalBuffersAreRevoked(Thread* thread) { |
| 337 | if (kIsDebugBuild) { |
| 338 | rosalloc_->AssertThreadLocalRunsAreRevoked(thread); |
| 339 | } |
| 340 | } |
| 341 | |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 342 | void RosAllocSpace::AssertAllThreadLocalBuffersAreRevoked() { |
| 343 | if (kIsDebugBuild) { |
| 344 | rosalloc_->AssertAllThreadLocalRunsAreRevoked(); |
| 345 | } |
| 346 | } |
| 347 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 348 | void RosAllocSpace::Clear() { |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 349 | size_t footprint_limit = GetFootprintLimit(); |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 350 | madvise(GetMemMap()->Begin(), GetMemMap()->Size(), MADV_DONTNEED); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 351 | live_bitmap_->Clear(); |
| 352 | mark_bitmap_->Clear(); |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 353 | SetEnd(begin_ + starting_size_); |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 354 | delete rosalloc_; |
| 355 | rosalloc_ = CreateRosAlloc(mem_map_->Begin(), starting_size_, initial_size_, Capacity(), |
| 356 | low_memory_mode_); |
| 357 | SetFootprintLimit(footprint_limit); |
Mathieu Chartier | 15d3402 | 2014-02-26 17:16:38 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 360 | } // namespace space |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame^] | 361 | |
| 362 | namespace allocator { |
| 363 | |
| 364 | // Callback from rosalloc when it needs to increase the footprint. |
| 365 | void* ArtRosAllocMoreCore(allocator::RosAlloc* rosalloc, intptr_t increment) { |
| 366 | Heap* heap = Runtime::Current()->GetHeap(); |
| 367 | art::gc::space::RosAllocSpace* rosalloc_space = heap->GetRosAllocSpace(rosalloc); |
| 368 | DCHECK(rosalloc_space != nullptr); |
| 369 | DCHECK_EQ(rosalloc_space->GetRosAlloc(), rosalloc); |
| 370 | return rosalloc_space->MoreCore(increment); |
| 371 | } |
| 372 | |
| 373 | } // namespace allocator |
| 374 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 375 | } // namespace gc |
| 376 | } // namespace art |