Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
latency_histogram.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
14#include <array>
15#include <atomic>
16#include <chrono>
17#include <cmath>
18#include <cstdint>
19#include <limits>
20
22
52public:
56 static constexpr std::size_t BUCKET_COUNT = 64;
57
62
68
73 LatencyHistogram(LatencyHistogram&& other) noexcept;
74
81
88
92 ~LatencyHistogram() = default;
93
101 void record(std::chrono::nanoseconds value);
102
107 void record_ns(std::uint64_t nanoseconds);
108
118 [[nodiscard]] double percentile(double p) const;
119
124 [[nodiscard]] double p50() const { return percentile(0.50); }
125
130 [[nodiscard]] double p90() const { return percentile(0.90); }
131
136 [[nodiscard]] double p95() const { return percentile(0.95); }
137
142 [[nodiscard]] double p99() const { return percentile(0.99); }
143
148 [[nodiscard]] double p999() const { return percentile(0.999); }
149
157 [[nodiscard]] double mean() const;
158
165 [[nodiscard]] double stddev() const;
166
173 [[nodiscard]] std::uint64_t min() const;
174
181 [[nodiscard]] std::uint64_t max() const;
182
187 [[nodiscard]] std::uint64_t count() const;
188
193 [[nodiscard]] std::uint64_t sum() const;
194
201 void reset();
202
207 [[nodiscard]] bool empty() const;
208
215 void merge(const LatencyHistogram& other);
216
222 [[nodiscard]] std::uint64_t bucket_count(std::size_t bucket_index) const;
223
229 [[nodiscard]] static std::uint64_t bucket_lower_bound(std::size_t bucket_index);
230
236 [[nodiscard]] static std::uint64_t bucket_upper_bound(std::size_t bucket_index);
237
238private:
242 std::array<std::atomic<std::uint64_t>, BUCKET_COUNT> buckets_;
243
247 std::atomic<std::uint64_t> total_count_{0};
248
252 std::atomic<std::uint64_t> total_sum_{0};
253
257 std::atomic<std::uint64_t> min_value_{std::numeric_limits<std::uint64_t>::max()};
258
262 std::atomic<std::uint64_t> max_value_{0};
263
269 [[nodiscard]] static std::size_t compute_bucket_index(std::uint64_t value);
270
276 [[nodiscard]] static double bucket_midpoint(std::size_t bucket_index);
277};
278
279} // namespace kcenon::thread::metrics
HDR-style histogram for latency distribution with logarithmic buckets.
LatencyHistogram()
Default constructor - initializes all buckets to zero.
void record(std::chrono::nanoseconds value)
Record a latency value.
double mean() const
Calculate the arithmetic mean.
static constexpr std::size_t BUCKET_COUNT
Number of histogram buckets.
std::uint64_t count() const
Get the total count of recorded values.
std::uint64_t sum() const
Get the sum of all recorded values.
std::uint64_t bucket_count(std::size_t bucket_index) const
Get the bucket count at a specific index.
static std::size_t compute_bucket_index(std::uint64_t value)
Compute the bucket index for a given value.
std::atomic< std::uint64_t > min_value_
Minimum recorded value.
double p50() const
Get the 50th percentile (median).
std::atomic< std::uint64_t > total_count_
Total number of recorded values.
std::array< std::atomic< std::uint64_t >, BUCKET_COUNT > buckets_
Histogram buckets using logarithmic distribution.
bool empty() const
Check if the histogram is empty.
double p95() const
Get the 95th percentile.
double p90() const
Get the 90th percentile.
static std::uint64_t bucket_lower_bound(std::size_t bucket_index)
Get the lower bound of a bucket.
double stddev() const
Calculate the standard deviation.
double p99() const
Get the 99th percentile.
double percentile(double p) const
Calculate the value at a given percentile.
std::atomic< std::uint64_t > max_value_
Maximum recorded value.
static std::uint64_t bucket_upper_bound(std::size_t bucket_index)
Get the upper bound of a bucket.
std::uint64_t min() const
Get the minimum recorded value.
std::atomic< std::uint64_t > total_sum_
Sum of all recorded values.
void reset()
Reset all buckets and counters to zero.
static double bucket_midpoint(std::size_t bucket_index)
Get the midpoint value of a bucket for estimation.
void record_ns(std::uint64_t nanoseconds)
Record a raw nanosecond value.
LatencyHistogram & operator=(const LatencyHistogram &other)
Copy assignment operator.
std::uint64_t max() const
Get the maximum recorded value.
void merge(const LatencyHistogram &other)
Merge another histogram into this one.
double p999() const
Get the 99.9th percentile.