|
|
@@ -0,0 +1,322 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+"""Run device-backed TD resource/timing comparisons for 6400 A/B candidates."""
|
|
|
+
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
+import argparse
|
|
|
+from datetime import datetime, timezone
|
|
|
+import hashlib
|
|
|
+import json
|
|
|
+import os
|
|
|
+from pathlib import Path
|
|
|
+import re
|
|
|
+import shutil
|
|
|
+import signal
|
|
|
+import subprocess
|
|
|
+import sys
|
|
|
+import time
|
|
|
+
|
|
|
+
|
|
|
+ROOT = Path(__file__).resolve().parents[1]
|
|
|
+CANDIDATE_SOURCE = ROOT / "tests/resource/architecture_ab_candidates.v"
|
|
|
+PIN_CONSTRAINTS = ROOT / "constraints/prod2u_6400_architecture_ab.adc"
|
|
|
+TIMING_CONSTRAINTS = ROOT / "constraints/prod2u_6400_architecture_ab.sdc"
|
|
|
+TD_SCRIPT = ROOT / "td/resource/prod2u_6400_architecture_ab.tcl"
|
|
|
+
|
|
|
+TRIALS = {
|
|
|
+ "fsm_encoding": ("ab_fsm_binary", "ab_fsm_onehot"),
|
|
|
+ "bmc_tx_serializer": ("ab_bmc_tx_dedicated", "ab_bmc_tx_shared"),
|
|
|
+ "array_storage": ("ab_store_ff", "ab_store_eram"),
|
|
|
+ "pi_form": ("ab_pi_positional", "ab_pi_incremental"),
|
|
|
+ "safety_compare": ("ab_safety_parallel", "ab_safety_sequential"),
|
|
|
+ "atomic_commit": ("ab_commit_copy", "ab_commit_bank"),
|
|
|
+ "crc_datapath": ("ab_crc_independent", "ab_crc_shared"),
|
|
|
+ "snapshot_materialization": ("ab_snapshot_copy", "ab_snapshot_stream"),
|
|
|
+}
|
|
|
+CANDIDATES = tuple(top for pair in TRIALS.values() for top in pair)
|
|
|
+
|
|
|
+
|
|
|
+def sha256(path: Path) -> str:
|
|
|
+ digest = hashlib.sha256()
|
|
|
+ with path.open("rb") as stream:
|
|
|
+ for chunk in iter(lambda: stream.read(1024 * 1024), b""):
|
|
|
+ digest.update(chunk)
|
|
|
+ return digest.hexdigest()
|
|
|
+
|
|
|
+
|
|
|
+def git(*args: str) -> str:
|
|
|
+ return subprocess.check_output(["git", *args], cwd=ROOT, text=True).strip()
|
|
|
+
|
|
|
+
|
|
|
+def first_int(text: str, patterns: tuple[str, ...]) -> int | None:
|
|
|
+ for pattern in patterns:
|
|
|
+ match = re.search(pattern, text, re.IGNORECASE | re.MULTILINE)
|
|
|
+ if match:
|
|
|
+ return int(match.group(1))
|
|
|
+ return None
|
|
|
+
|
|
|
+
|
|
|
+def first_float(text: str, patterns: tuple[str, ...]) -> float | None:
|
|
|
+ for pattern in patterns:
|
|
|
+ match = re.search(pattern, text, re.IGNORECASE | re.MULTILINE)
|
|
|
+ if match:
|
|
|
+ return float(match.group(1))
|
|
|
+ return None
|
|
|
+
|
|
|
+
|
|
|
+def run_bounded(
|
|
|
+ command: list[str], *, cwd: Path, env: dict[str, str], log: Path, timeout: int
|
|
|
+) -> dict[str, object]:
|
|
|
+ started = time.monotonic()
|
|
|
+ with log.open("wb") as stream:
|
|
|
+ process = subprocess.Popen(
|
|
|
+ command,
|
|
|
+ cwd=cwd,
|
|
|
+ env=env,
|
|
|
+ stdout=stream,
|
|
|
+ stderr=subprocess.STDOUT,
|
|
|
+ start_new_session=(os.name == "posix"),
|
|
|
+ )
|
|
|
+ timed_out = False
|
|
|
+ try:
|
|
|
+ return_code = process.wait(timeout=timeout)
|
|
|
+ except subprocess.TimeoutExpired:
|
|
|
+ timed_out = True
|
|
|
+ if os.name == "posix":
|
|
|
+ try:
|
|
|
+ os.killpg(process.pid, signal.SIGKILL)
|
|
|
+ except ProcessLookupError:
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ process.kill()
|
|
|
+ return_code = process.wait()
|
|
|
+ return {
|
|
|
+ "command": command,
|
|
|
+ "cwd": str(cwd),
|
|
|
+ "exit_code": return_code,
|
|
|
+ "timed_out": timed_out,
|
|
|
+ "timeout_seconds": timeout,
|
|
|
+ "duration_seconds": round(time.monotonic() - started, 3),
|
|
|
+ "log": str(log),
|
|
|
+ "log_sha256": sha256(log),
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def parse_candidate(top: str, root: Path) -> dict[str, object]:
|
|
|
+ area_paths = [root / f"{top}_gate.area", root / f"{top}_pr.area"]
|
|
|
+ timing_path = root / f"{top}_pr.timing"
|
|
|
+ log_path = root / "td.log"
|
|
|
+ area = "\n".join(
|
|
|
+ path.read_text(encoding="utf-8", errors="replace")
|
|
|
+ for path in area_paths
|
|
|
+ if path.is_file()
|
|
|
+ )
|
|
|
+ timing = timing_path.read_text(encoding="utf-8", errors="replace") if timing_path.is_file() else ""
|
|
|
+ log_text = log_path.read_text(encoding="utf-8", errors="replace") if log_path.is_file() else ""
|
|
|
+ slice_matches = re.findall(r"(\d+)\s+mslices,\s+(\d+)\s+lslices", log_text, re.IGNORECASE)
|
|
|
+ mslice = int(slice_matches[-1][0]) if slice_matches else None
|
|
|
+ lslice = int(slice_matches[-1][1]) if slice_matches else None
|
|
|
+ metrics = {
|
|
|
+ "total_luts": first_int(area, (r"^#Total_luts\s+(\d+)",)),
|
|
|
+ "lut": first_int(area, (r"^#lut\s+(\d+)",)),
|
|
|
+ "registers": first_int(area, (r"^#reg\s+(\d+)",)),
|
|
|
+ "dsp": first_int(area, (r"^#dsp\s+(\d+)",)),
|
|
|
+ "eram_bram": first_int(area, (r"^#(?:eram|bram)\s+(\d+)",)),
|
|
|
+ "mslice": mslice,
|
|
|
+ "lslice": lslice,
|
|
|
+ "packed_slices": mslice + lslice if mslice is not None and lslice is not None else None,
|
|
|
+ "packed_slices_derivation": "mslice_plus_lslice",
|
|
|
+ "fmax_mhz": first_float(
|
|
|
+ timing,
|
|
|
+ (r"Fmax[^\n]*?([0-9]+(?:\.[0-9]+)?)\s*MHz", r"([0-9]+(?:\.[0-9]+)?)\s*MHz"),
|
|
|
+ ),
|
|
|
+ "setup_slack_ns": first_float(
|
|
|
+ timing, (r"(?:Setup|WNS)[^\n]*?(-?[0-9]+(?:\.[0-9]+)?)\s*ns",)
|
|
|
+ ),
|
|
|
+ "hold_slack_ns": first_float(
|
|
|
+ timing, (r"(?:Hold|HWNS)[^\n]*?(-?[0-9]+(?:\.[0-9]+)?)\s*ns",)
|
|
|
+ ),
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def warning_audit(log_path: Path) -> dict[str, object]:
|
|
|
+ lines = log_path.read_text(encoding="utf-8", errors="replace").splitlines()
|
|
|
+ noteworthy = [
|
|
|
+ line.strip()
|
|
|
+ for line in lines
|
|
|
+ if re.search(r"\b(?:warning|error|fatal|unrouted|violation)\b", line, re.IGNORECASE)
|
|
|
+ ]
|
|
|
+ blocking = [
|
|
|
+ line
|
|
|
+ for line in noteworthy
|
|
|
+ if re.search(r"\b(?:error|fatal|unrouted|timing violation)\b", line, re.IGNORECASE)
|
|
|
+ ]
|
|
|
+ return {
|
|
|
+ "status": "FAIL" if blocking else "PASS",
|
|
|
+ "noteworthy_count": len(noteworthy),
|
|
|
+ "blocking_count": len(blocking),
|
|
|
+ "noteworthy": noteworthy[:200],
|
|
|
+ "blocking": blocking[:100],
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ "metrics": metrics,
|
|
|
+ "area_reports": [
|
|
|
+ {"path": str(path), "sha256": sha256(path)} for path in area_paths if path.is_file()
|
|
|
+ ],
|
|
|
+ "timing_report": (
|
|
|
+ {"path": str(timing_path), "sha256": sha256(timing_path)}
|
|
|
+ if timing_path.is_file()
|
|
|
+ else None
|
|
|
+ ),
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def main(argv: list[str] | None = None) -> int:
|
|
|
+ parser = argparse.ArgumentParser()
|
|
|
+ parser.add_argument("--run-root", type=Path)
|
|
|
+ parser.add_argument("--timeout-seconds", type=int, default=900)
|
|
|
+ args = parser.parse_args(argv)
|
|
|
+
|
|
|
+ td = shutil.which("td2026")
|
|
|
+ local_td = ROOT / "tools/td2026"
|
|
|
+ if td is None and local_td.is_file():
|
|
|
+ td = str(local_td)
|
|
|
+ if td is None:
|
|
|
+ print("TD_AB_MISSING: td2026 is not on PATH", file=sys.stderr)
|
|
|
+ return 4
|
|
|
+ dirty = git("status", "--porcelain")
|
|
|
+ if dirty:
|
|
|
+ print("TD_AB_BLOCKED: source worktree must be clean", file=sys.stderr)
|
|
|
+ print(dirty, file=sys.stderr)
|
|
|
+ return 3
|
|
|
+ if args.timeout_seconds <= 0:
|
|
|
+ raise SystemExit("--timeout-seconds must be positive")
|
|
|
+
|
|
|
+ commit = git("rev-parse", "HEAD")
|
|
|
+ tree = git("rev-parse", "HEAD^{tree}")
|
|
|
+ stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
|
|
+ run_id = f"6400-architecture-td-ab-{stamp}-{commit[:8]}"
|
|
|
+ run_root = args.run_root or Path(
|
|
|
+ os.environ.get("PROD2U_TD_AB_RUN_ROOT", "/data/prod2u/cpld-runs")
|
|
|
+ ) / run_id
|
|
|
+ run_root.mkdir(parents=True, exist_ok=False)
|
|
|
+
|
|
|
+ version = subprocess.run([td, "-version"], capture_output=True, text=True, check=False)
|
|
|
+ version_text = (version.stdout + version.stderr).strip()
|
|
|
+ resolved_td = Path(td).resolve()
|
|
|
+ vendor_td = Path(os.environ.get("PRODU2_TD_ROOT", "/data/anlogic/TD_Release_2026.1_SP2_NL")) / "bin/td"
|
|
|
+ env_base = os.environ.copy()
|
|
|
+ env_base["PROD2U_SOURCE_ROOT"] = str(ROOT)
|
|
|
+
|
|
|
+ candidate_records: dict[str, dict[str, object]] = {}
|
|
|
+ status = "PASS"
|
|
|
+ for top in CANDIDATES:
|
|
|
+ candidate_root = run_root / top
|
|
|
+ candidate_root.mkdir()
|
|
|
+ env = env_base.copy()
|
|
|
+ env["PROD2U_AB_TOP"] = top
|
|
|
+ run = run_bounded(
|
|
|
+ [td, str(TD_SCRIPT)],
|
|
|
+ cwd=candidate_root,
|
|
|
+ env=env,
|
|
|
+ log=candidate_root / "td.log",
|
|
|
+ timeout=args.timeout_seconds,
|
|
|
+ )
|
|
|
+ outputs = {
|
|
|
+ "gate_db": candidate_root / f"{top}_gate.db",
|
|
|
+ "pr_db": candidate_root / f"{top}_pr.db",
|
|
|
+ }
|
|
|
+ missing = [name for name, path in outputs.items() if not path.is_file()]
|
|
|
+ parsed = parse_candidate(top, candidate_root)
|
|
|
+ audit = warning_audit(candidate_root / "td.log")
|
|
|
+ metrics = parsed["metrics"]
|
|
|
+ required_metrics = (
|
|
|
+ "total_luts", "lut", "registers", "dsp", "eram_bram", "mslice",
|
|
|
+ "lslice", "packed_slices", "fmax_mhz", "setup_slack_ns", "hold_slack_ns",
|
|
|
+ )
|
|
|
+ missing_metrics = [name for name in required_metrics if metrics[name] is None]
|
|
|
+ timing_pass = bool(
|
|
|
+ not missing_metrics
|
|
|
+ and metrics["fmax_mhz"] >= 25.0
|
|
|
+ and metrics["setup_slack_ns"] >= 0.0
|
|
|
+ and metrics["hold_slack_ns"] >= 0.0
|
|
|
+ )
|
|
|
+ candidate_status = "PASS" if (
|
|
|
+ run["exit_code"] == 0
|
|
|
+ and not run["timed_out"]
|
|
|
+ and not missing
|
|
|
+ and not missing_metrics
|
|
|
+ and timing_pass
|
|
|
+ and audit["status"] == "PASS"
|
|
|
+ ) else "FAIL"
|
|
|
+ if candidate_status != "PASS":
|
|
|
+ status = "FAIL"
|
|
|
+ candidate_records[top] = {
|
|
|
+ "status": candidate_status,
|
|
|
+ "run": run,
|
|
|
+ "missing_outputs": missing,
|
|
|
+ "missing_metrics": missing_metrics,
|
|
|
+ "timing_pass_25mhz": timing_pass,
|
|
|
+ "warning_audit": audit,
|
|
|
+ "outputs": {
|
|
|
+ name: {"path": str(path), "sha256": sha256(path)}
|
|
|
+ for name, path in outputs.items()
|
|
|
+ if path.is_file()
|
|
|
+ },
|
|
|
+ **parsed,
|
|
|
+ }
|
|
|
+ print(
|
|
|
+ f"{top}: {candidate_status} exit={run['exit_code']} "
|
|
|
+ f"timeout={run['timed_out']} duration_s={run['duration_seconds']}",
|
|
|
+ flush=True,
|
|
|
+ )
|
|
|
+
|
|
|
+ trial_records = {
|
|
|
+ name: {
|
|
|
+ "candidates": pair,
|
|
|
+ "results": {top: candidate_records[top] for top in pair},
|
|
|
+ }
|
|
|
+ for name, pair in TRIALS.items()
|
|
|
+ }
|
|
|
+ clean_after = not bool(git("status", "--porcelain"))
|
|
|
+ if not clean_after:
|
|
|
+ status = "FAIL"
|
|
|
+ receipt = {
|
|
|
+ "schema": "prod2u-6400-architecture-td-ab-v1",
|
|
|
+ "run_id": run_id,
|
|
|
+ "generated_at": datetime.now(timezone.utc).isoformat(),
|
|
|
+ "status": status,
|
|
|
+ "commit": commit,
|
|
|
+ "tree": tree,
|
|
|
+ "branch": git("branch", "--show-current"),
|
|
|
+ "clean_before": not bool(dirty),
|
|
|
+ "clean_after": clean_after,
|
|
|
+ "command": "python3 tools/run_6400_architecture_td_ab.py",
|
|
|
+ "inputs": {
|
|
|
+ str(path.relative_to(ROOT)): sha256(path)
|
|
|
+ for path in (CANDIDATE_SOURCE, PIN_CONSTRAINTS, TIMING_CONSTRAINTS, TD_SCRIPT)
|
|
|
+ },
|
|
|
+ "tool": {
|
|
|
+ "wrapper": str(resolved_td),
|
|
|
+ "wrapper_sha256": sha256(resolved_td),
|
|
|
+ "vendor_binary": str(vendor_td),
|
|
|
+ "vendor_binary_sha256": sha256(vendor_td) if vendor_td.is_file() else None,
|
|
|
+ "version_probe_exit": version.returncode,
|
|
|
+ "version": version_text,
|
|
|
+ "container_image_id": os.environ.get("PROD2U_CONTAINER_IMAGE_ID"),
|
|
|
+ "container_digest": os.environ.get("PROD2U_CONTAINER_DIGEST"),
|
|
|
+ },
|
|
|
+ "trials": trial_records,
|
|
|
+ "hardware": "NOT RUN",
|
|
|
+ "jtag": "NOT RUN",
|
|
|
+ "com": "NOT RUN",
|
|
|
+ }
|
|
|
+ receipt_path = run_root / "architecture-td-ab-receipt.json"
|
|
|
+ receipt_path.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
|
+ print(receipt_path)
|
|
|
+ print(f"RESOURCE_ARCHITECTURE_TD_AB_{status} trials={len(TRIALS)} candidates={len(CANDIDATES)}")
|
|
|
+ return 0 if status == "PASS" else 1
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ raise SystemExit(main())
|