| 1 | #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_STATS 0 |
| 14 | |
| 15 | class PoissonSampler { |
| 16 | public: |
| 17 | /** |
| 18 | * @brief Construct a new SampleInterval object |
| 19 | * |
| 20 | */ |
| 21 | PoissonSampler(uint64_t SAMPLE_INTERVAL) |
| 22 | : gen(rd()), d(1.0 / SAMPLE_INTERVAL), allocs(0), frees(0) { |
| 23 | resetAlloc(); |
| 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 decrement(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 (unlikely(sample > _tillNextAlloc)) { |
| 47 | auto prev = _countdownAlloc; |
| 48 | auto diff = sample - _tillNextAlloc; |
| 49 | resetAlloc(); |
| 50 | ret = prev + diff; |
| 51 | frees += ret; |
| 52 | #if PRINT_STATS |
| 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 increment(uint64_t sample, void* ptr, size_t& ret) { |
| 72 | if (unlikely(sample > _tillNextAlloc)) { |
| 73 | auto prev = _countdownAlloc; |
| 74 | auto diff = sample - _tillNextAlloc; |
| 75 | resetAlloc(); |
| 76 | ret = prev + diff; |
| 77 | // _allocSize[ptr] = ret; |
| 78 | allocs += ret; |
| 79 | #if PRINT_STATS |
| 80 | printf_("ALLOC %p %lu (%lu)\n", ptr, ret, (allocs - frees) / 1048576); |
| 81 | #endif |
| 82 | return true; |
| 83 | } |
| 84 | _tillNextAlloc -= sample; |
| 85 | ret = 0; |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | std::random_device rd; |
| 91 | std::mt19937 gen; |
| 92 | std::geometric_distribution<uint64_t> d; |
| 93 | |
| 94 | uint64_t _tillNextAlloc; |
| 95 | uint64_t |
| 96 | _countdownAlloc; /// the number of frees since the last sample interval |
| 97 | uint64_t allocs; |
| 98 | uint64_t frees; |
| 99 | |
| 100 | std::unordered_map<void*, uint64_t> _allocSize; |
| 101 | |
| 102 | void resetAlloc() { |
| 103 | // Generate a new sample from the exponential distribution. |
| 104 | _countdownAlloc = d(gen); |
| 105 | _tillNextAlloc = _countdownAlloc; |
| 106 | } |
| 107 | }; |