OXYGENHUB
# 35f7a849bcc5 oxygenflow / crates / flow / src / facts.rs
4.4 KiB · Rust · last changed A job's finish carries its peak disk and memory 24 days ago source blame history raw
1
//! What a runner measures about itself (spec §11.1, docs/jetstream.md §6):
2
//! free disk, load and the kernel each claim, cheaply; and its speed, from
3
//! a short fixed benchmark run at start and again while idle.
4
5
use crate::home::Home;
6
use flow_core::api::Facts;
7
use std::time::Instant;
8
9
/// The benchmark's time on the reference machine (Earth), in ms: a runner
10
/// that takes as long scores 100.
11
const REFERENCE_MS: f64 = 225.0;
12
13
/// The cheap facts, with the last speed measured.
14
pub fn now(home: &Home, speed: Option<u32>) -> Facts {
15
    Facts {
16
        free_disk_mib: free_disk_mib(&home.root),
17
        load_x100: load_x100(),
18
        speed,
19
        speed_class: speed.map(Facts::class_of),
20
        kernel: std::fs::read_to_string("/proc/sys/kernel/osrelease").ok().map(|s| s.trim().to_string()),
21
    }
22
}
23
24
/// Free space for an unprivileged writer on the file system holding `path`, in MiB.
25
pub fn free_disk_mib(path: &std::path::Path) -> Option<u64> {
26
    use std::os::unix::ffi::OsStrExt;
27
    let c = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?;
28
    let mut s: libc::statvfs = unsafe { std::mem::zeroed() };
29
    // SAFETY: `c` is a valid NUL-terminated path and `s` a writable statvfs.
30
    if unsafe { libc::statvfs(c.as_ptr(), &mut s) } != 0 {
31
        return None;
32
    }
33
    Some((s.f_bavail as u64).saturating_mul(s.f_frsize as u64) >> 20)
34
}
35
36
/// The disk the files under `dir` take (their blocks, not their sizes: a
37
/// job's disks are sparse), in MiB; links are not followed.
38
pub fn allocated_mib(dir: &std::path::Path) -> Option<u64> {
39
    use std::os::unix::fs::MetadataExt;
40
    let mut total: u64 = 0;
41
    let mut stack = vec![dir.to_path_buf()];
42
    while let Some(d) = stack.pop() {
43
        for e in std::fs::read_dir(&d).ok()?.flatten() {
44
            let m = std::fs::symlink_metadata(e.path()).ok()?;
45
            if m.is_dir() {
46
                stack.push(e.path());
47
            } else {
48
                total = total.saturating_add(m.blocks() * 512);
49
            }
50
        }
51
    }
52
    Some(total >> 20)
53
}
54
55
/// A process's peak resident memory (`VmHWM`), in MiB.
56
pub fn peak_rss_mib(pid: u32) -> Option<u64> {
57
    let text = std::fs::read_to_string(format!("/proc/{pid}/status")).ok()?;
58
    let kib: u64 = text.lines().find_map(|l| l.strip_prefix("VmHWM:"))?.trim().trim_end_matches("kB").trim().parse().ok()?;
59
    Some(kib / 1024)
60
}
61
62
/// The 1-minute load average from /proc/loadavg, times 100.
63
pub fn load_x100() -> Option<u32> {
64
    let text = std::fs::read_to_string("/proc/loadavg").ok()?;
65
    let first: f64 = text.split_whitespace().next()?.parse().ok()?;
66
    Some((first * 100.0).round() as u32)
67
}
68
69
/// The benchmark, one thread: BLAKE3 over 256 MiB (spec §11.1), and a sort
70
/// of 4 Mi pseudo-random words for the integer and memory side. Answers
71
/// the score, 100 on the reference machine, and the time it took.
72
pub fn bench() -> (u32, u128) {
73
    let start = Instant::now();
74
    let buf: Vec<u8> = (0..32u32 << 20).map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8).collect();
75
    let mut h = blake3::Hasher::new();
76
    for _ in 0..8 {
77
        h.update(&buf);
78
    }
79
    std::hint::black_box(h.finalize());
80
    drop(buf);
81
    let mut x: u64 = 0x9e37_79b9_7f4a_7c15;
82
    let mut words: Vec<u64> = (0..4u64 << 20)
83
        .map(|_| {
84
            x ^= x << 13;
85
            x ^= x >> 7;
86
            x ^= x << 17;
87
            x
88
        })
89
        .collect();
90
    words.sort_unstable();
91
    std::hint::black_box(&words);
92
    let ms = start.elapsed().as_millis().max(1);
93
    ((REFERENCE_MS * 100.0 / ms as f64).round() as u32, ms)
94
}
95
96
#[cfg(test)]
97
mod tests {
98
    use super::*;
99
100
    #[test]
101
    fn the_cheap_facts_are_read_and_classes_double() {
102
        assert!(free_disk_mib(std::path::Path::new("/")).is_some());
103
        assert!(load_x100().is_some());
104
        assert!(peak_rss_mib(std::process::id()).is_some_and(|m| m > 0));
105
        let dir = tempfile::tempdir().unwrap();
106
        let f = std::fs::File::create(dir.path().join("sparse")).unwrap();
107
        f.set_len(1 << 30).unwrap();
108
        std::fs::write(dir.path().join("full"), vec![7u8; 3 << 20]).unwrap();
109
        let mib = allocated_mib(dir.path()).unwrap();
110
        assert!((3..=4).contains(&mib), "the sparse gigabyte takes nothing, the written 3 MiB do: {mib}");
111
        assert_eq!(Facts::class_of(100), 3, "the reference machine");
112
        assert_eq!((Facts::class_of(50), Facts::class_of(200), Facts::class_of(500), Facts::class_of(10)), (2, 4, 5, 1));
113
    }
114
}