blob: c0ab151d6cfefc1dbc63c27db72402be33b08550 [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));
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700102 if (ShouldReleasePages(rosalloc)) {
103 rosalloc->ReleasePageRange(start, start + byte_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700104 }
105 }
106 };
107
108 // Represents a run of memory slots of the same size.
109 //
110 // A run's memory layout:
111 //
112 // +-------------------+
113 // | magic_num |
114 // +-------------------+
115 // | size_bracket_idx |
116 // +-------------------+
117 // | is_thread_local |
118 // +-------------------+
119 // | to_be_bulk_freed |
120 // +-------------------+
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700121 // | top_bitmap_idx |
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700122 // +-------------------+
123 // | |
124 // | alloc bit map |
125 // | |
126 // +-------------------+
127 // | |
128 // | bulk free bit map |
129 // | |
130 // +-------------------+
131 // | |
132 // | thread-local free |
133 // | bit map |
134 // | |
135 // +-------------------+
136 // | padding due to |
137 // | alignment |
138 // +-------------------+
139 // | slot 0 |
140 // +-------------------+
141 // | slot 1 |
142 // +-------------------+
143 // | slot 2 |
144 // +-------------------+
145 // ...
146 // +-------------------+
147 // | last slot |
148 // +-------------------+
149 //
150 class Run {
151 public:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700152 byte magic_num_; // The magic number used for debugging.
153 byte size_bracket_idx_; // The index of the size bracket of this run.
154 byte is_thread_local_; // True if this run is used as a thread-local run.
155 byte to_be_bulk_freed_; // Used within BulkFree() to flag a run that's involved with a bulk free.
156 uint32_t first_search_vec_idx_; // The index of the first bitmap vector which may contain an available slot.
157 uint32_t alloc_bit_map_[0]; // The bit map that allocates if each slot is in use.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700158
159 // bulk_free_bit_map_[] : The bit map that is used for GC to
160 // temporarily mark the slots to free without using a lock. After
161 // all the slots to be freed in a run are marked, all those slots
162 // get freed in bulk with one locking per run, as opposed to one
163 // locking per slot to minimize the lock contention. This is used
164 // within BulkFree().
165
166 // thread_local_free_bit_map_[] : The bit map that is used for GC
167 // to temporarily mark the slots to free in a thread-local run
168 // without using a lock (without synchronizing the thread that
169 // owns the thread-local run.) When the thread-local run becomes
170 // full, the thread will check this bit map and update the
171 // allocation bit map of the run (that is, the slots get freed.)
172
173 // Returns the byte size of the header except for the bit maps.
174 static size_t fixed_header_size() {
175 Run temp;
176 size_t size = reinterpret_cast<byte*>(&temp.alloc_bit_map_) - reinterpret_cast<byte*>(&temp);
177 DCHECK_EQ(size, static_cast<size_t>(8));
178 return size;
179 }
180 // Returns the base address of the free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800181 uint32_t* BulkFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700182 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + bulkFreeBitMapOffsets[size_bracket_idx_]);
183 }
184 // Returns the base address of the thread local free bit map.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800185 uint32_t* ThreadLocalFreeBitMap() {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700186 return reinterpret_cast<uint32_t*>(reinterpret_cast<byte*>(this) + threadLocalFreeBitMapOffsets[size_bracket_idx_]);
187 }
188 void* End() {
189 return reinterpret_cast<byte*>(this) + kPageSize * numOfPages[size_bracket_idx_];
190 }
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700191 // Returns the number of bitmap words per run.
192 size_t NumberOfBitmapVectors() const {
193 return RoundUp(numOfSlots[size_bracket_idx_], 32) / 32;
194 }
195 void SetIsThreadLocal(bool is_thread_local) {
196 is_thread_local_ = is_thread_local ? 1 : 0;
197 }
198 bool IsThreadLocal() const {
199 return is_thread_local_ != 0;
200 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700201 // Frees slots in the allocation bit map with regard to the
202 // thread-local free bit map. Used when a thread-local run becomes
203 // full.
204 bool MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out);
205 // Frees slots in the allocation bit map with regard to the bulk
206 // free bit map. Used in a bulk free.
207 void MergeBulkFreeBitMapIntoAllocBitMap();
208 // Unions the slots to be freed in the free bit map into the
209 // thread-local free bit map. In a bulk free, as a two-step
210 // process, GC will first record all the slots to free in a run in
211 // the free bit map where it can write without a lock, and later
212 // acquire a lock once per run to union the bits of the free bit
213 // map to the thread-local free bit map.
214 void UnionBulkFreeBitMapToThreadLocalFreeBitMap();
215 // Allocates a slot in a run.
216 void* AllocSlot();
217 // Frees a slot in a run. This is used in a non-bulk free.
218 void FreeSlot(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700219 // Marks the slots to free in the bulk free bit map. Returns the bracket size.
220 size_t MarkBulkFreeBitMap(void* ptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700221 // Marks the slots to free in the thread-local free bit map.
222 void MarkThreadLocalFreeBitMap(void* ptr);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700223 // Last word mask, all of the bits in the last word which aren't valid slots are set to
224 // optimize allocation path.
Andreas Gampe59e67602014-04-25 17:15:12 -0700225 static uint32_t GetBitmapLastVectorMask(size_t num_slots, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700226 // Returns true if all the slots in the run are not in use.
227 bool IsAllFree();
228 // Returns true if all the slots in the run are in use.
229 bool IsFull();
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800230 // Returns true if the bulk free bit map is clean.
231 bool IsBulkFreeBitmapClean();
232 // Returns true if the thread local free bit map is clean.
233 bool IsThreadLocalFreeBitmapClean();
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700234 // Set the alloc_bit_map_ bits for slots that are past the end of the run.
235 void SetAllocBitMapBitsForInvalidSlots();
236 // Zero the run's data.
237 void ZeroData();
238 // Zero the run's header.
239 void ZeroHeader();
240 // Fill the alloc bitmap with 1s.
241 void FillAllocBitMap();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700242 // Iterate over all the slots and apply the given function.
243 void InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), void* arg);
244 // Dump the run metadata for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800245 std::string Dump();
246 // Verify for debugging.
247 void Verify(Thread* self, RosAlloc* rosalloc)
248 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
249 EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700250
251 private:
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700252 // The common part of MarkFreeBitMap() and MarkThreadLocalFreeBitMap(). Returns the bracket
253 // size.
254 size_t MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, const char* caller_name);
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800255 // Turns the bit map into a string for debugging.
256 static std::string BitMapToStr(uint32_t* bit_map_base, size_t num_vec);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700257 };
258
259 // The magic number for a run.
260 static const byte kMagicNum = 42;
261 // The magic number for free pages.
262 static const byte kMagicNumFree = 43;
263 // The number of size brackets. Sync this with the length of Thread::rosalloc_runs_.
264 static const size_t kNumOfSizeBrackets = 34;
265 // The number of smaller size brackets that are 16 bytes apart.
266 static const size_t kNumOfQuantumSizeBrackets = 32;
267 // The sizes (the slot sizes, in bytes) of the size brackets.
268 static size_t bracketSizes[kNumOfSizeBrackets];
269 // The numbers of pages that are used for runs for each size bracket.
270 static size_t numOfPages[kNumOfSizeBrackets];
271 // The numbers of slots of the runs for each size bracket.
272 static size_t numOfSlots[kNumOfSizeBrackets];
273 // The header sizes in bytes of the runs for each size bracket.
274 static size_t headerSizes[kNumOfSizeBrackets];
275 // The byte offsets of the bulk free bit maps of the runs for each size bracket.
276 static size_t bulkFreeBitMapOffsets[kNumOfSizeBrackets];
277 // The byte offsets of the thread-local free bit maps of the runs for each size bracket.
278 static size_t threadLocalFreeBitMapOffsets[kNumOfSizeBrackets];
279
280 // Initialize the run specs (the above arrays).
281 static void Initialize();
282 static bool initialized_;
283
284 // Returns the byte size of the bracket size from the index.
285 static size_t IndexToBracketSize(size_t idx) {
286 DCHECK(idx < kNumOfSizeBrackets);
287 return bracketSizes[idx];
288 }
289 // Returns the index of the size bracket from the bracket size.
290 static size_t BracketSizeToIndex(size_t size) {
291 DCHECK(16 <= size && ((size < 1 * KB && size % 16 == 0) || size == 1 * KB || size == 2 * KB));
292 size_t idx;
293 if (UNLIKELY(size == 1 * KB)) {
294 idx = kNumOfSizeBrackets - 2;
295 } else if (UNLIKELY(size == 2 * KB)) {
296 idx = kNumOfSizeBrackets - 1;
297 } else {
298 DCHECK(size < 1 * KB);
299 DCHECK_EQ(size % 16, static_cast<size_t>(0));
300 idx = size / 16 - 1;
301 }
302 DCHECK(bracketSizes[idx] == size);
303 return idx;
304 }
305 // Rounds up the size up the nearest bracket size.
306 static size_t RoundToBracketSize(size_t size) {
307 DCHECK(size <= kLargeSizeThreshold);
308 if (LIKELY(size <= 512)) {
309 return RoundUp(size, 16);
310 } else if (512 < size && size <= 1 * KB) {
311 return 1 * KB;
312 } else {
313 DCHECK(1 * KB < size && size <= 2 * KB);
314 return 2 * KB;
315 }
316 }
317 // Returns the size bracket index from the byte size with rounding.
318 static size_t SizeToIndex(size_t size) {
319 DCHECK(size <= kLargeSizeThreshold);
320 if (LIKELY(size <= 512)) {
321 return RoundUp(size, 16) / 16 - 1;
322 } else if (512 < size && size <= 1 * KB) {
323 return kNumOfSizeBrackets - 2;
324 } else {
325 DCHECK(1 * KB < size && size <= 2 * KB);
326 return kNumOfSizeBrackets - 1;
327 }
328 }
329 // A combination of SizeToIndex() and RoundToBracketSize().
330 static size_t SizeToIndexAndBracketSize(size_t size, size_t* bracket_size_out) {
331 DCHECK(size <= kLargeSizeThreshold);
332 if (LIKELY(size <= 512)) {
333 size_t bracket_size = RoundUp(size, 16);
334 *bracket_size_out = bracket_size;
335 size_t idx = bracket_size / 16 - 1;
336 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
337 return idx;
338 } else if (512 < size && size <= 1 * KB) {
339 size_t bracket_size = 1024;
340 *bracket_size_out = bracket_size;
341 size_t idx = kNumOfSizeBrackets - 2;
342 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
343 return idx;
344 } else {
345 DCHECK(1 * KB < size && size <= 2 * KB);
346 size_t bracket_size = 2048;
347 *bracket_size_out = bracket_size;
348 size_t idx = kNumOfSizeBrackets - 1;
349 DCHECK_EQ(bracket_size, IndexToBracketSize(idx));
350 return idx;
351 }
352 }
353 // Returns the page map index from an address. Requires that the
354 // address is page size aligned.
355 size_t ToPageMapIndex(const void* addr) const {
356 DCHECK(base_ <= addr && addr < base_ + capacity_);
357 size_t byte_offset = reinterpret_cast<const byte*>(addr) - base_;
358 DCHECK_EQ(byte_offset % static_cast<size_t>(kPageSize), static_cast<size_t>(0));
359 return byte_offset / kPageSize;
360 }
361 // Returns the page map index from an address with rounding.
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700362 size_t RoundDownToPageMapIndex(void* addr) const {
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700363 DCHECK(base_ <= addr && addr < reinterpret_cast<byte*>(base_) + capacity_);
364 return (reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(base_)) / kPageSize;
365 }
366
367 // A memory allocation request larger than this size is treated as a large object and allocated
368 // at a page-granularity.
369 static const size_t kLargeSizeThreshold = 2048;
370
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800371 // If true, check that the returned memory is actually zero.
372 static constexpr bool kCheckZeroMemory = kIsDebugBuild;
373
374 // If true, log verbose details of operations.
375 static constexpr bool kTraceRosAlloc = false;
376
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700377 struct hash_run {
378 size_t operator()(const RosAlloc::Run* r) const {
379 return reinterpret_cast<size_t>(r);
380 }
381 };
382
383 struct eq_run {
384 bool operator()(const RosAlloc::Run* r1, const RosAlloc::Run* r2) const {
385 return r1 == r2;
386 }
387 };
388
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800389 public:
390 // Different page release modes.
391 enum PageReleaseMode {
392 kPageReleaseModeNone, // Release no empty pages.
393 kPageReleaseModeEnd, // Release empty pages at the end of the space.
394 kPageReleaseModeSize, // Release empty pages that are larger than the threshold.
395 kPageReleaseModeSizeAndEnd, // Release empty pages that are larger than the threshold or
396 // at the end of the space.
397 kPageReleaseModeAll, // Release all empty pages.
398 };
399
400 // The default value for page_release_size_threshold_.
401 static constexpr size_t kDefaultPageReleaseSizeThreshold = 4 * MB;
402
Mathieu Chartier0651d412014-04-29 14:37:57 -0700403 // We use thread-local runs for the size Brackets whose indexes
404 // are less than this index. We use shared (current) runs for the rest.
405 static const size_t kNumThreadLocalSizeBrackets = 11;
406
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800407 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700408 // The base address of the memory region that's managed by this allocator.
409 byte* base_;
410
411 // The footprint in bytes of the currently allocated portion of the
412 // memory region.
413 size_t footprint_;
414
415 // The maximum footprint. The address, base_ + capacity_, indicates
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800416 // the end of the memory region that's currently managed by this allocator.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700417 size_t capacity_;
418
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800419 // The maximum capacity. The address, base_ + max_capacity_, indicates
420 // the end of the memory region that's ever managed by this allocator.
421 size_t max_capacity_;
422
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700423 // The run sets that hold the runs whose slots are not all
424 // full. non_full_runs_[i] is guarded by size_bracket_locks_[i].
425 std::set<Run*> non_full_runs_[kNumOfSizeBrackets];
426 // The run sets that hold the runs whose slots are all full. This is
427 // debug only. full_runs_[i] is guarded by size_bracket_locks_[i].
Ian Rogers700a4022014-05-19 16:49:03 -0700428 std::unordered_set<Run*, hash_run, eq_run> full_runs_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700429 // The set of free pages.
430 std::set<FreePageRun*> free_page_runs_ GUARDED_BY(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700431 // The dedicated full run, it is always full and shared by all threads when revoking happens.
432 // This is an optimization since enables us to avoid a null check for revoked runs.
433 static Run* dedicated_full_run_;
434 // Using size_t to ensure that it is at least word aligned.
435 static size_t dedicated_full_run_storage_[];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700436 // The current runs where the allocations are first attempted for
437 // the size brackes that do not use thread-local
438 // runs. current_runs_[i] is guarded by size_bracket_locks_[i].
439 Run* current_runs_[kNumOfSizeBrackets];
440 // The mutexes, one per size bracket.
441 Mutex* size_bracket_locks_[kNumOfSizeBrackets];
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700442 // Bracket lock names (since locks only have char* names).
Zuo Wangf37a88b2014-07-10 04:26:41 -0700443 std::string size_bracket_lock_names_[kNumOfSizeBrackets];
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700444 // The types of page map entries.
445 enum {
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700446 kPageMapReleased = 0, // Zero and released back to the OS.
447 kPageMapEmpty, // Zero but probably dirty.
448 kPageMapRun, // The beginning of a run.
449 kPageMapRunPart, // The non-beginning part of a run.
450 kPageMapLargeObject, // The beginning of a large object.
451 kPageMapLargeObjectPart, // The non-beginning part of a large object.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700452 };
453 // The table that indicates what pages are currently used for.
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700454 volatile byte* page_map_; // No GUARDED_BY(lock_) for kReadPageMapEntryWithoutLockInBulkFree.
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800455 size_t page_map_size_;
456 size_t max_page_map_size_;
Ian Rogers700a4022014-05-19 16:49:03 -0700457 std::unique_ptr<MemMap> page_map_mem_map_;
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800458
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700459 // The table that indicates the size of free page runs. These sizes
460 // are stored here to avoid storing in the free page header and
461 // release backing pages.
462 std::vector<size_t> free_page_run_size_map_ GUARDED_BY(lock_);
463 // The global lock. Used to guard the page map, the free page set,
464 // and the footprint.
465 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
466 // The reader-writer lock to allow one bulk free at a time while
Hiroshi Yamauchi70f60042014-02-03 12:31:29 -0800467 // allowing multiple individual frees at the same time. Also, this
468 // is used to avoid race conditions between BulkFree() and
469 // RevokeThreadLocalRuns() on the bulk free bitmaps.
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700470 ReaderWriterMutex bulk_free_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
471
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800472 // The page release mode.
473 const PageReleaseMode page_release_mode_;
474 // Under kPageReleaseModeSize(AndEnd), if the free page run size is
475 // greater than or equal to this value, release pages.
476 const size_t page_release_size_threshold_;
477
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700478 // The base address of the memory region that's managed by this allocator.
479 byte* Begin() { return base_; }
480 // The end address of the memory region that's managed by this allocator.
481 byte* End() { return base_ + capacity_; }
482
483 // Page-granularity alloc/free
484 void* AllocPages(Thread* self, size_t num_pages, byte page_map_type)
485 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700486 // Returns how many bytes were freed.
487 size_t FreePages(Thread* self, void* ptr, bool already_zero) EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700488
489 // Allocate/free a run slot.
490 void* AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated)
491 LOCKS_EXCLUDED(lock_);
Mathieu Chartier0651d412014-04-29 14:37:57 -0700492 // Allocate/free a run slot without acquiring locks.
493 // TODO: EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_)
494 void* AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated)
495 LOCKS_EXCLUDED(lock_);
496 void* AllocFromCurrentRunUnlocked(Thread* self, size_t idx);
497
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700498 // Returns the bracket size.
499 size_t FreeFromRun(Thread* self, void* ptr, Run* run)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700500 LOCKS_EXCLUDED(lock_);
501
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700502 // Used to allocate a new thread local run for a size bracket.
503 Run* AllocRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
504
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700505 // Used to acquire a new/reused run for a size bracket. Used when a
506 // thread-local or current run gets full.
507 Run* RefillRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_);
508
509 // The internal of non-bulk Free().
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700510 size_t FreeInternal(Thread* self, void* ptr) LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700511
Hiroshi Yamauchi3c2856e2013-11-22 13:42:53 -0800512 // Allocates large objects.
513 void* AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated) LOCKS_EXCLUDED(lock_);
514
Mathieu Chartier0651d412014-04-29 14:37:57 -0700515 // Revoke a run by adding it to non_full_runs_ or freeing the pages.
516 void RevokeRun(Thread* self, size_t idx, Run* run);
517
518 // Revoke the current runs which share an index with the thread local runs.
519 void RevokeThreadUnsafeCurrentRuns();
520
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700521 // Release a range of pages.
522 size_t ReleasePageRange(byte* start, byte* end) EXCLUSIVE_LOCKS_REQUIRED(lock_);
523
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700524 public:
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800525 RosAlloc(void* base, size_t capacity, size_t max_capacity,
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800526 PageReleaseMode page_release_mode,
527 size_t page_release_size_threshold = kDefaultPageReleaseSizeThreshold);
Mathieu Chartier661974a2014-01-09 11:23:53 -0800528 ~RosAlloc();
Mathieu Chartier0651d412014-04-29 14:37:57 -0700529 // If kThreadUnsafe is true then the allocator may avoid acquiring some locks as an optimization.
530 // If used, this may cause race conditions if multiple threads are allocating at the same time.
531 template<bool kThreadSafe = true>
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700532 void* Alloc(Thread* self, size_t size, size_t* bytes_allocated)
533 LOCKS_EXCLUDED(lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700534 size_t Free(Thread* self, void* ptr)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700535 LOCKS_EXCLUDED(bulk_free_lock_);
Mathieu Chartier8585bad2014-04-11 17:53:48 -0700536 size_t BulkFree(Thread* self, void** ptrs, size_t num_ptrs)
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700537 LOCKS_EXCLUDED(bulk_free_lock_);
538 // Returns the size of the allocated slot for a given allocated memory chunk.
539 size_t UsableSize(void* ptr);
540 // Returns the size of the allocated slot for a given size.
541 size_t UsableSize(size_t bytes) {
542 if (UNLIKELY(bytes > kLargeSizeThreshold)) {
543 return RoundUp(bytes, kPageSize);
544 } else {
545 return RoundToBracketSize(bytes);
546 }
547 }
548 // Try to reduce the current footprint by releasing the free page
549 // run at the end of the memory region, if any.
550 bool Trim();
551 // Iterates over all the memory slots and apply the given function.
552 void InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg),
553 void* arg)
554 LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchid9a88de2014-04-07 13:52:31 -0700555 // Release empty pages.
556 size_t ReleasePages() LOCKS_EXCLUDED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700557 // Returns the current footprint.
558 size_t Footprint() LOCKS_EXCLUDED(lock_);
559 // Returns the current capacity, maximum footprint.
560 size_t FootprintLimit() LOCKS_EXCLUDED(lock_);
561 // Update the current capacity.
562 void SetFootprintLimit(size_t bytes) LOCKS_EXCLUDED(lock_);
563 // Releases the thread-local runs assigned to the given thread back to the common set of runs.
564 void RevokeThreadLocalRuns(Thread* thread);
565 // Releases the thread-local runs assigned to all the threads back to the common set of runs.
566 void RevokeAllThreadLocalRuns() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchic93c5302014-03-20 16:15:37 -0700567 // Assert the thread local runs of a thread are revoked.
568 void AssertThreadLocalRunsAreRevoked(Thread* thread);
569 // Assert all the thread local runs are revoked.
570 void AssertAllThreadLocalRunsAreRevoked() LOCKS_EXCLUDED(Locks::thread_list_lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700571 // Dumps the page map for debugging.
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800572 std::string DumpPageMap() EXCLUSIVE_LOCKS_REQUIRED(lock_);
Mathieu Chartier73d1e172014-04-11 17:53:48 -0700573 static Run* GetDedicatedFullRun() {
574 return dedicated_full_run_;
575 }
Mathieu Chartiera5b5c552014-06-24 14:48:59 -0700576 bool IsFreePage(size_t idx) const {
577 DCHECK_LT(idx, capacity_ / kPageSize);
578 byte pm_type = page_map_[idx];
579 return pm_type == kPageMapReleased || pm_type == kPageMapEmpty;
580 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700581
582 // Callbacks for InspectAll that will count the number of bytes
583 // allocated and objects allocated, respectively.
584 static void BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
585 static void ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg);
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800586
587 bool DoesReleaseAllPages() const {
588 return page_release_mode_ == kPageReleaseModeAll;
589 }
Hiroshi Yamauchia4adbfd2014-02-04 18:12:17 -0800590
591 // Verify for debugging.
592 void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi654dd482014-07-09 12:54:32 -0700593
594 void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700595};
596
597} // namespace allocator
598} // namespace gc
599} // namespace art
600
601#endif // ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_