| 1 | #cxx . . $ `<file>/src/include/poissonsampler.hpp`/pragma once |
| 2 | #include <random> |
| 3 | #include <unordered_map> |
| 4 | |
| 5 | /** |
| 6 | * @brief "triggers" samples using a geometric distribution |
| 7 | * |
| 8 | * Sampled recording allocated objects with a rate of every |
| 9 | * SAMPLE_INTERVAL bytes (on average). |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define PRINT_STATScxx . . $ `src/include/poissonsampler.hpp:13:9`! 0 |
| 14 | |
| 15 | class PoissonSamplercxx . . $ PoissonSampler# { |
| 16 | public: |
| 17 | /** |
| 18 | * @brief Construct a new SampleInterval object |
| 19 | * |
| 20 | */ |
| 21 | PoissonSamplercxx . . $ PoissonSampler#PoissonSampler(2c44cd906ae4e829).(uint64_tcxx . . $ uint64_t# SAMPLE_INTERVAL) |
| 22 | : gencxx . . $ PoissonSampler#gen.(rdcxx . . $ PoissonSampler#rd.(cxx . . $ std/random_device#`operator()`(db52760e79bf9054).)), dcxx . . $ PoissonSampler#d.(1.0 / SAMPLE_INTERVAL), allocscxx . . $ PoissonSampler#allocs.(0), freescxx . . $ PoissonSampler#frees.(0) { |
| 23 | resetAlloccxx . . $ PoissonSampler#resetAlloc(49f6e7a06ebc5aa8).(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @brief Deallocate an object; if sampled, return the size of the recorded |
| 28 | * sampling interval, else 0. |
| 29 | * |
| 30 | * @param sample |
| 31 | * @return uint64_t the previous sample interval if we crossed it; 0 otherwise |
| 32 | */ |
| 33 | inline bool decrementcxx . . $ PoissonSampler#decrement(9031ac04415d63cc).(uint64_tcxx . . $ uint64_t# sample, void* ptr, size_t& ret) { |
| 34 | #if 0 |
| 35 | auto found = _allocSize.find(ptr) != _allocSize.end(); |
| 36 | if (!found) { |
| 37 | // Not found |
| 38 | ret = 0; |
| 39 | return false; |
| 40 | } |
| 41 | // It was sampled. Return the recorded size, removing the object first. |
| 42 | ret = _allocSize[ptr]; |
| 43 | _allocSize.erase(ptr); |
| 44 | frees += ret; |
| 45 | #else |
| 46 | if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(sample > _tillNextAlloc)) { |
| 47 | auto prev = _countdownAlloccxx . . $ PoissonSampler#_countdownAlloc.; |
| 48 | auto diff = sample - _tillNextAlloccxx . . $ PoissonSampler#_tillNextAlloc.; |
| 49 | resetAlloccxx . . $ PoissonSampler#resetAlloc(49f6e7a06ebc5aa8).(); |
| 50 | ret = prev + diff; |
| 51 | freescxx . . $ PoissonSampler#frees. += ret; |
| 52 | #if PRINT_STATScxx . . $ `src/include/poissonsampler.hpp:13:9`! |
| 53 | printf_("DEALLOC %p %lu (%lu)\n", ptr, ret, (allocs - frees) / 1048576); |
| 54 | #endif |
| 55 | return true; |
| 56 | } else { |
| 57 | ret = 0; |
| 58 | return false; |
| 59 | } |
| 60 | #endif |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @brief increment by the sample amount, triggering an interval reset when we |
| 66 | * cross the threshold |
| 67 | * |
| 68 | * @param sample the amount to decrement the sample interval by |
| 69 | * @return uint64_t the previous sample interval if we crossed it; 0 otherwise |
| 70 | */ |
| 71 | inline bool incrementcxx . . $ PoissonSampler#increment(9031ac04415d63cc).(uint64_tcxx . . $ uint64_t# sample, void* ptr, size_t& ret) { |
| 72 | if (unlikelycxx . . $ `src/include/common.hpp:8:9`!(sample > _tillNextAlloc)) { |
| 73 | auto prev = _countdownAlloccxx . . $ PoissonSampler#_countdownAlloc.; |
| 74 | auto diff = sample - _tillNextAlloccxx . . $ PoissonSampler#_tillNextAlloc.; |
| 75 | resetAlloccxx . . $ PoissonSampler#resetAlloc(49f6e7a06ebc5aa8).(); |
| 76 | ret = prev + diff; |
| 77 | // _allocSize[ptr] = ret; |
| 78 | allocscxx . . $ PoissonSampler#allocs. += ret; |
| 79 | #if PRINT_STATScxx . . $ `src/include/poissonsampler.hpp:13:9`! |
| 80 | printf_("ALLOC %p %lu (%lu)\n", ptr, ret, (allocs - frees) / 1048576); |
| 81 | #endif |
| 82 | return true; |
| 83 | } |
| 84 | _tillNextAlloccxx . . $ PoissonSampler#_tillNextAlloc. -= sample; |
| 85 | ret = 0; |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | stdcxx . . $ std/::random_devicecxx . . $ std/random_device# rdcxx . . $ PoissonSampler#rd.; |
| 91 | stdcxx . . $ std/::mt19937cxx . . $ std/mt19937# gencxx . . $ PoissonSampler#gen.; |
| 92 | stdcxx . . $ std/::geometric_distributioncxx . . $ std/geometric_distribution#<uint64_tcxx . . $ uint64_t#> dcxx . . $ PoissonSampler#d.; |
| 93 | |
| 94 | uint64_tcxx . . $ uint64_t# _tillNextAlloccxx . . $ PoissonSampler#_tillNextAlloc.; |
| 95 | uint64_tcxx . . $ uint64_t# |
| 96 | _countdownAlloccxx . . $ PoissonSampler#_countdownAlloc.; /// the number of frees since the last sample interval |
| 97 | uint64_tcxx . . $ uint64_t# allocscxx . . $ PoissonSampler#allocs.; |
| 98 | uint64_tcxx . . $ uint64_t# freescxx . . $ PoissonSampler#frees.; |
| 99 | |
| 100 | stdcxx . . $ std/::unordered_mapcxx . . $ std/unordered_map#<void*, uint64_tcxx . . $ uint64_t#> _allocSizecxx . . $ PoissonSampler#_allocSize.; |
| 101 | |
| 102 | void resetAlloccxx . . $ PoissonSampler#resetAlloc(49f6e7a06ebc5aa8).() { |
| 103 | // Generate a new sample from the exponential distribution. |
| 104 | _countdownAlloccxx . . $ PoissonSampler#_countdownAlloc. = dcxx . . $ PoissonSampler#d.(cxx . . $ std/geometric_distribution#`operator()`(bd98775284160f6b).gencxx . . $ PoissonSampler#gen.); |
| 105 | _tillNextAlloccxx . . $ PoissonSampler#_tillNextAlloc. = _countdownAlloccxx . . $ PoissonSampler#_countdownAlloc.; |
| 106 | } |
| 107 | }; |