repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

perf(ci): budgets pinned per platform inside the CI image; jobs install mold and valgrind; runner keeps ASLR

bb32860e4b5613126e7bbd493b35dc7402996c76

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (10)

+100 / −31 text lines

Diff layout

.ci/check.kdl

Modified

+1 / −0 text lines

55 branch "trunk"
66 }
77 }
8 run "apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq mold"
89 run "rustup component add rustfmt clippy"
910 run "cargo run -p xtask -- ci"

.ci/e2e.kdl

Modified

+1 / −0 text lines

55 branch "trunk"
66 }
77 }
8 run "apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq mold"
89 run "rustup component add rustfmt clippy"
910 run "cargo run -p xtask -- bootstrap"
1011 run "cargo run -p xtask -- e2e"

.ci/nightly.kdl

Modified

+1 / −0 text lines

33 trigger {
44 schedule "0 3 * * *"
55 }
6 run "apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq mold valgrind"
67 run "rustup component add rustfmt clippy llvm-tools"
78 run "cargo run -p xtask -- bootstrap"
89 run "cargo run -p xtask -- mutants || echo 'missed mutants recorded in the report'"

.ci/perf.kdl

Modified

+1 / −1 text lines

55 branch "trunk"
66 }
77 }
8 run "apt-get update && apt-get install -y valgrind"
8 run "apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq mold valgrind"
99 run "cargo run -p xtask -- bootstrap"
1010 run "cargo run -p xtask -- perf"

.ci/report.kdl

Modified

+1 / −0 text lines

