pub struct StageStats {
pub count: u64,
pub sum_ns: u64,
pub min_ns: u64,
pub max_ns: u64,
pub same_instant: u64,
pub backwards: u64,
pub unstamped: u64,
pub histogram: [u64; 960],
}Expand description
Fixed-size, non-allocating per-stage statistics: count, total, min, max, plus an HDR-style sub-bucketed histogram for percentile estimation, plus the three non-sample tallies that say why an observation produced no number at all.
count, sum_ns, min_ns and max_ns are exact. Quantiles are estimated
from the histogram and carry at most 3.125% relative error (exactly, for
deltas below 32 ns); see HISTOGRAM_BUCKETS.
§Not Copy
The histogram dominates the struct — a StageStats is
HISTOGRAM_BUCKETS × 8 bytes plus a handful of counters, several
kilobytes in total. It was Copy once, which made every by-value use a
silent multi-kilobyte memcpy; it is Clone-only now so a copy has to be
asked for. Reach for summary when all you want is the
numbers — a HopStats is small and genuinely Copy.
Fields§
§count: u64Number of measured deltas recorded for this stage — strictly
positive ones. Exact. See same_instant for the
zero-delta observations, which are counted but not measured.
sum_ns: u64Total of every recorded delta, in nanoseconds; saturating. Exact.
Divide by count for the mean.
min_ns: u64Smallest delta seen, in nanoseconds. Exact. u64::MAX before the
first observation.
max_ns: u64Largest delta seen, in nanoseconds. Exact. Zero before the first observation.
same_instant: u64Observations where both stamps were present and identical, i.e. the
two stages ran in the same engine cycle and shared
Ctx::wall_time’s per-cycle snap.
These are deliberately not folded into the histogram as 0 ns samples.
A hop that is structurally unmeasurable is not a hop that took under a
nanosecond, and recording it as one drags min, mean and p50 to
zero — which is exactly how a report can read count 120, min 0, p99 0
for a pair of stages that were never separately timed at all. Stamp
with Stamping::Precise to turn
these into real measurements.
backwards: u64Observations rejected because the later stage’s stamp preceded the earlier one’s. In one process this cannot happen; across processes it is the signature of unsynchronised clocks, and a non-zero tally here is the difference between “this hop is fast” and “these numbers are meaningless”.
unstamped: u64Observations skipped because one of the two stamps was never written (still zero). Distinguishes “this hop is not instrumented” from “this hop is instrumented and every sample was thrown away”.
histogram: [u64; 960]Sub-bucketed delta counts; see HISTOGRAM_BUCKETS for the layout.
Read quantiles out of it with StageStats::quantile_ns rather than
indexing it directly — the bucket layout is not part of the API contract.
Implementations§
Source§impl StageStats
impl StageStats
Sourcepub fn record(&mut self, delta_ns: u64)
pub fn record(&mut self, delta_ns: u64)
Fold one stage delta in: bumps count/sum_ns, widens the min/max,
and increments the histogram bucket it falls in. Allocation-free, so
it is safe on the graph path.
Sourcepub fn quantile_ns(&self, q: f64) -> u64
pub fn quantile_ns(&self, q: f64) -> u64
Estimate the value at quantile q in [0.0, 1.0], or 0 if no samples
have been recorded. q outside [0, 1] is clamped.
The rank is located in the histogram and then interpolated within its
bucket, so the estimate sits inside the bucket rather than at its edge,
and the result is clamped to the exactly-tracked [min_ns, max_ns].
Two properties follow, both of which the previous log2 implementation
broke: the result is always a value the stage could actually have
observed, and it is monotonic in q.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Drop every sample and tally, returning the stage to its zeroed state.
The building block of a windowed report: read a
summary, reset, and the next read describes only the
period since. Cumulative-since-start statistics cannot do that — one
outlier pins the p99 for the rest of the process’s life, which is the
difference between a gauge that tracks the system and one that records
its worst moment forever.
Sourcepub fn observations(&self) -> u64
pub fn observations(&self) -> u64
Every observation this stage saw, measured or not:
count + same_instant + backwards + unstamped.
Equal to the number of messages the report sink observed, for a stage
index that exists on the record — so a count well below it says a
large share of observations produced no measurement, and the three
tallies say which kind.
Sourcepub fn summary(&self, from: &'static str, to: &'static str) -> HopStats
pub fn summary(&self, from: &'static str, to: &'static str) -> HopStats
A small, Copy snapshot of this stage’s numbers, labelled with the hop
it describes.
Quantiles are evaluated once, here, rather than on every read — which
is what makes a HopStats cheap to pass around and to hold in a
stream value, where a StageStats would drag its whole histogram
along.
Sourcepub fn summary_unlabelled(&self) -> HopStats
pub fn summary_unlabelled(&self) -> HopStats
summary with empty labels — for an aggregator whose
stage names are runtime strings rather than &'static str (the Python
bindings), which applies its own labels afterwards. Keeping the numbers
here means the sentinel rule and the quantile set exist in one place.