blob: 9464331c70c4b3cf3ef2ce98a05710641853dd76 [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) {
113 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) {
118 madvise(start, byte_size, MADV_DONTNEED);
119 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700120 }
121 }
122 };
123
124 // Represents a run of memory slots of the same size.
125 //
126 // A run's memory layout:
127 //
128 // +-------------------+
129 // | magic_num |
130 // +-------------------+
131 // | size_bracket_idx |
132 // +-------------------+
133 // | is_thread_local |
134 // +-------------------+
135 // | to_be_bulk_freed |
136 // +-------------------+
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700137 // | top_bitmap_idx |
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700138 // +-------------------+
139 // | |
140 // | alloc bit map |
141 // | |
142 // +-------------------+
143 // | |
144 // | bulk free bit map |
145 // | |
146 // +-------------------+
147 // | |
148 // | thread-local free |
149 // | bit map |
150 // | |
151 // +-------------------+
152 // | padding due to |
153 // | alignment |
154 // +-------------------+
155 // | slot 0 |
156 // +-------------------+
157 // | slot 1 |
158 // +-------------------+
159 // | slot 2 |
160 // +-------------------+
161 // ...
162 // +-------------------+
163 // | last slot |
164 // +-------------------+
165 //
166 class Run {
167 public:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700168 byte magic_num_; // The magic number used for debugging.
169 byte size_bracket_idx_; // The index of the size bracket of this run.
170 byte is_thread_local_; // True if this run is used as a thread-local run.
171 byte to_be_bulk_freed_; // Used within BulkFree() to flag a run that's involved with a bulk free.
172 uint32_t first_search_vec_idx_; // The index of the first bitmap vector which may contain an available slot.
173 uint32_t alloc_bit_map_[0]; // The bit map that allocates if each slot is in use.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700174
175 // bulk_free_bit_map_[] : The bit map that is used for GC to
176 // temporarily mark the slots to free without using a lock. After
177 // all the slots to be freed in a run are marked, all those slots
178 // get freed in bulk with one locking per run, as opposed to one
179 // locking per slot to minimize the lock contention. This is used
180 // within BulkFree().
181
182 // thread_local_free_bit_map_[] : The bit map that is used for GC
183 // to temporarily mark the slots to free in a thread-local run
184 // without using a lock (without synchronizing the thread that
185 // owns the thread-local run.) When the thread-local run becomes
186 // full, the thread will check this bit map and update the
187 // allocation bit map of the run (that is, the slots get freed.)
188
189 // Returns the byte size of the header except for the bit maps.
190 static size_t fixed_header_size() {
191 Run temp;
192 size_t size = reinterpret_cast<byte*>(&temp.alloc_bit_map_) - reinterpret_cast<byte*>(&temp);
193 DCHECK_EQ(size, static_cast<size_t>(8));
194 return size;
195 }
196 // Returns the base address of the free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800197 uint32_t* BulkFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700198 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + bulkFreeBitMapOffsets[size_bracket_idx_]);
199 }
200 // Returns the base address of the thread local free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800201 uint32_t* ThreadLocalFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700202 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + threadLocalFreeBitMapOffsets[size_bracket_idx_]);
203 }
204 void* End() {
205 return reinterpret_cast<byte*>(this) + kPageSize * numOfPages[size_bracket_idx_];
206 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700207 // Returns the number of bitmap words per run.
208 size_t NumberOfBitmapVectors() const {
209 return RoundUp(numOfSlots[size_bracket_idx_], 32) / 32;
210 }
211 void SetIsThreadLocal(bool is_thread_local) {
212 is_thread_local_ = is_thread_local ? 1 : 0;
213 }
214 bool IsThreadLocal() const {
215 return is_thread_local_ != 0;
216 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700217 // Frees slots in the allocation bit map with regard to the
218 // thread-local free bit map. Used when a thread-local run becomes
219 // full.
220 bool MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out);
221 // Frees slots in the allocation bit map with regard to the bulk
222 // free bit map. Used in a bulk free.
223 void MergeBulkFreeBitMapIntoAllocBitMap();
224 // Unions the slots to be freed in the free bit map into the
225 // thread-local free bit map. In a bulk free, as a two-step
226 // process, GC will first record all the slots to free in a run in
227 // the free bit map where it can write without a lock, and later
228 // acquire a lock once per run to union the bits of the free bit
229 // map to the thread-local free bit map.
230 void UnionBulkFreeBitMapToThreadLocalFreeBitMap();
231 // Allocates a slot in a run.
232 void* AllocSlot();
233 // Frees a slot in a run. This is used in a non-bulk free.
234 void FreeSlot(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700235 // Marks the slots to free in the bulk free bit map. Returns the bracket size.
236 size_t MarkBulkFreeBitMap(void* ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700237 // Marks the slots to free in the thread-local free bit map.
238 void MarkThreadLocalFreeBitMap(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700239 // Last word mask, all of the bits in the last word which aren't valid slots are set to
240 // optimize allocation path.
Andreas Gampe59e67602014-04-25 17:15:12 -0700241 static uint32_t GetBitmapLastVectorMask(size_t num_slots, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700242 // Returns true if all the slots in the run are not in use.
243 bool IsAllFree();
244 // Returns true if all the slots in the run are in use.
245 bool IsFull();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800246 // Returns true if the bulk free bit map is clean.
247 bool IsBulkFreeBitmapClean();
248 // Returns true if the thread local free bit map is clean.
249 bool IsThreadLocalFreeBitmapClean();
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700250 // Set the alloc_bit_map_ bits for slots that are past the end of the run.
251 void SetAllocBitMapBitsForInvalidSlots();
252 // Zero the run's data.
253 void ZeroData();
254 // Zero the run's header.
255 void ZeroHeader();
256 // Fill the alloc bitmap with 1s.
257 void FillAllocBitMap();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700258 // Iterate over all the slots and apply the given function.
259 void InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), void* arg);
260 // Dump the run metadata for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800261 std::string Dump();
262 // Verify for debugging.
263 void Verify(Thread* self, RosAlloc* rosalloc)
264 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
265 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700266
267 private:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700268 // The common part of MarkFreeBitMap() and MarkThreadLocalFreeBitMap(). Returns the bracket
269 // size.
270 size_t MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, const char* caller_name);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800271 // Turns the bit map into a string for debugging.
272 static std::string BitMapToStr(uint32_t* bit_map_base, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700273 };
274
275 // The magic number for a run.
276 static const byte kMagicNum = 42;
277 // The magic number for free pages.
278 static const byte kMagicNumFree = 43;
279 // The number of size brackets. Sync this with the length of Thread::rosalloc_runs_.
280 static const size_t kNumOfSizeBrackets = 34;
281 // The number of smaller size brackets that are 16 bytes apart.
282 static const size_t kNumOfQuantumSizeBrackets = 32;
283 // The sizes (the slot sizes, in bytes) of the size brackets.
284 static size_t bracketSizes[kNumOfSizeBrackets];
285 // The numbers of pages that are used for runs for each size bracket.
286 static size_t numOfPages[kNumOfSizeBrackets];
287 // The numbers of slots of the runs for each size bracket.
288 static size_t numOfSlots[kNumOfSizeBrackets];
289 // The header sizes in bytes of the runs for each size bracket.
290 static size_t headerSizes[kNumOfSizeBrackets];
291 // The byte offsets of the bulk free bit maps of the runs for each size bracket.
292 static size_t bulkFreeBitMapOffsets[kNumOfSizeBrackets];
293 // The byte offsets of the thread-local free bit maps of the runs for each size bracket.
294 static size_t threadLocalFreeBitMapOffsets[kNumOfSizeBrackets];
295
296 // Initialize the run specs (the above arrays).
297 static void Initialize();
298 static bool initialized_;
299
300 // Returns the byte size of the bracket size from the index.
301 static size_t IndexToBracketSize(size_t idx) {
302 DCHECK(idx < kNumOfSizeBrackets);
303 return bracketSizes[idx];
304 }
305 // Returns the index of the size bracket from the bracket size.
306 static size_t BracketSizeToIndex(size_t size) {
307 DCHECK(16 <= size && ((size < 1 * KB && size % 16 == 0) || size == 1 * KB || size == 2 * KB));
308 size_t idx;
309 if (UNLIKELY(size == 1 * KB)) {
310 idx = kNumOfSizeBrackets - 2;
311 } else if (UNLIKELY(size == 2 * KB)) {
312 idx = kNumOfSizeBrackets - 1;
313 } else {
314 DCHECK(size < 1 * KB);
315 DCHECK_EQ(size % 16, static_cast<size_t>(0));
316 idx = size / 16 - 1;
317 }
318 DCHECK(bracketSizes[idx] == size);
319 return idx;
320 }
321 // Rounds up the size up the nearest bracket size.
322 static size_t RoundToBracketSize(size_t size) {
323 DCHECK(size <= kLargeSizeThreshold);
324 if (LIKELY(size <= 512)) {
325 return RoundUp(size, 16);
326 } else if (512 < size && size <= 1 * KB) {
327 return 1 * KB;
328 } else {
329 DCHECK(1 * KB < size && size <= 2 * KB);
330 return 2 * KB;
331 }
332 }
333 // Returns the size bracket index from the byte size with rounding.
334 static size_t SizeToIndex(size_t size) {
335 DCHECK(size <= kLargeSizeThreshold);
336 if (LIKELY(size <= 512)) {
337 return RoundUp(size, 16) / 16 - 1;
338 } else if (512 < size && size <= 1 * KB) {
339 return kNumOfSizeBrackets - 2;
340 } else {
341 DCHECK(1 * KB < size && size <= 2 * KB);
342 return kNumOfSizeBrackets - 1;
343 }
344 }
345 // A combination of SizeToIndex() and RoundToBracketSize().
346 static size_t SizeToIndexAndBracketSize(size_t size, size_t* bracket_size_out) {
347 DCHECK(size <= kLargeSizeThreshold);
348 if (LIKELY(size <= 512)) {
349 size_t bracket_size = RoundUp(size, 16);
350 *bracket_size_out = bracket_size;
351 size_t idx = bracket_size / 16 - 1;
352 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
353 return idx;
354 } else if (512 < size && size <= 1 * KB) {
355 size_t bracket_size = 1024;
356 *bracket_size_out = bracket_size;
357 size_t idx = kNumOfSizeBrackets - 2;
358 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
359 return idx;
360 } else {
361 DCHECK(1 * KB < size && size <= 2 * KB);
362 size_t bracket_size = 2048;
363 *bracket_size_out = bracket_size;
364 size_t idx = kNumOfSizeBrackets - 1;
365 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
366 return idx;
367 }
368 }
369 // Returns the page map index from an address. Requires that the
370 // address is page size aligned.
371 size_t ToPageMapIndex(const void* addr) const {
372 DCHECK(base_ <= addr && addr < base_ + capacity_);
373 size_t byte_offset = reinterpret_cast<const byte*>(addr) - base_;
374 DCHECK_EQ(byte_offset % static_cast<size_t>(kPageSize), static_cast<size_t>(0));
375 return byte_offset / kPageSize;
376 }
377 // Returns the page map index from an address with rounding.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700378 size_t RoundDownToPageMapIndex(void* addr) const {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700379 DCHECK(base_ <= addr && addr < reinterpret_cast<byte*>(base_) + capacity_);
380 return (reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(base_)) / kPageSize;
381 }
382
383 // A memory allocation request larger than this size is treated as a large object and allocated
384 // at a page-granularity.
385 static const size_t kLargeSizeThreshold = 2048;
386
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800387 // If true, check that the returned memory is actually zero.
388 static constexpr bool kCheckZeroMemory = kIsDebugBuild;
389
390 // If true, log verbose details of operations.
391 static constexpr bool kTraceRosAlloc = false;
392
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700393 struct hash_run {
394 size_t operator()(const RosAlloc::Run* r) const {
395 return reinterpret_cast<size_t>(r);
396 }
397 };
398
399 struct eq_run {
400 bool operator()(const RosAlloc::Run* r1, const RosAlloc::Run* r2) const {
401 return r1 == r2;
402 }
403 };
404
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800405 public:
406 // Different page release modes.
407 enum PageReleaseMode {
408 kPageReleaseModeNone, // Release no empty pages.
409 kPageReleaseModeEnd, // Release empty pages at the end of the space.
410 kPageReleaseModeSize, // Release empty pages that are larger than the threshold.
411 kPageReleaseModeSizeAndEnd, // Release empty pages that are larger than the threshold or
412 // at the end of the space.
413 kPageReleaseModeAll, // Release all empty pages.
414 };
415
416 // The default value for page_release_size_threshold_.
417 static constexpr size_t kDefaultPageReleaseSizeThreshold = 4 * MB;
418
Mathieu Chartier0651d412014-04-29 14:37:57 -0700419 // We use thread-local runs for the size Brackets whose indexes
420 // are less than this index. We use shared (current) runs for the rest.
421 static const size_t kNumThreadLocalSizeBrackets = 11;
422
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800423 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700424 // The base address of the memory region that's managed by this allocator.
425 byte* base_;
426
427 // The footprint in bytes of the currently allocated portion of the
428 // memory region.
429 size_t footprint_;
430
431 // The maximum footprint. The address, base_ + capacity_, indicates
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800432 // the end of the memory region that's currently managed by this allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700433 size_t capacity_;
434
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800435 // The maximum capacity. The address, base_ + max_capacity_, indicates
436 // the end of the memory region that's ever managed by this allocator.
437 size_t max_capacity_;
438
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700439 // The run sets that hold the runs whose slots are not all
440 // full. non_full_runs_[i] is guarded by size_bracket_locks_[i].
441 std::set<Run*> non_full_runs_[kNumOfSizeBrackets];
442 // The run sets that hold the runs whose slots are all full. This is
443 // debug only. full_runs_[i] is guarded by size_bracket_locks_[i].
Ian Rogers700a4022014-05-19 16:49:03 -0700444 std::unordered_set<Run*, hash_run, eq_run> full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700445 // The set of free pages.
446 std::set<FreePageRun*> free_page_runs_ GUARDED_BY(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700447 // The dedicated full run, it is always full and shared by all threads when revoking happens.
448 // This is an optimization since enables us to avoid a null check for revoked runs.
449 static Run* dedicated_full_run_;
450 // Using size_t to ensure that it is at least word aligned.
451 static size_t dedicated_full_run_storage_[];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700452 // The current runs where the allocations are first attempted for
453 // the size brackes that do not use thread-local
454 // runs. current_runs_[i] is guarded by size_bracket_locks_[i].
455 Run* current_runs_[kNumOfSizeBrackets];
456 // The mutexes, one per size bracket.
457 Mutex* size_bracket_locks_[kNumOfSizeBrackets];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700458 // Bracket lock names (since locks only have char* names).
459 std::string size_bracket_lock_names[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700460 // The types of page map entries.
461 enum {
462 kPageMapEmpty = 0, // Not allocated.
463 kPageMapRun = 1, // The beginning of a run.
464 kPageMapRunPart = 2, // The non-beginning part of a run.
465 kPageMapLargeObject = 3, // The beginning of a large object.
466 kPageMapLargeObjectPart = 4, // The non-beginning part of a large object.
467 };
468 // The table that indicates what pages are currently used for.
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800469 byte* page_map_; // No GUARDED_BY(lock_) for kReadPageMapEntryWithoutLockInBulkFree.
470 size_t page_map_size_;
471 size_t max_page_map_size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700472 std::unique_ptr<MemMap> page_map_mem_map_;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800473
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700474 // The table that indicates the size of free page runs. These sizes
475 // are stored here to avoid storing in the free page header and
476 // release backing pages.
477 std::vector<size_t> free_page_run_size_map_ GUARDED_BY(lock_);
478 // 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
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700493 // The base address of the memory region that's managed by this allocator.
494 byte* Begin() { return base_; }
495 // The end address of the memory region that's managed by this allocator.
496 byte* End() { return base_ + capacity_; }
497
498 // Page-granularity alloc/free
499 void* AllocPages(Thread* self, size_t num_pages, byte page_map_type)
500 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700501 // Returns how many bytes were freed.
502 size_t FreePages(Thread* self, void* ptr, bool already_zero) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700503
504 // Allocate/free a run slot.
505 void* AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated)
506 LOCKS_EXCLUDED(lock_);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700507 // Allocate/free a run slot without acquiring locks.
508 // TODO: EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
509 void* AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated)
510 LOCKS_EXCLUDED(lock_);
511 void* AllocFromCurrentRunUnlocked(Thread* self, size_t idx);
512
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700513 // Returns the bracket size.
514 size_t FreeFromRun(Thread* self, void* ptr, Run* run)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700515 LOCKS_EXCLUDED(lock_);
516
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700517 // Used to allocate a new thread local run for a size bracket.
518 Run* AllocRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
519
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700520 // Used to acquire a new/reused run for a size bracket. Used when a
521 // thread-local or current run gets full.
522 Run* RefillRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
523
524 // The internal of non-bulk Free().
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700525 size_t FreeInternal(Thread* self, void* ptr) LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700526
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800527 // Allocates large objects.
528 void* AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated) LOCKS_EXCLUDED(lock_);
529
Mathieu Chartier0651d412014-04-29 14:37:57 -0700530 // Revoke a run by adding it to non_full_runs_ or freeing the pages.
531 void RevokeRun(Thread* self, size_t idx, Run* run);
532
533 // Revoke the current runs which share an index with the thread local runs.
534 void RevokeThreadUnsafeCurrentRuns();
535
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700536 public:
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800537 RosAlloc(void* base, size_t capacity, size_t max_capacity,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800538 PageReleaseMode page_release_mode,
539 size_t page_release_size_threshold = kDefaultPageReleaseSizeThreshold);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800540 ~RosAlloc();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700541 // If kThreadUnsafe is true then the allocator may avoid acquiring some locks as an optimization.
542 // If used, this may cause race conditions if multiple threads are allocating at the same time.
543 template<bool kThreadSafe = true>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700544 void* Alloc(Thread* self, size_t size, size_t* bytes_allocated)
545 LOCKS_EXCLUDED(lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700546 size_t Free(Thread* self, void* ptr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700547 LOCKS_EXCLUDED(bulk_free_lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700548 size_t BulkFree(Thread* self, void** ptrs, size_t num_ptrs)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700549 LOCKS_EXCLUDED(bulk_free_lock_);
550 // Returns the size of the allocated slot for a given allocated memory chunk.
551 size_t UsableSize(void* ptr);
552 // Returns the size of the allocated slot for a given size.
553 size_t UsableSize(size_t bytes) {
554 if (UNLIKELY(bytes > kLargeSizeThreshold)) {
555 return RoundUp(bytes, kPageSize);
556 } else {
557 return RoundToBracketSize(bytes);
558 }
559 }
560 // Try to reduce the current footprint by releasing the free page
561 // run at the end of the memory region, if any.
562 bool Trim();
563 // Iterates over all the memory slots and apply the given function.
564 void InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
565 void* arg)
566 LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700567 // Release empty pages.
568 size_t ReleasePages() LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700569 // Returns the current footprint.
570 size_t Footprint() LOCKS_EXCLUDED(lock_);
571 // Returns the current capacity, maximum footprint.
572 size_t FootprintLimit() LOCKS_EXCLUDED(lock_);
573 // Update the current capacity.
574 void SetFootprintLimit(size_t bytes) LOCKS_EXCLUDED(lock_);
575 // Releases the thread-local runs assigned to the given thread back to the common set of runs.
576 void RevokeThreadLocalRuns(Thread* thread);
577 // Releases the thread-local runs assigned to all the threads back to the common set of runs.
578 void RevokeAllThreadLocalRuns() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700579 // Assert the thread local runs of a thread are revoked.
580 void AssertThreadLocalRunsAreRevoked(Thread* thread);
581 // Assert all the thread local runs are revoked.
582 void AssertAllThreadLocalRunsAreRevoked() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700583 // Dumps the page map for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800584 std::string DumpPageMap() EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700585 static Run* GetDedicatedFullRun() {
586 return dedicated_full_run_;
587 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700588
589 // Callbacks for InspectAll that will count the number of bytes
590 // allocated and objects allocated, respectively.
591 static void BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
592 static void ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800593
594 bool DoesReleaseAllPages() const {
595 return page_release_mode_ == kPageReleaseModeAll;
596 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800597
598 // Verify for debugging.
599 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700600};
601
602} // namespace allocator
603} // namespace gc
604} // namespace art
605
606#endif // ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_