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