Basic Prerequisites: 05 · Failure Modes and 06 · Advanced Extensions (C++) (benchmark methodology). The PTP math lives in 04 · Clock Synchronization - this page is the measurement-and-tooling practice that sits on it.
1. Intuition & Practical Objective
You cannot tune what you cannot measure - but in low-latency work the obvious measurement is wrong. Naive timing wraps a timer around one call and reports timer overhead (tens of ns) rather than the work; a histogram with coarse bins cannot resolve the percentile you actually quote; and a clock disciplined by an asymmetric PTP path reports timestamps that are biased by half the asymmetry. The objective of this page: the measurement discipline that turns “it feels fast” into trustworthy percentiles.
Three “aha”s:
- Report percentiles, not means - and size your bins to the percentile. A p99.9 quoted from 100 ns-wide bins carries up to 100 ns of error; quoting it from 1 000 ns bins carries 10× that. The tail estimate is the most bin-sensitive number you have.
perf’s histogram is a sampling tool, not a stopwatch. The kernel counts where time goes; the histogram of latencies you build yourself is what you tune against. Both have binning error - know it before you trust either.- PTP gives you a clock plane, not a free lunch. Sub-100 ns clocks exist (grandmaster + hardware timestamping), but only if the offset estimator’s symmetry assumption holds and the oscillator is continuously disciplined (04 · Clock Synchronization).
One-sentence essence. “Measurement is an instrument you must calibrate: batch to beat timer overhead, size histogram bins to the percentile you quote, and discipline the clock - because every latency number is only as honest as the histogram and the clock that produced it.”
2. Mathematical Ground Truth & Derivations
Estimator bias from timer overhead. A single-shot timing of a -latency operation wrapped by a timer of cost reports . When tens of ns and tens of ns, you measured the clock, not the work. Batching operations inside one timed region amortises it:
Histogram binning error. A histogram with bin width estimates a quantile only to within (the midpoint of the containing bin). The p99.9 of a distribution with a long tail is the most sensitive: for bin width , the worst-case p99.9 error is . From the §3 run: 100 ns bins give ~11 ns p99.9 error; 1 000 ns bins give ~61 ns - coarse bins bias the tail more than the median.
Clock offset (recap, cross-linked). PTP/NTP estimate the offset from four timestamps as , exact only when forward/reverse transit are symmetric. Hardware timestamping (NIC MAC-level) removes most of the software timestamping variance, which is why exchange racks run PTP-grandmaster + GPS.
3. Computational Implementation - the histogram-binning error model
Verifies §2: simulates a realistic latency distribution, buckets it into 100 ns and 1 000 ns bins (the two common perf/custom histogram widths), and shows how bin width biases the tail estimate far more than the median. Stdlib only.
The median estimate is nearly unchanged across a 10× change in bin width (1.45 vs 1.50 µs), but the p99.9 error grows ~6× (11 ns → 61 ns). So a coarse histogram flatters the median while corrupting the exact number that decides races - the practical reason to pick your histogram bin width after you know which percentile you quote, and to batch enough samples to keep the tail bins populated.
4. Failure Modes & First-Principles Breakdowns
- Quoting a p99.9 from bins too coarse to hold it. If your bins are 1 000 ns and your p99.9 moves by 50 ns, the histogram cannot see the change - you are tuning blind. Size bins to the percentile you quote (§3 quantifies this).
- Trusting a single-shot timer. Timer overhead (~tens of ns) is the same order as the operation; a single
clock_gettimearound one call reports the clock, not the work. Always batch and divide. - An undisciplined clock. PTP/GPS exist because a bare oscillator drifts 50 µs/s; a “sub-µs” timestamp on a drifting clock is a lie (05 · Clock Synchronization). Confirm the clock is hardware-stamped and continuously disciplined before you trust cross-host ordering.
- Mixing measurement points. IAT measured at the app vs at the wire differ by your own receive-path latency; comparing across points invents a “regression.” Always measure at the same point you intend to optimise.
5. References
- perf /
perf stat/perf recorddocumentation (kernel.org) - IEEE 1588-2008 (PTP) and NTP (RFC 5905)
- NIST PTP guidance & exchange timestamp specs (CME, Nasdaq, Cboe)
- Kerrisk, Michael - The Linux Programming Interface:
clock_gettime, timers,perf_event_open - Montgomery, D. - Introduction to Statistical Quality Control: EWMA monitoring (links to 05 · Failure Modes).
6. Connected Graph Bridges
- Back: 05 · Failure Modes · Index Hub
- Benchmark methodology: 06 · Advanced Extensions (C++)
- Clock plane: 04 · Clock Synchronization · 06 · Clock Sync Extensions
- Data quality downstream: Tick-Level Databases & Time Series
- Statistics: Probability & Measure Theory (quantiles, estimation error)