|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
HDR-style histogram for latency distribution with logarithmic buckets. More...
#include <latency_histogram.h>

Public Member Functions | |
| LatencyHistogram () | |
| Default constructor - initializes all buckets to zero. | |
| LatencyHistogram (const LatencyHistogram &other) | |
| Copy constructor. | |
| LatencyHistogram (LatencyHistogram &&other) noexcept | |
| Move constructor. | |
| LatencyHistogram & | operator= (const LatencyHistogram &other) |
| Copy assignment operator. | |
| LatencyHistogram & | operator= (LatencyHistogram &&other) noexcept |
| Move assignment operator. | |
| ~LatencyHistogram ()=default | |
| Destructor. | |
| void | record (std::chrono::nanoseconds value) |
| Record a latency value. | |
| void | record_ns (std::uint64_t nanoseconds) |
| Record a raw nanosecond value. | |
| double | percentile (double p) const |
| Calculate the value at a given percentile. | |
| double | p50 () const |
| Get the 50th percentile (median). | |
| double | p90 () const |
| Get the 90th percentile. | |
| double | p95 () const |
| Get the 95th percentile. | |
| double | p99 () const |
| Get the 99th percentile. | |
| double | p999 () const |
| Get the 99.9th percentile. | |
| double | mean () const |
| Calculate the arithmetic mean. | |
| double | stddev () const |
| Calculate the standard deviation. | |
| std::uint64_t | min () const |
| Get the minimum recorded value. | |
| std::uint64_t | max () const |
| Get the maximum recorded value. | |
| std::uint64_t | count () const |
| Get the total count of recorded values. | |
| std::uint64_t | sum () const |
| Get the sum of all recorded values. | |
| void | reset () |
| Reset all buckets and counters to zero. | |
| bool | empty () const |
| Check if the histogram is empty. | |
| void | merge (const LatencyHistogram &other) |
| Merge another histogram into this one. | |
| std::uint64_t | bucket_count (std::size_t bucket_index) const |
| Get the bucket count at a specific index. | |
Static Public Member Functions | |
| static std::uint64_t | bucket_lower_bound (std::size_t bucket_index) |
| Get the lower bound of a bucket. | |
| static std::uint64_t | bucket_upper_bound (std::size_t bucket_index) |
| Get the upper bound of a bucket. | |
Static Public Attributes | |
| static constexpr std::size_t | BUCKET_COUNT = 64 |
| Number of histogram buckets. | |
Static Private Member Functions | |
| static std::size_t | compute_bucket_index (std::uint64_t value) |
| Compute the bucket index for a given value. | |
| static double | bucket_midpoint (std::size_t bucket_index) |
| Get the midpoint value of a bucket for estimation. | |
Private Attributes | |
| std::array< std::atomic< std::uint64_t >, BUCKET_COUNT > | buckets_ |
| Histogram buckets using logarithmic distribution. | |
| std::atomic< std::uint64_t > | total_count_ {0} |
| Total number of recorded values. | |
| std::atomic< std::uint64_t > | total_sum_ {0} |
| Sum of all recorded values. | |
| std::atomic< std::uint64_t > | min_value_ {std::numeric_limits<std::uint64_t>::max()} |
| Minimum recorded value. | |
| std::atomic< std::uint64_t > | max_value_ {0} |
| Maximum recorded value. | |
HDR-style histogram for latency distribution with logarithmic buckets.
This class provides a lock-free, thread-safe histogram for recording latency measurements. It uses logarithmic bucketing to efficiently represent a wide range of values (from nanoseconds to seconds) with bounded memory usage.
Uses 64 logarithmic buckets covering the range from 0 to ~10^19 nanoseconds:
Definition at line 51 of file latency_histogram.h.
| kcenon::thread::metrics::LatencyHistogram::LatencyHistogram | ( | ) |
Default constructor - initializes all buckets to zero.
Definition at line 13 of file latency_histogram.cpp.
References buckets_.
| kcenon::thread::metrics::LatencyHistogram::LatencyHistogram | ( | const LatencyHistogram & | other | ) |
Copy constructor.
| other | The histogram to copy from. |
Definition at line 19 of file latency_histogram.cpp.
References BUCKET_COUNT, buckets_, max_value_, min_value_, total_count_, and total_sum_.
|
noexcept |
Move constructor.
| other | The histogram to move from. |
Definition at line 34 of file latency_histogram.cpp.
|
default |
Destructor.
|
nodiscard |
Get the bucket count at a specific index.
| bucket_index | The bucket index (0 to BUCKET_COUNT-1). |
Definition at line 259 of file latency_histogram.cpp.
References BUCKET_COUNT, and buckets_.
|
staticnodiscard |
Get the lower bound of a bucket.
| bucket_index | The bucket index. |
Definition at line 266 of file latency_histogram.cpp.
References BUCKET_COUNT.
Referenced by bucket_midpoint(), and percentile().

