diff --git a/tests/guest/osbench/README.md b/tests/guest/osbench/README.md index ed1c7514ba0..3d4331b44ee 100644 --- a/tests/guest/osbench/README.md +++ b/tests/guest/osbench/README.md @@ -32,8 +32,11 @@ and the C library beneath it are each system's own. - **At least four runs of each system, interleaved**: Linux, ToyOS, Linux, ToyOS. A ToyOS run is one boot and one readback directory. A Linux run is `osbench all `. It - refuses runs of one system that disagree on their setup (`SETUP`) and - states each system's, and what each run's boot chose (`PER_RUN`, the - clocksource) run by run. Each row's verdict says whether the runs resolve a - difference. Each p is its row's alone: with no difference anywhere, about - 2.9% of rows resolve by chance, a few in a table this size. + refuses runs of one system that disagree on their setup (`SETUP`, the + clocksource among it) and states each system's. Each row's verdict says + whether the runs resolve a difference. Each p is its row's alone: with no + difference anywhere, about 2.9% of rows resolve by chance, a few in a table + this size. - **The clocks are told beside each verdict.** No program reads the package's limit state or temperature on ToyOS, so the clock is what both systems report. A clock reading is the difference between a short add @@ -71,12 +74,11 @@ and the C library beneath it are each system's own. ## Linux as installed, and Linux under ToyOS's requests -**The baseline is Linux as the bench boots it**: its own governor, its own -clocksource, its own checks. What it does on that machine is ordinary OS -behaviour and part of what is compared. A comparison names its -configuration in its setup lines, and `osbench-compare` refuses to mix two. -What a boot chooses for itself, the clocksource, belongs to Linux as booted: -each run states its own, and runs that differ are compared together. +**The baseline is Linux as the bench boots it**: its own governor and its +own checks, on a boot that kept the TSC as its clocksource. What it does on +that machine is ordinary OS behaviour and part of what is compared. A +comparison names its configuration in its setup lines, and `osbench-compare` +refuses to mix two. **A matched request is a controlled experiment, labelled as one.** It asks the CPU's frequency control for what ToyOS's kernel asks of the same CPU, so @@ -126,8 +128,15 @@ carried by every per-operation timing of that run. In 30 boots' kernel logs In a 13th boot the calibration read 1799.565 MHz and the watchdog let it stand, so that Linux kept a TSC clock some 1,630 ppm fast for the whole boot. Its `clocksource=tsc` reads the same as a right one's. The 12 others -kept the TSC on a right calibration. Each run's clocksource is part of the -Linux it measured, so the compare states it per run. +kept the TSC on a right calibration. + +An HPET boot is no run. The scoreboard of 2026-11-11 took three of the AMD +bench's five Linux runs on the HPET: their per-operation rows read slower by +the timestamps' cost and their all-CPU rows a third lower, so the median of +five was an HPET run's, and `fair.shared.total` read ToyOS above it and under +both TSC runs. So `osbench` on Linux exits 3 before any group where the +clocksource is not the one its vDSO reads, and the bench is booted again +until it is; `osbench-compare` refuses runs whose clocksources differ. At runtime neither fault shows: across 16 CPUs and idle gaps up to 1 s, no CPU's TSC read behind another's and no step exceeded the 1.2 µs of a read. @@ -239,11 +248,30 @@ rest of the suite, on the benchcase image. Of the three only `persist` writes the bench's NVMe; `files` is the memory filesystem's rows, and `nvme` is read-only and unsupported on ToyOS. +## Waits are judged slack-free + +An ordinary Linux thread's sleep is padded by its timer slack, 50 µs by +default, and ToyOS pads none. Linux runs each wait row a second time with +the slack at 1 ns (`.slack1ns`), and that is the row judged: the +compare sets ToyOS's row of the unsuffixed name against it, and gives the +unsuffixed row no verdict. + +## A futex round trip across cores is one that slept + +A side of `pingpong.futex.*` that finds its word already changed never +sleeps. Across two cores a whole round can pass that way: three of the T14's +four boots of 2026-11-11 read the row at 250 to 257 ns and the fourth at +4.6 µs. So `pingpong.futex.cross_core` keeps the rounds in which each side's +wake found the other queued in the kernel; the others are +`pingpong.futex.cross_core.unparked`, and a run in which no round parked +reports the row `unmeasured`. On one CPU no round passes without both sides +leaving it, so that row keeps every round. Both say how many such wakes a +kept round had, of two (`parks_per_round`). + ## What is not a comparison The table also holds rows only one system can measure. They show the system's figure beside the other's reason: `unsupported` and `unmeasured` -rows, Linux's `.slack1ns` variants and `fair.session`, and ToyOS's -`tail.diag` and `fs.*.direct.*`, the file server's client without std's -layer above it, which Linux has no counterpart to. They are records, not -verdicts. +rows, Linux's `fair.session`, and ToyOS's `tail.diag` and `fs.*.direct.*`, +the file server's client without std's layer above it, which Linux has no +counterpart to. They are records, not verdicts. diff --git a/tests/guest/osbench/compare/src/lib.rs b/tests/guest/osbench/compare/src/lib.rs index 18ff087f94e..d0a0896d5a0 100644 --- a/tests/guest/osbench/compare/src/lib.rs +++ b/tests/guest/osbench/compare/src/lib.rs @@ -31,9 +31,11 @@ //! //! A system's runs on one machine are one configuration ([`SETUP`]): runs that //! disagree on it are refused, since their median would mix two, and each -//! machine's table states what each system's runs were taken under. What a -//! system chooses afresh at each boot ([`PER_RUN`]) is part of that system as -//! booted: it is stated run by run and never refused. +//! machine's table states what each system's runs were taken under. +//! +//! Linux pads an ordinary thread's sleep by its timer slack and ToyOS pads +//! none, so a row Linux also ran slack-free ([`SLACK_FREE`]) is judged there, +//! against ToyOS's row of the unsuffixed name, and not under that name. use std::collections::BTreeMap; use std::fmt::Write as _; @@ -261,15 +263,19 @@ pub enum Better { } /// Which way `unit` is better, for the units whose direction is known: a -/// time and a rate. A clock, a power or a share is no better higher or lower. +/// time, a rate and a distance from an entitlement. A clock, a power or a +/// share is no better higher or lower. pub fn better(unit: &str) -> Option { match unit { - "ns" => Some(Better::Lower), - "MB/s" => Some(Better::Higher), + "ns" | "ps" | "ns/MiB" | "permille_off" => Some(Better::Lower), + "MB/s" | "kops/s" | "chunks/s" => Some(Better::Higher), _ => None, } } +/// The suffix of a row Linux ran with its thread's timer slack at 1 ns. +pub const SLACK_FREE: &str = ".slack1ns"; + /// The condition every run of a machine must agree on for a benchmark's /// runs to be compared, and how many of its leading words must agree: the /// peer's link kind and driver, and the internet link's kind, not what @@ -277,11 +283,13 @@ pub fn better(unit: &str) -> Option { const PATHS: [(&str, &str, usize); 2] = [("lan.", "lan.peer_link", 2), ("internet.", "internet.link", 1)]; /// The conditions every run of one system on one machine must share: the -/// kernel, the CPU's frequency request, what serves each filesystem the `fs` -/// and `persist` rows use, and the panel, its light, the radios and the links -/// the `energy` rows draw beside. -pub const SETUP: [&str; 17] = [ +/// kernel, the clock every timestamp is read from, the CPU's frequency +/// request, what serves each filesystem the `fs` and `persist` rows use, and +/// the panel, its light, the radios and the links the `energy` rows draw +/// beside. +pub const SETUP: [&str; 18] = [ "kernel", + "clocksource", "cpufreq_driver", "governor", "epp", @@ -300,25 +308,6 @@ pub const SETUP: [&str; 17] = [ "energy.links", ]; -/// The conditions a system chooses at each boot, stated per run: the clock -/// every timestamp is read from, which Linux's watchdog can move off the TSC -/// in one boot and not the next. -pub const PER_RUN: [&str; 1] = ["clocksource"]; - -/// `os`'s [`PER_RUN`] conditions, each as `key=,...` in the -/// runs' order, `absent` for a run that names none. -fn per_run(runs: &[&Run], os: Os) -> Vec { - let runs: Vec<&&Run> = runs.iter().filter(|r| r.os == os).collect(); - PER_RUN - .iter() - .filter(|key| runs.iter().any(|r| r.conds.contains_key(**key))) - .map(|key| { - let said: Vec<&str> = runs.iter().map(|r| r.conds.get(*key).map_or("absent", String::as_str)).collect(); - format!("{key}={}", said.join(",")) - }) - .collect() -} - /// `os`'s setup on these runs, as `key=value` in [`SETUP`]'s order, or the /// first key its runs disagree on. fn setup(runs: &[&Run], os: Os) -> Result, String> { @@ -370,10 +359,6 @@ pub fn compare(runs: &[Run]) -> Result { if !said.is_empty() { writeln!(out, "{} setup: {}.\n", os.name(), said.join(", ")).unwrap(); } - let each = per_run(&runs, os); - if !each.is_empty() { - writeln!(out, "{} per run: {}.\n", os.name(), each.join(", ")).unwrap(); - } } writeln!(out, "| bench | unit | Linux p50s | Linux median | Linux range | ToyOS p50s | ToyOS median | ToyOS range | ToyOS/Linux | p | verdict |").unwrap(); writeln!(out, "|---|---|---|---|---|---|---|---|---|---|---|").unwrap(); @@ -403,8 +388,15 @@ fn compare_one(runs: &[&Run], name: &str) -> String { let mut settled: BTreeMap> = BTreeMap::new(); let mut media: BTreeMap>> = BTreeMap::new(); let mut not: BTreeMap> = BTreeMap::new(); + // ToyOS's side of a slack-free row is its row of the unsuffixed name. + let unpadded = name.strip_suffix(SLACK_FREE).filter(|_| !runs.iter().any(|r| r.os == Os::Toyos && r.benches.contains_key(name))); + let padded = runs.iter().any(|r| r.os == Os::Linux && r.benches.contains_key(&format!("{name}{SLACK_FREE}"))); for run in runs { - match run.benches.get(name) { + let outcome = match unpadded { + Some(unpadded) if run.os == Os::Toyos => run.benches.get(unpadded), + _ => run.benches.get(name), + }; + match outcome { Some(Outcome::Measured { unit: u, p50, clock_fell, settled: mhz, unsettled, medium }) => { if unit.get_or_insert(u.as_str()) != u { return "| | | | | | | | | units differ between runs".into(); @@ -442,6 +434,9 @@ fn compare_one(runs: &[&Run], name: &str) -> String { }; ("-".into(), "-".into(), format!("the media differ: Linux {}, ToyOS {}", named(Os::Linux), named(Os::Toyos))) } + (Some(_), Some(_)) if padded => { + ("-".into(), "-".into(), format!("Linux's timer slack pads this wait: judged as {name}{SLACK_FREE}")) + } (Some(l), Some(t)) => { let ratio = median(t) / median(l); let exact = mann_whitney(t, l); @@ -479,6 +474,9 @@ fn compare_one(runs: &[&Run], name: &str) -> String { note(format!("{said} in {n} of {of} {} run(s)", os.name())); } } + if let Some(unpadded) = unpadded.filter(|_| values.contains_key(&Os::Toyos)) { + note(format!("ToyOS's is its {unpadded}")); + } for (os, reasons) in ¬ { let mut distinct = reasons.clone(); distinct.sort(); @@ -629,7 +627,10 @@ mod tests { } #[test] - fn a_throughputs_higher_value_is_the_better_one() { + fn a_rates_higher_value_is_the_better_one_and_a_times_lower() { + assert_eq!(["ns", "ps", "ns/MiB", "permille_off"].map(better), [Some(Better::Lower); 4]); + assert_eq!(["MB/s", "kops/s", "chunks/s"].map(better), [Some(Better::Higher); 3]); + assert_eq!(["MHz", "mW", "permille", "us"].map(better), [None; 4]); let mbps = |os: &str, v: u64| run(os, &[&format!("bench=lan.wired.tcp.upload unit=MB/s n=5 min=1 p50={v} max=9 mean=5")]); let mut runs: Vec = [40, 41, 42, 43].iter().map(|v| mbps("linux", *v)).collect(); runs.extend([50, 51, 52, 53].iter().map(|v| mbps("toyos", *v))); @@ -715,7 +716,7 @@ mod tests { #[test] fn a_systems_runs_on_two_setups_are_refused_and_one_setup_is_stated() { let under = |os: &str, governor: &str| { - run(os, &[&format!("cond governor={governor}"), "cond clocksource=tsc", "bench=time.now unit=ps n=15 min=1 p50=14 max=1 mean=1"]) + run(os, &[&format!("cond governor={governor}"), "bench=time.now unit=ps n=15 min=1 p50=14 max=1 mean=1"]) }; let mixed = [under("linux", "performance*16"), under("linux", "schedutil*16"), under("toyos", "absent")]; let why = compare(&mixed).unwrap_err(); @@ -728,14 +729,41 @@ mod tests { } #[test] - fn a_clocksource_each_boot_chose_is_stated_per_run_and_never_refused() { + fn runs_timed_by_two_clocksources_are_refused() { let booted = |clocksource: &str| { run("linux", &[&format!("cond clocksource={clocksource}"), "bench=time.now unit=ps n=15 min=1 p50=14 max=1 mean=1"]) }; - let runs = [booted("hpet"), booted("tsc"), booted("hpet"), run("toyos", &["bench=time.now unit=ps n=15 min=1 p50=9 max=1 mean=1"])]; + let mixed = [booted("hpet"), booted("tsc"), booted("hpet")]; + assert!(compare(&mixed).unwrap_err().contains("the Linux runs disagree on clocksource: hpet / tsc")); + let one = [booted("tsc"), booted("tsc")]; + assert!(compare(&one).unwrap().contains("Linux setup: clocksource=tsc.")); + } + + #[test] + fn a_wait_linux_ran_slack_free_is_judged_there_against_toyos_unpadded_row() { + let linux = |padded: u64, slack_free: u64| { + run( + "linux", + &[ + &format!("bench=time.timer_1ms.late unit=ns n=300 min=1 p50={padded} max=9 mean=5"), + &format!("bench=time.timer_1ms.late.slack1ns unit=ns n=300 min=1 p50={slack_free} max=9 mean=5"), + ], + ) + }; + let toyos = |v: u64| run("toyos", &[&format!("bench=time.timer_1ms.late unit=ns n=300 min=1 p50={v} max=9 mean=5")]); + let mut runs: Vec = [(53_000, 3_000), (54_000, 3_100), (55_000, 3_200), (56_000, 3_300)].iter().map(|&(p, s)| linux(p, s)).collect(); + runs.extend([9_000, 9_100, 9_200, 9_300].iter().map(|v| toyos(*v))); let table = compare(&runs).unwrap(); - assert!(table.contains("Linux per run: clocksource=hpet,tsc,hpet."), "{table}"); - assert!(!table.contains("ToyOS per run"), "{table}"); + let row = |name: &str| table.lines().find(|l| l.starts_with(&format!("| {name} |"))).unwrap().to_string(); + // Against the padded row ToyOS would read six times better. + let padded = row("time.timer_1ms.late"); + assert!(padded.ends_with("| - | - | Linux's timer slack pads this wait: judged as time.timer_1ms.late.slack1ns |"), "{padded}"); + let judged = row("time.timer_1ms.late.slack1ns"); + assert!(judged.contains("| 9000, 9100, 9200, 9300 |"), "{judged}"); + assert!(judged.ends_with("| 0.029 | ToyOS worse; ToyOS's is its time.timer_1ms.late |"), "{judged}"); + // A row Linux ran only padded is judged as it is. + let alone = [run("linux", &["bench=time.yield unit=ps n=15 min=1 p50=10 max=1 mean=1"]), run("toyos", &["bench=time.yield unit=ps n=15 min=1 p50=30 max=1 mean=1"])]; + assert!(!compare(&alone).unwrap().contains("slack")); } #[test] diff --git a/tests/guest/osbench/rules/Cargo.toml b/tests/guest/osbench/rules/Cargo.toml index 40d34a97d06..8bd649f790a 100644 --- a/tests/guest/osbench/rules/Cargo.toml +++ b/tests/guest/osbench/rules/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "osbench-rules" -description = "osbench's pure rules, host-tested: when a munmap pair's x2APIC readings admit its figure, where the CPU crunch stops for heat, the package energy counter's span, unit and power, when a machine is quiet, whether two counters readings describe their row, and when a CPU's clock has settled." +description = "osbench's pure rules, host-tested: when a munmap pair's x2APIC readings admit its figure, where the CPU crunch stops for heat, the package energy counter's span, unit and power, when a machine is quiet, whether two counters readings describe their row, when a CPU's clock has settled, each contending job's fair share of the machine, and which rounds of a futex exchange slept." version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" diff --git a/tests/guest/osbench/rules/src/lib.rs b/tests/guest/osbench/rules/src/lib.rs index 0b425e60000..b65c6f3bfea 100644 --- a/tests/guest/osbench/rules/src/lib.rs +++ b/tests/guest/osbench/rules/src/lib.rs @@ -2,8 +2,139 @@ //! a host tests them: whether a munmap pair's figure stands ([`pair`]), where //! the CPU crunch stops for heat ([`heat`]), what an energy counter //! read and when the machine is quiet ([`energy`]), whether two counters readings -//! describe the row they were read around ([`window`]), and when a CPU's -//! clock has settled ([`settle`]). +//! describe the row they were read around ([`window`]), when a CPU's +//! clock has settled ([`settle`]), what share of the machine each of +//! several contending jobs is entitled to ([`fair`]), and which rounds of a +//! futex exchange slept ([`parked`]). + +pub mod parked { + //! A side of a futex exchange parked in a round when the other's wake + //! found it queued in the kernel. A side that finds its word already + //! changed never sleeps, and across two cores a round of two such sides + //! is no wake's cost. + + /// Whether a hand-over's wake found the other side queued: `None` from a + /// channel that cannot tell. + pub type Woke = Option; + + /// The parks in the kept rounds, at most two a round: `None` from a + /// channel that cannot tell. `kept` and `pong` are [`split`]'s. + pub fn parks(kept: &[(u32, u64, Woke)], pong: &[Woke]) -> Option { + kept.iter().map(|&(round, _, ping)| Some(u64::from(ping?) + u64::from(pong[round as usize - 1]?))).sum() + } + + /// The kept rounds' times, those in which both sides parked and the + /// rest. `kept` is each round's number from 1, its time and what the + /// pinger's wake found; `pong` what the ponger's found, by round. A round + /// either side cannot tell of counts as parked. + pub fn split(kept: &[(u32, u64, Woke)], pong: &[Woke]) -> (Vec, Vec) { + let (mut parked, mut unparked) = (Vec::new(), Vec::new()); + for &(round, took, ping) in kept { + let both = match (ping, pong[round as usize - 1]) { + (Some(ping), Some(pong)) => ping && pong, + _ => true, + }; + if both { parked.push(took) } else { unparked.push(took) } + } + (parked, unparked) + } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn a_round_parked_only_where_both_wakes_found_a_waiter() { + let pong = [Some(true), Some(true), Some(false), Some(false)]; + let kept = [(1, 4600, Some(true)), (2, 900, Some(false)), (3, 800, Some(true)), (4, 250, Some(false))]; + assert_eq!(split(&kept, &pong), (vec![4600], vec![900, 800, 250])); + } + + #[test] + fn a_round_is_matched_to_the_ponger_by_its_number() { + // Rounds 1 and 3 were not kept: the pinger's second kept round is the ponger's fourth. + let pong = [Some(false), Some(true), Some(false), Some(true)]; + assert_eq!(split(&[(2, 10, Some(true)), (4, 20, Some(true))], &pong), (vec![10, 20], vec![])); + } + + #[test] + fn a_channel_that_cannot_tell_keeps_every_round_and_counts_no_park() { + assert_eq!(split(&[(1, 7, None), (2, 9, None)], &[None, None]), (vec![7, 9], vec![])); + assert_eq!(parks(&[(1, 7, None), (2, 9, None)], &[None, None]), None); + } + + #[test] + fn each_wake_that_found_its_waiter_is_a_park() { + let pong = [Some(true), Some(true), Some(false), Some(false)]; + let kept = [(1, 4600, Some(true)), (2, 900, Some(false)), (4, 250, Some(false))]; + assert_eq!(parks(&kept, &pong), Some(3)); + } + } +} + +pub mod fair { + //! Max-min fairness at equal weights: no unit gets more than it can use, + //! and what one leaves is split evenly among the rest (Bertsekas and + //! Gallager, Data Networks, "Max-Min Flow Control"). A unit is whatever + //! the scheduler shares CPU time between, a job or a thread. + + /// What the scheduler shares CPU time between. + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + pub enum Unit { + Job, + Thread, + } + + /// Each demand's allocation of `capacity`, in the demands' order. + fn max_min(capacity: u64, demands: &[u64]) -> Vec { + let mut order: Vec = (0..demands.len()).collect(); + order.sort_by_key(|&i| demands[i]); + let mut given = vec![0; demands.len()]; + let mut left = capacity; + for (rank, &i) in order.iter().enumerate() { + given[i] = demands[i].min(left / (demands.len() - rank) as u64); + left -= given[i]; + } + given + } + + /// Each job's entitled share of a machine of `cpus` CPUs, in permille, + /// where job `i` keeps `threads[i]` threads runnable. + pub fn entitled_permille(cpus: u64, threads: &[u64], unit: Unit) -> Vec { + const MILLI: u64 = 1000; + let milli_cpus = match unit { + Unit::Job => max_min(cpus * MILLI, &threads.iter().map(|t| t * MILLI).collect::>()), + Unit::Thread => { + let each = max_min(cpus * MILLI, &vec![MILLI; threads.iter().sum::() as usize]); + threads.iter().map(|&t| t * each.first().copied().unwrap_or(0)).collect() + } + }; + milli_cpus.iter().map(|m| m / cpus.max(1)).collect() + } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn a_job_that_can_use_one_cpu_gets_it_and_the_others_split_the_rest() { + assert_eq!(entitled_permille(8, &[1, 8, 8], Unit::Job), [125, 437, 437]); + assert_eq!(entitled_permille(16, &[1, 16, 16], Unit::Job), [62, 468, 468]); + } + + #[test] + fn jobs_that_each_want_the_machine_get_a_third() { + assert_eq!(entitled_permille(8, &[8, 8, 8], Unit::Job), [333, 333, 333]); + assert_eq!(entitled_permille(2, &[1, 8, 8], Unit::Job), [333, 333, 333], "one CPU is over a third of two"); + } + + #[test] + fn threads_as_the_unit_share_by_their_number() { + assert_eq!(entitled_permille(8, &[1, 8, 8], Unit::Thread), [58, 470, 470]); + assert_eq!(entitled_permille(8, &[1, 2, 2], Unit::Thread), [125, 250, 250], "fewer threads than CPUs run whole"); + } + } +} pub mod pair { //! A munmap pair is an initiator that unmaps and a sibling that touched diff --git a/tests/guest/osbench/src/arch/aarch64.rs b/tests/guest/osbench/src/arch/aarch64.rs index 4edd8f4095c..f9332e9d9aa 100644 --- a/tests/guest/osbench/src/arch/aarch64.rs +++ b/tests/guest/osbench/src/arch/aarch64.rs @@ -32,6 +32,10 @@ pub fn add_chain(turns: u64) { /// Adds per turn of [`add_chain`]. pub const ADDS_PER_TURN: u64 = 100; +/// The clocksource Linux's vDSO answers `clock_gettime` from without a +/// syscall, as sysfs names it. +pub const VDSO_CLOCKSOURCE: &str = "arch_sys_counter"; + /// The ELF machine a generated program is for (`EM_AARCH64`). pub const ELF_MACHINE: u16 = 183; diff --git a/tests/guest/osbench/src/arch/x86_64.rs b/tests/guest/osbench/src/arch/x86_64.rs index da1d0bf1a4a..f8a3f4ef852 100644 --- a/tests/guest/osbench/src/arch/x86_64.rs +++ b/tests/guest/osbench/src/arch/x86_64.rs @@ -38,6 +38,11 @@ pub fn add_chain(turns: u64) { /// Adds per turn of [`add_chain`]. pub const ADDS_PER_TURN: u64 = 100; +/// The clocksource Linux's vDSO answers `clock_gettime` from without a +/// syscall on a bench, as sysfs names it. +#[cfg(target_os = "linux")] +pub const VDSO_CLOCKSOURCE: &str = "tsc"; + /// The ELF machine a generated program is for (`EM_X86_64`). pub const ELF_MACHINE: u16 = 62; diff --git a/tests/guest/osbench/src/fair.rs b/tests/guest/osbench/src/fair.rs index 831c8b63673..01808135d23 100644 --- a/tests/guest/osbench/src/fair.rs +++ b/tests/guest/osbench/src/fair.rs @@ -25,12 +25,20 @@ //! each, whose autogroup is the unit where autogroup applies. A `units` line //! says which unit applied: `3xcgroup` is a cgroup per workload, `1xcgroup` //! one cgroup for all three. +//! +//! `fair..off_fair` is how far the worst-served job's share lay +//! from its entitlement, max-min fairness at equal weights between the +//! grouping's units (`osbench_rules::fair`): the thread under `shared`, the +//! workload under the others. A helper's start costs chunks, so the row has a +//! floor above zero on any system. use std::process::{Command, Stdio}; use std::thread; use std::time::Duration; -use crate::report::{condition, refuse, result}; +use osbench_rules::fair::{entitled_permille, Unit}; + +use crate::report::{condition, refuse, result, unmeasured}; use crate::sys::monotonic_ns; /// The warm-up between the jobs' start and the first window, in which every @@ -223,6 +231,16 @@ pub enum Grouping { } impl Grouping { + /// What the grouping makes the scheduler's unit, where it took. + fn unit(self) -> Unit { + match self { + Grouping::Shared => Unit::Thread, + Grouping::Apart => Unit::Job, + #[cfg(target_os = "linux")] + Grouping::Session => Unit::Job, + } + } + fn name(self) -> &'static str { match self { Grouping::Shared => "shared", @@ -334,8 +352,24 @@ pub fn contend(grouping: Grouping) { return refuse(&bench, "a window in which no job finished a chunk"); } let keys: Vec = jobs.iter().map(|(_, _, _, key)| key.clone()).collect(); - condition(&format!("{bench}.units"), units(grouping, &keys)); + let units = units(grouping, &keys); + condition(&format!("{bench}.units"), &units); let cpus = crate::cpus() as u64; + let off_fair = format!("{bench}.off_fair"); + if grouping.unit() == Unit::Job && !units.starts_with(&format!("{}x", JOBS.len())) { + unmeasured(&off_fair, &format!("the workloads were not each a unit: {units}"), &[]); + } else { + // In `JOBS`' order, as the jobs were started and read. + let entitled = entitled_permille(cpus, &[1, cpus, cpus], grouping.unit()); + let mut off: Vec = (0..WINDOWS) + .map(|w| { + let apart = jobs.iter().zip(&entitled).map(|((_, counts, _, _), entitled)| (counts[w] * 1000 / totals[w]).abs_diff(*entitled)); + apart.max().expect("three jobs") + }) + .collect(); + let entitled = entitled.iter().map(u64::to_string).collect::>().join(","); + result(&off_fair, "permille_off", &mut off, &[("entitled", entitled)]); + } for (kind, counts, helpers, _) in &jobs { let mut permille: Vec = counts.iter().zip(&totals).map(|(c, t)| c * 1000 / t).collect(); permille.sort_unstable(); diff --git a/tests/guest/osbench/src/main.rs b/tests/guest/osbench/src/main.rs index cf184ad1b35..683804789c0 100644 --- a/tests/guest/osbench/src/main.rs +++ b/tests/guest/osbench/src/main.rs @@ -8,6 +8,9 @@ //! under, `osbench_`. Every other command is one this program gives its //! own children. //! +//! A Linux boot that keeps time on a clocksource its vDSO cannot read is +//! refused before any group runs, with exit 3. +//! //! Every result is one line of `key=value` words (`report`): a distribution //! with its sample count, minimum, p50, p90, p99, p99.9, maximum and mean, //! the clock it was taken at, and the conditions it was taken under. Each @@ -21,8 +24,9 @@ //! tail group's diagnostics. Everything else is the same source on both. //! //! On Linux `nvme` and `energy` read what only root may (the raw namespace, -//! the model-specific registers) and say so unprivileged. `README.md` is how -//! the Linux half is built and how a comparison is run. +//! the model-specific registers) and say so unprivileged; `all` starts `nvme` +//! through `sudo` where that asks no password. `README.md` is how the Linux +//! half is built and how a comparison is run. mod arch; mod around; @@ -159,9 +163,19 @@ const GROUPS: [(&str, fn()); 16] = [ /// running nothing else, in a ToyOS boot of its own and as root on Linux. const BY_NAME: [(&str, fn()); 3] = [("lan", lan::suite), ("internet", internet::suite), ("energy", energy::suite)]; +/// Exit 3 on a Linux boot whose clock is unfit to measure by. +fn refuse_unfit_clock() { + #[cfg(target_os = "linux")] + if let Some(why) = sys::unfit_clock() { + eprintln!("osbench: {why}"); + std::process::exit(3); + } +} + /// A group between its first and last lines, after the machine's conditions /// and the clock its CPU starts at. fn group(name: &str, run: fn()) { + refuse_unfit_clock(); report::group(name, "start"); conditions(); around::ran_during(&format!("{name}.cpu.clock"), clock::distribution); @@ -173,10 +187,16 @@ fn group(name: &str, run: fn()) { /// Every group in [`GROUPS`], each a child of its own, so no group runs in a /// process an earlier one shaped: exits 1 if any did not measure. fn all() { + refuse_unfit_clock(); let exe = std::env::current_exe().expect("a path to this program"); let mut failed = Vec::new(); for (name, _) in GROUPS { - let status = std::process::Command::new(&exe).arg(name).status(); + let mut command = std::process::Command::new(&exe); + #[cfg(target_os = "linux")] + if name == "nvme" { + command = sys::as_root(&exe); + } + let status = command.arg(name).status(); match status { Ok(status) if status.success() => {} Ok(status) => failed.push(format!("{name} ({status})")), diff --git a/tests/guest/osbench/src/pingpong.rs b/tests/guest/osbench/src/pingpong.rs index b2a1795f93a..372e897e197 100644 --- a/tests/guest/osbench/src/pingpong.rs +++ b/tests/guest/osbench/src/pingpong.rs @@ -1,13 +1,22 @@ //! A round trip between two threads, on one CPU and across two cores, through //! a pipe and through a futex. +//! +//! A side that finds its futex word already changed never sleeps. On one CPU +//! a round still cannot pass without each side leaving the CPU. Across two +//! cores it can, at a fraction of a wake's cost, so that row keeps only the +//! rounds in which both wakes found their waiter queued in the kernel and +//! reports the others as `.unparked`. Both futex rows say how many +//! such wakes a kept round had, of two (`parks_per_round`). use std::io::{Read, Write}; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{mpsc, Arc}; use std::time::Instant; +use osbench_rules::parked::Woke; + use crate::place; -use crate::report::{refuse, result}; +use crate::report::{refuse, result, unmeasured}; use crate::{ns_since, sys, wake}; const WARMUP: u32 = 500; @@ -19,8 +28,8 @@ const CHECK_EVERY: u32 = 256; /// One side of the exchange: `ping` hands the turn over and waits for it /// back, `pong` waits for it and hands it back. trait Channel: Send + 'static { - fn ping(&mut self, round: u32) -> std::io::Result<()>; - fn pong(&mut self, round: u32) -> std::io::Result<()>; + fn ping(&mut self, round: u32) -> std::io::Result; + fn pong(&mut self, round: u32) -> std::io::Result; } struct PipeEnd { @@ -29,14 +38,16 @@ struct PipeEnd { } impl Channel for PipeEnd { - fn ping(&mut self, _: u32) -> std::io::Result<()> { + fn ping(&mut self, _: u32) -> std::io::Result { self.writer.write_all(&[1])?; - self.reader.read_exact(&mut [0]) + self.reader.read_exact(&mut [0])?; + Ok(None) } - fn pong(&mut self, _: u32) -> std::io::Result<()> { + fn pong(&mut self, _: u32) -> std::io::Result { self.reader.read_exact(&mut [0])?; - self.writer.write_all(&[1]) + self.writer.write_all(&[1])?; + Ok(None) } } @@ -62,18 +73,17 @@ fn wait_for(word: &AtomicU32, round: u32) { } impl Channel for FutexEnd { - fn ping(&mut self, round: u32) -> std::io::Result<()> { + fn ping(&mut self, round: u32) -> std::io::Result { self.0.ping.store(round, Ordering::Release); - sys::futex_wake(&self.0.ping, 1); + let woke = sys::futex_wake(&self.0.ping, 1) == 1; wait_for(&self.0.pong, round); - Ok(()) + Ok(Some(woke)) } - fn pong(&mut self, round: u32) -> std::io::Result<()> { + fn pong(&mut self, round: u32) -> std::io::Result { wait_for(&self.0.ping, round); self.0.pong.store(round, Ordering::Release); - sys::futex_wake(&self.0.pong, 1); - Ok(()) + Ok(Some(sys::futex_wake(&self.0.pong, 1) == 1)) } } @@ -128,15 +138,18 @@ fn run(kind: &str, at: Where, mut pinger: impl Channel, mut ponger: impl Channel let mut seen = Vec::new(); for round in 1..=WARMUP + ROUNDS { let t = Instant::now(); - if let Err(e) = pinger.ping(round) { - let _ = ping_said.send(Err(e.to_string())); - return; - } + let woke = match pinger.ping(round) { + Ok(woke) => woke, + Err(e) => { + let _ = ping_said.send(Err(e.to_string())); + return; + } + }; let took = ns_since(t); if round % CHECK_EVERY == 0 { seen.push(sys::current_cpu()); } else if round > WARMUP { - kept.push(took); + kept.push((round, took, woke)); } } let _ = ping_said.send(Ok((kept, seen))); @@ -159,15 +172,17 @@ fn run(kind: &str, at: Where, mut pinger: impl Channel, mut ponger: impl Channel return; } let mut seen = Vec::new(); + let mut woke = Vec::with_capacity((WARMUP + ROUNDS) as usize); for round in 1..=WARMUP + ROUNDS { - if ponger.pong(round).is_err() { - break; + match ponger.pong(round) { + Ok(found) => woke.push(found), + Err(_) => break, } if round % CHECK_EVERY == 0 { seen.push(sys::current_cpu()); } } - let _ = pong_said.send(seen); + let _ = pong_said.send((seen, woke)); }); let go = pong_side.is_ok(); // A refused send is a side that already ended, which its join reports. @@ -178,22 +193,36 @@ fn run(kind: &str, at: Where, mut pinger: impl Channel, mut ponger: impl Channel Err(why) => return refuse(&bench, format!("the ponger was not placed: {why}")), }; pong_side.thread.join().expect("the ponger panicked"); - let (mut kept, ping_seen) = match ping_heard.recv() { + let (kept, ping_seen) = match ping_heard.recv() { Ok(Ok(measured)) => measured, Ok(Err(why)) => return refuse(&bench, why), Err(_) => return refuse(&bench, "the pinger said nothing"), }; - let pong_seen = pong_heard.recv().unwrap_or_default(); + let (pong_seen, pong_woke) = pong_heard.recv().unwrap_or_default(); + if pong_woke.len() != (WARMUP + ROUNDS) as usize { + return refuse(&bench, format!("the ponger answered {} of {} rounds", pong_woke.len(), WARMUP + ROUNDS)); + } let checks = ping_seen.len().min(pong_seen.len()); let held = ping_seen.iter().zip(&pong_seen).filter(|(&ping, &pong)| ping == ping_cpu && wanted(pong)).count(); - result( - &bench, - "ns", - &mut kept, - &[ - ("ping_cpu", ping_cpu.to_string()), - ("pong_cpu", pong_side.cpu.to_string()), - ("placed_held", format!("{held}/{checks}")), - ], - ); + let mut fields = vec![ + ("ping_cpu", ping_cpu.to_string()), + ("pong_cpu", pong_side.cpu.to_string()), + ("placed_held", format!("{held}/{checks}")), + ]; + if let Some(parks) = osbench_rules::parked::parks(&kept, &pong_woke) { + let hundredths = parks * 100 / kept.len().max(1) as u64; + fields.push(("parks_per_round", format!("{}.{:02}", hundredths / 100, hundredths % 100))); + } + let (mut parked, mut unparked) = match at { + Where::CrossCore => osbench_rules::parked::split(&kept, &pong_woke), + Where::SameCpu => (kept.iter().map(|&(_, took, _)| took).collect(), Vec::new()), + }; + if parked.is_empty() { + unmeasured(&bench, "no round found both sides asleep", &fields); + } else { + result(&bench, "ns", &mut parked, &fields); + } + if !unparked.is_empty() { + result(&format!("{bench}.unparked"), "ns", &mut unparked, &fields); + } } diff --git a/tests/guest/osbench/src/sys/linux.rs b/tests/guest/osbench/src/sys/linux.rs index 4c056d177d8..f3a0c03e80c 100644 --- a/tests/guest/osbench/src/sys/linux.rs +++ b/tests/guest/osbench/src/sys/linux.rs @@ -227,6 +227,38 @@ fn busy_permille(span: Duration) -> Option { (total > 0).then(|| (total - (idle1 - idle0)) * 1000 / total) } +/// `exe` started as root: as itself where this process is root, through +/// `sudo -n` where that asks no password, and otherwise as itself, whose +/// rows then say what they were refused. +pub fn as_root(exe: &std::path::Path) -> std::process::Command { + use std::process::{Command, Stdio}; + // SAFETY: geteuid takes no arguments and cannot fail. + let root = unsafe { libc::geteuid() } == 0; + let quiet = |c: &mut Command| c.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).status(); + if !root && quiet(Command::new("sudo").args(["-n", "true"])).is_ok_and(|s| s.success()) { + let mut sudo = Command::new("sudo"); + sudo.arg("-n").arg("--").arg(exe); + return sudo; + } + Command::new(exe) +} + +/// What every timestamp here is read from. +fn clocksource() -> String { + read("/sys/devices/system/clocksource/clocksource0/current_clocksource").unwrap_or_else(|| "absent".into()) +} + +/// Why this boot is no run, where it is none: its clocksource is one the +/// vDSO cannot read, so each `Instant::now` is a syscall, which every +/// per-operation timing would carry (`README.md`, "The clock"). +pub fn unfit_clock() -> Option { + let clocksource = clocksource(); + let vdso = crate::arch::VDSO_CLOCKSOURCE; + (clocksource != vdso).then(|| { + format!("this boot keeps time on {clocksource}, not {vdso}, so every timestamp would cost a syscall: boot again until current_clocksource is {vdso}") + }) +} + /// What a Linux number depends on, read and never changed. `README.md` says /// which of them a comparison sets. pub fn conditions() { @@ -250,10 +282,7 @@ pub fn conditions() { condition("boost", read("/sys/devices/system/cpu/cpufreq/boost").unwrap_or_else(|| "absent".into())); condition("amd_pstate", read("/sys/devices/system/cpu/amd_pstate/status").unwrap_or_else(|| "absent".into())); condition("platform_profile", read("/sys/firmware/acpi/platform_profile").unwrap_or_else(|| "absent".into())); - // What every timestamp here is read from: one the vDSO cannot read costs - // each `Instant::now` a syscall. - let clocksource = read("/sys/devices/system/clocksource/clocksource0/current_clocksource"); - condition("clocksource", clocksource.unwrap_or_else(|| "absent".into())); + condition("clocksource", clocksource()); for supply in fs::read_dir("/sys/class/power_supply").into_iter().flatten().flatten() { let at = supply.path(); let get = |f: &str| read(&at.join(f).to_string_lossy());