fluxframe-cli 0.12.0

fluxframe CLI binary: list, check, run, benchmark.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Periodic metrics reporter (Stage 5.D.3).
//!
//! A dedicated thread polls the supervisor's [`RuntimeMetrics`] every
//! `interval` seconds and emits a single `info!` line in the §29
//! format (`fps=… dropped=… latency_p50=…ms inference_p50=…ms …`).
//!
//! Lifecycle:
//! * [`MetricsReporter::spawn`] starts the thread.
//! * The thread exits when EITHER the caller-provided `running` flag
//!   flips to `false` (typical Ctrl-C / EOS path) OR the
//!   [`MetricsReporter`] itself is dropped (the supervisor explicitly
//!   tearing the run down).  Both paths join cleanly.
//!
//! The reporter never observes the histograms while holding any
//! supervisor-owned lock — it goes through the same poison-tolerant
//! [`fluxframe_core::LatencyHistogram::snapshot`] path as the
//! teardown summary, so a panic in the supervisor cannot deadlock or
//! crash the reporter.

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use fluxframe_core::{CONSUMER_EVENT_AGE_UNSET, CounterValues, PeriodicExtras, emit_metrics_line};

use crate::runtime_metrics::RuntimeMetrics;
use crate::thread_cpu::ThreadCpuSampler;

/// Granularity of the reporter's shutdown poll.  Smaller is more
/// responsive to Ctrl-C, larger spends less CPU.  Sized to match
/// `crate::runtime::WORKER_POLL_TIMEOUT` so the reporter's
/// teardown latency budget cannot exceed the supervisor's — keep
/// the two in sync if either is retuned.
const POLL_INTERVAL: Duration = Duration::from_millis(50);

/// How long a run of identical ticks may stay silent before one is
/// emitted anyway.
///
/// Suppression exists because an idle daemon otherwise writes the same
/// forty-field line every `interval` forever — that is what turned the
/// operator's log into hundreds of megabytes in which the handful of
/// meaningful lines are unfindable. The heartbeat is the other half of
/// the bargain: "nothing is happening" must still be *visible*, or a
/// dead reporter looks exactly like a quiet one.
const QUIET_HEARTBEAT: Duration = Duration::from_secs(600);

/// Silence threshold for the client-usage gauge: the tick that carries
/// the gauge *across* this value is always emitted.
///
/// The gauge grows every tick by construction, so it cannot take part in
/// the equality test — but crossing this threshold is the one thing an
/// idle daemon can report that is genuinely alarming (the presence
/// subscription has gone deaf), and hiding it behind a ten-minute
/// heartbeat would make the next incident harder to read than the last.
///
/// Strictly an *edge*: a level test would hold forever, because a node
/// nobody uses is legitimately silent for hours — and suppression, the
/// whole point of this module, would switch itself off five minutes
/// into every idle period.
const EVENT_SILENCE_ALERT: Duration = Duration::from_secs(300);

/// Whether a tick is worth a log line.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TickVerdict {
    Emit,
    Suppress,
}

/// What changed since the previously *emitted* tick.
///
/// A struct rather than four positional arguments: two `u64`s and two
/// `Duration`s in a row are a transposition waiting to happen, and the
/// cost of getting it wrong here is suppressing exactly the ticks this
/// function exists to surface.
struct TickInput<'a> {
    prev: &'a CounterValues,
    cur: &'a CounterValues,
    frames_out_delta: u64,
    dropped_delta: u64,
    since_last_emit: Duration,
    heartbeat: Duration,
}

/// Decide whether this tick says anything the previous one did not.
///
/// Pure so it can be tested without threads, a clock or a log capture:
/// the reporter's own tests deliberately do not assert on `tracing`
/// output, so logic left inside the loop would have shipped untested.
///
/// Three counters are excluded from the comparison because they advance
/// on their own schedule and would make every tick look eventful: the
/// idle placeholder frame count, the resync tick count, and the age of
/// the last client-usage event.
fn tick_verdict(input: &TickInput<'_>) -> TickVerdict {
    let TickInput {
        prev,
        cur,
        frames_out_delta,
        dropped_delta,
        since_last_emit,
        heartbeat,
    } = *input;

    if frames_out_delta != 0 || dropped_delta != 0 {
        return TickVerdict::Emit;
    }
    if since_last_emit >= heartbeat {
        return TickVerdict::Emit;
    }
    if crossed_silence_threshold(prev, cur) {
        return TickVerdict::Emit;
    }
    let mut baseline = *prev;
    baseline.idle_frames_pushed_total = cur.idle_frames_pushed_total;
    baseline.consumer_resync_total = cur.consumer_resync_total;
    baseline.consumer_last_external_event_age_secs = cur.consumer_last_external_event_age_secs;
    if baseline == *cur {
        TickVerdict::Suppress
    } else {
        TickVerdict::Emit
    }
}

