| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | */ |
| 16 | |
| Andreas Gampe | 88dbad3 | 2018-06-26 19:54:12 -0700 | [diff] [blame] | 17 | #include "rosalloc-inl.h" |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 18 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include <list> |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <map> |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include <sstream> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "android-base/stringprintf.h" |
| 25 | |
| Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 26 | #include "base/logging.h" // For VLOG |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 27 | #include "base/memory_tool.h" |
| David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 28 | #include "base/mem_map.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 29 | #include "base/mutex-inl.h" |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 30 | #include "gc/space/memory_tool_settings.h" |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 31 | #include "mirror/class-inl.h" |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 33 | #include "mirror/object.h" |
| Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 34 | #include "thread-current-inl.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 35 | #include "thread_list.h" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 36 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | namespace gc { |
| 39 | namespace allocator { |
| 40 | |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 41 | using android::base::StringPrintf; |
| 42 | |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 43 | static constexpr bool kUsePrefetchDuringAllocRun = false; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 44 | static constexpr bool kPrefetchNewRunDataByZeroing = false; |
| 45 | static constexpr size_t kPrefetchStride = 64; |
| 46 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 47 | size_t RosAlloc::bracketSizes[kNumOfSizeBrackets]; |
| 48 | size_t RosAlloc::numOfPages[kNumOfSizeBrackets]; |
| 49 | size_t RosAlloc::numOfSlots[kNumOfSizeBrackets]; |
| 50 | size_t RosAlloc::headerSizes[kNumOfSizeBrackets]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 51 | bool RosAlloc::initialized_ = false; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 52 | size_t RosAlloc::dedicated_full_run_storage_[kPageSize / sizeof(size_t)] = { 0 }; |
| 53 | RosAlloc::Run* RosAlloc::dedicated_full_run_ = |
| 54 | reinterpret_cast<RosAlloc::Run*>(dedicated_full_run_storage_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 55 | |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 56 | RosAlloc::RosAlloc(void* base, size_t capacity, size_t max_capacity, |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 57 | PageReleaseMode page_release_mode, bool running_on_memory_tool, |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 58 | size_t page_release_size_threshold) |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 59 | : base_(reinterpret_cast<uint8_t*>(base)), footprint_(capacity), |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 60 | capacity_(capacity), max_capacity_(max_capacity), |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 61 | lock_("rosalloc global lock", kRosAllocGlobalLock), |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 62 | bulk_free_lock_("rosalloc bulk free lock", kRosAllocBulkFreeLock), |
| 63 | page_release_mode_(page_release_mode), |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 64 | page_release_size_threshold_(page_release_size_threshold), |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 65 | is_running_on_memory_tool_(running_on_memory_tool) { |
| Hiroshi Yamauchi | b5e31f3 | 2016-02-18 15:01:17 -0800 | [diff] [blame] | 66 | DCHECK_ALIGNED(base, kPageSize); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 67 | DCHECK_EQ(RoundUp(capacity, kPageSize), capacity); |
| 68 | DCHECK_EQ(RoundUp(max_capacity, kPageSize), max_capacity); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 69 | CHECK_LE(capacity, max_capacity); |
| Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 70 | CHECK_ALIGNED(page_release_size_threshold_, kPageSize); |
| Hiroshi Yamauchi | b5e31f3 | 2016-02-18 15:01:17 -0800 | [diff] [blame] | 71 | // Zero the memory explicitly (don't rely on that the mem map is zero-initialized). |
| 72 | if (!kMadviseZeroes) { |
| 73 | memset(base_, 0, max_capacity); |
| 74 | } |
| 75 | CHECK_EQ(madvise(base_, max_capacity, MADV_DONTNEED), 0); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 76 | if (!initialized_) { |
| 77 | Initialize(); |
| 78 | } |
| 79 | VLOG(heap) << "RosAlloc base=" |
| 80 | << std::hex << (intptr_t)base_ << ", end=" |
| 81 | << std::hex << (intptr_t)(base_ + capacity_) |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 82 | << ", capacity=" << std::dec << capacity_ |
| 83 | << ", max_capacity=" << std::dec << max_capacity_; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 84 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 85 | size_bracket_lock_names_[i] = |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 86 | StringPrintf("an rosalloc size bracket %d lock", static_cast<int>(i)); |
| Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 87 | size_bracket_locks_[i] = new Mutex(size_bracket_lock_names_[i].c_str(), kRosAllocBracketLock); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 88 | current_runs_[i] = dedicated_full_run_; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 89 | } |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 90 | DCHECK_EQ(footprint_, capacity_); |
| 91 | size_t num_of_pages = footprint_ / kPageSize; |
| 92 | size_t max_num_of_pages = max_capacity_ / kPageSize; |
| 93 | std::string error_msg; |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 94 | page_map_mem_map_ = MemMap::MapAnonymous("rosalloc page map", |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 95 | RoundUp(max_num_of_pages, kPageSize), |
| 96 | PROT_READ | PROT_WRITE, |
| Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 97 | /*low_4gb=*/ false, |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 98 | &error_msg); |
| 99 | CHECK(page_map_mem_map_.IsValid()) << "Couldn't allocate the page map : " << error_msg; |
| 100 | page_map_ = page_map_mem_map_.Begin(); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 101 | page_map_size_ = num_of_pages; |
| 102 | max_page_map_size_ = max_num_of_pages; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 103 | free_page_run_size_map_.resize(num_of_pages); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 104 | FreePageRun* free_pages = reinterpret_cast<FreePageRun*>(base_); |
| 105 | if (kIsDebugBuild) { |
| 106 | free_pages->magic_num_ = kMagicNumFree; |
| 107 | } |
| 108 | free_pages->SetByteSize(this, capacity_); |
| 109 | DCHECK_EQ(capacity_ % kPageSize, static_cast<size_t>(0)); |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 110 | DCHECK(free_pages->IsFree()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 111 | free_pages->ReleasePages(this); |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 112 | DCHECK(free_pages->IsFree()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 113 | free_page_runs_.insert(free_pages); |
| 114 | if (kTraceRosAlloc) { |
| 115 | LOG(INFO) << "RosAlloc::RosAlloc() : Inserted run 0x" << std::hex |
| 116 | << reinterpret_cast<intptr_t>(free_pages) |
| 117 | << " into free_page_runs_"; |
| 118 | } |
| 119 | } |
| 120 | |
| Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 121 | RosAlloc::~RosAlloc() { |
| 122 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 123 | delete size_bracket_locks_[i]; |
| 124 | } |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 125 | if (is_running_on_memory_tool_) { |
| 126 | MEMORY_TOOL_MAKE_DEFINED(base_, capacity_); |
| 127 | } |
| Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 130 | void* RosAlloc::AllocPages(Thread* self, size_t num_pages, uint8_t page_map_type) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 131 | lock_.AssertHeld(self); |
| 132 | DCHECK(page_map_type == kPageMapRun || page_map_type == kPageMapLargeObject); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 133 | FreePageRun* res = nullptr; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 134 | const size_t req_byte_size = num_pages * kPageSize; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 135 | // Find the lowest address free page run that's large enough. |
| 136 | for (auto it = free_page_runs_.begin(); it != free_page_runs_.end(); ) { |
| 137 | FreePageRun* fpr = *it; |
| 138 | DCHECK(fpr->IsFree()); |
| 139 | size_t fpr_byte_size = fpr->ByteSize(this); |
| 140 | DCHECK_EQ(fpr_byte_size % kPageSize, static_cast<size_t>(0)); |
| 141 | if (req_byte_size <= fpr_byte_size) { |
| 142 | // Found one. |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 143 | it = free_page_runs_.erase(it); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 144 | if (kTraceRosAlloc) { |
| 145 | LOG(INFO) << "RosAlloc::AllocPages() : Erased run 0x" |
| 146 | << std::hex << reinterpret_cast<intptr_t>(fpr) |
| 147 | << " from free_page_runs_"; |
| 148 | } |
| 149 | if (req_byte_size < fpr_byte_size) { |
| 150 | // Split. |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 151 | FreePageRun* remainder = |
| 152 | reinterpret_cast<FreePageRun*>(reinterpret_cast<uint8_t*>(fpr) + req_byte_size); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 153 | if (kIsDebugBuild) { |
| 154 | remainder->magic_num_ = kMagicNumFree; |
| 155 | } |
| 156 | remainder->SetByteSize(this, fpr_byte_size - req_byte_size); |
| 157 | DCHECK_EQ(remainder->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 158 | // Don't need to call madvise on remainder here. |
| 159 | free_page_runs_.insert(remainder); |
| 160 | if (kTraceRosAlloc) { |
| 161 | LOG(INFO) << "RosAlloc::AllocPages() : Inserted run 0x" << std::hex |
| 162 | << reinterpret_cast<intptr_t>(remainder) |
| 163 | << " into free_page_runs_"; |
| 164 | } |
| 165 | fpr->SetByteSize(this, req_byte_size); |
| 166 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 167 | } |
| 168 | res = fpr; |
| 169 | break; |
| 170 | } else { |
| 171 | ++it; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Failed to allocate pages. Grow the footprint, if possible. |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 176 | if (UNLIKELY(res == nullptr && capacity_ > footprint_)) { |
| 177 | FreePageRun* last_free_page_run = nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 178 | size_t last_free_page_run_size; |
| 179 | auto it = free_page_runs_.rbegin(); |
| 180 | if (it != free_page_runs_.rend() && (last_free_page_run = *it)->End(this) == base_ + footprint_) { |
| 181 | // There is a free page run at the end. |
| 182 | DCHECK(last_free_page_run->IsFree()); |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 183 | DCHECK(IsFreePage(ToPageMapIndex(last_free_page_run))); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 184 | last_free_page_run_size = last_free_page_run->ByteSize(this); |
| 185 | } else { |
| 186 | // There is no free page run at the end. |
| 187 | last_free_page_run_size = 0; |
| 188 | } |
| 189 | DCHECK_LT(last_free_page_run_size, req_byte_size); |
| 190 | if (capacity_ - footprint_ + last_free_page_run_size >= req_byte_size) { |
| 191 | // If we grow the heap, we can allocate it. |
| 192 | size_t increment = std::min(std::max(2 * MB, req_byte_size - last_free_page_run_size), |
| 193 | capacity_ - footprint_); |
| 194 | DCHECK_EQ(increment % kPageSize, static_cast<size_t>(0)); |
| 195 | size_t new_footprint = footprint_ + increment; |
| 196 | size_t new_num_of_pages = new_footprint / kPageSize; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 197 | DCHECK_LT(page_map_size_, new_num_of_pages); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 198 | DCHECK_LT(free_page_run_size_map_.size(), new_num_of_pages); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 199 | page_map_size_ = new_num_of_pages; |
| 200 | DCHECK_LE(page_map_size_, max_page_map_size_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 201 | free_page_run_size_map_.resize(new_num_of_pages); |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 202 | ArtRosAllocMoreCore(this, increment); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 203 | if (last_free_page_run_size > 0) { |
| 204 | // There was a free page run at the end. Expand its size. |
| 205 | DCHECK_EQ(last_free_page_run_size, last_free_page_run->ByteSize(this)); |
| 206 | last_free_page_run->SetByteSize(this, last_free_page_run_size + increment); |
| 207 | DCHECK_EQ(last_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 208 | DCHECK_EQ(last_free_page_run->End(this), base_ + new_footprint); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 209 | } else { |
| 210 | // Otherwise, insert a new free page run at the end. |
| 211 | FreePageRun* new_free_page_run = reinterpret_cast<FreePageRun*>(base_ + footprint_); |
| 212 | if (kIsDebugBuild) { |
| 213 | new_free_page_run->magic_num_ = kMagicNumFree; |
| 214 | } |
| 215 | new_free_page_run->SetByteSize(this, increment); |
| 216 | DCHECK_EQ(new_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 217 | free_page_runs_.insert(new_free_page_run); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 218 | DCHECK_EQ(*free_page_runs_.rbegin(), new_free_page_run); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 219 | if (kTraceRosAlloc) { |
| 220 | LOG(INFO) << "RosAlloc::AlloPages() : Grew the heap by inserting run 0x" |
| 221 | << std::hex << reinterpret_cast<intptr_t>(new_free_page_run) |
| 222 | << " into free_page_runs_"; |
| 223 | } |
| 224 | } |
| 225 | DCHECK_LE(footprint_ + increment, capacity_); |
| 226 | if (kTraceRosAlloc) { |
| 227 | LOG(INFO) << "RosAlloc::AllocPages() : increased the footprint from " |
| 228 | << footprint_ << " to " << new_footprint; |
| 229 | } |
| 230 | footprint_ = new_footprint; |
| 231 | |
| 232 | // And retry the last free page run. |
| 233 | it = free_page_runs_.rbegin(); |
| 234 | DCHECK(it != free_page_runs_.rend()); |
| 235 | FreePageRun* fpr = *it; |
| 236 | if (kIsDebugBuild && last_free_page_run_size > 0) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 237 | DCHECK(last_free_page_run != nullptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 238 | DCHECK_EQ(last_free_page_run, fpr); |
| 239 | } |
| 240 | size_t fpr_byte_size = fpr->ByteSize(this); |
| 241 | DCHECK_EQ(fpr_byte_size % kPageSize, static_cast<size_t>(0)); |
| 242 | DCHECK_LE(req_byte_size, fpr_byte_size); |
| 243 | free_page_runs_.erase(fpr); |
| 244 | if (kTraceRosAlloc) { |
| 245 | LOG(INFO) << "RosAlloc::AllocPages() : Erased run 0x" << std::hex << reinterpret_cast<intptr_t>(fpr) |
| 246 | << " from free_page_runs_"; |
| 247 | } |
| 248 | if (req_byte_size < fpr_byte_size) { |
| 249 | // Split if there's a remainder. |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 250 | FreePageRun* remainder = reinterpret_cast<FreePageRun*>(reinterpret_cast<uint8_t*>(fpr) + req_byte_size); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 251 | if (kIsDebugBuild) { |
| 252 | remainder->magic_num_ = kMagicNumFree; |
| 253 | } |
| 254 | remainder->SetByteSize(this, fpr_byte_size - req_byte_size); |
| 255 | DCHECK_EQ(remainder->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 256 | free_page_runs_.insert(remainder); |
| 257 | if (kTraceRosAlloc) { |
| 258 | LOG(INFO) << "RosAlloc::AllocPages() : Inserted run 0x" << std::hex |
| 259 | << reinterpret_cast<intptr_t>(remainder) |
| 260 | << " into free_page_runs_"; |
| 261 | } |
| 262 | fpr->SetByteSize(this, req_byte_size); |
| 263 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 264 | } |
| 265 | res = fpr; |
| 266 | } |
| 267 | } |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 268 | if (LIKELY(res != nullptr)) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 269 | // Update the page map. |
| 270 | size_t page_map_idx = ToPageMapIndex(res); |
| 271 | for (size_t i = 0; i < num_pages; i++) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 272 | DCHECK(IsFreePage(page_map_idx + i)); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 273 | } |
| 274 | switch (page_map_type) { |
| 275 | case kPageMapRun: |
| 276 | page_map_[page_map_idx] = kPageMapRun; |
| 277 | for (size_t i = 1; i < num_pages; i++) { |
| 278 | page_map_[page_map_idx + i] = kPageMapRunPart; |
| 279 | } |
| 280 | break; |
| 281 | case kPageMapLargeObject: |
| 282 | page_map_[page_map_idx] = kPageMapLargeObject; |
| 283 | for (size_t i = 1; i < num_pages; i++) { |
| 284 | page_map_[page_map_idx + i] = kPageMapLargeObjectPart; |
| 285 | } |
| 286 | break; |
| 287 | default: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 288 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_type); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 289 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 290 | } |
| 291 | if (kIsDebugBuild) { |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 292 | // Clear the first page since it is not madvised due to the magic number. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 293 | memset(res, 0, kPageSize); |
| 294 | } |
| 295 | if (kTraceRosAlloc) { |
| 296 | LOG(INFO) << "RosAlloc::AllocPages() : 0x" << std::hex << reinterpret_cast<intptr_t>(res) |
| 297 | << "-0x" << (reinterpret_cast<intptr_t>(res) + num_pages * kPageSize) |
| 298 | << "(" << std::dec << (num_pages * kPageSize) << ")"; |
| 299 | } |
| 300 | return res; |
| 301 | } |
| 302 | |
| 303 | // Fail. |
| 304 | if (kTraceRosAlloc) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 305 | LOG(INFO) << "RosAlloc::AllocPages() : nullptr"; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 306 | } |
| 307 | return nullptr; |
| 308 | } |
| 309 | |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 310 | size_t RosAlloc::FreePages(Thread* self, void* ptr, bool already_zero) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 311 | lock_.AssertHeld(self); |
| 312 | size_t pm_idx = ToPageMapIndex(ptr); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 313 | DCHECK_LT(pm_idx, page_map_size_); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 314 | uint8_t pm_type = page_map_[pm_idx]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 315 | DCHECK(pm_type == kPageMapRun || pm_type == kPageMapLargeObject); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 316 | uint8_t pm_part_type; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 317 | switch (pm_type) { |
| 318 | case kPageMapRun: |
| 319 | pm_part_type = kPageMapRunPart; |
| 320 | break; |
| 321 | case kPageMapLargeObject: |
| 322 | pm_part_type = kPageMapLargeObjectPart; |
| 323 | break; |
| 324 | default: |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 325 | LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << " : " << "pm_idx=" << pm_idx << ", pm_type=" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 326 | << static_cast<int>(pm_type) << ", ptr=" << std::hex |
| 327 | << reinterpret_cast<intptr_t>(ptr); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 328 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 329 | } |
| 330 | // Update the page map and count the number of pages. |
| 331 | size_t num_pages = 1; |
| 332 | page_map_[pm_idx] = kPageMapEmpty; |
| 333 | size_t idx = pm_idx + 1; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 334 | size_t end = page_map_size_; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 335 | while (idx < end && page_map_[idx] == pm_part_type) { |
| 336 | page_map_[idx] = kPageMapEmpty; |
| 337 | num_pages++; |
| 338 | idx++; |
| 339 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 340 | const size_t byte_size = num_pages * kPageSize; |
| 341 | if (already_zero) { |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 342 | if (ShouldCheckZeroMemory()) { |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 343 | const uintptr_t* word_ptr = reinterpret_cast<uintptr_t*>(ptr); |
| 344 | for (size_t i = 0; i < byte_size / sizeof(uintptr_t); ++i) { |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 345 | CHECK_EQ(word_ptr[i], 0U) << "words don't match at index " << i; |
| 346 | } |
| 347 | } |
| 348 | } else if (!DoesReleaseAllPages()) { |
| 349 | memset(ptr, 0, byte_size); |
| 350 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 351 | |
| 352 | if (kTraceRosAlloc) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 353 | LOG(INFO) << __PRETTY_FUNCTION__ << " : 0x" << std::hex << reinterpret_cast<intptr_t>(ptr) |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 354 | << "-0x" << (reinterpret_cast<intptr_t>(ptr) + byte_size) |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 355 | << "(" << std::dec << (num_pages * kPageSize) << ")"; |
| 356 | } |
| 357 | |
| 358 | // Turn it into a free run. |
| 359 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(ptr); |
| 360 | if (kIsDebugBuild) { |
| 361 | fpr->magic_num_ = kMagicNumFree; |
| 362 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 363 | fpr->SetByteSize(this, byte_size); |
| Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 364 | DCHECK_ALIGNED(fpr->ByteSize(this), kPageSize); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 365 | |
| 366 | DCHECK(free_page_runs_.find(fpr) == free_page_runs_.end()); |
| 367 | if (!free_page_runs_.empty()) { |
| 368 | // Try to coalesce in the higher address direction. |
| 369 | if (kTraceRosAlloc) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 370 | LOG(INFO) << __PRETTY_FUNCTION__ << "RosAlloc::FreePages() : trying to coalesce a free page run 0x" |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 371 | << std::hex << reinterpret_cast<uintptr_t>(fpr) << " [" << std::dec << pm_idx << "] -0x" |
| 372 | << std::hex << reinterpret_cast<uintptr_t>(fpr->End(this)) << " [" << std::dec |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 373 | << (fpr->End(this) == End() ? page_map_size_ : ToPageMapIndex(fpr->End(this))) << "]"; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 374 | } |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 375 | for (auto it = free_page_runs_.upper_bound(fpr); it != free_page_runs_.end(); ) { |
| 376 | FreePageRun* h = *it; |
| 377 | DCHECK_EQ(h->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 378 | if (kTraceRosAlloc) { |
| 379 | LOG(INFO) << "RosAlloc::FreePages() : trying to coalesce with a higher free page run 0x" |
| 380 | << std::hex << reinterpret_cast<uintptr_t>(h) << " [" << std::dec << ToPageMapIndex(h) << "] -0x" |
| 381 | << std::hex << reinterpret_cast<uintptr_t>(h->End(this)) << " [" << std::dec |
| 382 | << (h->End(this) == End() ? page_map_size_ : ToPageMapIndex(h->End(this))) << "]"; |
| 383 | } |
| 384 | if (fpr->End(this) == h->Begin()) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 385 | if (kTraceRosAlloc) { |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 386 | LOG(INFO) << "Success"; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 387 | } |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 388 | // Clear magic num since this is no longer the start of a free page run. |
| 389 | if (kIsDebugBuild) { |
| 390 | h->magic_num_ = 0; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 391 | } |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 392 | it = free_page_runs_.erase(it); |
| 393 | if (kTraceRosAlloc) { |
| 394 | LOG(INFO) << "RosAlloc::FreePages() : (coalesce) Erased run 0x" << std::hex |
| 395 | << reinterpret_cast<intptr_t>(h) |
| 396 | << " from free_page_runs_"; |
| 397 | } |
| 398 | fpr->SetByteSize(this, fpr->ByteSize(this) + h->ByteSize(this)); |
| 399 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 400 | } else { |
| 401 | // Not adjacent. Stop. |
| 402 | if (kTraceRosAlloc) { |
| 403 | LOG(INFO) << "Fail"; |
| 404 | } |
| 405 | break; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | // Try to coalesce in the lower address direction. |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 409 | for (auto it = free_page_runs_.upper_bound(fpr); it != free_page_runs_.begin(); ) { |
| 410 | --it; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 411 | |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 412 | FreePageRun* l = *it; |
| 413 | DCHECK_EQ(l->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 414 | if (kTraceRosAlloc) { |
| 415 | LOG(INFO) << "RosAlloc::FreePages() : trying to coalesce with a lower free page run 0x" |
| 416 | << std::hex << reinterpret_cast<uintptr_t>(l) << " [" << std::dec << ToPageMapIndex(l) << "] -0x" |
| 417 | << std::hex << reinterpret_cast<uintptr_t>(l->End(this)) << " [" << std::dec |
| 418 | << (l->End(this) == End() ? page_map_size_ : ToPageMapIndex(l->End(this))) << "]"; |
| 419 | } |
| 420 | if (l->End(this) == fpr->Begin()) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 421 | if (kTraceRosAlloc) { |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 422 | LOG(INFO) << "Success"; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 423 | } |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 424 | it = free_page_runs_.erase(it); |
| 425 | if (kTraceRosAlloc) { |
| 426 | LOG(INFO) << "RosAlloc::FreePages() : (coalesce) Erased run 0x" << std::hex |
| 427 | << reinterpret_cast<intptr_t>(l) |
| 428 | << " from free_page_runs_"; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 429 | } |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 430 | l->SetByteSize(this, l->ByteSize(this) + fpr->ByteSize(this)); |
| 431 | DCHECK_EQ(l->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 432 | // Clear magic num since this is no longer the start of a free page run. |
| 433 | if (kIsDebugBuild) { |
| 434 | fpr->magic_num_ = 0; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 435 | } |
| Vladimir Marko | e74fe1e | 2016-08-31 18:56:04 +0100 | [diff] [blame] | 436 | fpr = l; |
| 437 | } else { |
| 438 | // Not adjacent. Stop. |
| 439 | if (kTraceRosAlloc) { |
| 440 | LOG(INFO) << "Fail"; |
| 441 | } |
| 442 | break; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // Insert it. |
| 448 | DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 449 | DCHECK(free_page_runs_.find(fpr) == free_page_runs_.end()); |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 450 | DCHECK(fpr->IsFree()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 451 | fpr->ReleasePages(this); |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 452 | DCHECK(fpr->IsFree()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 453 | free_page_runs_.insert(fpr); |
| 454 | DCHECK(free_page_runs_.find(fpr) != free_page_runs_.end()); |
| 455 | if (kTraceRosAlloc) { |
| 456 | LOG(INFO) << "RosAlloc::FreePages() : Inserted run 0x" << std::hex << reinterpret_cast<intptr_t>(fpr) |
| 457 | << " into free_page_runs_"; |
| 458 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 459 | return byte_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 462 | void* RosAlloc::AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated, |
| 463 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) { |
| 464 | DCHECK(bytes_allocated != nullptr); |
| 465 | DCHECK(usable_size != nullptr); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 466 | DCHECK_GT(size, kLargeSizeThreshold); |
| Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 467 | size_t num_pages = RoundUp(size, kPageSize) / kPageSize; |
| 468 | void* r; |
| 469 | { |
| 470 | MutexLock mu(self, lock_); |
| 471 | r = AllocPages(self, num_pages, kPageMapLargeObject); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 472 | } |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 473 | if (UNLIKELY(r == nullptr)) { |
| 474 | if (kTraceRosAlloc) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 475 | LOG(INFO) << "RosAlloc::AllocLargeObject() : nullptr"; |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 476 | } |
| 477 | return nullptr; |
| 478 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 479 | const size_t total_bytes = num_pages * kPageSize; |
| 480 | *bytes_allocated = total_bytes; |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 481 | *usable_size = total_bytes; |
| 482 | *bytes_tl_bulk_allocated = total_bytes; |
| Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 483 | if (kTraceRosAlloc) { |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 484 | LOG(INFO) << "RosAlloc::AllocLargeObject() : 0x" << std::hex << reinterpret_cast<intptr_t>(r) |
| 485 | << "-0x" << (reinterpret_cast<intptr_t>(r) + num_pages * kPageSize) |
| 486 | << "(" << std::dec << (num_pages * kPageSize) << ")"; |
| 487 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 488 | // Check if the returned memory is really all zero. |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 489 | if (ShouldCheckZeroMemory()) { |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 490 | CHECK_EQ(total_bytes % sizeof(uintptr_t), 0U); |
| 491 | const uintptr_t* words = reinterpret_cast<uintptr_t*>(r); |
| 492 | for (size_t i = 0; i < total_bytes / sizeof(uintptr_t); ++i) { |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 493 | CHECK_EQ(words[i], 0U); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 496 | return r; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 499 | size_t RosAlloc::FreeInternal(Thread* self, void* ptr) { |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 500 | DCHECK_LE(base_, ptr); |
| 501 | DCHECK_LT(ptr, base_ + footprint_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 502 | size_t pm_idx = RoundDownToPageMapIndex(ptr); |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 503 | Run* run = nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 504 | { |
| 505 | MutexLock mu(self, lock_); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 506 | DCHECK_LT(pm_idx, page_map_size_); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 507 | uint8_t page_map_entry = page_map_[pm_idx]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 508 | if (kTraceRosAlloc) { |
| 509 | LOG(INFO) << "RosAlloc::FreeInternal() : " << std::hex << ptr << ", pm_idx=" << std::dec << pm_idx |
| 510 | << ", page_map_entry=" << static_cast<int>(page_map_entry); |
| 511 | } |
| 512 | switch (page_map_[pm_idx]) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 513 | case kPageMapLargeObject: |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 514 | return FreePages(self, ptr, false); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 515 | case kPageMapLargeObjectPart: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 516 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 517 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 518 | case kPageMapRunPart: { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 519 | // Find the beginning of the run. |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 520 | do { |
| 521 | --pm_idx; |
| 522 | DCHECK_LT(pm_idx, capacity_ / kPageSize); |
| 523 | } while (page_map_[pm_idx] != kPageMapRun); |
| Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 524 | FALLTHROUGH_INTENDED; |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 525 | case kPageMapRun: |
| 526 | run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 527 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 528 | break; |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 529 | case kPageMapReleased: |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 530 | case kPageMapEmpty: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 531 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 532 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 533 | } |
| 534 | default: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 535 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 536 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 539 | DCHECK(run != nullptr); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 540 | return FreeFromRun(self, ptr, run); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 543 | size_t RosAlloc::Free(Thread* self, void* ptr) { |
| Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 544 | ReaderMutexLock rmu(self, bulk_free_lock_); |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 545 | return FreeInternal(self, ptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 548 | RosAlloc::Run* RosAlloc::AllocRun(Thread* self, size_t idx) { |
| 549 | RosAlloc::Run* new_run = nullptr; |
| 550 | { |
| 551 | MutexLock mu(self, lock_); |
| 552 | new_run = reinterpret_cast<Run*>(AllocPages(self, numOfPages[idx], kPageMapRun)); |
| 553 | } |
| 554 | if (LIKELY(new_run != nullptr)) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 555 | if (kIsDebugBuild) { |
| 556 | new_run->magic_num_ = kMagicNum; |
| 557 | } |
| 558 | new_run->size_bracket_idx_ = idx; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 559 | DCHECK(!new_run->IsThreadLocal()); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 560 | DCHECK(!new_run->to_be_bulk_freed_); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 561 | if (kUsePrefetchDuringAllocRun && idx < kNumThreadLocalSizeBrackets) { |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 562 | // Take ownership of the cache lines if we are likely to be thread local run. |
| 563 | if (kPrefetchNewRunDataByZeroing) { |
| 564 | // Zeroing the data is sometimes faster than prefetching but it increases memory usage |
| 565 | // since we end up dirtying zero pages which may have been madvised. |
| 566 | new_run->ZeroData(); |
| 567 | } else { |
| 568 | const size_t num_of_slots = numOfSlots[idx]; |
| 569 | const size_t bracket_size = bracketSizes[idx]; |
| 570 | const size_t num_of_bytes = num_of_slots * bracket_size; |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 571 | uint8_t* begin = reinterpret_cast<uint8_t*>(new_run) + headerSizes[idx]; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 572 | for (size_t i = 0; i < num_of_bytes; i += kPrefetchStride) { |
| 573 | __builtin_prefetch(begin + i); |
| 574 | } |
| 575 | } |
| 576 | } |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 577 | new_run->InitFreeList(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 578 | } |
| 579 | return new_run; |
| 580 | } |
| 581 | |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 582 | RosAlloc::Run* RosAlloc::RefillRun(Thread* self, size_t idx) { |
| 583 | // Get the lowest address non-full run from the binary tree. |
| Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 584 | auto* const bt = &non_full_runs_[idx]; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 585 | if (!bt->empty()) { |
| 586 | // If there's one, use it as the current run. |
| 587 | auto it = bt->begin(); |
| 588 | Run* non_full_run = *it; |
| 589 | DCHECK(non_full_run != nullptr); |
| 590 | DCHECK(!non_full_run->IsThreadLocal()); |
| 591 | bt->erase(it); |
| 592 | return non_full_run; |
| 593 | } |
| 594 | // If there's none, allocate a new run and use it as the current run. |
| 595 | return AllocRun(self, idx); |
| 596 | } |
| 597 | |
| Hiroshi Yamauchi | 52cf5c0 | 2014-05-02 12:20:36 -0700 | [diff] [blame] | 598 | inline void* RosAlloc::AllocFromCurrentRunUnlocked(Thread* self, size_t idx) { |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 599 | Run* current_run = current_runs_[idx]; |
| 600 | DCHECK(current_run != nullptr); |
| 601 | void* slot_addr = current_run->AllocSlot(); |
| 602 | if (UNLIKELY(slot_addr == nullptr)) { |
| 603 | // The current run got full. Try to refill it. |
| 604 | DCHECK(current_run->IsFull()); |
| 605 | if (kIsDebugBuild && current_run != dedicated_full_run_) { |
| 606 | full_runs_[idx].insert(current_run); |
| 607 | if (kTraceRosAlloc) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 608 | LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex |
| 609 | << reinterpret_cast<intptr_t>(current_run) |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 610 | << " into full_runs_[" << std::dec << idx << "]"; |
| 611 | } |
| 612 | DCHECK(non_full_runs_[idx].find(current_run) == non_full_runs_[idx].end()); |
| 613 | DCHECK(full_runs_[idx].find(current_run) != full_runs_[idx].end()); |
| 614 | } |
| 615 | current_run = RefillRun(self, idx); |
| 616 | if (UNLIKELY(current_run == nullptr)) { |
| 617 | // Failed to allocate a new run, make sure that it is the dedicated full run. |
| 618 | current_runs_[idx] = dedicated_full_run_; |
| 619 | return nullptr; |
| 620 | } |
| 621 | DCHECK(current_run != nullptr); |
| 622 | DCHECK(non_full_runs_[idx].find(current_run) == non_full_runs_[idx].end()); |
| 623 | DCHECK(full_runs_[idx].find(current_run) == full_runs_[idx].end()); |
| 624 | current_run->SetIsThreadLocal(false); |
| 625 | current_runs_[idx] = current_run; |
| 626 | DCHECK(!current_run->IsFull()); |
| 627 | slot_addr = current_run->AllocSlot(); |
| 628 | // Must succeed now with a new run. |
| 629 | DCHECK(slot_addr != nullptr); |
| 630 | } |
| 631 | return slot_addr; |
| 632 | } |
| 633 | |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 634 | void* RosAlloc::AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated, |
| 635 | size_t* usable_size, |
| 636 | size_t* bytes_tl_bulk_allocated) { |
| 637 | DCHECK(bytes_allocated != nullptr); |
| 638 | DCHECK(usable_size != nullptr); |
| 639 | DCHECK(bytes_tl_bulk_allocated != nullptr); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 640 | DCHECK_LE(size, kLargeSizeThreshold); |
| 641 | size_t bracket_size; |
| 642 | size_t idx = SizeToIndexAndBracketSize(size, &bracket_size); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 643 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 644 | void* slot_addr = AllocFromCurrentRunUnlocked(self, idx); |
| 645 | if (LIKELY(slot_addr != nullptr)) { |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 646 | *bytes_allocated = bracket_size; |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 647 | *usable_size = bracket_size; |
| 648 | *bytes_tl_bulk_allocated = bracket_size; |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 649 | } |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 650 | // Caller verifies that it is all 0. |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 651 | return slot_addr; |
| 652 | } |
| 653 | |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 654 | void* RosAlloc::AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated, |
| 655 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) { |
| 656 | DCHECK(bytes_allocated != nullptr); |
| 657 | DCHECK(usable_size != nullptr); |
| 658 | DCHECK(bytes_tl_bulk_allocated != nullptr); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 659 | DCHECK_LE(size, kLargeSizeThreshold); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 660 | size_t bracket_size; |
| 661 | size_t idx = SizeToIndexAndBracketSize(size, &bracket_size); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 662 | void* slot_addr; |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 663 | if (LIKELY(idx < kNumThreadLocalSizeBrackets)) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 664 | // Use a thread-local run. |
| Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 665 | Run* thread_local_run = reinterpret_cast<Run*>(self->GetRosAllocRun(idx)); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 666 | // Allow invalid since this will always fail the allocation. |
| Mathieu Chartier | 4fd2050 | 2014-04-28 09:35:55 -0700 | [diff] [blame] | 667 | if (kIsDebugBuild) { |
| 668 | // Need the lock to prevent race conditions. |
| 669 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| 670 | CHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 671 | CHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end()); |
| 672 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 673 | DCHECK(thread_local_run != nullptr); |
| 674 | DCHECK(thread_local_run->IsThreadLocal() || thread_local_run == dedicated_full_run_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 675 | slot_addr = thread_local_run->AllocSlot(); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 676 | // The allocation must fail if the run is invalid. |
| 677 | DCHECK(thread_local_run != dedicated_full_run_ || slot_addr == nullptr) |
| 678 | << "allocated from an invalid run"; |
| 679 | if (UNLIKELY(slot_addr == nullptr)) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 680 | // The run got full. Try to free slots. |
| 681 | DCHECK(thread_local_run->IsFull()); |
| 682 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| 683 | bool is_all_free_after_merge; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 684 | // This is safe to do for the dedicated_full_run_ since the bitmaps are empty. |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 685 | if (thread_local_run->MergeThreadLocalFreeListToFreeList(&is_all_free_after_merge)) { |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 686 | DCHECK_NE(thread_local_run, dedicated_full_run_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 687 | // Some slot got freed. Keep it. |
| 688 | DCHECK(!thread_local_run->IsFull()); |
| 689 | DCHECK_EQ(is_all_free_after_merge, thread_local_run->IsAllFree()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 690 | } else { |
| 691 | // No slots got freed. Try to refill the thread-local run. |
| 692 | DCHECK(thread_local_run->IsFull()); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 693 | if (thread_local_run != dedicated_full_run_) { |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 694 | thread_local_run->SetIsThreadLocal(false); |
| 695 | if (kIsDebugBuild) { |
| 696 | full_runs_[idx].insert(thread_local_run); |
| 697 | if (kTraceRosAlloc) { |
| 698 | LOG(INFO) << "RosAlloc::AllocFromRun() : Inserted run 0x" << std::hex |
| 699 | << reinterpret_cast<intptr_t>(thread_local_run) |
| 700 | << " into full_runs_[" << std::dec << idx << "]"; |
| 701 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 702 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 703 | DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 704 | DCHECK(full_runs_[idx].find(thread_local_run) != full_runs_[idx].end()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 705 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 706 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 707 | thread_local_run = RefillRun(self, idx); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 708 | if (UNLIKELY(thread_local_run == nullptr)) { |
| 709 | self->SetRosAllocRun(idx, dedicated_full_run_); |
| 710 | return nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 711 | } |
| 712 | DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 713 | DCHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end()); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 714 | thread_local_run->SetIsThreadLocal(true); |
| Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 715 | self->SetRosAllocRun(idx, thread_local_run); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 716 | DCHECK(!thread_local_run->IsFull()); |
| 717 | } |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 718 | DCHECK(thread_local_run != nullptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 719 | DCHECK(!thread_local_run->IsFull()); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 720 | DCHECK(thread_local_run->IsThreadLocal()); |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 721 | // Account for all the free slots in the new or refreshed thread local run. |
| 722 | *bytes_tl_bulk_allocated = thread_local_run->NumberOfFreeSlots() * bracket_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 723 | slot_addr = thread_local_run->AllocSlot(); |
| 724 | // Must succeed now with a new run. |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 725 | DCHECK(slot_addr != nullptr); |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 726 | } else { |
| 727 | // The slot is already counted. Leave it as is. |
| 728 | *bytes_tl_bulk_allocated = 0; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 729 | } |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 730 | DCHECK(slot_addr != nullptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 731 | if (kTraceRosAlloc) { |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 732 | LOG(INFO) << "RosAlloc::AllocFromRun() thread-local : 0x" << std::hex |
| 733 | << reinterpret_cast<intptr_t>(slot_addr) |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 734 | << "-0x" << (reinterpret_cast<intptr_t>(slot_addr) + bracket_size) |
| 735 | << "(" << std::dec << (bracket_size) << ")"; |
| 736 | } |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 737 | *bytes_allocated = bracket_size; |
| 738 | *usable_size = bracket_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 739 | } else { |
| 740 | // Use the (shared) current run. |
| 741 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 742 | slot_addr = AllocFromCurrentRunUnlocked(self, idx); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 743 | if (kTraceRosAlloc) { |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 744 | LOG(INFO) << "RosAlloc::AllocFromRun() : 0x" << std::hex |
| 745 | << reinterpret_cast<intptr_t>(slot_addr) |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 746 | << "-0x" << (reinterpret_cast<intptr_t>(slot_addr) + bracket_size) |
| 747 | << "(" << std::dec << (bracket_size) << ")"; |
| 748 | } |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 749 | if (LIKELY(slot_addr != nullptr)) { |
| 750 | *bytes_allocated = bracket_size; |
| 751 | *usable_size = bracket_size; |
| 752 | *bytes_tl_bulk_allocated = bracket_size; |
| 753 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 754 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 755 | // Caller verifies that it is all 0. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 756 | return slot_addr; |
| 757 | } |
| 758 | |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 759 | size_t RosAlloc::FreeFromRun(Thread* self, void* ptr, Run* run) { |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 760 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| 761 | DCHECK_LT(run, ptr); |
| 762 | DCHECK_LT(ptr, run->End()); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 763 | const size_t idx = run->size_bracket_idx_; |
| 764 | const size_t bracket_size = bracketSizes[idx]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 765 | bool run_was_full = false; |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 766 | MutexLock brackets_mu(self, *size_bracket_locks_[idx]); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 767 | if (kIsDebugBuild) { |
| 768 | run_was_full = run->IsFull(); |
| 769 | } |
| 770 | if (kTraceRosAlloc) { |
| 771 | LOG(INFO) << "RosAlloc::FreeFromRun() : 0x" << std::hex << reinterpret_cast<intptr_t>(ptr); |
| 772 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 773 | if (LIKELY(run->IsThreadLocal())) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 774 | // It's a thread-local run. Just mark the thread-local free bit map and return. |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 775 | DCHECK_LT(run->size_bracket_idx_, kNumThreadLocalSizeBrackets); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 776 | DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end()); |
| 777 | DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end()); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 778 | run->AddToThreadLocalFreeList(ptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 779 | if (kTraceRosAlloc) { |
| 780 | LOG(INFO) << "RosAlloc::FreeFromRun() : Freed a slot in a thread local run 0x" << std::hex |
| 781 | << reinterpret_cast<intptr_t>(run); |
| 782 | } |
| 783 | // A thread local run will be kept as a thread local even if it's become all free. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 784 | return bracket_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 785 | } |
| 786 | // Free the slot in the run. |
| 787 | run->FreeSlot(ptr); |
| Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 788 | auto* non_full_runs = &non_full_runs_[idx]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 789 | if (run->IsAllFree()) { |
| 790 | // It has just become completely free. Free the pages of this run. |
| 791 | std::set<Run*>::iterator pos = non_full_runs->find(run); |
| 792 | if (pos != non_full_runs->end()) { |
| 793 | non_full_runs->erase(pos); |
| 794 | if (kTraceRosAlloc) { |
| 795 | LOG(INFO) << "RosAlloc::FreeFromRun() : Erased run 0x" << std::hex |
| 796 | << reinterpret_cast<intptr_t>(run) << " from non_full_runs_"; |
| 797 | } |
| 798 | } |
| 799 | if (run == current_runs_[idx]) { |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 800 | current_runs_[idx] = dedicated_full_run_; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 801 | } |
| 802 | DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end()); |
| 803 | DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end()); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 804 | run->ZeroHeaderAndSlotHeaders(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 805 | { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 806 | MutexLock lock_mu(self, lock_); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 807 | FreePages(self, run, true); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 808 | } |
| 809 | } else { |
| 810 | // It is not completely free. If it wasn't the current run or |
| 811 | // already in the non-full run set (i.e., it was full) insert it |
| 812 | // into the non-full run set. |
| 813 | if (run != current_runs_[idx]) { |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 814 | auto* full_runs = kIsDebugBuild ? &full_runs_[idx] : nullptr; |
| Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 815 | auto pos = non_full_runs->find(run); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 816 | if (pos == non_full_runs->end()) { |
| 817 | DCHECK(run_was_full); |
| 818 | DCHECK(full_runs->find(run) != full_runs->end()); |
| 819 | if (kIsDebugBuild) { |
| 820 | full_runs->erase(run); |
| 821 | if (kTraceRosAlloc) { |
| 822 | LOG(INFO) << "RosAlloc::FreeFromRun() : Erased run 0x" << std::hex |
| 823 | << reinterpret_cast<intptr_t>(run) << " from full_runs_"; |
| 824 | } |
| 825 | } |
| 826 | non_full_runs->insert(run); |
| 827 | DCHECK(!run->IsFull()); |
| 828 | if (kTraceRosAlloc) { |
| 829 | LOG(INFO) << "RosAlloc::FreeFromRun() : Inserted run 0x" << std::hex |
| 830 | << reinterpret_cast<intptr_t>(run) |
| 831 | << " into non_full_runs_[" << std::dec << idx << "]"; |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 836 | return bracket_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 839 | template<bool kUseTail> |
| 840 | std::string RosAlloc::Run::FreeListToStr(SlotFreeList<kUseTail>* free_list) { |
| 841 | std::string free_list_str; |
| 842 | const uint8_t idx = size_bracket_idx_; |
| 843 | const size_t bracket_size = bracketSizes[idx]; |
| 844 | for (Slot* slot = free_list->Head(); slot != nullptr; slot = slot->Next()) { |
| 845 | bool is_last = slot->Next() == nullptr; |
| 846 | uintptr_t slot_offset = reinterpret_cast<uintptr_t>(slot) - |
| 847 | reinterpret_cast<uintptr_t>(FirstSlot()); |
| 848 | DCHECK_EQ(slot_offset % bracket_size, 0U); |
| 849 | uintptr_t slot_idx = slot_offset / bracket_size; |
| 850 | if (!is_last) { |
| 851 | free_list_str.append(StringPrintf("%u-", static_cast<uint32_t>(slot_idx))); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 852 | } else { |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 853 | free_list_str.append(StringPrintf("%u", static_cast<uint32_t>(slot_idx))); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 856 | return free_list_str; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | std::string RosAlloc::Run::Dump() { |
| 860 | size_t idx = size_bracket_idx_; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 861 | std::ostringstream stream; |
| 862 | stream << "RosAlloc Run = " << reinterpret_cast<void*>(this) |
| 863 | << "{ magic_num=" << static_cast<int>(magic_num_) |
| 864 | << " size_bracket_idx=" << idx |
| 865 | << " is_thread_local=" << static_cast<int>(is_thread_local_) |
| 866 | << " to_be_bulk_freed=" << static_cast<int>(to_be_bulk_freed_) |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 867 | << " free_list=" << FreeListToStr(&free_list_) |
| 868 | << " bulk_free_list=" << FreeListToStr(&bulk_free_list_) |
| 869 | << " thread_local_list=" << FreeListToStr(&thread_local_free_list_) |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 870 | << " }" << std::endl; |
| 871 | return stream.str(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 874 | void RosAlloc::Run::FreeSlot(void* ptr) { |
| 875 | DCHECK(!IsThreadLocal()); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 876 | const uint8_t idx = size_bracket_idx_; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 877 | const size_t bracket_size = bracketSizes[idx]; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 878 | Slot* slot = ToSlot(ptr); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 879 | // Zero out the memory. |
| 880 | // TODO: Investigate alternate memset since ptr is guaranteed to be aligned to 16. |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 881 | memset(slot, 0, bracket_size); |
| 882 | free_list_.Add(slot); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 883 | if (kTraceRosAlloc) { |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 884 | LOG(INFO) << "RosAlloc::Run::FreeSlot() : " << slot |
| 885 | << ", bracket_size=" << std::dec << bracket_size << ", slot_idx=" << SlotIndex(slot); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 889 | inline bool RosAlloc::Run::MergeThreadLocalFreeListToFreeList(bool* is_all_free_after_out) { |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 890 | DCHECK(IsThreadLocal()); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 891 | // Merge the thread local free list into the free list and clear the thread local free list. |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 892 | const uint8_t idx = size_bracket_idx_; |
| Andreas Gampe | 7c5acbb | 2018-09-20 13:54:52 -0700 | [diff] [blame] | 893 | size_t thread_local_free_list_size = thread_local_free_list_.Size(); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 894 | const size_t size_before = free_list_.Size(); |
| 895 | free_list_.Merge(&thread_local_free_list_); |
| 896 | const size_t size_after = free_list_.Size(); |
| 897 | DCHECK_EQ(size_before < size_after, thread_local_free_list_size > 0); |
| 898 | DCHECK_LE(size_before, size_after); |
| 899 | *is_all_free_after_out = free_list_.Size() == numOfSlots[idx]; |
| 900 | // Return true at least one slot was added to the free list. |
| 901 | return size_before < size_after; |
| 902 | } |
| 903 | |
| 904 | inline void RosAlloc::Run::MergeBulkFreeListToFreeList() { |
| 905 | DCHECK(!IsThreadLocal()); |
| 906 | // Merge the bulk free list into the free list and clear the bulk free list. |
| 907 | free_list_.Merge(&bulk_free_list_); |
| 908 | } |
| 909 | |
| 910 | inline void RosAlloc::Run::MergeBulkFreeListToThreadLocalFreeList() { |
| 911 | DCHECK(IsThreadLocal()); |
| 912 | // Merge the bulk free list into the thread local free list and clear the bulk free list. |
| 913 | thread_local_free_list_.Merge(&bulk_free_list_); |
| 914 | } |
| 915 | |
| 916 | inline void RosAlloc::Run::AddToThreadLocalFreeList(void* ptr) { |
| 917 | DCHECK(IsThreadLocal()); |
| 918 | AddToFreeListShared(ptr, &thread_local_free_list_, __FUNCTION__); |
| 919 | } |
| 920 | |
| 921 | inline size_t RosAlloc::Run::AddToBulkFreeList(void* ptr) { |
| 922 | return AddToFreeListShared(ptr, &bulk_free_list_, __FUNCTION__); |
| 923 | } |
| 924 | |
| 925 | inline size_t RosAlloc::Run::AddToFreeListShared(void* ptr, |
| 926 | SlotFreeList<true>* free_list, |
| 927 | const char* caller_name) { |
| 928 | const uint8_t idx = size_bracket_idx_; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 929 | const size_t bracket_size = bracketSizes[idx]; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 930 | Slot* slot = ToSlot(ptr); |
| 931 | memset(slot, 0, bracket_size); |
| 932 | free_list->Add(slot); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 933 | if (kTraceRosAlloc) { |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 934 | LOG(INFO) << "RosAlloc::Run::" << caller_name << "() : " << ptr |
| 935 | << ", bracket_size=" << std::dec << bracket_size << ", slot_idx=" << SlotIndex(slot); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 936 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 937 | return bracket_size; |
| 938 | } |
| 939 | |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 940 | inline void RosAlloc::Run::ZeroHeaderAndSlotHeaders() { |
| 941 | DCHECK(IsAllFree()); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 942 | const uint8_t idx = size_bracket_idx_; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 943 | // Zero the slot header (next pointers). |
| 944 | for (Slot* slot = free_list_.Head(); slot != nullptr; ) { |
| 945 | Slot* next_slot = slot->Next(); |
| 946 | slot->Clear(); |
| 947 | slot = next_slot; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 948 | } |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 949 | // Zero the header. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 950 | memset(this, 0, headerSizes[idx]); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 951 | // Check that the entire run is all zero. |
| 952 | if (kIsDebugBuild) { |
| 953 | const size_t size = numOfPages[idx] * kPageSize; |
| 954 | const uintptr_t* word_ptr = reinterpret_cast<uintptr_t*>(this); |
| 955 | for (size_t i = 0; i < size / sizeof(uintptr_t); ++i) { |
| 956 | CHECK_EQ(word_ptr[i], 0U) << "words don't match at index " << i; |
| 957 | } |
| 958 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | inline void RosAlloc::Run::ZeroData() { |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 962 | const uint8_t idx = size_bracket_idx_; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 963 | uint8_t* slot_begin = reinterpret_cast<uint8_t*>(FirstSlot()); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 964 | memset(slot_begin, 0, numOfSlots[idx] * bracketSizes[idx]); |
| 965 | } |
| 966 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 967 | void RosAlloc::Run::InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), |
| 968 | void* arg) { |
| 969 | size_t idx = size_bracket_idx_; |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 970 | uint8_t* slot_base = reinterpret_cast<uint8_t*>(this) + headerSizes[idx]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 971 | size_t num_slots = numOfSlots[idx]; |
| 972 | size_t bracket_size = IndexToBracketSize(idx); |
| Mathieu Chartier | c38c5ea | 2015-02-04 17:46:29 -0800 | [diff] [blame] | 973 | DCHECK_EQ(slot_base + num_slots * bracket_size, |
| 974 | reinterpret_cast<uint8_t*>(this) + numOfPages[idx] * kPageSize); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 975 | // Free slots are on the free list and the allocated/used slots are not. We traverse the free list |
| 976 | // to find out and record which slots are free in the is_free array. |
| 977 | std::unique_ptr<bool[]> is_free(new bool[num_slots]()); // zero initialized |
| 978 | for (Slot* slot = free_list_.Head(); slot != nullptr; slot = slot->Next()) { |
| 979 | size_t slot_idx = SlotIndex(slot); |
| 980 | DCHECK_LT(slot_idx, num_slots); |
| 981 | is_free[slot_idx] = true; |
| 982 | } |
| 983 | if (IsThreadLocal()) { |
| 984 | for (Slot* slot = thread_local_free_list_.Head(); slot != nullptr; slot = slot->Next()) { |
| 985 | size_t slot_idx = SlotIndex(slot); |
| 986 | DCHECK_LT(slot_idx, num_slots); |
| 987 | is_free[slot_idx] = true; |
| Mathieu Chartier | c38c5ea | 2015-02-04 17:46:29 -0800 | [diff] [blame] | 988 | } |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 989 | } |
| 990 | for (size_t slot_idx = 0; slot_idx < num_slots; ++slot_idx) { |
| 991 | uint8_t* slot_addr = slot_base + slot_idx * bracket_size; |
| 992 | if (!is_free[slot_idx]) { |
| 993 | handler(slot_addr, slot_addr + bracket_size, bracket_size, arg); |
| 994 | } else { |
| 995 | handler(slot_addr, slot_addr + bracket_size, 0, arg); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1000 | // If true, read the page map entries in BulkFree() without using the |
| 1001 | // lock for better performance, assuming that the existence of an |
| 1002 | // allocated chunk/pointer being freed in BulkFree() guarantees that |
| Hans Boehm | a253c2d | 2019-05-13 12:38:54 -0700 | [diff] [blame] | 1003 | // the page map entry won't change. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1004 | static constexpr bool kReadPageMapEntryWithoutLockInBulkFree = true; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1005 | |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1006 | size_t RosAlloc::BulkFree(Thread* self, void** ptrs, size_t num_ptrs) { |
| 1007 | size_t freed_bytes = 0; |
| Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 1008 | if ((false)) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1009 | // Used only to test Free() as GC uses only BulkFree(). |
| 1010 | for (size_t i = 0; i < num_ptrs; ++i) { |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1011 | freed_bytes += FreeInternal(self, ptrs[i]); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1012 | } |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1013 | return freed_bytes; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | WriterMutexLock wmu(self, bulk_free_lock_); |
| 1017 | |
| 1018 | // First mark slots to free in the bulk free bit map without locking the |
| Ian Rogers | 5fcfa7d | 2014-05-15 11:43:06 -0700 | [diff] [blame] | 1019 | // size bracket locks. On host, unordered_set is faster than vector + flag. |
| Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 1020 | #ifdef ART_TARGET_ANDROID |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1021 | std::vector<Run*> runs; |
| 1022 | #else |
| Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1023 | std::unordered_set<Run*, hash_run, eq_run> runs; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1024 | #endif |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1025 | for (size_t i = 0; i < num_ptrs; i++) { |
| 1026 | void* ptr = ptrs[i]; |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1027 | DCHECK_LE(base_, ptr); |
| 1028 | DCHECK_LT(ptr, base_ + footprint_); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1029 | size_t pm_idx = RoundDownToPageMapIndex(ptr); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1030 | Run* run = nullptr; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1031 | if (kReadPageMapEntryWithoutLockInBulkFree) { |
| 1032 | // Read the page map entries without locking the lock. |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1033 | uint8_t page_map_entry = page_map_[pm_idx]; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1034 | if (kTraceRosAlloc) { |
| 1035 | LOG(INFO) << "RosAlloc::BulkFree() : " << std::hex << ptr << ", pm_idx=" |
| 1036 | << std::dec << pm_idx |
| 1037 | << ", page_map_entry=" << static_cast<int>(page_map_entry); |
| 1038 | } |
| 1039 | if (LIKELY(page_map_entry == kPageMapRun)) { |
| 1040 | run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1041 | } else if (LIKELY(page_map_entry == kPageMapRunPart)) { |
| 1042 | size_t pi = pm_idx; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1043 | // Find the beginning of the run. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1044 | do { |
| 1045 | --pi; |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1046 | DCHECK_LT(pi, capacity_ / kPageSize); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1047 | } while (page_map_[pi] != kPageMapRun); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1048 | run = reinterpret_cast<Run*>(base_ + pi * kPageSize); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1049 | } else if (page_map_entry == kPageMapLargeObject) { |
| 1050 | MutexLock mu(self, lock_); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1051 | freed_bytes += FreePages(self, ptr, false); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1052 | continue; |
| 1053 | } else { |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1054 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_entry); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1055 | } |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1056 | } else { |
| 1057 | // Read the page map entries with a lock. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1058 | MutexLock mu(self, lock_); |
| 1059 | DCHECK_LT(pm_idx, page_map_size_); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1060 | uint8_t page_map_entry = page_map_[pm_idx]; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1061 | if (kTraceRosAlloc) { |
| 1062 | LOG(INFO) << "RosAlloc::BulkFree() : " << std::hex << ptr << ", pm_idx=" |
| 1063 | << std::dec << pm_idx |
| 1064 | << ", page_map_entry=" << static_cast<int>(page_map_entry); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1065 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1066 | if (LIKELY(page_map_entry == kPageMapRun)) { |
| 1067 | run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
| 1068 | } else if (LIKELY(page_map_entry == kPageMapRunPart)) { |
| 1069 | size_t pi = pm_idx; |
| 1070 | // Find the beginning of the run. |
| 1071 | do { |
| 1072 | --pi; |
| 1073 | DCHECK_LT(pi, capacity_ / kPageSize); |
| 1074 | } while (page_map_[pi] != kPageMapRun); |
| 1075 | run = reinterpret_cast<Run*>(base_ + pi * kPageSize); |
| 1076 | } else if (page_map_entry == kPageMapLargeObject) { |
| 1077 | freed_bytes += FreePages(self, ptr, false); |
| 1078 | continue; |
| 1079 | } else { |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1080 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_entry); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1083 | DCHECK(run != nullptr); |
| 1084 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| 1085 | // Set the bit in the bulk free bit map. |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1086 | freed_bytes += run->AddToBulkFreeList(ptr); |
| Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 1087 | #ifdef ART_TARGET_ANDROID |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1088 | if (!run->to_be_bulk_freed_) { |
| 1089 | run->to_be_bulk_freed_ = true; |
| 1090 | runs.push_back(run); |
| 1091 | } |
| 1092 | #else |
| 1093 | runs.insert(run); |
| 1094 | #endif |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | // Now, iterate over the affected runs and update the alloc bit map |
| 1098 | // based on the bulk free bit map (for non-thread-local runs) and |
| 1099 | // union the bulk free bit map into the thread-local free bit map |
| 1100 | // (for thread-local runs.) |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1101 | for (Run* run : runs) { |
| Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 1102 | #ifdef ART_TARGET_ANDROID |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1103 | DCHECK(run->to_be_bulk_freed_); |
| 1104 | run->to_be_bulk_freed_ = false; |
| 1105 | #endif |
| 1106 | size_t idx = run->size_bracket_idx_; |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1107 | MutexLock brackets_mu(self, *size_bracket_locks_[idx]); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1108 | if (run->IsThreadLocal()) { |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1109 | DCHECK_LT(run->size_bracket_idx_, kNumThreadLocalSizeBrackets); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1110 | DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end()); |
| 1111 | DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end()); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1112 | run->MergeBulkFreeListToThreadLocalFreeList(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1113 | if (kTraceRosAlloc) { |
| 1114 | LOG(INFO) << "RosAlloc::BulkFree() : Freed slot(s) in a thread local run 0x" |
| 1115 | << std::hex << reinterpret_cast<intptr_t>(run); |
| 1116 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1117 | DCHECK(run->IsThreadLocal()); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1118 | // A thread local run will be kept as a thread local even if |
| 1119 | // it's become all free. |
| 1120 | } else { |
| 1121 | bool run_was_full = run->IsFull(); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1122 | run->MergeBulkFreeListToFreeList(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1123 | if (kTraceRosAlloc) { |
| 1124 | LOG(INFO) << "RosAlloc::BulkFree() : Freed slot(s) in a run 0x" << std::hex |
| 1125 | << reinterpret_cast<intptr_t>(run); |
| 1126 | } |
| 1127 | // Check if the run should be moved to non_full_runs_ or |
| 1128 | // free_page_runs_. |
| Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 1129 | auto* non_full_runs = &non_full_runs_[idx]; |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1130 | auto* full_runs = kIsDebugBuild ? &full_runs_[idx] : nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1131 | if (run->IsAllFree()) { |
| 1132 | // It has just become completely free. Free the pages of the |
| 1133 | // run. |
| 1134 | bool run_was_current = run == current_runs_[idx]; |
| 1135 | if (run_was_current) { |
| 1136 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1137 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1138 | // If it was a current run, reuse it. |
| 1139 | } else if (run_was_full) { |
| 1140 | // If it was full, remove it from the full run set (debug |
| 1141 | // only.) |
| 1142 | if (kIsDebugBuild) { |
| Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1143 | std::unordered_set<Run*, hash_run, eq_run>::iterator pos = full_runs->find(run); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1144 | DCHECK(pos != full_runs->end()); |
| 1145 | full_runs->erase(pos); |
| 1146 | if (kTraceRosAlloc) { |
| 1147 | LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex |
| 1148 | << reinterpret_cast<intptr_t>(run) |
| 1149 | << " from full_runs_"; |
| 1150 | } |
| 1151 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1152 | } |
| 1153 | } else { |
| 1154 | // If it was in a non full run set, remove it from the set. |
| 1155 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1156 | DCHECK(non_full_runs->find(run) != non_full_runs->end()); |
| 1157 | non_full_runs->erase(run); |
| 1158 | if (kTraceRosAlloc) { |
| 1159 | LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex |
| 1160 | << reinterpret_cast<intptr_t>(run) |
| 1161 | << " from non_full_runs_"; |
| 1162 | } |
| 1163 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1164 | } |
| 1165 | if (!run_was_current) { |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1166 | run->ZeroHeaderAndSlotHeaders(); |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1167 | MutexLock lock_mu(self, lock_); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1168 | FreePages(self, run, true); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1169 | } |
| 1170 | } else { |
| 1171 | // It is not completely free. If it wasn't the current run or |
| 1172 | // already in the non-full run set (i.e., it was full) insert |
| 1173 | // it into the non-full run set. |
| 1174 | if (run == current_runs_[idx]) { |
| 1175 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1176 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1177 | // If it was a current run, keep it. |
| 1178 | } else if (run_was_full) { |
| 1179 | // If it was full, remove it from the full run set (debug |
| 1180 | // only) and insert into the non-full run set. |
| 1181 | DCHECK(full_runs->find(run) != full_runs->end()); |
| 1182 | DCHECK(non_full_runs->find(run) == non_full_runs->end()); |
| 1183 | if (kIsDebugBuild) { |
| 1184 | full_runs->erase(run); |
| 1185 | if (kTraceRosAlloc) { |
| 1186 | LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex |
| 1187 | << reinterpret_cast<intptr_t>(run) |
| 1188 | << " from full_runs_"; |
| 1189 | } |
| 1190 | } |
| 1191 | non_full_runs->insert(run); |
| 1192 | if (kTraceRosAlloc) { |
| 1193 | LOG(INFO) << "RosAlloc::BulkFree() : Inserted run 0x" << std::hex |
| 1194 | << reinterpret_cast<intptr_t>(run) |
| 1195 | << " into non_full_runs_[" << std::dec << idx; |
| 1196 | } |
| 1197 | } else { |
| 1198 | // If it was not full, so leave it in the non full run set. |
| 1199 | DCHECK(full_runs->find(run) == full_runs->end()); |
| 1200 | DCHECK(non_full_runs->find(run) != non_full_runs->end()); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1205 | return freed_bytes; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1208 | std::string RosAlloc::DumpPageMap() { |
| 1209 | std::ostringstream stream; |
| 1210 | stream << "RosAlloc PageMap: " << std::endl; |
| 1211 | lock_.AssertHeld(Thread::Current()); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1212 | size_t end = page_map_size_; |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1213 | FreePageRun* curr_fpr = nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1214 | size_t curr_fpr_size = 0; |
| 1215 | size_t remaining_curr_fpr_size = 0; |
| 1216 | size_t num_running_empty_pages = 0; |
| 1217 | for (size_t i = 0; i < end; ++i) { |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1218 | uint8_t pm = page_map_[i]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1219 | switch (pm) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1220 | case kPageMapReleased: |
| 1221 | // Fall-through. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1222 | case kPageMapEmpty: { |
| 1223 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
| 1224 | if (free_page_runs_.find(fpr) != free_page_runs_.end()) { |
| 1225 | // Encountered a fresh free page run. |
| 1226 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1227 | DCHECK(fpr->IsFree()); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1228 | DCHECK(curr_fpr == nullptr); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1229 | DCHECK_EQ(curr_fpr_size, static_cast<size_t>(0)); |
| 1230 | curr_fpr = fpr; |
| 1231 | curr_fpr_size = fpr->ByteSize(this); |
| 1232 | DCHECK_EQ(curr_fpr_size % kPageSize, static_cast<size_t>(0)); |
| 1233 | remaining_curr_fpr_size = curr_fpr_size - kPageSize; |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1234 | stream << "[" << i << "]=" << (pm == kPageMapReleased ? "Released" : "Empty") |
| 1235 | << " (FPR start) fpr_size=" << curr_fpr_size |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1236 | << " remaining_fpr_size=" << remaining_curr_fpr_size << std::endl; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1237 | if (remaining_curr_fpr_size == 0) { |
| 1238 | // Reset at the end of the current free page run. |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1239 | curr_fpr = nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1240 | curr_fpr_size = 0; |
| 1241 | } |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1242 | stream << "curr_fpr=0x" << std::hex << reinterpret_cast<intptr_t>(curr_fpr) << std::endl; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1243 | DCHECK_EQ(num_running_empty_pages, static_cast<size_t>(0)); |
| 1244 | } else { |
| 1245 | // Still part of the current free page run. |
| 1246 | DCHECK_NE(num_running_empty_pages, static_cast<size_t>(0)); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1247 | DCHECK(curr_fpr != nullptr && curr_fpr_size > 0 && remaining_curr_fpr_size > 0); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1248 | DCHECK_EQ(remaining_curr_fpr_size % kPageSize, static_cast<size_t>(0)); |
| 1249 | DCHECK_GE(remaining_curr_fpr_size, static_cast<size_t>(kPageSize)); |
| 1250 | remaining_curr_fpr_size -= kPageSize; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1251 | stream << "[" << i << "]=Empty (FPR part)" |
| 1252 | << " remaining_fpr_size=" << remaining_curr_fpr_size << std::endl; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1253 | if (remaining_curr_fpr_size == 0) { |
| 1254 | // Reset at the end of the current free page run. |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1255 | curr_fpr = nullptr; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1256 | curr_fpr_size = 0; |
| 1257 | } |
| 1258 | } |
| 1259 | num_running_empty_pages++; |
| 1260 | break; |
| 1261 | } |
| 1262 | case kPageMapLargeObject: { |
| 1263 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1264 | num_running_empty_pages = 0; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1265 | stream << "[" << i << "]=Large (start)" << std::endl; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | case kPageMapLargeObjectPart: |
| 1269 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1270 | num_running_empty_pages = 0; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1271 | stream << "[" << i << "]=Large (part)" << std::endl; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1272 | break; |
| 1273 | case kPageMapRun: { |
| 1274 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1275 | num_running_empty_pages = 0; |
| 1276 | Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize); |
| 1277 | size_t idx = run->size_bracket_idx_; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1278 | stream << "[" << i << "]=Run (start)" |
| 1279 | << " idx=" << idx |
| 1280 | << " numOfPages=" << numOfPages[idx] |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1281 | << " is_thread_local=" << run->is_thread_local_ |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1282 | << " is_all_free=" << (run->IsAllFree() ? 1 : 0) |
| 1283 | << std::endl; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1284 | break; |
| 1285 | } |
| 1286 | case kPageMapRunPart: |
| 1287 | DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0)); |
| 1288 | num_running_empty_pages = 0; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1289 | stream << "[" << i << "]=Run (part)" << std::endl; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1290 | break; |
| 1291 | default: |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1292 | stream << "[" << i << "]=Unrecognizable page map type: " << pm; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1293 | break; |
| 1294 | } |
| 1295 | } |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1296 | return stream.str(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 1299 | size_t RosAlloc::UsableSize(const void* ptr) { |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1300 | DCHECK_LE(base_, ptr); |
| 1301 | DCHECK_LT(ptr, base_ + footprint_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1302 | size_t pm_idx = RoundDownToPageMapIndex(ptr); |
| 1303 | MutexLock mu(Thread::Current(), lock_); |
| 1304 | switch (page_map_[pm_idx]) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1305 | case kPageMapReleased: |
| 1306 | // Fall-through. |
| 1307 | case kPageMapEmpty: |
| 1308 | LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << ": pm_idx=" << pm_idx << ", ptr=" |
| 1309 | << std::hex << reinterpret_cast<intptr_t>(ptr); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1310 | UNREACHABLE(); |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1311 | case kPageMapLargeObject: { |
| 1312 | size_t num_pages = 1; |
| 1313 | size_t idx = pm_idx + 1; |
| 1314 | size_t end = page_map_size_; |
| 1315 | while (idx < end && page_map_[idx] == kPageMapLargeObjectPart) { |
| 1316 | num_pages++; |
| 1317 | idx++; |
| 1318 | } |
| 1319 | return num_pages * kPageSize; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1320 | } |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1321 | case kPageMapLargeObjectPart: |
| 1322 | LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << ": pm_idx=" << pm_idx << ", ptr=" |
| 1323 | << std::hex << reinterpret_cast<intptr_t>(ptr); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1324 | UNREACHABLE(); |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1325 | case kPageMapRun: |
| 1326 | case kPageMapRunPart: { |
| 1327 | // Find the beginning of the run. |
| 1328 | while (page_map_[pm_idx] != kPageMapRun) { |
| 1329 | pm_idx--; |
| 1330 | DCHECK_LT(pm_idx, capacity_ / kPageSize); |
| 1331 | } |
| 1332 | DCHECK_EQ(page_map_[pm_idx], kPageMapRun); |
| 1333 | Run* run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize); |
| 1334 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| 1335 | size_t idx = run->size_bracket_idx_; |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 1336 | size_t offset_from_slot_base = reinterpret_cast<const uint8_t*>(ptr) |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1337 | - (reinterpret_cast<uint8_t*>(run) + headerSizes[idx]); |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1338 | DCHECK_EQ(offset_from_slot_base % bracketSizes[idx], static_cast<size_t>(0)); |
| 1339 | return IndexToBracketSize(idx); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1340 | } |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1341 | default: { |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1342 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1343 | UNREACHABLE(); |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1344 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1345 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | bool RosAlloc::Trim() { |
| 1349 | MutexLock mu(Thread::Current(), lock_); |
| 1350 | FreePageRun* last_free_page_run; |
| 1351 | DCHECK_EQ(footprint_ % kPageSize, static_cast<size_t>(0)); |
| 1352 | auto it = free_page_runs_.rbegin(); |
| 1353 | if (it != free_page_runs_.rend() && (last_free_page_run = *it)->End(this) == base_ + footprint_) { |
| 1354 | // Remove the last free page run, if any. |
| 1355 | DCHECK(last_free_page_run->IsFree()); |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1356 | DCHECK(IsFreePage(ToPageMapIndex(last_free_page_run))); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1357 | DCHECK_EQ(last_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0)); |
| 1358 | DCHECK_EQ(last_free_page_run->End(this), base_ + footprint_); |
| 1359 | free_page_runs_.erase(last_free_page_run); |
| 1360 | size_t decrement = last_free_page_run->ByteSize(this); |
| 1361 | size_t new_footprint = footprint_ - decrement; |
| 1362 | DCHECK_EQ(new_footprint % kPageSize, static_cast<size_t>(0)); |
| 1363 | size_t new_num_of_pages = new_footprint / kPageSize; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1364 | DCHECK_GE(page_map_size_, new_num_of_pages); |
| 1365 | // Zero out the tail of the page map. |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1366 | uint8_t* zero_begin = const_cast<uint8_t*>(page_map_) + new_num_of_pages; |
| 1367 | uint8_t* madvise_begin = AlignUp(zero_begin, kPageSize); |
| Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 1368 | DCHECK_LE(madvise_begin, page_map_mem_map_.End()); |
| 1369 | size_t madvise_size = page_map_mem_map_.End() - madvise_begin; |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1370 | if (madvise_size > 0) { |
| 1371 | DCHECK_ALIGNED(madvise_begin, kPageSize); |
| 1372 | DCHECK_EQ(RoundUp(madvise_size, kPageSize), madvise_size); |
| Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 1373 | if (!kMadviseZeroes) { |
| 1374 | memset(madvise_begin, 0, madvise_size); |
| 1375 | } |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1376 | CHECK_EQ(madvise(madvise_begin, madvise_size, MADV_DONTNEED), 0); |
| 1377 | } |
| 1378 | if (madvise_begin - zero_begin) { |
| 1379 | memset(zero_begin, 0, madvise_begin - zero_begin); |
| 1380 | } |
| 1381 | page_map_size_ = new_num_of_pages; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1382 | free_page_run_size_map_.resize(new_num_of_pages); |
| 1383 | DCHECK_EQ(free_page_run_size_map_.size(), new_num_of_pages); |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1384 | ArtRosAllocMoreCore(this, -(static_cast<intptr_t>(decrement))); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1385 | if (kTraceRosAlloc) { |
| 1386 | LOG(INFO) << "RosAlloc::Trim() : decreased the footprint from " |
| 1387 | << footprint_ << " to " << new_footprint; |
| 1388 | } |
| 1389 | DCHECK_LT(new_footprint, footprint_); |
| 1390 | DCHECK_LT(new_footprint, capacity_); |
| 1391 | footprint_ = new_footprint; |
| 1392 | return true; |
| 1393 | } |
| 1394 | return false; |
| 1395 | } |
| 1396 | |
| 1397 | void RosAlloc::InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), |
| 1398 | void* arg) { |
| 1399 | // Note: no need to use this to release pages as we already do so in FreePages(). |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1400 | if (handler == nullptr) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1401 | return; |
| 1402 | } |
| 1403 | MutexLock mu(Thread::Current(), lock_); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1404 | size_t pm_end = page_map_size_; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1405 | size_t i = 0; |
| 1406 | while (i < pm_end) { |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1407 | uint8_t pm = page_map_[i]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1408 | switch (pm) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1409 | case kPageMapReleased: |
| 1410 | // Fall-through. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1411 | case kPageMapEmpty: { |
| 1412 | // The start of a free page run. |
| 1413 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
| 1414 | DCHECK(free_page_runs_.find(fpr) != free_page_runs_.end()); |
| 1415 | size_t fpr_size = fpr->ByteSize(this); |
| Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1416 | DCHECK_ALIGNED(fpr_size, kPageSize); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1417 | void* start = fpr; |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 1418 | if (kIsDebugBuild) { |
| 1419 | // In the debug build, the first page of a free page run |
| 1420 | // contains a magic number for debugging. Exclude it. |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1421 | start = reinterpret_cast<uint8_t*>(fpr) + kPageSize; |
| Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 1422 | } |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1423 | void* end = reinterpret_cast<uint8_t*>(fpr) + fpr_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1424 | handler(start, end, 0, arg); |
| 1425 | size_t num_pages = fpr_size / kPageSize; |
| 1426 | if (kIsDebugBuild) { |
| 1427 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1428 | DCHECK(IsFreePage(j)); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | i += fpr_size / kPageSize; |
| 1432 | DCHECK_LE(i, pm_end); |
| 1433 | break; |
| 1434 | } |
| 1435 | case kPageMapLargeObject: { |
| 1436 | // The start of a large object. |
| 1437 | size_t num_pages = 1; |
| 1438 | size_t idx = i + 1; |
| 1439 | while (idx < pm_end && page_map_[idx] == kPageMapLargeObjectPart) { |
| 1440 | num_pages++; |
| 1441 | idx++; |
| 1442 | } |
| 1443 | void* start = base_ + i * kPageSize; |
| 1444 | void* end = base_ + (i + num_pages) * kPageSize; |
| 1445 | size_t used_bytes = num_pages * kPageSize; |
| 1446 | handler(start, end, used_bytes, arg); |
| 1447 | if (kIsDebugBuild) { |
| 1448 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| 1449 | DCHECK_EQ(page_map_[j], kPageMapLargeObjectPart); |
| 1450 | } |
| 1451 | } |
| 1452 | i += num_pages; |
| 1453 | DCHECK_LE(i, pm_end); |
| 1454 | break; |
| 1455 | } |
| 1456 | case kPageMapLargeObjectPart: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1457 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1458 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1459 | case kPageMapRun: { |
| 1460 | // The start of a run. |
| 1461 | Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1462 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1463 | // The dedicated full run doesn't contain any real allocations, don't visit the slots in |
| 1464 | // there. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1465 | run->InspectAllSlots(handler, arg); |
| 1466 | size_t num_pages = numOfPages[run->size_bracket_idx_]; |
| 1467 | if (kIsDebugBuild) { |
| 1468 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| 1469 | DCHECK_EQ(page_map_[j], kPageMapRunPart); |
| 1470 | } |
| 1471 | } |
| 1472 | i += num_pages; |
| 1473 | DCHECK_LE(i, pm_end); |
| 1474 | break; |
| 1475 | } |
| 1476 | case kPageMapRunPart: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1477 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1478 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1479 | default: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1480 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1481 | UNREACHABLE(); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | size_t RosAlloc::Footprint() { |
| 1487 | MutexLock mu(Thread::Current(), lock_); |
| 1488 | return footprint_; |
| 1489 | } |
| 1490 | |
| 1491 | size_t RosAlloc::FootprintLimit() { |
| 1492 | MutexLock mu(Thread::Current(), lock_); |
| 1493 | return capacity_; |
| 1494 | } |
| 1495 | |
| 1496 | void RosAlloc::SetFootprintLimit(size_t new_capacity) { |
| 1497 | MutexLock mu(Thread::Current(), lock_); |
| 1498 | DCHECK_EQ(RoundUp(new_capacity, kPageSize), new_capacity); |
| 1499 | // Only growing is supported here. But Trim() is supported. |
| 1500 | if (capacity_ < new_capacity) { |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1501 | CHECK_LE(new_capacity, max_capacity_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1502 | capacity_ = new_capacity; |
| 1503 | VLOG(heap) << "new capacity=" << capacity_; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| Lei Li | 5784621 | 2015-06-11 17:50:20 +0800 | [diff] [blame] | 1507 | // Below may be called by mutator itself just before thread termination. |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1508 | size_t RosAlloc::RevokeThreadLocalRuns(Thread* thread) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1509 | Thread* self = Thread::Current(); |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1510 | size_t free_bytes = 0U; |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1511 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; idx++) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1512 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1513 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(idx)); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1514 | CHECK(thread_local_run != nullptr); |
| 1515 | // Invalid means already revoked. |
| 1516 | DCHECK(thread_local_run->IsThreadLocal()); |
| 1517 | if (thread_local_run != dedicated_full_run_) { |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1518 | // Note the thread local run may not be full here. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1519 | thread->SetRosAllocRun(idx, dedicated_full_run_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1520 | DCHECK_EQ(thread_local_run->magic_num_, kMagicNum); |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1521 | // Count the number of free slots left. |
| 1522 | size_t num_free_slots = thread_local_run->NumberOfFreeSlots(); |
| 1523 | free_bytes += num_free_slots * bracketSizes[idx]; |
| Lei Li | 5784621 | 2015-06-11 17:50:20 +0800 | [diff] [blame] | 1524 | // The above bracket index lock guards thread local free list to avoid race condition |
| 1525 | // with unioning bulk free list to thread local free list by GC thread in BulkFree. |
| 1526 | // If thread local run is true, GC thread will help update thread local free list |
| 1527 | // in BulkFree. And the latest thread local free list will be merged to free list |
| 1528 | // either when this thread local run is full or when revoking this run here. In this |
| 1529 | // case the free list wll be updated. If thread local run is false, GC thread will help |
| 1530 | // merge bulk free list in next BulkFree. |
| 1531 | // Thus no need to merge bulk free list to free list again here. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1532 | bool dont_care; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1533 | thread_local_run->MergeThreadLocalFreeListToFreeList(&dont_care); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1534 | thread_local_run->SetIsThreadLocal(false); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1535 | DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end()); |
| 1536 | DCHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end()); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1537 | RevokeRun(self, idx, thread_local_run); |
| 1538 | } |
| 1539 | } |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1540 | return free_bytes; |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | void RosAlloc::RevokeRun(Thread* self, size_t idx, Run* run) { |
| 1544 | size_bracket_locks_[idx]->AssertHeld(self); |
| 1545 | DCHECK(run != dedicated_full_run_); |
| 1546 | if (run->IsFull()) { |
| 1547 | if (kIsDebugBuild) { |
| 1548 | full_runs_[idx].insert(run); |
| 1549 | DCHECK(full_runs_[idx].find(run) != full_runs_[idx].end()); |
| 1550 | if (kTraceRosAlloc) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1551 | LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1552 | << reinterpret_cast<intptr_t>(run) |
| 1553 | << " into full_runs_[" << std::dec << idx << "]"; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1554 | } |
| 1555 | } |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1556 | } else if (run->IsAllFree()) { |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1557 | run->ZeroHeaderAndSlotHeaders(); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1558 | MutexLock mu(self, lock_); |
| 1559 | FreePages(self, run, true); |
| 1560 | } else { |
| 1561 | non_full_runs_[idx].insert(run); |
| 1562 | DCHECK(non_full_runs_[idx].find(run) != non_full_runs_[idx].end()); |
| 1563 | if (kTraceRosAlloc) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1564 | LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1565 | << reinterpret_cast<intptr_t>(run) |
| 1566 | << " into non_full_runs_[" << std::dec << idx << "]"; |
| 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | void RosAlloc::RevokeThreadUnsafeCurrentRuns() { |
| 1572 | // Revoke the current runs which share the same idx as thread local runs. |
| 1573 | Thread* self = Thread::Current(); |
| 1574 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; ++idx) { |
| 1575 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| 1576 | if (current_runs_[idx] != dedicated_full_run_) { |
| 1577 | RevokeRun(self, idx, current_runs_[idx]); |
| 1578 | current_runs_[idx] = dedicated_full_run_; |
| 1579 | } |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1583 | size_t RosAlloc::RevokeAllThreadLocalRuns() { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1584 | // This is called when a mutator thread won't allocate such as at |
| 1585 | // the Zygote creation time or during the GC pause. |
| Hiroshi Yamauchi | f5b0e20 | 2014-02-11 17:02:22 -0800 | [diff] [blame] | 1586 | MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_); |
| 1587 | MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1588 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1589 | size_t free_bytes = 0U; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1590 | for (Thread* thread : thread_list) { |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1591 | free_bytes += RevokeThreadLocalRuns(thread); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1592 | } |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1593 | RevokeThreadUnsafeCurrentRuns(); |
| Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 1594 | return free_bytes; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
| Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1597 | void RosAlloc::AssertThreadLocalRunsAreRevoked(Thread* thread) { |
| 1598 | if (kIsDebugBuild) { |
| 1599 | Thread* self = Thread::Current(); |
| 1600 | // Avoid race conditions on the bulk free bit maps with BulkFree() (GC). |
| Mathieu Chartier | a1c1c71 | 2014-06-23 17:53:09 -0700 | [diff] [blame] | 1601 | ReaderMutexLock wmu(self, bulk_free_lock_); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1602 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; idx++) { |
| Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1603 | MutexLock mu(self, *size_bracket_locks_[idx]); |
| Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1604 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(idx)); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1605 | DCHECK(thread_local_run == nullptr || thread_local_run == dedicated_full_run_); |
| Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | void RosAlloc::AssertAllThreadLocalRunsAreRevoked() { |
| 1611 | if (kIsDebugBuild) { |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1612 | Thread* self = Thread::Current(); |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1613 | MutexLock shutdown_mu(self, *Locks::runtime_shutdown_lock_); |
| 1614 | MutexLock thread_list_mu(self, *Locks::thread_list_lock_); |
| Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1615 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 1616 | for (Thread* t : thread_list) { |
| 1617 | AssertThreadLocalRunsAreRevoked(t); |
| 1618 | } |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1619 | for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; ++idx) { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1620 | MutexLock brackets_mu(self, *size_bracket_locks_[idx]); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1621 | CHECK_EQ(current_runs_[idx], dedicated_full_run_); |
| 1622 | } |
| Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1626 | void RosAlloc::Initialize() { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1627 | // bracketSizes. |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1628 | static_assert(kNumRegularSizeBrackets == kNumOfSizeBrackets - 2, |
| 1629 | "There should be two non-regular brackets"); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1630 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1631 | if (i < kNumThreadLocalSizeBrackets) { |
| 1632 | bracketSizes[i] = kThreadLocalBracketQuantumSize * (i + 1); |
| 1633 | } else if (i < kNumRegularSizeBrackets) { |
| 1634 | bracketSizes[i] = kBracketQuantumSize * (i - kNumThreadLocalSizeBrackets + 1) + |
| 1635 | (kThreadLocalBracketQuantumSize * kNumThreadLocalSizeBrackets); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1636 | } else if (i == kNumOfSizeBrackets - 2) { |
| 1637 | bracketSizes[i] = 1 * KB; |
| 1638 | } else { |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1639 | DCHECK_EQ(i, kNumOfSizeBrackets - 1); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1640 | bracketSizes[i] = 2 * KB; |
| 1641 | } |
| 1642 | if (kTraceRosAlloc) { |
| 1643 | LOG(INFO) << "bracketSizes[" << i << "]=" << bracketSizes[i]; |
| 1644 | } |
| 1645 | } |
| 1646 | // numOfPages. |
| 1647 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1648 | if (i < kNumThreadLocalSizeBrackets) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1649 | numOfPages[i] = 1; |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1650 | } else if (i < (kNumThreadLocalSizeBrackets + kNumRegularSizeBrackets) / 2) { |
| Hiroshi Yamauchi | fc067bf | 2016-03-23 14:22:34 -0700 | [diff] [blame] | 1651 | numOfPages[i] = 1; |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1652 | } else if (i < kNumRegularSizeBrackets) { |
| Hiroshi Yamauchi | fc067bf | 2016-03-23 14:22:34 -0700 | [diff] [blame] | 1653 | numOfPages[i] = 1; |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1654 | } else if (i == kNumOfSizeBrackets - 2) { |
| Hiroshi Yamauchi | fc067bf | 2016-03-23 14:22:34 -0700 | [diff] [blame] | 1655 | numOfPages[i] = 2; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1656 | } else { |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1657 | DCHECK_EQ(i, kNumOfSizeBrackets - 1); |
| Hiroshi Yamauchi | fc067bf | 2016-03-23 14:22:34 -0700 | [diff] [blame] | 1658 | numOfPages[i] = 4; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1659 | } |
| 1660 | if (kTraceRosAlloc) { |
| 1661 | LOG(INFO) << "numOfPages[" << i << "]=" << numOfPages[i]; |
| 1662 | } |
| 1663 | } |
| 1664 | // Compute numOfSlots and slotOffsets. |
| 1665 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 1666 | size_t bracket_size = bracketSizes[i]; |
| 1667 | size_t run_size = kPageSize * numOfPages[i]; |
| 1668 | size_t max_num_of_slots = run_size / bracket_size; |
| 1669 | // Compute the actual number of slots by taking the header and |
| 1670 | // alignment into account. |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1671 | size_t fixed_header_size = RoundUp(Run::fixed_header_size(), sizeof(uint64_t)); |
| 1672 | DCHECK_EQ(fixed_header_size, 80U); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1673 | size_t header_size = 0; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1674 | size_t num_of_slots = 0; |
| 1675 | // Search for the maximum number of slots that allows enough space |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1676 | // for the header. |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1677 | for (int s = max_num_of_slots; s >= 0; s--) { |
| 1678 | size_t tmp_slots_size = bracket_size * s; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1679 | size_t tmp_unaligned_header_size = fixed_header_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1680 | // Align up the unaligned header size. bracket_size may not be a power of two. |
| 1681 | size_t tmp_header_size = (tmp_unaligned_header_size % bracket_size == 0) ? |
| 1682 | tmp_unaligned_header_size : |
| 1683 | tmp_unaligned_header_size + (bracket_size - tmp_unaligned_header_size % bracket_size); |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1684 | DCHECK_EQ(tmp_header_size % bracket_size, 0U); |
| 1685 | DCHECK_EQ(tmp_header_size % sizeof(uint64_t), 0U); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1686 | if (tmp_slots_size + tmp_header_size <= run_size) { |
| 1687 | // Found the right number of slots, that is, there was enough |
| 1688 | // space for the header (including the bit maps.) |
| 1689 | num_of_slots = s; |
| 1690 | header_size = tmp_header_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1691 | break; |
| 1692 | } |
| 1693 | } |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1694 | DCHECK_GT(num_of_slots, 0U) << i; |
| 1695 | DCHECK_GT(header_size, 0U) << i; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1696 | // Add the padding for the alignment remainder. |
| 1697 | header_size += run_size % bracket_size; |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1698 | DCHECK_EQ(header_size + num_of_slots * bracket_size, run_size); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1699 | numOfSlots[i] = num_of_slots; |
| 1700 | headerSizes[i] = header_size; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1701 | if (kTraceRosAlloc) { |
| 1702 | LOG(INFO) << "numOfSlots[" << i << "]=" << numOfSlots[i] |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1703 | << ", headerSizes[" << i << "]=" << headerSizes[i]; |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1704 | } |
| 1705 | } |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1706 | // Set up the dedicated full run so that nobody can successfully allocate from it. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1707 | if (kIsDebugBuild) { |
| 1708 | dedicated_full_run_->magic_num_ = kMagicNum; |
| 1709 | } |
| 1710 | // It doesn't matter which size bracket we use since the main goal is to have the allocation |
| 1711 | // fail 100% of the time you attempt to allocate into the dedicated full run. |
| 1712 | dedicated_full_run_->size_bracket_idx_ = 0; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1713 | DCHECK_EQ(dedicated_full_run_->FreeList()->Size(), 0U); // It looks full. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1714 | dedicated_full_run_->SetIsThreadLocal(true); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1715 | |
| 1716 | // The smallest bracket size must be at least as large as the sizeof(Slot). |
| 1717 | DCHECK_LE(sizeof(Slot), bracketSizes[0]) << "sizeof(Slot) <= the smallest bracket size"; |
| Hiroshi Yamauchi | 7ed9c56 | 2016-02-02 15:22:09 -0800 | [diff] [blame] | 1718 | // Check the invariants between the max bracket sizes and the number of brackets. |
| 1719 | DCHECK_EQ(kMaxThreadLocalBracketSize, bracketSizes[kNumThreadLocalSizeBrackets - 1]); |
| 1720 | DCHECK_EQ(kMaxRegularBracketSize, bracketSizes[kNumRegularSizeBrackets - 1]); |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
| Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1723 | void RosAlloc::BytesAllocatedCallback(void* start ATTRIBUTE_UNUSED, void* end ATTRIBUTE_UNUSED, |
| 1724 | size_t used_bytes, void* arg) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1725 | if (used_bytes == 0) { |
| 1726 | return; |
| 1727 | } |
| 1728 | size_t* bytes_allocated = reinterpret_cast<size_t*>(arg); |
| 1729 | *bytes_allocated += used_bytes; |
| 1730 | } |
| 1731 | |
| Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1732 | void RosAlloc::ObjectsAllocatedCallback(void* start ATTRIBUTE_UNUSED, void* end ATTRIBUTE_UNUSED, |
| 1733 | size_t used_bytes, void* arg) { |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1734 | if (used_bytes == 0) { |
| 1735 | return; |
| 1736 | } |
| 1737 | size_t* objects_allocated = reinterpret_cast<size_t*>(arg); |
| 1738 | ++(*objects_allocated); |
| 1739 | } |
| 1740 | |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1741 | void RosAlloc::Verify() { |
| 1742 | Thread* self = Thread::Current(); |
| 1743 | CHECK(Locks::mutator_lock_->IsExclusiveHeld(self)) |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1744 | << "The mutator locks isn't exclusively locked at " << __PRETTY_FUNCTION__; |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1745 | MutexLock thread_list_mu(self, *Locks::thread_list_lock_); |
| Mathieu Chartier | a1c1c71 | 2014-06-23 17:53:09 -0700 | [diff] [blame] | 1746 | ReaderMutexLock wmu(self, bulk_free_lock_); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1747 | std::vector<Run*> runs; |
| 1748 | { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1749 | MutexLock lock_mu(self, lock_); |
| Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 1750 | size_t pm_end = page_map_size_; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1751 | size_t i = 0; |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1752 | size_t memory_tool_modifier = is_running_on_memory_tool_ ? |
| 1753 | 2 * ::art::gc::space::kDefaultMemoryToolRedZoneBytes : // Redzones before and after. |
| Andreas Gampe | fef16ad | 2015-02-19 16:44:32 -0800 | [diff] [blame] | 1754 | 0; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1755 | while (i < pm_end) { |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1756 | uint8_t pm = page_map_[i]; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1757 | switch (pm) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1758 | case kPageMapReleased: |
| 1759 | // Fall-through. |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1760 | case kPageMapEmpty: { |
| 1761 | // The start of a free page run. |
| 1762 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1763 | DCHECK_EQ(fpr->magic_num_, kMagicNumFree); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1764 | CHECK(free_page_runs_.find(fpr) != free_page_runs_.end()) |
| 1765 | << "An empty page must belong to the free page run set"; |
| 1766 | size_t fpr_size = fpr->ByteSize(this); |
| Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 1767 | CHECK_ALIGNED(fpr_size, kPageSize) |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1768 | << "A free page run size isn't page-aligned : " << fpr_size; |
| 1769 | size_t num_pages = fpr_size / kPageSize; |
| 1770 | CHECK_GT(num_pages, static_cast<uintptr_t>(0)) |
| 1771 | << "A free page run size must be > 0 : " << fpr_size; |
| 1772 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1773 | CHECK(IsFreePage(j)) |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1774 | << "A mismatch between the page map table for kPageMapEmpty " |
| 1775 | << " at page index " << j |
| 1776 | << " and the free page run size : page index range : " |
| 1777 | << i << " to " << (i + num_pages) << std::endl << DumpPageMap(); |
| 1778 | } |
| 1779 | i += num_pages; |
| 1780 | CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end |
| 1781 | << std::endl << DumpPageMap(); |
| 1782 | break; |
| 1783 | } |
| 1784 | case kPageMapLargeObject: { |
| 1785 | // The start of a large object. |
| 1786 | size_t num_pages = 1; |
| 1787 | size_t idx = i + 1; |
| 1788 | while (idx < pm_end && page_map_[idx] == kPageMapLargeObjectPart) { |
| 1789 | num_pages++; |
| 1790 | idx++; |
| 1791 | } |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 1792 | uint8_t* start = base_ + i * kPageSize; |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1793 | if (is_running_on_memory_tool_) { |
| 1794 | start += ::art::gc::space::kDefaultMemoryToolRedZoneBytes; |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 1795 | } |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1796 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(start); |
| 1797 | size_t obj_size = obj->SizeOf(); |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1798 | CHECK_GT(obj_size + memory_tool_modifier, kLargeSizeThreshold) |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1799 | << "A rosalloc large object size must be > " << kLargeSizeThreshold; |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1800 | CHECK_EQ(num_pages, RoundUp(obj_size + memory_tool_modifier, kPageSize) / kPageSize) |
| 1801 | << "A rosalloc large object size " << obj_size + memory_tool_modifier |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1802 | << " does not match the page map table " << (num_pages * kPageSize) |
| 1803 | << std::endl << DumpPageMap(); |
| 1804 | i += num_pages; |
| 1805 | CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end |
| 1806 | << std::endl << DumpPageMap(); |
| 1807 | break; |
| 1808 | } |
| 1809 | case kPageMapLargeObjectPart: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1810 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl << DumpPageMap(); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1811 | UNREACHABLE(); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1812 | case kPageMapRun: { |
| 1813 | // The start of a run. |
| 1814 | Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize); |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1815 | DCHECK_EQ(run->magic_num_, kMagicNum); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1816 | size_t idx = run->size_bracket_idx_; |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1817 | CHECK_LT(idx, kNumOfSizeBrackets) << "Out of range size bracket index : " << idx; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1818 | size_t num_pages = numOfPages[idx]; |
| 1819 | CHECK_GT(num_pages, static_cast<uintptr_t>(0)) |
| 1820 | << "Run size must be > 0 : " << num_pages; |
| 1821 | for (size_t j = i + 1; j < i + num_pages; ++j) { |
| 1822 | CHECK_EQ(page_map_[j], kPageMapRunPart) |
| 1823 | << "A mismatch between the page map table for kPageMapRunPart " |
| 1824 | << " at page index " << j |
| 1825 | << " and the run size : page index range " << i << " to " << (i + num_pages) |
| 1826 | << std::endl << DumpPageMap(); |
| 1827 | } |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1828 | // Don't verify the dedicated_full_run_ since it doesn't have any real allocations. |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1829 | runs.push_back(run); |
| 1830 | i += num_pages; |
| 1831 | CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end |
| 1832 | << std::endl << DumpPageMap(); |
| 1833 | break; |
| 1834 | } |
| 1835 | case kPageMapRunPart: |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1836 | // Fall-through. |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1837 | default: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 1838 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl << DumpPageMap(); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1839 | UNREACHABLE(); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | } |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1843 | std::list<Thread*> threads = Runtime::Current()->GetThreadList()->GetList(); |
| 1844 | for (Thread* thread : threads) { |
| 1845 | for (size_t i = 0; i < kNumThreadLocalSizeBrackets; ++i) { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1846 | MutexLock brackets_mu(self, *size_bracket_locks_[i]); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1847 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(i)); |
| 1848 | CHECK(thread_local_run != nullptr); |
| 1849 | CHECK(thread_local_run->IsThreadLocal()); |
| 1850 | CHECK(thread_local_run == dedicated_full_run_ || |
| 1851 | thread_local_run->size_bracket_idx_ == i); |
| 1852 | } |
| 1853 | } |
| 1854 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1855 | MutexLock brackets_mu(self, *size_bracket_locks_[i]); |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1856 | Run* current_run = current_runs_[i]; |
| 1857 | CHECK(current_run != nullptr); |
| 1858 | if (current_run != dedicated_full_run_) { |
| 1859 | // The dedicated full run is currently marked as thread local. |
| 1860 | CHECK(!current_run->IsThreadLocal()); |
| 1861 | CHECK_EQ(current_run->size_bracket_idx_, i); |
| 1862 | } |
| 1863 | } |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1864 | // Call Verify() here for the lock order. |
| 1865 | for (auto& run : runs) { |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1866 | run->Verify(self, this, is_running_on_memory_tool_); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1870 | void RosAlloc::Run::Verify(Thread* self, RosAlloc* rosalloc, bool running_on_memory_tool) { |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1871 | DCHECK_EQ(magic_num_, kMagicNum) << "Bad magic number : " << Dump(); |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1872 | const size_t idx = size_bracket_idx_; |
| Ian Rogers | 5d05705 | 2014-03-12 14:32:27 -0700 | [diff] [blame] | 1873 | CHECK_LT(idx, kNumOfSizeBrackets) << "Out of range size bracket index : " << Dump(); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1874 | uint8_t* slot_base = reinterpret_cast<uint8_t*>(this) + headerSizes[idx]; |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1875 | const size_t num_slots = numOfSlots[idx]; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1876 | size_t bracket_size = IndexToBracketSize(idx); |
| 1877 | CHECK_EQ(slot_base + num_slots * bracket_size, |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1878 | reinterpret_cast<uint8_t*>(this) + numOfPages[idx] * kPageSize) |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1879 | << "Mismatch in the end address of the run " << Dump(); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1880 | // Check that the bulk free list is empty. It's only used during BulkFree(). |
| 1881 | CHECK(IsBulkFreeListEmpty()) << "The bulk free isn't empty " << Dump(); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1882 | // Check the thread local runs, the current runs, and the run sets. |
| Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 1883 | if (IsThreadLocal()) { |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1884 | // If it's a thread local run, then it must be pointed to by an owner thread. |
| 1885 | bool owner_found = false; |
| 1886 | std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList(); |
| 1887 | for (auto it = thread_list.begin(); it != thread_list.end(); ++it) { |
| 1888 | Thread* thread = *it; |
| Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 1889 | for (size_t i = 0; i < kNumThreadLocalSizeBrackets; i++) { |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1890 | MutexLock mu(self, *rosalloc->size_bracket_locks_[i]); |
| Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1891 | Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(i)); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1892 | if (thread_local_run == this) { |
| 1893 | CHECK(!owner_found) |
| 1894 | << "A thread local run has more than one owner thread " << Dump(); |
| 1895 | CHECK_EQ(i, idx) |
| 1896 | << "A mismatching size bracket index in a thread local run " << Dump(); |
| 1897 | owner_found = true; |
| 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | CHECK(owner_found) << "A thread local run has no owner thread " << Dump(); |
| 1902 | } else { |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1903 | // If it's not thread local, check that the thread local free list is empty. |
| 1904 | CHECK(IsThreadLocalFreeListEmpty()) |
| 1905 | << "A non-thread-local run's thread local free list isn't empty " |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1906 | << Dump(); |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1907 | // Check if it's a current run for the size bracket. |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1908 | bool is_current_run = false; |
| 1909 | for (size_t i = 0; i < kNumOfSizeBrackets; i++) { |
| 1910 | MutexLock mu(self, *rosalloc->size_bracket_locks_[i]); |
| 1911 | Run* current_run = rosalloc->current_runs_[i]; |
| 1912 | if (idx == i) { |
| 1913 | if (this == current_run) { |
| 1914 | is_current_run = true; |
| 1915 | } |
| 1916 | } else { |
| 1917 | // If the size bucket index does not match, then it must not |
| 1918 | // be a current run. |
| 1919 | CHECK_NE(this, current_run) |
| 1920 | << "A current run points to a run with a wrong size bracket index " << Dump(); |
| 1921 | } |
| 1922 | } |
| 1923 | // If it's neither a thread local or current run, then it must be |
| 1924 | // in a run set. |
| 1925 | if (!is_current_run) { |
| 1926 | MutexLock mu(self, rosalloc->lock_); |
| Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 1927 | auto& non_full_runs = rosalloc->non_full_runs_[idx]; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1928 | // If it's all free, it must be a free page run rather than a run. |
| 1929 | CHECK(!IsAllFree()) << "A free run must be in a free page run set " << Dump(); |
| 1930 | if (!IsFull()) { |
| 1931 | // If it's not full, it must in the non-full run set. |
| 1932 | CHECK(non_full_runs.find(this) != non_full_runs.end()) |
| 1933 | << "A non-full run isn't in the non-full run set " << Dump(); |
| 1934 | } else { |
| 1935 | // If it's full, it must in the full run set (debug build only.) |
| 1936 | if (kIsDebugBuild) { |
| Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 1937 | auto& full_runs = rosalloc->full_runs_[idx]; |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1938 | CHECK(full_runs.find(this) != full_runs.end()) |
| 1939 | << " A full run isn't in the full run set " << Dump(); |
| 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | // Check each slot. |
| Evgenii Stepanov | 1e13374 | 2015-05-20 12:30:59 -0700 | [diff] [blame] | 1945 | size_t memory_tool_modifier = running_on_memory_tool ? |
| 1946 | 2 * ::art::gc::space::kDefaultMemoryToolRedZoneBytes : |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 1947 | 0U; |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1948 | // TODO: reuse InspectAllSlots(). |
| 1949 | std::unique_ptr<bool[]> is_free(new bool[num_slots]()); // zero initialized |
| 1950 | // Mark the free slots and the remaining ones are allocated. |
| 1951 | for (Slot* slot = free_list_.Head(); slot != nullptr; slot = slot->Next()) { |
| 1952 | size_t slot_idx = SlotIndex(slot); |
| 1953 | DCHECK_LT(slot_idx, num_slots); |
| 1954 | is_free[slot_idx] = true; |
| 1955 | } |
| 1956 | if (IsThreadLocal()) { |
| 1957 | for (Slot* slot = thread_local_free_list_.Head(); slot != nullptr; slot = slot->Next()) { |
| 1958 | size_t slot_idx = SlotIndex(slot); |
| 1959 | DCHECK_LT(slot_idx, num_slots); |
| 1960 | is_free[slot_idx] = true; |
| 1961 | } |
| 1962 | } |
| 1963 | for (size_t slot_idx = 0; slot_idx < num_slots; ++slot_idx) { |
| 1964 | uint8_t* slot_addr = slot_base + slot_idx * bracket_size; |
| 1965 | if (running_on_memory_tool) { |
| 1966 | slot_addr += ::art::gc::space::kDefaultMemoryToolRedZoneBytes; |
| 1967 | } |
| 1968 | if (!is_free[slot_idx]) { |
| 1969 | // The slot is allocated |
| 1970 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(slot_addr); |
| 1971 | size_t obj_size = obj->SizeOf(); |
| 1972 | CHECK_LE(obj_size + memory_tool_modifier, kLargeSizeThreshold) |
| 1973 | << "A run slot contains a large object " << Dump(); |
| 1974 | CHECK_EQ(SizeToIndex(obj_size + memory_tool_modifier), idx) |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1975 | << obj->PrettyTypeOf() << " " |
| Hiroshi Yamauchi | 31bf42c | 2015-09-24 11:20:29 -0700 | [diff] [blame] | 1976 | << "obj_size=" << obj_size << "(" << obj_size + memory_tool_modifier << "), idx=" << idx |
| 1977 | << " A run slot contains an object with wrong size " << Dump(); |
| Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 1982 | size_t RosAlloc::ReleasePages() { |
| 1983 | VLOG(heap) << "RosAlloc::ReleasePages()"; |
| 1984 | DCHECK(!DoesReleaseAllPages()); |
| 1985 | Thread* self = Thread::Current(); |
| 1986 | size_t reclaimed_bytes = 0; |
| 1987 | size_t i = 0; |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1988 | // Check the page map size which might have changed due to grow/shrink. |
| 1989 | while (i < page_map_size_) { |
| 1990 | // Reading the page map without a lock is racy but the race is benign since it should only |
| 1991 | // result in occasionally not releasing pages which we could release. |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1992 | uint8_t pm = page_map_[i]; |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 1993 | switch (pm) { |
| Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 1994 | case kPageMapReleased: |
| 1995 | // Fall through. |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 1996 | case kPageMapEmpty: { |
| Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 1997 | // This is currently the start of a free page run. |
| 1998 | // Acquire the lock to prevent other threads racing in and modifying the page map. |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 1999 | MutexLock mu(self, lock_); |
| 2000 | // Check that it's still empty after we acquired the lock since another thread could have |
| 2001 | // raced in and placed an allocation here. |
| Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2002 | if (IsFreePage(i)) { |
| 2003 | // Free page runs can start with a released page if we coalesced a released page free |
| 2004 | // page run with an empty page run. |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 2005 | FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize); |
| Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2006 | // There is a race condition where FreePage can coalesce fpr with the previous |
| 2007 | // free page run before we acquire lock_. In that case free_page_runs_.find will not find |
| 2008 | // a run starting at fpr. To handle this race, we skip reclaiming the page range and go |
| 2009 | // to the next page. |
| 2010 | if (free_page_runs_.find(fpr) != free_page_runs_.end()) { |
| 2011 | size_t fpr_size = fpr->ByteSize(this); |
| Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 2012 | DCHECK_ALIGNED(fpr_size, kPageSize); |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2013 | uint8_t* start = reinterpret_cast<uint8_t*>(fpr); |
| Mathieu Chartier | e28ed99 | 2014-07-10 10:16:44 -0700 | [diff] [blame] | 2014 | reclaimed_bytes += ReleasePageRange(start, start + fpr_size); |
| 2015 | size_t pages = fpr_size / kPageSize; |
| 2016 | CHECK_GT(pages, 0U) << "Infinite loop probable"; |
| 2017 | i += pages; |
| 2018 | DCHECK_LE(i, page_map_size_); |
| 2019 | break; |
| 2020 | } |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2021 | } |
| Ian Rogers | fc787ec | 2014-10-09 21:56:44 -0700 | [diff] [blame] | 2022 | FALLTHROUGH_INTENDED; |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2023 | } |
| 2024 | case kPageMapLargeObject: // Fall through. |
| 2025 | case kPageMapLargeObjectPart: // Fall through. |
| 2026 | case kPageMapRun: // Fall through. |
| 2027 | case kPageMapRunPart: // Fall through. |
| 2028 | ++i; |
| 2029 | break; // Skip. |
| 2030 | default: |
| Maxim Kazantsev | 2fdeecb | 2014-10-16 10:55:47 +0700 | [diff] [blame] | 2031 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 2032 | UNREACHABLE(); |
| Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | return reclaimed_bytes; |
| 2036 | } |
| 2037 | |
| Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2038 | size_t RosAlloc::ReleasePageRange(uint8_t* start, uint8_t* end) { |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 2039 | DCHECK_ALIGNED(start, kPageSize); |
| 2040 | DCHECK_ALIGNED(end, kPageSize); |
| 2041 | DCHECK_LT(start, end); |
| 2042 | if (kIsDebugBuild) { |
| 2043 | // In the debug build, the first page of a free page run |
| 2044 | // contains a magic number for debugging. Exclude it. |
| 2045 | start += kPageSize; |
| Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 2046 | |
| 2047 | // Single pages won't be released. |
| 2048 | if (start == end) { |
| 2049 | return 0; |
| 2050 | } |
| Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 2051 | } |
| 2052 | if (!kMadviseZeroes) { |
| 2053 | // TODO: Do this when we resurrect the page instead. |
| 2054 | memset(start, 0, end - start); |
| 2055 | } |
| 2056 | CHECK_EQ(madvise(start, end - start, MADV_DONTNEED), 0); |
| 2057 | size_t pm_idx = ToPageMapIndex(start); |
| 2058 | size_t reclaimed_bytes = 0; |
| 2059 | // Calculate reclaimed bytes and upate page map. |
| 2060 | const size_t max_idx = pm_idx + (end - start) / kPageSize; |
| 2061 | for (; pm_idx < max_idx; ++pm_idx) { |
| 2062 | DCHECK(IsFreePage(pm_idx)); |
| 2063 | if (page_map_[pm_idx] == kPageMapEmpty) { |
| 2064 | // Mark the page as released and update how many bytes we released. |
| 2065 | reclaimed_bytes += kPageSize; |
| 2066 | page_map_[pm_idx] = kPageMapReleased; |
| 2067 | } |
| 2068 | } |
| 2069 | return reclaimed_bytes; |
| 2070 | } |
| 2071 | |
| Lokesh Gidra | 26e9e75 | 2021-05-18 04:16:06 -0700 | [diff] [blame] | 2072 | bool RosAlloc::LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) { |
| Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2073 | Thread* self = Thread::Current(); |
| 2074 | size_t largest_continuous_free_pages = 0; |
| 2075 | WriterMutexLock wmu(self, bulk_free_lock_); |
| 2076 | MutexLock mu(self, lock_); |
| Mathieu Chartier | a9033d7 | 2016-12-01 17:41:17 -0800 | [diff] [blame] | 2077 | uint64_t total_free = 0; |
| Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2078 | for (FreePageRun* fpr : free_page_runs_) { |
| 2079 | largest_continuous_free_pages = std::max(largest_continuous_free_pages, |
| 2080 | fpr->ByteSize(this)); |
| Mathieu Chartier | a9033d7 | 2016-12-01 17:41:17 -0800 | [diff] [blame] | 2081 | total_free += fpr->ByteSize(this); |
| Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2082 | } |
| Mathieu Chartier | a9033d7 | 2016-12-01 17:41:17 -0800 | [diff] [blame] | 2083 | size_t required_bytes = 0; |
| 2084 | const char* new_buffer_msg = ""; |
| Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2085 | if (failed_alloc_bytes > kLargeSizeThreshold) { |
| 2086 | // Large allocation. |
| Mathieu Chartier | a9033d7 | 2016-12-01 17:41:17 -0800 | [diff] [blame] | 2087 | required_bytes = RoundUp(failed_alloc_bytes, kPageSize); |
| Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2088 | } else { |
| 2089 | // Non-large allocation. |
| Mathieu Chartier | a9033d7 | 2016-12-01 17:41:17 -0800 | [diff] [blame] | 2090 | required_bytes = numOfPages[SizeToIndex(failed_alloc_bytes)] * kPageSize; |
| 2091 | new_buffer_msg = " for a new buffer"; |
| 2092 | } |
| 2093 | if (required_bytes > largest_continuous_free_pages) { |
| 2094 | os << "; failed due to fragmentation (" |
| 2095 | << "required contiguous free " << required_bytes << " bytes" << new_buffer_msg |
| 2096 | << ", largest contiguous free " << largest_continuous_free_pages << " bytes" |
| 2097 | << ", total free pages " << total_free << " bytes" |
| 2098 | << ", space footprint " << footprint_ << " bytes" |
| 2099 | << ", space max capacity " << max_capacity_ << " bytes" |
| 2100 | << ")" << std::endl; |
| Lokesh Gidra | 26e9e75 | 2021-05-18 04:16:06 -0700 | [diff] [blame] | 2101 | return true; |
| Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2102 | } |
| Lokesh Gidra | 26e9e75 | 2021-05-18 04:16:06 -0700 | [diff] [blame] | 2103 | return false; |
| Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
| Hiroshi Yamauchi | b62f2e6 | 2016-03-23 15:51:24 -0700 | [diff] [blame] | 2106 | void RosAlloc::DumpStats(std::ostream& os) { |
| 2107 | Thread* self = Thread::Current(); |
| 2108 | CHECK(Locks::mutator_lock_->IsExclusiveHeld(self)) |
| 2109 | << "The mutator locks isn't exclusively locked at " << __PRETTY_FUNCTION__; |
| 2110 | size_t num_large_objects = 0; |
| 2111 | size_t num_pages_large_objects = 0; |
| 2112 | // These arrays are zero initialized. |
| 2113 | std::unique_ptr<size_t[]> num_runs(new size_t[kNumOfSizeBrackets]()); |
| 2114 | std::unique_ptr<size_t[]> num_pages_runs(new size_t[kNumOfSizeBrackets]()); |
| 2115 | std::unique_ptr<size_t[]> num_slots(new size_t[kNumOfSizeBrackets]()); |
| 2116 | std::unique_ptr<size_t[]> num_used_slots(new size_t[kNumOfSizeBrackets]()); |
| 2117 | std::unique_ptr<size_t[]> num_metadata_bytes(new size_t[kNumOfSizeBrackets]()); |
| 2118 | ReaderMutexLock rmu(self, bulk_free_lock_); |
| 2119 | MutexLock lock_mu(self, lock_); |
| 2120 | for (size_t i = 0; i < page_map_size_; ) { |
| 2121 | uint8_t pm = page_map_[i]; |
| 2122 | switch (pm) { |
| 2123 | case kPageMapReleased: |
| 2124 | case kPageMapEmpty: |
| 2125 | ++i; |
| 2126 | break; |
| 2127 | case kPageMapLargeObject: { |
| 2128 | size_t num_pages = 1; |
| 2129 | size_t idx = i + 1; |
| 2130 | while (idx < page_map_size_ && page_map_[idx] == kPageMapLargeObjectPart) { |
| 2131 | num_pages++; |
| 2132 | idx++; |
| 2133 | } |
| 2134 | num_large_objects++; |
| 2135 | num_pages_large_objects += num_pages; |
| 2136 | i += num_pages; |
| 2137 | break; |
| 2138 | } |
| 2139 | case kPageMapLargeObjectPart: |
| 2140 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl |
| 2141 | << DumpPageMap(); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 2142 | UNREACHABLE(); |
| Hiroshi Yamauchi | b62f2e6 | 2016-03-23 15:51:24 -0700 | [diff] [blame] | 2143 | case kPageMapRun: { |
| 2144 | Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize); |
| 2145 | size_t idx = run->size_bracket_idx_; |
| 2146 | size_t num_pages = numOfPages[idx]; |
| 2147 | num_runs[idx]++; |
| 2148 | num_pages_runs[idx] += num_pages; |
| 2149 | num_slots[idx] += numOfSlots[idx]; |
| 2150 | size_t num_free_slots = run->NumberOfFreeSlots(); |
| 2151 | num_used_slots[idx] += numOfSlots[idx] - num_free_slots; |
| 2152 | num_metadata_bytes[idx] += headerSizes[idx]; |
| 2153 | i += num_pages; |
| 2154 | break; |
| 2155 | } |
| 2156 | case kPageMapRunPart: |
| 2157 | // Fall-through. |
| 2158 | default: |
| 2159 | LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl |
| 2160 | << DumpPageMap(); |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 2161 | UNREACHABLE(); |
| Hiroshi Yamauchi | b62f2e6 | 2016-03-23 15:51:24 -0700 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | os << "RosAlloc stats:\n"; |
| 2165 | for (size_t i = 0; i < kNumOfSizeBrackets; ++i) { |
| 2166 | os << "Bracket " << i << " (" << bracketSizes[i] << "):" |
| 2167 | << " #runs=" << num_runs[i] |
| 2168 | << " #pages=" << num_pages_runs[i] |
| 2169 | << " (" << PrettySize(num_pages_runs[i] * kPageSize) << ")" |
| 2170 | << " #metadata_bytes=" << PrettySize(num_metadata_bytes[i]) |
| 2171 | << " #slots=" << num_slots[i] << " (" << PrettySize(num_slots[i] * bracketSizes[i]) << ")" |
| 2172 | << " #used_slots=" << num_used_slots[i] |
| 2173 | << " (" << PrettySize(num_used_slots[i] * bracketSizes[i]) << ")\n"; |
| 2174 | } |
| 2175 | os << "Large #allocations=" << num_large_objects |
| 2176 | << " #pages=" << num_pages_large_objects |
| 2177 | << " (" << PrettySize(num_pages_large_objects * kPageSize) << ")\n"; |
| 2178 | size_t total_num_pages = 0; |
| 2179 | size_t total_metadata_bytes = 0; |
| 2180 | size_t total_allocated_bytes = 0; |
| 2181 | for (size_t i = 0; i < kNumOfSizeBrackets; ++i) { |
| 2182 | total_num_pages += num_pages_runs[i]; |
| 2183 | total_metadata_bytes += num_metadata_bytes[i]; |
| 2184 | total_allocated_bytes += num_used_slots[i] * bracketSizes[i]; |
| 2185 | } |
| 2186 | total_num_pages += num_pages_large_objects; |
| 2187 | total_allocated_bytes += num_pages_large_objects * kPageSize; |
| 2188 | os << "Total #total_bytes=" << PrettySize(total_num_pages * kPageSize) |
| 2189 | << " #metadata_bytes=" << PrettySize(total_metadata_bytes) |
| 2190 | << " #used_bytes=" << PrettySize(total_allocated_bytes) << "\n"; |
| 2191 | os << "\n"; |
| 2192 | } |
| 2193 | |
| Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 2194 | } // namespace allocator |
| 2195 | } // namespace gc |
| 2196 | } // namespace art |