Skip to content

stats.h

Source: samples/common/core/stats.h

Log2-bucketed histogram and running statistics for latency tracking.


Structs

Log2Histogram

Fixed-size histogram with log2-spaced buckets.

Bucket b covers the half-open range [2^(b-1), 2^b) microseconds, with bucket 0 holding the count of zero-valued samples and bucket 63 acting as an overflow bin.

Members

Name Type Description
kBuckets constexpr int
buckets uint64_t

Methods

Log2Histogram()
Reset()

Zero all bucket counts.

Add(uint64_t us)

Record a single sample.

Parameters:

Name Type Description
us uint64_t Latency in microseconds.
Count() const

Total number of samples recorded across all buckets.

ApproxPercentile(double pct) const

Approximate a percentile from the histogram.

Parameters:

Name Type Description
pct double Percentile as a fraction in [0.0, 1.0].

Returns: Upper bound of the bucket that contains the target percentile.

BucketForUs(uint64_t us)

Compute the bucket index for a given microsecond value.

Parameters:

Name Type Description
us uint64_t Latency in microseconds.

Returns: Bucket index in [0, kBuckets).

RunningStats

Accumulates min/max/avg/percentile statistics for a stream of microsecond latency samples.

Members

Name Type Description
count uint64_t
sum_us uint64_t
min_us uint64_t
max_us uint64_t
hist Log2Histogram

Methods

Reset()

Reset all counters and the histogram.

Add(uint64_t us)

Record a single latency sample.

Parameters:

Name Type Description
us uint64_t Latency in microseconds.
AvgMs() const

Average latency in milliseconds (0.0 if no samples).

MinMs() const

Minimum latency in milliseconds.

MaxMs() const

Maximum latency in milliseconds.

P50Ms() const

Approximate 50th-percentile latency in milliseconds.

P90Ms() const

Approximate 90th-percentile latency in milliseconds.

P99Ms() const

Approximate 99th-percentile latency in milliseconds.