28#include <seastar/core/metrics_types.hh>
29#include <seastar/core/print.hh>
30#include <seastar/core/bitops.hh>
34namespace seastar::metrics::internal {
123template<u
int64_t Min, u
int64_t Max,
size_t Precision>
125 static constexpr unsigned NUM_EXP_RANGES = log2floor(Max/Min);
126 static constexpr size_t NUM_BUCKETS = NUM_EXP_RANGES * Precision + 1;
127 static constexpr unsigned PRECISION_BITS = log2floor(Precision);
128 static constexpr unsigned BASESHIFT = log2floor(Min);
129 static constexpr uint64_t LOWER_BITS_MASK = Precision - 1;
130 static constexpr size_t MIN_ID = log2ceil(Min) * Precision + 1;
131 std::array<uint64_t, NUM_BUCKETS> _buckets;
143 if (bucket_id == NUM_BUCKETS - 1) {
146 int16_t exp_rang = (bucket_id >> PRECISION_BITS);
147 return (Min << exp_rang) + ((bucket_id & LOWER_BITS_MASK) << (exp_rang + BASESHIFT - PRECISION_BITS));
156 if (bucket_id == NUM_BUCKETS - 1) {
157 return std::numeric_limits<uint64_t>::max();
169 return NUM_BUCKETS - 1;
174 uint16_t range = log2floor(val);
175 val >>= range - PRECISION_BITS;
176 return ((range - BASESHIFT) << PRECISION_BITS) + (val & LOWER_BITS_MASK);
183 std::fill(_buckets.begin(), _buckets.end(), 0);
202 for (
size_t i = 0; i < NUM_BUCKETS; i ++) {
203 if (_buckets[i] > 0) {
218 for (
int i = NUM_BUCKETS - 1; i >= 0; i--) {
219 if (_buckets[i] > 0) {
230 for (
size_t i = 0; i < NUM_BUCKETS; i++) {
231 _buckets[i] += b.get(i);
236 template<u
int64_t A, u
int64_t B,
size_t C>
242 uint64_t get(
size_t bucket)
const {
243 return _buckets[bucket];
262 if (quantile < 0 || quantile > 1.0) {
263 throw std::runtime_error(
"Invalid quantile value " + std::to_string(
quantile) +
". Value should be between 0 and 1");
271 auto pcount = uint64_t(std::floor(c *
quantile));
272 uint64_t elements = 0;
273 for (
size_t i = 0; i < NUM_BUCKETS - 2; i++) {
275 elements += _buckets[i];
276 if (elements >= pcount) {
289 uint64_t elements = 0;
291 for (
size_t i = 0; i < NUM_BUCKETS - 1; i++) {
292 elements += _buckets[i];
295 return (elements) ? sum / elements : 0;
310 for (
size_t i = 0; i < NUM_BUCKETS; i++) {
320 for (
size_t i = 0; i < NUM_BUCKETS; i++) {
326 uint64_t& operator[](
size_t b)
noexcept {
340 return PRECISION_BITS;
357 res.buckets.resize(
size() - 1);
358 uint64_t cummulative_count = 0;
362 for (
size_t i = 0; i < NUM_BUCKETS - 1; i++) {
363 auto& v = res.buckets[i];
365 cummulative_count += get(i);
366 v.count = cummulative_count;
367 res.sample_sum += get(i) * v.upper_bound;
370 res.sample_count = cummulative_count + get(NUM_BUCKETS - 1);
376template<u
int64_t Min, u
int64_t Max,
size_t NumBuckets>
377inline approximate_exponential_histogram<Min, Max, NumBuckets> base_estimated_histogram_merge(approximate_exponential_histogram<Min, Max, NumBuckets> a,
const approximate_exponential_histogram<Min, Max, NumBuckets>& b) {
395 void add_micro(uint64_t n) {
400 void add(
const T& latency) {
401 add_micro(std::chrono::duration_cast<std::chrono::microseconds>(latency).
count());
Definition: estimated_histogram.hh:124
uint64_t mean() const
returns the mean histogram value (average of bucket offsets, weighted by count) It will return 0 if t...
Definition: estimated_histogram.hh:288
void clear()
clear the current values.
Definition: estimated_histogram.hh:182
uint64_t get_bucket_upper_limit(uint16_t bucket_id) const
Returns the bucket upper limit given the bucket id. The last bucket (Infinity bucket) will return UMA...
Definition: estimated_histogram.hh:155
int32_t get_schema() const noexcept
get_schema the schema of a native histogram
Definition: estimated_histogram.hh:339
uint64_t max() const
returns the largest value that could have been added to this histogram. This method looks for the fir...
Definition: estimated_histogram.hh:217
uint64_t get_bucket_lower_limit(uint16_t bucket_id) const
Returns the bucket lower limit given the bucket id. The first and last bucket will always return the ...
Definition: estimated_histogram.hh:142
int32_t min_as_native_histogram_id() const noexcept
return the histogram min as a native histogram ID
Definition: estimated_histogram.hh:351
uint64_t count() const
returns the total number of values inserted
Definition: estimated_histogram.hh:308
approximate_exponential_histogram & merge(const approximate_exponential_histogram &b)
merge a histogram to the current one.
Definition: estimated_histogram.hh:229
approximate_exponential_histogram & operator*=(double v)
multiple all the buckets content in the histogram by a constant
Definition: estimated_histogram.hh:319
uint64_t min() const
returns the smallest value that could have been added to this histogram This method looks for the fir...
Definition: estimated_histogram.hh:201
size_t size() const
returns the number of buckets;
Definition: estimated_histogram.hh:301
uint64_t quantile(float quantile) const
get a histogram quantile
Definition: estimated_histogram.hh:261
void add(uint64_t n)
Add an item to the histogram Increments the count of the bucket holding that value.
Definition: estimated_histogram.hh:190
uint16_t find_bucket_index(uint64_t val) const
Find the bucket index for a given value The position of a value that is lower or equal to Min will al...
Definition: estimated_histogram.hh:167
estimated histogram for duration values time_estimated_histogram is used for short task timing....
Definition: estimated_histogram.hh:388
native histogram specific information
Definition: metrics_types.hh:51
Histogram data type.
Definition: metrics_types.hh:69