11job "report" {
22 image "docker.io/library/rust:latest"
3 run "apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq mold valgrind"
34 run "rustup component add rustfmt clippy llvm-tools"
45 run "cargo run -p xtask -- bootstrap"
56 run "cargo run -p xtask -- report"

.system/RULES.md

Modified

+1 / −1 text lines

6565 - conformance kits: providers, plugin guests, frontends; fixtures in, vocabulary out.
6666 - golden corpus: checked-in sessions; replay and trace byte-identical; regenerated only with a frame-version bump.
6767 - bounds: allocation counts and type sizes; exact pins; tighten only.
68 - instruction budgets: callgrind instruction counts per hot path (`gungraun`), pinned in `smith-bench/budgets.toml` with a ±2 % band; a regression fails, an improvement fails until re-pinned; re-pins are deliberate commits, toolchain bumps the expected trigger.
68 - instruction budgets: callgrind instruction counts per hot path (`gungraun`), pinned in `smith-bench/budgets.toml` with a ±2 % band for the platform (toolchain and libc) named in the pins, the CI image; other platforms report without gating; a regression fails, an improvement fails until re-pinned; re-pins are deliberate commits, toolchain bumps the expected trigger.
6969 - wall clock: `criterion` and `hyperfine` compare two builds on one machine in one session; numbers are never committed and never gate.
7070 - mutation: all first-party crates nightly; missed mutants become issues.
7171 - coverage: `cargo-llvm-cov` over every tier; reported per push as a diff, never a percentage gate; uncovered spec sentences are the backlog.

.system/plans/SMH-PLAN-2_O9OFR0-benchmarking/index.md

Modified

+1 / −1 text lines

1919## Decisions
2121- Three signals, each where it is strong: allocation counts (exact, tests), instruction counts (`gungraun` over callgrind, exact per toolchain, CI gate), wall clock (`criterion`, `hyperfine`, local A/B, never committed).
22- Budgets are pinned per bench in `smith-bench/budgets.toml`; the gate is a ±2 % band on `Ir`; both directions fail so pins stay honest; re-pin is a deliberate commit.
22- Budgets are pinned per bench in `smith-bench/budgets.toml` for one platform (toolchain and libc, the CI image); the gate is a ±2 % band on `Ir` on that platform and a report elsewhere (glibc 2.41 versus 2.44 moved counts by 3 to 23 %); both directions fail so pins stay honest; re-pin is a deliberate commit made inside the CI image (`docker run … rust:latest … cargo x perf --pin`).
2323- One workspace member `smith-bench` owns every bench; it depends on any workspace crate; nothing depends on it; it is a default member so `cargo x check` type-checks benches (`cargo test --benches` is not run).
2424- `gungraun-runner` is a build tool installed by xtask at a pinned version matching the crate.
2525- Wall-clock spec targets are informational rows in the report bundle, never a gate.

.system/research/SMH-RESEARCH-UPU3A0HO-benchmarking/index.md

Modified

+8 / −0 text lines

110110- [gungraun guide](https://gungraun.github.io/gungraun/latest/html/): default environment clearing, entry points, binary benchmarks, command-line arguments, machine-readable output.
111111- `gungraun-runner` 0.19.4 source, `src/runner/args.rs`: environment clearing preserves `LD_PRELOAD` and `LD_LIBRARY_PATH`.
113## Platform dependence, measured
115The same tree on the same rustc under Debian glibc 2.41 (the `rust:latest` image) versus CachyOS glibc 2.44: SSE decoders +9 to +11 %, `handle_line::ping` +23 %, `smith --help` −4.6 %, `eval --mock` +3.6 %, frame encode +3.3 %; the rest within 1.5 %.
116Startup counts include the dynamic loader; every bench includes libc `malloc` and `memcpy`, whose implementations differ per glibc release.
117Consequence: pins carry the platform (`# Pinned with <rustc> | glibc <v>`); the gate applies only on that platform, other hosts only report; pins are taken inside the CI image.
118Inside the container `setarch` cannot disable ASLR, so the runner always gets `--allow-aslr=true`; with cache simulation off the counts stayed within band across repeated container runs.
119The CI image also lacks `mold`, which `.cargo/config.toml` requires; every job installs `mold` (and `valgrind` where callgrind runs) first.

smith-bench/budgets.toml

Modified

+15 / −15 text lines

11# Callgrind instruction budgets (Ir) per bench, gated within ±2 % by `cargo x perf`.
22# Written by `cargo x perf --pin`; a re-pin is a deliberate commit that says why.
3# Pinned with rustc 1.98.1 (48a229cea 2026-09-01).
3# Pinned with rustc 1.98.1 (48a229cea 2026-09-01) | glibc 2.41.
55[budgets]
6"instructions::agent::run_turn::history_1000_masked" = { ir = 11270600 }
7"instructions::providers::sse::anthropic" = { ir = 109026 }
8"instructions::providers::sse::gemini" = { ir = 47234 }
9"instructions::providers::sse::openai" = { ir = 81236 }
10"instructions::rpc::handle_line::ping" = { ir = 6885 }
11"instructions::session::canonical_json::nested" = { ir = 1138903 }
12"instructions::session::decode::linear_1000" = { ir = 11056510 }
13"instructions::session::decode::tool_calls_1000" = { ir = 10284262 }
14"instructions::session::encode::linear_1000" = { ir = 5736448 }
15"instructions::session::encode::tool_calls_1000" = { ir = 5133175 }
16"instructions::session::fork_at::linear_1000" = { ir = 232844 }
17"instructions::session::from_frames::linear_1000" = { ir = 1392615 }
18"startup::cli::smith::eval_mock" = { ir = 1156689 }
19"startup::cli::smith::help" = { ir = 741164 }
6"instructions::agent::run_turn::history_1000_masked" = { ir = 11384108 }
7"instructions::providers::sse::anthropic" = { ir = 119257 }
8"instructions::providers::sse::gemini" = { ir = 52389 }
9"instructions::providers::sse::openai" = { ir = 89745 }
10"instructions::rpc::handle_line::ping" = { ir = 8428 }
11"instructions::session::canonical_json::nested" = { ir = 1122300 }
12"instructions::session::decode::linear_1000" = { ir = 11152010 }
13"instructions::session::decode::tool_calls_1000" = { ir = 10396988 }
14"instructions::session::encode::linear_1000" = { ir = 5674199 }
15"instructions::session::encode::tool_calls_1000" = { ir = 5302087 }
16"instructions::session::fork_at::linear_1000" = { ir = 233231 }
17"instructions::session::from_frames::linear_1000" = { ir = 1388558 }
18"startup::cli::smith::eval_mock" = { ir = 1196624 }
19"startup::cli::smith::help" = { ir = 707441 }

xtask/src/main.rs

Modified

+70 / −13 text lines

255255 Err(message) => return fail(&format!("{message}\n")),
256256 };
257257 let complete = passthrough.is_empty();
258 let toolchain = toolchain();
259 let written = write_perf_report(&measured, &pins, complete, &toolchain);
258 let platform = platform();
259 let written = write_perf_report(&measured, &pins, complete, &platform);
260260 if written != ExitCode::SUCCESS {
261261 return written;
262262 }
263263 if repin {
264264 let mut pinned = if complete { BTreeMap::new() } else { pins };
265265 pinned.extend(measured);
266 if fs::write(BUDGETS, render_budgets(&pinned, &toolchain)).is_err() {
266 if fs::write(BUDGETS, render_budgets(&pinned, &platform)).is_err() {
267267 return fail(&format!("cannot write {BUDGETS}\n"));
268268 }
269269 note(&format!(
272272 ));
273273 return ExitCode::SUCCESS;
274274 }
275 // Instruction counts belong to one toolchain and libc: the gate runs
276 // where the pins were taken (the CI image); elsewhere the run reports.
277 let pinned_on = fs::read_to_string(BUDGETS)
278 .ok()
279 .and_then(|text| pinned_platform(&text));
280 if pinned_on.as_deref() != Some(platform.as_str()) {
281 note(&format!(
282 "perf: pins belong to `{}`, this run is `{platform}`; counts reported in target/report/perf.md, gate skipped\n",
283 pinned_on.as_deref().unwrap_or("no platform")
284 ));
285 return ExitCode::SUCCESS;
286 }
275287 let violations = budget_violations(&measured, &pins, complete);
276288 if violations.is_empty() {
277289 note(&format!(
337349 command.args(["--bench", bench]);
338350 }
339351 command
340 .args(["--", "--save-summary=json", "--output-format=json"])
352 // ASLR stays on: containers refuse the personality switch, and
353 // cache simulation is off, so instruction counts are unaffected.
354 .args([
355 "--",
356 "--save-summary=json",
357 "--output-format=json",
358 "--allow-aslr=true",
359 ])
341360 .args(passthrough)
342361 .env("SMITH_BIN", smith)
343362 .stderr(std::process::Stdio::inherit());
429448 Some((bench, ir.parse().ok()?))
432fn render_budgets(pins: &BTreeMap<String, u64>, toolchain: &str) -> String {
451/// The platform named in a budgets file header, if any.
452fn pinned_platform(text: &str) -> Option<String> {
453 text.lines()
454 .find_map(|line| line.trim().strip_prefix("# Pinned with "))
455 .map(|rest| rest.trim_end_matches('.').to_string())
456}
458fn render_budgets(pins: &BTreeMap<String, u64>, platform: &str) -> String {
433459 use std::fmt::Write as _;
434460 let mut out = format!(
435461 "# Callgrind instruction budgets (Ir) per bench, gated within ±{BAND_PERCENT} % by `cargo x perf`.\n\
436462 # Written by `cargo x perf --pin`; a re-pin is a deliberate commit that says why.\n\
437 # Pinned with {toolchain}.\n\n[budgets]\n"
463 # Pinned with {platform}.\n\n[budgets]\n"
438464 );
439465 for (bench, ir) in pins {
440466 let _ = writeln!(out, "\"{bench}\" = {{ ir = {ir} }}");
491517 measured: &BTreeMap<String, u64>,
492518 pins: &BTreeMap<String, u64>,
493519 complete: bool,
494 toolchain: &str,
520 platform: &str,
495521) -> ExitCode {
496522 use std::fmt::Write as _;
497523 let mut table = format!(
498524 "Callgrind instruction counts (`Ir`) against `{BUDGETS}`, band ±{BAND_PERCENT} %.\n\n\
499 Toolchain: `{toolchain}`.\n\n| bench | ir | pin | delta |\n|---|---|---|---|\n"
525 Platform: `{platform}`.\n\n| bench | ir | pin | delta |\n|---|---|---|---|\n"
500526 );
501527 for (bench, ir) in measured {
502528 let (pin, change) = pins.get(bench).map_or_else(
520546 ExitCode::SUCCESS
523/// `rustc --version` of the toolchain building the benches.
524fn toolchain() -> String {
525 Command::new("rustc")
549/// What instruction counts depend on: the toolchain building the benches
550/// and the libc they link, e.g. `rustc 1.98.1 (…) | glibc 2.41`.
551fn platform() -> String {
552 format!("{} | {}", tool_first_line("rustc"), libc_version())
553}
555fn tool_first_line(program: &str) -> String {
556 Command::new(program)
526557 .arg("--version")
527558 .output()
528559 .ok()
529560 .filter(|output| output.status.success())
561 .and_then(|output| {
562 String::from_utf8_lossy(&output.stdout)
563 .lines()
564 .next()
565 .map(str::to_string)
566 })
567 .unwrap_or_else(|| format!("{program} version unavailable"))
568}
570/// `glibc <version>` from `ldd --version`, whose first line ends with the
571/// version on every glibc distribution; other libcs report as unknown.
572fn libc_version() -> String {
573 let line = tool_first_line("ldd");
574 line.rsplit(' ')
575 .next()
576 .filter(|last| last.starts_with(|c: char| c.is_ascii_digit()))
530577 .map_or_else(
531 || "rustc version unavailable".to_string(),
532 |output| String::from_utf8_lossy(&output.stdout).trim().to_string(),
578 || "libc unknown".to_string(),
579 |version| format!("glibc {version}"),
533580 )
15521599 const FIRST_RUN: &str = r#"{"baselines":[null,null],"details":"(linear(1000))","function_name":"encode","id":"linear_1000","kind":"LibraryBenchmark","module_path":"instructions::session::encode","profiles":[{"summaries":{"parts":[{"details":{"Left":{"pid":1,"thread":1}},"metrics_summary":{"Callgrind":{"Ir":{"diffs":null,"metrics":{"Left":{"Int":999}}}}}}],"total":{"regressions":[],"summary":{"Callgrind":{"Ir":{"diffs":null,"metrics":{"Left":{"Int":5736448}}}}}}},"tool":"Callgrind"}],"version":"6"}"#;
1601 #[test]
1602 fn pinned_platform_reads_the_header() {
1603 let text = "# x\n# Pinned with rustc 1.98.1 (a 2026) | glibc 2.41.\n[budgets]\n";
1604 assert_eq!(
1605 super::pinned_platform(text).as_deref(),
1606 Some("rustc 1.98.1 (a 2026) | glibc 2.41")
1607 );
1608 assert_eq!(super::pinned_platform("[budgets]\n"), None);
1609 }
15541611 #[test]
15551612 fn bench_ir_reads_the_total_of_first_and_repeated_runs() {
15561613 assert_eq!(