agentmux_srv_core/backend/sweep_stats.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What the orphan reaper (`sagas/orphan_reaper.rs`) found on its last sweep, for the system report
5//! (`sysinfo.rs`). They live in the core so the report does not reach up into `sagas/`; the reaper writes
6//! them and re-exports the reader under its old path.
7
8use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
9
10/// Diagnostic-only counters updated at the end of every `sweep()` — for the
11/// periodic `mem_attribution` log (`sysinfo.rs`) to correlate commit/handle
12/// growth against reaper activity. A `reaper_candidates`/`reaper_first_seen`
13/// that climbs sweep over sweep without a matching rise in
14/// `reaper_reaped_cumulative` would mean the reaper keeps finding (but not
15/// managing to close) the same growing set of blocks — worth seeing
16/// directly rather than inferred from commit numbers alone.
17static LAST_CANDIDATES: AtomicUsize = AtomicUsize::new(0);
18static LAST_FIRST_SEEN_LEN: AtomicUsize = AtomicUsize::new(0);
19static REAPED_CUMULATIVE: AtomicU64 = AtomicU64::new(0);
20
21/// `(candidates found this sweep, first_seen entries carried across sweeps,
22/// cumulative blocks reaped since process start)`.
23#[cfg(any(windows, test, feature = "test-support"))]
24pub fn last_sweep_stats() -> (usize, usize, u64) {
25 (
26 LAST_CANDIDATES.load(Ordering::Relaxed),
27 LAST_FIRST_SEEN_LEN.load(Ordering::Relaxed),
28 REAPED_CUMULATIVE.load(Ordering::Relaxed),
29 )
30}
31
32/// One sweep's result: the candidates it found, the `first_seen` entries it carries into the next sweep, and
33/// how many blocks it reaped (added to the running total).
34pub fn record(candidates: usize, first_seen: usize, reaped: usize) {
35 LAST_CANDIDATES.store(candidates, Ordering::Relaxed);
36 LAST_FIRST_SEEN_LEN.store(first_seen, Ordering::Relaxed);
37 if reaped > 0 {
38 REAPED_CUMULATIVE.fetch_add(reaped as u64, Ordering::Relaxed);
39 }
40}