OXYGENHUB
# 8aa45fa6ece5 oxygenflow / crates / flow / src / facts.rs
6.2 KiB · Rust · last changed Windows runner: free disk in claims; dist builds flow-windows-x86_64.exe 10 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: kernel(),
21
    }
22
}
23
24
/// The kernel's release, as uname says. Not read from /proc: an Android
25
/// app may not read it there, and is refused (and logged) every claim.
26
/// None on Windows (oxygenflow#83).
27
#[cfg(unix)]
28
pub fn kernel() -> Option<String> {
29
    let mut u: libc::utsname = unsafe { std::mem::zeroed() };
30
    // SAFETY: `u` is a writable utsname.
31
    if unsafe { libc::uname(&mut u) } != 0 {
32
        return None;
33
    }
34
    // SAFETY: uname writes NUL-terminated strings.
35
    let release = unsafe { std::ffi::CStr::from_ptr(u.release.as_ptr()) };
36
    Some(release.to_string_lossy().into_owned())
37
}
38
39
#[cfg(not(unix))]
40
pub fn kernel() -> Option<String> {
41
    None
42
}
43
44
/// Free space for an unprivileged writer on the file system holding `path`,
45
/// in MiB.
46
#[cfg(unix)]
47
pub fn free_disk_mib(path: &std::path::Path) -> Option<u64> {
48
    use std::os::unix::ffi::OsStrExt;
49
    let c = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?;
50
    let mut s: libc::statvfs = unsafe { std::mem::zeroed() };
51
    // SAFETY: `c` is a valid NUL-terminated path and `s` a writable statvfs.
52
    if unsafe { libc::statvfs(c.as_ptr(), &mut s) } != 0 {
53
        return None;
54
    }
55
    Some((s.f_bavail as u64).saturating_mul(s.f_frsize as u64) >> 20)
56
}
57
58
#[cfg(windows)]
59
pub fn free_disk_mib(path: &std::path::Path) -> Option<u64> {
60
    use std::os::windows::ffi::OsStrExt;
61
    let wide: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect();
62
    let mut free: u64 = 0;
63
    // SAFETY: `wide` is a NUL-terminated path and `free` a writable u64;
64
    // the totals it may also fill are not asked for.
65
    let ok = unsafe {
66
        windows_sys::Win32::Storage::FileSystem::GetDiskFreeSpaceExW(wide.as_ptr(), &mut free, std::ptr::null_mut(), std::ptr::null_mut())
67
    };
68
    (ok != 0).then_some(free >> 20)
69
}
70
71
/// The disk the files under `dir` take (their blocks, not their sizes: a
72
/// job's disks are sparse), in MiB; links are not followed.
73
pub fn allocated_mib(dir: &std::path::Path) -> Option<u64> {
74
    let mut total: u64 = 0;
75
    let mut stack = vec![dir.to_path_buf()];
76
    while let Some(d) = stack.pop() {
77
        for e in std::fs::read_dir(&d).ok()?.flatten() {
78
            let m = std::fs::symlink_metadata(e.path()).ok()?;
79
            if m.is_dir() {
80
                stack.push(e.path());
81
            } else {
82
                total = total.saturating_add(crate::portable::disk_bytes(&m));
83
            }
84
        }
85
    }
86
    Some(total >> 20)
87
}
88
89
/// A process's peak resident memory (`VmHWM`), in MiB.
90
pub fn peak_rss_mib(pid: u32) -> Option<u64> {
91
    let text = std::fs::read_to_string(format!("/proc/{pid}/status")).ok()?;
92
    let kib: u64 = text.lines().find_map(|l| l.strip_prefix("VmHWM:"))?.trim().trim_end_matches("kB").trim().parse().ok()?;
93
    Some(kib / 1024)
94
}
95
96
/// The 1-minute load average, times 100, from sysinfo (the kernel's own
97
/// numbers behind /proc/loadavg, which an Android app may not read); not
98
/// measured on Windows.
99
#[cfg(not(unix))]
100
pub fn load_x100() -> Option<u32> {
101
    None
102
}
103
104
#[cfg(unix)]
105
pub fn load_x100() -> Option<u32> {
106
    let mut i: libc::sysinfo = unsafe { std::mem::zeroed() };
107
    // SAFETY: `i` is a writable sysinfo.
108
    if unsafe { libc::sysinfo(&mut i) } != 0 {
109
        return None;
110
    }
111
    // Fixed point with 16 fraction bits (SI_LOAD_SHIFT).
112
    Some(((i.loads[0] as f64) / 65536.0 * 100.0).round() as u32)
113
}
114
115
/// The benchmark, one thread: BLAKE3 over 256 MiB (spec §11.1), and a sort
116
/// of 4 Mi pseudo-random words for the integer and memory side. Answers
117
/// the score, 100 on the reference machine, and the time it took.
118
pub fn bench() -> (u32, u128) {
119
    let start = Instant::now();
120
    let buf: Vec<u8> = (0..32u32 << 20).map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8).collect();
121
    let mut h = blake3::Hasher::new();
122
    for _ in 0..8 {
123
        h.update(&buf);
124
    }
125
    std::hint::black_box(h.finalize());
126
    drop(buf);
127
    let mut x: u64 = 0x9e37_79b9_7f4a_7c15;
128
    let mut words: Vec<u64> = (0..4u64 << 20)
129
        .map(|_| {
130
            x ^= x << 13;
131
            x ^= x >> 7;
132
            x ^= x << 17;
133
            x
134
        })
135
        .collect();
136
    words.sort_unstable();
137
    std::hint::black_box(&words);
138
    let ms = start.elapsed().as_millis().max(1);
139
    ((REFERENCE_MS * 100.0 / ms as f64).round() as u32, ms)
140
}
141
142
#[cfg(test)]
143
mod tests {
144
    use super::*;
145
146
    #[test]
147
    fn the_cheap_facts_are_read_and_classes_double() {
148
        assert!(free_disk_mib(std::path::Path::new("/")).is_some());
149
        // The same numbers /proc gives, read without it (an Android app may not).
150
        let proc_load: f64 = std::fs::read_to_string("/proc/loadavg").unwrap().split_whitespace().next().unwrap().parse().unwrap();
151
        let load = load_x100().unwrap() as f64 / 100.0;
152
        assert!((load - proc_load).abs() < 1.0 + proc_load / 4.0, "sysinfo {load}, /proc {proc_load}");
153
        assert_eq!(kernel().unwrap(), std::fs::read_to_string("/proc/sys/kernel/osrelease").unwrap().trim());
154
        assert!(peak_rss_mib(std::process::id()).is_some_and(|m| m > 0));
155
        let dir = tempfile::tempdir().unwrap();
156
        let f = std::fs::File::create(dir.path().join("sparse")).unwrap();
157
        f.set_len(1 << 30).unwrap();
158
        std::fs::write(dir.path().join("full"), vec![7u8; 3 << 20]).unwrap();
159
        let mib = allocated_mib(dir.path()).unwrap();
160
        assert!((3..=4).contains(&mib), "the sparse gigabyte takes nothing, the written 3 MiB do: {mib}");
161
        assert_eq!(Facts::class_of(100), 3, "the reference machine");
162
        assert_eq!((Facts::class_of(50), Facts::class_of(200), Facts::class_of(500), Facts::class_of(10)), (2, 4, 5, 1));
163
    }
164
}