From eea9c428b6eba1cd8aee756a396f8913919a08a7 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sat, 7 Nov 2026 17:16:46 +0100 Subject: [PATCH] STRA-338 size6 t01: kernel_ab main split and use groups --- .../examples/kernel_ab/impl_parser.rs | 3 +- .../examples/kernel_ab/in_magic_types.rs | 4 +- .../examples/kernel_ab/main.rs | 403 ++++-------------- 3 files changed, 92 insertions(+), 318 deletions(-) diff --git a/crates/native-cpu-expert/examples/kernel_ab/impl_parser.rs b/crates/native-cpu-expert/examples/kernel_ab/impl_parser.rs index b564da08..ef00f151 100644 --- a/crates/native-cpu-expert/examples/kernel_ab/impl_parser.rs +++ b/crates/native-cpu-expert/examples/kernel_ab/impl_parser.rs @@ -156,7 +156,8 @@ impl Parser<'_> { } } } - /// After an element: a comma continues (false), the closing byte ends the container (true), anything else is `expected`. + /// After an element: a comma continues (false), the closing byte ends the container (true), anything else is + /// `expected`. fn separator(&mut self, close: u8, expected: &str) -> Result { self.ws(); match self.b.get(self.at) { diff --git a/crates/native-cpu-expert/examples/kernel_ab/in_magic_types.rs b/crates/native-cpu-expert/examples/kernel_ab/in_magic_types.rs index 6070edab..3359f5e9 100644 --- a/crates/native-cpu-expert/examples/kernel_ab/in_magic_types.rs +++ b/crates/native-cpu-expert/examples/kernel_ab/in_magic_types.rs @@ -194,7 +194,9 @@ pub(super) const ALIGN: usize = 4096; /// to Golden Cove /// and AMD Zen 1 to 5) then an xor (1 cycle); the shift runs beside the multiply. pub(super) const CHAIN_STEP_CYCLES: f64 = 4.0; -pub(super) const CLOCK_METHOD: &str = "chain of dependent steps x = (x * k) ^ (x >> 17), 4 cycles per step (3 multiply + 1 xor), 1000000 iterations of 4 steps, ghz = 4 * 4e6 / (elapsed ns); before and after every slice"; +pub(super) const CLOCK_METHOD: &str = + "chain of dependent steps x = (x * k) ^ (x >> 17), 4 cycles per step (3 multiply + 1 xor), 1000000 iterations of \ + 4 steps, ghz = 4 * 4e6 / (elapsed ns); before and after every slice"; pub(super) struct Llc { pub(super) level: u32, diff --git a/crates/native-cpu-expert/examples/kernel_ab/main.rs b/crates/native-cpu-expert/examples/kernel_ab/main.rs index b010b07d..c4c87d6d 100644 --- a/crates/native-cpu-expert/examples/kernel_ab/main.rs +++ b/crates/native-cpu-expert/examples/kernel_ab/main.rs @@ -3,52 +3,42 @@ // warnings of their own. #![deny(warnings, clippy::all)] //! Rust half of the CPU kernel A/B, part 1 (STRA-289, S0.8b): the KAB1 reader, the comparison against the C++ reference -//! outputs and -//! the COMPONENT_KERNEL record writer. The C++ driver (`tools/kernel-ab`, STRA-248) writes a run directory `D`; this -//! example only -//! reads it. C++ and Rust meet through files: no FFI, no unsafe, public API of the crate only, no module in the -//! production library. +//! outputs and the COMPONENT_KERNEL record writer. The C++ driver (`tools/kernel-ab`, STRA-248) writes a run directory +//! `D`; this example only reads it. C++ and Rust meet through files: no FFI, no unsafe, public API of the crate only, +//! no module in the production library. //! //! kernel_ab verify D the reader rule: manifest, every sha256 and length, header-derived -//! sizes, finite -//! FP16 block headers and activations; any mismatch refuses the WHOLE -//! directory +//! sizes, finite FP16 block headers and activations; any mismatch +//! refuses the WHOLE directory //! kernel_ab compare D [--out R] [--pin CPU] [--machine-role NAME] [--trees avx512,avx2] //! verify D, run our trees in fresh processes (`run-tree`), compare with -//! the C++ -//! `.out` files (bitwise on the f32 bit patterns, or the tolerance the -//! manifest case -//! declares with its basis), write `R/case-.rust-.out` and one -//! COMPONENT_KERNEL record per case and tree to `R/records/` +//! the C++ `.out` files (bitwise on the f32 bit patterns, or the +//! tolerance the manifest case declares with its basis), write +//! `R/case-.rust-.out` and one COMPONENT_KERNEL record per +//! case and tree to `R/records/` //! kernel_ab run-tree D --tree T --out R [--pin CPU] [--case ID] -//! internal: one fresh process, one tree (T = avx512 | avx2), all cases or -//! only ID +//! internal: one fresh process, one tree (T = avx512 | avx2), all cases +//! or only ID //! //! Timing is not in `verify` / `compare`. The records written by `compare` carry the fields the schema requires for -//! timing with the -//! explicit NOT_MEASURED marker (see `NOT_MEASURED`), never a measurement. Timing is the second half (STRA-318, -//! S0.8b2): +//! timing with the explicit NOT_MEASURED marker (see `NOT_MEASURED`), never a measurement. Timing is the second half +//! (STRA-318, S0.8b2): //! -//! kernel_ab time --run-dir D --case ID --slices N --pin CPU --out F.json [--slice-ms 60] [--regime -//! both|resident|streamed] -//! [--llc-bytes B] +//! kernel_ab time --run-dir D --case ID --slices N --pin CPU --out F.json [--slice-ms 60] +//! [--regime both|resident|streamed] [--llc-bytes B] //! the Rust twin of `kab time` (tools/kernel-ab, STRA-317): ONE fresh -//! process times ONE -//! case in the resident and the streamed regime on the pinned CPU and -//! writes the KAB1-time -//! JSON (writer "rust"); no statistics +//! process times ONE case in the resident and the streamed regime on the +//! pinned CPU and writes the KAB1-time JSON (writer "rust"); no +//! statistics //! kernel_ab alternate --run-dir D --case ID --rounds N --batch K --pin CPU --schedule FILE [--seed S] [--out R] -//! [--slices N] -//! [--slice-ms MS] [--llc-bytes B] [--machine-role NAME] [--kab PATH] +//! [--slices N] [--slice-ms MS] [--llc-bytes B] [--machine-role NAME] [--kab PATH] //! the alternation runner (ARCHITECT 395f0394 sec 1.3): -//! std::process::Command only. Per -//! round it starts FRESH processes for the cells {cpp, rust} x {avx512, -//! avx2} (each twice, -//! a and b, for the A/A pairs) in the order of a seeded schedule file -//! written before the +//! std::process::Command only. Per round it starts FRESH processes for +//! the cells {cpp, rust} x {avx512, avx2} (each twice, a and b, for the +//! A/A pairs) in the order of a seeded schedule file written before the //! first run, and writes COMPONENT_KERNEL round records. It computes NO -//! statistics: no -//! class, no interval, no A/A verdict (those are S0.10b, STRA-313). +//! statistics: no class, no interval, no A/A verdict (those are S0.10b, +//! STRA-313). //! //! cargo run --release -p leleka-native-cpu-expert --example kernel_ab -- compare D //! cargo test -p leleka-native-cpu-expert --example kernel_ab @@ -70,16 +60,34 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH}; thread_local! { static BUFFERS: Cell = const { Cell::new(0) }; } +/// `time` and `alternate` take flags only (the command line of `kab time`). +fn flag_command(cmd: &str, rest: &[String]) -> ExitCode { + let result = if cmd == "time" { cmd_time(rest) } else { cmd_alternate(rest) }; + result.unwrap_or_else(|e| { + eprintln!("kernel_ab {cmd}: {e}"); + ExitCode::from(1) + }) +} + +/// `verify D`: exit 0 when the directory is accepted, 2 when it is incomplete, 1 when it is refused. +fn verify_command(dir: &Path) -> Result { + match verify_dir(dir) { + Ok(v) => { + println!("OK {} files (manifest sha256 {})", v.file_count, v.manifest_sha256); + Ok(ExitCode::SUCCESS) + } + Err(e) => { + println!("{e}"); + Ok(ExitCode::from(if e.incomplete { 2 } else { 1 })) + } + } +} + fn main() -> ExitCode { let args: Vec = std::env::args().skip(1).collect(); - // `time` and `alternate` take flags only (the command line of `kab time`); the older commands take the directory - // first + // the older commands take the directory first if let Some(cmd @ ("time" | "alternate")) = args.first().map(String::as_str) { - let result = if cmd == "time" { cmd_time(&args[1..]) } else { cmd_alternate(&args[1..]) }; - return result.unwrap_or_else(|e| { - eprintln!("kernel_ab {cmd}: {e}"); - ExitCode::from(1) - }); + return flag_command(cmd, &args[1..]); } let (Some(cmd), Some(dir)) = (args.first(), args.get(1)) else { return usage() }; let dir = Path::new(dir); @@ -91,16 +99,7 @@ fn main() -> ExitCode { } }; let result = match cmd.as_str() { - "verify" => match verify_dir(dir) { - Ok(v) => { - println!("OK {} files (manifest sha256 {})", v.file_count, v.manifest_sha256); - Ok(ExitCode::SUCCESS) - } - Err(e) => { - println!("{e}"); - Ok(ExitCode::from(if e.incomplete { 2 } else { 1 })) - } - }, + "verify" => verify_command(dir), "compare" => cmd_compare(dir, &opts), "run-tree" => match (&opts.tree, &opts.out) { (Some(t), Some(o)) => cmd_run_tree(dir, t, o, opts.pin, opts.case.as_deref()), @@ -114,304 +113,76 @@ fn main() -> ExitCode { }) } -// ------------------------------------------------------------------------------------------------ tests +// -------------------------------------------------------------------------------------------- tests #[cfg(test)] mod tests; mod gate_up_row_fn; #[allow(unused_imports)] -use gate_up_row_fn::civil_from_days; -#[allow(unused_imports)] -use gate_up_row_fn::down_row; -#[allow(unused_imports)] -use gate_up_row_fn::gate_up_row; -#[allow(unused_imports)] -use gate_up_row_fn::is_hex; -#[allow(unused_imports)] -use gate_up_row_fn::is_utc; -#[allow(unused_imports)] -use gate_up_row_fn::le32; -#[allow(unused_imports)] -use gate_up_row_fn::now_utc; -#[allow(unused_imports)] -use gate_up_row_fn::sha_hex; -#[allow(unused_imports)] -use gate_up_row_fn::take_buffer; -#[allow(unused_imports)] -use gate_up_row_fn::utc_of; -#[allow(unused_imports)] -use gate_up_row_fn::valid_id; -#[allow(unused_imports)] -use gate_up_row_fn::words_le; +use gate_up_row_fn::{ + civil_from_days, down_row, gate_up_row, is_hex, is_utc, le32, now_utc, sha_hex, take_buffer, utc_of, valid_id, + words_le, +}; mod impl_parser; #[allow(unused_imports)] use impl_parser::parse_json; mod validate_in_header_fn; #[allow(unused_imports)] -use validate_in_header_fn::case_files; -#[allow(unused_imports)] -use validate_in_header_fn::fp16_finite; -#[allow(unused_imports)] -use validate_in_header_fn::parse_manifest; -#[allow(unused_imports)] -use validate_in_header_fn::validate_in_header; -#[allow(unused_imports)] -use validate_in_header_fn::validate_out_header; +use validate_in_header_fn::{case_files, fp16_finite, parse_manifest, validate_in_header, validate_out_header}; mod read_case_in_fn; #[allow(unused_imports)] -use read_case_in_fn::compare_bits; -#[allow(unused_imports)] -use read_case_in_fn::jstr; -#[allow(unused_imports)] -use read_case_in_fn::read_case_in; -#[allow(unused_imports)] -use read_case_in_fn::read_manifest; -#[allow(unused_imports)] -use read_case_in_fn::read_out; -#[allow(unused_imports)] -use read_case_in_fn::record_json; -#[allow(unused_imports)] -use read_case_in_fn::verify_dir; +use read_case_in_fn::{compare_bits, jstr, read_case_in, read_manifest, read_out, record_json, verify_dir}; mod write_out_fn; #[allow(unused_imports)] -use write_out_fn::cmd_run_tree; -#[allow(unused_imports)] -use write_out_fn::compute_case; -#[allow(unused_imports)] -use write_out_fn::cpu_model_of; -#[allow(unused_imports)] -use write_out_fn::family_of; -#[allow(unused_imports)] -use write_out_fn::host_os; -#[allow(unused_imports)] -use write_out_fn::parse_options; -#[allow(unused_imports)] -use write_out_fn::rustc_info; -#[allow(unused_imports)] -use write_out_fn::sibling_idle; -#[allow(unused_imports)] -use write_out_fn::write_out; +use write_out_fn::{ + cmd_run_tree, compute_case, cpu_model_of, family_of, host_os, parse_options, rustc_info, sibling_idle, write_out, +}; mod cmd_compare_fn; #[allow(unused_imports)] -use cmd_compare_fn::cmd_compare; -#[allow(unused_imports)] -use cmd_compare_fn::core_ghz; -#[allow(unused_imports)] -use cmd_compare_fn::jnum; -#[allow(unused_imports)] -use cmd_compare_fn::median_of; -#[allow(unused_imports)] -use cmd_compare_fn::mono_ns; -#[allow(unused_imports)] -use cmd_compare_fn::parse_cpu_list; -#[allow(unused_imports)] -use cmd_compare_fn::parse_size; -#[allow(unused_imports)] -use cmd_compare_fn::percentile_of; -#[allow(unused_imports)] -use cmd_compare_fn::read_text; +use cmd_compare_fn::{ + cmd_compare, core_ghz, jnum, median_of, mono_ns, parse_cpu_list, parse_size, percentile_of, read_text, +}; mod llc_of_cpu_fn; #[allow(unused_imports)] -use llc_of_cpu_fn::busy_fraction; -#[allow(unused_imports)] -use llc_of_cpu_fn::current_cpu; -#[allow(unused_imports)] -use llc_of_cpu_fn::is_managed_name; -#[allow(unused_imports)] -use llc_of_cpu_fn::json_env; -#[allow(unused_imports)] -use llc_of_cpu_fn::llc_of_cpu; -#[allow(unused_imports)] -use llc_of_cpu_fn::managed_env_now; -#[allow(unused_imports)] -use llc_of_cpu_fn::observed_threads; -#[allow(unused_imports)] -use llc_of_cpu_fn::parse_time_args; -#[allow(unused_imports)] -use llc_of_cpu_fn::proc_stat_cpu; -#[allow(unused_imports)] -use llc_of_cpu_fn::slice_json; -#[allow(unused_imports)] -use llc_of_cpu_fn::time_slice; +use llc_of_cpu_fn::{ + busy_fraction, current_cpu, is_managed_name, json_env, llc_of_cpu, managed_env_now, observed_threads, + parse_time_args, proc_stat_cpu, slice_json, time_slice, +}; mod run_regime_fn; #[allow(unused_imports)] -use run_regime_fn::f32_bits_sha; -#[allow(unused_imports)] -use run_regime_fn::run_regime; +use run_regime_fn::{f32_bits_sha, run_regime}; mod cmd_time_fn; #[allow(unused_imports)] use cmd_time_fn::cmd_time; mod impl_impl; #[allow(unused_imports)] -use impl_impl::cell_env; -#[allow(unused_imports)] -use impl_impl::inside_git_checkout; -#[allow(unused_imports)] -use impl_impl::make_schedule; -#[allow(unused_imports)] -use impl_impl::parse_regime; -#[allow(unused_imports)] -use impl_impl::parse_schedule; -#[allow(unused_imports)] -use impl_impl::procs_for; -#[allow(unused_imports)] -use impl_impl::run_cell; -#[allow(unused_imports)] -use impl_impl::schedule_text; -#[allow(unused_imports)] -use impl_impl::scrubbed_command; -#[allow(unused_imports)] -use impl_impl::splitmix; +use impl_impl::{ + cell_env, inside_git_checkout, make_schedule, parse_regime, parse_schedule, procs_for, run_cell, schedule_text, + scrubbed_command, splitmix, +}; mod describe_cpp_tree_fn; #[allow(unused_imports)] -use describe_cpp_tree_fn::aa_outcome; -#[allow(unused_imports)] -use describe_cpp_tree_fn::describe_cpp_tree; -#[allow(unused_imports)] -use describe_cpp_tree_fn::equality_block; -#[allow(unused_imports)] -use describe_cpp_tree_fn::parse_proc_json; -#[allow(unused_imports)] -use describe_cpp_tree_fn::side_stat; -#[allow(unused_imports)] -use describe_cpp_tree_fn::stat_json; -#[allow(unused_imports)] -use describe_cpp_tree_fn::tool_json; +use describe_cpp_tree_fn::{ + aa_outcome, describe_cpp_tree, equality_block, parse_proc_json, side_stat, stat_json, tool_json, +}; mod round_records_fn; #[allow(unused_imports)] -use round_records_fn::aa_equality_block; -#[allow(unused_imports)] -use round_records_fn::create_new_file; -#[allow(unused_imports)] -use round_records_fn::file_sha; -#[allow(unused_imports)] -use round_records_fn::ours_flags; -#[allow(unused_imports)] -use round_records_fn::parse_alt_args; -#[allow(unused_imports)] -use round_records_fn::round_records; +use round_records_fn::{aa_equality_block, create_new_file, file_sha, ours_flags, parse_alt_args, round_records}; mod cmd_alternate_fn; #[allow(unused_imports)] -use cmd_alternate_fn::cmd_alternate; -#[allow(unused_imports)] -use cmd_alternate_fn::usage; - +use cmd_alternate_fn::{cmd_alternate, usage}; mod in_magic_types; #[allow(unused_imports)] -use in_magic_types::ALIGN; -#[allow(unused_imports)] -use in_magic_types::CHAIN_STEP_CYCLES; -#[allow(unused_imports)] -use in_magic_types::CLOCK_METHOD; -#[allow(unused_imports)] -use in_magic_types::CLOCK_MOVED_FRACTION; -#[allow(unused_imports)] -use in_magic_types::CaseEntry; -#[allow(unused_imports)] -use in_magic_types::CaseIn; -#[allow(unused_imports)] -use in_magic_types::DEFAULT_SLICE_MS; -#[allow(unused_imports)] -use in_magic_types::DOWN; -#[allow(unused_imports)] -use in_magic_types::FF; -#[allow(unused_imports)] -use in_magic_types::FileTable; -#[allow(unused_imports)] -use in_magic_types::Flags; -#[allow(unused_imports)] -use in_magic_types::GATE_UP; -#[allow(unused_imports)] -use in_magic_types::H; -#[allow(unused_imports)] -use in_magic_types::IN_HEADER; -#[allow(unused_imports)] -use in_magic_types::IN_MAGIC; -#[allow(unused_imports)] -use in_magic_types::InHeader; -#[allow(unused_imports)] -use in_magic_types::Json; -#[allow(unused_imports)] -use in_magic_types::Llc; -#[allow(unused_imports)] -use in_magic_types::MAX_CASES; -#[allow(unused_imports)] -use in_magic_types::MAX_FILE_BYTES; -#[allow(unused_imports)] -use in_magic_types::MAX_MANIFEST_BYTES; -#[allow(unused_imports)] -use in_magic_types::MAX_ROWS; -#[allow(unused_imports)] -use in_magic_types::NOT_MEASURED; -#[allow(unused_imports)] -use in_magic_types::OUT_HEADER; -#[allow(unused_imports)] -use in_magic_types::OUT_MAGIC; -#[allow(unused_imports)] -use in_magic_types::Options; -#[allow(unused_imports)] -use in_magic_types::OutRef; -#[allow(unused_imports)] -use in_magic_types::Outcome; -#[allow(unused_imports)] -use in_magic_types::Parser; -#[allow(unused_imports)] -use in_magic_types::Provenance; -#[allow(unused_imports)] -use in_magic_types::RecordInput; -#[allow(unused_imports)] -use in_magic_types::Refusal; -#[allow(unused_imports)] -use in_magic_types::RegimeSel; -#[allow(unused_imports)] -use in_magic_types::SIBLING_BUSY_LIMIT; -#[allow(unused_imports)] -use in_magic_types::TOLERANCE_METRIC; -#[allow(unused_imports)] -use in_magic_types::TREES; -#[allow(unused_imports)] -use in_magic_types::Tolerance; -#[allow(unused_imports)] -use in_magic_types::Verified; +use in_magic_types::{ + ALIGN, CHAIN_STEP_CYCLES, CLOCK_METHOD, CLOCK_MOVED_FRACTION, CaseEntry, CaseIn, DEFAULT_SLICE_MS, DOWN, FF, + FileTable, Flags, GATE_UP, H, IN_HEADER, IN_MAGIC, InHeader, Json, Llc, MAX_CASES, MAX_FILE_BYTES, + MAX_MANIFEST_BYTES, MAX_ROWS, NOT_MEASURED, OUT_HEADER, OUT_MAGIC, Options, OutRef, Outcome, Parser, Provenance, + RecordInput, Refusal, RegimeSel, SIBLING_BUSY_LIMIT, TOLERANCE_METRIC, TREES, Tolerance, Verified, +}; mod time_args_types; #[allow(unused_imports)] -use time_args_types::ALT_FLAGS; -#[allow(unused_imports)] -use time_args_types::AltArgs; -#[allow(unused_imports)] -use time_args_types::Arena; -#[allow(unused_imports)] -use time_args_types::BatchCtx; -#[allow(unused_imports)] -use time_args_types::Expect; -#[allow(unused_imports)] -use time_args_types::Impl; -#[allow(unused_imports)] -use time_args_types::Kind; -#[allow(unused_imports)] -use time_args_types::Numbered; -#[allow(unused_imports)] -use time_args_types::Proc; -#[allow(unused_imports)] -use time_args_types::ProcOut; -#[allow(unused_imports)] -use time_args_types::RegimeRun; -#[allow(unused_imports)] -use time_args_types::RegimeSpec; -#[allow(unused_imports)] -use time_args_types::RegimeStat; -#[allow(unused_imports)] -use time_args_types::SCHEDULE_MAGIC; -#[allow(unused_imports)] -use time_args_types::SideStat; -#[allow(unused_imports)] -use time_args_types::Slice; -#[allow(unused_imports)] -use time_args_types::TIME_FLAGS; -#[allow(unused_imports)] -use time_args_types::TimeArgs; -#[allow(unused_imports)] -use time_args_types::Timed; -#[allow(unused_imports)] -use time_args_types::Tool; +use time_args_types::{ + ALT_FLAGS, AltArgs, Arena, BatchCtx, Expect, Impl, Kind, Numbered, Proc, ProcOut, RegimeRun, RegimeSpec, + RegimeStat, SCHEDULE_MAGIC, SideStat, Slice, TIME_FLAGS, TimeArgs, Timed, Tool, +};