#!/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 w65c02-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``, reading the records as the scenario's row (``scripts/compare_traces.py``). Stdlib only. """ from __future__ import annotations import argparse import json import os import subprocess import sys from concurrent.futures import ThreadPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] TRACES = ROOT / "external" / "traces" NICE = ["nice", "-n", "19"] def one(scenario: Path, python: str, rust_binary: Path, traces: Path) -> tuple[str, bool, str]: name = scenario.stem row = json.loads(scenario.read_text())["row"] ref, ours = traces / f"{name}.ref.jsonl", traces / f"{name}.rust.jsonl" environment = {**os.environ, "PYPY_GC_NURSERY": "1M"} reference = subprocess.run( [*NICE, python, str(ROOT / "scripts" / "reference_trace.py"), str(scenario), "--out", str(ref)], capture_output=True, text=True, env=environment, ) if reference.returncode: return name, False, "reference failed: " + reference.stderr.strip()[-400:] 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( [*NICE, python, str(ROOT / "scripts" / "compare_traces.py"), row, str(ref), str(ours)], capture_output=True, text=True, env=environment, ) with ref.open() as handle: lines = sum(1 for _ in handle) text = compared.stdout.strip() if compared.returncode == 0: ref.unlink() ours.unlink() return name, True, f"{text} ({lines:,} boundaries, row {row})" return name, False, (text or compared.stderr.strip())[: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=4) parser.add_argument("--python", default=os.environ.get("REFERENCE_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) args = parser.parse_args() if not args.python: raise SystemExit("no reference interpreter: set REFERENCE_PYTHON (scripts/env.sh does)") args.traces.mkdir(parents=True, exist_ok=True) rust_binary = args.rust or ROOT / "target" / "release" / "trace" if args.rust is None: build = subprocess.run([*NICE, "cargo", "build", "--release", "-q", "--manifest-path", str(ROOT / "Cargo.toml")]) if build.returncode: return 1 failed = 0 with ThreadPoolExecutor(min(args.jobs, 4)) as pool: for name, ok, text in pool.map(lambda s: one(s, args.python, rust_binary, args.traces), 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())