zc2 0.0.50

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Multi-thread throughput measurement shared by the CPU (GEMM) and memory
//! (triad) probes.
//!
//! KNOWN BIAS (accepted): at the end of a window the fast threads sit idle in
//! `barrier.wait()` while the slowest one finishes its last unit, yet the span
//! used for the rate runs from the earliest start to the LATEST end. On hybrid
//! CPUs (P + E cores) that reads about 10-15 % low. It is stable from run to
//! run and the score only needs a stable ordering, so this is a calibration
//! note, not something to fix.
use super::budget::Budget;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Barrier, Mutex};
use std::time::{Duration, Instant};

/// One worker's contribution to a window: units done, first and last instant.
pub(crate) type Sample = (f64, Instant, Instant);

#[derive(Debug, PartialEq)]
pub(crate) struct RateOutcome {
    /// Best (max over rounds) aggregate units per second, if any round yielded one.
    pub rate: Option<f64>,
    /// Workers that panicked (in setup or in `work`). Each counts as zero
    /// units for the rounds it missed; the caller must mark the result partial.
    pub panicked: usize,
}

/// Units per second over the span from the earliest start to the latest end.
pub(crate) fn aggregate(samples: &[Sample]) -> Option<f64> {
    let start = samples.iter().map(|s| s.1).min()?;
    let end = samples.iter().map(|s| s.2).max()?;
    let units: f64 = samples.iter().map(|s| s.0).sum();
    let secs = end.duration_since(start).as_secs_f64();
    (secs > 0.0 && units > 0.0).then(|| units / secs)
}

/// Aggregate throughput of `threads` workers over common wall-clock windows.
///
/// Every worker builds its own state (so memory is first-touched on its own
/// NUMA node), waits on a barrier, then loops `work` (which returns the units
/// it completed) until a window of `window` has elapsed. Units/second is the
/// sum of all units over the span from the earliest start to the latest end,
/// so a slow core just contributes less and nobody waits for it. Rounds
/// repeat until `b` expires (at least one) and the MAX rate is returned,
/// since interference only ever slows a round down.
///
/// A worker that panics is caught: it still reaches every barrier (so the
/// others never deadlock), contributes zero units and is counted in
/// `RateOutcome::panicked`.
pub(crate) fn max_rate<S>(
    b: &Budget,
    threads: usize,
    window: Duration,
    setup: impl Fn(usize) -> S + Sync,
    work: impl Fn(&mut S) -> f64 + Sync,
) -> RateOutcome {
    use std::panic::{catch_unwind, AssertUnwindSafe};
    let barrier = Barrier::new(threads + 1);
    let stop = AtomicBool::new(false);
    let panicked = AtomicUsize::new(0);
    let slots: Mutex<Vec<Option<Sample>>> = Mutex::new(vec![None; threads]);
    let mut best: Option<f64> = None;
    std::thread::scope(|s| {
        for i in 0..threads {
            let (barrier, stop, slots, setup, work, panicked) =
                (&barrier, &stop, &slots, &setup, &work, &panicked);
            s.spawn(move || {
                let mut state = match catch_unwind(AssertUnwindSafe(|| setup(i))) {
                    Ok(s) => Some(s),
                    Err(_) => {
                        panicked.fetch_add(1, Ordering::SeqCst);
                        None
                    }
                };
                loop {
                    barrier.wait();
                    if stop.load(Ordering::SeqCst) {
                        break;
                    }
                    let t0 = Instant::now();
                    let mut units = 0.0;
                    if let Some(st) = state.as_mut() {
                        let r = catch_unwind(AssertUnwindSafe(|| {
                            let mut u = 0.0;
                            loop {
                                u += work(st);
                                if t0.elapsed() >= window {
                                    return u;
                                }
                            }
                        }));
                        match r {
                            Ok(u) => units = u,
                            Err(_) => {
                                panicked.fetch_add(1, Ordering::SeqCst);
                                state = None;
                            }
                        }
                    } else {
                        // Dead worker: idle out the window so the span stays honest.
                        std::thread::sleep(window);
                    }
                    let end = Instant::now();
                    if let Ok(mut g) = slots.lock() {
                        g[i] = Some((units, t0, end));
                    }
                    barrier.wait();
                }
            });
        }
        let mut rounds = 0;
        loop {
            let done = b.cancelled() || (rounds > 0 && b.expired());
            stop.store(done, Ordering::SeqCst);
            barrier.wait();
            if done {
                break;
            }
            barrier.wait();
            rounds += 1;
            let g = slots.lock().map(|g| g.clone()).unwrap_or_default();
            let got: Vec<_> = g.into_iter().flatten().collect();
            if got.len() != threads {
                continue;
            }
            if let Some(rate) = aggregate(&got) {
                best = Some(best.map_or(rate, |x: f64| x.max(rate)));
            }
        }
    });
    RateOutcome {
        rate: best,
        panicked: panicked.load(Ordering::SeqCst),
    }
}

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

    fn budget(ms: u64) -> Budget {
        Budget::new(Duration::from_millis(ms), Arc::new(AtomicBool::new(false)))
    }

    #[test]
    fn aggregate_sums_units_over_the_common_span() {
        let t = Instant::now();
        let ms = Duration::from_millis;
        let samples = [
            (1000.0, t, t + ms(100)),
            (1000.0, t + ms(10), t + ms(110)),
            (1000.0, t, t + ms(100)),
        ];
        // 3000 units over t..t+110ms.
        let r = aggregate(&samples).unwrap();
        assert!((r - 3000.0 / 0.110).abs() < 1e-6, "{r}");
        // No units or no span: nothing to report.
        assert_eq!(aggregate(&[(0.0, t, t + ms(5))]), None);
        assert_eq!(aggregate(&[(5.0, t, t)]), None);
        assert_eq!(aggregate(&[]), None);
    }

    #[test]
    fn max_rate_reports_a_finite_positive_rate_for_healthy_workers() {
        // No timing assumption: only that a rate exists and nobody panicked.
        let o = max_rate(&budget(50), 3, Duration::from_millis(5), |_| (), |_| 1000.0);
        assert_eq!(o.panicked, 0);
        assert!(o.rate.is_some_and(|r| r.is_finite() && r > 0.0), "{o:?}");
    }

    /// Runs `f` on a thread and fails (instead of hanging the suite) if it deadlocks.
    fn within_a_deadline<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let _ = tx.send(f());
        });
        rx.recv_timeout(Duration::from_secs(20))
            .expect("max_rate deadlocked")
    }

    #[test]
    fn a_panicking_worker_does_not_deadlock_and_is_counted() {
        let o = within_a_deadline(|| {
            max_rate(
                &budget(60),
                3,
                Duration::from_millis(5),
                |i| i,
                |i| {
                    if *i == 1 {
                        panic!("worker 1 blew up");
                    }
                    1000.0
                },
            )
        });
        assert_eq!(o.panicked, 1);
        assert!(o.rate.is_some_and(|r| r > 0.0), "{o:?}");
    }

    #[test]
    fn a_panicking_setup_does_not_deadlock_and_is_counted() {
        let o = within_a_deadline(|| {
            max_rate(
                &budget(60),
                2,
                Duration::from_millis(5),
                |i| {
                    if i == 0 {
                        panic!("alloc blew up");
                    }
                },
                |_| 1000.0,
            )
        });
        assert_eq!(o.panicked, 1);
    }
}