suspect_machines 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,314 @@
1
+ //! Fixed-capacity table of per-peer detectors.
2
+
3
+ use core::time::Duration;
4
+
5
+ use crate::{
6
+ Beat, Config, ConfigError, Phi, Refusal, Statistics, Suspicion, Threshold, instant,
7
+ window::Window,
8
+ };
9
+
10
+ #[derive(Debug, Clone, PartialEq, Eq)]
11
+ struct Slot<const W: usize> {
12
+ peer: u64,
13
+ incarnation: u64,
14
+ window: Window<W>,
15
+ }
16
+
17
+ /// Whether `entry` is the occupied slot of `peer`.
18
+ fn holds<const W: usize>(entry: Option<&Slot<W>>, peer: u64) -> bool {
19
+ entry.is_some_and(|slot| slot.peer == peer)
20
+ }
21
+
22
+ /// A peer whose phi has reached the scan threshold.
23
+ ///
24
+ /// `Suspect::default()` is a placeholder for pre-filling a scan buffer; the
25
+ /// roster never emits it.
26
+ #[derive(Debug, Clone, Copy, Default, PartialEq)]
27
+ pub struct Suspect {
28
+ peer: u64,
29
+ incarnation: u64,
30
+ phi: Phi,
31
+ }
32
+
33
+ impl Suspect {
34
+ /// A suspect: any peer and incarnation, and its level.
35
+ #[must_use]
36
+ pub const fn new(peer: u64, incarnation: u64, phi: Phi) -> Self {
37
+ Self {
38
+ peer,
39
+ incarnation,
40
+ phi,
41
+ }
42
+ }
43
+
44
+ /// Caller-assigned peer identifier.
45
+ #[must_use]
46
+ pub const fn peer(&self) -> u64 {
47
+ self.peer
48
+ }
49
+
50
+ /// Incarnation the statistics belong to.
51
+ #[must_use]
52
+ pub const fn incarnation(&self) -> u64 {
53
+ self.incarnation
54
+ }
55
+
56
+ /// Suspicion level at the scan instant.
57
+ #[must_use]
58
+ pub const fn phi(&self) -> Phi {
59
+ self.phi
60
+ }
61
+ }
62
+
63
+ /// Result of one bounded [`Roster::scan`] call.
64
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
65
+ pub struct Scan {
66
+ suspects: usize,
67
+ next: Option<usize>,
68
+ }
69
+
70
+ impl Scan {
71
+ /// A scan a roster of `P` slots can answer: `None` when more than `P`
72
+ /// suspects are written or the resume cursor names no slot (`next >= P`).
73
+ #[must_use]
74
+ pub const fn new<const P: usize>(suspects: usize, next: Option<usize>) -> Option<Self> {
75
+ let resumable = match next {
76
+ Some(index) => index < P,
77
+ None => true,
78
+ };
79
+ if suspects > P || !resumable {
80
+ return None;
81
+ }
82
+ Some(Self { suspects, next })
83
+ }
84
+
85
+ /// How many suspects were written to the front of the output buffer.
86
+ #[must_use]
87
+ pub const fn suspects(&self) -> usize {
88
+ self.suspects
89
+ }
90
+
91
+ /// Cursor to resume from when a suspect did not fit; `None` once every
92
+ /// slot has been examined.
93
+ #[must_use]
94
+ pub const fn next(&self) -> Option<usize> {
95
+ self.next
96
+ }
97
+ }
98
+
99
+ /// What an honoured [`Roster::forget`] did.
100
+ #[must_use]
101
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
102
+ pub enum Forgetting {
103
+ /// The peer was tracked; its slot is now free.
104
+ Forgotten,
105
+ /// The peer was not tracked: an idempotent repeat that changed nothing.
106
+ NotTracked,
107
+ }
108
+
109
+ /// Up to `P` peers, each with a window of `W` intervals, sharing one policy.
110
+ ///
111
+ /// Peers are identified by a caller-assigned `u64` and an incarnation number
112
+ /// that the caller raises when a peer restarts. The roster never removes a
113
+ /// peer on its own: membership belongs to the caller.
114
+ #[derive(Debug, Clone, PartialEq, Eq)]
115
+ pub struct Roster<const P: usize, const W: usize> {
116
+ config: Config,
117
+ slots: [Option<Slot<W>>; P],
118
+ }
119
+
120
+ impl<const P: usize, const W: usize> Roster<P, W> {
121
+ /// Peers the roster can track.
122
+ pub const CAPACITY: usize = P;
123
+ /// Intervals kept per peer.
124
+ pub const WINDOW: usize = W;
125
+
126
+ /// An empty roster.
127
+ ///
128
+ /// # Errors
129
+ ///
130
+ /// [`ConfigError::ZeroWindow`] when `W` is zero and
131
+ /// [`ConfigError::WarmUpExceedsWindow`] when the warm-up could never
132
+ /// finish.
133
+ pub fn new(config: Config) -> Result<Self, ConfigError> {
134
+ Ok(Self {
135
+ config: config.fit(W)?,
136
+ slots: core::array::from_fn(|_| None),
137
+ })
138
+ }
139
+
140
+ fn find(&self, peer: u64) -> Option<&Slot<W>> {
141
+ self.slots
142
+ .iter()
143
+ .find(|entry| holds(entry.as_ref(), peer))?
144
+ .as_ref()
145
+ }
146
+
147
+ /// The slot entry holding `peer`, to update or free.
148
+ fn entry(&mut self, peer: u64) -> Option<&mut Option<Slot<W>>> {
149
+ self.slots
150
+ .iter_mut()
151
+ .find(|entry| holds(entry.as_ref(), peer))
152
+ }
153
+
154
+ /// Records a heartbeat from `peer` at `now`. An unknown peer takes a free
155
+ /// slot. A higher `incarnation` discards the peer's statistics and starts
156
+ /// a new warm-up.
157
+ ///
158
+ /// # Errors
159
+ ///
160
+ /// [`Refusal::Overflow`], [`Refusal::Stale`] for an older incarnation,
161
+ /// [`Refusal::TimeRegression`], [`Refusal::SameInstant`], or
162
+ /// [`Refusal::Full`] when an unknown peer finds no free slot. The roster
163
+ /// is unchanged.
164
+ pub fn heartbeat(
165
+ &mut self,
166
+ peer: u64,
167
+ incarnation: u64,
168
+ now: Duration,
169
+ ) -> Result<Beat, Refusal> {
170
+ let now = instant(now)?;
171
+ if let Some(Some(slot)) = self.entry(peer) {
172
+ if incarnation < slot.incarnation {
173
+ return Err(Refusal::Stale);
174
+ }
175
+ if incarnation == slot.incarnation {
176
+ return slot.window.observe(now);
177
+ }
178
+ if slot.window.last().is_some_and(|last| now < last) {
179
+ return Err(Refusal::TimeRegression);
180
+ }
181
+ slot.incarnation = incarnation;
182
+ slot.window = Window::started(now);
183
+ return Ok(Beat::Restarted);
184
+ }
185
+ let free = self
186
+ .slots
187
+ .iter_mut()
188
+ .find(|slot| slot.is_none())
189
+ .ok_or(Refusal::Full)?;
190
+ *free = Some(Slot {
191
+ peer,
192
+ incarnation,
193
+ window: Window::started(now),
194
+ });
195
+ Ok(Beat::First)
196
+ }
197
+
198
+ /// The suspicion for `peer` at `now`.
199
+ ///
200
+ /// # Errors
201
+ ///
202
+ /// [`Refusal::Overflow`], [`Refusal::Untracked`], or
203
+ /// [`Refusal::TimeRegression`] when `now` precedes the peer's last
204
+ /// heartbeat.
205
+ pub fn suspicion(&self, peer: u64, now: Duration) -> Result<Suspicion, Refusal> {
206
+ let now = instant(now)?;
207
+ let slot = self.find(peer).ok_or(Refusal::Untracked)?;
208
+ slot.window
209
+ .suspicion(&self.config, now)
210
+ .ok_or(Refusal::TimeRegression)
211
+ }
212
+
213
+ /// Writes peers whose phi has reached `threshold` at `now` into `out`,
214
+ /// examining slots from `cursor` (zero to start). When a suspect does not
215
+ /// fit, the scan stops and [`Scan::next`] names the slot to resume from.
216
+ ///
217
+ /// Peers still warming up have no phi and are never listed. Neither are
218
+ /// peers whose last heartbeat is later than `now`: [`Roster::suspicion`]
219
+ /// refuses them with [`Refusal::TimeRegression`], but one peer's later
220
+ /// heartbeat says nothing about the others, so the scan skips that peer
221
+ /// and goes on. Each call does at most `P * W` steps of work.
222
+ ///
223
+ /// # Errors
224
+ ///
225
+ /// [`Refusal::Overflow`] when `now` exceeds `u64` nanoseconds.
226
+ pub fn scan(
227
+ &self,
228
+ now: Duration,
229
+ threshold: Threshold,
230
+ cursor: usize,
231
+ out: &mut [Suspect],
232
+ ) -> Result<Scan, Refusal> {
233
+ let now = instant(now)?;
234
+ let mut suspects = 0;
235
+ for (index, slot) in self.slots.iter().enumerate().skip(cursor) {
236
+ let Some(slot) = slot else { continue };
237
+ let phi = match slot.window.suspicion(&self.config, now) {
238
+ Some(Suspicion::Measured(phi)) if phi.reaches(threshold) => phi,
239
+ // Below the threshold, or no phi yet (a tracked peer always
240
+ // has a heartbeat, so this is a warm-up).
241
+ Some(Suspicion::Measured(_) | Suspicion::WarmingUp { .. } | Suspicion::NoHeartbeat)
242
+ // The peer's last heartbeat is later than `now`: skipped,
243
+ // as documented above.
244
+ | None => continue,
245
+ };
246
+ let Some(entry) = out.get_mut(suspects) else {
247
+ return Ok(Scan {
248
+ suspects,
249
+ next: Some(index),
250
+ });
251
+ };
252
+ *entry = Suspect::new(slot.peer, slot.incarnation, phi);
253
+ suspects += 1;
254
+ }
255
+ Ok(Scan {
256
+ suspects,
257
+ next: None,
258
+ })
259
+ }
260
+
261
+ /// Statistics of `peer`'s stored intervals; `None` when untracked or
262
+ /// before its second heartbeat.
263
+ #[must_use]
264
+ pub fn statistics(&self, peer: u64) -> Option<Statistics> {
265
+ self.find(peer)?.window.statistics()
266
+ }
267
+
268
+ /// The incarnation tracked for `peer`.
269
+ #[must_use]
270
+ pub fn incarnation(&self, peer: u64) -> Option<u64> {
271
+ self.find(peer).map(|slot| slot.incarnation)
272
+ }
273
+
274
+ /// Time of `peer`'s last recorded heartbeat.
275
+ #[must_use]
276
+ pub fn last_heartbeat(&self, peer: u64) -> Option<Duration> {
277
+ self.find(peer)?.window.last().map(Duration::from_nanos)
278
+ }
279
+
280
+ /// Stops tracking `peer` and frees its slot. A later heartbeat from the
281
+ /// same peer starts from nothing, at any incarnation: the roster keeps no
282
+ /// tombstones.
283
+ ///
284
+ /// Never refuses. [`Forgetting::NotTracked`] is an idempotent repeat: the
285
+ /// peer was already absent and nothing changed.
286
+ #[must_use = "`NotTracked` means the peer was already absent and nothing changed"]
287
+ pub fn forget(&mut self, peer: u64) -> Forgetting {
288
+ self.entry(peer).map_or(Forgetting::NotTracked, |entry| {
289
+ *entry = None;
290
+ Forgetting::Forgotten
291
+ })
292
+ }
293
+
294
+ /// Peers currently tracked.
295
+ #[must_use]
296
+ pub fn len(&self) -> usize {
297
+ self.slots.iter().flatten().count()
298
+ }
299
+
300
+ /// Whether no peer is tracked.
301
+ #[must_use]
302
+ pub fn is_empty(&self) -> bool {
303
+ self.len() == 0
304
+ }
305
+
306
+ /// The policy shared by every peer.
307
+ #[must_use]
308
+ pub const fn config(&self) -> Config {
309
+ self.config
310
+ }
311
+ }
312
+
313
+ #[cfg(test)]
314
+ mod tests;
@@ -0,0 +1,189 @@
1
+ //! Helpers shared by the crate's test modules: a uniform draw on matryona's
2
+ //! deterministic generator, an independent phi reference and a naive detector model.
3
+ #![allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)]
4
+
5
+ use core::time::Duration;
6
+ use std::vec::Vec;
7
+
8
+ use crate::{Beat, Config, Detector, Distribution, Normal, Refusal, Roster, Suspicion, Threshold};
9
+
10
+ pub const SECOND: u64 = 1_000_000_000;
11
+ pub const MILLI: u64 = 1_000_000;
12
+
13
+ pub fn at(nanoseconds: u64) -> Duration {
14
+ Duration::from_nanos(nanoseconds)
15
+ }
16
+
17
+ pub fn normal(floor_ns: u64, min_samples: usize) -> Config {
18
+ let floor = Normal::new(at(floor_ns)).expect("a non-zero floor");
19
+ Config::new(Distribution::Normal(floor), min_samples).expect("a non-zero warm-up")
20
+ }
21
+
22
+ pub fn exponential(min_samples: usize) -> Config {
23
+ Config::new(Distribution::Exponential, min_samples).expect("a non-zero warm-up")
24
+ }
25
+
26
+ pub fn detector<const W: usize>(config: Config) -> Detector<W> {
27
+ Detector::new(config).expect("a config that fits the window")
28
+ }
29
+
30
+ pub fn roster<const P: usize, const W: usize>(config: Config) -> Roster<P, W> {
31
+ Roster::new(config).expect("a config that fits the window")
32
+ }
33
+
34
+ pub fn threshold(phi: f64) -> Threshold {
35
+ Threshold::new(phi).expect("a positive finite threshold")
36
+ }
37
+
38
+ pub fn measured(suspicion: Result<Suspicion, Refusal>) -> f64 {
39
+ let Ok(Suspicion::Measured(phi)) = suspicion else {
40
+ panic!("expected a phi, got {suspicion:?}");
41
+ };
42
+ phi.value()
43
+ }
44
+
45
+ /// `|actual - expected|` within `absolute` or within `relative * |expected|`.
46
+ pub fn within(actual: f64, expected: f64, absolute: f64, relative: f64) -> bool {
47
+ let error = (actual - expected).abs();
48
+ error <= absolute || error <= relative * expected.abs()
49
+ }
50
+
51
+ /// [`within`] with one tolerance, absolute and relative.
52
+ pub fn close(actual: f64, expected: f64, tolerance: f64) -> bool {
53
+ within(actual, expected, tolerance, tolerance)
54
+ }
55
+
56
+ pub use matryona_machines::testing::XorShift64;
57
+
58
+ /// A uniform draw in `[0, 1)` for the randomized tests.
59
+ pub trait Unit {
60
+ fn unit(&mut self) -> f64;
61
+ }
62
+
63
+ impl Unit for XorShift64 {
64
+ fn unit(&mut self) -> f64 {
65
+ (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
66
+ }
67
+ }
68
+
69
+ /// `-log10(Q(z))` by Simpson quadrature of the Gaussian tail with `std`
70
+ /// math: `Q(a) = pdf(a) * integral over u >= 0 of exp(-a u - u^2 / 2)`.
71
+ /// Shares no code and no method with `math::normal_phi`.
72
+ pub fn reference_normal_phi(z: f64) -> f64 {
73
+ const STEPS: u32 = 8_000;
74
+ let a = z.abs();
75
+ let upper = if a > 1.0 { 45.0 / a } else { 45.0 };
76
+ let width = upper / f64::from(STEPS);
77
+ let integrand = |u: f64| (-a * u - 0.5 * u * u).exp();
78
+ let mut total = integrand(0.0) + integrand(upper);
79
+ for step in 1..STEPS {
80
+ let weight = if step % 2 == 1 { 4.0 } else { 2.0 };
81
+ total += weight * integrand(f64::from(step) * width);
82
+ }
83
+ let integral = total * width / 3.0;
84
+ let ln_tail = -0.5 * a * a - 0.5 * (2.0 * std::f64::consts::PI).ln() + integral.ln();
85
+ let ln_survival = if z >= 0.0 {
86
+ ln_tail
87
+ } else {
88
+ (-ln_tail.exp()).ln_1p()
89
+ };
90
+ (-ln_survival / std::f64::consts::LN_10).max(0.0)
91
+ }
92
+
93
+ /// Naive reference: every interval kept in a growable list, statistics
94
+ /// recomputed in arrival order with `std` math, phi from quadrature.
95
+ #[derive(Clone, Default)]
96
+ pub struct Model {
97
+ pub intervals: Vec<u64>,
98
+ pub last: Option<u64>,
99
+ }
100
+
101
+ /// What the naive model expects a suspicion read to produce.
102
+ #[derive(Debug)]
103
+ pub enum Expected {
104
+ Refused,
105
+ Undefined,
106
+ Phi(f64),
107
+ }
108
+
109
+ impl Model {
110
+ pub fn observe(&mut self, now: u64, window: usize) -> Result<Beat, Refusal> {
111
+ let Some(last) = self.last else {
112
+ self.last = Some(now);
113
+ return Ok(Beat::First);
114
+ };
115
+ if now < last {
116
+ return Err(Refusal::TimeRegression);
117
+ }
118
+ if now == last {
119
+ return Err(Refusal::SameInstant);
120
+ }
121
+ self.intervals.push(now - last);
122
+ if self.intervals.len() > window {
123
+ self.intervals.remove(0);
124
+ }
125
+ self.last = Some(now);
126
+ Ok(Beat::Recorded {
127
+ interval: at(now - last),
128
+ samples: self.intervals.len(),
129
+ })
130
+ }
131
+
132
+ pub fn mean(&self) -> f64 {
133
+ self.intervals.iter().map(|&x| x as f64).sum::<f64>() / self.intervals.len() as f64
134
+ }
135
+
136
+ pub fn std_dev(&self) -> f64 {
137
+ let mean = self.mean();
138
+ let squares: f64 = self
139
+ .intervals
140
+ .iter()
141
+ .map(|&x| (x as f64 - mean).powi(2))
142
+ .sum();
143
+ (squares / self.intervals.len() as f64).sqrt()
144
+ }
145
+
146
+ pub fn phi(&self, config: &Config, now: u64) -> Expected {
147
+ let Some(last) = self.last else {
148
+ return Expected::Undefined;
149
+ };
150
+ if now < last {
151
+ return Expected::Refused;
152
+ }
153
+ if self.intervals.len() < config.min_samples() {
154
+ return Expected::Undefined;
155
+ }
156
+ let silence = (now - last) as f64;
157
+ let pause = config.acceptable_pause().as_nanos() as f64;
158
+ Expected::Phi(match config.distribution() {
159
+ Distribution::Normal(floor) => {
160
+ let deviation = self.std_dev().max(floor.min_std_dev().as_nanos() as f64);
161
+ reference_normal_phi((silence - self.mean() - pause) / deviation)
162
+ }
163
+ Distribution::Exponential => {
164
+ (((silence - pause) / self.mean()) * std::f64::consts::LOG10_E).max(0.0)
165
+ }
166
+ })
167
+ }
168
+ }
169
+
170
+ pub fn random_config(rng: &mut XorShift64, window: usize) -> Config {
171
+ let min_samples = 1 + rng.below(window as u64) as usize;
172
+ let config = if rng.below(2) == 0 {
173
+ normal(1_000 + rng.below(50 * MILLI), min_samples)
174
+ } else {
175
+ exponential(min_samples)
176
+ };
177
+ config
178
+ .with_acceptable_pause(at(rng.below(3) * rng.below(200 * MILLI)))
179
+ .expect("a pause that fits u64 nanoseconds")
180
+ }
181
+
182
+ /// A step that is usually a plausible heartbeat gap, sometimes zero.
183
+ pub fn random_gap(rng: &mut XorShift64) -> u64 {
184
+ match rng.below(10) {
185
+ 0 => 0,
186
+ 1 => 1 + rng.below(30 * SECOND),
187
+ _ => 900 * MILLI + rng.below(200 * MILLI),
188
+ }
189
+ }
@@ -0,0 +1,159 @@
1
+ //! Sliding window of heartbeat intervals for one peer.
2
+
3
+ use core::time::Duration;
4
+
5
+ use crate::{Beat, Config, Refusal, Statistics, Suspicion, duration_to_f64, half_range, math};
6
+
7
+ /// Nanoseconds as `f64`. Exact up to `2^53` ns (about 104 days); beyond that
8
+ /// the nearest representable value is used.
9
+ #[allow(
10
+ clippy::cast_precision_loss,
11
+ reason = "documented rounding above 2^53 ns"
12
+ )]
13
+ pub const fn to_f64(value: u64) -> f64 {
14
+ value as f64
15
+ }
16
+
17
+ #[allow(
18
+ clippy::cast_precision_loss,
19
+ reason = "a sum of u64 intervals rounds to the nearest f64"
20
+ )]
21
+ const fn wide_to_f64(value: u128) -> f64 {
22
+ value as f64
23
+ }
24
+
25
+ #[allow(
26
+ clippy::cast_precision_loss,
27
+ reason = "sample counts are far below 2^53"
28
+ )]
29
+ const fn count_to_f64(value: usize) -> f64 {
30
+ value as f64
31
+ }
32
+
33
+ /// The last `W` intervals, their exact sum and the last heartbeat time.
34
+ #[derive(Debug, Clone, PartialEq, Eq)]
35
+ pub struct Window<const W: usize> {
36
+ intervals: [u64; W],
37
+ next: usize,
38
+ len: usize,
39
+ sum: u128,
40
+ last: Option<u64>,
41
+ }
42
+
43
+ impl<const W: usize> Window<W> {
44
+ pub const fn new() -> Self {
45
+ Self {
46
+ intervals: [0; W],
47
+ next: 0,
48
+ len: 0,
49
+ sum: 0,
50
+ last: None,
51
+ }
52
+ }
53
+
54
+ /// A window whose first heartbeat was at `now`: what [`Window::observe`]
55
+ /// makes of an empty window, without a refusal arm it could never take.
56
+ pub const fn started(now: u64) -> Self {
57
+ let mut window = Self::new();
58
+ window.last = Some(now);
59
+ window
60
+ }
61
+
62
+ pub const fn len(&self) -> usize {
63
+ self.len
64
+ }
65
+
66
+ pub const fn last(&self) -> Option<u64> {
67
+ self.last
68
+ }
69
+
70
+ pub const fn reset(&mut self) {
71
+ *self = Self::new();
72
+ }
73
+
74
+ /// Records a heartbeat at `now` nanoseconds. Refusals leave the window
75
+ /// untouched.
76
+ pub fn observe(&mut self, now: u64) -> Result<Beat, Refusal> {
77
+ let Some(last) = self.last else {
78
+ self.last = Some(now);
79
+ return Ok(Beat::First);
80
+ };
81
+ if now < last {
82
+ return Err(Refusal::TimeRegression);
83
+ }
84
+ if now == last {
85
+ return Err(Refusal::SameInstant);
86
+ }
87
+ let interval = now - last;
88
+ // `next < W` whenever `W > 0`, so the slot is missing only for a
89
+ // window of no slots, where an interval enters and leaves at once.
90
+ // No public type builds one: `Config::fit` refuses `W = 0`.
91
+ if let Some(slot) = self.intervals.get_mut(self.next) {
92
+ if self.len == W {
93
+ self.sum -= u128::from(*slot);
94
+ } else {
95
+ self.len += 1;
96
+ }
97
+ *slot = interval;
98
+ // At most W u64 intervals, each subtracted when it leaves: the u128
99
+ // sum cannot wrap, and `% W` runs only when W > 0.
100
+ self.sum += u128::from(interval);
101
+ self.next = (self.next + 1) % W;
102
+ }
103
+ self.last = Some(now);
104
+ Ok(Beat::Recorded {
105
+ interval: Duration::from_nanos(interval),
106
+ samples: self.len,
107
+ })
108
+ }
109
+
110
+ /// Statistics of the stored intervals; `None` while there are none.
111
+ pub fn statistics(&self) -> Option<Statistics> {
112
+ let stored = self.intervals.iter().take(self.len);
113
+ let &first = stored.clone().next()?;
114
+ let (mut min, mut max) = (first, first);
115
+ for &interval in stored.clone() {
116
+ min = min.min(interval);
117
+ max = max.max(interval);
118
+ }
119
+ let (min, max) = (Duration::from_nanos(min), Duration::from_nanos(max));
120
+ let (low, high) = (duration_to_f64(min), duration_to_f64(max));
121
+ let count = count_to_f64(self.len);
122
+ // The exact mean lies within min..=max and the exact deviation within
123
+ // half the range; rounding may step past either bound, so clamp back
124
+ // to what the intervals can give.
125
+ let mean_ns = (wide_to_f64(self.sum) / count).max(low).min(high);
126
+ let mut squares = 0.0;
127
+ for &interval in stored {
128
+ let deviation = to_f64(interval) - mean_ns;
129
+ squares += deviation * deviation;
130
+ }
131
+ Some(Statistics {
132
+ samples: self.len,
133
+ mean_ns,
134
+ std_dev_ns: math::sqrt(squares / count).min(half_range(low, high)),
135
+ min,
136
+ max,
137
+ })
138
+ }
139
+
140
+ /// The suspicion at `now` nanoseconds under `config`; `None` when `now`
141
+ /// precedes the last heartbeat, so there is no silence to measure. The
142
+ /// callers answer that as [`Refusal::TimeRegression`] or skip the peer.
143
+ pub fn suspicion(&self, config: &Config, now: u64) -> Option<Suspicion> {
144
+ let Some(last) = self.last else {
145
+ return Some(Suspicion::NoHeartbeat);
146
+ };
147
+ let silence = now.checked_sub(last)?;
148
+ Some(match self.statistics() {
149
+ Some(statistics) if self.len >= config.min_samples() => {
150
+ Suspicion::Measured(config.phi(&statistics, silence))
151
+ }
152
+ Some(_) | None => Suspicion::WarmingUp {
153
+ samples: self.len,
154
+ required: config.min_samples(),
155
+ silence: Duration::from_nanos(silence),
156
+ },
157
+ })
158
+ }
159
+ }
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'ffi/extconf'
@@ -0,0 +1,21 @@
1
+ [package]
2
+ name = "suspect_machines_native"
3
+ version = "0.1.5"
4
+ edition.workspace = true
5
+ rust-version.workspace = true
6
+ license.workspace = true
7
+ publish = false
8
+
9
+ [lib]
10
+ crate-type = ["cdylib"]
11
+ test = false
12
+ doctest = false
13
+
14
+ [dependencies]
15
+ suspect-machines.workspace = true
16
+ magnus.workspace = true
17
+ matryona-machines = { workspace = true, features = ["ruby"] }
18
+ rb-sys.workspace = true
19
+
20
+ [lints]
21
+ workspace = true
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require 'mkmf'
3
+ require 'pathname'
4
+ require 'rb_sys/mkmf'
5
+
6
+ create_rust_makefile('suspect_machines_native/suspect_machines_native') do |e|
7
+ e.ext_dir = Pathname(__dir__).relative_path_from(Pathname(Dir.pwd)).to_s
8
+ e.profile = ENV.fetch('RB_SYS_CARGO_PROFILE', :release).to_sym
9
+ end
10
+ makefile = File.join(Dir.pwd, 'Makefile')
11
+ File.write(makefile, File.read(makefile).gsub(
12
+ /^RB_SYS_CARGO_MANIFEST_DIR \?=.*$/,
13
+ "RB_SYS_CARGO_MANIFEST_DIR ?= #{File.expand_path(__dir__)}"
14
+ ))