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