Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 1 | //===-- sanitizer_quarantine.h ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Memory quarantine for AddressSanitizer and potentially other tools. |
| 11 | // Quarantine caches some specified amount of memory in per-thread caches, |
| 12 | // then evicts to global FIFO queue. When the queue reaches specified threshold, |
| 13 | // oldest memory is recycled. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef SANITIZER_QUARANTINE_H |
| 18 | #define SANITIZER_QUARANTINE_H |
| 19 | |
| 20 | #include "sanitizer_internal_defs.h" |
| 21 | #include "sanitizer_mutex.h" |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 22 | #include "sanitizer_list.h" |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 23 | |
| 24 | namespace __sanitizer { |
| 25 | |
| 26 | template<typename Node> class QuarantineCache; |
| 27 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 28 | struct QuarantineBatch { |
Kostya Serebryany | a650de8 | 2013-10-21 08:36:10 +0000 | [diff] [blame] | 29 | static const uptr kSize = 1021; |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 30 | QuarantineBatch *next; |
| 31 | uptr size; |
| 32 | uptr count; |
| 33 | void *batch[kSize]; |
| 34 | }; |
| 35 | |
Kostya Serebryany | a650de8 | 2013-10-21 08:36:10 +0000 | [diff] [blame] | 36 | COMPILER_CHECK(sizeof(QuarantineBatch) <= (1 << 13)); // 8Kb. |
| 37 | |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 38 | // The callback interface is: |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 39 | // void Callback::Recycle(Node *ptr); |
| 40 | // void *cb.Allocate(uptr size); |
| 41 | // void cb.Deallocate(void *ptr); |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 42 | template<typename Callback, typename Node> |
| 43 | class Quarantine { |
| 44 | public: |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 45 | typedef QuarantineCache<Callback> Cache; |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 46 | |
| 47 | explicit Quarantine(LinkerInitialized) |
| 48 | : cache_(LINKER_INITIALIZED) { |
| 49 | } |
| 50 | |
| 51 | void Init(uptr size, uptr cache_size) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 52 | atomic_store(&max_size_, size, memory_order_release); |
| 53 | atomic_store(&min_size_, size / 10 * 9, |
| 54 | memory_order_release); // 90% of max size. |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 55 | max_cache_size_ = cache_size; |
| 56 | } |
| 57 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 58 | uptr GetSize() const { return atomic_load(&max_size_, memory_order_acquire); } |
| 59 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 60 | void Put(Cache *c, Callback cb, Node *ptr, uptr size) { |
| 61 | c->Enqueue(cb, ptr, size); |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 62 | if (c->Size() > max_cache_size_) |
| 63 | Drain(c, cb); |
| 64 | } |
| 65 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 66 | void NOINLINE Drain(Cache *c, Callback cb) { |
| 67 | { |
| 68 | SpinMutexLock l(&cache_mutex_); |
| 69 | cache_.Transfer(c); |
| 70 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 71 | if (cache_.Size() > GetSize() && recycle_mutex_.TryLock()) |
Dmitry Vyukov | f794195 | 2013-01-11 11:39:59 +0000 | [diff] [blame] | 72 | Recycle(cb); |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private: |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 76 | // Read-only data. |
| 77 | char pad0_[kCacheLineSize]; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 78 | atomic_uintptr_t max_size_; |
| 79 | atomic_uintptr_t min_size_; |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 80 | uptr max_cache_size_; |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 81 | char pad1_[kCacheLineSize]; |
| 82 | SpinMutex cache_mutex_; |
| 83 | SpinMutex recycle_mutex_; |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 84 | Cache cache_; |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 85 | char pad2_[kCacheLineSize]; |
Dmitry Vyukov | f794195 | 2013-01-11 11:39:59 +0000 | [diff] [blame] | 86 | |
| 87 | void NOINLINE Recycle(Callback cb) { |
| 88 | Cache tmp; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 89 | uptr min_size = atomic_load(&min_size_, memory_order_acquire); |
Dmitry Vyukov | f794195 | 2013-01-11 11:39:59 +0000 | [diff] [blame] | 90 | { |
| 91 | SpinMutexLock l(&cache_mutex_); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 92 | while (cache_.Size() > min_size) { |
Dmitry Vyukov | f794195 | 2013-01-11 11:39:59 +0000 | [diff] [blame] | 93 | QuarantineBatch *b = cache_.DequeueBatch(); |
| 94 | tmp.EnqueueBatch(b); |
| 95 | } |
| 96 | } |
| 97 | recycle_mutex_.Unlock(); |
Dmitry Vyukov | f99b94e | 2013-01-11 16:40:01 +0000 | [diff] [blame] | 98 | DoRecycle(&tmp, cb); |
| 99 | } |
| 100 | |
| 101 | void NOINLINE DoRecycle(Cache *c, Callback cb) { |
| 102 | while (QuarantineBatch *b = c->DequeueBatch()) { |
| 103 | const uptr kPrefetch = 16; |
| 104 | for (uptr i = 0; i < kPrefetch; i++) |
| 105 | PREFETCH(b->batch[i]); |
| 106 | for (uptr i = 0; i < b->count; i++) { |
| 107 | PREFETCH(b->batch[i + kPrefetch]); |
Dmitry Vyukov | f794195 | 2013-01-11 11:39:59 +0000 | [diff] [blame] | 108 | cb.Recycle((Node*)b->batch[i]); |
Dmitry Vyukov | f99b94e | 2013-01-11 16:40:01 +0000 | [diff] [blame] | 109 | } |
Dmitry Vyukov | f794195 | 2013-01-11 11:39:59 +0000 | [diff] [blame] | 110 | cb.Deallocate(b); |
| 111 | } |
| 112 | } |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 115 | // Per-thread cache of memory blocks. |
| 116 | template<typename Callback> |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 117 | class QuarantineCache { |
| 118 | public: |
| 119 | explicit QuarantineCache(LinkerInitialized) { |
| 120 | } |
| 121 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 122 | QuarantineCache() |
| 123 | : size_() { |
| 124 | list_.clear(); |
| 125 | } |
| 126 | |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 127 | uptr Size() const { |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 128 | return atomic_load(&size_, memory_order_relaxed); |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 131 | void Enqueue(Callback cb, void *ptr, uptr size) { |
Kostya Serebryany | a650de8 | 2013-10-21 08:36:10 +0000 | [diff] [blame] | 132 | if (list_.empty() || list_.back()->count == QuarantineBatch::kSize) { |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 133 | AllocBatch(cb); |
Kostya Serebryany | a650de8 | 2013-10-21 08:36:10 +0000 | [diff] [blame] | 134 | size += sizeof(QuarantineBatch); // Count the batch in Quarantine size. |
| 135 | } |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 136 | QuarantineBatch *b = list_.back(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 137 | CHECK(b); |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 138 | b->batch[b->count++] = ptr; |
| 139 | b->size += size; |
| 140 | SizeAdd(size); |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 143 | void Transfer(QuarantineCache *c) { |
| 144 | list_.append_back(&c->list_); |
| 145 | SizeAdd(c->Size()); |
| 146 | atomic_store(&c->size_, 0, memory_order_relaxed); |
| 147 | } |
| 148 | |
| 149 | void EnqueueBatch(QuarantineBatch *b) { |
| 150 | list_.push_back(b); |
| 151 | SizeAdd(b->size); |
| 152 | } |
| 153 | |
| 154 | QuarantineBatch *DequeueBatch() { |
| 155 | if (list_.empty()) |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 156 | return nullptr; |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 157 | QuarantineBatch *b = list_.front(); |
| 158 | list_.pop_front(); |
Kostya Serebryany | a650de8 | 2013-10-21 08:36:10 +0000 | [diff] [blame] | 159 | SizeSub(b->size); |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 160 | return b; |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private: |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 164 | IntrusiveList<QuarantineBatch> list_; |
| 165 | atomic_uintptr_t size_; |
| 166 | |
| 167 | void SizeAdd(uptr add) { |
| 168 | atomic_store(&size_, Size() + add, memory_order_relaxed); |
| 169 | } |
Kostya Serebryany | a650de8 | 2013-10-21 08:36:10 +0000 | [diff] [blame] | 170 | void SizeSub(uptr sub) { |
| 171 | atomic_store(&size_, Size() - sub, memory_order_relaxed); |
| 172 | } |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 173 | |
Timur Iskhodzhanov | 2b10d39 | 2013-02-08 12:02:00 +0000 | [diff] [blame] | 174 | NOINLINE QuarantineBatch* AllocBatch(Callback cb) { |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 175 | QuarantineBatch *b = (QuarantineBatch *)cb.Allocate(sizeof(*b)); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 176 | CHECK(b); |
Dmitry Vyukov | a61ec81 | 2013-01-11 11:03:35 +0000 | [diff] [blame] | 177 | b->count = 0; |
| 178 | b->size = 0; |
| 179 | list_.push_back(b); |
| 180 | return b; |
| 181 | } |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 182 | }; |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 183 | } // namespace __sanitizer |
Dmitry Vyukov | 9fc0df8 | 2013-01-11 08:07:43 +0000 | [diff] [blame] | 184 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 185 | #endif // SANITIZER_QUARANTINE_H |