Debian Codegraph @

src/include/thresholdsampler.hpp

raw symbols SCIP
1 #pragma once
2 #include <unistd.h>
3
4 #include <random>
5
6 /**
7 * @brief "triggers" samples periodically when |increments-decrements| >
8 * SAMPLE_INTERVAL
9 *
10 */
11
12 #define PRINT_STATS 0
13
14 class ThresholdSampler {
15 public:
16 /**
17 * @brief Construct a new ThresholdSampler object
18 *
19 */
20 ThresholdSampler(uint64_t SAMPLE_INTERVAL)
21 : _sampleInterval(SAMPLE_INTERVAL), allocs(0), frees(0) {
22 reset();
23 }
24
25 /**
26 * @brief decrement by the sample amount, triggering an interval reset when we
27 * cross the threshold
28 *
29 * @param sample the amount to decrement the sample interval by
30 * @return bool true iff sampled
31 */
32 inline bool decrement(uint64_t sample, void*, size_t& ret) {
33 _decrements += sample;
34 if (unlikely(_decrements >= _increments + _sampleInterval)) {
35 #if PRINT_STATS
36 printf_("[%d] DEALLOC DECREMENT: %lu, %lu -> %lu\n", getpid(),
37 _decrements, _increments, _decrements - _increments);
38 #endif
39 ret = _decrements - _increments;
40 reset();
41 frees += ret;
42 return true;
43 }
44 return false;
45 }
46
47 /**
48 * @brief increment by the sample amount, triggering an interval reset when we
49 * cross the threshold
50 *
51 * @param sample the amount to decrement the sample interval by
52 * @return bool true iff sampled
53 */
54 inline bool increment(uint64_t sample, void*, size_t& ret) {
55 _increments += sample;
56 if (unlikely(_increments >= _decrements + _sampleInterval)) {
57 ret = _increments - _decrements;
58 #if PRINT_STATS
59 printf_("[%d] ALLOC INCREMENT: %lu, %lu -> %lu\n", getpid(), _decrements,
60 _increments, _increments - _decrements);
61 #endif
62 reset();
63 allocs += ret;
64 return true;
65 }
66 return false;
67 }
68
69 private:
70 void reset() {
71 _increments = 0;
72 _decrements = 0;
73 #if PRINT_STATS
74 printf_("FOOTPRINT = %lu\n", allocs - frees);
75 #endif
76 }
77
78 const uint64_t _sampleInterval; /// the current sample interval
79 uint64_t _increments; /// the number of increments since the last sample
80 /// interval reset
81 uint64_t _decrements; /// the number of decrements since the last sample
82 /// interval reset
83 uint64_t allocs;
84 uint64_t frees;
85 };