/// Did the event-silence gauge cross [`EVENT_SILENCE_ALERT`] between the
/// last emitted tick and this one?
///
/// The sentinel is not a duration and must not be compared as one: read
/// as seconds it is larger than any threshold, so treating it as a level
/// would emit on every tick of a run that has seen no events at all.
fn crossed_silence_threshold(prev: &CounterValues, cur: &CounterValues) -> bool {
    let measured = |v: u64| (v != CONSUMER_EVENT_AGE_UNSET).then_some(v);
    let threshold = EVENT_SILENCE_ALERT.as_secs();
    let now_over =
        measured(cur.consumer_last_external_event_age_secs).is_some_and(|v| v >= threshold);
    let was_over =
        measured(prev.consumer_last_external_event_age_secs).is_some_and(|v| v >= threshold);
    now_over && !was_over
}

/// Handle to the reporter thread.  Dropping joins the thread; a panic
/// in the worker surfaces via the join result and is logged.
pub(crate) struct MetricsReporter {
    handle: Option<JoinHandle<()>>,
    stopped: Arc<AtomicBool>,
}

impl MetricsReporter {
    /// Spawn the reporter thread.  Returns the handle so the caller
    /// can drop it (and thereby join) at run teardown.
    ///
    /// # Errors
    ///
    /// Propagates [`std::io::Error`] from [`std::thread::Builder::spawn`].
    pub(crate) fn spawn(
        metrics: RuntimeMetrics,
        interval: Duration,
        running: Arc<AtomicBool>,
    ) -> std::io::Result<Self> {
        let stopped = Arc::new(AtomicBool::new(false));
        let stopped_for_worker = Arc::clone(&stopped);
        let handle = std::thread::Builder::new()
            .name("fluxframe-metrics".into())
            .spawn(move || reporter_loop(&metrics, interval, &running, &stopped_for_worker))?;
        Ok(Self {
            handle: Some(handle),
            stopped,
        })
    }
}

impl Drop for MetricsReporter {
    fn drop(&mut self) {
        self.stopped.store(true, Ordering::Release);
        if let Some(h) = self.handle.take() {
            if let Err(e) = h.join() {
                tracing::warn!(?e, "metrics reporter thread panicked while joining");
            }
        }
    }
}

