blob: 431686a97724b2cd17a8ce560e97945bdf57ac1d [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
17#ifndef ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_
18#define ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_
19
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070020#include <stdint.h>
21#include <stdlib.h>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070022#include <sys/mman.h>
Ian Rogers700a4022014-05-19 16:49:03 -070023#include <memory>
24#include <set>
25#include <string>
26#include <unordered_set>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070027#include <vector>
28
Mathieu Chartier58553c72014-09-16 16:25:55 -070029#include "base/allocator.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070030#include "base/mutex.h"
31#include "base/logging.h"
32#include "globals.h"
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080033#include "mem_map.h"
Ian Rogerse63db272014-07-15 15:36:11 -070034#include "thread.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070035#include "utils.h"
36
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070037namespace art {
38namespace gc {
39namespace allocator {
40
Ian Rogers6fac4472014-02-25 17:01:10 -080041// A runs-of-slots memory allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070042class RosAlloc {
43 private:
Ian Rogers6fac4472014-02-25 17:01:10 -080044 // Represents a run of free pages.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070045 class FreePageRun {
46 public:
Ian Rogers13735952014-10-08 12:43:28 -070047 uint8_t magic_num_; // The magic number used for debugging only.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070048
49 bool IsFree() const {
Mathieu Chartiera1c1c712014-06-23 17:53:09 -070050 return !kIsDebugBuild || magic_num_ == kMagicNumFree;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070051 }
52 size_t ByteSize(RosAlloc* rosalloc) const EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
Ian Rogers13735952014-10-08 12:43:28 -070053 const uint8_t* fpr_base = reinterpret_cast<const uint8_t*>(this);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070054 size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base);
55 size_t byte_size = rosalloc->free_page_run_size_map_[pm_idx];
56 DCHECK_GE(byte_size, static_cast<size_t>(0));
Mathieu Chartier58553c72014-09-16 16:25:55 -070057 DCHECK_ALIGNED(byte_size, kPageSize);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070058 return byte_size;
59 }
60 void SetByteSize(RosAlloc* rosalloc, size_t byte_size)
61 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
62 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
Ian Rogers13735952014-10-08 12:43:28 -070063 uint8_t* fpr_base = reinterpret_cast<uint8_t*>(this);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070064 size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base);
65 rosalloc->free_page_run_size_map_[pm_idx] = byte_size;
66 }
67 void* Begin() {
68 return reinterpret_cast<void*>(this);
69 }
70 void* End(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
Ian Rogers13735952014-10-08 12:43:28 -070071 uint8_t* fpr_base = reinterpret_cast<uint8_t*>(this);
72 uint8_t* end = fpr_base + ByteSize(rosalloc);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070073 return end;
74 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080075 bool IsLargerThanPageReleaseThreshold(RosAlloc* rosalloc)
76 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
77 return ByteSize(rosalloc) >= rosalloc->page_release_size_threshold_;
78 }
79 bool IsAtEndOfSpace(RosAlloc* rosalloc)
80 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
Ian Rogers13735952014-10-08 12:43:28 -070081 return reinterpret_cast<uint8_t*>(this) + ByteSize(rosalloc) == rosalloc->base_ + rosalloc->footprint_;
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080082 }
83 bool ShouldReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
84 switch (rosalloc->page_release_mode_) {
85 case kPageReleaseModeNone:
86 return false;
87 case kPageReleaseModeEnd:
88 return IsAtEndOfSpace(rosalloc);
89 case kPageReleaseModeSize:
90 return IsLargerThanPageReleaseThreshold(rosalloc);
91 case kPageReleaseModeSizeAndEnd:
92 return IsLargerThanPageReleaseThreshold(rosalloc) && IsAtEndOfSpace(rosalloc);
93 case kPageReleaseModeAll:
94 return true;
95 default:
96 LOG(FATAL) << "Unexpected page release mode ";
97 return false;
98 }
99 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700100 void ReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
Ian Rogers13735952014-10-08 12:43:28 -0700101 uint8_t* start = reinterpret_cast<uint8_t*>(this);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700102 size_t byte_size = ByteSize(rosalloc);
103 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700104 if (ShouldReleasePages(rosalloc)) {
105 rosalloc->ReleasePageRange(start, start + byte_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700106 }
107 }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(FreePageRun);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700111 };
112
113 // Represents a run of memory slots of the same size.
114 //
115 // A run's memory layout:
116 //
117 // +-------------------+
118 // | magic_num |
119 // +-------------------+
120 // | size_bracket_idx |
121 // +-------------------+
122 // | is_thread_local |
123 // +-------------------+
124 // | to_be_bulk_freed |
125 // +-------------------+
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700126 // | top_bitmap_idx |
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700127 // +-------------------+
128 // | |
129 // | alloc bit map |
130 // | |
131 // +-------------------+
132 // | |
133 // | bulk free bit map |
134 // | |
135 // +-------------------+
136 // | |
137 // | thread-local free |
138 // | bit map |
139 // | |
140 // +-------------------+
141 // | padding due to |
142 // | alignment |
143 // +-------------------+
144 // | slot 0 |
145 // +-------------------+
146 // | slot 1 |
147 // +-------------------+
148 // | slot 2 |
149 // +-------------------+
150 // ...
151 // +-------------------+
152 // | last slot |
153 // +-------------------+
154 //
155 class Run {
156 public:
Ian Rogers13735952014-10-08 12:43:28 -0700157 uint8_t magic_num_; // The magic number used for debugging.
158 uint8_t size_bracket_idx_; // The index of the size bracket of this run.
159 uint8_t is_thread_local_; // True if this run is used as a thread-local run.
160 uint8_t to_be_bulk_freed_; // Used within BulkFree() to flag a run that's involved with a bulk free.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700161 uint32_t first_search_vec_idx_; // The index of the first bitmap vector which may contain an available slot.
162 uint32_t alloc_bit_map_[0]; // The bit map that allocates if each slot is in use.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700163
164 // bulk_free_bit_map_[] : The bit map that is used for GC to
165 // temporarily mark the slots to free without using a lock. After
166 // all the slots to be freed in a run are marked, all those slots
167 // get freed in bulk with one locking per run, as opposed to one
168 // locking per slot to minimize the lock contention. This is used
169 // within BulkFree().
170
171 // thread_local_free_bit_map_[] : The bit map that is used for GC
172 // to temporarily mark the slots to free in a thread-local run
173 // without using a lock (without synchronizing the thread that
174 // owns the thread-local run.) When the thread-local run becomes
175 // full, the thread will check this bit map and update the
176 // allocation bit map of the run (that is, the slots get freed.)
177
178 // Returns the byte size of the header except for the bit maps.
179 static size_t fixed_header_size() {
180 Run temp;
Ian Rogers13735952014-10-08 12:43:28 -0700181 size_t size = reinterpret_cast<uint8_t*>(&temp.alloc_bit_map_) - reinterpret_cast<uint8_t*>(&temp);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700182 DCHECK_EQ(size, static_cast<size_t>(8));
183 return size;
184 }
185 // Returns the base address of the free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800186 uint32_t* BulkFreeBitMap() {
Ian Rogers13735952014-10-08 12:43:28 -0700187 return reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(this) + bulkFreeBitMapOffsets[size_bracket_idx_]);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700188 }
189 // Returns the base address of the thread local free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800190 uint32_t* ThreadLocalFreeBitMap() {
Ian Rogers13735952014-10-08 12:43:28 -0700191 return reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(this) + threadLocalFreeBitMapOffsets[size_bracket_idx_]);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700192 }
193 void* End() {
Ian Rogers13735952014-10-08 12:43:28 -0700194 return reinterpret_cast<uint8_t*>(this) + kPageSize * numOfPages[size_bracket_idx_];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700195 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700196 // Returns the number of bitmap words per run.
197 size_t NumberOfBitmapVectors() const {
198 return RoundUp(numOfSlots[size_bracket_idx_], 32) / 32;
199 }
200 void SetIsThreadLocal(bool is_thread_local) {
201 is_thread_local_ = is_thread_local ? 1 : 0;
202 }
203 bool IsThreadLocal() const {
204 return is_thread_local_ != 0;
205 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700206 // Frees slots in the allocation bit map with regard to the
207 // thread-local free bit map. Used when a thread-local run becomes
208 // full.
209 bool MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out);
210 // Frees slots in the allocation bit map with regard to the bulk
211 // free bit map. Used in a bulk free.
212 void MergeBulkFreeBitMapIntoAllocBitMap();
213 // Unions the slots to be freed in the free bit map into the
214 // thread-local free bit map. In a bulk free, as a two-step
215 // process, GC will first record all the slots to free in a run in
216 // the free bit map where it can write without a lock, and later
217 // acquire a lock once per run to union the bits of the free bit
218 // map to the thread-local free bit map.
219 void UnionBulkFreeBitMapToThreadLocalFreeBitMap();
220 // Allocates a slot in a run.
221 void* AllocSlot();
222 // Frees a slot in a run. This is used in a non-bulk free.
223 void FreeSlot(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700224 // Marks the slots to free in the bulk free bit map. Returns the bracket size.
225 size_t MarkBulkFreeBitMap(void* ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700226 // Marks the slots to free in the thread-local free bit map.
227 void MarkThreadLocalFreeBitMap(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700228 // Last word mask, all of the bits in the last word which aren't valid slots are set to
229 // optimize allocation path.
Andreas Gampe59e67602014-04-25 17:15:12 -0700230 static uint32_t GetBitmapLastVectorMask(size_t num_slots, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700231 // Returns true if all the slots in the run are not in use.
232 bool IsAllFree();
233 // Returns true if all the slots in the run are in use.
234 bool IsFull();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800235 // Returns true if the bulk free bit map is clean.
236 bool IsBulkFreeBitmapClean();
237 // Returns true if the thread local free bit map is clean.
238 bool IsThreadLocalFreeBitmapClean();
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700239 // Set the alloc_bit_map_ bits for slots that are past the end of the run.
240 void SetAllocBitMapBitsForInvalidSlots();
241 // Zero the run's data.
242 void ZeroData();
243 // Zero the run's header.
244 void ZeroHeader();
245 // Fill the alloc bitmap with 1s.
246 void FillAllocBitMap();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700247 // Iterate over all the slots and apply the given function.
248 void InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), void* arg);
249 // Dump the run metadata for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800250 std::string Dump();
251 // Verify for debugging.
Andreas Gamped7576322014-10-24 22:13:45 -0700252 void Verify(Thread* self, RosAlloc* rosalloc, bool running_on_valgrind)
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800253 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
254 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700255
256 private:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700257 // The common part of MarkFreeBitMap() and MarkThreadLocalFreeBitMap(). Returns the bracket
258 // size.
259 size_t MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, const char* caller_name);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800260 // Turns the bit map into a string for debugging.
261 static std::string BitMapToStr(uint32_t* bit_map_base, size_t num_vec);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700262
263 // TODO: DISALLOW_COPY_AND_ASSIGN(Run);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700264 };
265
266 // The magic number for a run.
Ian Rogers13735952014-10-08 12:43:28 -0700267 static constexpr uint8_t kMagicNum = 42;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700268 // The magic number for free pages.
Ian Rogers13735952014-10-08 12:43:28 -0700269 static constexpr uint8_t kMagicNumFree = 43;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700270 // The number of size brackets. Sync this with the length of Thread::rosalloc_runs_.
Ian Rogers13735952014-10-08 12:43:28 -0700271 static constexpr size_t kNumOfSizeBrackets = kNumRosAllocThreadLocalSizeBrackets;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700272 // The number of smaller size brackets that are 16 bytes apart.
Ian Rogers13735952014-10-08 12:43:28 -0700273 static constexpr size_t kNumOfQuantumSizeBrackets = 32;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700274 // The sizes (the slot sizes, in bytes) of the size brackets.
275 static size_t bracketSizes[kNumOfSizeBrackets];
276 // The numbers of pages that are used for runs for each size bracket.
277 static size_t numOfPages[kNumOfSizeBrackets];
278 // The numbers of slots of the runs for each size bracket.
279 static size_t numOfSlots[kNumOfSizeBrackets];
280 // The header sizes in bytes of the runs for each size bracket.
281 static size_t headerSizes[kNumOfSizeBrackets];
282 // The byte offsets of the bulk free bit maps of the runs for each size bracket.
283 static size_t bulkFreeBitMapOffsets[kNumOfSizeBrackets];
284 // The byte offsets of the thread-local free bit maps of the runs for each size bracket.
285 static size_t threadLocalFreeBitMapOffsets[kNumOfSizeBrackets];
286
287 // Initialize the run specs (the above arrays).
288 static void Initialize();
289 static bool initialized_;
290
291 // Returns the byte size of the bracket size from the index.
292 static size_t IndexToBracketSize(size_t idx) {
Ian Rogers59c07062014-10-10 13:03:39 -0700293 DCHECK_LT(idx, kNumOfSizeBrackets);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700294 return bracketSizes[idx];
295 }
296 // Returns the index of the size bracket from the bracket size.
297 static size_t BracketSizeToIndex(size_t size) {
298 DCHECK(16 <= size && ((size < 1 * KB && size % 16 == 0) || size == 1 * KB || size == 2 * KB));
299 size_t idx;
300 if (UNLIKELY(size == 1 * KB)) {
301 idx = kNumOfSizeBrackets - 2;
302 } else if (UNLIKELY(size == 2 * KB)) {
303 idx = kNumOfSizeBrackets - 1;
304 } else {
305 DCHECK(size < 1 * KB);
306 DCHECK_EQ(size % 16, static_cast<size_t>(0));
307 idx = size / 16 - 1;
308 }
309 DCHECK(bracketSizes[idx] == size);
310 return idx;
311 }
312 // Rounds up the size up the nearest bracket size.
313 static size_t RoundToBracketSize(size_t size) {
314 DCHECK(size <= kLargeSizeThreshold);
315 if (LIKELY(size <= 512)) {
316 return RoundUp(size, 16);
317 } else if (512 < size && size <= 1 * KB) {
318 return 1 * KB;
319 } else {
320 DCHECK(1 * KB < size && size <= 2 * KB);
321 return 2 * KB;
322 }
323 }
324 // Returns the size bracket index from the byte size with rounding.
325 static size_t SizeToIndex(size_t size) {
326 DCHECK(size <= kLargeSizeThreshold);
327 if (LIKELY(size <= 512)) {
328 return RoundUp(size, 16) / 16 - 1;
329 } else if (512 < size && size <= 1 * KB) {
330 return kNumOfSizeBrackets - 2;
331 } else {
332 DCHECK(1 * KB < size && size <= 2 * KB);
333 return kNumOfSizeBrackets - 1;
334 }
335 }
336 // A combination of SizeToIndex() and RoundToBracketSize().
337 static size_t SizeToIndexAndBracketSize(size_t size, size_t* bracket_size_out) {
338 DCHECK(size <= kLargeSizeThreshold);
339 if (LIKELY(size <= 512)) {
340 size_t bracket_size = RoundUp(size, 16);
341 *bracket_size_out = bracket_size;
342 size_t idx = bracket_size / 16 - 1;
343 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
344 return idx;
345 } else if (512 < size && size <= 1 * KB) {
346 size_t bracket_size = 1024;
347 *bracket_size_out = bracket_size;
348 size_t idx = kNumOfSizeBrackets - 2;
349 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
350 return idx;
351 } else {
352 DCHECK(1 * KB < size && size <= 2 * KB);
353 size_t bracket_size = 2048;
354 *bracket_size_out = bracket_size;
355 size_t idx = kNumOfSizeBrackets - 1;
356 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
357 return idx;
358 }
359 }
360 // Returns the page map index from an address. Requires that the
361 // address is page size aligned.
362 size_t ToPageMapIndex(const void* addr) const {
Andreas Gamped7576322014-10-24 22:13:45 -0700363 DCHECK_LE(base_, addr);
364 DCHECK_LT(addr, base_ + capacity_);
Ian Rogers13735952014-10-08 12:43:28 -0700365 size_t byte_offset = reinterpret_cast<const uint8_t*>(addr) - base_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700366 DCHECK_EQ(byte_offset % static_cast<size_t>(kPageSize), static_cast<size_t>(0));
367 return byte_offset / kPageSize;
368 }
369 // Returns the page map index from an address with rounding.
Andreas Gamped7576322014-10-24 22:13:45 -0700370 size_t RoundDownToPageMapIndex(const void* addr) const {
Ian Rogers13735952014-10-08 12:43:28 -0700371 DCHECK(base_ <= addr && addr < reinterpret_cast<uint8_t*>(base_) + capacity_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700372 return (reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(base_)) / kPageSize;
373 }
374
375 // A memory allocation request larger than this size is treated as a large object and allocated
376 // at a page-granularity.
377 static const size_t kLargeSizeThreshold = 2048;
378
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800379 // If true, check that the returned memory is actually zero.
380 static constexpr bool kCheckZeroMemory = kIsDebugBuild;
Andreas Gamped7576322014-10-24 22:13:45 -0700381 // Valgrind protects memory, so do not check memory when running under valgrind. In a normal
382 // build with kCheckZeroMemory the whole test should be optimized away.
383 // TODO: Unprotect before checks.
384 ALWAYS_INLINE bool ShouldCheckZeroMemory();
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800385
386 // If true, log verbose details of operations.
387 static constexpr bool kTraceRosAlloc = false;
388
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700389 struct hash_run {
390 size_t operator()(const RosAlloc::Run* r) const {
391 return reinterpret_cast<size_t>(r);
392 }
393 };
394
395 struct eq_run {
396 bool operator()(const RosAlloc::Run* r1, const RosAlloc::Run* r2) const {
397 return r1 == r2;
398 }
399 };
400
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800401 public:
402 // Different page release modes.
403 enum PageReleaseMode {
404 kPageReleaseModeNone, // Release no empty pages.
405 kPageReleaseModeEnd, // Release empty pages at the end of the space.
406 kPageReleaseModeSize, // Release empty pages that are larger than the threshold.
407 kPageReleaseModeSizeAndEnd, // Release empty pages that are larger than the threshold or
408 // at the end of the space.
409 kPageReleaseModeAll, // Release all empty pages.
410 };
411
412 // The default value for page_release_size_threshold_.
413 static constexpr size_t kDefaultPageReleaseSizeThreshold = 4 * MB;
414
Mathieu Chartier0651d412014-04-29 14:37:57 -0700415 // We use thread-local runs for the size Brackets whose indexes
416 // are less than this index. We use shared (current) runs for the rest.
Mathieu Chartier58553c72014-09-16 16:25:55 -0700417
Mathieu Chartier0651d412014-04-29 14:37:57 -0700418 static const size_t kNumThreadLocalSizeBrackets = 11;
419
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800420 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700421 // The base address of the memory region that's managed by this allocator.
Ian Rogers13735952014-10-08 12:43:28 -0700422 uint8_t* base_;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700423
424 // The footprint in bytes of the currently allocated portion of the
425 // memory region.
426 size_t footprint_;
427
428 // The maximum footprint. The address, base_ + capacity_, indicates
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800429 // the end of the memory region that's currently managed by this allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700430 size_t capacity_;
431
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800432 // The maximum capacity. The address, base_ + max_capacity_, indicates
433 // the end of the memory region that's ever managed by this allocator.
434 size_t max_capacity_;
435
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700436 // The run sets that hold the runs whose slots are not all
437 // full. non_full_runs_[i] is guarded by size_bracket_locks_[i].
Mathieu Chartier58553c72014-09-16 16:25:55 -0700438 AllocationTrackingSet<Run*, kAllocatorTagRosAlloc> non_full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700439 // The run sets that hold the runs whose slots are all full. This is
440 // debug only. full_runs_[i] is guarded by size_bracket_locks_[i].
Mathieu Chartier58553c72014-09-16 16:25:55 -0700441 std::unordered_set<Run*, hash_run, eq_run, TrackingAllocator<Run*, kAllocatorTagRosAlloc>>
442 full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700443 // The set of free pages.
Mathieu Chartier58553c72014-09-16 16:25:55 -0700444 AllocationTrackingSet<FreePageRun*, kAllocatorTagRosAlloc> free_page_runs_ GUARDED_BY(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700445 // The dedicated full run, it is always full and shared by all threads when revoking happens.
446 // This is an optimization since enables us to avoid a null check for revoked runs.
447 static Run* dedicated_full_run_;
448 // Using size_t to ensure that it is at least word aligned.
449 static size_t dedicated_full_run_storage_[];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700450 // The current runs where the allocations are first attempted for
451 // the size brackes that do not use thread-local
452 // runs. current_runs_[i] is guarded by size_bracket_locks_[i].
453 Run* current_runs_[kNumOfSizeBrackets];
454 // The mutexes, one per size bracket.
455 Mutex* size_bracket_locks_[kNumOfSizeBrackets];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700456 // Bracket lock names (since locks only have char* names).
Zuo Wangf37a88b2014-07-10 04:26:41 -0700457 std::string size_bracket_lock_names_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700458 // The types of page map entries.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700459 enum PageMapKind {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700460 kPageMapReleased = 0, // Zero and released back to the OS.
461 kPageMapEmpty, // Zero but probably dirty.
462 kPageMapRun, // The beginning of a run.
463 kPageMapRunPart, // The non-beginning part of a run.
464 kPageMapLargeObject, // The beginning of a large object.
465 kPageMapLargeObjectPart, // The non-beginning part of a large object.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700466 };
467 // The table that indicates what pages are currently used for.
Ian Rogers13735952014-10-08 12:43:28 -0700468 volatile uint8_t* page_map_; // No GUARDED_BY(lock_) for kReadPageMapEntryWithoutLockInBulkFree.
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800469 size_t page_map_size_;
470 size_t max_page_map_size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700471 std::unique_ptr<MemMap> page_map_mem_map_;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800472
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700473 // The table that indicates the size of free page runs. These sizes
474 // are stored here to avoid storing in the free page header and
475 // release backing pages.
Mathieu Chartier58553c72014-09-16 16:25:55 -0700476 std::vector<size_t, TrackingAllocator<size_t, kAllocatorTagRosAlloc>> free_page_run_size_map_
477 GUARDED_BY(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700478 // The global lock. Used to guard the page map, the free page set,
479 // and the footprint.
480 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
481 // The reader-writer lock to allow one bulk free at a time while
Hiroshi Yamauchi70f60042014-02-03 12:31:29 -0800482 // allowing multiple individual frees at the same time. Also, this
483 // is used to avoid race conditions between BulkFree() and
484 // RevokeThreadLocalRuns() on the bulk free bitmaps.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700485 ReaderWriterMutex bulk_free_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
486
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800487 // The page release mode.
488 const PageReleaseMode page_release_mode_;
489 // Under kPageReleaseModeSize(AndEnd), if the free page run size is
490 // greater than or equal to this value, release pages.
491 const size_t page_release_size_threshold_;
492
Andreas Gamped7576322014-10-24 22:13:45 -0700493 // Whether this allocator is running under Valgrind.
494 bool running_on_valgrind_;
495
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700496 // The base address of the memory region that's managed by this allocator.
Ian Rogers13735952014-10-08 12:43:28 -0700497 uint8_t* Begin() { return base_; }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700498 // The end address of the memory region that's managed by this allocator.
Ian Rogers13735952014-10-08 12:43:28 -0700499 uint8_t* End() { return base_ + capacity_; }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700500
501 // Page-granularity alloc/free
Ian Rogers13735952014-10-08 12:43:28 -0700502 void* AllocPages(Thread* self, size_t num_pages, uint8_t page_map_type)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700503 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700504 // Returns how many bytes were freed.
505 size_t FreePages(Thread* self, void* ptr, bool already_zero) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700506
507 // Allocate/free a run slot.
508 void* AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated)
509 LOCKS_EXCLUDED(lock_);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700510 // Allocate/free a run slot without acquiring locks.
511 // TODO: EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
512 void* AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated)
513 LOCKS_EXCLUDED(lock_);
514 void* AllocFromCurrentRunUnlocked(Thread* self, size_t idx);
515
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700516 // Returns the bracket size.
517 size_t FreeFromRun(Thread* self, void* ptr, Run* run)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700518 LOCKS_EXCLUDED(lock_);
519
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700520 // Used to allocate a new thread local run for a size bracket.
521 Run* AllocRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
522
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700523 // Used to acquire a new/reused run for a size bracket. Used when a
524 // thread-local or current run gets full.
525 Run* RefillRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
526
527 // The internal of non-bulk Free().
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700528 size_t FreeInternal(Thread* self, void* ptr) LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700529
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800530 // Allocates large objects.
531 void* AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated) LOCKS_EXCLUDED(lock_);
532
Mathieu Chartier0651d412014-04-29 14:37:57 -0700533 // Revoke a run by adding it to non_full_runs_ or freeing the pages.
534 void RevokeRun(Thread* self, size_t idx, Run* run);
535
536 // Revoke the current runs which share an index with the thread local runs.
537 void RevokeThreadUnsafeCurrentRuns();
538
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700539 // Release a range of pages.
Ian Rogers13735952014-10-08 12:43:28 -0700540 size_t ReleasePageRange(uint8_t* start, uint8_t* end) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700541
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700542 // Dumps the page map for debugging.
543 std::string DumpPageMap() EXCLUSIVE_LOCKS_REQUIRED(lock_);
544
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700545 public:
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800546 RosAlloc(void* base, size_t capacity, size_t max_capacity,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800547 PageReleaseMode page_release_mode,
Andreas Gamped7576322014-10-24 22:13:45 -0700548 bool running_on_valgrind,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800549 size_t page_release_size_threshold = kDefaultPageReleaseSizeThreshold);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800550 ~RosAlloc();
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700551
Mathieu Chartier0651d412014-04-29 14:37:57 -0700552 // If kThreadUnsafe is true then the allocator may avoid acquiring some locks as an optimization.
553 // If used, this may cause race conditions if multiple threads are allocating at the same time.
554 template<bool kThreadSafe = true>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700555 void* Alloc(Thread* self, size_t size, size_t* bytes_allocated)
556 LOCKS_EXCLUDED(lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700557 size_t Free(Thread* self, void* ptr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700558 LOCKS_EXCLUDED(bulk_free_lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700559 size_t BulkFree(Thread* self, void** ptrs, size_t num_ptrs)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700560 LOCKS_EXCLUDED(bulk_free_lock_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700561
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700562 // Returns the size of the allocated slot for a given allocated memory chunk.
Andreas Gamped7576322014-10-24 22:13:45 -0700563 size_t UsableSize(const void* ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700564 // Returns the size of the allocated slot for a given size.
565 size_t UsableSize(size_t bytes) {
566 if (UNLIKELY(bytes > kLargeSizeThreshold)) {
567 return RoundUp(bytes, kPageSize);
568 } else {
569 return RoundToBracketSize(bytes);
570 }
571 }
572 // Try to reduce the current footprint by releasing the free page
573 // run at the end of the memory region, if any.
574 bool Trim();
575 // Iterates over all the memory slots and apply the given function.
576 void InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
577 void* arg)
578 LOCKS_EXCLUDED(lock_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700579
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700580 // Release empty pages.
581 size_t ReleasePages() LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700582 // Returns the current footprint.
583 size_t Footprint() LOCKS_EXCLUDED(lock_);
584 // Returns the current capacity, maximum footprint.
585 size_t FootprintLimit() LOCKS_EXCLUDED(lock_);
586 // Update the current capacity.
587 void SetFootprintLimit(size_t bytes) LOCKS_EXCLUDED(lock_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700588
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700589 // Releases the thread-local runs assigned to the given thread back to the common set of runs.
590 void RevokeThreadLocalRuns(Thread* thread);
591 // Releases the thread-local runs assigned to all the threads back to the common set of runs.
592 void RevokeAllThreadLocalRuns() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700593 // Assert the thread local runs of a thread are revoked.
594 void AssertThreadLocalRunsAreRevoked(Thread* thread);
595 // Assert all the thread local runs are revoked.
596 void AssertAllThreadLocalRunsAreRevoked() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700597
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700598 static Run* GetDedicatedFullRun() {
599 return dedicated_full_run_;
600 }
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700601 bool IsFreePage(size_t idx) const {
602 DCHECK_LT(idx, capacity_ / kPageSize);
Ian Rogers13735952014-10-08 12:43:28 -0700603 uint8_t pm_type = page_map_[idx];
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700604 return pm_type == kPageMapReleased || pm_type == kPageMapEmpty;
605 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700606
607 // Callbacks for InspectAll that will count the number of bytes
608 // allocated and objects allocated, respectively.
609 static void BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
610 static void ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800611
612 bool DoesReleaseAllPages() const {
613 return page_release_mode_ == kPageReleaseModeAll;
614 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800615
616 // Verify for debugging.
617 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700618
619 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700620
621 private:
622 friend std::ostream& operator<<(std::ostream& os, const RosAlloc::PageMapKind& rhs);
623
624 DISALLOW_COPY_AND_ASSIGN(RosAlloc);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700625};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700626std::ostream& operator<<(std::ostream& os, const RosAlloc::PageMapKind& rhs);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700627
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800628// Callback from rosalloc when it needs to increase the footprint. Must be implemented somewhere
629// else (currently rosalloc_space.cc).
630void* ArtRosAllocMoreCore(allocator::RosAlloc* rosalloc, intptr_t increment);
631
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700632} // namespace allocator
633} // namespace gc
634} // namespace art
635
636#endif // ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_