Cotton code
230 lines · 7.7 KB · RustRaw
//! An invented process tree, and every rule a budget is decided by.//!//! Nothing here runs a process. Every fixture is made up: the pids are small//! numbers chosen to read, and the paths are paths a developer's Mac would//! have rather than any this machine does.use super::*;/// One row, spelled short because a tree takes a dozen of them.fn row(pid: u32, ppid: u32, rss_kb: u64, command: &str) -> ProcessRow {    ProcessRow {        pid,        ppid,        rss_kb,        cpu_pct: 0.0,        command: command.to_string(),    }}/// Gigabytes, as a `ps` row spells them.fn gb(gigabytes: u64) -> u64 {    gigabytes * 1024 * 1024}#[test]fn a_ps_table_is_read_field_by_field_and_the_command_keeps_its_spaces() {    let text = concat!(        "  501     1   40960   0.0 /sbin/launchd\n",        " 1200   501 8388608  99.5 /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild\n",        " 1300  1200 2097152  12.5 /Applications/Android Studio.app/Contents/emulator/emulator\n",    );    let rows = parse_ps(text);    assert_eq!(rows.len(), 3);    assert_eq!(rows[0], row(501, 1, 40_960, "/sbin/launchd"));    assert_eq!(rows[1].pid, 1200);    assert_eq!(rows[1].cpu_pct, 99.5);    assert_eq!(        rows[2].command,        "/Applications/Android Studio.app/Contents/emulator/emulator",        "comm is last precisely so a path with a space in it survives"    );}#[test]fn a_short_or_junk_line_costs_one_row_and_never_the_daemon() {    let text = concat!(        "  501     1   40960   0.0 /sbin/launchd\n",        "ps: something went wrong\n",        " 1200   501\n",        "\n",        " abc   501   40960   0.0 /usr/bin/thing\n",        " 1300   501   40960   0.0\n",        " 1400   501   40960   0.0 /usr/bin/kept\n",    );    let rows = parse_ps(text);    assert_eq!(        rows.iter().map(|row| row.pid).collect::<Vec<_>>(),        vec![501, 1400],        "a line that is short, or whose numbers are not numbers, is skipped"    );}#[test]fn a_tree_is_the_root_and_every_descendant_and_no_sibling() {    // 100 → 110 → 111, with 200 → 210 beside it and no relation to either.    let rows = vec![        row(100, 1, 1000, "/bin/claude"),        row(110, 100, 2000, "/usr/bin/cargo"),        row(111, 110, 3000, "/usr/bin/rustc"),        row(200, 1, 4000, "/bin/codex"),        row(210, 200, 5000, "/usr/bin/cargo"),    ];    assert_eq!(tree(&rows, 100), BTreeSet::from([100, 110, 111]));    assert_eq!(tree(&rows, 200), BTreeSet::from([200, 210]));    assert_eq!(        tree(&rows, 999),        BTreeSet::from([999]),        "a root nothing points at is still itself"    );}#[test]fn a_cycle_or_a_self_parent_terminates_rather_than_hanging_a_daemon() {    // 10 and 11 point at each other, and 12 is its own parent. A naive walk    // over this never returns; this daemon runs for weeks.    let rows = vec![        row(10, 11, 1000, "/bin/one"),        row(11, 10, 1000, "/bin/two"),        row(12, 12, 1000, "/bin/self"),        row(13, 11, 1000, "/bin/child"),    ];    assert_eq!(tree(&rows, 10), BTreeSet::from([10, 11, 13]));    assert_eq!(        tree(&rows, 12),        BTreeSet::from([12]),        "a process that parents itself absorbs nothing"    );}#[test]fn resident_memory_is_kilobytes_in_and_gigabytes_out() {    let rows = vec![        row(1, 0, gb(1), "/bin/one"),        row(2, 0, gb(1) / 2, "/bin/two"),        row(3, 0, 9_999_999, "/bin/uncounted"),    ];    assert_eq!(rss_gb(&rows, &BTreeSet::from([1])), 1.0);    assert_eq!(rss_gb(&rows, &BTreeSet::from([1, 2])), 1.5);    assert_eq!(rss_gb(&rows, &BTreeSet::new()), 0.0);}#[test]fn with_no_session_running_the_committed_figure_is_the_kernel_s_own() {    // Nothing to add headroom for, so the answer is what the OS said — and a    // Mac with a full process table is no different from an empty one here,    // because the sum of that table is not what this function is made of.    let rows = vec![row(700, 1, gb(9), "/usr/bin/xcodebuild")];    assert_eq!(committed_gb(&rows, &[], 11.0), 11.0);    assert_eq!(committed_gb(&[], &[], 11.0), 11.0);}#[test]fn a_session_well_under_its_peak_adds_exactly_what_it_will_want_back() {    // 0.5 GB now, measured at 4 GB while it compiled: 3.5 GB is still coming.    let rows = vec![        row(1, 0, 1000, "/sbin/launchd"),        row(100, 1, gb(1) / 2, "/Users/someone/.local/bin/claude"),    ];    let sessions = [SessionUsage {        root: 100,        peak_gb: 4.0,    }];    assert_eq!(committed_gb(&rows, &sessions, 11.0), 14.5);}#[test]fn a_session_sitting_at_its_peak_adds_nothing_because_it_is_already_in_use() {    // Mid-compile: the `cargo` and `rustc` under it are what the peak    // measured, and every byte of them is already inside the kernel's figure.    let rows = vec![        row(1, 0, 1000, "/sbin/launchd"),        row(100, 1, 1024 * 1024 / 2, "/Users/someone/.local/bin/claude"),        row(110, 100, gb(1), "/Users/someone/.cargo/bin/cargo"),        row(111, 110, gb(2) + gb(1) / 2, "/Users/someone/.cargo/bin/rustc"),    ];    let sessions = [SessionUsage {        root: 100,        peak_gb: 4.0,    }];    assert_eq!(        committed_gb(&rows, &sessions, 11.0),        11.0,        "the tree is at its peak, so there is no headroom left to promise"    );}#[test]fn a_session_over_its_recorded_peak_adds_zero_and_never_a_negative() {    // A peak the helper has not caught up with yet. A negative headroom would    // let one session PAY FOR another's, which is the one arithmetic a budget    // must not do.    let rows = vec![row(100, 1, gb(6), "/Users/someone/.local/bin/claude")];    let one = [SessionUsage {        root: 100,        peak_gb: 4.0,    }];    assert_eq!(committed_gb(&rows, &one, 11.0), 11.0);    let rows = vec![        row(100, 1, gb(6), "/Users/someone/.local/bin/claude"),        row(200, 1, gb(1) / 2, "/Users/someone/.local/bin/codex"),    ];    let two = [        SessionUsage {            root: 100,            peak_gb: 4.0,        },        SessionUsage {            root: 200,            peak_gb: 4.0,        },    ];    assert_eq!(        committed_gb(&rows, &two, 11.0),        14.5,        "the second session's 3.5 GB of headroom stands on its own"    );}#[test]fn a_build_outside_every_session_is_already_counted_and_raises_nothing() {    // THE WHOLE POINT of reading the kernel instead of summing a table: an    // `xcodebuild` somebody started in a terminal is inside `used_now`,    // whether or not anything here knows its name. Adding it to the process    // table must not move the answer.    let session = [SessionUsage {        root: 100,        peak_gb: 4.0,    }];    let alone = vec![        row(1, 0, 1000, "/sbin/launchd"),        row(100, 1, gb(1) / 2, "/Users/someone/.local/bin/claude"),    ];    let with_a_build = {        let mut rows = alone.clone();        rows.push(row(700, 1, gb(2), "/bin/zsh"));        rows.push(row(701, 700, gb(8), "/usr/bin/xcodebuild"));        rows.push(row(702, 701, gb(3), "/usr/bin/swift-frontend"));        rows    };    assert_eq!(        committed_gb(&with_a_build, &session, 11.0),        committed_gb(&alone, &session, 11.0),        "the kernel measured that build already; summing it again is the 37.9 GB bug"    );}#[test]fn a_process_table_that_could_not_be_read_errs_toward_a_fuller_mac() {    // A `ps` that failed answers an empty vec, so a session's tree reads as    // holding nothing and its whole peak counts as headroom. The Mac looks    // fuller than it is, which costs it work rather than costing a session its    // memory.    let sessions = [SessionUsage {        root: 100,        peak_gb: 4.0,    }];    assert_eq!(committed_gb(&[], &sessions, 11.0), 15.0);    assert!(parse_ps("").is_empty());}