#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Rungs 1 and 4: run each scenario on both cores and compare the traces. lockstep.py SCENARIO.json [...] [--jobs N] [--python REFERENCE_PYTHON] Each scenario runs on huc6280-python (``scripts/reference_trace.py`` on the reference's interpreter) and on this crate (``target/release/trace``), into ``external/traces/``; the two traces are compared with the reference's own ``first_trace_divergence`` (``scripts/compare_traces.py``). A scenario file may hold a list of scenarios; their records follow one another. Stdlib only. """ from __future__ import annotations import argparse import subprocess import sys from concurrent.futures import ThreadPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] TRACES = ROOT / "external" / "traces" def one(scenario: Path, python: str, rust_binary: Path, traces: Path, cache: Path | None) -> tuple[str, bool, str]: name = scenario.stem ref, ours = traces / f"{name}.ref.jsonl", traces / f"{name}.rust.jsonl" nice = ["nice"] if cache is not None: # The reference's trace of a scenario does not change: keep it between runs. ref = cache / f"{name}.ref.jsonl" if cache is None or not ref.exists(): partial = ref.with_suffix(".partial") reference = subprocess.run( [*nice, python, str(ROOT / "scripts" / "reference_trace.py"), str(scenario), "--out", str(partial)], capture_output=True, text=True, ) if reference.returncode: return name, False, "reference failed: " + reference.stderr.strip()[-400:] partial.replace(ref) rust = subprocess.run( [*nice, str(rust_binary), str(scenario), "--out", str(ours)], capture_output=True, text=True, ) if rust.returncode: return name, False, "rust failed: " + rust.stderr.strip()[-400:] compared = subprocess.run( [python, str(ROOT / "scripts" / "compare_traces.py"), str(ref), str(ours)], capture_output=True, text=True, ) lines = sum(1 for _ in ref.open()) text = compared.stdout.strip() if compared.returncode == 0: if cache is None: ref.unlink() ours.unlink() return name, True, f"{text} ({lines:,} boundaries)" return name, False, text[:2000] def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("scenarios", nargs="+", type=Path) parser.add_argument("--jobs", type=int, default=8) parser.add_argument("--python", default=str(ROOT / "external" / "huc6280-python" / ".venv" / "bin" / "python")) parser.add_argument("--rust", type=Path, help="a trace binary to judge instead of this tree's") parser.add_argument("--traces", type=Path, default=TRACES) parser.add_argument("--reference-cache", type=Path, help="keep the reference's traces here and reuse them") args = parser.parse_args() args.traces.mkdir(parents=True, exist_ok=True) if args.reference_cache: args.reference_cache.mkdir(parents=True, exist_ok=True) rust_binary = args.rust or ROOT / "target" / "release" / "trace" if args.rust is None: build = subprocess.run(["cargo", "build", "--release", "-q", "--manifest-path", str(ROOT / "Cargo.toml")]) if build.returncode: return 1 failed = 0 with ThreadPoolExecutor(args.jobs) as pool: for name, ok, text in pool.map(lambda s: one(s, args.python, rust_binary, args.traces, args.reference_cache), args.scenarios): print(f"{name}: {text}", flush=True) failed += not ok print(f"{len(args.scenarios) - failed} of {len(args.scenarios)} scenarios identical") return 1 if failed else 0 if __name__ == "__main__": sys.exit(main())