|
staticnodiscardprivate |
Get the midpoint value of a bucket for estimation.
| bucket_index | The bucket index. |
Definition at line 309 of file latency_histogram.cpp.
References bucket_lower_bound(), and bucket_upper_bound().
Referenced by percentile(), and stddev().


|
staticnodiscard |
Get the upper bound of a bucket.
| bucket_index | The bucket index. |
Definition at line 276 of file latency_histogram.cpp.
References BUCKET_COUNT.
Referenced by bucket_midpoint(), and percentile().

|
staticnodiscardprivate |
Compute the bucket index for a given value.
| value | The value in nanoseconds. |
Definition at line 283 of file latency_histogram.cpp.
References BUCKET_COUNT.
Referenced by record_ns().

|
nodiscard |
Get the total count of recorded values.
Definition at line 203 of file latency_histogram.cpp.
References total_count_.
|
nodiscard |
Check if the histogram is empty.
Definition at line 222 of file latency_histogram.cpp.
References total_count_.
|
nodiscard |
Get the maximum recorded value.
Returns 0 if the histogram is empty.
Definition at line 195 of file latency_histogram.cpp.
References max_value_, and total_count_.
|
nodiscard |
Calculate the arithmetic mean.
Returns 0.0 if the histogram is empty. Note: The mean is approximated using bucket midpoints.
Definition at line 158 of file latency_histogram.cpp.
References total_count_, and total_sum_.
Referenced by stddev().

| void kcenon::thread::metrics::LatencyHistogram::merge | ( | const LatencyHistogram & | other | ) |
Merge another histogram into this one.
| other | The histogram to merge. |
All bucket counts from other are added to this histogram.
Definition at line 226 of file latency_histogram.cpp.
References BUCKET_COUNT, buckets_, max_value_, min_value_, total_count_, and total_sum_.
|
nodiscard |
Get the minimum recorded value.
Returns 0 if the histogram is empty.
Definition at line 187 of file latency_histogram.cpp.
References min_value_, and total_count_.
| LatencyHistogram & kcenon::thread::metrics::LatencyHistogram::operator= | ( | const LatencyHistogram & | other | ) |
Copy assignment operator.
| other | The histogram to copy from. |
Definition at line 52 of file latency_histogram.cpp.
References BUCKET_COUNT, buckets_, max_value_, min_value_, total_count_, and total_sum_.
|
noexcept |
Move assignment operator.
| other | The histogram to move from. |
Definition at line 70 of file latency_histogram.cpp.
|
inlinenodiscard |
Get the 50th percentile (median).
Definition at line 124 of file latency_histogram.h.
References percentile().
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::snapshot().


|
inlinenodiscard |
Get the 90th percentile.
Definition at line 130 of file latency_histogram.h.
References percentile().
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::snapshot().


|
inlinenodiscard |
Get the 95th percentile.
Definition at line 136 of file latency_histogram.h.
References percentile().

