use super::budget::Budget;
use super::rate::max_rate;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
const FULL_LEN: usize = 32 * 1024 * 1024; const WINDOW: Duration = Duration::from_millis(250);
struct Chunk {
a: Vec<f64>,
b: Vec<f64>,
c: Vec<f64>,
}
fn alloc(len: usize, v: f64) -> Option<Vec<f64>> {
let mut x = Vec::new();
x.try_reserve_exact(len).ok()?;
x.resize(len, v);
Some(x)
}
const TINY_LEN: usize = 1024;
fn build_chunk(len: usize, mk: impl Fn(usize) -> Option<Chunk>) -> (Chunk, bool) {
match mk(len).or_else(|| mk(len / 4)) {
Some(c) => (c, false),
None => (
Chunk {
a: vec![0.0; TINY_LEN],
b: vec![1.0; TINY_LEN],
c: vec![2.0; TINY_LEN],
},
true,
),
}
}
fn chunk(len: usize, tiny: &AtomicBool) -> Chunk {
let (c, was_tiny) = build_chunk(len, |len| {
Some(Chunk {
a: alloc(len, 0.0)?,
b: alloc(len, 1.0)?,
c: alloc(len, 2.0)?,
})
});
if was_tiny {
tiny.store(true, Ordering::SeqCst);
}
c
}
fn pass(ch: &mut Chunk) -> f64 {
for ((x, y), z) in ch.a.iter_mut().zip(&ch.b).zip(&ch.c) {
*x = *y + 3.0 * *z;
}
std::hint::black_box(&ch.a);
3.0 * 8.0 * ch.a.len() as f64
}
#[derive(Debug, PartialEq)]
pub struct Triad {
pub gbps: f64,
pub note: Option<String>,
}
fn finish(rate: Option<f64>, tiny: bool, panicked: usize) -> Option<Triad> {
let r = rate.filter(|r| r.is_finite() && *r > 0.0)?;
if tiny {
return Some(Triad {
gbps: 0.0,
note: Some(
"memory: too little free RAM for the bandwidth test; not measured".to_string(),
),
});
}
Some(Triad {
gbps: r / 1e9,
note: (panicked > 0)
.then(|| format!("memory: {panicked} worker(s) panicked; bandwidth understated")),
})
}
pub fn triad_gbps(b: &Budget) -> Result<Triad, String> {
if b.cancelled() {
return Err("cancelled".into());
}
let threads = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
let len = FULL_LEN.div_ceil(threads);
let tiny = AtomicBool::new(false);
let o = max_rate(b, threads, WINDOW, |_| chunk(len, &tiny), pass);
match finish(o.rate, tiny.load(Ordering::SeqCst), o.panicked) {
Some(t) => Ok(t),
None if b.cancelled() => Err("cancelled".into()),
None => Err("triad timing failed".into()),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ok_chunk(len: usize) -> Option<Chunk> {
Some(Chunk {
a: vec![0.0; len],
b: vec![0.0; len],
c: vec![0.0; len],
})
}
#[test]
fn a_failed_allocation_falls_back_in_two_steps_and_flags_only_the_last() {
let (c, tiny) = build_chunk(4096, ok_chunk);
assert_eq!((c.a.len(), tiny), (4096, false));
let (c, tiny) = build_chunk(4096, |l| if l <= 1024 { ok_chunk(l) } else { None });
assert_eq!((c.a.len(), tiny), (1024, false));
let (c, tiny) = build_chunk(1 << 20, |_| None);
assert_eq!((c.a.len(), tiny), (TINY_LEN, true));
}
#[test]
fn a_cache_resident_run_reports_no_bandwidth_but_a_note() {
let t = finish(Some(900e9), true, 0).unwrap();
assert_eq!(t.gbps, 0.0);
assert!(t.note.unwrap().contains("not measured"));
let t = finish(Some(40e9), false, 0).unwrap();
assert_eq!((t.gbps, t.note), (40.0, None));
let t = finish(Some(40e9), false, 1).unwrap();
assert!(t.note.unwrap().contains("panicked"));
assert_eq!(finish(None, false, 0), None);
assert_eq!(finish(Some(f64::NAN), false, 0), None);
}
}