blob: 2fbd97a8d5fb4c4f6c566f2e0bd253dd0170d51c [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:
47 byte magic_num_; // The magic number used for debugging only.
48
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_) {
53 const byte* fpr_base = reinterpret_cast<const byte*>(this);
54 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));
63 byte* fpr_base = reinterpret_cast<byte*>(this);
64 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_) {
71 byte* fpr_base = reinterpret_cast<byte*>(this);
72 byte* end = fpr_base + ByteSize(rosalloc);
73 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_) {
81 return reinterpret_cast<byte*>(this) + ByteSize(rosalloc) == rosalloc->base_ + rosalloc->footprint_;
82 }
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_) {
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800101 byte* start = reinterpret_cast<byte*>(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 }
108 };
109
110 // Represents a run of memory slots of the same size.
111 //
112 // A run's memory layout:
113 //
114 // +-------------------+
115 // | magic_num |
116 // +-------------------+
117 // | size_bracket_idx |
118 // +-------------------+
119 // | is_thread_local |
120 // +-------------------+
121 // | to_be_bulk_freed |
122 // +-------------------+
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700123 // | top_bitmap_idx |
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700124 // +-------------------+
125 // | |
126 // | alloc bit map |
127 // | |
128 // +-------------------+
129 // | |
130 // | bulk free bit map |
131 // | |
132 // +-------------------+
133 // | |
134 // | thread-local free |
135 // | bit map |
136 // | |
137 // +-------------------+
138 // | padding due to |
139 // | alignment |
140 // +-------------------+
141 // | slot 0 |
142 // +-------------------+
143 // | slot 1 |
144 // +-------------------+
145 // | slot 2 |
146 // +-------------------+
147 // ...
148 // +-------------------+
149 // | last slot |
150 // +-------------------+
151 //
152 class Run {
153 public:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700154 byte magic_num_; // The magic number used for debugging.
155 byte size_bracket_idx_; // The index of the size bracket of this run.
156 byte is_thread_local_; // True if this run is used as a thread-local run.
157 byte to_be_bulk_freed_; // Used within BulkFree() to flag a run that's involved with a bulk free.
158 uint32_t first_search_vec_idx_; // The index of the first bitmap vector which may contain an available slot.
159 uint32_t alloc_bit_map_[0]; // The bit map that allocates if each slot is in use.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700160
161 // bulk_free_bit_map_[] : The bit map that is used for GC to
162 // temporarily mark the slots to free without using a lock. After
163 // all the slots to be freed in a run are marked, all those slots
164 // get freed in bulk with one locking per run, as opposed to one
165 // locking per slot to minimize the lock contention. This is used
166 // within BulkFree().
167
168 // thread_local_free_bit_map_[] : The bit map that is used for GC
169 // to temporarily mark the slots to free in a thread-local run
170 // without using a lock (without synchronizing the thread that
171 // owns the thread-local run.) When the thread-local run becomes
172 // full, the thread will check this bit map and update the
173 // allocation bit map of the run (that is, the slots get freed.)
174
175 // Returns the byte size of the header except for the bit maps.
176 static size_t fixed_header_size() {
177 Run temp;
178 size_t size = reinterpret_cast<byte*>(&temp.alloc_bit_map_) - reinterpret_cast<byte*>(&temp);
179 DCHECK_EQ(size, static_cast<size_t>(8));
180 return size;
181 }
182 // Returns the base address of the free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800183 uint32_t* BulkFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700184 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + bulkFreeBitMapOffsets[size_bracket_idx_]);
185 }
186 // Returns the base address of the thread local free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800187 uint32_t* ThreadLocalFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700188 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + threadLocalFreeBitMapOffsets[size_bracket_idx_]);
189 }
190 void* End() {
191 return reinterpret_cast<byte*>(this) + kPageSize * numOfPages[size_bracket_idx_];
192 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700193 // Returns the number of bitmap words per run.
194 size_t NumberOfBitmapVectors() const {
195 return RoundUp(numOfSlots[size_bracket_idx_], 32) / 32;
196 }
197 void SetIsThreadLocal(bool is_thread_local) {
198 is_thread_local_ = is_thread_local ? 1 : 0;
199 }
200 bool IsThreadLocal() const {
201 return is_thread_local_ != 0;
202 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700203 // Frees slots in the allocation bit map with regard to the
204 // thread-local free bit map. Used when a thread-local run becomes
205 // full.
206 bool MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out);
207 // Frees slots in the allocation bit map with regard to the bulk
208 // free bit map. Used in a bulk free.
209 void MergeBulkFreeBitMapIntoAllocBitMap();
210 // Unions the slots to be freed in the free bit map into the
211 // thread-local free bit map. In a bulk free, as a two-step
212 // process, GC will first record all the slots to free in a run in
213 // the free bit map where it can write without a lock, and later
214 // acquire a lock once per run to union the bits of the free bit
215 // map to the thread-local free bit map.
216 void UnionBulkFreeBitMapToThreadLocalFreeBitMap();
217 // Allocates a slot in a run.
218 void* AllocSlot();
219 // Frees a slot in a run. This is used in a non-bulk free.
220 void FreeSlot(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700221 // Marks the slots to free in the bulk free bit map. Returns the bracket size.
222 size_t MarkBulkFreeBitMap(void* ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700223 // Marks the slots to free in the thread-local free bit map.
224 void MarkThreadLocalFreeBitMap(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700225 // Last word mask, all of the bits in the last word which aren't valid slots are set to
226 // optimize allocation path.
Andreas Gampe59e67602014-04-25 17:15:12 -0700227 static uint32_t GetBitmapLastVectorMask(size_t num_slots, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700228 // Returns true if all the slots in the run are not in use.
229 bool IsAllFree();
230 // Returns true if all the slots in the run are in use.
231 bool IsFull();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800232 // Returns true if the bulk free bit map is clean.
233 bool IsBulkFreeBitmapClean();
234 // Returns true if the thread local free bit map is clean.
235 bool IsThreadLocalFreeBitmapClean();
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700236 // Set the alloc_bit_map_ bits for slots that are past the end of the run.
237 void SetAllocBitMapBitsForInvalidSlots();
238 // Zero the run's data.
239 void ZeroData();
240 // Zero the run's header.
241 void ZeroHeader();
242 // Fill the alloc bitmap with 1s.
243 void FillAllocBitMap();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700244 // Iterate over all the slots and apply the given function.
245 void InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), void* arg);
246 // Dump the run metadata for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800247 std::string Dump();
248 // Verify for debugging.
249 void Verify(Thread* self, RosAlloc* rosalloc)
250 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
251 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700252
253 private:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700254 // The common part of MarkFreeBitMap() and MarkThreadLocalFreeBitMap(). Returns the bracket
255 // size.
256 size_t MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, const char* caller_name);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800257 // Turns the bit map into a string for debugging.
258 static std::string BitMapToStr(uint32_t* bit_map_base, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700259 };
260
261 // The magic number for a run.
262 static const byte kMagicNum = 42;
263 // The magic number for free pages.
264 static const byte kMagicNumFree = 43;
265 // The number of size brackets. Sync this with the length of Thread::rosalloc_runs_.
Ian Rogerse63db272014-07-15 15:36:11 -0700266 static const size_t kNumOfSizeBrackets = kNumRosAllocThreadLocalSizeBrackets;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700267 // The number of smaller size brackets that are 16 bytes apart.
268 static const size_t kNumOfQuantumSizeBrackets = 32;
269 // The sizes (the slot sizes, in bytes) of the size brackets.
270 static size_t bracketSizes[kNumOfSizeBrackets];
271 // The numbers of pages that are used for runs for each size bracket.
272 static size_t numOfPages[kNumOfSizeBrackets];
273 // The numbers of slots of the runs for each size bracket.
274 static size_t numOfSlots[kNumOfSizeBrackets];
275 // The header sizes in bytes of the runs for each size bracket.
276 static size_t headerSizes[kNumOfSizeBrackets];
277 // The byte offsets of the bulk free bit maps of the runs for each size bracket.
278 static size_t bulkFreeBitMapOffsets[kNumOfSizeBrackets];
279 // The byte offsets of the thread-local free bit maps of the runs for each size bracket.
280 static size_t threadLocalFreeBitMapOffsets[kNumOfSizeBrackets];
281
282 // Initialize the run specs (the above arrays).
283 static void Initialize();
284 static bool initialized_;
285
286 // Returns the byte size of the bracket size from the index.
287 static size_t IndexToBracketSize(size_t idx) {
288 DCHECK(idx < kNumOfSizeBrackets);
289 return bracketSizes[idx];
290 }
291 // Returns the index of the size bracket from the bracket size.
292 static size_t BracketSizeToIndex(size_t size) {
293 DCHECK(16 <= size && ((size < 1 * KB && size % 16 == 0) || size == 1 * KB || size == 2 * KB));
294 size_t idx;
295 if (UNLIKELY(size == 1 * KB)) {
296 idx = kNumOfSizeBrackets - 2;
297 } else if (UNLIKELY(size == 2 * KB)) {
298 idx = kNumOfSizeBrackets - 1;
299 } else {
300 DCHECK(size < 1 * KB);
301 DCHECK_EQ(size % 16, static_cast<size_t>(0));
302 idx = size / 16 - 1;
303 }
304 DCHECK(bracketSizes[idx] == size);
305 return idx;
306 }
307 // Rounds up the size up the nearest bracket size.
308 static size_t RoundToBracketSize(size_t size) {
309 DCHECK(size <= kLargeSizeThreshold);
310 if (LIKELY(size <= 512)) {
311 return RoundUp(size, 16);
312 } else if (512 < size && size <= 1 * KB) {
313 return 1 * KB;
314 } else {
315 DCHECK(1 * KB < size && size <= 2 * KB);
316 return 2 * KB;
317 }
318 }
319 // Returns the size bracket index from the byte size with rounding.
320 static size_t SizeToIndex(size_t size) {
321 DCHECK(size <= kLargeSizeThreshold);
322 if (LIKELY(size <= 512)) {
323 return RoundUp(size, 16) / 16 - 1;
324 } else if (512 < size && size <= 1 * KB) {
325 return kNumOfSizeBrackets - 2;
326 } else {
327 DCHECK(1 * KB < size && size <= 2 * KB);
328 return kNumOfSizeBrackets - 1;
329 }
330 }
331 // A combination of SizeToIndex() and RoundToBracketSize().
332 static size_t SizeToIndexAndBracketSize(size_t size, size_t* bracket_size_out) {
333 DCHECK(size <= kLargeSizeThreshold);
334 if (LIKELY(size <= 512)) {
335 size_t bracket_size = RoundUp(size, 16);
336 *bracket_size_out = bracket_size;
337 size_t idx = bracket_size / 16 - 1;
338 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
339 return idx;
340 } else if (512 < size && size <= 1 * KB) {
341 size_t bracket_size = 1024;
342 *bracket_size_out = bracket_size;
343 size_t idx = kNumOfSizeBrackets - 2;
344 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
345 return idx;
346 } else {
347 DCHECK(1 * KB < size && size <= 2 * KB);
348 size_t bracket_size = 2048;
349 *bracket_size_out = bracket_size;
350 size_t idx = kNumOfSizeBrackets - 1;
351 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
352 return idx;
353 }
354 }
355 // Returns the page map index from an address. Requires that the
356 // address is page size aligned.
357 size_t ToPageMapIndex(const void* addr) const {
358 DCHECK(base_ <= addr && addr < base_ + capacity_);
359 size_t byte_offset = reinterpret_cast<const byte*>(addr) - base_;
360 DCHECK_EQ(byte_offset % static_cast<size_t>(kPageSize), static_cast<size_t>(0));
361 return byte_offset / kPageSize;
362 }
363 // Returns the page map index from an address with rounding.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700364 size_t RoundDownToPageMapIndex(void* addr) const {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700365 DCHECK(base_ <= addr && addr < reinterpret_cast<byte*>(base_) + capacity_);
366 return (reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(base_)) / kPageSize;
367 }
368
369 // A memory allocation request larger than this size is treated as a large object and allocated
370 // at a page-granularity.
371 static const size_t kLargeSizeThreshold = 2048;
372
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800373 // If true, check that the returned memory is actually zero.
374 static constexpr bool kCheckZeroMemory = kIsDebugBuild;
375
376 // If true, log verbose details of operations.
377 static constexpr bool kTraceRosAlloc = false;
378
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700379 struct hash_run {
380 size_t operator()(const RosAlloc::Run* r) const {
381 return reinterpret_cast<size_t>(r);
382 }
383 };
384
385 struct eq_run {
386 bool operator()(const RosAlloc::Run* r1, const RosAlloc::Run* r2) const {
387 return r1 == r2;
388 }
389 };
390
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800391 public:
392 // Different page release modes.
393 enum PageReleaseMode {
394 kPageReleaseModeNone, // Release no empty pages.
395 kPageReleaseModeEnd, // Release empty pages at the end of the space.
396 kPageReleaseModeSize, // Release empty pages that are larger than the threshold.
397 kPageReleaseModeSizeAndEnd, // Release empty pages that are larger than the threshold or
398 // at the end of the space.
399 kPageReleaseModeAll, // Release all empty pages.
400 };
401
402 // The default value for page_release_size_threshold_.
403 static constexpr size_t kDefaultPageReleaseSizeThreshold = 4 * MB;
404
Mathieu Chartier0651d412014-04-29 14:37:57 -0700405 // We use thread-local runs for the size Brackets whose indexes
406 // are less than this index. We use shared (current) runs for the rest.
Mathieu Chartier58553c72014-09-16 16:25:55 -0700407
Mathieu Chartier0651d412014-04-29 14:37:57 -0700408 static const size_t kNumThreadLocalSizeBrackets = 11;
409
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800410 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700411 // The base address of the memory region that's managed by this allocator.
412 byte* base_;
413
414 // The footprint in bytes of the currently allocated portion of the
415 // memory region.
416 size_t footprint_;
417
418 // The maximum footprint. The address, base_ + capacity_, indicates
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800419 // the end of the memory region that's currently managed by this allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700420 size_t capacity_;
421
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800422 // The maximum capacity. The address, base_ + max_capacity_, indicates
423 // the end of the memory region that's ever managed by this allocator.
424 size_t max_capacity_;
425
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700426 // The run sets that hold the runs whose slots are not all
427 // full. non_full_runs_[i] is guarded by size_bracket_locks_[i].
Mathieu Chartier58553c72014-09-16 16:25:55 -0700428 AllocationTrackingSet<Run*, kAllocatorTagRosAlloc> non_full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700429 // The run sets that hold the runs whose slots are all full. This is
430 // debug only. full_runs_[i] is guarded by size_bracket_locks_[i].
Mathieu Chartier58553c72014-09-16 16:25:55 -0700431 std::unordered_set<Run*, hash_run, eq_run, TrackingAllocator<Run*, kAllocatorTagRosAlloc>>
432 full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700433 // The set of free pages.
Mathieu Chartier58553c72014-09-16 16:25:55 -0700434 AllocationTrackingSet<FreePageRun*, kAllocatorTagRosAlloc> free_page_runs_ GUARDED_BY(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700435 // The dedicated full run, it is always full and shared by all threads when revoking happens.
436 // This is an optimization since enables us to avoid a null check for revoked runs.
437 static Run* dedicated_full_run_;
438 // Using size_t to ensure that it is at least word aligned.
439 static size_t dedicated_full_run_storage_[];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700440 // The current runs where the allocations are first attempted for
441 // the size brackes that do not use thread-local
442 // runs. current_runs_[i] is guarded by size_bracket_locks_[i].
443 Run* current_runs_[kNumOfSizeBrackets];
444 // The mutexes, one per size bracket.
445 Mutex* size_bracket_locks_[kNumOfSizeBrackets];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700446 // Bracket lock names (since locks only have char* names).
Zuo Wangf37a88b2014-07-10 04:26:41 -0700447 std::string size_bracket_lock_names_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700448 // The types of page map entries.
449 enum {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700450 kPageMapReleased = 0, // Zero and released back to the OS.
451 kPageMapEmpty, // Zero but probably dirty.
452 kPageMapRun, // The beginning of a run.
453 kPageMapRunPart, // The non-beginning part of a run.
454 kPageMapLargeObject, // The beginning of a large object.
455 kPageMapLargeObjectPart, // The non-beginning part of a large object.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700456 };
457 // The table that indicates what pages are currently used for.
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700458 volatile byte* page_map_; // No GUARDED_BY(lock_) for kReadPageMapEntryWithoutLockInBulkFree.
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800459 size_t page_map_size_;
460 size_t max_page_map_size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700461 std::unique_ptr<MemMap> page_map_mem_map_;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800462
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700463 // The table that indicates the size of free page runs. These sizes
464 // are stored here to avoid storing in the free page header and
465 // release backing pages.
Mathieu Chartier58553c72014-09-16 16:25:55 -0700466 std::vector<size_t, TrackingAllocator<size_t, kAllocatorTagRosAlloc>> free_page_run_size_map_
467 GUARDED_BY(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700468 // The global lock. Used to guard the page map, the free page set,
469 // and the footprint.
470 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
471 // The reader-writer lock to allow one bulk free at a time while
Hiroshi Yamauchi70f60042014-02-03 12:31:29 -0800472 // allowing multiple individual frees at the same time. Also, this
473 // is used to avoid race conditions between BulkFree() and
474 // RevokeThreadLocalRuns() on the bulk free bitmaps.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700475 ReaderWriterMutex bulk_free_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
476
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800477 // The page release mode.
478 const PageReleaseMode page_release_mode_;
479 // Under kPageReleaseModeSize(AndEnd), if the free page run size is
480 // greater than or equal to this value, release pages.
481 const size_t page_release_size_threshold_;
482
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700483 // The base address of the memory region that's managed by this allocator.
484 byte* Begin() { return base_; }
485 // The end address of the memory region that's managed by this allocator.
486 byte* End() { return base_ + capacity_; }
487
488 // Page-granularity alloc/free
489 void* AllocPages(Thread* self, size_t num_pages, byte page_map_type)
490 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700491 // Returns how many bytes were freed.
492 size_t FreePages(Thread* self, void* ptr, bool already_zero) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700493
494 // Allocate/free a run slot.
495 void* AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated)
496 LOCKS_EXCLUDED(lock_);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700497 // Allocate/free a run slot without acquiring locks.
498 // TODO: EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
499 void* AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated)
500 LOCKS_EXCLUDED(lock_);
501 void* AllocFromCurrentRunUnlocked(Thread* self, size_t idx);
502
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700503 // Returns the bracket size.
504 size_t FreeFromRun(Thread* self, void* ptr, Run* run)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700505 LOCKS_EXCLUDED(lock_);
506
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700507 // Used to allocate a new thread local run for a size bracket.
508 Run* AllocRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
509
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700510 // Used to acquire a new/reused run for a size bracket. Used when a
511 // thread-local or current run gets full.
512 Run* RefillRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
513
514 // The internal of non-bulk Free().
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700515 size_t FreeInternal(Thread* self, void* ptr) LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700516
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800517 // Allocates large objects.
518 void* AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated) LOCKS_EXCLUDED(lock_);
519
Mathieu Chartier0651d412014-04-29 14:37:57 -0700520 // Revoke a run by adding it to non_full_runs_ or freeing the pages.
521 void RevokeRun(Thread* self, size_t idx, Run* run);
522
523 // Revoke the current runs which share an index with the thread local runs.
524 void RevokeThreadUnsafeCurrentRuns();
525
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700526 // Release a range of pages.
527 size_t ReleasePageRange(byte* start, byte* end) EXCLUSIVE_LOCKS_REQUIRED(lock_);
528
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700529 public:
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800530 RosAlloc(void* base, size_t capacity, size_t max_capacity,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800531 PageReleaseMode page_release_mode,
532 size_t page_release_size_threshold = kDefaultPageReleaseSizeThreshold);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800533 ~RosAlloc();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700534 // If kThreadUnsafe is true then the allocator may avoid acquiring some locks as an optimization.
535 // If used, this may cause race conditions if multiple threads are allocating at the same time.
536 template<bool kThreadSafe = true>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700537 void* Alloc(Thread* self, size_t size, size_t* bytes_allocated)
538 LOCKS_EXCLUDED(lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700539 size_t Free(Thread* self, void* ptr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700540 LOCKS_EXCLUDED(bulk_free_lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700541 size_t BulkFree(Thread* self, void** ptrs, size_t num_ptrs)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700542 LOCKS_EXCLUDED(bulk_free_lock_);
543 // Returns the size of the allocated slot for a given allocated memory chunk.
544 size_t UsableSize(void* ptr);
545 // Returns the size of the allocated slot for a given size.
546 size_t UsableSize(size_t bytes) {
547 if (UNLIKELY(bytes > kLargeSizeThreshold)) {
548 return RoundUp(bytes, kPageSize);
549 } else {
550 return RoundToBracketSize(bytes);
551 }
552 }
553 // Try to reduce the current footprint by releasing the free page
554 // run at the end of the memory region, if any.
555 bool Trim();
556 // Iterates over all the memory slots and apply the given function.
557 void InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
558 void* arg)
559 LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700560 // Release empty pages.
561 size_t ReleasePages() LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700562 // Returns the current footprint.
563 size_t Footprint() LOCKS_EXCLUDED(lock_);
564 // Returns the current capacity, maximum footprint.
565 size_t FootprintLimit() LOCKS_EXCLUDED(lock_);
566 // Update the current capacity.
567 void SetFootprintLimit(size_t bytes) LOCKS_EXCLUDED(lock_);
568 // Releases the thread-local runs assigned to the given thread back to the common set of runs.
569 void RevokeThreadLocalRuns(Thread* thread);
570 // Releases the thread-local runs assigned to all the threads back to the common set of runs.
571 void RevokeAllThreadLocalRuns() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700572 // Assert the thread local runs of a thread are revoked.
573 void AssertThreadLocalRunsAreRevoked(Thread* thread);
574 // Assert all the thread local runs are revoked.
575 void AssertAllThreadLocalRunsAreRevoked() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700576 // Dumps the page map for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800577 std::string DumpPageMap() EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700578 static Run* GetDedicatedFullRun() {
579 return dedicated_full_run_;
580 }
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700581 bool IsFreePage(size_t idx) const {
582 DCHECK_LT(idx, capacity_ / kPageSize);
583 byte pm_type = page_map_[idx];
584 return pm_type == kPageMapReleased || pm_type == kPageMapEmpty;
585 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700586
587 // Callbacks for InspectAll that will count the number of bytes
588 // allocated and objects allocated, respectively.
589 static void BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
590 static void ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800591
592 bool DoesReleaseAllPages() const {
593 return page_release_mode_ == kPageReleaseModeAll;
594 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800595
596 // Verify for debugging.
597 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700598
599 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes);
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_