Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 1 | /* |
| 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 Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 22 | #include <sys/mman.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <set> |
| 25 | #include <string> |
| 26 | #include <unordered_set> |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 29 | #include "base/allocator.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 30 | #include "base/mutex.h" |
| 31 | #include "base/logging.h" |
| 32 | #include "globals.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 33 | #include "thread.h" |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 34 | #include "utils.h" |
| 35 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 36 | namespace art { |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 37 | |
| 38 | class MemMap; |
| 39 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 40 | namespace gc { |
| 41 | namespace allocator { |
| 42 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 43 | // A runs-of-slots memory allocator. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 44 | class RosAlloc { |
| 45 | private: |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 46 | // Represents a run of free pages. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 47 | class FreePageRun { |
| 48 | public: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 49 | uint8_t magic_num_; // The magic number used for debugging only. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 50 | |
| 51 | bool IsFree() const { |
Mathieu Chartier | a1c1c71 | 2014-06-23 17:53:09 -0700 | [diff] [blame] | 52 | return !kIsDebugBuild || magic_num_ == kMagicNumFree; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 53 | } |
| 54 | size_t ByteSize(RosAlloc* rosalloc) const EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 55 | const uint8_t* fpr_base = reinterpret_cast<const uint8_t*>(this); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 56 | size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base); |
| 57 | size_t byte_size = rosalloc->free_page_run_size_map_[pm_idx]; |
| 58 | DCHECK_GE(byte_size, static_cast<size_t>(0)); |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 59 | DCHECK_ALIGNED(byte_size, kPageSize); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 60 | return byte_size; |
| 61 | } |
| 62 | void SetByteSize(RosAlloc* rosalloc, size_t byte_size) |
| 63 | EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) { |
| 64 | DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0)); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 65 | uint8_t* fpr_base = reinterpret_cast<uint8_t*>(this); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 66 | size_t pm_idx = rosalloc->ToPageMapIndex(fpr_base); |
| 67 | rosalloc->free_page_run_size_map_[pm_idx] = byte_size; |
| 68 | } |
| 69 | void* Begin() { |
| 70 | return reinterpret_cast<void*>(this); |
| 71 | } |
| 72 | void* End(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 73 | uint8_t* fpr_base = reinterpret_cast<uint8_t*>(this); |
| 74 | uint8_t* end = fpr_base + ByteSize(rosalloc); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 75 | return end; |
| 76 | } |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 77 | bool IsLargerThanPageReleaseThreshold(RosAlloc* rosalloc) |
| 78 | EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) { |
| 79 | return ByteSize(rosalloc) >= rosalloc->page_release_size_threshold_; |
| 80 | } |
| 81 | bool IsAtEndOfSpace(RosAlloc* rosalloc) |
| 82 | EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 83 | return reinterpret_cast<uint8_t*>(this) + ByteSize(rosalloc) == rosalloc->base_ + rosalloc->footprint_; |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 84 | } |
| 85 | bool ShouldReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) { |
| 86 | switch (rosalloc->page_release_mode_) { |
| 87 | case kPageReleaseModeNone: |
| 88 | return false; |
| 89 | case kPageReleaseModeEnd: |
| 90 | return IsAtEndOfSpace(rosalloc); |
| 91 | case kPageReleaseModeSize: |
| 92 | return IsLargerThanPageReleaseThreshold(rosalloc); |
| 93 | case kPageReleaseModeSizeAndEnd: |
| 94 | return IsLargerThanPageReleaseThreshold(rosalloc) && IsAtEndOfSpace(rosalloc); |
| 95 | case kPageReleaseModeAll: |
| 96 | return true; |
| 97 | default: |
| 98 | LOG(FATAL) << "Unexpected page release mode "; |
| 99 | return false; |
| 100 | } |
| 101 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 102 | void ReleasePages(RosAlloc* rosalloc) EXCLUSIVE_LOCKS_REQUIRED(rosalloc->lock_) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 103 | uint8_t* start = reinterpret_cast<uint8_t*>(this); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 104 | size_t byte_size = ByteSize(rosalloc); |
| 105 | DCHECK_EQ(byte_size % kPageSize, static_cast<size_t>(0)); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 106 | if (ShouldReleasePages(rosalloc)) { |
| 107 | rosalloc->ReleasePageRange(start, start + byte_size); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | DISALLOW_COPY_AND_ASSIGN(FreePageRun); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | // Represents a run of memory slots of the same size. |
| 116 | // |
| 117 | // A run's memory layout: |
| 118 | // |
| 119 | // +-------------------+ |
| 120 | // | magic_num | |
| 121 | // +-------------------+ |
| 122 | // | size_bracket_idx | |
| 123 | // +-------------------+ |
| 124 | // | is_thread_local | |
| 125 | // +-------------------+ |
| 126 | // | to_be_bulk_freed | |
| 127 | // +-------------------+ |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 128 | // | top_bitmap_idx | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 129 | // +-------------------+ |
| 130 | // | | |
| 131 | // | alloc bit map | |
| 132 | // | | |
| 133 | // +-------------------+ |
| 134 | // | | |
| 135 | // | bulk free bit map | |
| 136 | // | | |
| 137 | // +-------------------+ |
| 138 | // | | |
| 139 | // | thread-local free | |
| 140 | // | bit map | |
| 141 | // | | |
| 142 | // +-------------------+ |
| 143 | // | padding due to | |
| 144 | // | alignment | |
| 145 | // +-------------------+ |
| 146 | // | slot 0 | |
| 147 | // +-------------------+ |
| 148 | // | slot 1 | |
| 149 | // +-------------------+ |
| 150 | // | slot 2 | |
| 151 | // +-------------------+ |
| 152 | // ... |
| 153 | // +-------------------+ |
| 154 | // | last slot | |
| 155 | // +-------------------+ |
| 156 | // |
| 157 | class Run { |
| 158 | public: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 159 | uint8_t magic_num_; // The magic number used for debugging. |
| 160 | uint8_t size_bracket_idx_; // The index of the size bracket of this run. |
| 161 | uint8_t is_thread_local_; // True if this run is used as a thread-local run. |
| 162 | uint8_t to_be_bulk_freed_; // Used within BulkFree() to flag a run that's involved with a bulk free. |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 163 | uint32_t first_search_vec_idx_; // The index of the first bitmap vector which may contain an available slot. |
| 164 | uint32_t alloc_bit_map_[0]; // The bit map that allocates if each slot is in use. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 165 | |
| 166 | // bulk_free_bit_map_[] : The bit map that is used for GC to |
| 167 | // temporarily mark the slots to free without using a lock. After |
| 168 | // all the slots to be freed in a run are marked, all those slots |
| 169 | // get freed in bulk with one locking per run, as opposed to one |
| 170 | // locking per slot to minimize the lock contention. This is used |
| 171 | // within BulkFree(). |
| 172 | |
| 173 | // thread_local_free_bit_map_[] : The bit map that is used for GC |
| 174 | // to temporarily mark the slots to free in a thread-local run |
| 175 | // without using a lock (without synchronizing the thread that |
| 176 | // owns the thread-local run.) When the thread-local run becomes |
| 177 | // full, the thread will check this bit map and update the |
| 178 | // allocation bit map of the run (that is, the slots get freed.) |
| 179 | |
| 180 | // Returns the byte size of the header except for the bit maps. |
| 181 | static size_t fixed_header_size() { |
| 182 | Run temp; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 183 | size_t size = reinterpret_cast<uint8_t*>(&temp.alloc_bit_map_) - reinterpret_cast<uint8_t*>(&temp); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 184 | DCHECK_EQ(size, static_cast<size_t>(8)); |
| 185 | return size; |
| 186 | } |
| 187 | // Returns the base address of the free bit map. |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 188 | uint32_t* BulkFreeBitMap() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 189 | return reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(this) + bulkFreeBitMapOffsets[size_bracket_idx_]); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 190 | } |
| 191 | // Returns the base address of the thread local free bit map. |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 192 | uint32_t* ThreadLocalFreeBitMap() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 193 | return reinterpret_cast<uint32_t*>(reinterpret_cast<uint8_t*>(this) + threadLocalFreeBitMapOffsets[size_bracket_idx_]); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 194 | } |
| 195 | void* End() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 196 | return reinterpret_cast<uint8_t*>(this) + kPageSize * numOfPages[size_bracket_idx_]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 197 | } |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 198 | // Returns the number of bitmap words per run. |
| 199 | size_t NumberOfBitmapVectors() const { |
| 200 | return RoundUp(numOfSlots[size_bracket_idx_], 32) / 32; |
| 201 | } |
| 202 | void SetIsThreadLocal(bool is_thread_local) { |
| 203 | is_thread_local_ = is_thread_local ? 1 : 0; |
| 204 | } |
| 205 | bool IsThreadLocal() const { |
| 206 | return is_thread_local_ != 0; |
| 207 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 208 | // Frees slots in the allocation bit map with regard to the |
| 209 | // thread-local free bit map. Used when a thread-local run becomes |
| 210 | // full. |
| 211 | bool MergeThreadLocalFreeBitMapToAllocBitMap(bool* is_all_free_after_out); |
| 212 | // Frees slots in the allocation bit map with regard to the bulk |
| 213 | // free bit map. Used in a bulk free. |
| 214 | void MergeBulkFreeBitMapIntoAllocBitMap(); |
| 215 | // Unions the slots to be freed in the free bit map into the |
| 216 | // thread-local free bit map. In a bulk free, as a two-step |
| 217 | // process, GC will first record all the slots to free in a run in |
| 218 | // the free bit map where it can write without a lock, and later |
| 219 | // acquire a lock once per run to union the bits of the free bit |
| 220 | // map to the thread-local free bit map. |
| 221 | void UnionBulkFreeBitMapToThreadLocalFreeBitMap(); |
| 222 | // Allocates a slot in a run. |
| 223 | void* AllocSlot(); |
| 224 | // Frees a slot in a run. This is used in a non-bulk free. |
| 225 | void FreeSlot(void* ptr); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 226 | // Marks the slots to free in the bulk free bit map. Returns the bracket size. |
| 227 | size_t MarkBulkFreeBitMap(void* ptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 228 | // Marks the slots to free in the thread-local free bit map. |
| 229 | void MarkThreadLocalFreeBitMap(void* ptr); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 230 | // Last word mask, all of the bits in the last word which aren't valid slots are set to |
| 231 | // optimize allocation path. |
Andreas Gampe | 59e6760 | 2014-04-25 17:15:12 -0700 | [diff] [blame] | 232 | static uint32_t GetBitmapLastVectorMask(size_t num_slots, size_t num_vec); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 233 | // Returns true if all the slots in the run are not in use. |
| 234 | bool IsAllFree(); |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 235 | // Returns the number of free slots. |
| 236 | size_t NumberOfFreeSlots(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 237 | // Returns true if all the slots in the run are in use. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 238 | ALWAYS_INLINE bool IsFull(); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 239 | // Returns true if the bulk free bit map is clean. |
| 240 | bool IsBulkFreeBitmapClean(); |
| 241 | // Returns true if the thread local free bit map is clean. |
| 242 | bool IsThreadLocalFreeBitmapClean(); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 243 | // Set the alloc_bit_map_ bits for slots that are past the end of the run. |
| 244 | void SetAllocBitMapBitsForInvalidSlots(); |
| 245 | // Zero the run's data. |
| 246 | void ZeroData(); |
| 247 | // Zero the run's header. |
| 248 | void ZeroHeader(); |
| 249 | // Fill the alloc bitmap with 1s. |
| 250 | void FillAllocBitMap(); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 251 | // Iterate over all the slots and apply the given function. |
| 252 | void InspectAllSlots(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), void* arg); |
| 253 | // Dump the run metadata for debugging. |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 254 | std::string Dump(); |
| 255 | // Verify for debugging. |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 256 | void Verify(Thread* self, RosAlloc* rosalloc, bool running_on_valgrind) |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 257 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 258 | EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 259 | |
| 260 | private: |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 261 | // The common part of MarkFreeBitMap() and MarkThreadLocalFreeBitMap(). Returns the bracket |
| 262 | // size. |
| 263 | size_t MarkFreeBitMapShared(void* ptr, uint32_t* free_bit_map_base, const char* caller_name); |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 264 | // Turns the bit map into a string for debugging. |
| 265 | static std::string BitMapToStr(uint32_t* bit_map_base, size_t num_vec); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 266 | |
| 267 | // TODO: DISALLOW_COPY_AND_ASSIGN(Run); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | // The magic number for a run. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 271 | static constexpr uint8_t kMagicNum = 42; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 272 | // The magic number for free pages. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 273 | static constexpr uint8_t kMagicNumFree = 43; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 274 | // The number of size brackets. Sync this with the length of Thread::rosalloc_runs_. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 275 | static constexpr size_t kNumOfSizeBrackets = kNumRosAllocThreadLocalSizeBrackets; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 276 | // The number of smaller size brackets that are 16 bytes apart. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 277 | static constexpr size_t kNumOfQuantumSizeBrackets = 32; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 278 | // The sizes (the slot sizes, in bytes) of the size brackets. |
| 279 | static size_t bracketSizes[kNumOfSizeBrackets]; |
| 280 | // The numbers of pages that are used for runs for each size bracket. |
| 281 | static size_t numOfPages[kNumOfSizeBrackets]; |
| 282 | // The numbers of slots of the runs for each size bracket. |
| 283 | static size_t numOfSlots[kNumOfSizeBrackets]; |
| 284 | // The header sizes in bytes of the runs for each size bracket. |
| 285 | static size_t headerSizes[kNumOfSizeBrackets]; |
| 286 | // The byte offsets of the bulk free bit maps of the runs for each size bracket. |
| 287 | static size_t bulkFreeBitMapOffsets[kNumOfSizeBrackets]; |
| 288 | // The byte offsets of the thread-local free bit maps of the runs for each size bracket. |
| 289 | static size_t threadLocalFreeBitMapOffsets[kNumOfSizeBrackets]; |
| 290 | |
| 291 | // Initialize the run specs (the above arrays). |
| 292 | static void Initialize(); |
| 293 | static bool initialized_; |
| 294 | |
| 295 | // Returns the byte size of the bracket size from the index. |
| 296 | static size_t IndexToBracketSize(size_t idx) { |
Ian Rogers | 59c0706 | 2014-10-10 13:03:39 -0700 | [diff] [blame] | 297 | DCHECK_LT(idx, kNumOfSizeBrackets); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 298 | return bracketSizes[idx]; |
| 299 | } |
| 300 | // Returns the index of the size bracket from the bracket size. |
| 301 | static size_t BracketSizeToIndex(size_t size) { |
| 302 | DCHECK(16 <= size && ((size < 1 * KB && size % 16 == 0) || size == 1 * KB || size == 2 * KB)); |
| 303 | size_t idx; |
| 304 | if (UNLIKELY(size == 1 * KB)) { |
| 305 | idx = kNumOfSizeBrackets - 2; |
| 306 | } else if (UNLIKELY(size == 2 * KB)) { |
| 307 | idx = kNumOfSizeBrackets - 1; |
| 308 | } else { |
| 309 | DCHECK(size < 1 * KB); |
| 310 | DCHECK_EQ(size % 16, static_cast<size_t>(0)); |
| 311 | idx = size / 16 - 1; |
| 312 | } |
| 313 | DCHECK(bracketSizes[idx] == size); |
| 314 | return idx; |
| 315 | } |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 316 | // Returns true if the given allocation size is for a thread local allocation. |
| 317 | static bool IsSizeForThreadLocal(size_t size) { |
| 318 | DCHECK_GT(kNumThreadLocalSizeBrackets, 0U); |
| 319 | size_t max_thread_local_bracket_idx = kNumThreadLocalSizeBrackets - 1; |
| 320 | bool is_size_for_thread_local = size <= bracketSizes[max_thread_local_bracket_idx]; |
| 321 | DCHECK(size > kLargeSizeThreshold || |
| 322 | (is_size_for_thread_local == (SizeToIndex(size) < kNumThreadLocalSizeBrackets))); |
| 323 | return is_size_for_thread_local; |
| 324 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 325 | // Rounds up the size up the nearest bracket size. |
| 326 | static size_t RoundToBracketSize(size_t size) { |
| 327 | DCHECK(size <= kLargeSizeThreshold); |
| 328 | if (LIKELY(size <= 512)) { |
| 329 | return RoundUp(size, 16); |
| 330 | } else if (512 < size && size <= 1 * KB) { |
| 331 | return 1 * KB; |
| 332 | } else { |
| 333 | DCHECK(1 * KB < size && size <= 2 * KB); |
| 334 | return 2 * KB; |
| 335 | } |
| 336 | } |
| 337 | // Returns the size bracket index from the byte size with rounding. |
| 338 | static size_t SizeToIndex(size_t size) { |
| 339 | DCHECK(size <= kLargeSizeThreshold); |
| 340 | if (LIKELY(size <= 512)) { |
| 341 | return RoundUp(size, 16) / 16 - 1; |
| 342 | } else if (512 < size && size <= 1 * KB) { |
| 343 | return kNumOfSizeBrackets - 2; |
| 344 | } else { |
| 345 | DCHECK(1 * KB < size && size <= 2 * KB); |
| 346 | return kNumOfSizeBrackets - 1; |
| 347 | } |
| 348 | } |
| 349 | // A combination of SizeToIndex() and RoundToBracketSize(). |
| 350 | static size_t SizeToIndexAndBracketSize(size_t size, size_t* bracket_size_out) { |
| 351 | DCHECK(size <= kLargeSizeThreshold); |
| 352 | if (LIKELY(size <= 512)) { |
| 353 | size_t bracket_size = RoundUp(size, 16); |
| 354 | *bracket_size_out = bracket_size; |
| 355 | size_t idx = bracket_size / 16 - 1; |
| 356 | DCHECK_EQ(bracket_size, IndexToBracketSize(idx)); |
| 357 | return idx; |
| 358 | } else if (512 < size && size <= 1 * KB) { |
| 359 | size_t bracket_size = 1024; |
| 360 | *bracket_size_out = bracket_size; |
| 361 | size_t idx = kNumOfSizeBrackets - 2; |
| 362 | DCHECK_EQ(bracket_size, IndexToBracketSize(idx)); |
| 363 | return idx; |
| 364 | } else { |
| 365 | DCHECK(1 * KB < size && size <= 2 * KB); |
| 366 | size_t bracket_size = 2048; |
| 367 | *bracket_size_out = bracket_size; |
| 368 | size_t idx = kNumOfSizeBrackets - 1; |
| 369 | DCHECK_EQ(bracket_size, IndexToBracketSize(idx)); |
| 370 | return idx; |
| 371 | } |
| 372 | } |
| 373 | // Returns the page map index from an address. Requires that the |
| 374 | // address is page size aligned. |
| 375 | size_t ToPageMapIndex(const void* addr) const { |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 376 | DCHECK_LE(base_, addr); |
| 377 | DCHECK_LT(addr, base_ + capacity_); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 378 | size_t byte_offset = reinterpret_cast<const uint8_t*>(addr) - base_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 379 | DCHECK_EQ(byte_offset % static_cast<size_t>(kPageSize), static_cast<size_t>(0)); |
| 380 | return byte_offset / kPageSize; |
| 381 | } |
| 382 | // Returns the page map index from an address with rounding. |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 383 | size_t RoundDownToPageMapIndex(const void* addr) const { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 384 | DCHECK(base_ <= addr && addr < reinterpret_cast<uint8_t*>(base_) + capacity_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 385 | return (reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(base_)) / kPageSize; |
| 386 | } |
| 387 | |
| 388 | // A memory allocation request larger than this size is treated as a large object and allocated |
| 389 | // at a page-granularity. |
| 390 | static const size_t kLargeSizeThreshold = 2048; |
| 391 | |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 392 | // If true, check that the returned memory is actually zero. |
| 393 | static constexpr bool kCheckZeroMemory = kIsDebugBuild; |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 394 | // Valgrind protects memory, so do not check memory when running under valgrind. In a normal |
| 395 | // build with kCheckZeroMemory the whole test should be optimized away. |
| 396 | // TODO: Unprotect before checks. |
| 397 | ALWAYS_INLINE bool ShouldCheckZeroMemory(); |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 398 | |
| 399 | // If true, log verbose details of operations. |
| 400 | static constexpr bool kTraceRosAlloc = false; |
| 401 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 402 | struct hash_run { |
| 403 | size_t operator()(const RosAlloc::Run* r) const { |
| 404 | return reinterpret_cast<size_t>(r); |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | struct eq_run { |
| 409 | bool operator()(const RosAlloc::Run* r1, const RosAlloc::Run* r2) const { |
| 410 | return r1 == r2; |
| 411 | } |
| 412 | }; |
| 413 | |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 414 | public: |
| 415 | // Different page release modes. |
| 416 | enum PageReleaseMode { |
| 417 | kPageReleaseModeNone, // Release no empty pages. |
| 418 | kPageReleaseModeEnd, // Release empty pages at the end of the space. |
| 419 | kPageReleaseModeSize, // Release empty pages that are larger than the threshold. |
| 420 | kPageReleaseModeSizeAndEnd, // Release empty pages that are larger than the threshold or |
| 421 | // at the end of the space. |
| 422 | kPageReleaseModeAll, // Release all empty pages. |
| 423 | }; |
| 424 | |
| 425 | // The default value for page_release_size_threshold_. |
| 426 | static constexpr size_t kDefaultPageReleaseSizeThreshold = 4 * MB; |
| 427 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 428 | // We use thread-local runs for the size Brackets whose indexes |
| 429 | // are less than this index. We use shared (current) runs for the rest. |
Hiroshi Yamauchi | 3f3c6c0 | 2014-11-20 14:16:06 -0800 | [diff] [blame] | 430 | static const size_t kNumThreadLocalSizeBrackets = 8; |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 431 | |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 432 | private: |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 433 | // The base address of the memory region that's managed by this allocator. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 434 | uint8_t* base_; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 435 | |
| 436 | // The footprint in bytes of the currently allocated portion of the |
| 437 | // memory region. |
| 438 | size_t footprint_; |
| 439 | |
| 440 | // The maximum footprint. The address, base_ + capacity_, indicates |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 441 | // the end of the memory region that's currently managed by this allocator. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 442 | size_t capacity_; |
| 443 | |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 444 | // The maximum capacity. The address, base_ + max_capacity_, indicates |
| 445 | // the end of the memory region that's ever managed by this allocator. |
| 446 | size_t max_capacity_; |
| 447 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 448 | // The run sets that hold the runs whose slots are not all |
| 449 | // full. non_full_runs_[i] is guarded by size_bracket_locks_[i]. |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 450 | AllocationTrackingSet<Run*, kAllocatorTagRosAlloc> non_full_runs_[kNumOfSizeBrackets]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 451 | // The run sets that hold the runs whose slots are all full. This is |
| 452 | // debug only. full_runs_[i] is guarded by size_bracket_locks_[i]. |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 453 | std::unordered_set<Run*, hash_run, eq_run, TrackingAllocator<Run*, kAllocatorTagRosAlloc>> |
| 454 | full_runs_[kNumOfSizeBrackets]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 455 | // The set of free pages. |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 456 | AllocationTrackingSet<FreePageRun*, kAllocatorTagRosAlloc> free_page_runs_ GUARDED_BY(lock_); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 457 | // The dedicated full run, it is always full and shared by all threads when revoking happens. |
| 458 | // This is an optimization since enables us to avoid a null check for revoked runs. |
| 459 | static Run* dedicated_full_run_; |
| 460 | // Using size_t to ensure that it is at least word aligned. |
| 461 | static size_t dedicated_full_run_storage_[]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 462 | // The current runs where the allocations are first attempted for |
| 463 | // the size brackes that do not use thread-local |
| 464 | // runs. current_runs_[i] is guarded by size_bracket_locks_[i]. |
| 465 | Run* current_runs_[kNumOfSizeBrackets]; |
| 466 | // The mutexes, one per size bracket. |
| 467 | Mutex* size_bracket_locks_[kNumOfSizeBrackets]; |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 468 | // Bracket lock names (since locks only have char* names). |
Zuo Wang | f37a88b | 2014-07-10 04:26:41 -0700 | [diff] [blame] | 469 | std::string size_bracket_lock_names_[kNumOfSizeBrackets]; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 470 | // The types of page map entries. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 471 | enum PageMapKind { |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 472 | kPageMapReleased = 0, // Zero and released back to the OS. |
| 473 | kPageMapEmpty, // Zero but probably dirty. |
| 474 | kPageMapRun, // The beginning of a run. |
| 475 | kPageMapRunPart, // The non-beginning part of a run. |
| 476 | kPageMapLargeObject, // The beginning of a large object. |
| 477 | kPageMapLargeObjectPart, // The non-beginning part of a large object. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 478 | }; |
| 479 | // The table that indicates what pages are currently used for. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 480 | volatile uint8_t* page_map_; // No GUARDED_BY(lock_) for kReadPageMapEntryWithoutLockInBulkFree. |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 481 | size_t page_map_size_; |
| 482 | size_t max_page_map_size_; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 483 | std::unique_ptr<MemMap> page_map_mem_map_; |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 484 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 485 | // The table that indicates the size of free page runs. These sizes |
| 486 | // are stored here to avoid storing in the free page header and |
| 487 | // release backing pages. |
Mathieu Chartier | 58553c7 | 2014-09-16 16:25:55 -0700 | [diff] [blame] | 488 | std::vector<size_t, TrackingAllocator<size_t, kAllocatorTagRosAlloc>> free_page_run_size_map_ |
| 489 | GUARDED_BY(lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 490 | // The global lock. Used to guard the page map, the free page set, |
| 491 | // and the footprint. |
| 492 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 493 | // The reader-writer lock to allow one bulk free at a time while |
Hiroshi Yamauchi | 70f6004 | 2014-02-03 12:31:29 -0800 | [diff] [blame] | 494 | // allowing multiple individual frees at the same time. Also, this |
| 495 | // is used to avoid race conditions between BulkFree() and |
| 496 | // RevokeThreadLocalRuns() on the bulk free bitmaps. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 497 | ReaderWriterMutex bulk_free_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 498 | |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 499 | // The page release mode. |
| 500 | const PageReleaseMode page_release_mode_; |
| 501 | // Under kPageReleaseModeSize(AndEnd), if the free page run size is |
| 502 | // greater than or equal to this value, release pages. |
| 503 | const size_t page_release_size_threshold_; |
| 504 | |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 505 | // Whether this allocator is running under Valgrind. |
| 506 | bool running_on_valgrind_; |
| 507 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 508 | // The base address of the memory region that's managed by this allocator. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 509 | uint8_t* Begin() { return base_; } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 510 | // The end address of the memory region that's managed by this allocator. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 511 | uint8_t* End() { return base_ + capacity_; } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 512 | |
| 513 | // Page-granularity alloc/free |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 514 | void* AllocPages(Thread* self, size_t num_pages, uint8_t page_map_type) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 515 | EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 516 | // Returns how many bytes were freed. |
| 517 | size_t FreePages(Thread* self, void* ptr, bool already_zero) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 518 | |
| 519 | // Allocate/free a run slot. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 520 | void* AllocFromRun(Thread* self, size_t size, size_t* bytes_allocated, size_t* usable_size, |
| 521 | size_t* bytes_tl_bulk_allocated) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 522 | LOCKS_EXCLUDED(lock_); |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 523 | // Allocate/free a run slot without acquiring locks. |
| 524 | // TODO: EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 525 | void* AllocFromRunThreadUnsafe(Thread* self, size_t size, size_t* bytes_allocated, |
| 526 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 527 | LOCKS_EXCLUDED(lock_); |
| 528 | void* AllocFromCurrentRunUnlocked(Thread* self, size_t idx); |
| 529 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 530 | // Returns the bracket size. |
| 531 | size_t FreeFromRun(Thread* self, void* ptr, Run* run) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 532 | LOCKS_EXCLUDED(lock_); |
| 533 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 534 | // Used to allocate a new thread local run for a size bracket. |
| 535 | Run* AllocRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_); |
| 536 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 537 | // Used to acquire a new/reused run for a size bracket. Used when a |
| 538 | // thread-local or current run gets full. |
| 539 | Run* RefillRun(Thread* self, size_t idx) LOCKS_EXCLUDED(lock_); |
| 540 | |
| 541 | // The internal of non-bulk Free(). |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 542 | size_t FreeInternal(Thread* self, void* ptr) LOCKS_EXCLUDED(lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 543 | |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 544 | // Allocates large objects. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 545 | void* AllocLargeObject(Thread* self, size_t size, size_t* bytes_allocated, |
| 546 | size_t* usable_size, size_t* bytes_tl_bulk_allocated) |
| 547 | LOCKS_EXCLUDED(lock_); |
Hiroshi Yamauchi | 3c2856e | 2013-11-22 13:42:53 -0800 | [diff] [blame] | 548 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 549 | // Revoke a run by adding it to non_full_runs_ or freeing the pages. |
| 550 | void RevokeRun(Thread* self, size_t idx, Run* run); |
| 551 | |
| 552 | // Revoke the current runs which share an index with the thread local runs. |
| 553 | void RevokeThreadUnsafeCurrentRuns(); |
| 554 | |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 555 | // Release a range of pages. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 556 | size_t ReleasePageRange(uint8_t* start, uint8_t* end) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 557 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 558 | // Dumps the page map for debugging. |
| 559 | std::string DumpPageMap() EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 560 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 561 | public: |
Hiroshi Yamauchi | 26d69ff | 2014-02-27 11:27:10 -0800 | [diff] [blame] | 562 | RosAlloc(void* base, size_t capacity, size_t max_capacity, |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 563 | PageReleaseMode page_release_mode, |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 564 | bool running_on_valgrind, |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 565 | size_t page_release_size_threshold = kDefaultPageReleaseSizeThreshold); |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 566 | ~RosAlloc(); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 567 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 568 | // If kThreadUnsafe is true then the allocator may avoid acquiring some locks as an optimization. |
| 569 | // If used, this may cause race conditions if multiple threads are allocating at the same time. |
| 570 | template<bool kThreadSafe = true> |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 571 | void* Alloc(Thread* self, size_t size, size_t* bytes_allocated, size_t* usable_size, |
| 572 | size_t* bytes_tl_bulk_allocated) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 573 | LOCKS_EXCLUDED(lock_); |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 574 | size_t Free(Thread* self, void* ptr) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 575 | LOCKS_EXCLUDED(bulk_free_lock_); |
Mathieu Chartier | 8585bad | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 576 | size_t BulkFree(Thread* self, void** ptrs, size_t num_ptrs) |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 577 | LOCKS_EXCLUDED(bulk_free_lock_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 578 | |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 579 | // Returns true if the given allocation request can be allocated in |
| 580 | // an existing thread local run without allocating a new run. |
| 581 | ALWAYS_INLINE bool CanAllocFromThreadLocalRun(Thread* self, size_t size); |
| 582 | // Allocate the given allocation request in an existing thread local |
| 583 | // run without allocating a new run. |
| 584 | ALWAYS_INLINE void* AllocFromThreadLocalRun(Thread* self, size_t size, size_t* bytes_allocated); |
| 585 | |
| 586 | // Returns the maximum bytes that could be allocated for the given |
| 587 | // size in bulk, that is the maximum value for the |
| 588 | // bytes_allocated_bulk out param returned by RosAlloc::Alloc(). |
| 589 | ALWAYS_INLINE size_t MaxBytesBulkAllocatedFor(size_t size); |
| 590 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 591 | // Returns the size of the allocated slot for a given allocated memory chunk. |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 592 | size_t UsableSize(const void* ptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 593 | // Returns the size of the allocated slot for a given size. |
| 594 | size_t UsableSize(size_t bytes) { |
| 595 | if (UNLIKELY(bytes > kLargeSizeThreshold)) { |
| 596 | return RoundUp(bytes, kPageSize); |
| 597 | } else { |
| 598 | return RoundToBracketSize(bytes); |
| 599 | } |
| 600 | } |
| 601 | // Try to reduce the current footprint by releasing the free page |
| 602 | // run at the end of the memory region, if any. |
| 603 | bool Trim(); |
| 604 | // Iterates over all the memory slots and apply the given function. |
| 605 | void InspectAll(void (*handler)(void* start, void* end, size_t used_bytes, void* callback_arg), |
| 606 | void* arg) |
| 607 | LOCKS_EXCLUDED(lock_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 608 | |
Hiroshi Yamauchi | d9a88de | 2014-04-07 13:52:31 -0700 | [diff] [blame] | 609 | // Release empty pages. |
| 610 | size_t ReleasePages() LOCKS_EXCLUDED(lock_); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 611 | // Returns the current footprint. |
| 612 | size_t Footprint() LOCKS_EXCLUDED(lock_); |
| 613 | // Returns the current capacity, maximum footprint. |
| 614 | size_t FootprintLimit() LOCKS_EXCLUDED(lock_); |
| 615 | // Update the current capacity. |
| 616 | void SetFootprintLimit(size_t bytes) LOCKS_EXCLUDED(lock_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 617 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 618 | // Releases the thread-local runs assigned to the given thread back to the common set of runs. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 619 | // Returns the total bytes of free slots in the revoked thread local runs. This is to be |
| 620 | // subtracted from Heap::num_bytes_allocated_ to cancel out the ahead-of-time counting. |
| 621 | size_t RevokeThreadLocalRuns(Thread* thread); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 622 | // Releases the thread-local runs assigned to all the threads back to the common set of runs. |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 623 | // Returns the total bytes of free slots in the revoked thread local runs. This is to be |
| 624 | // subtracted from Heap::num_bytes_allocated_ to cancel out the ahead-of-time counting. |
| 625 | size_t RevokeAllThreadLocalRuns() LOCKS_EXCLUDED(Locks::thread_list_lock_); |
Hiroshi Yamauchi | c93c530 | 2014-03-20 16:15:37 -0700 | [diff] [blame] | 626 | // Assert the thread local runs of a thread are revoked. |
| 627 | void AssertThreadLocalRunsAreRevoked(Thread* thread); |
| 628 | // Assert all the thread local runs are revoked. |
| 629 | void AssertAllThreadLocalRunsAreRevoked() LOCKS_EXCLUDED(Locks::thread_list_lock_); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 630 | |
Mathieu Chartier | 73d1e17 | 2014-04-11 17:53:48 -0700 | [diff] [blame] | 631 | static Run* GetDedicatedFullRun() { |
| 632 | return dedicated_full_run_; |
| 633 | } |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 634 | bool IsFreePage(size_t idx) const { |
| 635 | DCHECK_LT(idx, capacity_ / kPageSize); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 636 | uint8_t pm_type = page_map_[idx]; |
Mathieu Chartier | a5b5c55 | 2014-06-24 14:48:59 -0700 | [diff] [blame] | 637 | return pm_type == kPageMapReleased || pm_type == kPageMapEmpty; |
| 638 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 639 | |
| 640 | // Callbacks for InspectAll that will count the number of bytes |
| 641 | // allocated and objects allocated, respectively. |
| 642 | static void BytesAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg); |
| 643 | static void ObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, void* arg); |
Hiroshi Yamauchi | 573f7d2 | 2013-12-17 11:54:23 -0800 | [diff] [blame] | 644 | |
| 645 | bool DoesReleaseAllPages() const { |
| 646 | return page_release_mode_ == kPageReleaseModeAll; |
| 647 | } |
Hiroshi Yamauchi | a4adbfd | 2014-02-04 18:12:17 -0800 | [diff] [blame] | 648 | |
| 649 | // Verify for debugging. |
| 650 | void Verify() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 654dd48 | 2014-07-09 12:54:32 -0700 | [diff] [blame] | 651 | |
| 652 | void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes); |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 653 | |
| 654 | private: |
| 655 | friend std::ostream& operator<<(std::ostream& os, const RosAlloc::PageMapKind& rhs); |
| 656 | |
| 657 | DISALLOW_COPY_AND_ASSIGN(RosAlloc); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 658 | }; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 659 | std::ostream& operator<<(std::ostream& os, const RosAlloc::PageMapKind& rhs); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 660 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 661 | // Callback from rosalloc when it needs to increase the footprint. Must be implemented somewhere |
| 662 | // else (currently rosalloc_space.cc). |
| 663 | void* ArtRosAllocMoreCore(allocator::RosAlloc* rosalloc, intptr_t increment); |
| 664 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 665 | } // namespace allocator |
| 666 | } // namespace gc |
| 667 | } // namespace art |
| 668 | |
| 669 | #endif // ART_RUNTIME_GC_ALLOCATOR_ROSALLOC_H_ |