zc2 0.0.51

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Sequential disk throughput on the volume holding `dir`.
use super::budget::Budget;
use super::report::DiskMetrics;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime};

const CHUNK: usize = 4 * 1024 * 1024;
const TOTAL: usize = 256 * 1024 * 1024;

/// Deletes the scratch file on drop, including on early return.
struct Scratch(PathBuf);
impl Drop for Scratch {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.0);
    }
}

/// Per process, so two runs in one state dir never share (or delete) a file.
fn scratch_name(pid: u32) -> String {
    format!("bench-{pid}.tmp")
}

/// Scratch files older than this are leftovers of a killed run.
pub const STALE_SCRATCH_AFTER: Duration = Duration::from_secs(3600);

/// Deletes `bench-*.tmp` files in `dir` last modified more than `max_age`
/// before `now` (a SIGKILLed probe never ran `Scratch::drop`). Returns how many
/// went. A live run's file is minutes old at most, so the age bound keeps a
/// concurrent run in another state dir safe.
pub fn sweep_stale_scratch(dir: &Path, now: SystemTime, max_age: Duration) -> usize {
    let Ok(rd) = std::fs::read_dir(dir) else {
        return 0;
    };
    let mut n = 0;
    for e in rd.flatten() {
        let name = e.file_name();
        let Some(name) = name.to_str() else { continue };
        if !(name.starts_with("bench-") && name.ends_with(".tmp")) {
            continue;
        }
        let old = e
            .metadata()
            .and_then(|m| m.modified())
            .ok()
            .and_then(|t| now.duration_since(t).ok())
            .is_some_and(|age| age > max_age);
        if old && std::fs::remove_file(e.path()).is_ok() {
            n += 1;
        }
    }
    n
}

pub fn probe(dir: &Path, b: &Budget) -> Result<DiskMetrics, String> {
    probe_sized(dir, b, TOTAL)
}

/// [`probe`] over `total` bytes (a multiple of [`CHUNK`]). Tests pass a few
/// MiB: 256 MiB plus an fsync outlived the budget on a busy CI runner, so the
/// test measured the runner's disk rather than this code.
fn probe_sized(dir: &Path, b: &Budget, total: usize) -> Result<DiskMetrics, String> {
    if b.cancelled() {
        return Err("cancelled".into());
    }
    let scratch = Scratch(dir.join(scratch_name(std::process::id())));
    // Pseudo-random fill (xorshift64): compressible data would inflate write
    // speed on compressing filesystems such as ZFS.
    let mut x: u64 = 0x9E37_79B9_7F4A_7C15;
    let mut buf = vec![0u8; CHUNK];
    for w in buf.chunks_mut(8) {
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        w.copy_from_slice(&x.to_le_bytes()[..w.len()]);
    }
    let chunks = total / CHUNK;

    let t = Instant::now();
    {
        let mut f = File::create(&scratch.0).map_err(|e| e.to_string())?;
        for _ in 0..chunks {
            if b.expired() {
                return Err("budget exhausted during disk write".into());
            }
            f.write_all(&buf).map_err(|e| e.to_string())?;
        }
        f.sync_all().map_err(|e| e.to_string())?;
    }
    let write_secs = t.elapsed().as_secs_f64();

    // Reads are served from the page cache when the file fits in RAM, so
    // read_mbps is optimistic. Accepted: the score only needs a stable ordering.
    let t = Instant::now();
    let mut f = File::open(&scratch.0).map_err(|e| e.to_string())?;
    let mut rbuf = vec![0u8; CHUNK];
    let mut read = 0usize;
    while read < total {
        if b.expired() {
            return Err("budget exhausted during disk read".into());
        }
        let n = f.read(&mut rbuf).map_err(|e| e.to_string())?;
        if n == 0 {
            break;
        }
        read += n;
    }
    let read_secs = t.elapsed().as_secs_f64();
    if read < total || write_secs <= 0.0 || read_secs <= 0.0 {
        return Err("disk probe short read".into());
    }
    let mb = total as f64 / 1e6;
    Ok(DiskMetrics {
        write_mbps: mb / write_secs,
        read_mbps: mb / read_secs,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::AtomicBool;
    use std::sync::Arc;
    #[test]
    fn the_sweep_removes_only_old_bench_scratch_files() {
        let dir = std::env::temp_dir().join(format!("zc-bench-sweep-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        for n in [
            "bench-123.tmp",
            "bench-9.tmp",
            "last.json",
            "last.json.tmp",
            "notbench-1.tmp",
            "bench-1.log",
        ] {
            std::fs::write(dir.join(n), b"x").unwrap();
        }
        let hour = Duration::from_secs(3600);
        // Everything was just written: nothing is older than an hour.
        assert_eq!(sweep_stale_scratch(&dir, SystemTime::now(), hour), 0);
        assert!(dir.join("bench-123.tmp").exists());
        // Two hours later the scratch files are stale; nothing else is touched.
        let later = SystemTime::now() + 2 * hour;
        assert_eq!(sweep_stale_scratch(&dir, later, hour), 2);
        assert!(!dir.join("bench-123.tmp").exists() && !dir.join("bench-9.tmp").exists());
        for keep in [
            "last.json",
            "last.json.tmp",
            "notbench-1.tmp",
            "bench-1.log",
        ] {
            assert!(dir.join(keep).exists(), "{keep} must survive");
        }
        // A missing directory is not an error.
        assert_eq!(sweep_stale_scratch(&dir.join("nope"), later, hour), 0);
        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn disk_probe_measures_and_removes_its_file() {
        let dir = std::env::temp_dir().join(format!("zc-bench-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let m = probe_sized(
            &dir,
            &Budget::new(Duration::from_secs(10), Arc::new(AtomicBool::new(false))),
            2 * CHUNK,
        )
        .unwrap();
        assert!(m.write_mbps > 0.0 && m.read_mbps > 0.0);
        assert!(!dir.join(scratch_name(std::process::id())).exists());
        assert_ne!(scratch_name(1), scratch_name(2));
        std::fs::remove_dir_all(&dir).ok();
    }
}