Files
leleka/crates/native-cpu-expert/examples/kernel_ab/main.rs
T

398 lines
14 KiB
Rust

#![cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
// This file is its own lint gate: `cargo clippy -- -D warnings` also lints the other workspace crates, which carry 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.
//!
//! 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
//! 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-<id>.rust-<tree>.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
//!
//! 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):
//!
//! 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
//! 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]
//! 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
//! 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).
//!
//! cargo run --release -p leleka-native-cpu-expert --example kernel_ab -- compare D
//! cargo test -p leleka-native-cpu-expert --example kernel_ab
use leleka_native_cpu_expert::{
BorrowedExpert, BorrowedGroup, DirectCpuPool, DownFormat, GateUpFormat, PreparedJob, cpu_kernel_report,
rust_topology,
};
use sha2::{Digest, Sha256};
use std::cell::Cell;
use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsString;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode};
use std::sync::OnceLock;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
thread_local! { static BUFFERS: Cell<usize> = const { Cell::new(0) }; }
fn main() -> ExitCode {
let args: Vec<String> = 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
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)
});
}
let (Some(cmd), Some(dir)) = (args.first(), args.get(1)) else { return usage() };
let dir = Path::new(dir);
let opts = match parse_options(&args[2..]) {
Ok(o) => o,
Err(e) => {
eprintln!("kernel_ab: {e}");
return usage();
}
};
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 }))
}
},
"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()),
_ => return usage(),
},
_ => return usage(),
};
result.unwrap_or_else(|e| {
eprintln!("kernel_ab: {e}");
ExitCode::from(1)
})
}
// ------------------------------------------------------------------------------------------------ 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;
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;
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;
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;
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;
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;
mod run_regime_fn;
#[allow(unused_imports)]
use run_regime_fn::f32_bits_sha;
#[allow(unused_imports)]
use run_regime_fn::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;
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;
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;
mod cmd_alternate_fn;
#[allow(unused_imports)]
use cmd_alternate_fn::cmd_alternate;
#[allow(unused_imports)]
use cmd_alternate_fn::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;
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;