"""The captures' range lists: one union for every caller each time a list changes, its sorts as merges of sorted runs (gpu/vk/capunion.h; vk_memory.cpp capture_union_take_locked, refresh_pool_ranges_locked; no game, no GPU). (Triforce, 11-26.) capture_make and capture_ranges_merged each kept a union of the captures' lists of their own, so the Presentation thread built it twice at every change of a list (three sorts, two clips, the left-out ranges published, each time), and the pool table's look kept a map of the pools' first captures that only RX_VK_CAPTURE_EAGER reads. The lists decide which pages a capture takes: a wrong one is stale pages in a mirror, so they must be what they were, byte for byte. This compiles the header with vk_memory.cpp's own ranges_merged, ranges_clip and capture_union_locked (cut from the source, the four lists, the left-out ranges, their lock and version and RX_VK_CAP_ONLY_GPU's mode stubbed), so that a list added to the old union, or a change of its clip or join, fails here until capunion.h follows: over random import, pool, texture storage and small pool lists (sorted, unsorted, overlapping, touching, with duplicates, empty), with RX_VK_CAP_ONLY_GPU 0, 1 and 2, the union and the left-out ranges must be the same vectors, and with 0 neither touches the left-out ranges; merged_runs must be ranges_merged whatever the runs and the cuts, and must not sort runs that arrive sorted; the shared union is built once a version whoever asks, and again when the version moves; the pools' first captures are kept only with RX_VK_CAPTURE_EAGER or with the switch off; the switch's values. And reads the source: both callers take the union through capture_union_take_locked behind their own version, which copies the shared one; the old capture_union_locked is still there for RX_VK_CAP_UNION_SHARED=0, the lab knob nocapunion and the check; the left-out ranges are published where the shared union is built; the pool table's look uses the runs and skips the map by the rule; the counters are in the capture's 10 s line; the switches table has the row. python -I -B tests/test_capunion.py [--cxx clang++] """ import argparse import subprocess import sys import tempfile from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) # (python -I leaves the check's own folder off the path) from srccut import block as cut_block # noqa: E402 ROOT = Path(__file__).resolve().parents[1] VK = ROOT / "gpu" / "vk" RUN_ALL_ARGS = ["--cxx", "{cxx}"] def check(ok, why): if not ok: raise SystemExit("FAIL: " + why) def block(text, marker): check(marker in text, "vk_memory.cpp has no " + marker) start = text.index(marker) end = text.index("{", start + len(marker) - 1) + 1 depth = 1 while depth: depth += (text[end] == "{") - (text[end] == "}") end += 1 return text[start:end] PROGRAM = r""" #include #include #include #include "capunion.h" using capunion::Ranges; static int fails = 0; static long compared = 0; static uint64_t rnd_state = 0x853C49E6748FEA9Bull; static uint64_t rnd() { rnd_state ^= rnd_state << 13, rnd_state ^= rnd_state >> 7, rnd_state ^= rnd_state << 17; return rnd_state; } static void fail(const char* what) { if (fails++ < 20) std::printf("FAIL %s\n", what); } // ---- the lists as vk_memory.cpp makes them: its own ranges_merged, ranges_clip and capture_union_locked ---- static int g_only_gpu = 1; static int cap_only_gpu() { return g_only_gpu; } static Ranges cap_ranges, all_pool_ranges, tex_cap_ranges, pool_cap_ranges, cap_excl; static std::mutex cap_excl_mu; static std::atomic cap_excl_ver{1}; @CUT@ /// capture_union_locked over these lists and RX_VK_CAP_ONLY_GPU's mode: the union; the left-out ranges in excl, and /// whether they were published (their version moved) static Ranges vk_union(const Ranges& imports, const Ranges& pools, const Ranges& tex, const Ranges& small, int only_gpu, Ranges& excl, bool& published) { cap_ranges = imports, all_pool_ranges = pools, tex_cap_ranges = tex, pool_cap_ranges = small, g_only_gpu = only_gpu; cap_excl = excl; const uint64_t ver = cap_excl_ver.load(); Ranges uni = capture_union_locked(); published = cap_excl_ver.load() != ver; excl = cap_excl; return uni; } // ---- lists ---- static const uint64_t P = 4096; /// kind 0: sorted, apart; 1: sorted, some touching or overlapping; 2: as 1 with duplicates; 3: unsorted static Ranges some(int n, int kind, uint64_t base, uint64_t stride) { Ranges r; uint64_t at = base + (rnd() % 8) * P; for (int i = 0; i < n; i++) { const uint64_t len = (1 + rnd() % 12) * P; r.push_back({at, at + len}); if (kind >= 2 && rnd() % 5 == 0) r.push_back(r.back()); if (kind >= 1 && rnd() % 4 == 0) at += (rnd() % 3) * (len / 2); // (the next one inside this, at its end, or the same) else at += len + (kind >= 1 && rnd() % 4 == 0 ? 0 : (1 + rnd() % stride) * P); } if (kind == 3) for (size_t i = r.size(); i > 1; i--) std::swap(r[i - 1], r[rnd() % i]); return r; } static void one_union(const Ranges& imports, const Ranges& pools, const Ranges& tex, const Ranges& small, const char* what) { for (int only_gpu = 0; only_gpu <= 2; only_gpu++) { Ranges excl_old{{1, 2}}; bool pub_old = false; const Ranges uni_old = vk_union(imports, pools, tex, small, only_gpu, excl_old, pub_old); capunion::Built b; b.excl = {{7, 9}}; // (left alone when nothing is published) uint64_t sorts = 0; capunion::build(imports, pools, tex, small, only_gpu, b, &sorts); compared++; if (b.uni != uni_old) fail(what), std::printf(" the union differs (RX_VK_CAP_ONLY_GPU=%d): %zu ranges, %zu before\n", only_gpu, b.uni.size(), uni_old.size()); if (b.excl_made != pub_old) fail(what), std::printf(" the left-out ranges are %spublished (RX_VK_CAP_ONLY_GPU=%d)\n", b.excl_made ? "" : "not ", only_gpu); if (pub_old && b.excl != excl_old) fail(what), std::printf(" the left-out ranges differ (RX_VK_CAP_ONLY_GPU=%d)\n", only_gpu); if (!pub_old && excl_old != Ranges{{1, 2}}) fail(what), std::printf(" capture_union_locked touched the left-out ranges with RX_VK_CAP_ONLY_GPU=0\n"); if (!pub_old && b.excl != Ranges{{7, 9}}) fail(what), std::printf(" the left-out ranges were touched with RX_VK_CAP_ONLY_GPU=0\n"); } } int main() { // merged_runs is ranges_merged, whatever the runs and wherever the cuts for (int t = 0; t < 4000; t++) { Ranges v; size_t cut[2] = {0, 0}; for (int run = 0; run < 3; run++) { const Ranges r = some((int)(rnd() % (t % 7 == 0 ? 200 : 12)), (int)(rnd() % 4), (rnd() % 64) * P, 1 + rnd() % 9); v.insert(v.end(), r.begin(), r.end()); if (run < 2) cut[run] = v.size(); } if (t % 5 == 0) cut[0] = rnd() % (v.size() + 3), cut[1] = rnd() % (v.size() + 3); // (any cuts: past the end, out of order) compared++; if (capunion::merged_runs(v, cut[0], cut[1], nullptr) != ranges_merged(v)) fail("merged_runs is not ranges_merged"); Ranges j = v; std::sort(j.begin(), j.end()); capunion::join(j); if (j != ranges_merged(v)) fail("join is not ranges_merged's join"); } { // sorted runs are not sorted again; an unsorted one is, and counted Ranges a = some(300, 0, 0, 4), b = some(300, 1, 16 * P, 4), c = some(300, 2, 5 * P, 4); std::sort(b.begin(), b.end()), std::sort(c.begin(), c.end()); // (overlapping and duplicates, in order) Ranges v = a; v.insert(v.end(), b.begin(), b.end()); v.insert(v.end(), c.begin(), c.end()); uint64_t sorts = 0; if (capunion::merged_runs(v, a.size(), a.size() + b.size(), &sorts) != ranges_merged(v)) fail("three sorted runs: not ranges_merged"); if (sorts != 0) fail("three sorted runs were sorted again"); std::swap(v[a.size() + 3], v[a.size() + 200]); sorts = 0; if (capunion::merged_runs(v, a.size(), a.size() + b.size(), &sorts) != ranges_merged(v)) fail("an unsorted run: not ranges_merged"); if (sorts != 1) fail("one unsorted run of three: not one sort counted"); } // the union and the left-out ranges one_union({}, {}, {}, {}, "no list"); one_union({{16 * P, 32 * P}}, {}, {}, {}, "an import and no pool"); one_union({{16 * P, 32 * P}, {64 * P, 96 * P}}, {{18 * P, 20 * P}, {20 * P, 24 * P}, {70 * P, 71 * P}}, {{30 * P, 40 * P}}, {{18 * P, 20 * P}, {200 * P, 201 * P}}, "pools inside imports"); one_union({{64 * P, 96 * P}, {16 * P, 32 * P}, {16 * P, 32 * P}}, {{0, 300 * P}}, {{30 * P, 40 * P}, {40 * P, 41 * P}}, {}, "imports unsorted, with a duplicate"); for (int t = 0; t < 6000; t++) { const int big = t % 11 == 0 ? 400 : 14; // (as the game has them: imports are chunks, the pools small ranges inside and outside them, the texture storage and // the small pools sorted and merged; and every list in any state) const int kind = t % 3 == 0 ? (int)(rnd() % 4) : -1; const Ranges imports = some((int)(rnd() % 8), kind < 0 ? (int)(rnd() % 2) : (int)(rnd() % 4), 0, 40); Ranges pools = some((int)(rnd() % big), kind < 0 ? 0 : kind, (rnd() % 16) * P, 6); Ranges tex = some((int)(rnd() % big), kind < 0 ? 0 : (int)(rnd() % 4), (rnd() % 32) * P, 6); Ranges small = some((int)(rnd() % big), kind < 0 ? 0 : (int)(rnd() % 4), (rnd() % 16) * P, 6); if (kind < 0) pools = ranges_merged(pools), tex = ranges_merged(tex), small = ranges_merged(small); one_union(imports, pools, tex, small, "random lists"); } { // the game's shape: every list sorted and merged: no run sorted the slow way Ranges imports = ranges_merged(some(40, 1, 0, 400)), pools = ranges_merged(some(2000, 1, 0, 6)), tex = ranges_merged(some(300, 1, 0, 40)), small = ranges_merged(some(1500, 1, 0, 8)); capunion::Built b; uint64_t sorts = 0; capunion::build(imports, pools, tex, small, 1, b, &sorts); if (sorts != 0) fail("lists that arrive sorted were sorted again"); Ranges excl; bool pub = false; if (b.uni != vk_union(imports, pools, tex, small, 1, excl, pub) || b.excl != excl) fail("the game's shape: the lists differ"); } { // one build a version, whoever asks; again when the version moves capunion::Shared s; int builds = 0; uint64_t ver = 1; // (cap_ranges_ver starts at 1: nothing is shared before the first build) const auto make = [&](Ranges& uni) { builds++, uni = {{ver * P, (ver + 1) * P}}; }; bool built = false; const Ranges& a = capunion::get(s, ver, make, &built); if (!built || builds != 1 || a != Ranges{{P, 2 * P}}) fail("the first caller of a version does not build"); built = true; const Ranges& b = capunion::get(s, ver, make, &built); if (built || builds != 1 || &a != &b || b != Ranges{{P, 2 * P}}) fail("the second caller of a version builds again"); for (int i = 0; i < 50; i++) capunion::get(s, ver, make, nullptr); if (builds != 1) fail("a version built more than once"); ver = 2; const Ranges& c = capunion::get(s, ver, make, &built); if (!built || builds != 2 || c != Ranges{{2 * P, 3 * P}}) fail("a moved version does not build again"); ver = 1; // (any other version, not only a higher one) capunion::get(s, ver, make, &built); if (!built || builds != 3) fail("a version that differs does not build again"); } // the switch, the knob, the pools' first captures if (capunion::mode(nullptr) != capunion::ON || capunion::mode("") != capunion::ON || capunion::mode("1") != capunion::ON || capunion::mode("0") != capunion::OFF || capunion::mode("check") != capunion::CHECK || capunion::mode("00") != capunion::ON) fail("RX_VK_CAP_UNION_SHARED: on unless 0, check"); if (!capunion::shared(capunion::ON, false) || !capunion::shared(capunion::CHECK, false) || capunion::shared(capunion::OFF, false) || capunion::shared(capunion::ON, true) || capunion::shared(capunion::CHECK, true) || capunion::shared(capunion::OFF, true)) fail("the shared union is on with the switch on or check, and off in the lab knob's phases"); if (capunion::first_seen_needed(0, true) || !capunion::first_seen_needed(0, false) || !capunion::first_seen_needed(1, true) || !capunion::first_seen_needed(30, false)) fail("the pools' first captures: kept with RX_VK_CAPTURE_EAGER, or as before with the switch off"); if (fails) std::printf("capunion: FAILED (%d of %ld)\n", fails, compared); else std::printf("capunion: %ld lists, every one as before\n", compared); return fails != 0; } """ def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--cxx", default="clang++") args = parser.parse_args() check((VK / "capunion.h").exists(), "gpu/vk/capunion.h is missing") mem = (VK / "vk_memory.cpp").read_text(encoding="utf-8") check('#include "capunion.h"' in mem, "vk_memory.cpp does not include capunion.h") # the switch and the knob mode = block(mem, "int cap_union_mode() {") check('getenv("RX_VK_CAP_UNION_SHARED")' in mode and "static const int v" in mode and "capunion::mode(" in mode and "if (m != capunion::ON)" in mode and "logf(" in mode, "RX_VK_CAP_UNION_SHARED is not read once, and logged when not the default") shared = block(mem, "bool cap_union_shared() {") check('return capunion::shared(cap_union_mode(), lab_ab::on("nocapunion"));' in shared, "the shared union is not the switch's with the lab knob nocapunion") # the old union is still there, unchanged, and only the taker and the check call it old = block(mem, "std::vector> capture_union_locked() {") for line in ("imp = ranges_merged(std::move(imp));", "keep = ranges_merged(std::move(keep));", "std::vector> excl = ranges_clip(imp, keep, false);", "cap_excl.swap(excl);", "if (cap_only_gpu() == 1) imp = ranges_clip(imp, all_pool_ranges, true);", "return ranges_merged(std::move(imp));"): check(line in old, "capture_union_locked (the path of RX_VK_CAP_UNION_SHARED=0) changed: " + line) take = block(mem, "void capture_union_take_locked(std::vector>& mine) {") check(mem.count("capture_union_locked()") == 3 and take.count("capture_union_locked()") == 2, "capture_union_locked is called elsewhere than by capture_union_take_locked (the switch off, and the check)") check("if (!cap_union_shared()) {\n mine = capture_union_locked();" in take, "with the switch off a caller does not build its own union, as before") check("capunion::get(cap_union, cap_ranges_ver," in take, "the shared union is not kept by cap_ranges_ver") check("capunion::build(cap_ranges, all_pool_ranges, tex_cap_ranges, pool_cap_ranges, cap_only_gpu()," in take, "the shared union is not built from the four lists and RX_VK_CAP_ONLY_GPU's mode") check("mine = u;" in take and "return u" not in take and "const std::vector" not in take.split("{")[0], "a caller does not get its own copy of the shared union (the shared one is read under cap_ranges_mu only)") # (the left-out ranges: published where the union is built, with their version, under their lock; nowhere else in the taker) pub = take[take.index("if (b.excl_made) {"):] pub = pub[:pub.index("}") + 1] check("std::lock_guard lk(cap_excl_mu);" in pub and "cap_excl.swap(b.excl);" in pub and "cap_excl_ver.fetch_add(1, std::memory_order_release);" in pub and take.count("cap_excl_ver.fetch_add") == 1 and take.count("cap_excl.swap(") == 1, "the left-out ranges are not published once, where the shared union is built") check(take.index("if (b.excl_made) {") < take.index("mine = u;"), "the left-out ranges are published after a caller has the union") check(take.count("cap_union_differs(") == 4 and "capunion::CHECK" in take, "the check does not count its differences (the union and the left-out ranges, built and reused)") differs = block(mem, "void cap_union_differs(const char* what, size_t now, size_t before) {") check("st_cu_diff.fetch_add(1, std::memory_order_relaxed) < 20" in differs and "logf(" in differs, "a difference is not counted, and the first 20 logged") # the callers: their own version still gates, and the union comes through the taker make = block(mem, "std::unique_ptr capture_make(bool behind) {") check("if (ver != cap_ranges_ver) {" in make and "ver = cap_ranges_ver;\n capture_union_take_locked(mirrored);" in make and "texr = tex_cap_ranges;" in make, "capture_make does not take the shared union behind its own version (with its texture ranges)") merged = block(mem, "const std::vector>& capture_ranges_merged() {") check("static thread_local std::vector> merged;" in merged and "ver = cap_ranges_ver;\n capture_union_take_locked(merged);" in merged and "return merged;" in merged, "capture_ranges_merged does not copy the shared union into the calling thread's own list") born = block(mem, "void cap_pool_born_now(uint64_t gpu, uint64_t size) {") check("cap_excl = ranges_clip(cap_excl, {{lo, hi}}, false);" in born and "cap_ranges_ver++;" in born, "cap_pool_born_now no longer takes a new pool out of the left-out ranges with the lists' version moved") # the pool table's look refresh = block(mem, "void refresh_pool_ranges_locked() {") check("const bool cu = cap_union_shared();" in refresh and "const bool first_seen = capunion::first_seen_needed(eager_captures(), cu);" in refresh and "if (first_seen) pool_first_seen.swap(seen);" in refresh, "the pool table's look does not keep the pools' first captures by the rule") check('cu ? cap_union_merged(std::move(every), cut, "the game\'s pools\' list (all_pool_ranges)") : ranges_merged(std::move(every));' in refresh and 'm = cap_union_merged(std::move(v), whole, "the small pools\' list (pool_cap_ranges)");' in refresh, "the pool table's look does not merge its lists as sorted runs") check("if (all != all_pool_ranges) all_pool_ranges = std::move(all), changed = true;" in refresh and "if (m != pool_cap_ranges) pool_cap_ranges.swap(m), changed = true;" in refresh and "if (changed) cap_ranges_ver++;" in refresh, "the pool table's look moves the lists' version by another rule") runs = block(mem, "std::vector> cap_union_merged(std::vector> v, size_t cut, const char* what) {") check("capunion::merged_runs(" in runs and "ranges_merged(" in runs and "cap_union_differs(what," in runs, "cap_union_merged has no check against ranges_merged that names its list") # the counters, in the capture's 10 s line present = block(mem, "void memory_cap_present() {") check("RX_VK_CAP_UNION_SHARED=%s" in present and all(c in present for c in ("st_cu_built", "st_cu_reused", "st_cu_own", "st_cu_sorts", "st_cu_diff")), "the capture's 10 s line does not carry the shared union's counters") dev = (ROOT / "docs" / "development.md").read_text(encoding="utf-8") check("| `RX_VK_CAP_UNION_SHARED=0`, `=check` |" in dev and "`nocapunion`" in dev and "tests/test_capunion.py" in dev, "docs/development.md: RX_VK_CAP_UNION_SHARED is not in the switches table") # (the old union and the helpers it calls, as they are in the source: compiled against capunion.h's copies below) real = "\n".join(cut_block(mem, m, where="gpu/vk/vk_memory.cpp") for m in ( "std::vector> ranges_merged(std::vector> v) {", "std::vector> ranges_clip(const std::vector>& a,", "std::vector> capture_union_locked() {")) with tempfile.TemporaryDirectory(prefix="capunion-") as tmp: src, exe = Path(tmp) / "capunion.cpp", Path(tmp) / "capunion.exe" src.write_text(PROGRAM.replace("@CUT@", real), encoding="utf-8") subprocess.run([args.cxx, "-std=c++20", "-O2", "-Wall", "-Wextra", "-Werror", "-I" + str(VK), str(src), "-o", str(exe)], check=True) r = subprocess.run([str(exe)], capture_output=True, text=True, timeout=300) print(r.stdout.rstrip()) check(r.returncode == 0, "the shared union's lists are not the lists as before") print("PASS: one union of the captures' lists a version for every caller, its lists what they were") if __name__ == "__main__": main()