Files
Triforce-Source/tests/test_bindspan.py
ruckus7382sfgsgdf b527e29adb Triforce v0.8.5
A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
2026-11-28 10:54:41 -06:00

484 lines
30 KiB
Python

"""A read binding answered from a span of its pool's mirror (gpu/vk/bindspan.h; vk_memory.cpp gpu_buffer_bind; no game,
no GPU).
(Triforce, 11-26, the GPU pass on v0.8.3, C08.) A read binding the memo does not know took the lock-free path at every
draw: the thread's pool table, the pool's fields, an add on Pool::binds, the resident pieces' walk, to find the mirror
the binding before it in the same pool found. The thread now keeps a few spans of guest addresses, each filled by a
binding that path gave the mirror, and a later binding inside one under the same key is given the mirror at once.
Compiles a driver against the header:
- the switch's three values, and any other value as unset; the bindings that never use a span (written, no bytes, outside a submission's uploads,
the three diagnostics / modes that act at every binding);
- a hit inside a span; a miss for a range that crosses its end, for no bytes, for a size that would wrap; a miss after
the stamp, the pool table's generation, the segment, the recording or the imports left overlapping move;
- a hit =check resolves the whole way too is counted as neither a span's answer nor a binding to fold;
- the counted hits are folded into the pool exactly once, when the span is replaced or the key moves, and dropped
(never folded) when the pool table's generation moved;
- eight spans: four pools bound in turn all hit; a ninth pool replaces one, the others stand;
- a mirror in pieces: the span is the run of resident pieces around the binding, cut at the pool's end, never past a
piece without memory, at most kRunMost pieces further each way;
- a model: random pools, whole and in pieces, random bindings; every hit is a binding the whole path's own tests
(the pool's bounds, every piece resident) give the mirror, at the same offset.
Compiles vk_memory.cpp's own bind_span_fill and pools_flag_overlapping (cut from the source; the pool table and the
thread's spans stubbed): an import made beside another (pool_lookup's plan past 1 GB, as in a v0.8.4 log) flags both
and moves the spans' generation once, one beside two flags all three, one beside none flags nothing, an import inside a
larger one and wholly below the new one's start is passed over and the larger one flagged; no span is filled for a
flagged import, one is for an import apart.
Reads the source:
- gpu_buffer_bind: cap_excl_note (not again for =check's whole way), the 1 TB translation, late_deferred_bound and
the memo come before the span; the span before the pool table and the lock; the rule's arguments; the key; the
thread's counts taken at every question; a hit goes into the memo; a span is filled only where the lock-free
path returns the mirror; =check resolves the whole way and returns that;
- RX_VK_BIND_SPAN read once, logged when not the default; the lab's knob read once a submission, in
memory_begin_submission, and no lab question at a binding; the status line, not without mirrors;
- pool_lookup flags an import its plan left overlapping, after the import is in the table;
- what the key relies on: every pieces_set_direct call is followed by a move of pools_gen, a mirror freed moves it,
mirror_upload goes on only with a new mirror_epoch, and mirror_sync is only called where a sync is due;
- the switches table has the row.
python -I -B tests/test_bindspan.py [--cxx clang++]
"""
import argparse
import re
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]
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
DRIVER = r'''
#include "bindspan.h"
#include <cstdio>
#include <map>
#include <vector>
using namespace mkwvk::bindspan;
static int checks, fails;
static void want(const char* what, bool ok) {
checks++;
if (!ok) fails++, printf("FAIL %s\n", what);
}
struct P { // (a pool, as far as the lock-free path looks at it)
uint64_t lo, hi;
bool pieces;
uint32_t shift;
std::vector<uint64_t> bits;
};
using S = Set<int, P*>;
static std::map<P*, uint32_t> folded;
static int folds;
static const auto fold = [](P* p, uint32_t n) { folded[p] += n, folds++; };
// (vk_memory.cpp pieces_resident, bit by bit)
static bool resident(const P& p, uint64_t off, uint64_t size) {
if (!p.pieces || !size) return true;
for (uint64_t i = off >> p.shift, e = (off + size - 1) >> p.shift; i <= e; i++)
if (!((p.bits[i >> 6] >> (i & 63)) & 1)) return false;
return true;
}
static uint64_t rnd_state = 0x1234567887654321ull;
static uint64_t rnd() {
rnd_state ^= rnd_state << 13, rnd_state ^= rnd_state >> 7, rnd_state ^= rnd_state << 17;
return rnd_state;
}
int main() {
want("the switch: on unless set, 0 off, check", mode(nullptr) == kOn && mode("1") == kOn && mode("0") == kOff && mode("check") == kCheck);
want("any other value, empty included: on, as unset", mode("") == kOn && mode("on") == kOn && mode("true") == kOn &&
mode("2") == kOn && mode("Check") == kOn);
want("a read binding of bytes within a submission's uploads may use a span", eligible(false, 64, true, false, false, false));
want("a written binding never", !eligible(true, 64, true, false, false, false));
want("a binding of no bytes never", !eligible(false, 0, true, false, false, false));
want("outside a submission's uploads (or without mirrors) never", !eligible(false, 64, false, false, false, false));
want("RX_VK_LOG_MIRROR_USE: never", !eligible(false, 64, true, true, false, false));
want("RX_VK_MIRROR_HEAT: never", !eligible(false, 64, true, false, true, false));
want("RX_VK_MIRROR_PIECE_IDLE: never", !eligible(false, 64, true, false, false, true));
want("inside", inside(0x1000, 0x2000, 0x1000, 0x1000) && inside(0x1000, 0x2000, 0x1ff0, 16) && inside(0x1000, 0x2000, 0x1800, 1));
want("crossing the end", !inside(0x1000, 0x2000, 0x1ff0, 17) && !inside(0x1000, 0x2000, 0x2000, 1));
want("before the start", !inside(0x1000, 0x2000, 0xfff, 2));
want("no bytes", !inside(0x1000, 0x2000, 0x1800, 0));
want("no wrap-around for a huge size", !inside(0x1000, 0x2000, 0x1800, ~0ull) && !inside(0x1000, 0x2000, 0x1800, ~0ull - 0x17ff));
{ // a hit, and what ends it
S s;
P a{0x10000, 0x20000, false, 0, {}};
const Key k{7, 3, 0, 100};
want("nothing before a fill", !s.find(k, 0x10040, 64));
s.fill(k, a.lo, a.hi, a.lo, 11, &a, fold);
S::Span* h = s.find(k, 0x10040, 64);
want("a hit inside the span", h && h->buffer == 11 && h->base == a.lo && h->pool == &a);
want("a range crossing the span's end misses", !s.find(k, 0x1ffc0, 65));
want("a range outside misses", !s.find(k, 0x20000, 64) && !s.find(k, 0xffc0, 64));
want("the stamp moved: a miss", !s.find(Key{8, 3, 0, 100}, 0x10040, 64));
want("the pool table's generation moved: a miss", !s.find(Key{7, 4, 0, 100}, 0x10040, 64));
want("an earlier or later segment: a miss", !s.find(Key{7, 3, 1, 100}, 0x10040, 64));
want("the next recording: a miss", !s.find(Key{7, 3, 0, 101}, 0x10040, 64));
want("the key again: a hit again", s.find(k, 0x10040, 64) != nullptr);
uint64_t hits = 7, misses = 7, fills = 7;
want("the thread's counts stay its own for a while", !s.take_counts(&hits, &misses, &fills) && hits == 7 && s.hits == 2 && s.misses == 8 && s.fills == 1);
for (uint64_t i = 0; i < kTakeAt; i++) s.find(k, 0x10040, 64);
want("taken once they are worth it", s.take_counts(&hits, &misses, &fills) && hits == 2 + kTakeAt && misses == 8 && fills == 1);
want("taken once", !s.take_counts(&hits, &misses, &fills) && s.hits == 0 && s.misses == 0 && s.fills == 0);
want("imports left overlapping: a miss", !s.find(Key{7, 3, 0, 100, 1}, 0x10040, 64));
S::Span* c = s.find(k, 0x10040, 64);
const uint32_t pending = c ? c->pending : 0;
if (c) s.checked(c);
want("a checked hit is neither a span's answer nor a binding to fold", c && s.hits == 0 && c->pending == pending - 1);
}
{ // the counted hits: folded once, at a key move with the pools standing
S s;
P a{0x10000, 0x20000, false, 0, {}}, b{0x40000, 0x50000, false, 0, {}};
folded.clear(), folds = 0;
const Key k{7, 3, 0, 100};
s.fill(k, a.lo, a.hi, a.lo, 11, &a, fold);
s.fill(k, b.lo, b.hi, b.lo, 12, &b, fold);
for (int i = 0; i < 5; i++) s.find(k, a.lo + 64 * (uint64_t)i, 64);
for (int i = 0; i < 3; i++) s.find(k, b.lo + 64 * (uint64_t)i, 64);
want("nothing folded at a hit", folds == 0);
const Key k2{9, 3, 0, 101}; // (the next submission, the same pools)
s.fill(k2, a.lo, a.hi, a.lo, 11, &a, fold);
want("folded at the key's move: as many as the hits", folded[&a] == 5 && folded[&b] == 3 && folds == 2);
want("the set is the new key's alone", s.n == 1 && !s.find(k2, b.lo, 64) && s.find(k2, a.lo, 64));
s.fill(k2, b.lo, b.hi, b.lo, 12, &b, fold);
want("a second fill folds nothing twice", folded[&a] == 5 && folded[&b] == 3);
s.fill(k2, a.lo, a.hi, a.lo, 11, &a, fold); // (its span filled again: its one hit folded now)
want("a span filled again folds its hits and stays one span", folded[&a] == 6 && s.n == 2);
}
{ // dropped when the pool table's generation moved: a pool may be gone
S s;
P a{0x10000, 0x20000, false, 0, {}}, b{0x40000, 0x50000, false, 0, {}};
folded.clear(), folds = 0;
const Key k{7, 3, 0, 100};
s.fill(k, a.lo, a.hi, a.lo, 11, &a, fold);
for (int i = 0; i < 9; i++) s.find(k, a.lo, 64);
const Key k2{7 ^ 1, 4, 0, 100};
s.fill(k2, b.lo, b.hi, b.lo, 12, &b, fold);
want("pools_gen moved: the hits are dropped, no pool written", folds == 0);
for (int i = 0; i < 20; i++) { // (and never come back through a slot used again)
P* q = i % 2 ? &a : &b;
s.fill(Key{100 + (uint64_t)i, 4, 0, 100}, q->lo, q->hi, q->lo, 1, q, fold);
}
want("dropped hits never folded later", folds == 0);
}
{ // eight spans
S s;
std::vector<P> pools;
for (uint64_t i = 0; i < 9; i++) pools.push_back(P{0x100000 * (i + 1), 0x100000 * (i + 1) + 0x10000, false, 0, {}});
folded.clear(), folds = 0;
const Key k{7, 3, 0, 100};
for (int i = 0; i < 4; i++) s.fill(k, pools[i].lo, pools[i].hi, pools[i].lo, i, &pools[i], fold);
bool all = true;
for (int r = 0; r < 100; r++)
for (int i = 0; i < 4; i++) {
S::Span* h = s.find(k, pools[i].lo + 16 * (uint64_t)r, 128);
all = all && h && h->buffer == i && h->base == pools[i].lo;
}
want("four pools bound in turn all hit", all);
for (int i = 4; i < 9; i++) s.fill(k, pools[i].lo, pools[i].hi, pools[i].lo, i, &pools[i], fold);
int held = 0;
for (int i = 0; i < 9; i++) held += s.find(k, pools[i].lo, 64) != nullptr;
want("a ninth pool replaces one span, eight stand", s.n == kSpans && held == 8 && s.find(k, pools[8].lo, 64));
want("the replaced span's hits were folded", folds == 1);
}
{ // a mirror in pieces: the resident run
const uint32_t shift = 18; // (256 KB)
const uint64_t piece = 1ull << shift, lo = 0x40000000ull;
P p{lo, lo + 10 * piece + 0x1234, true, shift, {0}}; // (11 pieces, the last one short)
for (int i : {2, 3, 4, 6, 9, 10}) p.bits[0] |= 1ull << i;
uint64_t a = 0, b = 0;
bounds(p.lo, p.hi, 3 * piece + 100, 64, p.bits.data(), p.bits.size(), shift, &a, &b);
want("the run around a binding in piece 3 is pieces 2 to 4", a == lo + 2 * piece && b == lo + 5 * piece);
bounds(p.lo, p.hi, 2 * piece + 100, 2 * piece, p.bits.data(), p.bits.size(), shift, &a, &b);
want("a binding over pieces 2 to 4: the same run", a == lo + 2 * piece && b == lo + 5 * piece);
bounds(p.lo, p.hi, 6 * piece, 64, p.bits.data(), p.bits.size(), shift, &a, &b);
want("a lone resident piece: itself", a == lo + 6 * piece && b == lo + 7 * piece);
bounds(p.lo, p.hi, 10 * piece, 64, p.bits.data(), p.bits.size(), shift, &a, &b);
want("the pool's last, short piece: cut at the pool's end", a == lo + 9 * piece && b == p.hi);
bounds(p.lo, p.hi, 3 * piece, 64, nullptr, 0, 0, &a, &b);
want("a whole mirror: the pool", a == p.lo && b == p.hi);
S s;
const Key k{7, 3, 0, 100};
bounds(p.lo, p.hi, 3 * piece + 100, 64, p.bits.data(), p.bits.size(), shift, &a, &b);
s.fill(k, a, b, p.lo, 5, &p, fold);
want("inside the run: a hit", s.find(k, lo + 4 * piece + 8, 64) != nullptr);
want("reaching into a piece without memory: a miss", !s.find(k, lo + 5 * piece - 8, 64) && !s.find(k, lo + 2 * piece - 1, 2));
want("a piece with memory beyond a gap is not this span's", !s.find(k, lo + 6 * piece, 64));
// (a long run: no further than kRunMost pieces each way)
P big{lo, lo + 512 * piece, true, shift, std::vector<uint64_t>(8, ~0ull)};
bounds(big.lo, big.hi, 200 * piece, 64, big.bits.data(), big.bits.size(), shift, &a, &b);
want("at most kRunMost pieces further each way", a == lo + (200 - kRunMost) * piece && b == lo + (200 + kRunMost + 1) * piece);
uint64_t i0 = 0, i1 = 0;
resident_run(big.bits.data(), 512, 0, 0, &i0, &i1);
want("the run stops at piece 0", i0 == 0 && i1 == kRunMost);
resident_run(big.bits.data(), 512, 511, 511, &i0, &i1);
want("and at the pool's last piece", i0 == 511 - kRunMost && i1 == 511);
}
{ // the model: a hit is always a binding the whole path gives the mirror, at the same offset
const uint32_t shift = 12;
std::vector<P> pools;
for (uint64_t i = 0; i < 12; i++) {
P p{0x1000000 * (i + 1), 0x1000000 * (i + 1) + 0x40000 + (rnd() & 0xfff), i % 3 != 0, shift, {}};
const uint64_t n = (p.hi - p.lo + (1ull << shift) - 1) >> shift;
p.bits.assign((n + 63) / 64, 0);
for (uint64_t j = 0; j < n; j++)
if (rnd() % 8) p.bits[j >> 6] |= 1ull << (j & 63);
pools.push_back(p);
}
S s;
Key k{1, 1, 0, 1};
long hits = 0, fills = 0, wrong = 0;
for (int it = 0; it < 400000; it++) {
if (it % 5000 == 4999) k.stamp++, k.rec_value++; // (the next submission)
P& p = pools[rnd() % pools.size()];
const uint64_t size = 1 + rnd() % 3000;
const uint64_t guest = p.lo - 64 + rnd() % (p.hi - p.lo + 128);
const bool in_pool = guest >= p.lo && guest <= p.hi && size <= p.hi - guest;
const bool mirror = in_pool && resident(p, guest - p.lo, size);
if (S::Span* h = s.find(k, guest, size)) {
hits++;
if (!mirror || h->pool != &p || h->base != p.lo || h->buffer != (int)(&p - pools.data())) wrong++;
} else if (mirror) {
uint64_t a = 0, b = 0;
bounds(p.lo, p.hi, guest - p.lo, size, p.pieces ? p.bits.data() : nullptr, p.bits.size(), shift, &a, &b);
if (!inside(a, b, guest, size)) wrong++; // (its own binding is in its span)
s.fill(k, a, b, p.lo, (int)(&p - pools.data()), &p, fold);
fills++;
}
}
printf("model: %ld hits, %ld fills, %ld wrong\n", hits, fills, wrong);
want("the model: no hit the whole path would not give the mirror", wrong == 0 && hits > 1000 && fills > 100);
}
printf("%d checks, %d failed\n", checks, fails);
return fails != 0;
}
'''
OVERLAP_DRIVER = r'''
#include "bindspan.h"
#include <atomic>
#include <cstdio>
#include <map>
#include <vector>
using namespace mkwvk;
using VkBuffer = int;
struct Pool { // (as far as the two functions look at it)
uint64_t lo = 0, hi = 0;
VkBuffer mirror = 0;
uint32_t binds = 0, piece_shift = 0;
bool mpieces = false, left_overlapping = false;
std::vector<uint64_t> res_bits;
};
static std::map<uint64_t, Pool> imported;
static std::atomic<uint64_t> pools_overlap_gen{0};
static thread_local bindspan::Set<VkBuffer, Pool*> tl_bind_spans;
// (vk_memory.cpp's pools_flag_overlapping and bind_span_fill, cut from the source)
@CUT@
static int fails;
static void want(const char* what, bool ok) {
if (!ok) fails++, std::printf("FAIL %s\n", what);
}
static std::map<uint64_t, Pool>::iterator add(uint64_t lo, uint64_t hi, int mirror) {
Pool p;
p.lo = lo, p.hi = hi, p.mirror = mirror;
return imported.emplace(lo, p).first;
}
int main() {
// (a v0.8.4 log: the import 0x7f71000000..0x7f86000000 stood, a binding from 0x7f70f1bf00 planned 1040 MB, and
// 0x7f70000000..0x7f72000000 was made beside it)
auto q = add(0x7f71000000, 0x7f86000000, 1);
auto below = add(0x7f6e000000, 0x7f70000000, 2); // (ends where the new one starts: apart)
auto above = add(0x7f86000000, 0x7f88000000, 3); // (starts where q ends: apart from the new one)
auto far = add(0x7e00000000, 0x7e01000000, 4); // (more than 1 GB further down)
auto lone = add(0x7f40000000, 0x7f42000000, 5);
want("an import beside none flags nothing", pools_flag_overlapping(lone) == 0 && pools_overlap_gen.load() == 0 &&
!lone->second.left_overlapping);
auto n = add(0x7f70000000, 0x7f72000000, 6);
want("the import made beside another overlaps one", pools_flag_overlapping(n) == 1);
want("both are flagged", n->second.left_overlapping && q->second.left_overlapping);
want("the imports apart are not", !below->second.left_overlapping && !above->second.left_overlapping &&
!far->second.left_overlapping && !lone->second.left_overlapping);
want("the spans' generation moved once", pools_overlap_gen.load() == 1);
auto a = add(0x7f50000000, 0x7f52000000, 7), b = add(0x7f53000000, 0x7f55000000, 8);
auto mid = add(0x7f51000000, 0x7f54000000, 9);
want("one beside two flags all three", pools_flag_overlapping(mid) == 2 && mid->second.left_overlapping &&
a->second.left_overlapping && b->second.left_overlapping && pools_overlap_gen.load() == 2);
// (a small import inside a large one, wholly below the new import's start: passed over, and the large one, which
// starts further down and reaches into the new import, is still found)
auto outer = add(0x7f20000000, 0x7f30000000, 10), inner = add(0x7f21000000, 0x7f22000000, 11);
auto top = add(0x7f2f000000, 0x7f31000000, 12);
want("an import below the new one's start does not end the walk", pools_flag_overlapping(top) == 1 &&
top->second.left_overlapping && outer->second.left_overlapping && !inner->second.left_overlapping &&
pools_overlap_gen.load() == 3);
const bindspan::Key k{7, 3, 0, 100, pools_overlap_gen.load()};
bind_span_fill(k, &q->second, 0x100, 64);
bind_span_fill(k, &n->second, 0x100, 64);
want("no span for an import left overlapping, nor for the one beside it",
tl_bind_spans.n == 0 && !tl_bind_spans.find(k, 0x7f71000100, 64) && !tl_bind_spans.find(k, 0x7f70000100, 64));
bind_span_fill(k, &above->second, 0x100, 64);
want("a span for an import apart", tl_bind_spans.n == 1 && tl_bind_spans.find(k, 0x7f86000100, 64) != nullptr);
std::printf("%s\n", fails ? "overlapping imports: FAILED" : "overlapping imports: flagged, no span for either");
return fails != 0;
}
'''
def block(text, marker):
start = text.index(marker)
at = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[at] == "{") - (text[at] == "}")
at += 1
return text[start:at]
def need(ok, why):
if not ok:
raise SystemExit("FAIL: " + why)
def ordered(text, parts, why):
last = 0
for p in parts:
k = text.find(p, last)
need(k >= 0, why + " (missing, or out of order: " + p + ")")
last = k + len(p)
def check_source():
mem = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8")
need('#include "bindspan.h"' in mem, "vk_memory.cpp does not include bindspan.h")
mode = block(mem, "static int bind_span_mode() {")
need('static const int m = [] {' in mode and 'const char* e = getenv("RX_VK_BIND_SPAN");' in mode and
"const int v = bindspan::mode(e);" in mode and "if (v != bindspan::kOn)" in mode and "(RX_VK_BIND_SPAN)" in mode,
"the switch is not RX_VK_BIND_SPAN read once and logged when it is not the default")
need('else if (e && e[0] && strcmp(e, "1"))' in mode and "RX_VK_BIND_SPAN=%s not known" in mode,
"an RX_VK_BIND_SPAN value other than 1, 0 or check is not logged")
need("static thread_local bindspan::Set<VkBuffer, Pool*> tl_bind_spans;" in mem, "the spans are not a thread's own")
bind = block(mem, "BufferRef gpu_buffer_bind(")
ordered(bind, ['if (cap_only_gpu() && !tl_bind_span_checking) cap_excl_note(gpu, size, "a buffer binding");',
"if (gpu >= (1ull << 40)) {", "late_deferred_bound(guest, size);",
"bind_memo_hit(guest, size, gpu_bind_stamp(),", "return m.ref;",
"const int span_mode = bind_span_mode();",
"const bool span_ok = span_mode != bindspan::kOff && !tl_bind_span_checking &&",
"bindspan::eligible(written, size, mirror_upload && mirror_on(), mirror_use_on(), mirror_heat_on(), piece_idle_for() != 0) &&",
"!bind_span_lab_off.load(std::memory_order_relaxed);",
"if (span_ok) {",
"span_key = {gpu_bind_stamp(), pools_gen.load(std::memory_order_relaxed), texture_segment(), mem_rec_value,\n"
" pools_overlap_gen.load(std::memory_order_acquire)};",
"bind_span_take();", "if (auto* s = tl_bind_spans.find(span_key, guest, size)) {",
"const BufferRef ref{s->buffer, guest - s->base, size, 0};",
"if (span_mode != bindspan::kCheck) return remember(ref);", "tl_bind_spans.checked(s);",
"tl_bind_span_checking = true;", "const BufferRef whole = gpu_buffer_bind(gpu, size, false);",
"tl_bind_span_checking = false;", "bind_span_checked(ref, whole, guest, span, span_key);", "return whole;",
"TlPool& tl = tl_pool_slot(guest);",
"if (p->last_value < mem_rec_value) p->last_value = mem_rec_value;",
"if (p->mstate == Pool::MIRROR_ON && p->synced_epoch == mirror_epoch && p->sync_seg <= texture_segment()) {",
"if (pieces_resident(p, off, size)) {", "if (span_ok) bind_span_fill(span_key, p, off, size);",
"return remember({p->mirror, off, size, 0});", "ParLock plk;"],
"gpu_buffer_bind does not ask the span after the memo and before the pool table, by the rule, or fill it only "
"where the lock-free path returns the mirror")
need(bind.count("bind_span_fill(") == 1 and bind.count("tl_bind_spans.") == 2, "a span is filled or asked somewhere else in gpu_buffer_bind")
need("lab_ab::on(" not in bind, "gpu_buffer_bind asks the lab at a binding: its string compares run in the knobs' phases alone")
begin = block(mem, "void memory_begin_submission() {")
ordered(begin, ["mirror_upload = true;", 'bind_span_lab_off.store(lab_ab::on("nobindspan"), std::memory_order_relaxed);'],
"the lab knob nobindspan is not read once a submission, where the uploads start")
need(mem.count("bind_span_lab_off.store(") == 1, "the lab knob's flag is written somewhere else too")
need(mem.count("bind_span_fill(") == 2 and mem.count("tl_bind_spans.find(") == 1, "a span is filled or asked outside gpu_buffer_bind")
fill = block(mem, "static void bind_span_fill(")
ordered(fill, ["if (__atomic_load_n(&p->left_overlapping, __ATOMIC_RELAXED)) return;", "if (pieces && p->res_bits.empty()) return;",
"bindspan::bounds(p->lo, p->hi, off, size, pieces ? p->res_bits.data() : nullptr, p->res_bits.size(), p->piece_shift, &lo, &hi);",
"tl_bind_spans.fill(key, lo, hi, p->lo, p->mirror, p,", "__atomic_fetch_add(&q->binds, n, __ATOMIC_RELAXED);"],
"a span is not the pool's or its resident run's, or its hits are not folded into Pool::binds")
need("if (!tl_bind_spans.take_counts(&h, &m, &f)) return;" in block(mem, "static void bind_span_take() {") and
mem.count("take_counts(") == 1, "the thread's counts are not taken in bind_span_take alone")
look = block(mem, "Pool* pool_lookup(uint64_t guest, uint64_t size) {")
ordered(look, ["bool left_overlapping = false;", "auto plan = [&](bool spanned) {", "left_overlapping = false;",
"if (hi - lo > (1ull << 30)) {", "left_overlapping = true;", "return padding_only;",
"imp = imported.emplace(p.lo, p).first;", "if (left_overlapping) pools_flag_overlapping(imp);"],
"pool_lookup does not flag an import its last plan left overlapping, after it is in the table")
need(mem.count("pools_flag_overlapping(") == 2 and mem.count("pools_overlap_gen.fetch_add(") == 1,
"imports are flagged, or the spans' generation moved, somewhere else too")
chk = block(mem, "static void bind_span_checked(")
need("span.buffer == whole.buffer && span.offset == whole.offset && span.size == whole.size && span.address == whole.address" in chk and
"st_span_diffs.fetch_add(1" in chk, "=check does not compare the whole result and count the differences")
report = block(mem, "static void bind_span_report() {")
need("if (mode == bindspan::kOff || !mirror_on()) return;" in report, "the status line is printed without mirrors, all zeros")
need("vk: bind spans (600 submits): %llu read bindings answered from a span, %llu the whole way past the memo" in report and
"%llu differed (RX_VK_BIND_SPAN=check)" in report, "the status line does not give the hits, the misses and =check's differences")
end = block(mem, "void memory_end_submission(VkCommandBuffer cmd) {")
ordered(end, ["if (++n % 600 == 0) {", "bind_span_report();"], "the status line is not in memory_end_submission's 600 submissions")
# what the key relies on
stamp = block(mem, "uint64_t gpu_bind_stamp() {")
need("pools_gen" in stamp and "mirror_epoch" in stamp, "the stamp no longer holds pools_gen and mirror_epoch")
calls = [m.start() for m in re.finditer(r"(?<![A-Za-z0-9_])pieces_set_direct\(", mem)]
need(len(calls) >= 7, "pieces_set_direct's callers were not found")
for at in calls:
if mem.startswith("void ", mem.rfind("\n", 0, at) + 1):
continue # (its definition)
need("pools_gen++" in mem[at:at + 1500],
"a pieces_set_direct call is not followed by a move of pools_gen: a span would keep a piece taken out of its mirror (line %d)"
% (mem.count("\n", 0, at) + 1))
free = block(mem, "void mirror_free(Pool& p")
need("p.mstate = Pool::MIRROR_NONE;\n pools_gen++;" in free, "a mirror freed does not move pools_gen")
need(mem.count("mirror_upload = true;") == 1 and "mirror_epoch++;\n mirror_upload = true;" in mem,
"mirror_upload goes on somewhere else than with a new mirror_epoch")
need(len(re.findall(r"(?<![A-Za-z0-9_])mirror_sync\(", mem)) == 3 and
"if (p->synced_epoch != mirror_epoch || p->sync_seg > texture_segment()) mirror_sync(*p);" in mem and
"if (p.mstate != Pool::MIRROR_ON || p.synced_epoch == mirror_epoch) continue;\n mirror_sync(p);\n p.sync_seg = 0;" in mem,
"mirror_sync is called where no sync is due: a pool's sync_seg could rise within a submission, under a span")
header = (ROOT / "gpu/vk/bindspan.h").read_text(encoding="utf-8")
for word in ("RX_VK_BIND_SPAN=0", "=check", '"nobindspan"', "(Triforce, 11-26", "mirror_upload", "last_value", "resident"):
need(word in header, "bindspan.h's comment no longer says: " + word)
docs = (ROOT / "docs/development.md").read_text(encoding="utf-8")
row = [line for line in docs.splitlines() if line.startswith("| `RX_VK_BIND_SPAN=0`")]
need(len(row) == 1 and "`gpu/vk/bindspan.h`" in row[0] and "`nobindspan`" in row[0] and "`check`" in row[0] and
"vk: bind spans (600 submits)" in row[0], "docs/development.md: the switches table has no RX_VK_BIND_SPAN row")
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
need((ROOT / "gpu/vk/bindspan.h").is_file(), "gpu/vk/bindspan.h is missing: read bindings have no span rule")
with tempfile.TemporaryDirectory(prefix="bindspan-") as tmp:
cpp, exe = Path(tmp) / "bindspan_test.cpp", Path(tmp) / "bindspan.exe"
cpp.write_text(DRIVER, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)],
check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: the bind spans' checks failed")
mem = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8")
cut = "\n".join(cut_block(mem, m, where="gpu/vk/vk_memory.cpp") for m in (
"int pools_flag_overlapping(std::map<uint64_t, Pool>::iterator n) {", "static void bind_span_fill("))
cpp, exe = Path(tmp) / "bindspan_overlap.cpp", Path(tmp) / "bindspan_overlap.exe"
cpp.write_text(OVERLAP_DRIVER.replace("@CUT@", cut), encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror", "-I" + str(ROOT / "gpu/vk"), str(cpp), "-o", str(exe)],
check=True)
r = subprocess.run([str(exe)], capture_output=True, text=True)
print(r.stdout.rstrip())
if r.returncode:
raise SystemExit("FAIL: vk_memory.cpp's bind_span_fill fills a span for an import left overlapping, or the flags are wrong")
check_source()
print("PASS: a read binding inside a span this thread resolved is given its pool's mirror at once; RX_VK_BIND_SPAN=0 as before, =check both ways")
if __name__ == "__main__":
main()