fn reporter_loop(
    metrics: &RuntimeMetrics,
    interval: Duration,
    running: &AtomicBool,
    stopped: &AtomicBool,
) {
    let mut last_tick = Instant::now();
    let mut last_frames_out: u64 = 0;
    let mut last_frames_dropped: u64 = 0;
    let mut last_emit = Instant::now();
    let mut last_emitted: Option<CounterValues> = None;
    // Per-thread CPU accounting; `None` off Linux, in which case the
    // `cpu shares` line is simply never emitted.
    let mut cpu_sampler = ThreadCpuSampler::new();
    while running.load(Ordering::Acquire) && !stopped.load(Ordering::Acquire) {
        // Sleep no longer than the time remaining until the next
        // scheduled tick, but cap at POLL_INTERVAL so shutdown stays
        // responsive even when `interval` is many seconds.  Without
        // this cap we would either burn 20 wake-ups/s for nothing
        // (constant POLL sleep) or hold shutdown for the full
        // `interval` (constant interval sleep).
        let pre_sleep = Instant::now();
        let until_next = interval.saturating_sub(pre_sleep.duration_since(last_tick));
        let sleep_for = std::cmp::min(POLL_INTERVAL, until_next);
        // `sleep_for == ZERO` is legal — the OS yields and we loop
        // immediately into the tick branch.
        std::thread::sleep(sleep_for);
        // Estimate "now" from the pre-sleep instant + the requested
        // sleep; saves one `clock_gettime` per iteration.  Accuracy
        // loss is bounded by the kernel's sleep slack (microseconds)
        // and irrelevant against the seconds-scale `interval`.
        let now = pre_sleep + sleep_for;
        let dt = now.duration_since(last_tick);
        if dt < interval {
            continue;
        }
        last_tick = now;

        // Sampled every tick, like the frame deltas, so a suppressed
        // tick does not fold its CPU time into the next window.
        let cpu = cpu_sampler.as_mut().and_then(ThreadCpuSampler::sample);
        let snap = metrics.snapshot().with_cpu(cpu);
        let frames_now = snap.counters.frames_out;
        let frames_delta = frames_now.saturating_sub(last_frames_out);
        last_frames_out = frames_now;
        let dropped_now = snap.counters.frames_dropped;
        let dropped_delta = dropped_now.saturating_sub(last_frames_dropped);
        last_frames_dropped = dropped_now;

        // The frame deltas are consumed by the verdict, so they are
        // measured every tick even when the line is suppressed —
        // otherwise a suppressed tick would fold its frames into the
        // next emitted window and misreport fps.
        let verdict = last_emitted.as_ref().map_or(TickVerdict::Emit, |prev| {
            tick_verdict(&TickInput {
                prev,
                cur: &snap.counters,
                frames_out_delta: frames_delta,
                dropped_delta,
                since_last_emit: now.duration_since(last_emit),
                heartbeat: QUIET_HEARTBEAT,
            })
        });
        if verdict == TickVerdict::Suppress {
            continue;
        }
        last_emit = now;
        last_emitted = Some(snap.counters);

        emit_metrics_line(
            &snap,
            Some(PeriodicExtras {
                frames_out_delta: frames_delta,
                dropped_delta,
                window: dt,
            }),
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A steady-state snapshot: a run that has published a verdict and
    /// is sitting idle.
    fn quiet() -> CounterValues {
        let mut c = CounterValues::default();
        c.frames_in = 100;
        c.frames_out = 100;
        c.consumer_status = 0;
        c
    }

    /// A tick with no frames and well inside the heartbeat window: the
    /// case where suppression is allowed to make a decision.
    fn verdict(prev: &CounterValues, cur: &CounterValues) -> TickVerdict {
        tick_verdict(&TickInput {
            prev,
            cur,
            frames_out_delta: 0,
            dropped_delta: 0,
            since_last_emit: Duration::from_secs(30),
            heartbeat: Duration::from_secs(600),
        })
    }

    #[test]
    fn an_identical_tick_is_suppressed() {
        assert_eq!(verdict(&quiet(), &quiet()), TickVerdict::Suppress);
    }

    #[test]
    fn the_three_self_advancing_counters_do_not_make_a_tick_eventful() {
        // Placeholder frames, resync ticks and the event-age gauge move
        // on their own every interval; if they counted, nothing would
        // ever be suppressed and the file would grow exactly as before.
        let mut cur = quiet();
        cur.idle_frames_pushed_total += 300;
        cur.consumer_resync_total += 1;
        cur.consumer_last_external_event_age_secs = 42;
        assert_eq!(verdict(&quiet(), &cur), TickVerdict::Suppress);
    }

    #[test]
    fn a_changed_consumer_verdict_is_always_emitted() {
        // The single line that says the camera woke up must never be
        // the one that gets swallowed.
        let mut cur = quiet();
        cur.consumer_status = 1;
        cur.consumer_transitions_total += 1;
        assert_eq!(verdict(&quiet(), &cur), TickVerdict::Emit);
    }

    #[test]
    fn a_resync_correction_is_always_emitted() {
        let mut cur = quiet();
        cur.consumer_resync_corrections_total += 1;
        assert_eq!(verdict(&quiet(), &cur), TickVerdict::Emit);
    }

    #[test]
    fn frames_flowing_is_always_eventful() {
        assert_eq!(
            tick_verdict(&TickInput {
                prev: &quiet(),
                cur: &quiet(),
                frames_out_delta: 750,
                dropped_delta: 0,
                since_last_emit: Duration::from_secs(30),
                heartbeat: Duration::from_secs(600),
            }),
            TickVerdict::Emit
        );
    }

    #[test]
    fn the_heartbeat_breaks_a_long_silence() {
        assert_eq!(
            tick_verdict(&TickInput {
                prev: &quiet(),
                cur: &quiet(),
                frames_out_delta: 0,
                dropped_delta: 0,
                since_last_emit: Duration::from_secs(601),
                heartbeat: Duration::from_secs(600),
            }),
            TickVerdict::Emit
        );
    }

    #[test]
    fn crossing_the_event_silence_threshold_defeats_suppression() {
        // A deaf presence subscription is the one thing an otherwise
        // idle daemon can report that is worth reading promptly.
        let mut prev = quiet();
        prev.consumer_last_external_event_age_secs = EVENT_SILENCE_ALERT.as_secs() - 1;
        let mut cur = quiet();
        cur.consumer_last_external_event_age_secs = EVENT_SILENCE_ALERT.as_secs();
        assert_eq!(verdict(&prev, &cur), TickVerdict::Emit);
    }

    #[test]
    fn staying_above_the_silence_threshold_is_suppressed_again() {
        // The regression this guards: as a *level* test, the threshold
        // switched suppression off permanently five minutes into every
        // idle period — a node nobody uses is legitimately silent for
        // hours, and the log went back to a line every 30 s.
        let mut prev = quiet();
        prev.consumer_last_external_event_age_secs = EVENT_SILENCE_ALERT.as_secs();
        let mut cur = quiet();
        cur.consumer_last_external_event_age_secs = EVENT_SILENCE_ALERT.as_secs() * 4;
        assert_eq!(verdict(&prev, &cur), TickVerdict::Suppress);
    }

    #[test]
    fn a_reattached_consumer_rearms_the_silence_edge() {
        // Age resets when an event arrives, so the next slide into
        // silence must be reported again rather than swallowed as
        // "already known".
        let mut prev = quiet();
        prev.consumer_last_external_event_age_secs = 3;
        let mut cur = quiet();
        cur.consumer_last_external_event_age_secs = EVENT_SILENCE_ALERT.as_secs() + 30;
        assert_eq!(verdict(&prev, &cur), TickVerdict::Emit);
    }

    #[test]
    fn an_unmeasured_event_age_does_not_trip_the_silence_alert() {
        // The sentinel is `u64::MAX`; read as seconds it would trip the
        // threshold on every single tick and suppress nothing.
        let mut cur = quiet();
        cur.consumer_last_external_event_age_secs = CONSUMER_EVENT_AGE_UNSET;
        assert_eq!(verdict(&quiet(), &cur), TickVerdict::Suppress);
    }

    #[test]
    fn reporter_joins_when_running_flag_drops() {
        let running = Arc::new(AtomicBool::new(true));
        let metrics = RuntimeMetrics::new();
        let reporter =
            MetricsReporter::spawn(metrics, Duration::from_millis(40), Arc::clone(&running))
                .expect("spawn");
        // Give the worker a chance to wake at least once.
        std::thread::sleep(Duration::from_millis(120));
        running.store(false, Ordering::Release);
        // Drop joins the thread; if it does not exit promptly the
        // test will hang and CI catches it.
        drop(reporter);
    }

    #[test]
    fn reporter_joins_when_dropped_even_if_running_stays_true() {
        let running = Arc::new(AtomicBool::new(true));
        let metrics = RuntimeMetrics::new();
        let reporter =
            MetricsReporter::spawn(metrics, Duration::from_millis(40), Arc::clone(&running))
                .expect("spawn");
        // Do NOT flip `running`; the Drop-induced `stopped` flag
        // must be sufficient on its own.
        drop(reporter);
        // `running` still observable here — the supervisor would
        // typically flip it on its own teardown path.
        assert!(running.load(Ordering::Acquire));
    }

    #[test]
    fn reporter_emits_at_least_once_when_window_elapses() {
        // Smoke test: exercise the snapshot + emit path inside the
        // loop without asserting on log output (capturing tracing
        // from a unit test is more trouble than it is worth here —
        // we just confirm the loop runs through `emit` without
        // panicking on an empty snapshot).
        let running = Arc::new(AtomicBool::new(true));
        let metrics = RuntimeMetrics::new();
        // Populate a little so the snapshot has non-zero content.
        metrics.counters.inc_frames_in();
        metrics.counters.inc_frames_out();
        metrics.processing.record_us(1000);
        let reporter =
            MetricsReporter::spawn(metrics, Duration::from_millis(40), Arc::clone(&running))
                .expect("spawn");
        std::thread::sleep(Duration::from_millis(150));
        running.store(false, Ordering::Release);
        drop(reporter);
    }
}