blob: 13f61ec9352aa8c069944bb2823a53db9f076538 [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
29#include "base/mutex.h"
30#include "base/logging.h"
31#include "globals.h"
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -080032#include "mem_map.h"
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070033#include "utils.h"
34
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070035namespace art {
36namespace gc {
37namespace allocator {
38
Ian Rogers6fac4472014-02-25 17:01:10 -080039// A runs-of-slots memory allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070040class RosAlloc {
41 private:
Ian Rogers6fac4472014-02-25 17:01:10 -080042 // Represents a run of free pages.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070043 class FreePageRun {
44 public:
45 byte magic_num_; // The magic number used for debugging only.
46
47 bool IsFree() const {
Mathieu Chartiera1c1c712014-06-23 17:53:09 -070048 return !kIsDebugBuild || magic_num_ == kMagicNumFree;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070049 }
50 size_t ByteSize(RosAlloc* rosalloc) const EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
51 const byte* fpr_base = reinterpret_cast<const byte*>(this);
52 size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base);
53 size_t byte_size = rosalloc->free_page_run_size_map_[pm_idx];
54 DCHECK_GE(byte_size, static_cast<size_t>(0));
55 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
56 return byte_size;
57 }
58 void SetByteSize(RosAlloc* rosalloc, size_t byte_size)
59 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
60 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
61 byte* fpr_base = reinterpret_cast<byte*>(this);
62 size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base);
63 rosalloc->free_page_run_size_map_[pm_idx] = byte_size;
64 }
65 void* Begin() {
66 return reinterpret_cast<void*>(this);
67 }
68 void* End(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
69 byte* fpr_base = reinterpret_cast<byte*>(this);
70 byte* end = fpr_base + ByteSize(rosalloc);
71 return end;
72 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080073 bool IsLargerThanPageReleaseThreshold(RosAlloc* rosalloc)
74 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
75 return ByteSize(rosalloc) >= rosalloc->page_release_size_threshold_;
76 }
77 bool IsAtEndOfSpace(RosAlloc* rosalloc)
78 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
79 return reinterpret_cast<byte*>(this) + ByteSize(rosalloc) == rosalloc->base_ + rosalloc->footprint_;
80 }
81 bool ShouldReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
82 switch (rosalloc->page_release_mode_) {
83 case kPageReleaseModeNone:
84 return false;
85 case kPageReleaseModeEnd:
86 return IsAtEndOfSpace(rosalloc);
87 case kPageReleaseModeSize:
88 return IsLargerThanPageReleaseThreshold(rosalloc);
89 case kPageReleaseModeSizeAndEnd:
90 return IsLargerThanPageReleaseThreshold(rosalloc) && IsAtEndOfSpace(rosalloc);
91 case kPageReleaseModeAll:
92 return true;
93 default:
94 LOG(FATAL) << "Unexpected page release mode ";
95 return false;
96 }
97 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070098 void ReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080099 byte* start = reinterpret_cast<byte*>(this);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700100 size_t byte_size = ByteSize(rosalloc);
101 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800102 bool release_pages = ShouldReleasePages(rosalloc);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700103 if (kIsDebugBuild) {
104 // Exclude the first page that stores the magic number.
105 DCHECK_GE(byte_size, static_cast<size_t>(kPageSize));
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800106 start += kPageSize;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700107 byte_size -= kPageSize;
108 if (byte_size > 0) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800109 if (release_pages) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700110 if (!kMadviseZeroes) {
111 memset(start, 0, byte_size);
112 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800113 madvise(start, byte_size, MADV_DONTNEED);
114 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700115 }
116 } else {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800117 if (release_pages) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700118 if (!kMadviseZeroes) {
119 memset(start, 0, byte_size);
120 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800121 madvise(start, byte_size, MADV_DONTNEED);
122 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700123 }
124 }
125 };
126
127 // Represents a run of memory slots of the same size.
128 //
129 // A run's memory layout:
130 //
131 // +-------------------+
132 // | magic_num |
133 // +-------------------+
134 // | size_bracket_idx |
135 // +-------------------+
136 // | is_thread_local |
137 // +-------------------+
138 // | to_be_bulk_freed |
139 // +-------------------+
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700140 // | top_bitmap_idx |
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700141 // +-------------------+
142 // | |
143 // | alloc bit map |
144 // | |
145 // +-------------------+
146 // | |
147 // | bulk free bit map |
148 // | |
149 // +-------------------+
150 // | |
151 // | thread-local free |
152 // | bit map |
153 // | |
154 // +-------------------+
155 // | padding due to |
156 // | alignment |
157 // +-------------------+
158 // | slot 0 |
159 // +-------------------+
160 // | slot 1 |
161 // +-------------------+
162 // | slot 2 |
163 // +-------------------+
164 // ...
165 // +-------------------+
166 // | last slot |
167 // +-------------------+
168 //
169 class Run {
170 public:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700171 byte magic_num_; // The magic number used for debugging.
172 byte size_bracket_idx_; // The index of the size bracket of this run.
173 byte is_thread_local_; // True if this run is used as a thread-local run.
174 byte to_be_bulk_freed_; // Used within BulkFree() to flag a run that's involved with a bulk free.
175 uint32_t first_search_vec_idx_; // The index of the first bitmap vector which may contain an available slot.
176 uint32_t alloc_bit_map_[0]; // The bit map that allocates if each slot is in use.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700177
178 // bulk_free_bit_map_[] : The bit map that is used for GC to
179 // temporarily mark the slots to free without using a lock. After
180 // all the slots to be freed in a run are marked, all those slots
181 // get freed in bulk with one locking per run, as opposed to one
182 // locking per slot to minimize the lock contention. This is used
183 // within BulkFree().
184
185 // thread_local_free_bit_map_[] : The bit map that is used for GC
186 // to temporarily mark the slots to free in a thread-local run
187 // without using a lock (without synchronizing the thread that
188 // owns the thread-local run.) When the thread-local run becomes
189 // full, the thread will check this bit map and update the
190 // allocation bit map of the run (that is, the slots get freed.)
191
192 // Returns the byte size of the header except for the bit maps.
193 static size_t fixed_header_size() {
194 Run temp;
195 size_t size = reinterpret_cast<byte*>(&temp.alloc_bit_map_) - reinterpret_cast<byte*>(&temp);
196 DCHECK_EQ(size, static_cast<size_t>(8));
197 return size;
198 }
199 // Returns the base address of the free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800200 uint32_t* BulkFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700201 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + bulkFreeBitMapOffsets[size_bracket_idx_]);
202 }
203 // Returns the base address of the thread local free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800204 uint32_t* ThreadLocalFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700205 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + threadLocalFreeBitMapOffsets[size_bracket_idx_]);
206 }
207 void* End() {
208 return reinterpret_cast<byte*>(this) + kPageSize * numOfPages[size_bracket_idx_];
209 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700210 // Returns the number of bitmap words per run.
211 size_t NumberOfBitmapVectors() const {
212 return RoundUp(numOfSlots[size_bracket_idx_], 32) / 32;
213 }
214 void SetIsThreadLocal(bool is_thread_local) {
215 is_thread_local_ = is_thread_local ? 1 : 0;
216 }
217 bool IsThreadLocal() const {
218 return is_thread_local_ != 0;
219 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700220 // Frees slots in the allocation bit map with regard to the
221 // thread-local free bit map. Used when a thread-local run becomes
222 // full.
223 bool MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out);
224 // Frees slots in the allocation bit map with regard to the bulk
225 // free bit map. Used in a bulk free.
226 void MergeBulkFreeBitMapIntoAllocBitMap();
227 // Unions the slots to be freed in the free bit map into the
228 // thread-local free bit map. In a bulk free, as a two-step
229 // process, GC will first record all the slots to free in a run in
230 // the free bit map where it can write without a lock, and later
231 // acquire a lock once per run to union the bits of the free bit
232 // map to the thread-local free bit map.
233 void UnionBulkFreeBitMapToThreadLocalFreeBitMap();
234 // Allocates a slot in a run.
235 void* AllocSlot();
236 // Frees a slot in a run. This is used in a non-bulk free.
237 void FreeSlot(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700238 // Marks the slots to free in the bulk free bit map. Returns the bracket size.
239 size_t MarkBulkFreeBitMap(void* ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700240 // Marks the slots to free in the thread-local free bit map.
241 void MarkThreadLocalFreeBitMap(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700242 // Last word mask, all of the bits in the last word which aren't valid slots are set to
243 // optimize allocation path.
Andreas Gampe59e67602014-04-25 17:15:12 -0700244 static uint32_t GetBitmapLastVectorMask(size_t num_slots, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700245 // Returns true if all the slots in the run are not in use.
246 bool IsAllFree();
247 // Returns true if all the slots in the run are in use.
248 bool IsFull();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800249 // Returns true if the bulk free bit map is clean.
250 bool IsBulkFreeBitmapClean();
251 // Returns true if the thread local free bit map is clean.
252 bool IsThreadLocalFreeBitmapClean();
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700253 // Set the alloc_bit_map_ bits for slots that are past the end of the run.
254 void SetAllocBitMapBitsForInvalidSlots();
255 // Zero the run's data.
256 void ZeroData();
257 // Zero the run's header.
258 void ZeroHeader();
259 // Fill the alloc bitmap with 1s.
260 void FillAllocBitMap();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700261 // Iterate over all the slots and apply the given function.
262 void InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), void* arg);
263 // Dump the run metadata for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800264 std::string Dump();
265 // Verify for debugging.
266 void Verify(Thread* self, RosAlloc* rosalloc)
267 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
268 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700269
270 private:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700271 // The common part of MarkFreeBitMap() and MarkThreadLocalFreeBitMap(). Returns the bracket
272 // size.
273 size_t MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, const char* caller_name);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800274 // Turns the bit map into a string for debugging.
275 static std::string BitMapToStr(uint32_t* bit_map_base, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700276 };
277
278 // The magic number for a run.
279 static const byte kMagicNum = 42;
280 // The magic number for free pages.
281 static const byte kMagicNumFree = 43;
282 // The number of size brackets. Sync this with the length of Thread::rosalloc_runs_.
283 static const size_t kNumOfSizeBrackets = 34;
284 // The number of smaller size brackets that are 16 bytes apart.
285 static const size_t kNumOfQuantumSizeBrackets = 32;
286 // The sizes (the slot sizes, in bytes) of the size brackets.
287 static size_t bracketSizes[kNumOfSizeBrackets];
288 // The numbers of pages that are used for runs for each size bracket.
289 static size_t numOfPages[kNumOfSizeBrackets];
290 // The numbers of slots of the runs for each size bracket.
291 static size_t numOfSlots[kNumOfSizeBrackets];
292 // The header sizes in bytes of the runs for each size bracket.
293 static size_t headerSizes[kNumOfSizeBrackets];
294 // The byte offsets of the bulk free bit maps of the runs for each size bracket.
295 static size_t bulkFreeBitMapOffsets[kNumOfSizeBrackets];
296 // The byte offsets of the thread-local free bit maps of the runs for each size bracket.
297 static size_t threadLocalFreeBitMapOffsets[kNumOfSizeBrackets];
298
299 // Initialize the run specs (the above arrays).
300 static void Initialize();
301 static bool initialized_;
302
303 // Returns the byte size of the bracket size from the index.
304 static size_t IndexToBracketSize(size_t idx) {
305 DCHECK(idx < kNumOfSizeBrackets);
306 return bracketSizes[idx];
307 }
308 // Returns the index of the size bracket from the bracket size.
309 static size_t BracketSizeToIndex(size_t size) {
310 DCHECK(16 <= size && ((size < 1 * KB && size % 16 == 0) || size == 1 * KB || size == 2 * KB));
311 size_t idx;
312 if (UNLIKELY(size == 1 * KB)) {
313 idx = kNumOfSizeBrackets - 2;
314 } else if (UNLIKELY(size == 2 * KB)) {
315 idx = kNumOfSizeBrackets - 1;
316 } else {
317 DCHECK(size < 1 * KB);
318 DCHECK_EQ(size % 16, static_cast<size_t>(0));
319 idx = size / 16 - 1;
320 }
321 DCHECK(bracketSizes[idx] == size);
322 return idx;
323 }
324 // Rounds up the size up the nearest bracket size.
325 static size_t RoundToBracketSize(size_t size) {
326 DCHECK(size <= kLargeSizeThreshold);
327 if (LIKELY(size <= 512)) {
328 return RoundUp(size, 16);
329 } else if (512 < size && size <= 1 * KB) {
330 return 1 * KB;
331 } else {
332 DCHECK(1 * KB < size && size <= 2 * KB);
333 return 2 * KB;
334 }
335 }
336 // Returns the size bracket index from the byte size with rounding.
337 static size_t SizeToIndex(size_t size) {
338 DCHECK(size <= kLargeSizeThreshold);
339 if (LIKELY(size <= 512)) {
340 return RoundUp(size, 16) / 16 - 1;
341 } else if (512 < size && size <= 1 * KB) {
342 return kNumOfSizeBrackets - 2;
343 } else {
344 DCHECK(1 * KB < size && size <= 2 * KB);
345 return kNumOfSizeBrackets - 1;
346 }
347 }
348 // A combination of SizeToIndex() and RoundToBracketSize().
349 static size_t SizeToIndexAndBracketSize(size_t size, size_t* bracket_size_out) {
350 DCHECK(size <= kLargeSizeThreshold);
351 if (LIKELY(size <= 512)) {
352 size_t bracket_size = RoundUp(size, 16);
353 *bracket_size_out = bracket_size;
354 size_t idx = bracket_size / 16 - 1;
355 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
356 return idx;
357 } else if (512 < size && size <= 1 * KB) {
358 size_t bracket_size = 1024;
359 *bracket_size_out = bracket_size;
360 size_t idx = kNumOfSizeBrackets - 2;
361 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
362 return idx;
363 } else {
364 DCHECK(1 * KB < size && size <= 2 * KB);
365 size_t bracket_size = 2048;
366 *bracket_size_out = bracket_size;
367 size_t idx = kNumOfSizeBrackets - 1;
368 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
369 return idx;
370 }
371 }
372 // Returns the page map index from an address. Requires that the
373 // address is page size aligned.
374 size_t ToPageMapIndex(const void* addr) const {
375 DCHECK(base_ <= addr && addr < base_ + capacity_);
376 size_t byte_offset = reinterpret_cast<const byte*>(addr) - base_;
377 DCHECK_EQ(byte_offset % static_cast<size_t>(kPageSize), static_cast<size_t>(0));
378 return byte_offset / kPageSize;
379 }
380 // Returns the page map index from an address with rounding.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700381 size_t RoundDownToPageMapIndex(void* addr) const {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700382 DCHECK(base_ <= addr && addr < reinterpret_cast<byte*>(base_) + capacity_);
383 return (reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(base_)) / kPageSize;
384 }
385
386 // A memory allocation request larger than this size is treated as a large object and allocated
387 // at a page-granularity.
388 static const size_t kLargeSizeThreshold = 2048;
389
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800390 // If true, check that the returned memory is actually zero.
391 static constexpr bool kCheckZeroMemory = kIsDebugBuild;
392
393 // If true, log verbose details of operations.
394 static constexpr bool kTraceRosAlloc = false;
395
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700396 struct hash_run {
397 size_t operator()(const RosAlloc::Run* r) const {
398 return reinterpret_cast<size_t>(r);
399 }
400 };
401
402 struct eq_run {
403 bool operator()(const RosAlloc::Run* r1, const RosAlloc::Run* r2) const {
404 return r1 == r2;
405 }
406 };
407
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800408 public:
409 // Different page release modes.
410 enum PageReleaseMode {
411 kPageReleaseModeNone, // Release no empty pages.
412 kPageReleaseModeEnd, // Release empty pages at the end of the space.
413 kPageReleaseModeSize, // Release empty pages that are larger than the threshold.
414 kPageReleaseModeSizeAndEnd, // Release empty pages that are larger than the threshold or
415 // at the end of the space.
416 kPageReleaseModeAll, // Release all empty pages.
417 };
418
419 // The default value for page_release_size_threshold_.
420 static constexpr size_t kDefaultPageReleaseSizeThreshold = 4 * MB;
421
Mathieu Chartier0651d412014-04-29 14:37:57 -0700422 // We use thread-local runs for the size Brackets whose indexes
423 // are less than this index. We use shared (current) runs for the rest.
424 static const size_t kNumThreadLocalSizeBrackets = 11;
425
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800426 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700427 // The base address of the memory region that's managed by this allocator.
428 byte* base_;
429
430 // The footprint in bytes of the currently allocated portion of the
431 // memory region.
432 size_t footprint_;
433
434 // The maximum footprint. The address, base_ + capacity_, indicates
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800435 // the end of the memory region that's currently managed by this allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700436 size_t capacity_;
437
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800438 // The maximum capacity. The address, base_ + max_capacity_, indicates
439 // the end of the memory region that's ever managed by this allocator.
440 size_t max_capacity_;
441
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700442 // The run sets that hold the runs whose slots are not all
443 // full. non_full_runs_[i] is guarded by size_bracket_locks_[i].
444 std::set<Run*> non_full_runs_[kNumOfSizeBrackets];
445 // The run sets that hold the runs whose slots are all full. This is
446 // debug only. full_runs_[i] is guarded by size_bracket_locks_[i].
Ian Rogers700a4022014-05-19 16:49:03 -0700447 std::unordered_set<Run*, hash_run, eq_run> full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700448 // The set of free pages.
449 std::set<FreePageRun*> free_page_runs_ GUARDED_BY(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700450 // The dedicated full run, it is always full and shared by all threads when revoking happens.
451 // This is an optimization since enables us to avoid a null check for revoked runs.
452 static Run* dedicated_full_run_;
453 // Using size_t to ensure that it is at least word aligned.
454 static size_t dedicated_full_run_storage_[];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700455 // The current runs where the allocations are first attempted for
456 // the size brackes that do not use thread-local
457 // runs. current_runs_[i] is guarded by size_bracket_locks_[i].
458 Run* current_runs_[kNumOfSizeBrackets];
459 // The mutexes, one per size bracket.
460 Mutex* size_bracket_locks_[kNumOfSizeBrackets];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700461 // Bracket lock names (since locks only have char* names).
462 std::string size_bracket_lock_names[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700463 // The types of page map entries.
464 enum {
465 kPageMapEmpty = 0, // Not allocated.
466 kPageMapRun = 1, // The beginning of a run.
467 kPageMapRunPart = 2, // The non-beginning part of a run.
468 kPageMapLargeObject = 3, // The beginning of a large object.
469 kPageMapLargeObjectPart = 4, // The non-beginning part of a large object.
470 };
471 // The table that indicates what pages are currently used for.
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800472 byte* page_map_; // No GUARDED_BY(lock_) for kReadPageMapEntryWithoutLockInBulkFree.
473 size_t page_map_size_;
474 size_t max_page_map_size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700475 std::unique_ptr<MemMap> page_map_mem_map_;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800476
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700477 // The table that indicates the size of free page runs. These sizes
478 // are stored here to avoid storing in the free page header and
479 // release backing pages.
480 std::vector<size_t> free_page_run_size_map_ GUARDED_BY(lock_);
481 // The global lock. Used to guard the page map, the free page set,
482 // and the footprint.
483 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
484 // The reader-writer lock to allow one bulk free at a time while
Hiroshi Yamauchi70f60042014-02-03 12:31:29 -0800485 // allowing multiple individual frees at the same time. Also, this
486 // is used to avoid race conditions between BulkFree() and
487 // RevokeThreadLocalRuns() on the bulk free bitmaps.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700488 ReaderWriterMutex bulk_free_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
489
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800490 // The page release mode.
491 const PageReleaseMode page_release_mode_;
492 // Under kPageReleaseModeSize(AndEnd), if the free page run size is
493 // greater than or equal to this value, release pages.
494 const size_t page_release_size_threshold_;
495
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700496 // The base address of the memory region that's managed by this allocator.
497 byte* Begin() { return base_; }
498 // The end address of the memory region that's managed by this allocator.
499 byte* End() { return base_ + capacity_; }
500
501 // Page-granularity alloc/free
502 void* AllocPages(Thread* self, size_t num_pages, byte page_map_type)
503 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
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700539 public:
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800540 RosAlloc(void* base, size_t capacity, size_t max_capacity,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800541 PageReleaseMode page_release_mode,
542 size_t page_release_size_threshold = kDefaultPageReleaseSizeThreshold);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800543 ~RosAlloc();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700544 // If kThreadUnsafe is true then the allocator may avoid acquiring some locks as an optimization.
545 // If used, this may cause race conditions if multiple threads are allocating at the same time.
546 template<bool kThreadSafe = true>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700547 void* Alloc(Thread* self, size_t size, size_t* bytes_allocated)
548 LOCKS_EXCLUDED(lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700549 size_t Free(Thread* self, void* ptr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700550 LOCKS_EXCLUDED(bulk_free_lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700551 size_t BulkFree(Thread* self, void** ptrs, size_t num_ptrs)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700552 LOCKS_EXCLUDED(bulk_free_lock_);
553 // Returns the size of the allocated slot for a given allocated memory chunk.
554 size_t UsableSize(void* ptr);
555 // Returns the size of the allocated slot for a given size.
556 size_t UsableSize(size_t bytes) {
557 if (UNLIKELY(bytes > kLargeSizeThreshold)) {
558 return RoundUp(bytes, kPageSize);
559 } else {
560 return RoundToBracketSize(bytes);
561 }
562 }
563 // Try to reduce the current footprint by releasing the free page
564 // run at the end of the memory region, if any.
565 bool Trim();
566 // Iterates over all the memory slots and apply the given function.
567 void InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
568 void* arg)
569 LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700570 // Release empty pages.
571 size_t ReleasePages() LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700572 // Returns the current footprint.
573 size_t Footprint() LOCKS_EXCLUDED(lock_);
574 // Returns the current capacity, maximum footprint.
575 size_t FootprintLimit() LOCKS_EXCLUDED(lock_);
576 // Update the current capacity.
577 void SetFootprintLimit(size_t bytes) LOCKS_EXCLUDED(lock_);
578 // Releases the thread-local runs assigned to the given thread back to the common set of runs.
579 void RevokeThreadLocalRuns(Thread* thread);
580 // Releases the thread-local runs assigned to all the threads back to the common set of runs.
581 void RevokeAllThreadLocalRuns() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700582 // Assert the thread local runs of a thread are revoked.
583 void AssertThreadLocalRunsAreRevoked(Thread* thread);
584 // Assert all the thread local runs are revoked.
585 void AssertAllThreadLocalRunsAreRevoked() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700586 // Dumps the page map for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800587 std::string DumpPageMap() EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700588 static Run* GetDedicatedFullRun() {
589 return dedicated_full_run_;
590 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700591
592 // Callbacks for InspectAll that will count the number of bytes
593 // allocated and objects allocated, respectively.
594 static void BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
595 static void ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800596
597 bool DoesReleaseAllPages() const {
598 return page_release_mode_ == kPageReleaseModeAll;
599 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800600
601 // Verify for debugging.
602 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700603};
604
605} // namespace allocator
606} // namespace gc
607} // namespace art
608
609#endif // ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_