|
inlinenodiscard |
Get the 99th percentile.
Definition at line 142 of file latency_histogram.h.
References percentile().
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::snapshot().


|
inlinenodiscard |
Get the 99.9th percentile.
Definition at line 148 of file latency_histogram.h.
References percentile().

|
nodiscard |
Calculate the value at a given percentile.
| p | The percentile (0.0 to 1.0, e.g., 0.99 for P99). |
Returns 0 if the histogram is empty. For an empty bucket at the target percentile, returns the bucket's upper bound to provide an upper estimate.
Definition at line 122 of file latency_histogram.cpp.
References BUCKET_COUNT, bucket_lower_bound(), bucket_midpoint(), bucket_upper_bound(), buckets_, and total_count_.
Referenced by p50(), p90(), p95(), p99(), and p999().


| void kcenon::thread::metrics::LatencyHistogram::record | ( | std::chrono::nanoseconds | value | ) |
Record a latency value.
| value | The latency duration to record. |
This operation is lock-free and thread-safe. Complexity: O(1) - single atomic increment.
Definition at line 91 of file latency_histogram.cpp.
References record_ns().
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_enqueue(), kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_execution(), and kcenon::thread::metrics::EnhancedThreadPoolMetrics::record_wait_time().


| void kcenon::thread::metrics::LatencyHistogram::record_ns | ( | std::uint64_t | nanoseconds | ) |
Record a raw nanosecond value.
| nanoseconds | The latency in nanoseconds. |
Definition at line 95 of file latency_histogram.cpp.
References buckets_, compute_bucket_index(), max_value_, min_value_, total_count_, and total_sum_.
Referenced by record().


| void kcenon::thread::metrics::LatencyHistogram::reset | ( | ) |
Reset all buckets and counters to zero.
This operation is thread-safe but not atomic with respect to concurrent records. Some in-flight records may be lost during reset.
Definition at line 211 of file latency_histogram.cpp.
References buckets_, max_value_, min_value_, total_count_, and total_sum_.
Referenced by kcenon::thread::metrics::EnhancedThreadPoolMetrics::reset().

|
nodiscard |
Calculate the standard deviation.
Returns 0.0 if the histogram has fewer than 2 samples.
Definition at line 166 of file latency_histogram.cpp.
References BUCKET_COUNT, bucket_midpoint(), buckets_, mean(), and total_count_.

|
nodiscard |
Get the sum of all recorded values.
Definition at line 207 of file latency_histogram.cpp.
References total_sum_.
|
staticconstexpr |
Number of histogram buckets.
Definition at line 56 of file latency_histogram.h.
Referenced by bucket_count(), bucket_lower_bound(), bucket_upper_bound(), compute_bucket_index(), LatencyHistogram(), merge(), operator=(), percentile(), and stddev().
|
private |
Histogram buckets using logarithmic distribution.
Definition at line 242 of file latency_histogram.h.
Referenced by bucket_count(), LatencyHistogram(), LatencyHistogram(), merge(), operator=(), percentile(), record_ns(), reset(), and stddev().
|
private |
Maximum recorded value.
Definition at line 262 of file latency_histogram.h.
Referenced by LatencyHistogram(), max(), merge(), operator=(), record_ns(), and reset().
|
private |
Minimum recorded value.
Definition at line 257 of file latency_histogram.h.
Referenced by LatencyHistogram(), merge(), min(), operator=(), record_ns(), and reset().
|
private |
Total number of recorded values.
Definition at line 247 of file latency_histogram.h.
Referenced by count(), empty(), LatencyHistogram(), max(), mean(), merge(), min(), operator=(), percentile(), record_ns(), reset(), and stddev().
|
private |
Sum of all recorded values.
Definition at line 252 of file latency_histogram.h.
Referenced by LatencyHistogram(), mean(), merge(), operator=(), record_ns(), reset(), and sum().