blob: a4391888580129997cee6b8509d0eabf0ad3bc2b [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 {
48 if (kIsDebugBuild) {
49 return magic_num_ == kMagicNumFree;
50 }
51 return true;
52 }
53 size_t ByteSize(RosAlloc* rosalloc) const EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
54 const byte* fpr_base = reinterpret_cast<const byte*>(this);
55 size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base);
56 size_t byte_size = rosalloc->free_page_run_size_map_[pm_idx];
57 DCHECK_GE(byte_size, static_cast<size_t>(0));
58 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
59 return byte_size;
60 }
61 void SetByteSize(RosAlloc* rosalloc, size_t byte_size)
62 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
63 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
64 byte* fpr_base = reinterpret_cast<byte*>(this);
65 size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base);
66 rosalloc->free_page_run_size_map_[pm_idx] = byte_size;
67 }
68 void* Begin() {
69 return reinterpret_cast<void*>(this);
70 }
71 void* End(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
72 byte* fpr_base = reinterpret_cast<byte*>(this);
73 byte* end = fpr_base + ByteSize(rosalloc);
74 return end;
75 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -080076 bool IsLargerThanPageReleaseThreshold(RosAlloc* rosalloc)
77 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
78 return ByteSize(rosalloc) >= rosalloc->page_release_size_threshold_;
79 }
80 bool IsAtEndOfSpace(RosAlloc* rosalloc)
81 EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
82 return reinterpret_cast<byte*>(this) + ByteSize(rosalloc) == rosalloc->base_ + rosalloc->footprint_;
83 }
84 bool ShouldReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
85 switch (rosalloc->page_release_mode_) {
86 case kPageReleaseModeNone:
87 return false;
88 case kPageReleaseModeEnd:
89 return IsAtEndOfSpace(rosalloc);
90 case kPageReleaseModeSize:
91 return IsLargerThanPageReleaseThreshold(rosalloc);
92 case kPageReleaseModeSizeAndEnd:
93 return IsLargerThanPageReleaseThreshold(rosalloc) && IsAtEndOfSpace(rosalloc);
94 case kPageReleaseModeAll:
95 return true;
96 default:
97 LOG(FATAL) << "Unexpected page release mode ";
98 return false;
99 }
100 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700101 void ReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800102 byte* start = reinterpret_cast<byte*>(this);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700103 size_t byte_size = ByteSize(rosalloc);
104 DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0));
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800105 bool release_pages = ShouldReleasePages(rosalloc);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700106 if (kIsDebugBuild) {
107 // Exclude the first page that stores the magic number.
108 DCHECK_GE(byte_size, static_cast<size_t>(kPageSize));
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800109 start += kPageSize;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700110 byte_size -= kPageSize;
111 if (byte_size > 0) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800112 if (release_pages) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700113 if (!kMadviseZeroes) {
114 memset(start, 0, byte_size);
115 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800116 madvise(start, byte_size, MADV_DONTNEED);
117 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700118 }
119 } else {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800120 if (release_pages) {
Ian Rogersc5f17732014-06-05 20:48:42 -0700121 if (!kMadviseZeroes) {
122 memset(start, 0, byte_size);
123 }
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800124 madvise(start, byte_size, MADV_DONTNEED);
125 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700126 }
127 }
128 };
129
130 // Represents a run of memory slots of the same size.
131 //
132 // A run's memory layout:
133 //
134 // +-------------------+
135 // | magic_num |
136 // +-------------------+
137 // | size_bracket_idx |
138 // +-------------------+
139 // | is_thread_local |
140 // +-------------------+
141 // | to_be_bulk_freed |
142 // +-------------------+
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700143 // | top_bitmap_idx |
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700144 // +-------------------+
145 // | |
146 // | alloc bit map |
147 // | |
148 // +-------------------+
149 // | |
150 // | bulk free bit map |
151 // | |
152 // +-------------------+
153 // | |
154 // | thread-local free |
155 // | bit map |
156 // | |
157 // +-------------------+
158 // | padding due to |
159 // | alignment |
160 // +-------------------+
161 // | slot 0 |
162 // +-------------------+
163 // | slot 1 |
164 // +-------------------+
165 // | slot 2 |
166 // +-------------------+
167 // ...
168 // +-------------------+
169 // | last slot |
170 // +-------------------+
171 //
172 class Run {
173 public:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700174 byte magic_num_; // The magic number used for debugging.
175 byte size_bracket_idx_; // The index of the size bracket of this run.
176 byte is_thread_local_; // True if this run is used as a thread-local run.
177 byte to_be_bulk_freed_; // Used within BulkFree() to flag a run that's involved with a bulk free.
178 uint32_t first_search_vec_idx_; // The index of the first bitmap vector which may contain an available slot.
179 uint32_t alloc_bit_map_[0]; // The bit map that allocates if each slot is in use.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700180
181 // bulk_free_bit_map_[] : The bit map that is used for GC to
182 // temporarily mark the slots to free without using a lock. After
183 // all the slots to be freed in a run are marked, all those slots
184 // get freed in bulk with one locking per run, as opposed to one
185 // locking per slot to minimize the lock contention. This is used
186 // within BulkFree().
187
188 // thread_local_free_bit_map_[] : The bit map that is used for GC
189 // to temporarily mark the slots to free in a thread-local run
190 // without using a lock (without synchronizing the thread that
191 // owns the thread-local run.) When the thread-local run becomes
192 // full, the thread will check this bit map and update the
193 // allocation bit map of the run (that is, the slots get freed.)
194
195 // Returns the byte size of the header except for the bit maps.
196 static size_t fixed_header_size() {
197 Run temp;
198 size_t size = reinterpret_cast<byte*>(&temp.alloc_bit_map_) - reinterpret_cast<byte*>(&temp);
199 DCHECK_EQ(size, static_cast<size_t>(8));
200 return size;
201 }
202 // Returns the base address of the free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800203 uint32_t* BulkFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700204 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + bulkFreeBitMapOffsets[size_bracket_idx_]);
205 }
206 // Returns the base address of the thread local free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800207 uint32_t* ThreadLocalFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700208 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + threadLocalFreeBitMapOffsets[size_bracket_idx_]);
209 }
210 void* End() {
211 return reinterpret_cast<byte*>(this) + kPageSize * numOfPages[size_bracket_idx_];
212 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700213 // Returns the number of bitmap words per run.
214 size_t NumberOfBitmapVectors() const {
215 return RoundUp(numOfSlots[size_bracket_idx_], 32) / 32;
216 }
217 void SetIsThreadLocal(bool is_thread_local) {
218 is_thread_local_ = is_thread_local ? 1 : 0;
219 }
220 bool IsThreadLocal() const {
221 return is_thread_local_ != 0;
222 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700223 // Frees slots in the allocation bit map with regard to the
224 // thread-local free bit map. Used when a thread-local run becomes
225 // full.
226 bool MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out);
227 // Frees slots in the allocation bit map with regard to the bulk
228 // free bit map. Used in a bulk free.
229 void MergeBulkFreeBitMapIntoAllocBitMap();
230 // Unions the slots to be freed in the free bit map into the
231 // thread-local free bit map. In a bulk free, as a two-step
232 // process, GC will first record all the slots to free in a run in
233 // the free bit map where it can write without a lock, and later
234 // acquire a lock once per run to union the bits of the free bit
235 // map to the thread-local free bit map.
236 void UnionBulkFreeBitMapToThreadLocalFreeBitMap();
237 // Allocates a slot in a run.
238 void* AllocSlot();
239 // Frees a slot in a run. This is used in a non-bulk free.
240 void FreeSlot(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700241 // Marks the slots to free in the bulk free bit map. Returns the bracket size.
242 size_t MarkBulkFreeBitMap(void* ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700243 // Marks the slots to free in the thread-local free bit map.
244 void MarkThreadLocalFreeBitMap(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700245 // Last word mask, all of the bits in the last word which aren't valid slots are set to
246 // optimize allocation path.
Andreas Gampe59e67602014-04-25 17:15:12 -0700247 static uint32_t GetBitmapLastVectorMask(size_t num_slots, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700248 // Returns true if all the slots in the run are not in use.
249 bool IsAllFree();
250 // Returns true if all the slots in the run are in use.
251 bool IsFull();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800252 // Returns true if the bulk free bit map is clean.
253 bool IsBulkFreeBitmapClean();
254 // Returns true if the thread local free bit map is clean.
255 bool IsThreadLocalFreeBitmapClean();
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700256 // Set the alloc_bit_map_ bits for slots that are past the end of the run.
257 void SetAllocBitMapBitsForInvalidSlots();
258 // Zero the run's data.
259 void ZeroData();
260 // Zero the run's header.
261 void ZeroHeader();
262 // Fill the alloc bitmap with 1s.
263 void FillAllocBitMap();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700264 // Iterate over all the slots and apply the given function.
265 void InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), void* arg);
266 // Dump the run metadata for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800267 std::string Dump();
268 // Verify for debugging.
269 void Verify(Thread* self, RosAlloc* rosalloc)
270 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
271 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700272
273 private:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700274 // The common part of MarkFreeBitMap() and MarkThreadLocalFreeBitMap(). Returns the bracket
275 // size.
276 size_t MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, const char* caller_name);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800277 // Turns the bit map into a string for debugging.
278 static std::string BitMapToStr(uint32_t* bit_map_base, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700279 };
280
281 // The magic number for a run.
282 static const byte kMagicNum = 42;
283 // The magic number for free pages.
284 static const byte kMagicNumFree = 43;
285 // The number of size brackets. Sync this with the length of Thread::rosalloc_runs_.
286 static const size_t kNumOfSizeBrackets = 34;
287 // The number of smaller size brackets that are 16 bytes apart.
288 static const size_t kNumOfQuantumSizeBrackets = 32;
289 // The sizes (the slot sizes, in bytes) of the size brackets.
290 static size_t bracketSizes[kNumOfSizeBrackets];
291 // The numbers of pages that are used for runs for each size bracket.
292 static size_t numOfPages[kNumOfSizeBrackets];
293 // The numbers of slots of the runs for each size bracket.
294 static size_t numOfSlots[kNumOfSizeBrackets];
295 // The header sizes in bytes of the runs for each size bracket.
296 static size_t headerSizes[kNumOfSizeBrackets];
297 // The byte offsets of the bulk free bit maps of the runs for each size bracket.
298 static size_t bulkFreeBitMapOffsets[kNumOfSizeBrackets];
299 // The byte offsets of the thread-local free bit maps of the runs for each size bracket.
300 static size_t threadLocalFreeBitMapOffsets[kNumOfSizeBrackets];
301
302 // Initialize the run specs (the above arrays).
303 static void Initialize();
304 static bool initialized_;
305
306 // Returns the byte size of the bracket size from the index.
307 static size_t IndexToBracketSize(size_t idx) {
308 DCHECK(idx < kNumOfSizeBrackets);
309 return bracketSizes[idx];
310 }
311 // Returns the index of the size bracket from the bracket size.
312 static size_t BracketSizeToIndex(size_t size) {
313 DCHECK(16 <= size && ((size < 1 * KB && size % 16 == 0) || size == 1 * KB || size == 2 * KB));
314 size_t idx;
315 if (UNLIKELY(size == 1 * KB)) {
316 idx = kNumOfSizeBrackets - 2;
317 } else if (UNLIKELY(size == 2 * KB)) {
318 idx = kNumOfSizeBrackets - 1;
319 } else {
320 DCHECK(size < 1 * KB);
321 DCHECK_EQ(size % 16, static_cast<size_t>(0));
322 idx = size / 16 - 1;
323 }
324 DCHECK(bracketSizes[idx] == size);
325 return idx;
326 }
327 // Rounds up the size up the nearest bracket size.
328 static size_t RoundToBracketSize(size_t size) {
329 DCHECK(size <= kLargeSizeThreshold);
330 if (LIKELY(size <= 512)) {
331 return RoundUp(size, 16);
332 } else if (512 < size && size <= 1 * KB) {
333 return 1 * KB;
334 } else {
335 DCHECK(1 * KB < size && size <= 2 * KB);
336 return 2 * KB;
337 }
338 }
339 // Returns the size bracket index from the byte size with rounding.
340 static size_t SizeToIndex(size_t size) {
341 DCHECK(size <= kLargeSizeThreshold);
342 if (LIKELY(size <= 512)) {
343 return RoundUp(size, 16) / 16 - 1;
344 } else if (512 < size && size <= 1 * KB) {
345 return kNumOfSizeBrackets - 2;
346 } else {
347 DCHECK(1 * KB < size && size <= 2 * KB);
348 return kNumOfSizeBrackets - 1;
349 }
350 }
351 // A combination of SizeToIndex() and RoundToBracketSize().
352 static size_t SizeToIndexAndBracketSize(size_t size, size_t* bracket_size_out) {
353 DCHECK(size <= kLargeSizeThreshold);
354 if (LIKELY(size <= 512)) {
355 size_t bracket_size = RoundUp(size, 16);
356 *bracket_size_out = bracket_size;
357 size_t idx = bracket_size / 16 - 1;
358 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
359 return idx;
360 } else if (512 < size && size <= 1 * KB) {
361 size_t bracket_size = 1024;
362 *bracket_size_out = bracket_size;
363 size_t idx = kNumOfSizeBrackets - 2;
364 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
365 return idx;
366 } else {
367 DCHECK(1 * KB < size && size <= 2 * KB);
368 size_t bracket_size = 2048;
369 *bracket_size_out = bracket_size;
370 size_t idx = kNumOfSizeBrackets - 1;
371 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
372 return idx;
373 }
374 }
375 // Returns the page map index from an address. Requires that the
376 // address is page size aligned.
377 size_t ToPageMapIndex(const void* addr) const {
378 DCHECK(base_ <= addr && addr < base_ + capacity_);
379 size_t byte_offset = reinterpret_cast<const byte*>(addr) - base_;
380 DCHECK_EQ(byte_offset % static_cast<size_t>(kPageSize), static_cast<size_t>(0));
381 return byte_offset / kPageSize;
382 }
383 // Returns the page map index from an address with rounding.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700384 size_t RoundDownToPageMapIndex(void* addr) const {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700385 DCHECK(base_ <= addr && addr < reinterpret_cast<byte*>(base_) + capacity_);
386 return (reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(base_)) / kPageSize;
387 }
388
389 // A memory allocation request larger than this size is treated as a large object and allocated
390 // at a page-granularity.
391 static const size_t kLargeSizeThreshold = 2048;
392
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800393 // If true, check that the returned memory is actually zero.
394 static constexpr bool kCheckZeroMemory = kIsDebugBuild;
395
396 // If true, log verbose details of operations.
397 static constexpr bool kTraceRosAlloc = false;
398
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700399 struct hash_run {
400 size_t operator()(const RosAlloc::Run* r) const {
401 return reinterpret_cast<size_t>(r);
402 }
403 };
404
405 struct eq_run {
406 bool operator()(const RosAlloc::Run* r1, const RosAlloc::Run* r2) const {
407 return r1 == r2;
408 }
409 };
410
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800411 public:
412 // Different page release modes.
413 enum PageReleaseMode {
414 kPageReleaseModeNone, // Release no empty pages.
415 kPageReleaseModeEnd, // Release empty pages at the end of the space.
416 kPageReleaseModeSize, // Release empty pages that are larger than the threshold.
417 kPageReleaseModeSizeAndEnd, // Release empty pages that are larger than the threshold or
418 // at the end of the space.
419 kPageReleaseModeAll, // Release all empty pages.
420 };
421
422 // The default value for page_release_size_threshold_.
423 static constexpr size_t kDefaultPageReleaseSizeThreshold = 4 * MB;
424
Mathieu Chartier0651d412014-04-29 14:37:57 -0700425 // We use thread-local runs for the size Brackets whose indexes
426 // are less than this index. We use shared (current) runs for the rest.
427 static const size_t kNumThreadLocalSizeBrackets = 11;
428
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800429 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700430 // The base address of the memory region that's managed by this allocator.
431 byte* base_;
432
433 // The footprint in bytes of the currently allocated portion of the
434 // memory region.
435 size_t footprint_;
436
437 // The maximum footprint. The address, base_ + capacity_, indicates
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800438 // the end of the memory region that's currently managed by this allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700439 size_t capacity_;
440
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800441 // The maximum capacity. The address, base_ + max_capacity_, indicates
442 // the end of the memory region that's ever managed by this allocator.
443 size_t max_capacity_;
444
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700445 // The run sets that hold the runs whose slots are not all
446 // full. non_full_runs_[i] is guarded by size_bracket_locks_[i].
447 std::set<Run*> non_full_runs_[kNumOfSizeBrackets];
448 // The run sets that hold the runs whose slots are all full. This is
449 // debug only. full_runs_[i] is guarded by size_bracket_locks_[i].
Ian Rogers700a4022014-05-19 16:49:03 -0700450 std::unordered_set<Run*, hash_run, eq_run> full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700451 // The set of free pages.
452 std::set<FreePageRun*> free_page_runs_ GUARDED_BY(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700453 // The dedicated full run, it is always full and shared by all threads when revoking happens.
454 // This is an optimization since enables us to avoid a null check for revoked runs.
455 static Run* dedicated_full_run_;
456 // Using size_t to ensure that it is at least word aligned.
457 static size_t dedicated_full_run_storage_[];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700458 // The current runs where the allocations are first attempted for
459 // the size brackes that do not use thread-local
460 // runs. current_runs_[i] is guarded by size_bracket_locks_[i].
461 Run* current_runs_[kNumOfSizeBrackets];
462 // The mutexes, one per size bracket.
463 Mutex* size_bracket_locks_[kNumOfSizeBrackets];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700464 // Bracket lock names (since locks only have char* names).
465 std::string size_bracket_lock_names[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700466 // The types of page map entries.
467 enum {
468 kPageMapEmpty = 0, // Not allocated.
469 kPageMapRun = 1, // The beginning of a run.
470 kPageMapRunPart = 2, // The non-beginning part of a run.
471 kPageMapLargeObject = 3, // The beginning of a large object.
472 kPageMapLargeObjectPart = 4, // The non-beginning part of a large object.
473 };
474 // The table that indicates what pages are currently used for.
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800475 byte* page_map_; // No GUARDED_BY(lock_) for kReadPageMapEntryWithoutLockInBulkFree.
476 size_t page_map_size_;
477 size_t max_page_map_size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700478 std::unique_ptr<MemMap> page_map_mem_map_;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800479
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700480 // The table that indicates the size of free page runs. These sizes
481 // are stored here to avoid storing in the free page header and
482 // release backing pages.
483 std::vector<size_t> free_page_run_size_map_ GUARDED_BY(lock_);
484 // The global lock. Used to guard the page map, the free page set,
485 // and the footprint.
486 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
487 // The reader-writer lock to allow one bulk free at a time while
Hiroshi Yamauchi70f60042014-02-03 12:31:29 -0800488 // allowing multiple individual frees at the same time. Also, this
489 // is used to avoid race conditions between BulkFree() and
490 // RevokeThreadLocalRuns() on the bulk free bitmaps.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700491 ReaderWriterMutex bulk_free_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
492
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800493 // The page release mode.
494 const PageReleaseMode page_release_mode_;
495 // Under kPageReleaseModeSize(AndEnd), if the free page run size is
496 // greater than or equal to this value, release pages.
497 const size_t page_release_size_threshold_;
498
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700499 // The base address of the memory region that's managed by this allocator.
500 byte* Begin() { return base_; }
501 // The end address of the memory region that's managed by this allocator.
502 byte* End() { return base_ + capacity_; }
503
504 // Page-granularity alloc/free
505 void* AllocPages(Thread* self, size_t num_pages, byte page_map_type)
506 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700507 // Returns how many bytes were freed.
508 size_t FreePages(Thread* self, void* ptr, bool already_zero) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700509
510 // Allocate/free a run slot.
511 void* AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated)
512 LOCKS_EXCLUDED(lock_);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700513 // Allocate/free a run slot without acquiring locks.
514 // TODO: EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
515 void* AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated)
516 LOCKS_EXCLUDED(lock_);
517 void* AllocFromCurrentRunUnlocked(Thread* self, size_t idx);
518
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700519 // Returns the bracket size.
520 size_t FreeFromRun(Thread* self, void* ptr, Run* run)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700521 LOCKS_EXCLUDED(lock_);
522
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700523 // Used to allocate a new thread local run for a size bracket.
524 Run* AllocRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
525
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700526 // Used to acquire a new/reused run for a size bracket. Used when a
527 // thread-local or current run gets full.
528 Run* RefillRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
529
530 // The internal of non-bulk Free().
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700531 size_t FreeInternal(Thread* self, void* ptr) LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700532
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800533 // Allocates large objects.
534 void* AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated) LOCKS_EXCLUDED(lock_);
535
Mathieu Chartier0651d412014-04-29 14:37:57 -0700536 // Revoke a run by adding it to non_full_runs_ or freeing the pages.
537 void RevokeRun(Thread* self, size_t idx, Run* run);
538
539 // Revoke the current runs which share an index with the thread local runs.
540 void RevokeThreadUnsafeCurrentRuns();
541
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700542 public:
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800543 RosAlloc(void* base, size_t capacity, size_t max_capacity,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800544 PageReleaseMode page_release_mode,
545 size_t page_release_size_threshold = kDefaultPageReleaseSizeThreshold);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800546 ~RosAlloc();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700547 // If kThreadUnsafe is true then the allocator may avoid acquiring some locks as an optimization.
548 // If used, this may cause race conditions if multiple threads are allocating at the same time.
549 template<bool kThreadSafe = true>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700550 void* Alloc(Thread* self, size_t size, size_t* bytes_allocated)
551 LOCKS_EXCLUDED(lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700552 size_t Free(Thread* self, void* ptr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700553 LOCKS_EXCLUDED(bulk_free_lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700554 size_t BulkFree(Thread* self, void** ptrs, size_t num_ptrs)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700555 LOCKS_EXCLUDED(bulk_free_lock_);
556 // Returns the size of the allocated slot for a given allocated memory chunk.
557 size_t UsableSize(void* ptr);
558 // Returns the size of the allocated slot for a given size.
559 size_t UsableSize(size_t bytes) {
560 if (UNLIKELY(bytes > kLargeSizeThreshold)) {
561 return RoundUp(bytes, kPageSize);
562 } else {
563 return RoundToBracketSize(bytes);
564 }
565 }
566 // Try to reduce the current footprint by releasing the free page
567 // run at the end of the memory region, if any.
568 bool Trim();
569 // Iterates over all the memory slots and apply the given function.
570 void InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
571 void* arg)
572 LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700573 // Release empty pages.
574 size_t ReleasePages() LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700575 // Returns the current footprint.
576 size_t Footprint() LOCKS_EXCLUDED(lock_);
577 // Returns the current capacity, maximum footprint.
578 size_t FootprintLimit() LOCKS_EXCLUDED(lock_);
579 // Update the current capacity.
580 void SetFootprintLimit(size_t bytes) LOCKS_EXCLUDED(lock_);
581 // Releases the thread-local runs assigned to the given thread back to the common set of runs.
582 void RevokeThreadLocalRuns(Thread* thread);
583 // Releases the thread-local runs assigned to all the threads back to the common set of runs.
584 void RevokeAllThreadLocalRuns() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700585 // Assert the thread local runs of a thread are revoked.
586 void AssertThreadLocalRunsAreRevoked(Thread* thread);
587 // Assert all the thread local runs are revoked.
588 void AssertAllThreadLocalRunsAreRevoked() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700589 // Dumps the page map for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800590 std::string DumpPageMap() EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700591 static Run* GetDedicatedFullRun() {
592 return dedicated_full_run_;
593 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700594
595 // Callbacks for InspectAll that will count the number of bytes
596 // allocated and objects allocated, respectively.
597 static void BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
598 static void ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800599
600 bool DoesReleaseAllPages() const {
601 return page_release_mode_ == kPageReleaseModeAll;
602 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800603
604 // Verify for debugging.
605 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700606};
607
608} // namespace allocator
609} // namespace gc
610} // namespace art
611
612#endif // ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_