Files
Triforce-Source/tests/test_pool_span.py
ruckus7382sfgsgdf 34b3683441 Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:32:48 -06:00

69 lines
2.4 KiB
Python

"""A repeated pool-end lookup in the same guest pool skips the lock.
Storage-buffer entries ask where their pool ends. The locked lookup was about 8% of
replay time before buffer binds grew a cache; the pool-end query still took the lock.
python -I -B tests/test_pool_span.py [--cxx g++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def before_lock(fn, name):
lock = fn.index("ParLock")
head = fn[:lock]
if "guest_span_cached(" not in head:
raise SystemExit(f"{name} takes the pool lock before checking the span cache")
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cxx", default="g++")
a = p.parse_args()
source = (ROOT / "gpu" / "vk" / "vk_memory.cpp").read_text(encoding="utf-8")
fn = block(source, "bool guest_span_covers(")
before_lock(block(source, "uint64_t gpu_pool_end("), "gpu_pool_end")
before_lock(block(source, "uint64_t gpu_pool_start("), "gpu_pool_start")
sync = block(source, "void memory_sync(")
swap = sync.index("guest_pools.swap(next);")
if "guest_pools_gen" not in sync[swap:swap + 200]:
raise SystemExit("refreshing the guest pools does not drop the span cache")
fixture = f'''#include <cstdint>
#include <cstdio>
#include <cstdlib>
#define CHECK(x) do {{ if (!(x)) {{ std::fprintf(stderr, "FAIL:%d %s\\n", __LINE__, #x); std::exit(1); }} }} while (0)
{fn}
int main() {{
CHECK(guest_span_covers(150, 100, 200, 4, 4));
CHECK(guest_span_covers(100, 100, 200, 4, 4));
CHECK(!guest_span_covers(200, 100, 200, 4, 4));
CHECK(!guest_span_covers(150, 100, 200, 4, 5));
CHECK(!guest_span_covers(150, 200, 100, 4, 4));
CHECK(!guest_span_covers(150, 100, 100, 1, 1));
std::printf("PASS: a pool span is reused only inside that generation\\n");
}}
'''
with tempfile.TemporaryDirectory(prefix="pool-span-") as tmp:
cpp = Path(tmp) / "fixture.cpp"
exe = Path(tmp) / "fixture"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([a.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", str(cpp), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()