1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Periodic-event timer safe to call from a busy-spin hot loop.
//!
//! Calling `Instant::now()` on every iteration of a thread that busy-
//! spins at ~10 M iterations/s costs ~15–25 % of a CPU core on Linux
//! x86_64 — the vDSO `clock_gettime(CLOCK_MONOTONIC)` is fast (~15–25 ns)
//! but not free, and the hot path has nothing else in it to amortize
//! that cost against. Multiple stages in the transport busy-spin; a naive
//! `last.elapsed() >= period` check in any of them is a measurable
//! throughput tax (seen on the replica receiver, the primary per-slot
//! replication handler, and the shadow snapshot stage — in the latter,
//! ~10 % of total process cycles landed on `__vdso_clock_gettime`
//! before this timer was adopted).
//!
//! [`AmortizedTimer::tick`](crate::amortized_timer::AmortizedTimer::tick)
//! reads the clock only once every `CHECK_MASK` + 1 iterations (~65 k)
//! **while spinning**. When
//! the caller has fallen back to `yield_now()` the loop rate drops to
//! scheduler timeslice frequency (typically hundreds of iterations per
//! second), so the mask would delay a 5 s heartbeat by 2^16 × yield_latency
//! ≈ 1 minute. Passing `spinning = false` bypasses the mask and reads
//! the clock every iteration; the yield syscall already paid orders of
//! magnitude more than a vDSO clock read.
//!
//! Not a general-purpose rate limiter: it is tuned for
//! once-per-second-ish checks called from loops that execute millions
//! of times per second. Use a real clock-based timer for anything
//! needing tight timing or for infrequent loops.