#!/usr/bin/env python3 """Offline tests for the 7 GPU PMU binary oracle scripts and exec() slice contracts.""" from __future__ import annotations import ast import json import pathlib import sys import tempfile import unittest ROOT = pathlib.Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / "tools")) import _gpu_pmu_oracle_check as helper # noqa: E402 ALL_PMU_FILES = ( "tools/pmu_original_environment.py", "tools/check_gpu_pmu_fbq_binary.py", "tools/check_gpu_pmu_level_binary.py", "tools/check_gpu_pmu_ls_start_binary.py", "tools/check_gpu_pmu_service_binary.py", "tools/check_gpu_pmu_unbind_binary.py", "tools/check_gpu_pmu_vaspace_binary.py", ) CHECK_SCRIPTS = ALL_PMU_FILES[1:] class GpuPmuBinaryOraclesTests(unittest.TestCase): def test_no_elidable_asserts_and_check_wired(self) -> None: for rel in ALL_PMU_FILES: src = (ROOT / rel).read_text() tree = ast.parse(src, filename=rel) asserts = [node.lineno for node in ast.walk(tree) if isinstance(node, ast.Assert)] self.assertEqual(asserts, [], f"{rel} still contains assert at lines {asserts}") for rel in CHECK_SCRIPTS: src = (ROOT / rel).read_text() self.assertIn("--check", src, f"{rel} missing --check flag") self.assertIn("write_or_check_text", src, f"{rel} missing write_or_check_text") self.assertIn("write_or_check_json", src, f"{rel} missing write_or_check_json") def test_exec_slice_contracts_compile_cleanly(self) -> None: # 1. pmu_original_environment.py slices check_gpu_acr_load_binary.py acr_src = (ROOT / "tools/check_gpu_acr_load_binary.py").read_text() acr_marker = 'if "--traced" in sys.argv:' self.assertIn(acr_marker, acr_src) compile(acr_src[: acr_src.index(acr_marker)], "acr_slice", "exec") # 2. check_gpu_pmu_fbq_binary.py slices check_gpu_pmu_ls_start_binary.py ls_src = (ROOT / "tools/check_gpu_pmu_ls_start_binary.py").read_text() ls_begin = "HERE = Path(__file__).resolve()" ls_end_fbq = "# ------------------------------------------------------------------ variations" self.assertIn(ls_begin, ls_src) self.assertIn(ls_end_fbq, ls_src) compile(ls_src[ls_src.index(ls_begin) : ls_src.index(ls_end_fbq)], "ls_fbq_slice", "exec") # 3. check_gpu_pmu_unbind_binary.py slices check_gpu_pmu_ls_start_binary.py ls_end_unbind = 'if "--traced" in sys.argv:' self.assertIn(ls_end_unbind, ls_src) compile(ls_src[: ls_src.index(ls_end_unbind)], "ls_unbind_slice", "exec") # 4. check_gpu_pmu_level_binary.py & service slice check_gpu_pmu_fbq_binary.py fbq_src = (ROOT / "tools/check_gpu_pmu_fbq_binary.py").read_text() fbq_end = "# ------------------------------------------------------------------- INIT" self.assertIn(fbq_end, fbq_src) compile(fbq_src[: fbq_src.index(fbq_end)], "fbq_slice", "exec") # 5. check_gpu_pmu_vaspace_binary.py slices check_gpu_falcon_vaspace_binary.py falcon_src = (ROOT / "tools/check_gpu_falcon_vaspace_binary.py").read_text() f_update = "HANDLERS.update({ # noqa: F821" f_end = "# ------------------------------------------------- the traced V100 case" self.assertEqual(falcon_src.count(f_update), 1) self.assertEqual(falcon_src.count(f_end), 1) compile(falcon_src[: falcon_src.index(f_update)], "falcon_part1", "exec") compile(falcon_src[falcon_src.index(f_update) : falcon_src.index(f_end)], "falcon_part2", "exec") def test_require_and_check_helpers_under_optimize(self) -> None: helper.require(True) with self.assertRaises(AssertionError) as ctx: helper.require(False, "pmu failure") self.assertIn("pmu failure", str(ctx.exception)) with tempfile.TemporaryDirectory() as tmp: txt = pathlib.Path(tmp) / "sub" / "oracle.txt" rep = pathlib.Path(tmp) / "rep" / "report.json" with self.assertRaises(SystemExit): helper.write_or_check_text(txt, "abc\n", check=True) self.assertFalse(txt.parent.exists()) helper.write_or_check_text(txt, "abc\n", check=False) helper.write_or_check_text(txt, "abc\n", check=True) with self.assertRaises(SystemExit): helper.write_or_check_text(txt, "xyz\n", check=True) self.assertEqual(txt.read_text(), "abc\n") helper.write_or_check_json(rep, {"ok": False, "n": 0}, check=False) helper.write_or_check_json(rep, {"n": 0, "ok": False}, check=True) with self.assertRaises(SystemExit): helper.write_or_check_json(rep, {"ok": 0, "n": 0}, check=True) self.assertIs(json.loads(rep.read_text())["ok"], False) if __name__ == "__main__": unittest.main()