/Users/eugenesiegel/btc/bitcoin/src/support/lockedpool.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2016-2022 The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <support/lockedpool.h> |
6 | | #include <support/cleanse.h> |
7 | | |
8 | | #ifdef WIN32 |
9 | | #include <windows.h> |
10 | | #else |
11 | | #include <sys/mman.h> // for mmap |
12 | | #include <sys/resource.h> // for getrlimit |
13 | | #include <limits.h> // for PAGESIZE |
14 | | #include <unistd.h> // for sysconf |
15 | | #endif |
16 | | |
17 | | #include <algorithm> |
18 | | #include <limits> |
19 | | #include <stdexcept> |
20 | | #include <utility> |
21 | | #ifdef ARENA_DEBUG |
22 | | #include <iomanip> |
23 | | #include <iostream> |
24 | | #endif |
25 | | |
26 | | LockedPoolManager* LockedPoolManager::_instance = nullptr; |
27 | | |
28 | | /*******************************************************************************/ |
29 | | // Utilities |
30 | | // |
31 | | /** Align up to power of 2 */ |
32 | | static inline size_t align_up(size_t x, size_t align) |
33 | 49.9k | { |
34 | 49.9k | return (x + align - 1) & ~(align - 1); |
35 | 49.9k | } |
36 | | |
37 | | /*******************************************************************************/ |
38 | | // Implementation: Arena |
39 | | |
40 | | Arena::Arena(void *base_in, size_t size_in, size_t alignment_in): |
41 | 0 | base(base_in), end(static_cast<char*>(base_in) + size_in), alignment(alignment_in) |
42 | 0 | { |
43 | | // Start with one free chunk that covers the entire arena |
44 | 0 | auto it = size_to_free_chunk.emplace(size_in, base); |
45 | 0 | chunks_free.emplace(base, it); |
46 | 0 | chunks_free_end.emplace(static_cast<char*>(base) + size_in, it); |
47 | 0 | } |
48 | | |
49 | 0 | Arena::~Arena() = default; |
50 | | |
51 | | void* Arena::alloc(size_t size) |
52 | 49.9k | { |
53 | | // Round to next multiple of alignment |
54 | 49.9k | size = align_up(size, alignment); |
55 | | |
56 | | // Don't handle zero-sized chunks |
57 | 49.9k | if (size == 0) |
58 | 0 | return nullptr; |
59 | | |
60 | | // Pick a large enough free-chunk. Returns an iterator pointing to the first element that is not less than key. |
61 | | // This allocation strategy is best-fit. According to "Dynamic Storage Allocation: A Survey and Critical Review", |
62 | | // Wilson et. al. 1995, https://www.scs.stanford.edu/14wi-cs140/sched/readings/wilson.pdf, best-fit and first-fit |
63 | | // policies seem to work well in practice. |
64 | 49.9k | auto size_ptr_it = size_to_free_chunk.lower_bound(size); |
65 | 49.9k | if (size_ptr_it == size_to_free_chunk.end()) |
66 | 0 | return nullptr; |
67 | | |
68 | | // Create the used-chunk, taking its space from the end of the free-chunk |
69 | 49.9k | const size_t size_remaining = size_ptr_it->first - size; |
70 | 49.9k | char* const free_chunk = static_cast<char*>(size_ptr_it->second); |
71 | 49.9k | auto allocated = chunks_used.emplace(free_chunk + size_remaining, size).first; |
72 | 49.9k | chunks_free_end.erase(free_chunk + size_ptr_it->first); |
73 | 49.9k | if (size_ptr_it->first == size) { |
74 | | // whole chunk is used up |
75 | 0 | chunks_free.erase(size_ptr_it->second); |
76 | 49.9k | } else { |
77 | | // still some memory left in the chunk |
78 | 49.9k | auto it_remaining = size_to_free_chunk.emplace(size_remaining, size_ptr_it->second); |
79 | 49.9k | chunks_free[size_ptr_it->second] = it_remaining; |
80 | 49.9k | chunks_free_end.emplace(free_chunk + size_remaining, it_remaining); |
81 | 49.9k | } |
82 | 49.9k | size_to_free_chunk.erase(size_ptr_it); |
83 | | |
84 | 49.9k | return allocated->first; |
85 | 49.9k | } |
86 | | |
87 | | void Arena::free(void *ptr) |
88 | 49.9k | { |
89 | | // Freeing the nullptr pointer is OK. |
90 | 49.9k | if (ptr == nullptr) { |
91 | 0 | return; |
92 | 0 | } |
93 | | |
94 | | // Remove chunk from used map |
95 | 49.9k | auto i = chunks_used.find(ptr); |
96 | 49.9k | if (i == chunks_used.end()) { |
97 | 0 | throw std::runtime_error("Arena: invalid or double free"); |
98 | 0 | } |
99 | 49.9k | auto freed = std::make_pair(static_cast<char*>(i->first), i->second); |
100 | 49.9k | chunks_used.erase(i); |
101 | | |
102 | | // coalesce freed with previous chunk |
103 | 49.9k | auto prev = chunks_free_end.find(freed.first); |
104 | 49.9k | if (prev != chunks_free_end.end()) { |
105 | 49.9k | freed.first -= prev->second->first; |
106 | 49.9k | freed.second += prev->second->first; |
107 | 49.9k | size_to_free_chunk.erase(prev->second); |
108 | 49.9k | chunks_free_end.erase(prev); |
109 | 49.9k | } |
110 | | |
111 | | // coalesce freed with chunk after freed |
112 | 49.9k | auto next = chunks_free.find(freed.first + freed.second); |
113 | 49.9k | if (next != chunks_free.end()) { |
114 | 0 | freed.second += next->second->first; |
115 | 0 | size_to_free_chunk.erase(next->second); |
116 | 0 | chunks_free.erase(next); |
117 | 0 | } |
118 | | |
119 | | // Add/set space with coalesced free chunk |
120 | 49.9k | auto it = size_to_free_chunk.emplace(freed.second, freed.first); |
121 | 49.9k | chunks_free[freed.first] = it; |
122 | 49.9k | chunks_free_end[freed.first + freed.second] = it; |
123 | 49.9k | } |
124 | | |
125 | | Arena::Stats Arena::stats() const |
126 | 0 | { |
127 | 0 | Arena::Stats r{ 0, 0, 0, chunks_used.size(), chunks_free.size() }; |
128 | 0 | for (const auto& chunk: chunks_used) |
129 | 0 | r.used += chunk.second; |
130 | 0 | for (const auto& chunk: chunks_free) |
131 | 0 | r.free += chunk.second->first; |
132 | 0 | r.total = r.used + r.free; |
133 | 0 | return r; |
134 | 0 | } |
135 | | |
136 | | #ifdef ARENA_DEBUG |
137 | | static void printchunk(void* base, size_t sz, bool used) { |
138 | | std::cout << |
139 | | "0x" << std::hex << std::setw(16) << std::setfill('0') << base << |
140 | | " 0x" << std::hex << std::setw(16) << std::setfill('0') << sz << |
141 | | " 0x" << used << std::endl; |
142 | | } |
143 | | void Arena::walk() const |
144 | | { |
145 | | for (const auto& chunk: chunks_used) |
146 | | printchunk(chunk.first, chunk.second, true); |
147 | | std::cout << std::endl; |
148 | | for (const auto& chunk: chunks_free) |
149 | | printchunk(chunk.first, chunk.second->first, false); |
150 | | std::cout << std::endl; |
151 | | } |
152 | | #endif |
153 | | |
154 | | /*******************************************************************************/ |
155 | | // Implementation: Win32LockedPageAllocator |
156 | | |
157 | | #ifdef WIN32 |
158 | | /** LockedPageAllocator specialized for Windows. |
159 | | */ |
160 | | class Win32LockedPageAllocator: public LockedPageAllocator |
161 | | { |
162 | | public: |
163 | | Win32LockedPageAllocator(); |
164 | | void* AllocateLocked(size_t len, bool *lockingSuccess) override; |
165 | | void FreeLocked(void* addr, size_t len) override; |
166 | | size_t GetLimit() override; |
167 | | private: |
168 | | size_t page_size; |
169 | | }; |
170 | | |
171 | | Win32LockedPageAllocator::Win32LockedPageAllocator() |
172 | | { |
173 | | // Determine system page size in bytes |
174 | | SYSTEM_INFO sSysInfo; |
175 | | GetSystemInfo(&sSysInfo); |
176 | | page_size = sSysInfo.dwPageSize; |
177 | | } |
178 | | void *Win32LockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess) |
179 | | { |
180 | | len = align_up(len, page_size); |
181 | | void *addr = VirtualAlloc(nullptr, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
182 | | if (addr) { |
183 | | // VirtualLock is used to attempt to keep keying material out of swap. Note |
184 | | // that it does not provide this as a guarantee, but, in practice, memory |
185 | | // that has been VirtualLock'd almost never gets written to the pagefile |
186 | | // except in rare circumstances where memory is extremely low. |
187 | | *lockingSuccess = VirtualLock(const_cast<void*>(addr), len) != 0; |
188 | | } |
189 | | return addr; |
190 | | } |
191 | | void Win32LockedPageAllocator::FreeLocked(void* addr, size_t len) |
192 | | { |
193 | | len = align_up(len, page_size); |
194 | | memory_cleanse(addr, len); |
195 | | VirtualUnlock(const_cast<void*>(addr), len); |
196 | | } |
197 | | |
198 | | size_t Win32LockedPageAllocator::GetLimit() |
199 | | { |
200 | | size_t min, max; |
201 | | if(GetProcessWorkingSetSize(GetCurrentProcess(), &min, &max) != 0) { |
202 | | return min; |
203 | | } |
204 | | return std::numeric_limits<size_t>::max(); |
205 | | } |
206 | | #endif |
207 | | |
208 | | /*******************************************************************************/ |
209 | | // Implementation: PosixLockedPageAllocator |
210 | | |
211 | | #ifndef WIN32 |
212 | | /** LockedPageAllocator specialized for OSes that don't try to be |
213 | | * special snowflakes. |
214 | | */ |
215 | | class PosixLockedPageAllocator: public LockedPageAllocator |
216 | | { |
217 | | public: |
218 | | PosixLockedPageAllocator(); |
219 | | void* AllocateLocked(size_t len, bool *lockingSuccess) override; |
220 | | void FreeLocked(void* addr, size_t len) override; |
221 | | size_t GetLimit() override; |
222 | | private: |
223 | | size_t page_size; |
224 | | }; |
225 | | |
226 | | PosixLockedPageAllocator::PosixLockedPageAllocator() |
227 | 0 | { |
228 | | // Determine system page size in bytes |
229 | | #if defined(PAGESIZE) // defined in limits.h |
230 | | page_size = PAGESIZE; |
231 | | #else // assume some POSIX OS |
232 | 0 | page_size = sysconf(_SC_PAGESIZE); |
233 | 0 | #endif |
234 | 0 | } |
235 | | |
236 | | void *PosixLockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess) |
237 | 0 | { |
238 | 0 | void *addr; |
239 | 0 | len = align_up(len, page_size); |
240 | 0 | addr = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
241 | 0 | if (addr == MAP_FAILED) { |
242 | 0 | return nullptr; |
243 | 0 | } |
244 | 0 | if (addr) { |
245 | 0 | *lockingSuccess = mlock(addr, len) == 0; |
246 | | #if defined(MADV_DONTDUMP) // Linux |
247 | | madvise(addr, len, MADV_DONTDUMP); |
248 | | #elif defined(MADV_NOCORE) // FreeBSD |
249 | | madvise(addr, len, MADV_NOCORE); |
250 | | #endif |
251 | 0 | } |
252 | 0 | return addr; |
253 | 0 | } |
254 | | void PosixLockedPageAllocator::FreeLocked(void* addr, size_t len) |
255 | 0 | { |
256 | 0 | len = align_up(len, page_size); |
257 | 0 | memory_cleanse(addr, len); |
258 | 0 | munlock(addr, len); |
259 | 0 | munmap(addr, len); |
260 | 0 | } |
261 | | size_t PosixLockedPageAllocator::GetLimit() |
262 | 0 | { |
263 | 0 | #ifdef RLIMIT_MEMLOCK |
264 | 0 | struct rlimit rlim; |
265 | 0 | if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) { |
266 | 0 | if (rlim.rlim_cur != RLIM_INFINITY) { |
267 | 0 | return rlim.rlim_cur; |
268 | 0 | } |
269 | 0 | } |
270 | 0 | #endif |
271 | 0 | return std::numeric_limits<size_t>::max(); |
272 | 0 | } |
273 | | #endif |
274 | | |
275 | | /*******************************************************************************/ |
276 | | // Implementation: LockedPool |
277 | | |
278 | | LockedPool::LockedPool(std::unique_ptr<LockedPageAllocator> allocator_in, LockingFailed_Callback lf_cb_in) |
279 | 0 | : allocator(std::move(allocator_in)), lf_cb(lf_cb_in) |
280 | 0 | { |
281 | 0 | } |
282 | | |
283 | 0 | LockedPool::~LockedPool() = default; |
284 | | |
285 | | void* LockedPool::alloc(size_t size) |
286 | 49.9k | { |
287 | 49.9k | std::lock_guard<std::mutex> lock(mutex); |
288 | | |
289 | | // Don't handle impossible sizes |
290 | 49.9k | if (size == 0 || size > ARENA_SIZE) |
291 | 0 | return nullptr; |
292 | | |
293 | | // Try allocating from each current arena |
294 | 49.9k | for (auto &arena: arenas) { |
295 | 49.9k | void *addr = arena.alloc(size); |
296 | 49.9k | if (addr) { |
297 | 49.9k | return addr; |
298 | 49.9k | } |
299 | 49.9k | } |
300 | | // If that fails, create a new one |
301 | 0 | if (new_arena(ARENA_SIZE, ARENA_ALIGN)) { |
302 | 0 | return arenas.back().alloc(size); |
303 | 0 | } |
304 | 0 | return nullptr; |
305 | 0 | } |
306 | | |
307 | | void LockedPool::free(void *ptr) |
308 | 49.9k | { |
309 | 49.9k | std::lock_guard<std::mutex> lock(mutex); |
310 | | // TODO we can do better than this linear search by keeping a map of arena |
311 | | // extents to arena, and looking up the address. |
312 | 49.9k | for (auto &arena: arenas) { |
313 | 49.9k | if (arena.addressInArena(ptr)) { |
314 | 49.9k | arena.free(ptr); |
315 | 49.9k | return; |
316 | 49.9k | } |
317 | 49.9k | } |
318 | 0 | throw std::runtime_error("LockedPool: invalid address not pointing to any arena"); |
319 | 49.9k | } |
320 | | |
321 | | LockedPool::Stats LockedPool::stats() const |
322 | 0 | { |
323 | 0 | std::lock_guard<std::mutex> lock(mutex); |
324 | 0 | LockedPool::Stats r{0, 0, 0, cumulative_bytes_locked, 0, 0}; |
325 | 0 | for (const auto &arena: arenas) { |
326 | 0 | Arena::Stats i = arena.stats(); |
327 | 0 | r.used += i.used; |
328 | 0 | r.free += i.free; |
329 | 0 | r.total += i.total; |
330 | 0 | r.chunks_used += i.chunks_used; |
331 | 0 | r.chunks_free += i.chunks_free; |
332 | 0 | } |
333 | 0 | return r; |
334 | 0 | } |
335 | | |
336 | | bool LockedPool::new_arena(size_t size, size_t align) |
337 | 0 | { |
338 | 0 | bool locked; |
339 | | // If this is the first arena, handle this specially: Cap the upper size |
340 | | // by the process limit. This makes sure that the first arena will at least |
341 | | // be locked. An exception to this is if the process limit is 0: |
342 | | // in this case no memory can be locked at all so we'll skip past this logic. |
343 | 0 | if (arenas.empty()) { |
344 | 0 | size_t limit = allocator->GetLimit(); |
345 | 0 | if (limit > 0) { |
346 | 0 | size = std::min(size, limit); |
347 | 0 | } |
348 | 0 | } |
349 | 0 | void *addr = allocator->AllocateLocked(size, &locked); |
350 | 0 | if (!addr) { |
351 | 0 | return false; |
352 | 0 | } |
353 | 0 | if (locked) { |
354 | 0 | cumulative_bytes_locked += size; |
355 | 0 | } else if (lf_cb) { // Call the locking-failed callback if locking failed |
356 | 0 | if (!lf_cb()) { // If the callback returns false, free the memory and fail, otherwise consider the user warned and proceed. |
357 | 0 | allocator->FreeLocked(addr, size); |
358 | 0 | return false; |
359 | 0 | } |
360 | 0 | } |
361 | 0 | arenas.emplace_back(allocator.get(), addr, size, align); |
362 | 0 | return true; |
363 | 0 | } |
364 | | |
365 | | LockedPool::LockedPageArena::LockedPageArena(LockedPageAllocator *allocator_in, void *base_in, size_t size_in, size_t align_in): |
366 | 0 | Arena(base_in, size_in, align_in), base(base_in), size(size_in), allocator(allocator_in) |
367 | 0 | { |
368 | 0 | } |
369 | | LockedPool::LockedPageArena::~LockedPageArena() |
370 | 0 | { |
371 | 0 | allocator->FreeLocked(base, size); |
372 | 0 | } |
373 | | |
374 | | /*******************************************************************************/ |
375 | | // Implementation: LockedPoolManager |
376 | | // |
377 | | LockedPoolManager::LockedPoolManager(std::unique_ptr<LockedPageAllocator> allocator_in): |
378 | 0 | LockedPool(std::move(allocator_in), &LockedPoolManager::LockingFailed) |
379 | 0 | { |
380 | 0 | } |
381 | | |
382 | | bool LockedPoolManager::LockingFailed() |
383 | 0 | { |
384 | | // TODO: log something but how? without including util.h |
385 | 0 | return true; |
386 | 0 | } |
387 | | |
388 | | void LockedPoolManager::CreateInstance() |
389 | 0 | { |
390 | | // Using a local static instance guarantees that the object is initialized |
391 | | // when it's first needed and also deinitialized after all objects that use |
392 | | // it are done with it. I can think of one unlikely scenario where we may |
393 | | // have a static deinitialization order/problem, but the check in |
394 | | // LockedPoolManagerBase's destructor helps us detect if that ever happens. |
395 | | #ifdef WIN32 |
396 | | std::unique_ptr<LockedPageAllocator> allocator(new Win32LockedPageAllocator()); |
397 | | #else |
398 | 0 | std::unique_ptr<LockedPageAllocator> allocator(new PosixLockedPageAllocator()); |
399 | 0 | #endif |
400 | 0 | static LockedPoolManager instance(std::move(allocator)); |
401 | 0 | LockedPoolManager::_instance = &instance; |
402 | 0 | } |