44 sampler() : random_gen(rd_device()) {
49 sampling_interval_ = sampling_interval;
50 if (sampling_interval_ == 0) {
54 interval_to_next_sample_ = std::numeric_limits<int64_t>::max();
57 sampling_rate_ = 1.0 /
static_cast<double>(sampling_interval_);
58 interval_to_next_sample_ = next_sampling_interval();
66 [[gnu::always_inline]]
68 return (interval_to_next_sample_ -= alloc_size) < 0;
76 if (interval_to_next_sample_ >= 0) {
79 reset_interval_to_next_sample(alloc_size);
80 return sampling_interval_ != 0;
83 uint64_t sampling_interval()
const {
return sampling_interval_; }
87 return std::max(allocation_size, sampling_interval_);
95 , previous_sampling_interval_(sampler_->sampling_interval_)
96 , previous_sampling_rate_(sampler_->sampling_rate_)
97 , previous_interval_to_next_sample_(sampler_->interval_to_next_sample_) {
103 sampler_->sampling_interval_ = previous_sampling_interval_;
104 sampler_->sampling_rate_ = previous_sampling_rate_;
105 sampler_->interval_to_next_sample_ = previous_interval_to_next_sample_;
111 uint64_t previous_sampling_interval_ = 0;
112 double previous_sampling_rate_ = 0;
113 int64_t previous_interval_to_next_sample_ = 0;
130 void reset_interval_to_next_sample(
size_t alloc_size)
132 if (sampling_interval_ == 0) {
133 interval_to_next_sample_ = std::numeric_limits<int64_t>::max();
139 if (alloc_size > sampling_interval_) {
140 interval_to_next_sample_ += alloc_size;
143 while (interval_to_next_sample_ < 0) {
144 interval_to_next_sample_ += next_sampling_interval();
150 int64_t next_sampling_interval() {
151 std::exponential_distribution<double> dist(sampling_rate_);
152 int64_t next =
static_cast<int64_t
>(dist(random_gen));
158 uint64_t sampling_interval_;
159 double sampling_rate_;
163 int64_t interval_to_next_sample_;
164 std::random_device rd_device;
165 std::mt19937_64 random_gen;
Definition: sampler.hh:42
bool maybe_sample(size_t alloc_size)
Definition: sampler.hh:67
void set_sampling_interval(uint64_t sampling_interval)
Sets the sampling interval in bytes. Setting it to 0 means to never sample.
Definition: sampler.hh:48
bool definitely_sample(size_t alloc_size)
Definition: sampler.hh:74
size_t sample_size(size_t allocation_size) const
How much should an allocation of size allocation_size count for.
Definition: sampler.hh:86
disable_sampling_temporarily pause_sampling()
Definition: sampler.hh:122
RAII class to temporarily pause sampling.
Definition: sampler.hh:91