blob: 8b125dd1674b4a363326f6d8a428fd4701da5f52 [file] [log] [blame]
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001/*
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 Gamped7576322014-10-24 22:13:45 -070017#include "rosalloc.h"
18
Evgenii Stepanov1e133742015-05-20 12:30:59 -070019#include "base/memory_tool.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070020#include "base/mutex-inl.h"
Evgenii Stepanov1e133742015-05-20 12:30:59 -070021#include "gc/space/memory_tool_settings.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "mem_map.h"
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -080023#include "mirror/class-inl.h"
24#include "mirror/object.h"
25#include "mirror/object-inl.h"
Brian Carlstrom218daa22013-11-25 14:51:44 -080026#include "thread-inl.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070027#include "thread_list.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070028
29#include <map>
30#include <list>
Ian Rogersc7dd2952014-10-21 23:31:19 -070031#include <sstream>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070032#include <vector>
33
34namespace art {
35namespace gc {
36namespace allocator {
37
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -070038static constexpr bool kUsePrefetchDuringAllocRun = false;
Mathieu Chartier73d1e172014-04-11 17:53:48 -070039static constexpr bool kPrefetchNewRunDataByZeroing = false;
40static constexpr size_t kPrefetchStride = 64;
41
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070042size_t RosAlloc::bracketSizes[kNumOfSizeBrackets];
43size_t RosAlloc::numOfPages[kNumOfSizeBrackets];
44size_t RosAlloc::numOfSlots[kNumOfSizeBrackets];
45size_t RosAlloc::headerSizes[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070046bool RosAlloc::initialized_ = false;
Mathieu Chartier73d1e172014-04-11 17:53:48 -070047size_t RosAlloc::dedicated_full_run_storage_[kPageSize / sizeof(size_t)] = { 0 };
48RosAlloc::Run* RosAlloc::dedicated_full_run_ =
49 reinterpret_cast<RosAlloc::Run*>(dedicated_full_run_storage_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070050
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080051RosAlloc::RosAlloc(void* base, size_t capacity, size_t max_capacity,
Evgenii Stepanov1e133742015-05-20 12:30:59 -070052 PageReleaseMode page_release_mode, bool running_on_memory_tool,
Andreas Gamped7576322014-10-24 22:13:45 -070053 size_t page_release_size_threshold)
Ian Rogers13735952014-10-08 12:43:28 -070054 : base_(reinterpret_cast<uint8_t*>(base)), footprint_(capacity),
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080055 capacity_(capacity), max_capacity_(max_capacity),
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070056 lock_("rosalloc global lock", kRosAllocGlobalLock),
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080057 bulk_free_lock_("rosalloc bulk free lock", kRosAllocBulkFreeLock),
58 page_release_mode_(page_release_mode),
Andreas Gamped7576322014-10-24 22:13:45 -070059 page_release_size_threshold_(page_release_size_threshold),
Evgenii Stepanov1e133742015-05-20 12:30:59 -070060 is_running_on_memory_tool_(running_on_memory_tool) {
Ian Rogers5d057052014-03-12 14:32:27 -070061 DCHECK_EQ(RoundUp(capacity, kPageSize), capacity);
62 DCHECK_EQ(RoundUp(max_capacity, kPageSize), max_capacity);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080063 CHECK_LE(capacity, max_capacity);
Roland Levillain14d90572015-07-16 10:52:26 +010064 CHECK_ALIGNED(page_release_size_threshold_, kPageSize);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070065 if (!initialized_) {
66 Initialize();
67 }
68 VLOG(heap) << "RosAlloc base="
69 << std::hex << (intptr_t)base_ << ", end="
70 << std::hex << (intptr_t)(base_ + capacity_)
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080071 << ", capacity=" << std::dec << capacity_
72 << ", max_capacity=" << std::dec << max_capacity_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070073 for (size_t i = 0; i < kNumOfSizeBrackets; i++) {
Zuo Wangf37a88b2014-07-10 04:26:41 -070074 size_bracket_lock_names_[i] =
Mathieu Chartier73d1e172014-04-11 17:53:48 -070075 StringPrintf("an rosalloc size bracket %d lock", static_cast<int>(i));
Zuo Wangf37a88b2014-07-10 04:26:41 -070076 size_bracket_locks_[i] = new Mutex(size_bracket_lock_names_[i].c_str(), kRosAllocBracketLock);
Mathieu Chartier0651d412014-04-29 14:37:57 -070077 current_runs_[i] = dedicated_full_run_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070078 }
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080079 DCHECK_EQ(footprint_, capacity_);
80 size_t num_of_pages = footprint_ / kPageSize;
81 size_t max_num_of_pages = max_capacity_ / kPageSize;
82 std::string error_msg;
Vladimir Marko5c42c292015-02-25 12:02:49 +000083 page_map_mem_map_.reset(MemMap::MapAnonymous("rosalloc page map", nullptr,
84 RoundUp(max_num_of_pages, kPageSize),
85 PROT_READ | PROT_WRITE, false, false, &error_msg));
Mathieu Chartier73d1e172014-04-11 17:53:48 -070086 CHECK(page_map_mem_map_.get() != nullptr) << "Couldn't allocate the page map : " << error_msg;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080087 page_map_ = page_map_mem_map_->Begin();
88 page_map_size_ = num_of_pages;
89 max_page_map_size_ = max_num_of_pages;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070090 free_page_run_size_map_.resize(num_of_pages);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070091 FreePageRun* free_pages = reinterpret_cast<FreePageRun*>(base_);
92 if (kIsDebugBuild) {
93 free_pages->magic_num_ = kMagicNumFree;
94 }
95 free_pages->SetByteSize(this, capacity_);
96 DCHECK_EQ(capacity_ % kPageSize, static_cast<size_t>(0));
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080097 DCHECK(free_pages->IsFree());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070098 free_pages->ReleasePages(this);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080099 DCHECK(free_pages->IsFree());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700100 free_page_runs_.insert(free_pages);
101 if (kTraceRosAlloc) {
102 LOG(INFO) << "RosAlloc::RosAlloc() : Inserted run 0x" << std::hex
103 << reinterpret_cast<intptr_t>(free_pages)
104 << " into free_page_runs_";
105 }
106}
107
Mathieu Chartier661974a2014-01-09 11:23:53 -0800108RosAlloc::~RosAlloc() {
109 for (size_t i = 0; i < kNumOfSizeBrackets; i++) {
110 delete size_bracket_locks_[i];
111 }
Evgenii Stepanov1e133742015-05-20 12:30:59 -0700112 if (is_running_on_memory_tool_) {
113 MEMORY_TOOL_MAKE_DEFINED(base_, capacity_);
114 }
Mathieu Chartier661974a2014-01-09 11:23:53 -0800115}
116
Ian Rogers13735952014-10-08 12:43:28 -0700117void* RosAlloc::AllocPages(Thread* self, size_t num_pages, uint8_t page_map_type) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700118 lock_.AssertHeld(self);
119 DCHECK(page_map_type == kPageMapRun || page_map_type == kPageMapLargeObject);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700120 FreePageRun* res = nullptr;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700121 const size_t req_byte_size = num_pages * kPageSize;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700122 // Find the lowest address free page run that's large enough.
123 for (auto it = free_page_runs_.begin(); it != free_page_runs_.end(); ) {
124 FreePageRun* fpr = *it;
125 DCHECK(fpr->IsFree());
126 size_t fpr_byte_size = fpr->ByteSize(this);
127 DCHECK_EQ(fpr_byte_size % kPageSize, static_cast<size_t>(0));
128 if (req_byte_size <= fpr_byte_size) {
129 // Found one.
130 free_page_runs_.erase(it++);
131 if (kTraceRosAlloc) {
132 LOG(INFO) << "RosAlloc::AllocPages() : Erased run 0x"
133 << std::hex << reinterpret_cast<intptr_t>(fpr)
134 << " from free_page_runs_";
135 }
136 if (req_byte_size < fpr_byte_size) {
137 // Split.
Ian Rogers13735952014-10-08 12:43:28 -0700138 FreePageRun* remainder = reinterpret_cast<FreePageRun*>(reinterpret_cast<uint8_t*>(fpr) + req_byte_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700139 if (kIsDebugBuild) {
140 remainder->magic_num_ = kMagicNumFree;
141 }
142 remainder->SetByteSize(this, fpr_byte_size - req_byte_size);
143 DCHECK_EQ(remainder->ByteSize(this) % kPageSize, static_cast<size_t>(0));
144 // Don't need to call madvise on remainder here.
145 free_page_runs_.insert(remainder);
146 if (kTraceRosAlloc) {
147 LOG(INFO) << "RosAlloc::AllocPages() : Inserted run 0x" << std::hex
148 << reinterpret_cast<intptr_t>(remainder)
149 << " into free_page_runs_";
150 }
151 fpr->SetByteSize(this, req_byte_size);
152 DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0));
153 }
154 res = fpr;
155 break;
156 } else {
157 ++it;
158 }
159 }
160
161 // Failed to allocate pages. Grow the footprint, if possible.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700162 if (UNLIKELY(res == nullptr && capacity_ > footprint_)) {
163 FreePageRun* last_free_page_run = nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700164 size_t last_free_page_run_size;
165 auto it = free_page_runs_.rbegin();
166 if (it != free_page_runs_.rend() && (last_free_page_run = *it)->End(this) == base_ + footprint_) {
167 // There is a free page run at the end.
168 DCHECK(last_free_page_run->IsFree());
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700169 DCHECK(IsFreePage(ToPageMapIndex(last_free_page_run)));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700170 last_free_page_run_size = last_free_page_run->ByteSize(this);
171 } else {
172 // There is no free page run at the end.
173 last_free_page_run_size = 0;
174 }
175 DCHECK_LT(last_free_page_run_size, req_byte_size);
176 if (capacity_ - footprint_ + last_free_page_run_size >= req_byte_size) {
177 // If we grow the heap, we can allocate it.
178 size_t increment = std::min(std::max(2 * MB, req_byte_size - last_free_page_run_size),
179 capacity_ - footprint_);
180 DCHECK_EQ(increment % kPageSize, static_cast<size_t>(0));
181 size_t new_footprint = footprint_ + increment;
182 size_t new_num_of_pages = new_footprint / kPageSize;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800183 DCHECK_LT(page_map_size_, new_num_of_pages);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700184 DCHECK_LT(free_page_run_size_map_.size(), new_num_of_pages);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800185 page_map_size_ = new_num_of_pages;
186 DCHECK_LE(page_map_size_, max_page_map_size_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700187 free_page_run_size_map_.resize(new_num_of_pages);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800188 ArtRosAllocMoreCore(this, increment);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700189 if (last_free_page_run_size > 0) {
190 // There was a free page run at the end. Expand its size.
191 DCHECK_EQ(last_free_page_run_size, last_free_page_run->ByteSize(this));
192 last_free_page_run->SetByteSize(this, last_free_page_run_size + increment);
193 DCHECK_EQ(last_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0));
Ian Rogers5d057052014-03-12 14:32:27 -0700194 DCHECK_EQ(last_free_page_run->End(this), base_ + new_footprint);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700195 } else {
196 // Otherwise, insert a new free page run at the end.
197 FreePageRun* new_free_page_run = reinterpret_cast<FreePageRun*>(base_ + footprint_);
198 if (kIsDebugBuild) {
199 new_free_page_run->magic_num_ = kMagicNumFree;
200 }
201 new_free_page_run->SetByteSize(this, increment);
202 DCHECK_EQ(new_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0));
203 free_page_runs_.insert(new_free_page_run);
Ian Rogers5d057052014-03-12 14:32:27 -0700204 DCHECK_EQ(*free_page_runs_.rbegin(), new_free_page_run);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700205 if (kTraceRosAlloc) {
206 LOG(INFO) << "RosAlloc::AlloPages() : Grew the heap by inserting run 0x"
207 << std::hex << reinterpret_cast<intptr_t>(new_free_page_run)
208 << " into free_page_runs_";
209 }
210 }
211 DCHECK_LE(footprint_ + increment, capacity_);
212 if (kTraceRosAlloc) {
213 LOG(INFO) << "RosAlloc::AllocPages() : increased the footprint from "
214 << footprint_ << " to " << new_footprint;
215 }
216 footprint_ = new_footprint;
217
218 // And retry the last free page run.
219 it = free_page_runs_.rbegin();
220 DCHECK(it != free_page_runs_.rend());
221 FreePageRun* fpr = *it;
222 if (kIsDebugBuild && last_free_page_run_size > 0) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700223 DCHECK(last_free_page_run != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700224 DCHECK_EQ(last_free_page_run, fpr);
225 }
226 size_t fpr_byte_size = fpr->ByteSize(this);
227 DCHECK_EQ(fpr_byte_size % kPageSize, static_cast<size_t>(0));
228 DCHECK_LE(req_byte_size, fpr_byte_size);
229 free_page_runs_.erase(fpr);
230 if (kTraceRosAlloc) {
231 LOG(INFO) << "RosAlloc::AllocPages() : Erased run 0x" << std::hex << reinterpret_cast<intptr_t>(fpr)
232 << " from free_page_runs_";
233 }
234 if (req_byte_size < fpr_byte_size) {
235 // Split if there's a remainder.
Ian Rogers13735952014-10-08 12:43:28 -0700236 FreePageRun* remainder = reinterpret_cast<FreePageRun*>(reinterpret_cast<uint8_t*>(fpr) + req_byte_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700237 if (kIsDebugBuild) {
238 remainder->magic_num_ = kMagicNumFree;
239 }
240 remainder->SetByteSize(this, fpr_byte_size - req_byte_size);
241 DCHECK_EQ(remainder->ByteSize(this) % kPageSize, static_cast<size_t>(0));
242 free_page_runs_.insert(remainder);
243 if (kTraceRosAlloc) {
244 LOG(INFO) << "RosAlloc::AllocPages() : Inserted run 0x" << std::hex
245 << reinterpret_cast<intptr_t>(remainder)
246 << " into free_page_runs_";
247 }
248 fpr->SetByteSize(this, req_byte_size);
249 DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0));
250 }
251 res = fpr;
252 }
253 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700254 if (LIKELY(res != nullptr)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700255 // Update the page map.
256 size_t page_map_idx = ToPageMapIndex(res);
257 for (size_t i = 0; i < num_pages; i++) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700258 DCHECK(IsFreePage(page_map_idx + i));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700259 }
260 switch (page_map_type) {
261 case kPageMapRun:
262 page_map_[page_map_idx] = kPageMapRun;
263 for (size_t i = 1; i < num_pages; i++) {
264 page_map_[page_map_idx + i] = kPageMapRunPart;
265 }
266 break;
267 case kPageMapLargeObject:
268 page_map_[page_map_idx] = kPageMapLargeObject;
269 for (size_t i = 1; i < num_pages; i++) {
270 page_map_[page_map_idx + i] = kPageMapLargeObjectPart;
271 }
272 break;
273 default:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +0700274 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_type);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700275 break;
276 }
277 if (kIsDebugBuild) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700278 // Clear the first page since it is not madvised due to the magic number.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700279 memset(res, 0, kPageSize);
280 }
281 if (kTraceRosAlloc) {
282 LOG(INFO) << "RosAlloc::AllocPages() : 0x" << std::hex << reinterpret_cast<intptr_t>(res)
283 << "-0x" << (reinterpret_cast<intptr_t>(res) + num_pages * kPageSize)
284 << "(" << std::dec << (num_pages * kPageSize) << ")";
285 }
286 return res;
287 }
288
289 // Fail.
290 if (kTraceRosAlloc) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700291 LOG(INFO) << "RosAlloc::AllocPages() : nullptr";
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700292 }
293 return nullptr;
294}
295
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700296size_t RosAlloc::FreePages(Thread* self, void* ptr, bool already_zero) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700297 lock_.AssertHeld(self);
298 size_t pm_idx = ToPageMapIndex(ptr);
Ian Rogers5d057052014-03-12 14:32:27 -0700299 DCHECK_LT(pm_idx, page_map_size_);
Ian Rogers13735952014-10-08 12:43:28 -0700300 uint8_t pm_type = page_map_[pm_idx];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700301 DCHECK(pm_type == kPageMapRun || pm_type == kPageMapLargeObject);
Ian Rogers13735952014-10-08 12:43:28 -0700302 uint8_t pm_part_type;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700303 switch (pm_type) {
304 case kPageMapRun:
305 pm_part_type = kPageMapRunPart;
306 break;
307 case kPageMapLargeObject:
308 pm_part_type = kPageMapLargeObjectPart;
309 break;
310 default:
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700311 LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << " : " << "pm_idx=" << pm_idx << ", pm_type="
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700312 << static_cast<int>(pm_type) << ", ptr=" << std::hex
313 << reinterpret_cast<intptr_t>(ptr);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700314 return 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700315 }
316 // Update the page map and count the number of pages.
317 size_t num_pages = 1;
318 page_map_[pm_idx] = kPageMapEmpty;
319 size_t idx = pm_idx + 1;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800320 size_t end = page_map_size_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700321 while (idx < end && page_map_[idx] == pm_part_type) {
322 page_map_[idx] = kPageMapEmpty;
323 num_pages++;
324 idx++;
325 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700326 const size_t byte_size = num_pages * kPageSize;
327 if (already_zero) {
Andreas Gamped7576322014-10-24 22:13:45 -0700328 if (ShouldCheckZeroMemory()) {
Ian Rogers13735952014-10-08 12:43:28 -0700329 const uintptr_t* word_ptr = reinterpret_cast<uintptr_t*>(ptr);
330 for (size_t i = 0; i < byte_size / sizeof(uintptr_t); ++i) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700331 CHECK_EQ(word_ptr[i], 0U) << "words don't match at index " << i;
332 }
333 }
334 } else if (!DoesReleaseAllPages()) {
335 memset(ptr, 0, byte_size);
336 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700337
338 if (kTraceRosAlloc) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700339 LOG(INFO) << __PRETTY_FUNCTION__ << " : 0x" << std::hex << reinterpret_cast<intptr_t>(ptr)
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700340 << "-0x" << (reinterpret_cast<intptr_t>(ptr) + byte_size)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700341 << "(" << std::dec << (num_pages * kPageSize) << ")";
342 }
343
344 // Turn it into a free run.
345 FreePageRun* fpr = reinterpret_cast<FreePageRun*>(ptr);
346 if (kIsDebugBuild) {
347 fpr->magic_num_ = kMagicNumFree;
348 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700349 fpr->SetByteSize(this, byte_size);
Roland Levillain14d90572015-07-16 10:52:26 +0100350 DCHECK_ALIGNED(fpr->ByteSize(this), kPageSize);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700351
352 DCHECK(free_page_runs_.find(fpr) == free_page_runs_.end());
353 if (!free_page_runs_.empty()) {
354 // Try to coalesce in the higher address direction.
355 if (kTraceRosAlloc) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700356 LOG(INFO) << __PRETTY_FUNCTION__ << "RosAlloc::FreePages() : trying to coalesce a free page run 0x"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700357 << std::hex << reinterpret_cast<uintptr_t>(fpr) << " [" << std::dec << pm_idx << "] -0x"
358 << std::hex << reinterpret_cast<uintptr_t>(fpr->End(this)) << " [" << std::dec
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800359 << (fpr->End(this) == End() ? page_map_size_ : ToPageMapIndex(fpr->End(this))) << "]";
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700360 }
361 auto higher_it = free_page_runs_.upper_bound(fpr);
362 if (higher_it != free_page_runs_.end()) {
363 for (auto it = higher_it; it != free_page_runs_.end(); ) {
364 FreePageRun* h = *it;
365 DCHECK_EQ(h->ByteSize(this) % kPageSize, static_cast<size_t>(0));
366 if (kTraceRosAlloc) {
367 LOG(INFO) << "RosAlloc::FreePages() : trying to coalesce with a higher free page run 0x"
368 << std::hex << reinterpret_cast<uintptr_t>(h) << " [" << std::dec << ToPageMapIndex(h) << "] -0x"
369 << std::hex << reinterpret_cast<uintptr_t>(h->End(this)) << " [" << std::dec
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800370 << (h->End(this) == End() ? page_map_size_ : ToPageMapIndex(h->End(this))) << "]";
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700371 }
372 if (fpr->End(this) == h->Begin()) {
373 if (kTraceRosAlloc) {
374 LOG(INFO) << "Success";
375 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700376 // Clear magic num since this is no longer the start of a free page run.
377 if (kIsDebugBuild) {
378 h->magic_num_ = 0;
379 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700380 free_page_runs_.erase(it++);
381 if (kTraceRosAlloc) {
382 LOG(INFO) << "RosAlloc::FreePages() : (coalesce) Erased run 0x" << std::hex
383 << reinterpret_cast<intptr_t>(h)
384 << " from free_page_runs_";
385 }
386 fpr->SetByteSize(this, fpr->ByteSize(this) + h->ByteSize(this));
387 DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0));
388 } else {
389 // Not adjacent. Stop.
390 if (kTraceRosAlloc) {
391 LOG(INFO) << "Fail";
392 }
393 break;
394 }
395 }
396 }
397 // Try to coalesce in the lower address direction.
398 auto lower_it = free_page_runs_.upper_bound(fpr);
399 if (lower_it != free_page_runs_.begin()) {
400 --lower_it;
401 for (auto it = lower_it; ; ) {
402 // We want to try to coalesce with the first element but
403 // there's no "<=" operator for the iterator.
404 bool to_exit_loop = it == free_page_runs_.begin();
405
406 FreePageRun* l = *it;
407 DCHECK_EQ(l->ByteSize(this) % kPageSize, static_cast<size_t>(0));
408 if (kTraceRosAlloc) {
409 LOG(INFO) << "RosAlloc::FreePages() : trying to coalesce with a lower free page run 0x"
410 << std::hex << reinterpret_cast<uintptr_t>(l) << " [" << std::dec << ToPageMapIndex(l) << "] -0x"
411 << std::hex << reinterpret_cast<uintptr_t>(l->End(this)) << " [" << std::dec
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800412 << (l->End(this) == End() ? page_map_size_ : ToPageMapIndex(l->End(this))) << "]";
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700413 }
414 if (l->End(this) == fpr->Begin()) {
415 if (kTraceRosAlloc) {
416 LOG(INFO) << "Success";
417 }
418 free_page_runs_.erase(it--);
419 if (kTraceRosAlloc) {
420 LOG(INFO) << "RosAlloc::FreePages() : (coalesce) Erased run 0x" << std::hex
421 << reinterpret_cast<intptr_t>(l)
422 << " from free_page_runs_";
423 }
424 l->SetByteSize(this, l->ByteSize(this) + fpr->ByteSize(this));
425 DCHECK_EQ(l->ByteSize(this) % kPageSize, static_cast<size_t>(0));
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700426 // Clear magic num since this is no longer the start of a free page run.
427 if (kIsDebugBuild) {
428 fpr->magic_num_ = 0;
429 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700430 fpr = l;
431 } else {
432 // Not adjacent. Stop.
433 if (kTraceRosAlloc) {
434 LOG(INFO) << "Fail";
435 }
436 break;
437 }
438 if (to_exit_loop) {
439 break;
440 }
441 }
442 }
443 }
444
445 // Insert it.
446 DCHECK_EQ(fpr->ByteSize(this) % kPageSize, static_cast<size_t>(0));
447 DCHECK(free_page_runs_.find(fpr) == free_page_runs_.end());
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800448 DCHECK(fpr->IsFree());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700449 fpr->ReleasePages(this);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800450 DCHECK(fpr->IsFree());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700451 free_page_runs_.insert(fpr);
452 DCHECK(free_page_runs_.find(fpr) != free_page_runs_.end());
453 if (kTraceRosAlloc) {
454 LOG(INFO) << "RosAlloc::FreePages() : Inserted run 0x" << std::hex << reinterpret_cast<intptr_t>(fpr)
455 << " into free_page_runs_";
456 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700457 return byte_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700458}
459
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700460void* RosAlloc::AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated,
461 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
462 DCHECK(bytes_allocated != nullptr);
463 DCHECK(usable_size != nullptr);
Ian Rogers5d057052014-03-12 14:32:27 -0700464 DCHECK_GT(size, kLargeSizeThreshold);
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800465 size_t num_pages = RoundUp(size, kPageSize) / kPageSize;
466 void* r;
467 {
468 MutexLock mu(self, lock_);
469 r = AllocPages(self, num_pages, kPageMapLargeObject);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700470 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800471 if (UNLIKELY(r == nullptr)) {
472 if (kTraceRosAlloc) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700473 LOG(INFO) << "RosAlloc::AllocLargeObject() : nullptr";
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800474 }
475 return nullptr;
476 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700477 const size_t total_bytes = num_pages * kPageSize;
478 *bytes_allocated = total_bytes;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700479 *usable_size = total_bytes;
480 *bytes_tl_bulk_allocated = total_bytes;
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800481 if (kTraceRosAlloc) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800482 LOG(INFO) << "RosAlloc::AllocLargeObject() : 0x" << std::hex << reinterpret_cast<intptr_t>(r)
483 << "-0x" << (reinterpret_cast<intptr_t>(r) + num_pages * kPageSize)
484 << "(" << std::dec << (num_pages * kPageSize) << ")";
485 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700486 // Check if the returned memory is really all zero.
Andreas Gamped7576322014-10-24 22:13:45 -0700487 if (ShouldCheckZeroMemory()) {
Ian Rogers13735952014-10-08 12:43:28 -0700488 CHECK_EQ(total_bytes % sizeof(uintptr_t), 0U);
489 const uintptr_t* words = reinterpret_cast<uintptr_t*>(r);
490 for (size_t i = 0; i < total_bytes / sizeof(uintptr_t); ++i) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700491 CHECK_EQ(words[i], 0U);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700492 }
493 }
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800494 return r;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700495}
496
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700497size_t RosAlloc::FreeInternal(Thread* self, void* ptr) {
Ian Rogers5d057052014-03-12 14:32:27 -0700498 DCHECK_LE(base_, ptr);
499 DCHECK_LT(ptr, base_ + footprint_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700500 size_t pm_idx = RoundDownToPageMapIndex(ptr);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700501 Run* run = nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700502 {
503 MutexLock mu(self, lock_);
Ian Rogers5d057052014-03-12 14:32:27 -0700504 DCHECK_LT(pm_idx, page_map_size_);
Ian Rogers13735952014-10-08 12:43:28 -0700505 uint8_t page_map_entry = page_map_[pm_idx];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700506 if (kTraceRosAlloc) {
507 LOG(INFO) << "RosAlloc::FreeInternal() : " << std::hex << ptr << ", pm_idx=" << std::dec << pm_idx
508 << ", page_map_entry=" << static_cast<int>(page_map_entry);
509 }
510 switch (page_map_[pm_idx]) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700511 case kPageMapLargeObject:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700512 return FreePages(self, ptr, false);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700513 case kPageMapLargeObjectPart:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +0700514 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700515 return 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700516 case kPageMapRunPart: {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700517 // Find the beginning of the run.
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700518 do {
519 --pm_idx;
520 DCHECK_LT(pm_idx, capacity_ / kPageSize);
521 } while (page_map_[pm_idx] != kPageMapRun);
Ian Rogersfc787ec2014-10-09 21:56:44 -0700522 FALLTHROUGH_INTENDED;
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700523 case kPageMapRun:
524 run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize);
Ian Rogers5d057052014-03-12 14:32:27 -0700525 DCHECK_EQ(run->magic_num_, kMagicNum);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700526 break;
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700527 case kPageMapReleased:
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700528 case kPageMapEmpty:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +0700529 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]);
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700530 return 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700531 }
532 default:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +0700533 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700534 return 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700535 }
536 }
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700537 DCHECK(run != nullptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700538 return FreeFromRun(self, ptr, run);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700539}
540
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700541size_t RosAlloc::Free(Thread* self, void* ptr) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700542 ReaderMutexLock rmu(self, bulk_free_lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700543 return FreeInternal(self, ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700544}
545
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700546RosAlloc::Run* RosAlloc::AllocRun(Thread* self, size_t idx) {
547 RosAlloc::Run* new_run = nullptr;
548 {
549 MutexLock mu(self, lock_);
550 new_run = reinterpret_cast<Run*>(AllocPages(self, numOfPages[idx], kPageMapRun));
551 }
552 if (LIKELY(new_run != nullptr)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700553 if (kIsDebugBuild) {
554 new_run->magic_num_ = kMagicNum;
555 }
556 new_run->size_bracket_idx_ = idx;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700557 DCHECK(!new_run->IsThreadLocal());
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700558 DCHECK(!new_run->to_be_bulk_freed_);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700559 if (kUsePrefetchDuringAllocRun && idx < kNumThreadLocalSizeBrackets) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700560 // Take ownership of the cache lines if we are likely to be thread local run.
561 if (kPrefetchNewRunDataByZeroing) {
562 // Zeroing the data is sometimes faster than prefetching but it increases memory usage
563 // since we end up dirtying zero pages which may have been madvised.
564 new_run->ZeroData();
565 } else {
566 const size_t num_of_slots = numOfSlots[idx];
567 const size_t bracket_size = bracketSizes[idx];
568 const size_t num_of_bytes = num_of_slots * bracket_size;
Ian Rogers13735952014-10-08 12:43:28 -0700569 uint8_t* begin = reinterpret_cast<uint8_t*>(new_run) + headerSizes[idx];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700570 for (size_t i = 0; i < num_of_bytes; i += kPrefetchStride) {
571 __builtin_prefetch(begin + i);
572 }
573 }
574 }
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700575 new_run->InitFreeList();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700576 }
577 return new_run;
578}
579
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700580RosAlloc::Run* RosAlloc::RefillRun(Thread* self, size_t idx) {
581 // Get the lowest address non-full run from the binary tree.
Mathieu Chartier58553c72014-09-16 16:25:55 -0700582 auto* const bt = &non_full_runs_[idx];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700583 if (!bt->empty()) {
584 // If there's one, use it as the current run.
585 auto it = bt->begin();
586 Run* non_full_run = *it;
587 DCHECK(non_full_run != nullptr);
588 DCHECK(!non_full_run->IsThreadLocal());
589 bt->erase(it);
590 return non_full_run;
591 }
592 // If there's none, allocate a new run and use it as the current run.
593 return AllocRun(self, idx);
594}
595
Hiroshi Yamauchi52cf5c02014-05-02 12:20:36 -0700596inline void* RosAlloc::AllocFromCurrentRunUnlocked(Thread* self, size_t idx) {
Mathieu Chartier0651d412014-04-29 14:37:57 -0700597 Run* current_run = current_runs_[idx];
598 DCHECK(current_run != nullptr);
599 void* slot_addr = current_run->AllocSlot();
600 if (UNLIKELY(slot_addr == nullptr)) {
601 // The current run got full. Try to refill it.
602 DCHECK(current_run->IsFull());
603 if (kIsDebugBuild && current_run != dedicated_full_run_) {
604 full_runs_[idx].insert(current_run);
605 if (kTraceRosAlloc) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700606 LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex
607 << reinterpret_cast<intptr_t>(current_run)
Mathieu Chartier0651d412014-04-29 14:37:57 -0700608 << " into full_runs_[" << std::dec << idx << "]";
609 }
610 DCHECK(non_full_runs_[idx].find(current_run) == non_full_runs_[idx].end());
611 DCHECK(full_runs_[idx].find(current_run) != full_runs_[idx].end());
612 }
613 current_run = RefillRun(self, idx);
614 if (UNLIKELY(current_run == nullptr)) {
615 // Failed to allocate a new run, make sure that it is the dedicated full run.
616 current_runs_[idx] = dedicated_full_run_;
617 return nullptr;
618 }
619 DCHECK(current_run != nullptr);
620 DCHECK(non_full_runs_[idx].find(current_run) == non_full_runs_[idx].end());
621 DCHECK(full_runs_[idx].find(current_run) == full_runs_[idx].end());
622 current_run->SetIsThreadLocal(false);
623 current_runs_[idx] = current_run;
624 DCHECK(!current_run->IsFull());
625 slot_addr = current_run->AllocSlot();
626 // Must succeed now with a new run.
627 DCHECK(slot_addr != nullptr);
628 }
629 return slot_addr;
630}
631
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700632void* RosAlloc::AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated,
633 size_t* usable_size,
634 size_t* bytes_tl_bulk_allocated) {
635 DCHECK(bytes_allocated != nullptr);
636 DCHECK(usable_size != nullptr);
637 DCHECK(bytes_tl_bulk_allocated != nullptr);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700638 DCHECK_LE(size, kLargeSizeThreshold);
639 size_t bracket_size;
640 size_t idx = SizeToIndexAndBracketSize(size, &bracket_size);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700641 Locks::mutator_lock_->AssertExclusiveHeld(self);
642 void* slot_addr = AllocFromCurrentRunUnlocked(self, idx);
643 if (LIKELY(slot_addr != nullptr)) {
Mathieu Chartier0651d412014-04-29 14:37:57 -0700644 *bytes_allocated = bracket_size;
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700645 *usable_size = bracket_size;
646 *bytes_tl_bulk_allocated = bracket_size;
Mathieu Chartier0651d412014-04-29 14:37:57 -0700647 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700648 // Caller verifies that it is all 0.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700649 return slot_addr;
650}
651
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700652void* RosAlloc::AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated,
653 size_t* usable_size, size_t* bytes_tl_bulk_allocated) {
654 DCHECK(bytes_allocated != nullptr);
655 DCHECK(usable_size != nullptr);
656 DCHECK(bytes_tl_bulk_allocated != nullptr);
Ian Rogers5d057052014-03-12 14:32:27 -0700657 DCHECK_LE(size, kLargeSizeThreshold);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700658 size_t bracket_size;
659 size_t idx = SizeToIndexAndBracketSize(size, &bracket_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700660 void* slot_addr;
Mathieu Chartier0651d412014-04-29 14:37:57 -0700661 if (LIKELY(idx < kNumThreadLocalSizeBrackets)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700662 // Use a thread-local run.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700663 Run* thread_local_run = reinterpret_cast<Run*>(self->GetRosAllocRun(idx));
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700664 // Allow invalid since this will always fail the allocation.
Mathieu Chartier4fd20502014-04-28 09:35:55 -0700665 if (kIsDebugBuild) {
666 // Need the lock to prevent race conditions.
667 MutexLock mu(self, *size_bracket_locks_[idx]);
668 CHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end());
669 CHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end());
670 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700671 DCHECK(thread_local_run != nullptr);
672 DCHECK(thread_local_run->IsThreadLocal() || thread_local_run == dedicated_full_run_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700673 slot_addr = thread_local_run->AllocSlot();
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700674 // The allocation must fail if the run is invalid.
675 DCHECK(thread_local_run != dedicated_full_run_ || slot_addr == nullptr)
676 << "allocated from an invalid run";
677 if (UNLIKELY(slot_addr == nullptr)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700678 // The run got full. Try to free slots.
679 DCHECK(thread_local_run->IsFull());
680 MutexLock mu(self, *size_bracket_locks_[idx]);
681 bool is_all_free_after_merge;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700682 // This is safe to do for the dedicated_full_run_ since the bitmaps are empty.
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700683 if (thread_local_run->MergeThreadLocalFreeListToFreeList(&is_all_free_after_merge)) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700684 DCHECK_NE(thread_local_run, dedicated_full_run_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700685 // Some slot got freed. Keep it.
686 DCHECK(!thread_local_run->IsFull());
687 DCHECK_EQ(is_all_free_after_merge, thread_local_run->IsAllFree());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700688 } else {
689 // No slots got freed. Try to refill the thread-local run.
690 DCHECK(thread_local_run->IsFull());
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700691 if (thread_local_run != dedicated_full_run_) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700692 thread_local_run->SetIsThreadLocal(false);
693 if (kIsDebugBuild) {
694 full_runs_[idx].insert(thread_local_run);
695 if (kTraceRosAlloc) {
696 LOG(INFO) << "RosAlloc::AllocFromRun() : Inserted run 0x" << std::hex
697 << reinterpret_cast<intptr_t>(thread_local_run)
698 << " into full_runs_[" << std::dec << idx << "]";
699 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700700 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700701 DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end());
702 DCHECK(full_runs_[idx].find(thread_local_run) != full_runs_[idx].end());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700703 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700704
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700705 thread_local_run = RefillRun(self, idx);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700706 if (UNLIKELY(thread_local_run == nullptr)) {
707 self->SetRosAllocRun(idx, dedicated_full_run_);
708 return nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700709 }
710 DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end());
711 DCHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end());
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700712 thread_local_run->SetIsThreadLocal(true);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700713 self->SetRosAllocRun(idx, thread_local_run);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700714 DCHECK(!thread_local_run->IsFull());
715 }
Mathieu Chartier0651d412014-04-29 14:37:57 -0700716 DCHECK(thread_local_run != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700717 DCHECK(!thread_local_run->IsFull());
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700718 DCHECK(thread_local_run->IsThreadLocal());
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700719 // Account for all the free slots in the new or refreshed thread local run.
720 *bytes_tl_bulk_allocated = thread_local_run->NumberOfFreeSlots() * bracket_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700721 slot_addr = thread_local_run->AllocSlot();
722 // Must succeed now with a new run.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700723 DCHECK(slot_addr != nullptr);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700724 } else {
725 // The slot is already counted. Leave it as is.
726 *bytes_tl_bulk_allocated = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700727 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700728 DCHECK(slot_addr != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700729 if (kTraceRosAlloc) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700730 LOG(INFO) << "RosAlloc::AllocFromRun() thread-local : 0x" << std::hex
731 << reinterpret_cast<intptr_t>(slot_addr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700732 << "-0x" << (reinterpret_cast<intptr_t>(slot_addr) + bracket_size)
733 << "(" << std::dec << (bracket_size) << ")";
734 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700735 *bytes_allocated = bracket_size;
736 *usable_size = bracket_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700737 } else {
738 // Use the (shared) current run.
739 MutexLock mu(self, *size_bracket_locks_[idx]);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700740 slot_addr = AllocFromCurrentRunUnlocked(self, idx);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700741 if (kTraceRosAlloc) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700742 LOG(INFO) << "RosAlloc::AllocFromRun() : 0x" << std::hex
743 << reinterpret_cast<intptr_t>(slot_addr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700744 << "-0x" << (reinterpret_cast<intptr_t>(slot_addr) + bracket_size)
745 << "(" << std::dec << (bracket_size) << ")";
746 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700747 if (LIKELY(slot_addr != nullptr)) {
748 *bytes_allocated = bracket_size;
749 *usable_size = bracket_size;
750 *bytes_tl_bulk_allocated = bracket_size;
751 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700752 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700753 // Caller verifies that it is all 0.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700754 return slot_addr;
755}
756
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700757size_t RosAlloc::FreeFromRun(Thread* self, void* ptr, Run* run) {
Ian Rogers5d057052014-03-12 14:32:27 -0700758 DCHECK_EQ(run->magic_num_, kMagicNum);
759 DCHECK_LT(run, ptr);
760 DCHECK_LT(ptr, run->End());
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700761 const size_t idx = run->size_bracket_idx_;
762 const size_t bracket_size = bracketSizes[idx];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700763 bool run_was_full = false;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800764 MutexLock brackets_mu(self, *size_bracket_locks_[idx]);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700765 if (kIsDebugBuild) {
766 run_was_full = run->IsFull();
767 }
768 if (kTraceRosAlloc) {
769 LOG(INFO) << "RosAlloc::FreeFromRun() : 0x" << std::hex << reinterpret_cast<intptr_t>(ptr);
770 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700771 if (LIKELY(run->IsThreadLocal())) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700772 // It's a thread-local run. Just mark the thread-local free bit map and return.
Mathieu Chartier0651d412014-04-29 14:37:57 -0700773 DCHECK_LT(run->size_bracket_idx_, kNumThreadLocalSizeBrackets);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700774 DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end());
775 DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end());
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700776 run->AddToThreadLocalFreeList(ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700777 if (kTraceRosAlloc) {
778 LOG(INFO) << "RosAlloc::FreeFromRun() : Freed a slot in a thread local run 0x" << std::hex
779 << reinterpret_cast<intptr_t>(run);
780 }
781 // A thread local run will be kept as a thread local even if it's become all free.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700782 return bracket_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700783 }
784 // Free the slot in the run.
785 run->FreeSlot(ptr);
Mathieu Chartier58553c72014-09-16 16:25:55 -0700786 auto* non_full_runs = &non_full_runs_[idx];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700787 if (run->IsAllFree()) {
788 // It has just become completely free. Free the pages of this run.
789 std::set<Run*>::iterator pos = non_full_runs->find(run);
790 if (pos != non_full_runs->end()) {
791 non_full_runs->erase(pos);
792 if (kTraceRosAlloc) {
793 LOG(INFO) << "RosAlloc::FreeFromRun() : Erased run 0x" << std::hex
794 << reinterpret_cast<intptr_t>(run) << " from non_full_runs_";
795 }
796 }
797 if (run == current_runs_[idx]) {
Mathieu Chartier0651d412014-04-29 14:37:57 -0700798 current_runs_[idx] = dedicated_full_run_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700799 }
800 DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end());
801 DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end());
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700802 run->ZeroHeaderAndSlotHeaders();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700803 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800804 MutexLock lock_mu(self, lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700805 FreePages(self, run, true);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700806 }
807 } else {
808 // It is not completely free. If it wasn't the current run or
809 // already in the non-full run set (i.e., it was full) insert it
810 // into the non-full run set.
811 if (run != current_runs_[idx]) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700812 auto* full_runs = kIsDebugBuild ? &full_runs_[idx] : nullptr;
Mathieu Chartier58553c72014-09-16 16:25:55 -0700813 auto pos = non_full_runs->find(run);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700814 if (pos == non_full_runs->end()) {
815 DCHECK(run_was_full);
816 DCHECK(full_runs->find(run) != full_runs->end());
817 if (kIsDebugBuild) {
818 full_runs->erase(run);
819 if (kTraceRosAlloc) {
820 LOG(INFO) << "RosAlloc::FreeFromRun() : Erased run 0x" << std::hex
821 << reinterpret_cast<intptr_t>(run) << " from full_runs_";
822 }
823 }
824 non_full_runs->insert(run);
825 DCHECK(!run->IsFull());
826 if (kTraceRosAlloc) {
827 LOG(INFO) << "RosAlloc::FreeFromRun() : Inserted run 0x" << std::hex
828 << reinterpret_cast<intptr_t>(run)
829 << " into non_full_runs_[" << std::dec << idx << "]";
830 }
831 }
832 }
833 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700834 return bracket_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700835}
836
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700837template<bool kUseTail>
838std::string RosAlloc::Run::FreeListToStr(SlotFreeList<kUseTail>* free_list) {
839 std::string free_list_str;
840 const uint8_t idx = size_bracket_idx_;
841 const size_t bracket_size = bracketSizes[idx];
842 for (Slot* slot = free_list->Head(); slot != nullptr; slot = slot->Next()) {
843 bool is_last = slot->Next() == nullptr;
844 uintptr_t slot_offset = reinterpret_cast<uintptr_t>(slot) -
845 reinterpret_cast<uintptr_t>(FirstSlot());
846 DCHECK_EQ(slot_offset % bracket_size, 0U);
847 uintptr_t slot_idx = slot_offset / bracket_size;
848 if (!is_last) {
849 free_list_str.append(StringPrintf("%u-", static_cast<uint32_t>(slot_idx)));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700850 } else {
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700851 free_list_str.append(StringPrintf("%u", static_cast<uint32_t>(slot_idx)));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700852 }
853 }
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700854 return free_list_str;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800855}
856
857std::string RosAlloc::Run::Dump() {
858 size_t idx = size_bracket_idx_;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800859 std::ostringstream stream;
860 stream << "RosAlloc Run = " << reinterpret_cast<void*>(this)
861 << "{ magic_num=" << static_cast<int>(magic_num_)
862 << " size_bracket_idx=" << idx
863 << " is_thread_local=" << static_cast<int>(is_thread_local_)
864 << " to_be_bulk_freed=" << static_cast<int>(to_be_bulk_freed_)
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700865 << " free_list=" << FreeListToStr(&free_list_)
866 << " bulk_free_list=" << FreeListToStr(&bulk_free_list_)
867 << " thread_local_list=" << FreeListToStr(&thread_local_free_list_)
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800868 << " }" << std::endl;
869 return stream.str();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700870}
871
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700872void RosAlloc::Run::FreeSlot(void* ptr) {
873 DCHECK(!IsThreadLocal());
Ian Rogers13735952014-10-08 12:43:28 -0700874 const uint8_t idx = size_bracket_idx_;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700875 const size_t bracket_size = bracketSizes[idx];
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700876 Slot* slot = ToSlot(ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700877 // Zero out the memory.
878 // TODO: Investigate alternate memset since ptr is guaranteed to be aligned to 16.
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700879 memset(slot, 0, bracket_size);
880 free_list_.Add(slot);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700881 if (kTraceRosAlloc) {
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700882 LOG(INFO) << "RosAlloc::Run::FreeSlot() : " << slot
883 << ", bracket_size=" << std::dec << bracket_size << ", slot_idx=" << SlotIndex(slot);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700884 }
885}
886
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700887inline bool RosAlloc::Run::MergeThreadLocalFreeListToFreeList(bool* is_all_free_after_out) {
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700888 DCHECK(IsThreadLocal());
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700889 // Merge the thread local free list into the free list and clear the thread local free list.
Ian Rogers13735952014-10-08 12:43:28 -0700890 const uint8_t idx = size_bracket_idx_;
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700891 bool thread_local_free_list_size = thread_local_free_list_.Size();
892 const size_t size_before = free_list_.Size();
893 free_list_.Merge(&thread_local_free_list_);
894 const size_t size_after = free_list_.Size();
895 DCHECK_EQ(size_before < size_after, thread_local_free_list_size > 0);
896 DCHECK_LE(size_before, size_after);
897 *is_all_free_after_out = free_list_.Size() == numOfSlots[idx];
898 // Return true at least one slot was added to the free list.
899 return size_before < size_after;
900}
901
902inline void RosAlloc::Run::MergeBulkFreeListToFreeList() {
903 DCHECK(!IsThreadLocal());
904 // Merge the bulk free list into the free list and clear the bulk free list.
905 free_list_.Merge(&bulk_free_list_);
906}
907
908inline void RosAlloc::Run::MergeBulkFreeListToThreadLocalFreeList() {
909 DCHECK(IsThreadLocal());
910 // Merge the bulk free list into the thread local free list and clear the bulk free list.
911 thread_local_free_list_.Merge(&bulk_free_list_);
912}
913
914inline void RosAlloc::Run::AddToThreadLocalFreeList(void* ptr) {
915 DCHECK(IsThreadLocal());
916 AddToFreeListShared(ptr, &thread_local_free_list_, __FUNCTION__);
917}
918
919inline size_t RosAlloc::Run::AddToBulkFreeList(void* ptr) {
920 return AddToFreeListShared(ptr, &bulk_free_list_, __FUNCTION__);
921}
922
923inline size_t RosAlloc::Run::AddToFreeListShared(void* ptr,
924 SlotFreeList<true>* free_list,
925 const char* caller_name) {
926 const uint8_t idx = size_bracket_idx_;
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700927 const size_t bracket_size = bracketSizes[idx];
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700928 Slot* slot = ToSlot(ptr);
929 memset(slot, 0, bracket_size);
930 free_list->Add(slot);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700931 if (kTraceRosAlloc) {
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700932 LOG(INFO) << "RosAlloc::Run::" << caller_name << "() : " << ptr
933 << ", bracket_size=" << std::dec << bracket_size << ", slot_idx=" << SlotIndex(slot);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700934 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700935 return bracket_size;
936}
937
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700938inline void RosAlloc::Run::ZeroHeaderAndSlotHeaders() {
939 DCHECK(IsAllFree());
Ian Rogers13735952014-10-08 12:43:28 -0700940 const uint8_t idx = size_bracket_idx_;
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700941 // Zero the slot header (next pointers).
942 for (Slot* slot = free_list_.Head(); slot != nullptr; ) {
943 Slot* next_slot = slot->Next();
944 slot->Clear();
945 slot = next_slot;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700946 }
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700947 // Zero the header.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700948 memset(this, 0, headerSizes[idx]);
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700949 // Check that the entire run is all zero.
950 if (kIsDebugBuild) {
951 const size_t size = numOfPages[idx] * kPageSize;
952 const uintptr_t* word_ptr = reinterpret_cast<uintptr_t*>(this);
953 for (size_t i = 0; i < size / sizeof(uintptr_t); ++i) {
954 CHECK_EQ(word_ptr[i], 0U) << "words don't match at index " << i;
955 }
956 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700957}
958
959inline void RosAlloc::Run::ZeroData() {
Ian Rogers13735952014-10-08 12:43:28 -0700960 const uint8_t idx = size_bracket_idx_;
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700961 uint8_t* slot_begin = reinterpret_cast<uint8_t*>(FirstSlot());
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700962 memset(slot_begin, 0, numOfSlots[idx] * bracketSizes[idx]);
963}
964
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700965void RosAlloc::Run::InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
966 void* arg) {
967 size_t idx = size_bracket_idx_;
Ian Rogers13735952014-10-08 12:43:28 -0700968 uint8_t* slot_base = reinterpret_cast<uint8_t*>(this) + headerSizes[idx];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700969 size_t num_slots = numOfSlots[idx];
970 size_t bracket_size = IndexToBracketSize(idx);
Mathieu Chartierc38c5ea2015-02-04 17:46:29 -0800971 DCHECK_EQ(slot_base + num_slots * bracket_size,
972 reinterpret_cast<uint8_t*>(this) + numOfPages[idx] * kPageSize);
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700973 // Free slots are on the free list and the allocated/used slots are not. We traverse the free list
974 // to find out and record which slots are free in the is_free array.
975 std::unique_ptr<bool[]> is_free(new bool[num_slots]()); // zero initialized
976 for (Slot* slot = free_list_.Head(); slot != nullptr; slot = slot->Next()) {
977 size_t slot_idx = SlotIndex(slot);
978 DCHECK_LT(slot_idx, num_slots);
979 is_free[slot_idx] = true;
980 }
981 if (IsThreadLocal()) {
982 for (Slot* slot = thread_local_free_list_.Head(); slot != nullptr; slot = slot->Next()) {
983 size_t slot_idx = SlotIndex(slot);
984 DCHECK_LT(slot_idx, num_slots);
985 is_free[slot_idx] = true;
Mathieu Chartierc38c5ea2015-02-04 17:46:29 -0800986 }
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -0700987 }
988 for (size_t slot_idx = 0; slot_idx < num_slots; ++slot_idx) {
989 uint8_t* slot_addr = slot_base + slot_idx * bracket_size;
990 if (!is_free[slot_idx]) {
991 handler(slot_addr, slot_addr + bracket_size, bracket_size, arg);
992 } else {
993 handler(slot_addr, slot_addr + bracket_size, 0, arg);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700994 }
995 }
996}
997
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800998// If true, read the page map entries in BulkFree() without using the
999// lock for better performance, assuming that the existence of an
1000// allocated chunk/pointer being freed in BulkFree() guarantees that
1001// the page map entry won't change. Disabled for now.
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001002static constexpr bool kReadPageMapEntryWithoutLockInBulkFree = true;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001003
Mathieu Chartier8585bad2014-04-11 17:53:48 -07001004size_t RosAlloc::BulkFree(Thread* self, void** ptrs, size_t num_ptrs) {
1005 size_t freed_bytes = 0;
Ian Rogerscf7f1912014-10-22 22:06:39 -07001006 if ((false)) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001007 // Used only to test Free() as GC uses only BulkFree().
1008 for (size_t i = 0; i < num_ptrs; ++i) {
Mathieu Chartier8585bad2014-04-11 17:53:48 -07001009 freed_bytes += FreeInternal(self, ptrs[i]);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001010 }
Mathieu Chartier8585bad2014-04-11 17:53:48 -07001011 return freed_bytes;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001012 }
1013
1014 WriterMutexLock wmu(self, bulk_free_lock_);
1015
1016 // First mark slots to free in the bulk free bit map without locking the
Ian Rogers5fcfa7d2014-05-15 11:43:06 -07001017 // size bracket locks. On host, unordered_set is faster than vector + flag.
Andreas Gampec60e1b72015-07-30 08:57:50 -07001018#ifdef __ANDROID__
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001019 std::vector<Run*> runs;
1020#else
Ian Rogers700a4022014-05-19 16:49:03 -07001021 std::unordered_set<Run*, hash_run, eq_run> runs;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001022#endif
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001023 for (size_t i = 0; i < num_ptrs; i++) {
1024 void* ptr = ptrs[i];
Ian Rogers5d057052014-03-12 14:32:27 -07001025 DCHECK_LE(base_, ptr);
1026 DCHECK_LT(ptr, base_ + footprint_);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001027 size_t pm_idx = RoundDownToPageMapIndex(ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001028 Run* run = nullptr;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001029 if (kReadPageMapEntryWithoutLockInBulkFree) {
1030 // Read the page map entries without locking the lock.
Ian Rogers13735952014-10-08 12:43:28 -07001031 uint8_t page_map_entry = page_map_[pm_idx];
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001032 if (kTraceRosAlloc) {
1033 LOG(INFO) << "RosAlloc::BulkFree() : " << std::hex << ptr << ", pm_idx="
1034 << std::dec << pm_idx
1035 << ", page_map_entry=" << static_cast<int>(page_map_entry);
1036 }
1037 if (LIKELY(page_map_entry == kPageMapRun)) {
1038 run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001039 } else if (LIKELY(page_map_entry == kPageMapRunPart)) {
1040 size_t pi = pm_idx;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001041 // Find the beginning of the run.
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001042 do {
1043 --pi;
Ian Rogers5d057052014-03-12 14:32:27 -07001044 DCHECK_LT(pi, capacity_ / kPageSize);
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001045 } while (page_map_[pi] != kPageMapRun);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001046 run = reinterpret_cast<Run*>(base_ + pi * kPageSize);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001047 } else if (page_map_entry == kPageMapLargeObject) {
1048 MutexLock mu(self, lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001049 freed_bytes += FreePages(self, ptr, false);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001050 continue;
1051 } else {
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001052 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_entry);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001053 }
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001054 } else {
1055 // Read the page map entries with a lock.
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001056 MutexLock mu(self, lock_);
1057 DCHECK_LT(pm_idx, page_map_size_);
Ian Rogers13735952014-10-08 12:43:28 -07001058 uint8_t page_map_entry = page_map_[pm_idx];
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001059 if (kTraceRosAlloc) {
1060 LOG(INFO) << "RosAlloc::BulkFree() : " << std::hex << ptr << ", pm_idx="
1061 << std::dec << pm_idx
1062 << ", page_map_entry=" << static_cast<int>(page_map_entry);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001063 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001064 if (LIKELY(page_map_entry == kPageMapRun)) {
1065 run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize);
1066 } else if (LIKELY(page_map_entry == kPageMapRunPart)) {
1067 size_t pi = pm_idx;
1068 // Find the beginning of the run.
1069 do {
1070 --pi;
1071 DCHECK_LT(pi, capacity_ / kPageSize);
1072 } while (page_map_[pi] != kPageMapRun);
1073 run = reinterpret_cast<Run*>(base_ + pi * kPageSize);
1074 } else if (page_map_entry == kPageMapLargeObject) {
1075 freed_bytes += FreePages(self, ptr, false);
1076 continue;
1077 } else {
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001078 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_entry);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001079 }
1080 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001081 DCHECK(run != nullptr);
1082 DCHECK_EQ(run->magic_num_, kMagicNum);
1083 // Set the bit in the bulk free bit map.
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001084 freed_bytes += run->AddToBulkFreeList(ptr);
Andreas Gampec60e1b72015-07-30 08:57:50 -07001085#ifdef __ANDROID__
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001086 if (!run->to_be_bulk_freed_) {
1087 run->to_be_bulk_freed_ = true;
1088 runs.push_back(run);
1089 }
1090#else
1091 runs.insert(run);
1092#endif
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001093 }
1094
1095 // Now, iterate over the affected runs and update the alloc bit map
1096 // based on the bulk free bit map (for non-thread-local runs) and
1097 // union the bulk free bit map into the thread-local free bit map
1098 // (for thread-local runs.)
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001099 for (Run* run : runs) {
Andreas Gampec60e1b72015-07-30 08:57:50 -07001100#ifdef __ANDROID__
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001101 DCHECK(run->to_be_bulk_freed_);
1102 run->to_be_bulk_freed_ = false;
1103#endif
1104 size_t idx = run->size_bracket_idx_;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001105 MutexLock brackets_mu(self, *size_bracket_locks_[idx]);
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001106 if (run->IsThreadLocal()) {
Mathieu Chartier0651d412014-04-29 14:37:57 -07001107 DCHECK_LT(run->size_bracket_idx_, kNumThreadLocalSizeBrackets);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001108 DCHECK(non_full_runs_[idx].find(run) == non_full_runs_[idx].end());
1109 DCHECK(full_runs_[idx].find(run) == full_runs_[idx].end());
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001110 run->MergeBulkFreeListToThreadLocalFreeList();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001111 if (kTraceRosAlloc) {
1112 LOG(INFO) << "RosAlloc::BulkFree() : Freed slot(s) in a thread local run 0x"
1113 << std::hex << reinterpret_cast<intptr_t>(run);
1114 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001115 DCHECK(run->IsThreadLocal());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001116 // A thread local run will be kept as a thread local even if
1117 // it's become all free.
1118 } else {
1119 bool run_was_full = run->IsFull();
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001120 run->MergeBulkFreeListToFreeList();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001121 if (kTraceRosAlloc) {
1122 LOG(INFO) << "RosAlloc::BulkFree() : Freed slot(s) in a run 0x" << std::hex
1123 << reinterpret_cast<intptr_t>(run);
1124 }
1125 // Check if the run should be moved to non_full_runs_ or
1126 // free_page_runs_.
Mathieu Chartier58553c72014-09-16 16:25:55 -07001127 auto* non_full_runs = &non_full_runs_[idx];
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001128 auto* full_runs = kIsDebugBuild ? &full_runs_[idx] : nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001129 if (run->IsAllFree()) {
1130 // It has just become completely free. Free the pages of the
1131 // run.
1132 bool run_was_current = run == current_runs_[idx];
1133 if (run_was_current) {
1134 DCHECK(full_runs->find(run) == full_runs->end());
1135 DCHECK(non_full_runs->find(run) == non_full_runs->end());
1136 // If it was a current run, reuse it.
1137 } else if (run_was_full) {
1138 // If it was full, remove it from the full run set (debug
1139 // only.)
1140 if (kIsDebugBuild) {
Ian Rogers700a4022014-05-19 16:49:03 -07001141 std::unordered_set<Run*, hash_run, eq_run>::iterator pos = full_runs->find(run);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001142 DCHECK(pos != full_runs->end());
1143 full_runs->erase(pos);
1144 if (kTraceRosAlloc) {
1145 LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex
1146 << reinterpret_cast<intptr_t>(run)
1147 << " from full_runs_";
1148 }
1149 DCHECK(full_runs->find(run) == full_runs->end());
1150 }
1151 } else {
1152 // If it was in a non full run set, remove it from the set.
1153 DCHECK(full_runs->find(run) == full_runs->end());
1154 DCHECK(non_full_runs->find(run) != non_full_runs->end());
1155 non_full_runs->erase(run);
1156 if (kTraceRosAlloc) {
1157 LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex
1158 << reinterpret_cast<intptr_t>(run)
1159 << " from non_full_runs_";
1160 }
1161 DCHECK(non_full_runs->find(run) == non_full_runs->end());
1162 }
1163 if (!run_was_current) {
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001164 run->ZeroHeaderAndSlotHeaders();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001165 MutexLock lock_mu(self, lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001166 FreePages(self, run, true);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001167 }
1168 } else {
1169 // It is not completely free. If it wasn't the current run or
1170 // already in the non-full run set (i.e., it was full) insert
1171 // it into the non-full run set.
1172 if (run == current_runs_[idx]) {
1173 DCHECK(non_full_runs->find(run) == non_full_runs->end());
1174 DCHECK(full_runs->find(run) == full_runs->end());
1175 // If it was a current run, keep it.
1176 } else if (run_was_full) {
1177 // If it was full, remove it from the full run set (debug
1178 // only) and insert into the non-full run set.
1179 DCHECK(full_runs->find(run) != full_runs->end());
1180 DCHECK(non_full_runs->find(run) == non_full_runs->end());
1181 if (kIsDebugBuild) {
1182 full_runs->erase(run);
1183 if (kTraceRosAlloc) {
1184 LOG(INFO) << "RosAlloc::BulkFree() : Erased run 0x" << std::hex
1185 << reinterpret_cast<intptr_t>(run)
1186 << " from full_runs_";
1187 }
1188 }
1189 non_full_runs->insert(run);
1190 if (kTraceRosAlloc) {
1191 LOG(INFO) << "RosAlloc::BulkFree() : Inserted run 0x" << std::hex
1192 << reinterpret_cast<intptr_t>(run)
1193 << " into non_full_runs_[" << std::dec << idx;
1194 }
1195 } else {
1196 // If it was not full, so leave it in the non full run set.
1197 DCHECK(full_runs->find(run) == full_runs->end());
1198 DCHECK(non_full_runs->find(run) != non_full_runs->end());
1199 }
1200 }
1201 }
1202 }
Mathieu Chartier8585bad2014-04-11 17:53:48 -07001203 return freed_bytes;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001204}
1205
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001206std::string RosAlloc::DumpPageMap() {
1207 std::ostringstream stream;
1208 stream << "RosAlloc PageMap: " << std::endl;
1209 lock_.AssertHeld(Thread::Current());
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001210 size_t end = page_map_size_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001211 FreePageRun* curr_fpr = nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001212 size_t curr_fpr_size = 0;
1213 size_t remaining_curr_fpr_size = 0;
1214 size_t num_running_empty_pages = 0;
1215 for (size_t i = 0; i < end; ++i) {
Ian Rogers13735952014-10-08 12:43:28 -07001216 uint8_t pm = page_map_[i];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001217 switch (pm) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001218 case kPageMapReleased:
1219 // Fall-through.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001220 case kPageMapEmpty: {
1221 FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize);
1222 if (free_page_runs_.find(fpr) != free_page_runs_.end()) {
1223 // Encountered a fresh free page run.
1224 DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0));
1225 DCHECK(fpr->IsFree());
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001226 DCHECK(curr_fpr == nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001227 DCHECK_EQ(curr_fpr_size, static_cast<size_t>(0));
1228 curr_fpr = fpr;
1229 curr_fpr_size = fpr->ByteSize(this);
1230 DCHECK_EQ(curr_fpr_size % kPageSize, static_cast<size_t>(0));
1231 remaining_curr_fpr_size = curr_fpr_size - kPageSize;
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001232 stream << "[" << i << "]=" << (pm == kPageMapReleased ? "Released" : "Empty")
1233 << " (FPR start) fpr_size=" << curr_fpr_size
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001234 << " remaining_fpr_size=" << remaining_curr_fpr_size << std::endl;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001235 if (remaining_curr_fpr_size == 0) {
1236 // Reset at the end of the current free page run.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001237 curr_fpr = nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001238 curr_fpr_size = 0;
1239 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001240 stream << "curr_fpr=0x" << std::hex << reinterpret_cast<intptr_t>(curr_fpr) << std::endl;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001241 DCHECK_EQ(num_running_empty_pages, static_cast<size_t>(0));
1242 } else {
1243 // Still part of the current free page run.
1244 DCHECK_NE(num_running_empty_pages, static_cast<size_t>(0));
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001245 DCHECK(curr_fpr != nullptr && curr_fpr_size > 0 && remaining_curr_fpr_size > 0);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001246 DCHECK_EQ(remaining_curr_fpr_size % kPageSize, static_cast<size_t>(0));
1247 DCHECK_GE(remaining_curr_fpr_size, static_cast<size_t>(kPageSize));
1248 remaining_curr_fpr_size -= kPageSize;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001249 stream << "[" << i << "]=Empty (FPR part)"
1250 << " remaining_fpr_size=" << remaining_curr_fpr_size << std::endl;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001251 if (remaining_curr_fpr_size == 0) {
1252 // Reset at the end of the current free page run.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001253 curr_fpr = nullptr;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001254 curr_fpr_size = 0;
1255 }
1256 }
1257 num_running_empty_pages++;
1258 break;
1259 }
1260 case kPageMapLargeObject: {
1261 DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0));
1262 num_running_empty_pages = 0;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001263 stream << "[" << i << "]=Large (start)" << std::endl;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001264 break;
1265 }
1266 case kPageMapLargeObjectPart:
1267 DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0));
1268 num_running_empty_pages = 0;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001269 stream << "[" << i << "]=Large (part)" << std::endl;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001270 break;
1271 case kPageMapRun: {
1272 DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0));
1273 num_running_empty_pages = 0;
1274 Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize);
1275 size_t idx = run->size_bracket_idx_;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001276 stream << "[" << i << "]=Run (start)"
1277 << " idx=" << idx
1278 << " numOfPages=" << numOfPages[idx]
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001279 << " is_thread_local=" << run->is_thread_local_
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001280 << " is_all_free=" << (run->IsAllFree() ? 1 : 0)
1281 << std::endl;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001282 break;
1283 }
1284 case kPageMapRunPart:
1285 DCHECK_EQ(remaining_curr_fpr_size, static_cast<size_t>(0));
1286 num_running_empty_pages = 0;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001287 stream << "[" << i << "]=Run (part)" << std::endl;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001288 break;
1289 default:
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001290 stream << "[" << i << "]=Unrecognizable page map type: " << pm;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001291 break;
1292 }
1293 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001294 return stream.str();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001295}
1296
Andreas Gamped7576322014-10-24 22:13:45 -07001297size_t RosAlloc::UsableSize(const void* ptr) {
Ian Rogers5d057052014-03-12 14:32:27 -07001298 DCHECK_LE(base_, ptr);
1299 DCHECK_LT(ptr, base_ + footprint_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001300 size_t pm_idx = RoundDownToPageMapIndex(ptr);
1301 MutexLock mu(Thread::Current(), lock_);
1302 switch (page_map_[pm_idx]) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001303 case kPageMapReleased:
1304 // Fall-through.
1305 case kPageMapEmpty:
1306 LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << ": pm_idx=" << pm_idx << ", ptr="
1307 << std::hex << reinterpret_cast<intptr_t>(ptr);
1308 break;
1309 case kPageMapLargeObject: {
1310 size_t num_pages = 1;
1311 size_t idx = pm_idx + 1;
1312 size_t end = page_map_size_;
1313 while (idx < end && page_map_[idx] == kPageMapLargeObjectPart) {
1314 num_pages++;
1315 idx++;
1316 }
1317 return num_pages * kPageSize;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001318 }
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001319 case kPageMapLargeObjectPart:
1320 LOG(FATAL) << "Unreachable - " << __PRETTY_FUNCTION__ << ": pm_idx=" << pm_idx << ", ptr="
1321 << std::hex << reinterpret_cast<intptr_t>(ptr);
1322 break;
1323 case kPageMapRun:
1324 case kPageMapRunPart: {
1325 // Find the beginning of the run.
1326 while (page_map_[pm_idx] != kPageMapRun) {
1327 pm_idx--;
1328 DCHECK_LT(pm_idx, capacity_ / kPageSize);
1329 }
1330 DCHECK_EQ(page_map_[pm_idx], kPageMapRun);
1331 Run* run = reinterpret_cast<Run*>(base_ + pm_idx * kPageSize);
1332 DCHECK_EQ(run->magic_num_, kMagicNum);
1333 size_t idx = run->size_bracket_idx_;
Andreas Gamped7576322014-10-24 22:13:45 -07001334 size_t offset_from_slot_base = reinterpret_cast<const uint8_t*>(ptr)
Ian Rogers13735952014-10-08 12:43:28 -07001335 - (reinterpret_cast<uint8_t*>(run) + headerSizes[idx]);
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001336 DCHECK_EQ(offset_from_slot_base % bracketSizes[idx], static_cast<size_t>(0));
1337 return IndexToBracketSize(idx);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001338 }
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001339 default: {
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001340 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(page_map_[pm_idx]);
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001341 break;
1342 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001343 }
1344 return 0;
1345}
1346
1347bool RosAlloc::Trim() {
1348 MutexLock mu(Thread::Current(), lock_);
1349 FreePageRun* last_free_page_run;
1350 DCHECK_EQ(footprint_ % kPageSize, static_cast<size_t>(0));
1351 auto it = free_page_runs_.rbegin();
1352 if (it != free_page_runs_.rend() && (last_free_page_run = *it)->End(this) == base_ + footprint_) {
1353 // Remove the last free page run, if any.
1354 DCHECK(last_free_page_run->IsFree());
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001355 DCHECK(IsFreePage(ToPageMapIndex(last_free_page_run)));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001356 DCHECK_EQ(last_free_page_run->ByteSize(this) % kPageSize, static_cast<size_t>(0));
1357 DCHECK_EQ(last_free_page_run->End(this), base_ + footprint_);
1358 free_page_runs_.erase(last_free_page_run);
1359 size_t decrement = last_free_page_run->ByteSize(this);
1360 size_t new_footprint = footprint_ - decrement;
1361 DCHECK_EQ(new_footprint % kPageSize, static_cast<size_t>(0));
1362 size_t new_num_of_pages = new_footprint / kPageSize;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001363 DCHECK_GE(page_map_size_, new_num_of_pages);
1364 // Zero out the tail of the page map.
Ian Rogers13735952014-10-08 12:43:28 -07001365 uint8_t* zero_begin = const_cast<uint8_t*>(page_map_) + new_num_of_pages;
1366 uint8_t* madvise_begin = AlignUp(zero_begin, kPageSize);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001367 DCHECK_LE(madvise_begin, page_map_mem_map_->End());
1368 size_t madvise_size = page_map_mem_map_->End() - madvise_begin;
1369 if (madvise_size > 0) {
1370 DCHECK_ALIGNED(madvise_begin, kPageSize);
1371 DCHECK_EQ(RoundUp(madvise_size, kPageSize), madvise_size);
Ian Rogersc5f17732014-06-05 20:48:42 -07001372 if (!kMadviseZeroes) {
1373 memset(madvise_begin, 0, madvise_size);
1374 }
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001375 CHECK_EQ(madvise(madvise_begin, madvise_size, MADV_DONTNEED), 0);
1376 }
1377 if (madvise_begin - zero_begin) {
1378 memset(zero_begin, 0, madvise_begin - zero_begin);
1379 }
1380 page_map_size_ = new_num_of_pages;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001381 free_page_run_size_map_.resize(new_num_of_pages);
1382 DCHECK_EQ(free_page_run_size_map_.size(), new_num_of_pages);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001383 ArtRosAllocMoreCore(this, -(static_cast<intptr_t>(decrement)));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001384 if (kTraceRosAlloc) {
1385 LOG(INFO) << "RosAlloc::Trim() : decreased the footprint from "
1386 << footprint_ << " to " << new_footprint;
1387 }
1388 DCHECK_LT(new_footprint, footprint_);
1389 DCHECK_LT(new_footprint, capacity_);
1390 footprint_ = new_footprint;
1391 return true;
1392 }
1393 return false;
1394}
1395
1396void RosAlloc::InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
1397 void* arg) {
1398 // Note: no need to use this to release pages as we already do so in FreePages().
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001399 if (handler == nullptr) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001400 return;
1401 }
1402 MutexLock mu(Thread::Current(), lock_);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001403 size_t pm_end = page_map_size_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001404 size_t i = 0;
1405 while (i < pm_end) {
Ian Rogers13735952014-10-08 12:43:28 -07001406 uint8_t pm = page_map_[i];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001407 switch (pm) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001408 case kPageMapReleased:
1409 // Fall-through.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001410 case kPageMapEmpty: {
1411 // The start of a free page run.
1412 FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize);
1413 DCHECK(free_page_runs_.find(fpr) != free_page_runs_.end());
1414 size_t fpr_size = fpr->ByteSize(this);
Roland Levillain14d90572015-07-16 10:52:26 +01001415 DCHECK_ALIGNED(fpr_size, kPageSize);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001416 void* start = fpr;
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -08001417 if (kIsDebugBuild) {
1418 // In the debug build, the first page of a free page run
1419 // contains a magic number for debugging. Exclude it.
Ian Rogers13735952014-10-08 12:43:28 -07001420 start = reinterpret_cast<uint8_t*>(fpr) + kPageSize;
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -08001421 }
Ian Rogers13735952014-10-08 12:43:28 -07001422 void* end = reinterpret_cast<uint8_t*>(fpr) + fpr_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001423 handler(start, end, 0, arg);
1424 size_t num_pages = fpr_size / kPageSize;
1425 if (kIsDebugBuild) {
1426 for (size_t j = i + 1; j < i + num_pages; ++j) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001427 DCHECK(IsFreePage(j));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001428 }
1429 }
1430 i += fpr_size / kPageSize;
1431 DCHECK_LE(i, pm_end);
1432 break;
1433 }
1434 case kPageMapLargeObject: {
1435 // The start of a large object.
1436 size_t num_pages = 1;
1437 size_t idx = i + 1;
1438 while (idx < pm_end && page_map_[idx] == kPageMapLargeObjectPart) {
1439 num_pages++;
1440 idx++;
1441 }
1442 void* start = base_ + i * kPageSize;
1443 void* end = base_ + (i + num_pages) * kPageSize;
1444 size_t used_bytes = num_pages * kPageSize;
1445 handler(start, end, used_bytes, arg);
1446 if (kIsDebugBuild) {
1447 for (size_t j = i + 1; j < i + num_pages; ++j) {
1448 DCHECK_EQ(page_map_[j], kPageMapLargeObjectPart);
1449 }
1450 }
1451 i += num_pages;
1452 DCHECK_LE(i, pm_end);
1453 break;
1454 }
1455 case kPageMapLargeObjectPart:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001456 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001457 break;
1458 case kPageMapRun: {
1459 // The start of a run.
1460 Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize);
Ian Rogers5d057052014-03-12 14:32:27 -07001461 DCHECK_EQ(run->magic_num_, kMagicNum);
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001462 // The dedicated full run doesn't contain any real allocations, don't visit the slots in
1463 // there.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001464 run->InspectAllSlots(handler, arg);
1465 size_t num_pages = numOfPages[run->size_bracket_idx_];
1466 if (kIsDebugBuild) {
1467 for (size_t j = i + 1; j < i + num_pages; ++j) {
1468 DCHECK_EQ(page_map_[j], kPageMapRunPart);
1469 }
1470 }
1471 i += num_pages;
1472 DCHECK_LE(i, pm_end);
1473 break;
1474 }
1475 case kPageMapRunPart:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001476 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001477 break;
1478 default:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001479 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001480 break;
1481 }
1482 }
1483}
1484
1485size_t RosAlloc::Footprint() {
1486 MutexLock mu(Thread::Current(), lock_);
1487 return footprint_;
1488}
1489
1490size_t RosAlloc::FootprintLimit() {
1491 MutexLock mu(Thread::Current(), lock_);
1492 return capacity_;
1493}
1494
1495void RosAlloc::SetFootprintLimit(size_t new_capacity) {
1496 MutexLock mu(Thread::Current(), lock_);
1497 DCHECK_EQ(RoundUp(new_capacity, kPageSize), new_capacity);
1498 // Only growing is supported here. But Trim() is supported.
1499 if (capacity_ < new_capacity) {
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001500 CHECK_LE(new_capacity, max_capacity_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001501 capacity_ = new_capacity;
1502 VLOG(heap) << "new capacity=" << capacity_;
1503 }
1504}
1505
Lei Li57846212015-06-11 17:50:20 +08001506// Below may be called by mutator itself just before thread termination.
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001507size_t RosAlloc::RevokeThreadLocalRuns(Thread* thread) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001508 Thread* self = Thread::Current();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001509 size_t free_bytes = 0U;
Mathieu Chartier0651d412014-04-29 14:37:57 -07001510 for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; idx++) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001511 MutexLock mu(self, *size_bracket_locks_[idx]);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001512 Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(idx));
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001513 CHECK(thread_local_run != nullptr);
1514 // Invalid means already revoked.
1515 DCHECK(thread_local_run->IsThreadLocal());
1516 if (thread_local_run != dedicated_full_run_) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001517 // Note the thread local run may not be full here.
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001518 thread->SetRosAllocRun(idx, dedicated_full_run_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001519 DCHECK_EQ(thread_local_run->magic_num_, kMagicNum);
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001520 // Count the number of free slots left.
1521 size_t num_free_slots = thread_local_run->NumberOfFreeSlots();
1522 free_bytes += num_free_slots * bracketSizes[idx];
Lei Li57846212015-06-11 17:50:20 +08001523 // The above bracket index lock guards thread local free list to avoid race condition
1524 // with unioning bulk free list to thread local free list by GC thread in BulkFree.
1525 // If thread local run is true, GC thread will help update thread local free list
1526 // in BulkFree. And the latest thread local free list will be merged to free list
1527 // either when this thread local run is full or when revoking this run here. In this
1528 // case the free list wll be updated. If thread local run is false, GC thread will help
1529 // merge bulk free list in next BulkFree.
1530 // Thus no need to merge bulk free list to free list again here.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001531 bool dont_care;
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001532 thread_local_run->MergeThreadLocalFreeListToFreeList(&dont_care);
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001533 thread_local_run->SetIsThreadLocal(false);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001534 DCHECK(non_full_runs_[idx].find(thread_local_run) == non_full_runs_[idx].end());
1535 DCHECK(full_runs_[idx].find(thread_local_run) == full_runs_[idx].end());
Mathieu Chartier0651d412014-04-29 14:37:57 -07001536 RevokeRun(self, idx, thread_local_run);
1537 }
1538 }
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001539 return free_bytes;
Mathieu Chartier0651d412014-04-29 14:37:57 -07001540}
1541
1542void RosAlloc::RevokeRun(Thread* self, size_t idx, Run* run) {
1543 size_bracket_locks_[idx]->AssertHeld(self);
1544 DCHECK(run != dedicated_full_run_);
1545 if (run->IsFull()) {
1546 if (kIsDebugBuild) {
1547 full_runs_[idx].insert(run);
1548 DCHECK(full_runs_[idx].find(run) != full_runs_[idx].end());
1549 if (kTraceRosAlloc) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001550 LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex
Mathieu Chartier0651d412014-04-29 14:37:57 -07001551 << reinterpret_cast<intptr_t>(run)
1552 << " into full_runs_[" << std::dec << idx << "]";
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001553 }
1554 }
Mathieu Chartier0651d412014-04-29 14:37:57 -07001555 } else if (run->IsAllFree()) {
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001556 run->ZeroHeaderAndSlotHeaders();
Mathieu Chartier0651d412014-04-29 14:37:57 -07001557 MutexLock mu(self, lock_);
1558 FreePages(self, run, true);
1559 } else {
1560 non_full_runs_[idx].insert(run);
1561 DCHECK(non_full_runs_[idx].find(run) != non_full_runs_[idx].end());
1562 if (kTraceRosAlloc) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001563 LOG(INFO) << __PRETTY_FUNCTION__ << " : Inserted run 0x" << std::hex
Mathieu Chartier0651d412014-04-29 14:37:57 -07001564 << reinterpret_cast<intptr_t>(run)
1565 << " into non_full_runs_[" << std::dec << idx << "]";
1566 }
1567 }
1568}
1569
1570void RosAlloc::RevokeThreadUnsafeCurrentRuns() {
1571 // Revoke the current runs which share the same idx as thread local runs.
1572 Thread* self = Thread::Current();
1573 for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; ++idx) {
1574 MutexLock mu(self, *size_bracket_locks_[idx]);
1575 if (current_runs_[idx] != dedicated_full_run_) {
1576 RevokeRun(self, idx, current_runs_[idx]);
1577 current_runs_[idx] = dedicated_full_run_;
1578 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001579 }
1580}
1581
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001582size_t RosAlloc::RevokeAllThreadLocalRuns() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001583 // This is called when a mutator thread won't allocate such as at
1584 // the Zygote creation time or during the GC pause.
Hiroshi Yamauchif5b0e202014-02-11 17:02:22 -08001585 MutexLock mu(Thread::Current(), *Locks::runtime_shutdown_lock_);
1586 MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001587 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001588 size_t free_bytes = 0U;
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001589 for (Thread* thread : thread_list) {
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001590 free_bytes += RevokeThreadLocalRuns(thread);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001591 }
Mathieu Chartier0651d412014-04-29 14:37:57 -07001592 RevokeThreadUnsafeCurrentRuns();
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -07001593 return free_bytes;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001594}
1595
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001596void RosAlloc::AssertThreadLocalRunsAreRevoked(Thread* thread) {
1597 if (kIsDebugBuild) {
1598 Thread* self = Thread::Current();
1599 // Avoid race conditions on the bulk free bit maps with BulkFree() (GC).
Mathieu Chartiera1c1c712014-06-23 17:53:09 -07001600 ReaderMutexLock wmu(self, bulk_free_lock_);
Mathieu Chartier0651d412014-04-29 14:37:57 -07001601 for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; idx++) {
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001602 MutexLock mu(self, *size_bracket_locks_[idx]);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001603 Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(idx));
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001604 DCHECK(thread_local_run == nullptr || thread_local_run == dedicated_full_run_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001605 }
1606 }
1607}
1608
1609void RosAlloc::AssertAllThreadLocalRunsAreRevoked() {
1610 if (kIsDebugBuild) {
Mathieu Chartier0651d412014-04-29 14:37:57 -07001611 Thread* self = Thread::Current();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001612 MutexLock shutdown_mu(self, *Locks::runtime_shutdown_lock_);
1613 MutexLock thread_list_mu(self, *Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001614 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
1615 for (Thread* t : thread_list) {
1616 AssertThreadLocalRunsAreRevoked(t);
1617 }
Mathieu Chartier0651d412014-04-29 14:37:57 -07001618 for (size_t idx = 0; idx < kNumThreadLocalSizeBrackets; ++idx) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001619 MutexLock brackets_mu(self, *size_bracket_locks_[idx]);
Mathieu Chartier0651d412014-04-29 14:37:57 -07001620 CHECK_EQ(current_runs_[idx], dedicated_full_run_);
1621 }
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -07001622 }
1623}
1624
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001625void RosAlloc::Initialize() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001626 // bracketSizes.
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001627 static_assert(kNumRegularSizeBrackets == kNumOfSizeBrackets - 2,
1628 "There should be two non-regular brackets");
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001629 for (size_t i = 0; i < kNumOfSizeBrackets; i++) {
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001630 if (i < kNumThreadLocalSizeBrackets) {
1631 bracketSizes[i] = kThreadLocalBracketQuantumSize * (i + 1);
1632 } else if (i < kNumRegularSizeBrackets) {
1633 bracketSizes[i] = kBracketQuantumSize * (i - kNumThreadLocalSizeBrackets + 1) +
1634 (kThreadLocalBracketQuantumSize * kNumThreadLocalSizeBrackets);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001635 } else if (i == kNumOfSizeBrackets - 2) {
1636 bracketSizes[i] = 1 * KB;
1637 } else {
Ian Rogers5d057052014-03-12 14:32:27 -07001638 DCHECK_EQ(i, kNumOfSizeBrackets - 1);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001639 bracketSizes[i] = 2 * KB;
1640 }
1641 if (kTraceRosAlloc) {
1642 LOG(INFO) << "bracketSizes[" << i << "]=" << bracketSizes[i];
1643 }
1644 }
1645 // numOfPages.
1646 for (size_t i = 0; i < kNumOfSizeBrackets; i++) {
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001647 if (i < kNumThreadLocalSizeBrackets) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001648 numOfPages[i] = 1;
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001649 } else if (i < (kNumThreadLocalSizeBrackets + kNumRegularSizeBrackets) / 2) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001650 numOfPages[i] = 4;
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001651 } else if (i < kNumRegularSizeBrackets) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001652 numOfPages[i] = 8;
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001653 } else if (i == kNumOfSizeBrackets - 2) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001654 numOfPages[i] = 16;
1655 } else {
Ian Rogers5d057052014-03-12 14:32:27 -07001656 DCHECK_EQ(i, kNumOfSizeBrackets - 1);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001657 numOfPages[i] = 32;
1658 }
1659 if (kTraceRosAlloc) {
1660 LOG(INFO) << "numOfPages[" << i << "]=" << numOfPages[i];
1661 }
1662 }
1663 // Compute numOfSlots and slotOffsets.
1664 for (size_t i = 0; i < kNumOfSizeBrackets; i++) {
1665 size_t bracket_size = bracketSizes[i];
1666 size_t run_size = kPageSize * numOfPages[i];
1667 size_t max_num_of_slots = run_size / bracket_size;
1668 // Compute the actual number of slots by taking the header and
1669 // alignment into account.
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001670 size_t fixed_header_size = RoundUp(Run::fixed_header_size(), sizeof(uint64_t));
1671 DCHECK_EQ(fixed_header_size, 80U);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001672 size_t header_size = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001673 size_t num_of_slots = 0;
1674 // Search for the maximum number of slots that allows enough space
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001675 // for the header.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001676 for (int s = max_num_of_slots; s >= 0; s--) {
1677 size_t tmp_slots_size = bracket_size * s;
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001678 size_t tmp_unaligned_header_size = fixed_header_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001679 // Align up the unaligned header size. bracket_size may not be a power of two.
1680 size_t tmp_header_size = (tmp_unaligned_header_size % bracket_size == 0) ?
1681 tmp_unaligned_header_size :
1682 tmp_unaligned_header_size + (bracket_size - tmp_unaligned_header_size % bracket_size);
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001683 DCHECK_EQ(tmp_header_size % bracket_size, 0U);
1684 DCHECK_EQ(tmp_header_size % sizeof(uint64_t), 0U);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001685 if (tmp_slots_size + tmp_header_size <= run_size) {
1686 // Found the right number of slots, that is, there was enough
1687 // space for the header (including the bit maps.)
1688 num_of_slots = s;
1689 header_size = tmp_header_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001690 break;
1691 }
1692 }
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001693 DCHECK_GT(num_of_slots, 0U) << i;
1694 DCHECK_GT(header_size, 0U) << i;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001695 // Add the padding for the alignment remainder.
1696 header_size += run_size % bracket_size;
Ian Rogers5d057052014-03-12 14:32:27 -07001697 DCHECK_EQ(header_size + num_of_slots * bracket_size, run_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001698 numOfSlots[i] = num_of_slots;
1699 headerSizes[i] = header_size;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001700 if (kTraceRosAlloc) {
1701 LOG(INFO) << "numOfSlots[" << i << "]=" << numOfSlots[i]
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001702 << ", headerSizes[" << i << "]=" << headerSizes[i];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001703 }
1704 }
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001705 // Set up the dedicated full run so that nobody can successfully allocate from it.
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001706 if (kIsDebugBuild) {
1707 dedicated_full_run_->magic_num_ = kMagicNum;
1708 }
1709 // It doesn't matter which size bracket we use since the main goal is to have the allocation
1710 // fail 100% of the time you attempt to allocate into the dedicated full run.
1711 dedicated_full_run_->size_bracket_idx_ = 0;
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001712 DCHECK_EQ(dedicated_full_run_->FreeList()->Size(), 0U); // It looks full.
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001713 dedicated_full_run_->SetIsThreadLocal(true);
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001714
1715 // The smallest bracket size must be at least as large as the sizeof(Slot).
1716 DCHECK_LE(sizeof(Slot), bracketSizes[0]) << "sizeof(Slot) <= the smallest bracket size";
Hiroshi Yamauchi7ed9c562016-02-02 15:22:09 -08001717 // Check the invariants between the max bracket sizes and the number of brackets.
1718 DCHECK_EQ(kMaxThreadLocalBracketSize, bracketSizes[kNumThreadLocalSizeBrackets - 1]);
1719 DCHECK_EQ(kMaxRegularBracketSize, bracketSizes[kNumRegularSizeBrackets - 1]);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001720}
1721
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001722void RosAlloc::BytesAllocatedCallback(void* start ATTRIBUTE_UNUSED, void* end ATTRIBUTE_UNUSED,
1723 size_t used_bytes, void* arg) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001724 if (used_bytes == 0) {
1725 return;
1726 }
1727 size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
1728 *bytes_allocated += used_bytes;
1729}
1730
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001731void RosAlloc::ObjectsAllocatedCallback(void* start ATTRIBUTE_UNUSED, void* end ATTRIBUTE_UNUSED,
1732 size_t used_bytes, void* arg) {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001733 if (used_bytes == 0) {
1734 return;
1735 }
1736 size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
1737 ++(*objects_allocated);
1738}
1739
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001740void RosAlloc::Verify() {
1741 Thread* self = Thread::Current();
1742 CHECK(Locks::mutator_lock_->IsExclusiveHeld(self))
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001743 << "The mutator locks isn't exclusively locked at " << __PRETTY_FUNCTION__;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001744 MutexLock thread_list_mu(self, *Locks::thread_list_lock_);
Mathieu Chartiera1c1c712014-06-23 17:53:09 -07001745 ReaderMutexLock wmu(self, bulk_free_lock_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001746 std::vector<Run*> runs;
1747 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001748 MutexLock lock_mu(self, lock_);
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -08001749 size_t pm_end = page_map_size_;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001750 size_t i = 0;
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001751 size_t memory_tool_modifier = is_running_on_memory_tool_ ?
1752 2 * ::art::gc::space::kDefaultMemoryToolRedZoneBytes : // Redzones before and after.
Andreas Gampefef16ad2015-02-19 16:44:32 -08001753 0;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001754 while (i < pm_end) {
Ian Rogers13735952014-10-08 12:43:28 -07001755 uint8_t pm = page_map_[i];
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001756 switch (pm) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001757 case kPageMapReleased:
1758 // Fall-through.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001759 case kPageMapEmpty: {
1760 // The start of a free page run.
1761 FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize);
Ian Rogers5d057052014-03-12 14:32:27 -07001762 DCHECK_EQ(fpr->magic_num_, kMagicNumFree);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001763 CHECK(free_page_runs_.find(fpr) != free_page_runs_.end())
1764 << "An empty page must belong to the free page run set";
1765 size_t fpr_size = fpr->ByteSize(this);
Roland Levillain14d90572015-07-16 10:52:26 +01001766 CHECK_ALIGNED(fpr_size, kPageSize)
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001767 << "A free page run size isn't page-aligned : " << fpr_size;
1768 size_t num_pages = fpr_size / kPageSize;
1769 CHECK_GT(num_pages, static_cast<uintptr_t>(0))
1770 << "A free page run size must be > 0 : " << fpr_size;
1771 for (size_t j = i + 1; j < i + num_pages; ++j) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001772 CHECK(IsFreePage(j))
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001773 << "A mismatch between the page map table for kPageMapEmpty "
1774 << " at page index " << j
1775 << " and the free page run size : page index range : "
1776 << i << " to " << (i + num_pages) << std::endl << DumpPageMap();
1777 }
1778 i += num_pages;
1779 CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end
1780 << std::endl << DumpPageMap();
1781 break;
1782 }
1783 case kPageMapLargeObject: {
1784 // The start of a large object.
1785 size_t num_pages = 1;
1786 size_t idx = i + 1;
1787 while (idx < pm_end && page_map_[idx] == kPageMapLargeObjectPart) {
1788 num_pages++;
1789 idx++;
1790 }
Andreas Gamped7576322014-10-24 22:13:45 -07001791 uint8_t* start = base_ + i * kPageSize;
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001792 if (is_running_on_memory_tool_) {
1793 start += ::art::gc::space::kDefaultMemoryToolRedZoneBytes;
Andreas Gamped7576322014-10-24 22:13:45 -07001794 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001795 mirror::Object* obj = reinterpret_cast<mirror::Object*>(start);
1796 size_t obj_size = obj->SizeOf();
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001797 CHECK_GT(obj_size + memory_tool_modifier, kLargeSizeThreshold)
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001798 << "A rosalloc large object size must be > " << kLargeSizeThreshold;
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001799 CHECK_EQ(num_pages, RoundUp(obj_size + memory_tool_modifier, kPageSize) / kPageSize)
1800 << "A rosalloc large object size " << obj_size + memory_tool_modifier
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001801 << " does not match the page map table " << (num_pages * kPageSize)
1802 << std::endl << DumpPageMap();
1803 i += num_pages;
1804 CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end
1805 << std::endl << DumpPageMap();
1806 break;
1807 }
1808 case kPageMapLargeObjectPart:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001809 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl << DumpPageMap();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001810 break;
1811 case kPageMapRun: {
1812 // The start of a run.
1813 Run* run = reinterpret_cast<Run*>(base_ + i * kPageSize);
Ian Rogers5d057052014-03-12 14:32:27 -07001814 DCHECK_EQ(run->magic_num_, kMagicNum);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001815 size_t idx = run->size_bracket_idx_;
Ian Rogers5d057052014-03-12 14:32:27 -07001816 CHECK_LT(idx, kNumOfSizeBrackets) << "Out of range size bracket index : " << idx;
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001817 size_t num_pages = numOfPages[idx];
1818 CHECK_GT(num_pages, static_cast<uintptr_t>(0))
1819 << "Run size must be > 0 : " << num_pages;
1820 for (size_t j = i + 1; j < i + num_pages; ++j) {
1821 CHECK_EQ(page_map_[j], kPageMapRunPart)
1822 << "A mismatch between the page map table for kPageMapRunPart "
1823 << " at page index " << j
1824 << " and the run size : page index range " << i << " to " << (i + num_pages)
1825 << std::endl << DumpPageMap();
1826 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001827 // Don't verify the dedicated_full_run_ since it doesn't have any real allocations.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001828 runs.push_back(run);
1829 i += num_pages;
1830 CHECK_LE(i, pm_end) << "Page map index " << i << " out of range < " << pm_end
1831 << std::endl << DumpPageMap();
1832 break;
1833 }
1834 case kPageMapRunPart:
Mathieu Chartier0651d412014-04-29 14:37:57 -07001835 // Fall-through.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001836 default:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07001837 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm) << std::endl << DumpPageMap();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001838 break;
1839 }
1840 }
1841 }
Mathieu Chartier0651d412014-04-29 14:37:57 -07001842 std::list<Thread*> threads = Runtime::Current()->GetThreadList()->GetList();
1843 for (Thread* thread : threads) {
1844 for (size_t i = 0; i < kNumThreadLocalSizeBrackets; ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001845 MutexLock brackets_mu(self, *size_bracket_locks_[i]);
Mathieu Chartier0651d412014-04-29 14:37:57 -07001846 Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(i));
1847 CHECK(thread_local_run != nullptr);
1848 CHECK(thread_local_run->IsThreadLocal());
1849 CHECK(thread_local_run == dedicated_full_run_ ||
1850 thread_local_run->size_bracket_idx_ == i);
1851 }
1852 }
1853 for (size_t i = 0; i < kNumOfSizeBrackets; i++) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001854 MutexLock brackets_mu(self, *size_bracket_locks_[i]);
Mathieu Chartier0651d412014-04-29 14:37:57 -07001855 Run* current_run = current_runs_[i];
1856 CHECK(current_run != nullptr);
1857 if (current_run != dedicated_full_run_) {
1858 // The dedicated full run is currently marked as thread local.
1859 CHECK(!current_run->IsThreadLocal());
1860 CHECK_EQ(current_run->size_bracket_idx_, i);
1861 }
1862 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001863 // Call Verify() here for the lock order.
1864 for (auto& run : runs) {
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001865 run->Verify(self, this, is_running_on_memory_tool_);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001866 }
1867}
1868
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001869void RosAlloc::Run::Verify(Thread* self, RosAlloc* rosalloc, bool running_on_memory_tool) {
Ian Rogers5d057052014-03-12 14:32:27 -07001870 DCHECK_EQ(magic_num_, kMagicNum) << "Bad magic number : " << Dump();
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001871 const size_t idx = size_bracket_idx_;
Ian Rogers5d057052014-03-12 14:32:27 -07001872 CHECK_LT(idx, kNumOfSizeBrackets) << "Out of range size bracket index : " << Dump();
Ian Rogers13735952014-10-08 12:43:28 -07001873 uint8_t* slot_base = reinterpret_cast<uint8_t*>(this) + headerSizes[idx];
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001874 const size_t num_slots = numOfSlots[idx];
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001875 size_t bracket_size = IndexToBracketSize(idx);
1876 CHECK_EQ(slot_base + num_slots * bracket_size,
Ian Rogers13735952014-10-08 12:43:28 -07001877 reinterpret_cast<uint8_t*>(this) + numOfPages[idx] * kPageSize)
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001878 << "Mismatch in the end address of the run " << Dump();
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001879 // Check that the bulk free list is empty. It's only used during BulkFree().
1880 CHECK(IsBulkFreeListEmpty()) << "The bulk free isn't empty " << Dump();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001881 // Check the thread local runs, the current runs, and the run sets.
Mathieu Chartier73d1e172014-04-11 17:53:48 -07001882 if (IsThreadLocal()) {
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001883 // If it's a thread local run, then it must be pointed to by an owner thread.
1884 bool owner_found = false;
1885 std::list<Thread*> thread_list = Runtime::Current()->GetThreadList()->GetList();
1886 for (auto it = thread_list.begin(); it != thread_list.end(); ++it) {
1887 Thread* thread = *it;
Mathieu Chartier0651d412014-04-29 14:37:57 -07001888 for (size_t i = 0; i < kNumThreadLocalSizeBrackets; i++) {
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001889 MutexLock mu(self, *rosalloc->size_bracket_locks_[i]);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001890 Run* thread_local_run = reinterpret_cast<Run*>(thread->GetRosAllocRun(i));
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001891 if (thread_local_run == this) {
1892 CHECK(!owner_found)
1893 << "A thread local run has more than one owner thread " << Dump();
1894 CHECK_EQ(i, idx)
1895 << "A mismatching size bracket index in a thread local run " << Dump();
1896 owner_found = true;
1897 }
1898 }
1899 }
1900 CHECK(owner_found) << "A thread local run has no owner thread " << Dump();
1901 } else {
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001902 // If it's not thread local, check that the thread local free list is empty.
1903 CHECK(IsThreadLocalFreeListEmpty())
1904 << "A non-thread-local run's thread local free list isn't empty "
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001905 << Dump();
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001906 // Check if it's a current run for the size bracket.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001907 bool is_current_run = false;
1908 for (size_t i = 0; i < kNumOfSizeBrackets; i++) {
1909 MutexLock mu(self, *rosalloc->size_bracket_locks_[i]);
1910 Run* current_run = rosalloc->current_runs_[i];
1911 if (idx == i) {
1912 if (this == current_run) {
1913 is_current_run = true;
1914 }
1915 } else {
1916 // If the size bucket index does not match, then it must not
1917 // be a current run.
1918 CHECK_NE(this, current_run)
1919 << "A current run points to a run with a wrong size bracket index " << Dump();
1920 }
1921 }
1922 // If it's neither a thread local or current run, then it must be
1923 // in a run set.
1924 if (!is_current_run) {
1925 MutexLock mu(self, rosalloc->lock_);
Mathieu Chartier58553c72014-09-16 16:25:55 -07001926 auto& non_full_runs = rosalloc->non_full_runs_[idx];
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001927 // If it's all free, it must be a free page run rather than a run.
1928 CHECK(!IsAllFree()) << "A free run must be in a free page run set " << Dump();
1929 if (!IsFull()) {
1930 // If it's not full, it must in the non-full run set.
1931 CHECK(non_full_runs.find(this) != non_full_runs.end())
1932 << "A non-full run isn't in the non-full run set " << Dump();
1933 } else {
1934 // If it's full, it must in the full run set (debug build only.)
1935 if (kIsDebugBuild) {
Mathieu Chartier58553c72014-09-16 16:25:55 -07001936 auto& full_runs = rosalloc->full_runs_[idx];
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001937 CHECK(full_runs.find(this) != full_runs.end())
1938 << " A full run isn't in the full run set " << Dump();
1939 }
1940 }
1941 }
1942 }
1943 // Check each slot.
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001944 size_t memory_tool_modifier = running_on_memory_tool ?
1945 2 * ::art::gc::space::kDefaultMemoryToolRedZoneBytes :
Andreas Gamped7576322014-10-24 22:13:45 -07001946 0U;
Hiroshi Yamauchi31bf42c2015-09-24 11:20:29 -07001947 // TODO: reuse InspectAllSlots().
1948 std::unique_ptr<bool[]> is_free(new bool[num_slots]()); // zero initialized
1949 // Mark the free slots and the remaining ones are allocated.
1950 for (Slot* slot = free_list_.Head(); slot != nullptr; slot = slot->Next()) {
1951 size_t slot_idx = SlotIndex(slot);
1952 DCHECK_LT(slot_idx, num_slots);
1953 is_free[slot_idx] = true;
1954 }
1955 if (IsThreadLocal()) {
1956 for (Slot* slot = thread_local_free_list_.Head(); slot != nullptr; slot = slot->Next()) {
1957 size_t slot_idx = SlotIndex(slot);
1958 DCHECK_LT(slot_idx, num_slots);
1959 is_free[slot_idx] = true;
1960 }
1961 }
1962 for (size_t slot_idx = 0; slot_idx < num_slots; ++slot_idx) {
1963 uint8_t* slot_addr = slot_base + slot_idx * bracket_size;
1964 if (running_on_memory_tool) {
1965 slot_addr += ::art::gc::space::kDefaultMemoryToolRedZoneBytes;
1966 }
1967 if (!is_free[slot_idx]) {
1968 // The slot is allocated
1969 mirror::Object* obj = reinterpret_cast<mirror::Object*>(slot_addr);
1970 size_t obj_size = obj->SizeOf();
1971 CHECK_LE(obj_size + memory_tool_modifier, kLargeSizeThreshold)
1972 << "A run slot contains a large object " << Dump();
1973 CHECK_EQ(SizeToIndex(obj_size + memory_tool_modifier), idx)
1974 << PrettyTypeOf(obj) << " "
1975 << "obj_size=" << obj_size << "(" << obj_size + memory_tool_modifier << "), idx=" << idx
1976 << " A run slot contains an object with wrong size " << Dump();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -08001977 }
1978 }
1979}
1980
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -07001981size_t RosAlloc::ReleasePages() {
1982 VLOG(heap) << "RosAlloc::ReleasePages()";
1983 DCHECK(!DoesReleaseAllPages());
1984 Thread* self = Thread::Current();
1985 size_t reclaimed_bytes = 0;
1986 size_t i = 0;
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001987 // Check the page map size which might have changed due to grow/shrink.
1988 while (i < page_map_size_) {
1989 // Reading the page map without a lock is racy but the race is benign since it should only
1990 // result in occasionally not releasing pages which we could release.
Ian Rogers13735952014-10-08 12:43:28 -07001991 uint8_t pm = page_map_[i];
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -07001992 switch (pm) {
Mathieu Chartiere28ed992014-07-10 10:16:44 -07001993 case kPageMapReleased:
1994 // Fall through.
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -07001995 case kPageMapEmpty: {
Mathieu Chartiere28ed992014-07-10 10:16:44 -07001996 // This is currently the start of a free page run.
1997 // Acquire the lock to prevent other threads racing in and modifying the page map.
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07001998 MutexLock mu(self, lock_);
1999 // Check that it's still empty after we acquired the lock since another thread could have
2000 // raced in and placed an allocation here.
Mathieu Chartiere28ed992014-07-10 10:16:44 -07002001 if (IsFreePage(i)) {
2002 // Free page runs can start with a released page if we coalesced a released page free
2003 // page run with an empty page run.
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07002004 FreePageRun* fpr = reinterpret_cast<FreePageRun*>(base_ + i * kPageSize);
Mathieu Chartiere28ed992014-07-10 10:16:44 -07002005 // There is a race condition where FreePage can coalesce fpr with the previous
2006 // free page run before we acquire lock_. In that case free_page_runs_.find will not find
2007 // a run starting at fpr. To handle this race, we skip reclaiming the page range and go
2008 // to the next page.
2009 if (free_page_runs_.find(fpr) != free_page_runs_.end()) {
2010 size_t fpr_size = fpr->ByteSize(this);
Roland Levillain14d90572015-07-16 10:52:26 +01002011 DCHECK_ALIGNED(fpr_size, kPageSize);
Ian Rogers13735952014-10-08 12:43:28 -07002012 uint8_t* start = reinterpret_cast<uint8_t*>(fpr);
Mathieu Chartiere28ed992014-07-10 10:16:44 -07002013 reclaimed_bytes += ReleasePageRange(start, start + fpr_size);
2014 size_t pages = fpr_size / kPageSize;
2015 CHECK_GT(pages, 0U) << "Infinite loop probable";
2016 i += pages;
2017 DCHECK_LE(i, page_map_size_);
2018 break;
2019 }
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -07002020 }
Ian Rogersfc787ec2014-10-09 21:56:44 -07002021 FALLTHROUGH_INTENDED;
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -07002022 }
2023 case kPageMapLargeObject: // Fall through.
2024 case kPageMapLargeObjectPart: // Fall through.
2025 case kPageMapRun: // Fall through.
2026 case kPageMapRunPart: // Fall through.
2027 ++i;
2028 break; // Skip.
2029 default:
Maxim Kazantsev2fdeecb2014-10-16 10:55:47 +07002030 LOG(FATAL) << "Unreachable - page map type: " << static_cast<int>(pm);
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -07002031 break;
2032 }
2033 }
2034 return reclaimed_bytes;
2035}
2036
Ian Rogers13735952014-10-08 12:43:28 -07002037size_t RosAlloc::ReleasePageRange(uint8_t* start, uint8_t* end) {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07002038 DCHECK_ALIGNED(start, kPageSize);
2039 DCHECK_ALIGNED(end, kPageSize);
2040 DCHECK_LT(start, end);
2041 if (kIsDebugBuild) {
2042 // In the debug build, the first page of a free page run
2043 // contains a magic number for debugging. Exclude it.
2044 start += kPageSize;
Andreas Gamped7576322014-10-24 22:13:45 -07002045
2046 // Single pages won't be released.
2047 if (start == end) {
2048 return 0;
2049 }
Mathieu Chartiera5b5c552014-06-24 14:48:59 -07002050 }
2051 if (!kMadviseZeroes) {
2052 // TODO: Do this when we resurrect the page instead.
2053 memset(start, 0, end - start);
2054 }
2055 CHECK_EQ(madvise(start, end - start, MADV_DONTNEED), 0);
2056 size_t pm_idx = ToPageMapIndex(start);
2057 size_t reclaimed_bytes = 0;
2058 // Calculate reclaimed bytes and upate page map.
2059 const size_t max_idx = pm_idx + (end - start) / kPageSize;
2060 for (; pm_idx < max_idx; ++pm_idx) {
2061 DCHECK(IsFreePage(pm_idx));
2062 if (page_map_[pm_idx] == kPageMapEmpty) {
2063 // Mark the page as released and update how many bytes we released.
2064 reclaimed_bytes += kPageSize;
2065 page_map_[pm_idx] = kPageMapReleased;
2066 }
2067 }
2068 return reclaimed_bytes;
2069}
2070
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -07002071void RosAlloc::LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) {
2072 Thread* self = Thread::Current();
2073 size_t largest_continuous_free_pages = 0;
2074 WriterMutexLock wmu(self, bulk_free_lock_);
2075 MutexLock mu(self, lock_);
2076 for (FreePageRun* fpr : free_page_runs_) {
2077 largest_continuous_free_pages = std::max(largest_continuous_free_pages,
2078 fpr->ByteSize(this));
2079 }
2080 if (failed_alloc_bytes > kLargeSizeThreshold) {
2081 // Large allocation.
2082 size_t required_bytes = RoundUp(failed_alloc_bytes, kPageSize);
2083 if (required_bytes > largest_continuous_free_pages) {
2084 os << "; failed due to fragmentation (required continguous free "
2085 << required_bytes << " bytes where largest contiguous free "
2086 << largest_continuous_free_pages << " bytes)";
2087 }
2088 } else {
2089 // Non-large allocation.
2090 size_t required_bytes = numOfPages[SizeToIndex(failed_alloc_bytes)] * kPageSize;
2091 if (required_bytes > largest_continuous_free_pages) {
2092 os << "; failed due to fragmentation (required continguous free "
2093 << required_bytes << " bytes for a new buffer where largest contiguous free "
2094 << largest_continuous_free_pages << " bytes)";
2095 }
2096 }
2097}
2098
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07002099} // namespace allocator
2100} // namespace gc
2101} // namespace art