Expand description
Fixed-window latency profiling for the phases inside
crate::NativeRuntime::tick.
Before this module existed, “the node burns CPU with zero live PTY
sessions” had no number attached to it anywhere in the process –
tick() ran six distinct pieces of work every call (drain observations,
drain ingress, reduce the control plane, dispatch effects, publish the
step, and walk the provider supervisors) and none of them were timed.
This module gives each of those six phases its own always-on reading.
Two contracts every series here honours, mirrored from
hatchery-tui’s profile.rs (that crate is a different workspace and
is not a dependency of this one, so the types are re-derived here rather
than imported – see that module’s own doc comment for the same
reasoning spelled out for the TUI’s redraw loop):
- Distributions, not an average. A mean spreads one expensive tick
across 255 idle ones and reports a number nobody can act on. Every
phase keeps the last
SAMPLE_WINDOWsamples in a fixed ring (RingStats) and reports p50/p95/max plus the sample count, computed on demand rather than tracked incrementally. - Always-on cheap, no allocation after construction.
pushis an array write and an index bump. The O(N log N) sort behindstats()only runs when something actually reads a snapshot (an HTTP/metricsrequest), never once per tick – so this stays cheap enough to leave enabled unconditionally, which is the only way it can ever catch the stall it exists to find.
Structs§
- Distribution
- One windowed phase’s current reading: nearest-rank p50/p95/max over
whatever
RingStatscurrently holds, pluscount– the denominator a reader must check alongside the three numbers, sincecountbelowSAMPLE_WINDOWmeans “the process hasn’t produced a full window yet,” not “the window is smaller than advertised.” - Ring
Stats - Fixed-size ring buffer of
u32samples.pushoverwrites the oldest entry once full – the only state this holds isvalues/len/next, all stack-sized by the const genericN, so a profiler holding several of these never allocates past its own construction. - Tick
Phase Profiler - Owns the six per-phase rings
NativeRuntime::tickwrites into every call. Lives onNativeRuntimeitself (not a side channel) so the timing and the work it describes can never drift apart. - Tick
Profile Snapshot - One snapshot of every phase’s current distribution – what
crate::NativeRuntime::tick_profile_snapshotreturns.
Constants§
- SAMPLE_
WINDOW - Ring capacity shared by every phase below – “the last 256 ticks,” never an average across the process lifetime. At the ~10ms drive-loop cadence this is a little over 2.5 seconds of tick history, enough to catch a transient stall without growing unbounded.
Functions§
- duration_
micros - Clamped
Duration-> microsecond sample: a tick phase measured in hours would mean the process already hung far worse than this profiler needs to describe, so this saturates atu32::MAXrather than panicking or widening every sample to au128.