// tests/test_readback_rec_end.py: gpu/vk/wb_rec_wait.h's readback wait (Parts, readback_wait, rec_end_on) against a made-up GPU. // A recording made in parts writes back at each part's end, under its one value; a readback waits for that value on the // recordings' timeline. The old wait named the first part; the rule waits for the last. #include "../gpu/vk/wb_rec_wait.h" #include #include #include #include #define CHECK(name, condition) do { if (!(condition)) { std::fprintf(stderr, "FAIL %s at line %d\n", name, __LINE__); return 1; } } while (0) namespace wr = mkwvk::wb_rec_wait; // One recording of value V made in parts while a readback waits: parts[i] is part i's number on the recordings' timeline, the last // one ends the recording. `sent` parts are submitted when the read comes; the replay submits the next one each time the reader // waits, flushes or slices, `step` parts at a time (0: the replay is stuck, the recording never ends). The GPU runs every part // submitted, so a wait for a submitted part comes back with it done; a wait for one never submitted is a hang (counted). struct Gpu { uint64_t value = 100; std::vector parts; size_t sent = 1; int step = 1; uint64_t rec_done = 0; // (the recordings' timeline) bool timeline_at_v = false; // (the submissions' timeline at the value: only once the recording's last part is done) int timeline_after = -1; // (slices until the submissions' timeline reaches the value with the recording stuck; -1 never) int rec_wait_error_at = -1; // (the n-th recording wait gives an error) int rec_waits = 0, flushes = 0, slices = 0, hangs = 0; wr::Parts table; uint64_t first_noted = 0; // (value_rec: the first part, emplace) void submit_up_to(size_t n) { for (; sent < n && sent < parts.size(); sent++) note(sent); } void note(size_t i) { table.submitted(value, parts[i], i + 1 == parts.size()); if (!first_noted) first_noted = parts[i]; } void begin() { for (size_t i = 0; i < sent; i++) note(i); } void replay_goes_on() { submit_up_to(sent + (size_t)step); } bool ended() const { return sent == parts.size(); } // the calls readback_wait makes uint64_t first_part() { return first_noted; } wr::Parts::Entry look() { return table.of(value); } int rec_wait(uint64_t seq) { rec_waits++; if (rec_wait_error_at >= 0 && rec_waits > rec_wait_error_at) return 2; if (seq > parts[sent - 1]) { // (never submitted: the wait would never come back) hangs++; return 2; } if (seq > rec_done) rec_done = seq; if (ended() && rec_done >= parts.back()) timeline_at_v = true; replay_goes_on(); return 0; } void flush() { flushes++; replay_goes_on(); } int slice() { slices++; if (timeline_after >= 0 && slices >= timeline_after) { // (the replay ended it and the GPU ran it meanwhile) submit_up_to(parts.size()); rec_done = parts.back(); timeline_at_v = true; } if (timeline_at_v) return 0; replay_goes_on(); return 1; } }; int main() { // the switch: on unset or empty; set, it is its number (atoi, as the renderer's other switches: a word reads as 0, off) CHECK("unset: off (0.8.5.1)", !wr::rec_end_on(nullptr)); CHECK("empty: off (0.8.5.1)", !wr::rec_end_on("")); CHECK("0: off", !wr::rec_end_on("0")); CHECK("00: off", !wr::rec_end_on("00")); CHECK("1: on", wr::rec_end_on("1")); CHECK("2: on", wr::rec_end_on("2")); CHECK("a word: off", !wr::rec_end_on("off")); // the parts' table: the newest part and whether it ended the recording; an older note changes nothing; a later recording // under the same value takes the entry over from its first part; nothing for no value or no number; the oldest past the cap wr::Parts t; CHECK("not known", t.of(7).seq == 0 && !t.of(7).ended); t.submitted(7, 10, false); CHECK("a part", t.of(7).seq == 10 && !t.of(7).ended); t.submitted(7, 11, false), t.submitted(7, 12, true); CHECK("its end", t.of(7).seq == 12 && t.of(7).ended); t.submitted(7, 11, false); CHECK("an older note after a newer one: nothing", t.of(7).seq == 12 && t.of(7).ended); t.submitted(7, 20, false); CHECK("a later recording under the value: from its first part, going on", t.of(7).seq == 20 && !t.of(7).ended); t.submitted(0, 30, true), t.submitted(8, 0, true); CHECK("no value or no number: nothing noted", t.by_value.size() == 1); t.cap = 3; for (uint64_t v = 40; v < 44; v++) t.submitted(v, 100 + v, true); CHECK("cap held", t.by_value.size() == 3 && t.of(7).seq == 0 && t.of(40).seq == 0 && t.of(43).seq == 143); // the meter's case: a recording in three parts (10, 11, 12), the read after the first one's submit. The old wait stops at // the first part, with the last two (and the write-back they make) still to come: the read got the CPU's 0 { Gpu g; g.parts = {10, 11, 12}; g.begin(); const wr::ReadbackWait r = wr::readback_wait(false, g); CHECK("old: known", r.known && r.ok); CHECK("old: ends at the first part, before the last", g.rec_done == 10 && g.rec_done < g.parts.back()); } { Gpu g; g.parts = {10, 11, 12}; g.begin(); const wr::ReadbackWait r = wr::readback_wait(true, g); CHECK("rule: known and done", r.known && r.ok); CHECK("rule: its recording went on past the part first found", r.went_on); CHECK("rule: ends with the last part done", g.rec_done == 12); CHECK("rule: no wait for a part never submitted", g.hangs == 0); } // an ended recording: its last part, one wait, as the old wait's one { Gpu g; g.parts = {10, 11, 12}; g.sent = 3; g.begin(); const wr::ReadbackWait r = wr::readback_wait(true, g); CHECK("ended: one wait for the last part", r.known && r.ok && !r.went_on && g.rec_waits == 1 && g.rec_done == 12 && g.slices == 0); } // a recording in one part: the same as the old wait { Gpu g; g.parts = {10}; g.begin(); const wr::ReadbackWait r = wr::readback_wait(true, g); CHECK("one part: one wait", r.known && r.ok && !r.went_on && g.rec_waits == 1 && g.rec_done == 10); } // not known at all (not recorded yet): the caller's fallback, nothing waited { Gpu g; g.parts = {10, 11}; const wr::ReadbackWait r = wr::readback_wait(true, g); const wr::ReadbackWait r0 = wr::readback_wait(false, g); CHECK("not known: the fallback's", !r.known && !r0.known && g.rec_waits == 0 && g.slices == 0); } // the replay stuck with the recording going on: slices of the submissions' timeline, ended once it reaches the value { Gpu g; g.parts = {10, 11, 12}; g.step = 0; g.timeline_after = 5; g.begin(); const wr::ReadbackWait r = wr::readback_wait(true, g); CHECK("stuck: ended by the timeline", r.known && r.ok && r.went_on && r.slices == 5 && g.timeline_at_v); } // an error (a lost device) ends the wait, not done { Gpu g; g.parts = {10, 11, 12}; g.rec_wait_error_at = 1; g.begin(); const wr::ReadbackWait r = wr::readback_wait(true, g); CHECK("error: not done", r.known && !r.ok && g.rec_waits == 2); } // 20,000 random recordings: 1 to 8 parts, the read after 1 to all of them are submitted, the replay going on 0 to 3 parts at // each call (0: stuck, the timeline reaching the value after some slices). The rule never ends before the last part is done // (or the timeline at the value), never waits for a part not submitted, and never takes more waits than parts plus one; the // old wait ends at the first part whenever the read came before the recording ended std::mt19937_64 rng(20261127); int old_early = 0, cases_going_on = 0; for (int k = 0; k < 20000; k++) { Gpu g, g0; const size_t n = 1 + rng() % 8; uint64_t s = 1000 + (uint64_t)k * 16; for (size_t i = 0; i < n; i++) g.parts.push_back(s++); g.sent = 1 + rng() % n; g.step = (int)(rng() % 4); if (g.step == 0) g.timeline_after = 1 + (int)(rng() % 20); g0 = g; g.begin(), g0.begin(); const bool going_on = g.sent < n; cases_going_on += going_on; const wr::ReadbackWait r = wr::readback_wait(true, g); CHECK("random: known and done", r.known && r.ok); CHECK("random: never before the last part is done", g.rec_done == g.parts.back() || g.timeline_at_v); CHECK("random: never a wait for a part not submitted", g.hangs == 0); CHECK("random: went on exactly when the read came before the end", r.went_on == going_on); CHECK("random: waits bounded", g.rec_waits <= (int)n + 1); const wr::ReadbackWait r0 = wr::readback_wait(false, g0); CHECK("random old: known", r0.known && r0.ok && g0.rec_waits == 1); if (going_on && g0.rec_done < g0.parts.back()) old_early++; } CHECK("random: the reads before a recording's end were many", cases_going_on > 10000); CHECK("random: the old wait ended early at every one of them", old_early == cases_going_on); std::printf("readback rec end: %d of 20000 reads came before their recording's end; the old wait ended early at all %d\n", cases_going_on, old_early); return 0; }