Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 1 | //===-- local_cache.h -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef SCUDO_LOCAL_CACHE_H_ |
| 10 | #define SCUDO_LOCAL_CACHE_H_ |
| 11 | |
| 12 | #include "internal_defs.h" |
| 13 | #include "report.h" |
| 14 | #include "stats.h" |
| 15 | |
| 16 | namespace scudo { |
| 17 | |
| 18 | template <class SizeClassAllocator> struct SizeClassAllocatorLocalCache { |
| 19 | typedef typename SizeClassAllocator::SizeClassMap SizeClassMap; |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 20 | typedef typename SizeClassAllocator::CompactPtrT CompactPtrT; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 21 | |
| 22 | struct TransferBatch { |
| 23 | static const u32 MaxNumCached = SizeClassMap::MaxNumCachedHint; |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 24 | void setFromArray(CompactPtrT *Array, u32 N) { |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 25 | DCHECK_LE(N, MaxNumCached); |
| 26 | Count = N; |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 27 | memcpy(Batch, Array, sizeof(Batch[0]) * Count); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 28 | } |
| 29 | void clear() { Count = 0; } |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 30 | void add(CompactPtrT P) { |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 31 | DCHECK_LT(Count, MaxNumCached); |
| 32 | Batch[Count++] = P; |
| 33 | } |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 34 | void copyToArray(CompactPtrT *Array) const { |
| 35 | memcpy(Array, Batch, sizeof(Batch[0]) * Count); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 36 | } |
| 37 | u32 getCount() const { return Count; } |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 38 | CompactPtrT get(u32 I) const { |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 39 | DCHECK_LE(I, Count); |
| 40 | return Batch[I]; |
| 41 | } |
| 42 | static u32 getMaxCached(uptr Size) { |
| 43 | return Min(MaxNumCached, SizeClassMap::getMaxCachedHint(Size)); |
| 44 | } |
| 45 | TransferBatch *Next; |
| 46 | |
| 47 | private: |
| 48 | u32 Count; |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 49 | CompactPtrT Batch[MaxNumCached]; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Kostya Kortchinsky | 4a435d2 | 2021-05-25 15:00:58 -0700 | [diff] [blame] | 52 | void init(GlobalStats *S, SizeClassAllocator *A) { |
| 53 | DCHECK(isEmpty()); |
| 54 | Stats.init(); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 55 | if (LIKELY(S)) |
| 56 | S->link(&Stats); |
| 57 | Allocator = A; |
| 58 | } |
| 59 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 60 | void destroy(GlobalStats *S) { |
| 61 | drain(); |
| 62 | if (LIKELY(S)) |
| 63 | S->unlink(&Stats); |
| 64 | } |
| 65 | |
| 66 | void *allocate(uptr ClassId) { |
| 67 | DCHECK_LT(ClassId, NumClasses); |
| 68 | PerClass *C = &PerClassArray[ClassId]; |
| 69 | if (C->Count == 0) { |
| 70 | if (UNLIKELY(!refill(C, ClassId))) |
| 71 | return nullptr; |
| 72 | DCHECK_GT(C->Count, 0); |
| 73 | } |
| 74 | // We read ClassSize first before accessing Chunks because it's adjacent to |
| 75 | // Count, while Chunks might be further off (depending on Count). That keeps |
| 76 | // the memory accesses in close quarters. |
| 77 | const uptr ClassSize = C->ClassSize; |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 78 | CompactPtrT CompactP = C->Chunks[--C->Count]; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 79 | Stats.add(StatAllocated, ClassSize); |
| 80 | Stats.sub(StatFree, ClassSize); |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 81 | return Allocator->decompactPtr(ClassId, CompactP); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void deallocate(uptr ClassId, void *P) { |
| 85 | CHECK_LT(ClassId, NumClasses); |
| 86 | PerClass *C = &PerClassArray[ClassId]; |
| 87 | // We still have to initialize the cache in the event that the first heap |
| 88 | // operation in a thread is a deallocation. |
| 89 | initCacheMaybe(C); |
| 90 | if (C->Count == C->MaxCount) |
| 91 | drain(C, ClassId); |
| 92 | // See comment in allocate() about memory accesses. |
| 93 | const uptr ClassSize = C->ClassSize; |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 94 | C->Chunks[C->Count++] = |
| 95 | Allocator->compactPtr(ClassId, reinterpret_cast<uptr>(P)); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 96 | Stats.sub(StatAllocated, ClassSize); |
| 97 | Stats.add(StatFree, ClassSize); |
| 98 | } |
| 99 | |
Vitaly Buka | ff35093 | 2021-04-01 12:44:31 -0700 | [diff] [blame] | 100 | bool isEmpty() const { |
| 101 | for (uptr I = 0; I < NumClasses; ++I) |
| 102 | if (PerClassArray[I].Count) |
| 103 | return false; |
| 104 | return true; |
| 105 | } |
| 106 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 107 | void drain() { |
Kostya Kortchinsky | 1b13235 | 2021-04-07 12:39:24 -0700 | [diff] [blame] | 108 | // Drain BatchClassId last as createBatch can refill it. |
| 109 | for (uptr I = 0; I < NumClasses; ++I) { |
| 110 | if (I == BatchClassId) |
| 111 | continue; |
| 112 | while (PerClassArray[I].Count > 0) |
| 113 | drain(&PerClassArray[I], I); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 114 | } |
Kostya Kortchinsky | 1b13235 | 2021-04-07 12:39:24 -0700 | [diff] [blame] | 115 | while (PerClassArray[BatchClassId].Count > 0) |
| 116 | drain(&PerClassArray[BatchClassId], BatchClassId); |
Vitaly Buka | ff35093 | 2021-04-01 12:44:31 -0700 | [diff] [blame] | 117 | DCHECK(isEmpty()); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TransferBatch *createBatch(uptr ClassId, void *B) { |
Kostya Kortchinsky | 1b13235 | 2021-04-07 12:39:24 -0700 | [diff] [blame] | 121 | if (ClassId != BatchClassId) |
| 122 | B = allocate(BatchClassId); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 123 | return reinterpret_cast<TransferBatch *>(B); |
| 124 | } |
| 125 | |
| 126 | LocalStats &getStats() { return Stats; } |
| 127 | |
| 128 | private: |
| 129 | static const uptr NumClasses = SizeClassMap::NumClasses; |
Kostya Kortchinsky | 1b13235 | 2021-04-07 12:39:24 -0700 | [diff] [blame] | 130 | static const uptr BatchClassId = SizeClassMap::BatchClassId; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 131 | struct PerClass { |
| 132 | u32 Count; |
| 133 | u32 MaxCount; |
Mitch Phillips | bebf30b | 2021-05-03 10:42:19 -0700 | [diff] [blame] | 134 | // Note: ClassSize is zero for the transfer batch. |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 135 | uptr ClassSize; |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 136 | CompactPtrT Chunks[2 * TransferBatch::MaxNumCached]; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 137 | }; |
Vitaly Buka | 5d3d727 | 2021-04-29 01:19:51 -0700 | [diff] [blame] | 138 | PerClass PerClassArray[NumClasses] = {}; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 139 | LocalStats Stats; |
Vitaly Buka | 5d3d727 | 2021-04-29 01:19:51 -0700 | [diff] [blame] | 140 | SizeClassAllocator *Allocator = nullptr; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 141 | |
| 142 | ALWAYS_INLINE void initCacheMaybe(PerClass *C) { |
| 143 | if (LIKELY(C->MaxCount)) |
| 144 | return; |
| 145 | initCache(); |
| 146 | DCHECK_NE(C->MaxCount, 0U); |
| 147 | } |
| 148 | |
| 149 | NOINLINE void initCache() { |
| 150 | for (uptr I = 0; I < NumClasses; I++) { |
| 151 | PerClass *P = &PerClassArray[I]; |
| 152 | const uptr Size = SizeClassAllocator::getSizeByClassId(I); |
| 153 | P->MaxCount = 2 * TransferBatch::getMaxCached(Size); |
Mitch Phillips | bebf30b | 2021-05-03 10:42:19 -0700 | [diff] [blame] | 154 | if (I != BatchClassId) { |
| 155 | P->ClassSize = Size; |
| 156 | } else { |
| 157 | // ClassSize in this struct is only used for malloc/free stats, which |
| 158 | // should only track user allocations, not internal movements. |
| 159 | P->ClassSize = 0; |
| 160 | } |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void destroyBatch(uptr ClassId, void *B) { |
Kostya Kortchinsky | 1b13235 | 2021-04-07 12:39:24 -0700 | [diff] [blame] | 165 | if (ClassId != BatchClassId) |
| 166 | deallocate(BatchClassId, B); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | NOINLINE bool refill(PerClass *C, uptr ClassId) { |
| 170 | initCacheMaybe(C); |
| 171 | TransferBatch *B = Allocator->popBatch(this, ClassId); |
| 172 | if (UNLIKELY(!B)) |
| 173 | return false; |
| 174 | DCHECK_GT(B->getCount(), 0); |
| 175 | C->Count = B->getCount(); |
| 176 | B->copyToArray(C->Chunks); |
Kostya Kortchinsky | 394cc82 | 2020-06-17 10:31:53 -0700 | [diff] [blame] | 177 | B->clear(); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 178 | destroyBatch(ClassId, B); |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | NOINLINE void drain(PerClass *C, uptr ClassId) { |
| 183 | const u32 Count = Min(C->MaxCount / 2, C->Count); |
Kostya Kortchinsky | c936954 | 2021-02-10 10:17:18 -0800 | [diff] [blame] | 184 | TransferBatch *B = |
| 185 | createBatch(ClassId, Allocator->decompactPtr(ClassId, C->Chunks[0])); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 186 | if (UNLIKELY(!B)) |
Kostya Kortchinsky | 1b13235 | 2021-04-07 12:39:24 -0700 | [diff] [blame] | 187 | reportOutOfMemory(SizeClassAllocator::getSizeByClassId(BatchClassId)); |
Dynamic Tools Team | ebcf82d | 2020-02-25 14:23:34 -0800 | [diff] [blame] | 188 | B->setFromArray(&C->Chunks[0], Count); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 189 | C->Count -= Count; |
Dynamic Tools Team | ebcf82d | 2020-02-25 14:23:34 -0800 | [diff] [blame] | 190 | for (uptr I = 0; I < C->Count; I++) |
| 191 | C->Chunks[I] = C->Chunks[I + Count]; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 192 | Allocator->pushBatch(ClassId, B); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | } // namespace scudo |
| 197 | |
| 198 | #endif // SCUDO_LOCAL_CACHE_H_ |