git mv of the files and of the module directories next to them, mod declarations, #[path] attributes and the paths that name the modules, rewritten by scripts/dev/rename_mechanical.py from a reviewed name table. Two files lost one to two lines so the 400-line limit holds again (qsa_prompt_index, original_verify_expert_probe). limits-baseline.json: test fn_lines 0 -> 1 is --allow-increase for crates/deploy builtin_profiles.rs:149 (37 lines, the speed-vs-strata session's file, not touched here); the prod params census falls 108 -> 1 (w_launch, extern C ABI). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
189 lines
8.8 KiB
Rust
189 lines
8.8 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, ExpertBytes, ExpertFormats, 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) }; }
|
|
|
|
/// `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<ExitCode, String> {
|
|
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<String> = std::env::args().skip(1).collect();
|
|
// the older commands take the directory first
|
|
if let Some(cmd @ ("time" | "alternate")) = args.first().map(String::as_str) {
|
|
return flag_command(cmd, &args[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" => 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()),
|
|
_ => 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;
|
|
#[allow(unused_imports)]
|
|
use gate_up_row::{
|
|
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 parser;
|
|
#[allow(unused_imports)]
|
|
use parser::parse_json;
|
|
mod validate_in_header;
|
|
#[allow(unused_imports)]
|
|
use validate_in_header::{case_files, fp16_finite, parse_manifest, validate_in_header, validate_out_header};
|
|
mod read_case_in;
|
|
#[allow(unused_imports)]
|
|
use read_case_in::{compare_bits, jstr, read_case_in, read_manifest, read_out, record_json, verify_dir};
|
|
mod write_out;
|
|
#[allow(unused_imports)]
|
|
use write_out::{
|
|
cmd_run_tree, compute_case, cpu_model_of, family_of, host_os, parse_options, rustc_info, sibling_idle, write_out,
|
|
};
|
|
mod cmd_compare;
|
|
#[allow(unused_imports)]
|
|
use cmd_compare::{
|
|
cmd_compare, core_ghz, jnum, median_of, mono_ns, parse_cpu_list, parse_size, percentile_of, read_text,
|
|
};
|
|
mod llc_of_cpu;
|
|
#[allow(unused_imports)]
|
|
use llc_of_cpu::{
|
|
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;
|
|
#[allow(unused_imports)]
|
|
use run_regime::{f32_bits_sha, run_regime};
|
|
mod cmd_time;
|
|
#[allow(unused_imports)]
|
|
use cmd_time::cmd_time;
|
|
mod regime_schedule;
|
|
#[allow(unused_imports)]
|
|
use regime_schedule::{
|
|
ScheduleHeader, cell_env, inside_git_checkout, make_schedule, parse_regime, parse_schedule, procs_for, run_cell,
|
|
schedule_text, scrubbed_command, splitmix,
|
|
};
|
|
mod describe_cpp_tree;
|
|
#[allow(unused_imports)]
|
|
use describe_cpp_tree::{
|
|
aa_outcome, describe_cpp_tree, equality_block, parse_proc_json, side_stat, stat_json, tool_json,
|
|
};
|
|
mod round_records;
|
|
#[allow(unused_imports)]
|
|
use round_records::{aa_equality_block, create_new_file, file_sha, ours_flags, parse_alt_args, round_records};
|
|
mod cmd_alternate;
|
|
#[allow(unused_imports)]
|
|
use cmd_alternate::{cmd_alternate, usage};
|
|
mod in_magic_types;
|
|
#[allow(unused_imports)]
|
|
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, AltArgs, Arena, BatchCtx, Expect, Impl, Kind, Numbered, Proc, ProcOut, RegimeRun, RegimeSpec,
|
|
RegimeStat, SCHEDULE_MAGIC, SideStat, Slice, TIME_FLAGS, TimeArgs, Timed, Tool,
|
|
};
|