From fc1a20f33f1f7b70f84610326f4f0d339f279966 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:26:12 -0500 Subject: [PATCH 1/5] RR runtime: retire list in rr_retire.h with a CPU test (instance destroy unchanged) The deferred-release list of the RR runtime moves into a header-only RetireList (any object with Release, a counted flag for the live-resource total) so that a CPU test can drive it with a fake marker value and fake objects that count Release. The instance destroy goes through retire_instance with its current behaviour: the buffers wait for the frame marker, the descriptor and query heaps are released after the bounded wait. test_rr_retire builds tests/rr_retire_test.cpp and fails here: a destroy with the marker behind the instance's last frame releases both heaps (issue #63). The next commit fixes it. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/rr_f/runtime/src/rr_retire.h | 90 +++++++++++++++++++ clean/rr_f/runtime/src/rr_runtime.cpp | 39 +++------ clean/rr_f/runtime/tests/rr_retire_test.cpp | 95 +++++++++++++++++++++ clean/rr_f/runtime/tests/test_rr_retire.py | 84 ++++++++++++++++++ 4 files changed, 283 insertions(+), 25 deletions(-) create mode 100644 clean/rr_f/runtime/src/rr_retire.h create mode 100644 clean/rr_f/runtime/tests/rr_retire_test.cpp create mode 100644 clean/rr_f/runtime/tests/test_rr_retire.py diff --git a/clean/rr_f/runtime/src/rr_retire.h b/clean/rr_f/runtime/src/rr_retire.h new file mode 100644 index 00000000..44272708 --- /dev/null +++ b/clean/rr_f/runtime/src/rr_retire.h @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: MIT +// WrongGPU RR runtime: objects retired while a frame recorded into the game's command list may still use them +// (issue #63). Original WrongGPU code. +// +// The rule (the same for the RR runtime, the front end and Model M's executor): +// 1. an object a recorded frame may use is released only when that frame's completion is proven (here: the +// runtime-wide frame marker has reached the frame's serial), never because a wait timed out; +// 2. until then the object stays on this list; when the device is dropped or removed nothing of it runs any more, +// and everything left is released then (at process exit the system reclaims the rest); +// 3. no caller waits here: retiring and pumping only compare serials. +// +// Header-only and free of D3D12 so that the CPU test (clean/rr_f/runtime/tests/test_rr_retire.py) builds it with a +// fake object on any host. T is any type with Release() (IUnknown in the runtime); "counted" marks the objects the +// runtime counts in its live-resource total (the buffers and textures it made, not its heaps), and the functions +// return how many counted objects they released so the caller can keep that total. +#pragma once + +#include +#include + +#include + +namespace wgrr_rt { + +template +class RetireList { + public: + struct Entry { + uint64_t serial; // the last frame that may use obj + T* obj; + bool counted; + }; + + // Hands obj over: released at once when the frame `serial` has completed (done >= serial), else kept until a + // pump() sees it completed. Returns the number of counted objects released now (0 or 1). A null obj is ignored. + int retire(T* obj, uint64_t serial, uint64_t done, bool counted) { + if (!obj) return 0; + if (serial <= done) { + obj->Release(); + return counted ? 1 : 0; + } + entries_.push_back({serial, obj, counted}); + return 0; + } + + // Releases every object whose frame has completed. Returns the number of counted objects released. + int pump(uint64_t done) { + int counted = 0; + size_t keep = 0; + for (size_t i = 0; i < entries_.size(); ++i) { + Entry& e = entries_[i]; + if (e.serial <= done) { + e.obj->Release(); + if (e.counted) ++counted; + } else { + entries_[keep++] = e; + } + } + entries_.resize(keep); + return counted; + } + + // The device is gone (dropped, or removed: its queues run nothing any more): releases everything left. Returns the + // number of counted objects released. + int release_all() { return pump(UINT64_MAX); } + + // true when an object retired with `serial` would still be kept (its frame has not completed) + static bool in_flight(uint64_t serial, uint64_t done) { return serial > done; } + + size_t size() const { return entries_.size(); } + bool empty() const { return entries_.empty(); } + + private: + std::vector entries_; +}; + +// Instance destroy: the objects the instance's recorded frames may use. Buffers (counted) go to the list with the +// instance's last serial; the shader-visible descriptor heap and the timestamp query heap are released here (the +// caller has waited for the serial, bounded). Returns the number of counted objects released now. +template +int retire_instance(RetireList& l, T* const* buffers, size_t nb, T* const* heaps, size_t nh, uint64_t serial, + uint64_t done) { + int counted = 0; + for (size_t i = 0; i < nb; ++i) counted += l.retire(buffers[i], serial, done, true); + for (size_t i = 0; i < nh; ++i) + if (heaps[i]) heaps[i]->Release(); + return counted; +} + +} // namespace wgrr_rt diff --git a/clean/rr_f/runtime/src/rr_runtime.cpp b/clean/rr_f/runtime/src/rr_runtime.cpp index c642a468..6e6d2650 100644 --- a/clean/rr_f/runtime/src/rr_runtime.cpp +++ b/clean/rr_f/runtime/src/rr_runtime.cpp @@ -52,6 +52,7 @@ #include "rr_model.h" #include "rr_runtime_test.h" #include "rr_modes.h" +#include "rr_retire.h" #include "../../proxy/include/wg_settings_reader.h" #include "../../../runtime/interop/descriptor_publish.h" @@ -378,8 +379,6 @@ const StageDef kStages[15] = { {"D1-F", K_D1F, 1, 64, 'f', 0}, {"D1-R", K_D1R, 1, 64, 'r', 0}, {"D0-F", K_D0F, 0, 32, 'f', 0}}; // ---------------------------------------------------------------------------------------------------- runtime state -struct Deferred { uint64_t serial; ID3D12Resource* res; }; - struct Runtime { std::recursive_mutex mu; bool init_done = false; @@ -429,7 +428,7 @@ struct Runtime { uint64_t ts_freq = 0; std::string adapter_name; bool ready_logged = false; - std::vector deferred; + RetireList deferred; // objects kept until the frame marker proves their frames done (rr_retire.h) std::vector transient; // retain_transient copies alive std::atomic live_resources{0}; std::atomic created_resources{0}; @@ -504,21 +503,11 @@ uint64_t marker_done() { // releases deferred resources whose frames have completed void pump() { Runtime& rt = R(); - const uint64_t done = marker_done(); - auto it = rt.deferred.begin(); - while (it != rt.deferred.end()) { - if (it->serial <= done) { - drop(it->res); - it = rt.deferred.erase(it); - } else { - ++it; - } - } + rt.live_resources -= rt.deferred.pump(marker_done()); } void defer_release(ID3D12Resource*& r, uint64_t serial) { - if (!r) return; - if (serial <= marker_done()) drop(r); - else R().deferred.push_back({serial, r}), r = nullptr; + R().live_resources -= R().deferred.retire(r, serial, marker_done(), true); + r = nullptr; } bool wait_serial(uint64_t serial, DWORD ms) { for (DWORD i = 0; i < ms && marker_done() < serial; ++i) { @@ -1078,8 +1067,7 @@ void stop_compile() { void drop_device() { Runtime& rt = R(); stop_compile(); - for (auto& d : rt.deferred) drop(d.res); - rt.deferred.clear(); + rt.live_resources -= rt.deferred.release_all(); release(rt.pass_heap); drop(rt.pass_dummy); for (uint64_t& s : rt.pass_serial) s = 0; @@ -1709,17 +1697,18 @@ void render_layout(Layout* L, uint32_t rw, uint32_t rh) { } void destroy_instance_resources(Instance* in, uint64_t serial) { - defer_release(in->arena, serial); + Runtime& rt = R(); if (in->upload) in->upload->Unmap(0, nullptr), in->upload_map = nullptr; - defer_release(in->upload, serial); if (in->qrb) in->qrb->Unmap(0, nullptr), in->qmap = nullptr; - defer_release(in->qrb, serial); - for (OutTarget& o : in->out) defer_release(o.proxy, serial); // descriptor and query heaps: no Release while frames are in flight (bounded wait, then release) if (!wait_serial(serial, 2000)) log_line(fmt("instance %u: frames still in flight at destroy; heaps released after 2 s", in->id)); - release(in->heap); - release(in->null_tmpl); - release(in->qh); + IUnknown* bufs[] = {in->arena, in->upload, in->qrb, in->out[0].proxy, in->out[1].proxy}; + IUnknown* heaps[] = {in->heap, in->qh}; + rt.live_resources -= retire_instance(rt.deferred, bufs, 5, heaps, 2, serial, marker_done()); + in->arena = in->upload = in->qrb = in->out[0].proxy = in->out[1].proxy = nullptr; + in->heap = nullptr; + in->qh = nullptr; + release(in->null_tmpl); // CPU-only heap, a copy source at record time: never used by the GPU } bool new_arena(Instance* in, uint64_t bytes, std::string* err) { diff --git a/clean/rr_f/runtime/tests/rr_retire_test.cpp b/clean/rr_f/runtime/tests/rr_retire_test.cpp new file mode 100644 index 00000000..d65c2028 --- /dev/null +++ b/clean/rr_f/runtime/tests/rr_retire_test.cpp @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: MIT +// CPU test of the RR runtime's retire list (src/rr_retire.h, issue #63): objects a recorded frame may use are released +// only when the frame marker proves that frame complete. Fake objects count their Release calls; the marker is a +// plain number. Built and run by test_rr_retire.py; exit status 0 when every check passes. Original WrongGPU code. +#include + +#include "rr_retire.h" + +namespace { + +struct Fake { + int releases = 0; + unsigned long Release() { return (unsigned long)++releases; } +}; + +int g_failed = 0; +void check(bool ok, const char* what) { + printf("%s: %s\n", ok ? "ok " : "FAIL", what); + if (!ok) ++g_failed; +} + +void heap_waits_for_marker() { + wgrr_rt::RetireList l; + Fake heap; + int c = l.retire(&heap, 5, 4, false); + check(heap.releases == 0 && c == 0 && l.size() == 1, "heap retired at serial 5 with the marker at 4: not released"); + c = l.pump(4); + check(heap.releases == 0 && c == 0 && l.size() == 1, "pump with the marker still at 4: not released"); + c = l.pump(5); + check(heap.releases == 1 && c == 0 && l.empty(), "marker 5, pump: released once (not counted)"); + l.pump(6); + check(heap.releases == 1, "a later pump does not release it again"); +} + +void completed_frame_releases_at_once() { + wgrr_rt::RetireList l; + Fake buf; + const int c = l.retire(&buf, 5, 5, true); + check(buf.releases == 1 && c == 1 && l.empty(), "buffer retired at serial 5 with the marker at 5: released at once, counted"); + check(l.retire(nullptr, 9, 0, true) == 0 && l.empty(), "a null object is ignored"); +} + +void destroy_keeps_everything_until_complete() { + // the instance destroy of issue #63: arena, upload, query readback, two output proxies (counted) and the + // shader-visible descriptor heap and timestamp query heap, all used by the frame with serial 5 + wgrr_rt::RetireList l; + Fake arena, upload, qrb, proxy, heap, qh; + Fake* bufs[5] = {&arena, &upload, &qrb, &proxy, nullptr}; + Fake* heaps[2] = {&heap, &qh}; + int c = wgrr_rt::retire_instance(l, bufs, 5, heaps, 2, 5, 4); + check(c == 0, "destroy with the marker at 4: no counted object released"); + check(arena.releases + upload.releases + qrb.releases + proxy.releases == 0, "destroy with the marker at 4: no buffer released"); + check(heap.releases == 0, "destroy with the marker at 4: descriptor heap not released"); + check(qh.releases == 0, "destroy with the marker at 4: query heap not released"); + c = l.pump(5); + check(c == 4, "marker 5, pump: the four buffers released and counted"); + check(arena.releases == 1 && upload.releases == 1 && qrb.releases == 1 && proxy.releases == 1 && heap.releases == 1 && + qh.releases == 1 && l.empty(), + "marker 5, pump: every object released exactly once"); +} + +void destroy_after_completion_releases_at_once() { + wgrr_rt::RetireList l; + Fake arena, heap, qh; + Fake* bufs[1] = {&arena}; + Fake* heaps[2] = {&heap, &qh}; + const int c = wgrr_rt::retire_instance(l, bufs, 1, heaps, 2, 5, 7); + check(c == 1 && arena.releases == 1 && heap.releases == 1 && qh.releases == 1 && l.empty(), + "destroy with the frame complete: everything released at once"); +} + +void partial_pump_and_device_drop() { + wgrr_rt::RetireList l; + Fake a, b, h; + l.retire(&a, 3, 1, true); + l.retire(&b, 8, 1, true); + l.retire(&h, 8, 1, false); + int c = l.pump(3); + check(c == 1 && a.releases == 1 && b.releases == 0 && h.releases == 0 && l.size() == 2, "pump to 3 releases only serial 3"); + check(wgrr_rt::RetireList::in_flight(8, 3) && !wgrr_rt::RetireList::in_flight(3, 3), "in_flight compares serials"); + c = l.release_all(); // device dropped or removed: the frame can never run + check(c == 1 && b.releases == 1 && h.releases == 1 && l.empty(), "release_all (device dropped / removed) releases the rest once"); +} + +} // namespace + +int main() { + heap_waits_for_marker(); + completed_frame_releases_at_once(); + destroy_keeps_everything_until_complete(); + destroy_after_completion_releases_at_once(); + partial_pump_and_device_drop(); + printf("%s (%d failed)\n", g_failed ? "FAILED" : "passed", g_failed); + return g_failed ? 1 : 0; +} diff --git a/clean/rr_f/runtime/tests/test_rr_retire.py b/clean/rr_f/runtime/tests/test_rr_retire.py new file mode 100644 index 00000000..c51a954b --- /dev/null +++ b/clean/rr_f/runtime/tests/test_rr_retire.py @@ -0,0 +1,84 @@ +# SPDX-License-Identifier: MIT +"""CPU test of the RR runtime's retire list (clean/rr_f/runtime/src/rr_retire.h, issue #63): an object that a frame +recorded into the game's command list may use (buffers, the shader-visible descriptor heap, the timestamp query heap) +is released only when the frame marker proves that frame complete, never because a wait ran out; device drop releases +the rest. rr_retire_test.cpp drives the list with fake objects that count Release and a fake marker value. + +The program is compiled with the first available C++17 compiler: $CXX, clang++ / g++ on PATH, MSVC cl on PATH (a +developer prompt), then MSVC through vswhere and vcvars64.bat. Skipped when there is none. + +Run: python -m unittest clean.rr_f.runtime.tests.test_rr_retire +""" +import os +import shutil +import subprocess +import tempfile +import unittest + +HERE = os.path.dirname(os.path.abspath(__file__)) +SRC = os.path.join(os.path.dirname(HERE), "src") +TEST_CPP = os.path.join(HERE, "rr_retire_test.cpp") +EXE = ".exe" if os.name == "nt" else "" +NO_WINDOW = getattr(subprocess, "CREATE_NO_WINDOW", 0) + + +def _run(cmd, **kw): + return subprocess.run(cmd, capture_output=True, text=True, creationflags=NO_WINDOW, **kw) + + +def _gcc_style(cxx): + def build(out): + r = _run([cxx, "-std=c++17", "-O2", "-Wall", "-I" + SRC, TEST_CPP, "-o", out]) + if r.returncode != 0: + raise AssertionError("compile failed:\n" + r.stdout + r.stderr) + return build + + +def _cl(prefix): + def build(out): + d = os.path.dirname(out) + cmd = 'cl /nologo /std:c++17 /O2 /EHsc /W4 /I"%s" "%s" /Fo"%s\\\\" /Fe"%s"' % (SRC, TEST_CPP, d, out) + bat = os.path.join(d, "build.bat") + with open(bat, "w") as f: + f.write("@echo off\r\n" + (prefix or "") + cmd + " || exit /b 1\r\n") + r = _run(["cmd", "/c", bat]) + if r.returncode != 0: + raise AssertionError("compile failed:\n" + r.stdout + r.stderr) + return build + + +def find_compiler(): + cxx = os.environ.get("CXX") + if cxx: + return _gcc_style(cxx) + for name in ("clang++", "g++"): + p = shutil.which(name) + if p: + return _gcc_style(p) + if os.name == "nt": + if shutil.which("cl"): + return _cl("") + vswhere = r"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" + if os.path.exists(vswhere): + r = _run([vswhere, "-latest", "-property", "installationPath"]) + vcvars = os.path.join(r.stdout.strip(), "VC", "Auxiliary", "Build", "vcvars64.bat") + if r.returncode == 0 and r.stdout.strip() and os.path.exists(vcvars): + return _cl('call "%s" >nul || exit /b 1\r\n' % vcvars) + return None + + +class TestRrRetire(unittest.TestCase): + def test_retire_until_complete(self): + build = find_compiler() + if build is None: + self.skipTest("no C++ compiler (clang++, g++ or MSVC)") + with tempfile.TemporaryDirectory(prefix="wg_rr_retire_") as d: + exe = os.path.join(d, "rr_retire_test" + EXE) + build(exe) + r = _run([exe]) + self.assertEqual(r.returncode, 0, "rr_retire_test failed:\n" + r.stdout + r.stderr) + self.assertIn("passed", r.stdout) + + +if __name__ == "__main__": + unittest.main() From 94e02b05db8904a30b2912d4440d4d91bd8edf1a Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:27:33 -0500 Subject: [PATCH 2/5] RR runtime: keep an instance's heaps until its last frame completes; no wait at destroy Fixes the premature release of issue #63: - the shader-visible descriptor heap and the timestamp query heap go to the retire list with the instance's last serial, as its buffers do (not counted in live resources); they are released when the frame marker reaches that serial, at device drop, or once the device is removed (pump releases everything kept then); never on a timeout; - a destroy while that frame is still in flight logs one line and keeps the objects; - the 2 s wait before the heap release is gone, and so is the 2 s wait for the timing harvest in destroy_locked: destroy harvests what has completed and waits for nothing while it holds the runtime-wide lock. test_rr_retire now passes. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- BUILDING.md | 1 + clean/rr_f/runtime/src/rr_retire.h | 10 +++++----- clean/rr_f/runtime/src/rr_runtime.cpp | 23 ++++++++++++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/BUILDING.md b/BUILDING.md index 46d3f5c4..ce82a0fa 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -150,6 +150,7 @@ python -m unittest discover -s clean\rr_f\head\tests -t . rem Folders with GPU modules (clean\k\tests, clean\rr_f\runtime\tests): one module at a time, leaving out the GPU rem modules listed below, e.g. python -m unittest clean.k.tests.test_k_reference clean.k.tests.test_k_runtime_tables clean.k.tests.test_k_runtime_pack +python -m unittest clean.rr_f.runtime.tests.test_rr_retire rem Test folders without __init__.py (clean\dx12\tests, clean\fsr\tests, clean\rr_f\proxy\tests) run from their own rem folder, one file at a time where the folder has a GPU module: python -m unittest discover -s clean\dx12\tests -t clean\dx12\tests diff --git a/clean/rr_f/runtime/src/rr_retire.h b/clean/rr_f/runtime/src/rr_retire.h index 44272708..7d9ad2e2 100644 --- a/clean/rr_f/runtime/src/rr_retire.h +++ b/clean/rr_f/runtime/src/rr_retire.h @@ -74,16 +74,16 @@ class RetireList { std::vector entries_; }; -// Instance destroy: the objects the instance's recorded frames may use. Buffers (counted) go to the list with the -// instance's last serial; the shader-visible descriptor heap and the timestamp query heap are released here (the -// caller has waited for the serial, bounded). Returns the number of counted objects released now. +// Instance destroy: every object the instance's recorded frames may use goes to the list with the instance's last +// serial, the shader-visible descriptor heap and the timestamp query heap (the game's list dispatches with the one and +// runs EndQuery / ResolveQueryData on the other) exactly as the buffers; only the buffers are counted. Nothing waits: +// what the marker has not reached yet stays on the list. Returns the number of counted objects released now. template int retire_instance(RetireList& l, T* const* buffers, size_t nb, T* const* heaps, size_t nh, uint64_t serial, uint64_t done) { int counted = 0; for (size_t i = 0; i < nb; ++i) counted += l.retire(buffers[i], serial, done, true); - for (size_t i = 0; i < nh; ++i) - if (heaps[i]) heaps[i]->Release(); + for (size_t i = 0; i < nh; ++i) counted += l.retire(heaps[i], serial, done, false); return counted; } diff --git a/clean/rr_f/runtime/src/rr_runtime.cpp b/clean/rr_f/runtime/src/rr_runtime.cpp index 6e6d2650..027db64a 100644 --- a/clean/rr_f/runtime/src/rr_runtime.cpp +++ b/clean/rr_f/runtime/src/rr_runtime.cpp @@ -500,10 +500,16 @@ uint64_t marker_done() { if (v > rt.serial) v -= 0x100000000ull; return v; } -// releases deferred resources whose frames have completed +// releases deferred objects whose frames have completed; once the device is removed nothing of it runs any more and +// everything left is released (issue #63: kept objects go at completion, device removal or device drop, never on a timeout) void pump() { Runtime& rt = R(); rt.live_resources -= rt.deferred.pump(marker_done()); + if (!rt.deferred.empty() && rt.dev && FAILED(rt.dev->GetDeviceRemovedReason())) { + const size_t n = rt.deferred.size(); + rt.live_resources -= rt.deferred.release_all(); + log_line(fmt("device removed: %zu kept object(s) released", n)); + } } void defer_release(ID3D12Resource*& r, uint64_t serial) { R().live_resources -= R().deferred.retire(r, serial, marker_done(), true); @@ -1696,15 +1702,20 @@ void render_layout(Layout* L, uint32_t rw, uint32_t rh) { L->state_end = o; } +// issue #63: every object the instance's recorded frames may use, the descriptor and query heaps included, is released +// only when the frame marker reaches the instance's last serial (pump), or at device removal / device drop. No wait here: +// a frame not yet submitted (destroy in the middle of the game's frame) keeps them instead of stalling the game thread. void destroy_instance_resources(Instance* in, uint64_t serial) { Runtime& rt = R(); if (in->upload) in->upload->Unmap(0, nullptr), in->upload_map = nullptr; if (in->qrb) in->qrb->Unmap(0, nullptr), in->qmap = nullptr; - // descriptor and query heaps: no Release while frames are in flight (bounded wait, then release) - if (!wait_serial(serial, 2000)) log_line(fmt("instance %u: frames still in flight at destroy; heaps released after 2 s", in->id)); + const uint64_t done = marker_done(); + if (RetireList::in_flight(serial, done)) + log_line(fmt("instance %u: frame %llu still in flight at destroy (marker at %llu); its buffers and heaps are kept until " + "it completes, or until device removal", in->id, (unsigned long long)serial, (unsigned long long)done)); IUnknown* bufs[] = {in->arena, in->upload, in->qrb, in->out[0].proxy, in->out[1].proxy}; IUnknown* heaps[] = {in->heap, in->qh}; - rt.live_resources -= retire_instance(rt.deferred, bufs, 5, heaps, 2, serial, marker_done()); + rt.live_resources -= retire_instance(rt.deferred, bufs, 5, heaps, 2, serial, done); in->arena = in->upload = in->qrb = in->out[0].proxy = in->out[1].proxy = nullptr; in->heap = nullptr; in->qh = nullptr; @@ -2320,7 +2331,9 @@ void harvest(Instance* in) { void destroy_locked(Instance* in) { Runtime& rt = R(); if (!in) return; - if (wait_serial(in->last_serial, 2000)) harvest(in); + // the last frames' timings: harvest what has completed; no wait (rt.mu is held, and a frame not yet submitted would + // never complete during it; issue #63). Frames still in flight are left out of the last window. + harvest(in); log_timing(in); // the last, partial window log_line(fmt("destroy instance %u after %llu frames (%llu failed)", in->id, (unsigned long long)in->frames, (unsigned long long)in->failed)); From c0f8d8e68905231d614ad3ba2416866d513e46cd Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:38:50 -0500 Subject: [PATCH 3/5] RR runtime: per-list completion; old device kept until its lists complete Review of #82: a later serial does not prove that an earlier list ran (recording order is not submission order), and a device switch released the old device's in-flight objects without proof. - Every list the runtime records into (frame, pass-through, transient copy) writes its serial into a marker slot of its own (rr_retire.h ListMarks); a slot is reused only after its holder proved complete. - An object is released only when every list that may use it proved complete: instance objects wait for all the instance's lists, an old arena or output texture for the instance's lists, a transient copy for its copy list and every frame / pass-through that read it. - Frame and pass-through slots are taken only when their last list proved complete (no "serial % 8" reuse on a later serial). - Device switch: the old device's pending lists, deferred objects, shared pipelines / buffers, marker buffer and device reference are parked until each list proved complete or the device is removed; at most 4 old devices are tracked, beyond that the oldest is left allocated (never released early); both logged. - CPU test: out-of-order submission, a never-submitted list, slot reuse, device switch with work pending, removal, the bound (27 checks). Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/rr_f/runtime/src/rr_retire.h | 247 ++++++++++++-- clean/rr_f/runtime/src/rr_runtime.cpp | 348 ++++++++++++++------ clean/rr_f/runtime/tests/rr_retire_test.cpp | 260 +++++++++++---- clean/rr_f/runtime/tests/test_rr_retire.py | 11 +- 4 files changed, 672 insertions(+), 194 deletions(-) diff --git a/clean/rr_f/runtime/src/rr_retire.h b/clean/rr_f/runtime/src/rr_retire.h index 7d9ad2e2..3bf9960e 100644 --- a/clean/rr_f/runtime/src/rr_retire.h +++ b/clean/rr_f/runtime/src/rr_retire.h @@ -1,71 +1,174 @@ // SPDX-License-Identifier: MIT -// WrongGPU RR runtime: objects retired while a frame recorded into the game's command list may still use them -// (issue #63). Original WrongGPU code. +// WrongGPU RR runtime: objects retired while command lists recorded by the runtime may still use them (issue #63). +// Original WrongGPU code. +// +// Completion is proven PER LIST. Every command list the runtime records into (a frame, a pass-through, a transient +// copy) gets a marker slot of its own and ends with a GPU write of its serial into that slot. A list is complete only +// when its own slot holds its serial. A later list's marker proves nothing about an earlier one: the game decides the +// submission order, and recording order is not submission order (a list recorded first can be submitted last, on +// another queue, or never). A slot is handed to a new list only after the list that held it proved complete, so a +// slot's value always belongs to its current holder. // // The rule (the same for the RR runtime, the front end and Model M's executor): -// 1. an object a recorded frame may use is released only when that frame's completion is proven (here: the -// runtime-wide frame marker has reached the frame's serial), never because a wait timed out; -// 2. until then the object stays on this list; when the device is dropped or removed nothing of it runs any more, -// and everything left is released then (at process exit the system reclaims the rest); -// 3. no caller waits here: retiring and pumping only compare serials. +// 1. an object is released only when every list that may use it has proved complete, never because a wait timed +// out, and never because some other list completed; +// 2. until then it stays on the list; when its device is removed nothing of it runs any more and everything left is +// released then (at process exit the system reclaims the rest); +// 3. no caller waits here: retiring and pumping only read the marker slots. +// +// A device switch (set-device with another device) keeps the old device's lists, marker slots and waiting objects +// (ParkedSet) until each list proved complete or the old device is removed. At most kMaxParked old devices are +// tracked; beyond that the oldest one's objects are left allocated without tracking (never released early). // -// Header-only and free of D3D12 so that the CPU test (clean/rr_f/runtime/tests/test_rr_retire.py) builds it with a -// fake object on any host. T is any type with Release() (IUnknown in the runtime); "counted" marks the objects the -// runtime counts in its live-resource total (the buffers and textures it made, not its heaps), and the functions -// return how many counted objects they released so the caller can keep that total. +// Header-only and free of D3D12 so that the CPU test (clean/rr_f/runtime/tests/test_rr_retire.py) builds it with fake +// objects and a fake marker buffer on any host. T is any type with Release() (IUnknown in the runtime); "counted" marks +// the objects the runtime counts in its live-resource total (the buffers and textures it made, not its heaps), and the +// functions return how many counted objects they released so the caller can keep that total. #pragma once #include #include +#include +#include +#include +#include #include namespace wgrr_rt { +// The marker slots of one device: which list holds which slot, and which lists have proved complete. +class ListMarks { + public: + explicit ListMarks(uint32_t slots = 1024) : occ_(slots, 0) {} + + // A new list: its serial (> 0), and in *slot the marker word it must write that serial into (the low 32 bits) as + // its last command. 0 when every slot is held by a list that has not proved complete (nothing may be recorded then). + uint64_t begin(uint32_t* slot) { + const uint32_t n = (uint32_t)occ_.size(); + for (uint32_t i = 0; i < n; ++i) { + const uint32_t s = (hint_ + i) % n; + if (occ_[s]) continue; + ++last_; + if ((uint32_t)last_ == 0) ++last_; // a fresh slot reads 0: never a serial whose low 32 bits are 0 + occ_[s] = last_; + pending_.emplace(last_, s); + hint_ = (s + 1) % n; + *slot = s; + return last_; + } + return 0; + } + + // A list that recorded nothing after all (the caller failed before recording any command into it): its slot is free + // again and the serial counts as complete. Never for a list that recorded commands. + void cancel(uint64_t serial) { + auto it = pending_.find(serial); + if (it == pending_.end()) return; + occ_[it->second] = 0; + pending_.erase(it); + } + + // Reads the marker slots (the device's mapped marker words): a list whose slot holds its serial is complete. + void poll(const volatile uint32_t* words) { + if (!words) return; + for (auto it = pending_.begin(); it != pending_.end();) { + if (words[it->second] == (uint32_t)it->first) { + occ_[it->second] = 0; + it = pending_.erase(it); + } else { + ++it; + } + } + } + + // true when list `serial` proved complete (as of the last poll), or was cancelled; serial 0 = no list. + bool done(uint64_t serial) const { return serial == 0 || (serial <= last_ && pending_.find(serial) == pending_.end()); } + + size_t pending() const { return pending_.size(); } + uint64_t last() const { return last_; } + uint32_t slots() const { return (uint32_t)occ_.size(); } + // the serials of every list that has not proved complete + std::vector pending_serials() const { + std::vector v; + v.reserve(pending_.size()); + for (const auto& kv : pending_) v.push_back(kv.first); + std::sort(v.begin(), v.end()); + return v; + } + + private: + std::vector occ_; // the serial holding each slot, 0 = free + std::unordered_map pending_; // serial -> slot of every list not proved complete + uint64_t last_ = 0; + uint32_t hint_ = 0; +}; + +// Drops the serials of lists that have proved complete (users lists stay short). +inline void prune_done(std::vector& users, const ListMarks& m) { + users.erase(std::remove_if(users.begin(), users.end(), [&](uint64_t s) { return m.done(s); }), users.end()); +} + template class RetireList { public: struct Entry { - uint64_t serial; // the last frame that may use obj + std::vector users; // the lists that may use obj and have not proved complete T* obj; bool counted; }; - // Hands obj over: released at once when the frame `serial` has completed (done >= serial), else kept until a - // pump() sees it completed. Returns the number of counted objects released now (0 or 1). A null obj is ignored. - int retire(T* obj, uint64_t serial, uint64_t done, bool counted) { + // Hands obj over: released at once when every list in `users` has proved complete, else kept until a pump() sees + // them all complete. Returns the number of counted objects released now (0 or 1). A null obj is ignored. + int retire(T* obj, std::vector users, const ListMarks& m, bool counted) { if (!obj) return 0; - if (serial <= done) { + prune_done(users, m); + if (users.empty()) { obj->Release(); return counted ? 1 : 0; } - entries_.push_back({serial, obj, counted}); + entries_.push_back({std::move(users), obj, counted}); return 0; } - // Releases every object whose frame has completed. Returns the number of counted objects released. - int pump(uint64_t done) { + // Releases every object whose lists have all proved complete. Returns the number of counted objects released. + int pump(const ListMarks& m) { int counted = 0; size_t keep = 0; for (size_t i = 0; i < entries_.size(); ++i) { Entry& e = entries_[i]; - if (e.serial <= done) { + prune_done(e.users, m); + if (e.users.empty()) { e.obj->Release(); if (e.counted) ++counted; } else { - entries_[keep++] = e; + if (keep != i) entries_[keep] = std::move(e); + ++keep; } } entries_.resize(keep); return counted; } - // The device is gone (dropped, or removed: its queues run nothing any more): releases everything left. Returns the - // number of counted objects released. - int release_all() { return pump(UINT64_MAX); } + // The device is removed (its queues run nothing any more): releases everything left. Returns the number of counted + // objects released. + int release_all() { + int counted = 0; + for (Entry& e : entries_) { + e.obj->Release(); + if (e.counted) ++counted; + } + entries_.clear(); + return counted; + } - // true when an object retired with `serial` would still be kept (its frame has not completed) - static bool in_flight(uint64_t serial, uint64_t done) { return serial > done; } + // Forgets every entry WITHOUT releasing it (an old device beyond the ParkedSet bound: left allocated, never freed + // early). Returns the number of objects forgotten. + size_t abandon() { + const size_t n = entries_.size(); + entries_.clear(); + return n; + } size_t size() const { return entries_.size(); } bool empty() const { return entries_.empty(); } @@ -74,17 +177,93 @@ class RetireList { std::vector entries_; }; -// Instance destroy: every object the instance's recorded frames may use goes to the list with the instance's last -// serial, the shader-visible descriptor heap and the timestamp query heap (the game's list dispatches with the one and -// runs EndQuery / ResolveQueryData on the other) exactly as the buffers; only the buffers are counted. Nothing waits: -// what the marker has not reached yet stays on the list. Returns the number of counted objects released now. +// Instance destroy: every object the instance's recorded lists may use goes to the list with the instance's lists not +// yet proved complete, the shader-visible descriptor heap and the timestamp query heap (the game's list dispatches with +// the one and runs EndQuery / ResolveQueryData on the other) exactly as the buffers; only the buffers are counted. +// Nothing waits. Returns the number of counted objects released now. template -int retire_instance(RetireList& l, T* const* buffers, size_t nb, T* const* heaps, size_t nh, uint64_t serial, - uint64_t done) { +int retire_instance(RetireList& l, T* const* buffers, size_t nb, T* const* heaps, size_t nh, + const std::vector& users, const ListMarks& m) { int counted = 0; - for (size_t i = 0; i < nb; ++i) counted += l.retire(buffers[i], serial, done, true); - for (size_t i = 0; i < nh; ++i) counted += l.retire(heaps[i], serial, done, false); + for (size_t i = 0; i < nb; ++i) counted += l.retire(buffers[i], users, m, true); + for (size_t i = 0; i < nh; ++i) counted += l.retire(heaps[i], users, m, false); return counted; } +// An old device after a device switch: its lists' marker slots, the objects waiting on them, and what goes last (the +// marker buffer the lists write, then the device reference) once every list proved complete or the device is removed. +template +struct Parked { + uint64_t id = 0; // device generation, for the log + ListMarks marks; + RetireList deferred; + const volatile uint32_t* words = nullptr; // the old device's marker slots (mapped; owned by `last`) + std::vector last; // released after everything else, in order + int last_counted = 0; // how many of `last` count as live resources +}; + +template +class ParkedSet { + public: + static constexpr size_t kMaxParked = 4; + + // Takes an old device's state. Returns a log line. Beyond kMaxParked the oldest one is abandoned: its objects are + // left allocated without tracking (memory reclaimed at process exit), never released without proof. + std::string park(Parked&& p) { + std::string msg = "device generation " + std::to_string(p.id) + " switched away with " + + std::to_string(p.marks.pending()) + " list(s) not proved complete: " + + std::to_string(p.deferred.size() + p.last.size()) + + " object(s) kept until each list completes or the device is removed"; + set_.push_back(std::move(p)); + if (set_.size() > kMaxParked) { + Parked& o = set_.front(); + const size_t n = o.deferred.abandon() + o.last.size(); + msg += "; more than " + std::to_string(kMaxParked) + " old devices kept: generation " + std::to_string(o.id) + + " (" + std::to_string(o.marks.pending()) + " list(s) never proved complete) has its " + std::to_string(n) + + " object(s) left allocated, no longer tracked"; + set_.erase(set_.begin()); + } + return msg; + } + + // Polls each old device's marker slots and releases what has proved complete; removed(p) tells whether p's device + // is removed (then everything of it is released). An old device whose lists all proved complete is released in full. + // Returns the number of counted objects released; log lines go to *log. + template + int pump(Removed removed, std::vector* log) { + int counted = 0; + for (size_t i = 0; i < set_.size();) { + Parked& p = set_[i]; + p.marks.poll(p.words); + counted += p.deferred.pump(p.marks); + const bool gone = removed(p); + if (gone || p.marks.pending() == 0) { + const size_t n = p.deferred.size() + p.last.size(); + counted += p.deferred.release_all(); // empty unless the device is removed + for (T* o : p.last) + if (o) o->Release(); + counted += p.last_counted; + if (log) + log->push_back("device generation " + std::to_string(p.id) + + (gone ? ": device removed, " : ": every list proved complete, ") + std::to_string(n) + + " kept object(s) released"); + set_.erase(set_.begin() + (ptrdiff_t)i); + } else { + ++i; + } + } + return counted; + } + + size_t size() const { return set_.size(); } + bool empty() const { return set_.empty(); } + size_t objects() const { + size_t n = 0; + for (const Parked& p : set_) n += p.deferred.size() + p.last.size(); + return n; + } + private: + std::vector> set_; +}; + } // namespace wgrr_rt diff --git a/clean/rr_f/runtime/src/rr_runtime.cpp b/clean/rr_f/runtime/src/rr_runtime.cpp index 68176c64..69535587 100644 --- a/clean/rr_f/runtime/src/rr_runtime.cpp +++ b/clean/rr_f/runtime/src/rr_runtime.cpp @@ -17,8 +17,10 @@ // render-size change (53.9). The frame's work buffers are placed by their lifetimes (buffers that are never live at // the same pass share memory). The host bookkeeping of page 53 (par, fc, pj, CP, pending resets) is the proxy's. // -// Completion: one runtime-wide marker (WriteBufferImmediate after each recorded frame) tells which frames the game's -// queue has finished; per-frame upload / descriptor / timestamp slots and every deferred release wait for it. +// Completion: every list the runtime records into (frame, pass-through, transient copy) ends with a write of its serial +// into a marker slot of its own (rr_retire.h ListMarks); a list is complete when its own slot holds its serial, never +// because a later list completed. Per-frame upload / descriptor / timestamp slots and every deferred release wait for +// the lists that use them. // Log: wronggpu-rr.log next to this library (the proxy's directory). #define WIN32_LEAN_AND_MEAN #define NOMINMAX @@ -96,6 +98,11 @@ constexpr uint32_t kPoolTableBytes = 2050 * 4, kExp2TableBytes = 8192; // motion (4) and history (32) images, no linear / trace outputs (8) constexpr uint32_t kOutFastFlags = 1u | 2u | 4u | 8u | 32u; constexpr uint32_t kTimingWindow = 300; +// marker buffer: word 0 = the serial of the last list that finished (diagnostics only, the removal log), words 2, 3 = +// the GPU breadcrumbs; from word kMarkBase one completion slot per list in flight (rr_retire.h ListMarks) +constexpr uint32_t kMarkBase = 64; +constexpr uint32_t kMarkSlots = 1024; +constexpr uint32_t kMarkerBytes = 4 * (kMarkBase + kMarkSlots); const char* const kPassNames[kTs - 1] = {"pre", "input", "body", "head", "output", "resolve", "store"}; @@ -423,13 +430,19 @@ struct Runtime { ID3D12Resource* ags_buf = nullptr; ID3D12Resource* marker = nullptr; volatile uint32_t* marker_map = nullptr; - uint64_t serial = 0; // frames recorded into the game's lists (all instances) + uint64_t serial = 0; // the last list serial handed out on this device (frames, pass-throughs, copies) + ListMarks marks{kMarkSlots}; // per-list completion on this device (rr_retire.h) + uint64_t gen = 0; // device generation (set-device count), for transients and the parked old devices UINT inc = 0; uint64_t ts_freq = 0; std::string adapter_name; bool ready_logged = false; - RetireList deferred; // objects kept until the frame marker proves their frames done (rr_retire.h) - std::vector transient; // retain_transient copies alive + RetireList deferred; // objects kept until every list using them proved complete (rr_retire.h) + ParkedSet parked; // old devices' lists still in flight after a device switch (rr_retire.h) + // retain_transient copies alive: the copy, its device generation, and every list that may read it (the copy's own + // list, and each frame / pass-through that took it as an input) + struct Transient { ID3D12Resource* r; uint64_t gen; std::vector users; }; + std::vector transient; std::atomic live_resources{0}; std::atomic created_resources{0}; int live_instances = 0; @@ -492,35 +505,90 @@ void drop(ID3D12Resource*& r) { R().live_resources--; } } -uint64_t marker_done() { +// the completion slots of this device's lists (null before set-device) +const volatile uint32_t* mark_words() { return R().marker_map ? R().marker_map + kMarkBase : nullptr; } +// reads this device's completion slots +void poll_marks() { R().marks.poll(mark_words()); } +// starts a list on this device: its serial (0 when every completion slot is held by a list not yet proved complete; +// nothing may be recorded then) and the GPU address its marker write goes to +uint64_t begin_list(D3D12_GPU_VIRTUAL_ADDRESS* va) { Runtime& rt = R(); - if (!rt.marker_map) return rt.serial; - const uint32_t low = *rt.marker_map; - uint64_t v = (rt.serial & ~0xffffffffull) | low; - if (v > rt.serial) v -= 0x100000000ull; - return v; + poll_marks(); + uint32_t slot = 0; + const uint64_t s = rt.marks.begin(&slot); + if (!s) { + static bool logged = false; + if (!logged) { + logged = true; + log_line(fmt("all %u completion slots are held by command lists that have not completed (or were never submitted): " + "nothing is recorded until one completes (logged once)", kMarkSlots)); + } + return 0; + } + rt.serial = s; + *va = rt.marker->GetGPUVirtualAddress() + 4ull * (kMarkBase + slot); + return s; +} +// the list's last command: its serial into its own completion slot (and into word 0 for the removal log) +void end_list(ID3D12GraphicsCommandList2* l2, uint64_t serial, D3D12_GPU_VIRTUAL_ADDRESS va) { + const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER p[2] = {{va, (UINT)(serial & 0xffffffffu)}, + {R().marker->GetGPUVirtualAddress(), (UINT)(serial & 0xffffffffu)}}; + const D3D12_WRITEBUFFERIMMEDIATE_MODE mode[2] = {D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, + D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT}; + l2->WriteBufferImmediate(2, p, mode); +} +// a transient copy that list `serial` reads (evaluate, pass-through): one more list its release waits for +void note_transient_use(const void* res, uint64_t serial) { + for (Runtime::Transient& t : R().transient) + if (t.r == res && t.gen == R().gen) prune_done(t.users, R().marks), t.users.push_back(serial); } -// releases deferred objects whose frames have completed; once the device is removed nothing of it runs any more and -// everything left is released (issue #63: kept objects go at completion, device removal or device drop, never on a timeout) +// releases deferred objects whose lists have all proved complete; once the device is removed nothing of it runs any more +// and everything left is released (issue #63: kept objects go at completion or device removal, never on a timeout). +// The old devices of earlier device switches are pumped the same way. void pump() { Runtime& rt = R(); - rt.live_resources -= rt.deferred.pump(marker_done()); + poll_marks(); + rt.live_resources -= rt.deferred.pump(rt.marks); if (!rt.deferred.empty() && rt.dev && FAILED(rt.dev->GetDeviceRemovedReason())) { const size_t n = rt.deferred.size(); rt.live_resources -= rt.deferred.release_all(); log_line(fmt("device removed: %zu kept object(s) released", n)); } + if (!rt.parked.empty()) { + std::vector lines; + rt.live_resources -= rt.parked.pump( + [](Parked& p) { + ID3D12Device* d = p.last.empty() ? nullptr : (ID3D12Device*)p.last.back(); + return d && FAILED(d->GetDeviceRemovedReason()); + }, + &lines); + for (const std::string& s : lines) log_line(s); + } } -void defer_release(ID3D12Resource*& r, uint64_t serial) { - R().live_resources -= R().deferred.retire(r, serial, marker_done(), true); +void defer_release(ID3D12Resource*& r, const std::vector& users) { + poll_marks(); + R().live_resources -= R().deferred.retire(r, users, R().marks, true); r = nullptr; } -bool wait_serial(uint64_t serial, DWORD ms) { - for (DWORD i = 0; i < ms && marker_done() < serial; ++i) { - if (R().dev && FAILED(R().dev->GetDeviceRemovedReason())) return false; +// waits (polling, at most ms) until list `serial` proved complete; false on a timeout or a removed device +bool wait_list(uint64_t serial, DWORD ms) { + Runtime& rt = R(); + for (DWORD i = 0;; ++i) { + poll_marks(); + if (rt.marks.done(serial)) return true; + if (i >= ms || (rt.dev && FAILED(rt.dev->GetDeviceRemovedReason()))) return false; + Sleep(1); + } +} +// waits (polling, at most ms) until every list recorded on this device proved complete (test hooks only) +bool wait_all_lists(DWORD ms) { + Runtime& rt = R(); + for (DWORD i = 0;; ++i) { + poll_marks(); + if (!rt.marks.pending()) return true; + if (i >= ms || (rt.dev && FAILED(rt.dev->GetDeviceRemovedReason()))) return false; Sleep(1); } - return marker_done() >= serial; } // our own queue: create-time work only @@ -1070,10 +1138,56 @@ void stop_compile() { rt.all_ready = false; } +// Drops the device: set-device with another device (or a failed set-device), the end of prepare, test shutdown. +// Issue #63 / #82: the game's lists recorded on this device that have not proved complete may still run (releasing our +// device reference stops none of the game's queues). Then the device's deferred objects, the objects every such list +// uses (pipelines, root signature, model and AGS buffers, pass-through heap and buffer) and the marker buffer they write +// are parked with the device reference (rr_retire.h ParkedSet) until each list proved complete or the device is removed. void drop_device() { Runtime& rt = R(); stop_compile(); - rt.live_resources -= rt.deferred.release_all(); + if (rt.rm_thread.joinable()) SetEvent(rt.rm_quit), rt.rm_thread.join(); + release(rt.rm_fence); + if (rt.rm_ev) CloseHandle(rt.rm_ev), rt.rm_ev = nullptr; + if (rt.rm_quit) CloseHandle(rt.rm_quit), rt.rm_quit = nullptr; + pump(); + const bool removed = rt.dev && FAILED(rt.dev->GetDeviceRemovedReason()); + if (rt.marks.pending() && rt.marker && rt.marker_map && rt.dev && !removed) { + Parked p; + p.id = rt.gen; + const std::vector users = rt.marks.pending_serials(); + p.marks = std::move(rt.marks); + p.deferred = std::move(rt.deferred); + // live transient copies of this device: the plugin may still release them (then a no-op); kept for their lists + for (size_t i = 0; i < rt.transient.size();) { + Runtime::Transient& t = rt.transient[i]; + if (t.gen != rt.gen) { ++i; continue; } + rt.live_resources -= p.deferred.retire(t.r, t.users, p.marks, true); + rt.transient.erase(rt.transient.begin() + (ptrdiff_t)i); + } + auto keep = [&](IUnknown* o, bool counted) { if (o) rt.live_resources -= p.deferred.retire(o, users, p.marks, counted); }; + keep(rt.pass_heap, false); + keep(rt.pass_dummy, true); + for (int k = 0; k < K_COUNT; ++k) keep(rt.pso[k], false); + keep(rt.rs, false); + keep(rt.model_buf, true); + keep(rt.ags_buf, true); + rt.pass_heap = nullptr, rt.pass_dummy = rt.model_buf = rt.ags_buf = nullptr, rt.rs = nullptr; + for (int k = 0; k < K_COUNT; ++k) rt.pso[k] = nullptr; + p.words = rt.marker_map + kMarkBase; + p.last = {rt.marker, rt.dev}; // the marker buffer the lists write (still mapped), then the device reference + p.last_counted = 1; + rt.marker = nullptr, rt.marker_map = nullptr, rt.dev = nullptr; + log_line(rt.parked.park(std::move(p))); + rt.marks = ListMarks(kMarkSlots); + rt.deferred = RetireList(); + } else { + if (rt.marks.pending() || !rt.deferred.empty()) + log_line(fmt("device dropped%s: %zu kept object(s) released", removed ? " (removed)" : "", rt.deferred.size())); + rt.live_resources -= rt.deferred.release_all(); + rt.marks = ListMarks(kMarkSlots); + } + rt.gen++; // the next device's lists, serials and transient copies are a new generation release(rt.pass_heap); drop(rt.pass_dummy); for (uint64_t& s : rt.pass_serial) s = 0; @@ -1081,10 +1195,6 @@ void drop_device() { release(rt.rs); drop(rt.model_buf); drop(rt.ags_buf); - if (rt.rm_thread.joinable()) SetEvent(rt.rm_quit), rt.rm_thread.join(); - release(rt.rm_fence); - if (rt.rm_ev) CloseHandle(rt.rm_ev), rt.rm_ev = nullptr; - if (rt.rm_quit) CloseHandle(rt.rm_quit), rt.rm_quit = nullptr; if (rt.marker && rt.marker_map) rt.marker->Unmap(0, nullptr); // only a buffer that was mapped (set_device's Map can fail) rt.marker_map = nullptr; drop(rt.marker); @@ -1250,23 +1360,23 @@ uint32_t set_device_locked(ID3D12Device* given) { } if (FAILED(make_buffer(256, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COMMON, &rt.ags_buf)) || - FAILED(make_buffer(256, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST, + FAILED(make_buffer(kMarkerBytes, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST, &rt.marker))) { drop_device(); return WG_E_D3D_API; } { void* m = nullptr; - D3D12_RANGE r{0, 256}; + D3D12_RANGE r{0, kMarkerBytes}; // Issue #67: a failed Map leaves m null; the writes below would dereference it (the Model M runtime checks it too). if (FAILED(rt.marker->Map(0, &r, &m)) || !m) { drop_device(); return WG_E_D3D_API; } rt.marker_map = (volatile uint32_t*)m; - *rt.marker_map = 0; - rt.marker_map[2] = rt.marker_map[3] = 0; + for (uint32_t w = 0; w < kMarkerBytes / 4; ++w) rt.marker_map[w] = 0; rt.serial = 0; + rt.marks = ListMarks(kMarkSlots); } { char cv[8] = {}; @@ -1519,9 +1629,10 @@ struct Instance { ID3D12Resource* qrb = nullptr; uint64_t* qmap = nullptr; uint64_t slot_serial[kSlots] = {}; - bool slot_pending[kSlots] = {}; + bool slot_pending[kSlots] = {}; // the slot's list has not proved complete (or its timings not harvested yet) uint64_t slot_frame[kSlots] = {}; uint64_t last_serial = 0; + std::vector lists; // issue #82: the serials of this instance's lists not yet proved complete OutTarget out[2]; // 0 colour, 1 separate output alpha uint64_t frames = 0, failed = 0; std::string tlog_sig; // clean/release-060 crash-cp2077: the frame's image shapes at the last evaluate @@ -1706,20 +1817,29 @@ void render_layout(Layout* L, uint32_t rw, uint32_t rh) { L->state_end = o; } -// issue #63: every object the instance's recorded frames may use, the descriptor and query heaps included, is released -// only when the frame marker reaches the instance's last serial (pump), or at device removal / device drop. No wait here: -// a frame not yet submitted (destroy in the middle of the game's frame) keeps them instead of stalling the game thread. -void destroy_instance_resources(Instance* in, uint64_t serial) { +// the instance's lists that have not proved complete: every object of the instance waits for all of them +const std::vector& instance_lists(Instance* in) { + poll_marks(); + prune_done(in->lists, R().marks); + return in->lists; +} + +// issue #63 / #82: every object the instance's recorded lists may use, the descriptor and query heaps included, is +// released only when EACH of the instance's lists has proved complete (its own completion slot), or at device removal. +// No wait here: a frame not yet submitted (destroy in the middle of the game's frame) keeps them instead of stalling the +// game thread, however many later lists complete. +void destroy_instance_resources(Instance* in) { Runtime& rt = R(); if (in->upload) in->upload->Unmap(0, nullptr), in->upload_map = nullptr; if (in->qrb) in->qrb->Unmap(0, nullptr), in->qmap = nullptr; - const uint64_t done = marker_done(); - if (RetireList::in_flight(serial, done)) - log_line(fmt("instance %u: frame %llu still in flight at destroy (marker at %llu); its buffers and heaps are kept until " - "it completes, or until device removal", in->id, (unsigned long long)serial, (unsigned long long)done)); + const std::vector users = instance_lists(in); + if (!users.empty()) + log_line(fmt("instance %u: %zu of its command lists (serials %llu .. %llu) not proved complete at destroy; its buffers " + "and heaps are kept until each completes, or until device removal", + in->id, users.size(), (unsigned long long)users.front(), (unsigned long long)users.back())); IUnknown* bufs[] = {in->arena, in->upload, in->qrb, in->out[0].proxy, in->out[1].proxy}; IUnknown* heaps[] = {in->heap, in->qh}; - rt.live_resources -= retire_instance(rt.deferred, bufs, 5, heaps, 2, serial, done); + rt.live_resources -= retire_instance(rt.deferred, bufs, 5, heaps, 2, users, rt.marks); in->arena = in->upload = in->qrb = in->out[0].proxy = in->out[1].proxy = nullptr; in->heap = nullptr; in->qh = nullptr; @@ -2232,7 +2352,7 @@ uint32_t create_locked(const WgRrCreate* d, void** out) { } if (!ok) { log_line(fmt("create: failed (%s)", err.empty() ? "D3D12 objects" : err.c_str())); - destroy_instance_resources(in, 0); + destroy_instance_resources(in); // no list recorded: released at once delete in; return WG_E_D3D_API; } @@ -2296,9 +2416,9 @@ std::atomic g_frame_gpu_ms{0.0f}; // timestamps of every completed frame (never waits); one log line per 300 frames void harvest(Instance* in) { - const uint64_t done = marker_done(); + poll_marks(); for (uint32_t s = 0; s < kSlots; ++s) { - if (!in->slot_pending[s] || in->slot_serial[s] > done) continue; + if (!in->slot_pending[s] || !R().marks.done(in->slot_serial[s])) continue; in->slot_pending[s] = false; const uint64_t* t = in->qmap + s * kTq; const double f = 1000.0 / (double)R().ts_freq; @@ -2341,7 +2461,7 @@ void destroy_locked(Instance* in) { log_timing(in); // the last, partial window log_line(fmt("destroy instance %u after %llu frames (%llu failed)", in->id, (unsigned long long)in->frames, (unsigned long long)in->failed)); - destroy_instance_resources(in, in->last_serial); + destroy_instance_resources(in); rt.live_instances--; delete in; pump(); @@ -2551,7 +2671,7 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) OutTarget& t = in->out[k]; if (!o.on || o.direct) continue; if (!t.proxy || t.proxy_fmt != o.uf || t.proxy_w != o.d.Width || t.proxy_h != o.d.Height) { - defer_release(t.proxy, in->last_serial); + defer_release(t.proxy, instance_lists(in)); D3D12_HEAP_PROPERTIES hp = {}; hp.Type = D3D12_HEAP_TYPE_DEFAULT; D3D12_RESOURCE_DESC rd = {}; @@ -2573,12 +2693,42 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) } } - // ---- slot (upload block, descriptors, timestamps): wait for the frame that used it last - const uint64_t serial = rt.serial + 1; - const uint32_t slot = (uint32_t)(serial % kSlots); - if (!wait_serial(in->slot_serial[slot], 5000)) - return fail(in, "the game's command lists with earlier frames did not finish within 5 s (slot reuse)", res, WG_E_D3D_API); - harvest(in); + // ---- slot (upload block, descriptors, timestamps): one whose last list proved complete (issue #82: its own completion + // slot; a later frame completing proves nothing about it). Wait at most 5 s for one. + auto free_slot = [&]() -> int { + harvest(in); // polls the completion slots; a completed slot's timings are read before it is reused + for (uint32_t s = 0; s < kSlots; ++s) + if (!in->slot_pending[s]) return (int)s; + return -1; + }; + int fslot = free_slot(); + for (DWORD w = 0; fslot < 0 && w < 5000 && !FAILED(rt.dev->GetDeviceRemovedReason()); ++w) { + Sleep(1); + fslot = free_slot(); + } + if (fslot < 0) + return fail(in, "the game's command lists with this instance's last frames did not complete within 5 s (slot reuse)", + res, WG_E_D3D_API); + const uint32_t slot = (uint32_t)fslot; + D3D12_GPU_VIRTUAL_ADDRESS mark_va = 0; + const uint64_t serial = begin_list(&mark_va); + if (!serial) return fail(in, "no completion slot free (command lists not completing or never submitted)", res, WG_E_D3D_API); + // from here every list this frame records into may use the instance's objects and the frame's transient inputs + in->lists.push_back(serial); + in->slot_serial[slot] = serial; + in->slot_pending[slot] = true; + in->slot_frame[slot] = in->frames; + for (int r = 0; r < WG_ROLE_COUNT; ++r) + if (attached(r)) note_transient_use(img[r].resource, serial); + // nothing recorded yet: a failure before the recording gives the serial and the slot back + auto unbegin = [&]() { + rt.marks.cancel(serial); + in->lists.erase(std::remove(in->lists.begin(), in->lists.end(), serial), in->lists.end()); + in->slot_serial[slot] = 0; + in->slot_pending[slot] = false; + for (Runtime::Transient& t : rt.transient) + if (!t.users.empty() && t.users.back() == serial) t.users.pop_back(); + }; // ---- arena: a new one when the frame needs more or the fixed items move (the state is copied on the game's list) ID3D12Resource* old_arena = nullptr; @@ -2591,6 +2741,7 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) const uint64_t bytes = need <= in->arena_bytes ? in->arena_bytes : (th_new || oh_new) ? need : align256(need + need / 8); if (!new_arena(in, bytes, &err)) { in->arena = keep; + unbegin(); return fail(in, err, res, WG_W_OUT_OF_VIDEO_MEMORY); } old_arena = keep; @@ -2833,8 +2984,8 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) std::vector bars; auto want = [&](ID3D12Resource* r, uint32_t declared, D3D12_RESOURCE_STATES to) { if (!r) return; - for (ID3D12Resource* t : rt.transient) - if (t == r) return; // our lifetime-0 copies stay in NON_PIXEL_SHADER_RESOURCE + for (const Runtime::Transient& t : rt.transient) + if (t.r == r) return; // our lifetime-0 copies stay in NON_PIXEL_SHADER_RESOURCE if (declared == 0xffffffffu) return; if ((declared & (uint32_t)to) == (uint32_t)to) return; for (const Bar& b : bars) @@ -2920,7 +3071,7 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) // clean/rr-mp1-input I1: the input features / replay kernels read their 1 KB constant block through root SRV 6 // (rr_input.hlsl RR_IN_KCB 1, scalar loads): rebound to this frame's block in the upload ring around those dispatches // (the arena block they also get as pK is a byte copy of it), back to the ring's start afterwards (rt.blkcopy's view). - // The slot is not rewritten before this frame completed (wait_serial(slot_serial[slot]) above). + // The slot is not rewritten before this frame's list proved complete (free_slot above). const D3D12_GPU_VIRTUAL_ADDRESS ring_slot = in->upload->GetGPUVirtualAddress() + (uint64_t)slot * kBlock; if (arena_keep && in->arena_mode != 8) dispatch(l, K_BLKCOPY, 4, {(uint32_t)blk, slot * kBlock}); if (arena_keep) uav_barrier(l, A); @@ -3411,11 +3562,7 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) if (in->barbatch) bar_all(true); else for (auto it = bars.rbegin(); it != bars.rend(); ++it) transition_game(l, it->r, it->to, it->from); l->ResolveQueryData(in->qh, D3D12_QUERY_TYPE_TIMESTAMP, q0, kTq, in->qrb, (uint64_t)q0 * 8); - { - D3D12_WRITEBUFFERIMMEDIATE_PARAMETER p{rt.marker->GetGPUVirtualAddress(), (UINT)(serial & 0xffffffffu)}; - D3D12_WRITEBUFFERIMMEDIATE_MODE mode = D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT; - list2->WriteBufferImmediate(1, &p, &mode); - } + end_list(list2, serial, mark_va); // this list's own completion slot // ================================================================ bookkeeping if (g_test_used) { // the buffer map of t_describe: only built once a test host fetched the test interface static const char* const kRoleName[kRoles] = {"C", "Z", "V", "SV", "Dd", "Ds", "N", "R", "Q", "B", "G1", "G2", "Hd"}; @@ -3447,16 +3594,12 @@ uint32_t evaluate_locked(Instance* in, const WgRrFrame* f, WgRrFrameResult* res) m += fmt("direct %d" "\n" "camera_usable %d" "\n", oi[0].direct ? 1 : 0, camera_usable ? 1 : 0); in->map_text = m; } - rt.serial = serial; in->last_serial = serial; - in->slot_serial[slot] = serial; - in->slot_pending[slot] = true; - in->slot_frame[slot] = in->frames; in->work_bytes = P.work; if (old_arena) { log_line(fmt("instance %u: arena %s, %llu MiB (render %ux%u)", in->id, (th_new || oh_new) ? "re-laid out" : "grown", (unsigned long long)(in->arena_bytes >> 20), rw, rh)); - defer_release(old_arena, serial); + defer_release(old_arena, instance_lists(in)); // this frame's list (it copies the state out of it) and the earlier ones } if (th_new || oh_new) log_line(fmt("instance %u frame %llu: %s created zero-filled (54b.6; this frame resets them)", in->id, @@ -3509,8 +3652,13 @@ uint32_t retain_locked(void* cmd, void* resource, uint32_t format, uint32_t stat D3D12_HEAP_PROPERTIES hp = {}; hp.Type = D3D12_HEAP_TYPE_DEFAULT; ID3D12Resource* c = nullptr; - if (FAILED(rt.dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, - IID_PPV_ARGS(&c)))) { + // bug hunt B / issue #82: the copy runs when the game's list does; the list gets a serial and completion slot of its + // own (written after the copy), and the copy's release waits for that list and every list that read the copy + D3D12_GPU_VIRTUAL_ADDRESS mark_va = 0; + const uint64_t serial = rt.marker ? begin_list(&mark_va) : 0; + if (!serial || FAILED(rt.dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COPY_DEST, + nullptr, IID_PPV_ARGS(&c)))) { + if (serial) rt.marks.cancel(serial); // nothing recorded release(l2); release(l); return WG_E_D3D_API; @@ -3533,29 +3681,28 @@ uint32_t retain_locked(void* cmd, void* resource, uint32_t format, uint32_t stat } if (move) transition_game(l, src, D3D12_RESOURCE_STATE_COPY_SOURCE, from); transition(l, c, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); - // bug hunt B: the copy runs when the game's list does, and a copy alone never advanced rt.serial: a release before the - // next frame (the input re-tagged or untagged) freed it at once, before its CopyResource ran. The copy gets a serial of - // its own, written into the same list after it (the frames' marker), so release_transient's defer waits for that list. - if (rt.marker) { - const uint64_t serial = rt.serial + 1; - D3D12_WRITEBUFFERIMMEDIATE_PARAMETER p{rt.marker->GetGPUVirtualAddress(), (UINT)(serial & 0xffffffffu)}; - D3D12_WRITEBUFFERIMMEDIATE_MODE mode = D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT; - l2->WriteBufferImmediate(1, &p, &mode); - rt.serial = serial; - } + end_list(l2, serial, mark_va); release(l2); release(l); - rt.transient.push_back(c); + rt.transient.push_back({c, rt.gen, {serial}}); *out = c; return WG_OK; } +// A copy of this device waits for its copy list and every list that read it. A copy of an earlier device is still here +// only when that device was dropped with no list in flight (all proved complete) or removed (drop_device parks the +// copies of a device with lists in flight), so it goes at once. +void release_transient_copy(Runtime::Transient& t) { + if (t.gen == R().gen) defer_release(t.r, t.users); + else drop(t.r); +} void release_transient_locked(void* copy) { Runtime& rt = R(); - auto it = std::find(rt.transient.begin(), rt.transient.end(), (ID3D12Resource*)copy); + auto it = std::find_if(rt.transient.begin(), rt.transient.end(), + [&](const Runtime::Transient& t) { return t.r == (ID3D12Resource*)copy; }); if (it == rt.transient.end()) return; - ID3D12Resource* r = *it; + Runtime::Transient t = *it; rt.transient.erase(it); - defer_release(r, rt.serial); + release_transient_copy(t); // issue #8: create, evaluate and destroy run pump() too, but a game that tags its inputs every frame while no // instance exists (RR available, switched off) calls none of them, and the released copies were never freed. // pump() frees only resources whose frame marker the GPU has written (the same test as at evaluate), takes no lock @@ -3839,13 +3986,28 @@ uint32_t a_passthrough(void*, const WgRrFrame* f0) { } init_heap(rt.pass_heap); } - const uint64_t serial = rt.serial + 1; - const uint32_t slot = (uint32_t)(serial % kSlots); - if (!wait_serial(rt.pass_serial[slot], 5000)) { + // a descriptor slot whose last pass-through list proved complete (issue #82: its own completion slot); at most 5 s + auto free_pass_slot = [&]() -> int { + poll_marks(); + for (uint32_t s = 0; s < kSlots; ++s) + if (rt.marks.done(rt.pass_serial[s])) return (int)s; + return -1; + }; + int pslot = free_pass_slot(); + for (DWORD w = 0; pslot < 0 && w < 5000 && !FAILED(rt.dev->GetDeviceRemovedReason()); ++w) { + Sleep(1); + pslot = free_pass_slot(); + } + D3D12_GPU_VIRTUAL_ADDRESS mark_va = 0; + const uint64_t serial = pslot < 0 ? 0 : begin_list(&mark_va); + if (!serial) { release(l2); release(l); return WG_E_D3D_API; } + const uint32_t slot = (uint32_t)pslot; + rt.pass_serial[slot] = serial; + note_transient_use(cr, serial); D3D12_CPU_DESCRIPTOR_HANDLE hc = rt.pass_heap->GetCPUDescriptorHandleForHeapStart(); D3D12_GPU_DESCRIPTOR_HANDLE hg = rt.pass_heap->GetGPUDescriptorHandleForHeapStart(); hc.ptr += (uint64_t)(kHeapFixed + 2 * slot) * rt.inc; @@ -3867,8 +4029,8 @@ uint32_t a_passthrough(void*, const WgRrFrame* f0) { std::vector bars; auto want = [&](ID3D12Resource* r, uint32_t declared, D3D12_RESOURCE_STATES to) { if (declared == 0xffffffffu || (declared & (uint32_t)to) == (uint32_t)to) return; - for (ID3D12Resource* t : rt.transient) - if (t == r) return; + for (const Runtime::Transient& t : rt.transient) + if (t.r == r) return; bars.push_back({r, (D3D12_RESOURCE_STATES)declared, to}); }; want(cr, c.state, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); @@ -3890,11 +4052,7 @@ uint32_t a_passthrough(void*, const WgRrFrame* f0) { {c.origin_x, c.origin_y, rw, rh, o.origin_x, o.origin_y, ow, oh, fb(sx), fb(sy)}); uav_barrier(l, orr); for (auto it = bars.rbegin(); it != bars.rend(); ++it) transition_game(l, it->r, it->to, it->from); - D3D12_WRITEBUFFERIMMEDIATE_PARAMETER p{rt.marker->GetGPUVirtualAddress(), (UINT)(serial & 0xffffffffu)}; - D3D12_WRITEBUFFERIMMEDIATE_MODE mode = D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT; - l2->WriteBufferImmediate(1, &p, &mode); - rt.serial = serial; - rt.pass_serial[slot] = serial; + end_list(l2, serial, mark_va); // this list's own completion slot if (++rt.passes == 1) log_line(fmt("pass-through: first frame passed through (colour %ux%u -> output %ux%u)", rw, rh, ow, oh)); release(l2); release(l); @@ -3941,7 +4099,7 @@ uint32_t t_dump(void* inst, const wchar_t* path) { std::lock_guard lock(rt.mu); Instance* in = (Instance*)inst; if (!in || !rt.dev) return WG_E_INVALID_PARAMETER; - if (!wait_serial(in->last_serial, 10000)) return WG_E_D3D_API; + if (!wait_list(in->last_serial, 10000)) return WG_E_D3D_API; ID3D12Resource* rb = nullptr; if (FAILED(make_buffer(in->arena_bytes, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST, &rb))) @@ -3991,13 +4149,13 @@ void t_counters(int64_t out[4]) { pump(); out[0] = rt.live_resources.load(); out[1] = rt.live_instances; - out[2] = (int64_t)rt.deferred.size(); + out[2] = (int64_t)(rt.deferred.size() + rt.parked.objects()); out[3] = rt.created_resources.load(); } uint32_t t_drain(uint32_t ms) { Runtime& rt = R(); std::lock_guard lock(rt.mu); - const bool ok = wait_serial(rt.serial, ms); + const bool ok = wait_all_lists(ms); pump(); return ok ? WG_OK : WG_E_D3D_API; } @@ -4005,10 +4163,10 @@ uint32_t t_shutdown() { Runtime& rt = R(); std::lock_guard lock(rt.mu); if (rt.live_instances) return WG_E_INVALID_STATE; - wait_serial(rt.serial, 5000); - for (ID3D12Resource*& t : rt.transient) drop(t); + wait_all_lists(5000); + for (Runtime::Transient& t : rt.transient) release_transient_copy(t); rt.transient.clear(); - drop_device(); + drop_device(); // anything still in flight is parked, not released return WG_OK; } WgRrRuntimeTestV1 g_test = {sizeof(WgRrRuntimeTestV1), t_dump, t_layout, t_counters, t_drain, t_shutdown, t_describe}; diff --git a/clean/rr_f/runtime/tests/rr_retire_test.cpp b/clean/rr_f/runtime/tests/rr_retire_test.cpp index d65c2028..5fed58c8 100644 --- a/clean/rr_f/runtime/tests/rr_retire_test.cpp +++ b/clean/rr_f/runtime/tests/rr_retire_test.cpp @@ -1,95 +1,233 @@ // SPDX-License-Identifier: MIT -// CPU test of the RR runtime's retire list (src/rr_retire.h, issue #63): objects a recorded frame may use are released -// only when the frame marker proves that frame complete. Fake objects count their Release calls; the marker is a -// plain number. Built and run by test_rr_retire.py; exit status 0 when every check passes. Original WrongGPU code. +// CPU test of the RR runtime's per-list completion and retire list (src/rr_retire.h, issues #63 and #82): an object is +// released only when EVERY command list that may use it has proved complete through its own marker slot, never because +// a later list completed; a device switch keeps the old device's objects until its lists complete or it is removed. +// Fake objects count their Release calls; a plain array stands in for the mapped marker buffer, and "the GPU runs list +// s" is the write of s into s's slot. Built and run by test_rr_retire.py; exit status 0 when every check passes. +// Original WrongGPU code. #include +#include + #include "rr_retire.h" namespace { +using wgrr_rt::ListMarks; +using wgrr_rt::Parked; +using wgrr_rt::ParkedSet; +using wgrr_rt::RetireList; + struct Fake { int releases = 0; unsigned long Release() { return (unsigned long)++releases; } }; -int g_failed = 0; +int g_failed = 0, g_checks = 0; void check(bool ok, const char* what) { printf("%s: %s\n", ok ? "ok " : "FAIL", what); + ++g_checks; if (!ok) ++g_failed; } -void heap_waits_for_marker() { - wgrr_rt::RetireList l; +// a device's marker slots and its lists +struct Gpu { + ListMarks marks; + std::vector words; + std::vector slot_of; // by serial + explicit Gpu(uint32_t n = 16) : marks(n), words(n, 0), slot_of(1, 0) {} + uint64_t record() { // the runtime records a list: serial and slot + uint32_t s = 0; + const uint64_t serial = marks.begin(&s); + if (serial) { + if (slot_of.size() <= serial) slot_of.resize((size_t)serial + 1, 0); + slot_of[(size_t)serial] = s; + } + return serial; + } + void run(uint64_t serial) { words[slot_of[(size_t)serial]] = (uint32_t)serial; } // the list executed (its last write) + void poll() { marks.poll(words.data()); } +}; + +void own_list_proves_completion() { + Gpu g; + RetireList l; Fake heap; - int c = l.retire(&heap, 5, 4, false); - check(heap.releases == 0 && c == 0 && l.size() == 1, "heap retired at serial 5 with the marker at 4: not released"); - c = l.pump(4); - check(heap.releases == 0 && c == 0 && l.size() == 1, "pump with the marker still at 4: not released"); - c = l.pump(5); - check(heap.releases == 1 && c == 0 && l.empty(), "marker 5, pump: released once (not counted)"); - l.pump(6); + const uint64_t a = g.record(); + int c = l.retire(&heap, {a}, g.marks, false); + check(heap.releases == 0 && c == 0 && l.size() == 1, "heap used by list A (recorded, not run): not released"); + g.poll(); + c = l.pump(g.marks); + check(heap.releases == 0 && l.size() == 1, "pump before A ran: not released"); + g.run(a); + g.poll(); + c = l.pump(g.marks); + check(heap.releases == 1 && c == 0 && l.empty(), "A wrote its own slot, pump: released once (not counted)"); + l.pump(g.marks); check(heap.releases == 1, "a later pump does not release it again"); } -void completed_frame_releases_at_once() { - wgrr_rt::RetireList l; - Fake buf; - const int c = l.retire(&buf, 5, 5, true); - check(buf.releases == 1 && c == 1 && l.empty(), "buffer retired at serial 5 with the marker at 5: released at once, counted"); - check(l.retire(nullptr, 9, 0, true) == 0 && l.empty(), "a null object is ignored"); +void out_of_order_submission() { + // The blocker of the #82 review: A is recorded first, B later; the game submits B first and it completes. A's + // objects must NOT be released (a monotonic "marker >= serial" test would have released them). + Gpu g; + RetireList l; + Fake a_arena, a_heap, a_qh; + const uint64_t a = g.record(); // instance X's frame, list A + const uint64_t b = g.record(); // instance Y's frame, list B (recorded after A) + check(a < b, "A recorded before B (smaller serial)"); + Fake* bufs[1] = {&a_arena}; + Fake* heaps[2] = {&a_heap, &a_qh}; + int c = wgrr_rt::retire_instance(l, bufs, 1, heaps, 2, std::vector{a}, g.marks); // X destroyed + g.run(b); // B submitted and completed first + g.poll(); + c += l.pump(g.marks); + check(g.marks.done(b) && !g.marks.done(a), "B complete, A not: completion is per list"); + check(c == 0 && a_arena.releases + a_heap.releases + a_qh.releases == 0 && l.size() == 3, + "B completed first: A's buffer, descriptor heap and query heap NOT released"); + g.run(a); + g.poll(); + c = l.pump(g.marks); + check(c == 1 && a_arena.releases == 1 && a_heap.releases == 1 && a_qh.releases == 1 && l.empty(), + "A completed: its three objects released once each, the buffer counted"); + + // one instance with two lists in flight: the later one completing does not release the instance's objects + Gpu h; + RetireList m; + Fake arena; + const uint64_t f1 = h.record(), f2 = h.record(); + m.retire(&arena, {f1, f2}, h.marks, true); + h.run(f2); + h.poll(); + m.pump(h.marks); + check(arena.releases == 0, "instance with frames 1 and 2 in flight, frame 2 ran first: arena kept"); + h.run(f1); + h.poll(); + check(m.pump(h.marks) == 1 && arena.releases == 1, "frame 1 ran too: arena released"); +} + +void never_submitted_list() { + Gpu g(8); + RetireList l; + Fake heap, buf; + const uint64_t a = g.record(); // recorded, never submitted + l.retire(&heap, {a}, g.marks, false); + l.retire(&buf, {a}, g.marks, true); + // many later lists run and complete; the pool cycles through the other slots + bool slot_reused = false; + for (int i = 0; i < 100; ++i) { + const uint64_t s = g.record(); + if (!s) break; + if (g.slot_of[(size_t)s] == g.slot_of[(size_t)a]) slot_reused = true; + g.run(s); + g.poll(); + l.pump(g.marks); + } + check(!slot_reused, "100 later lists: none was given the never-submitted list's marker slot"); + check(!g.marks.done(a) && heap.releases == 0 && buf.releases == 0 && l.size() == 2, + "list never submitted, 100 later lists complete: its objects still kept"); + check(g.marks.pending() == 1, "only the never-submitted list is pending"); + const int c = l.release_all(); // device removed: nothing of it runs any more + check(c == 1 && heap.releases == 1 && buf.releases == 1 && l.empty(), "device removed: released once each"); } -void destroy_keeps_everything_until_complete() { - // the instance destroy of issue #63: arena, upload, query readback, two output proxies (counted) and the - // shader-visible descriptor heap and timestamp query heap, all used by the frame with serial 5 - wgrr_rt::RetireList l; - Fake arena, upload, qrb, proxy, heap, qh; - Fake* bufs[5] = {&arena, &upload, &qrb, &proxy, nullptr}; - Fake* heaps[2] = {&heap, &qh}; - int c = wgrr_rt::retire_instance(l, bufs, 5, heaps, 2, 5, 4); - check(c == 0, "destroy with the marker at 4: no counted object released"); - check(arena.releases + upload.releases + qrb.releases + proxy.releases == 0, "destroy with the marker at 4: no buffer released"); - check(heap.releases == 0, "destroy with the marker at 4: descriptor heap not released"); - check(qh.releases == 0, "destroy with the marker at 4: query heap not released"); - c = l.pump(5); - check(c == 4, "marker 5, pump: the four buffers released and counted"); - check(arena.releases == 1 && upload.releases == 1 && qrb.releases == 1 && proxy.releases == 1 && heap.releases == 1 && - qh.releases == 1 && l.empty(), - "marker 5, pump: every object released exactly once"); +void slot_reuse_needs_the_holders_proof() { + Gpu g(4); + uint64_t s[4]; + for (uint64_t& v : s) v = g.record(); + check(s[3] != 0 && g.record() == 0, "4 slots, 4 lists in flight: no fifth list (nothing may be recorded)"); + g.run(s[3]); // the LAST recorded list completes; the others not + g.poll(); + const uint64_t n = g.record(); + check(n != 0 && g.slot_of[(size_t)n] == g.slot_of[(size_t)s[3]], "the next list takes the slot of the list that completed"); + g.poll(); // its slot still holds s[3]'s value: that proves nothing about the new holder + check(!g.marks.done(n), "a stale value of the slot's previous holder does not prove the new list"); + check(!g.marks.done(s[0]) && !g.marks.done(s[1]) && !g.marks.done(s[2]), "the earlier lists are still in flight"); + // per-instance frame slots work the same way in the runtime: a slot is reused only when ITS list proved complete + const uint64_t c = g.record(); + check(c == 0, "pool full again"); + g.marks.cancel(n); // a list that recorded nothing gives its slot back and counts as complete + check(g.marks.done(n) && g.record() != 0, "cancel: serial complete, slot free again"); } -void destroy_after_completion_releases_at_once() { - wgrr_rt::RetireList l; - Fake arena, heap, qh; - Fake* bufs[1] = {&arena}; - Fake* heaps[2] = {&heap, &qh}; - const int c = wgrr_rt::retire_instance(l, bufs, 1, heaps, 2, 5, 7); - check(c == 1 && arena.releases == 1 && heap.releases == 1 && qh.releases == 1 && l.empty(), - "destroy with the frame complete: everything released at once"); +void device_switch_keeps_old_objects() { + // set-device with another device while a frame of the old one is recorded but not complete (the #82 review's major) + ParkedSet ps; + Gpu old; + const uint64_t a = old.record(); + Fake heap, arena, pso, marker, device; + Parked p; + p.id = 1; + p.marks = old.marks; // the runtime moves its state; the fake GPU keeps a copy of the slot table + p.words = old.words.data(); + p.deferred.retire(&heap, {a}, p.marks, false); + p.deferred.retire(&arena, {a}, p.marks, true); + p.deferred.retire(&pso, {a}, p.marks, false); + p.last = {&marker, &device}; + p.last_counted = 1; + ps.park(std::move(p)); + std::vector log; + bool removed = false; + auto is_removed = [&](Parked&) { return removed; }; + int c = ps.pump(is_removed, &log); + check(c == 0 && heap.releases + arena.releases + pso.releases + marker.releases + device.releases == 0 && ps.size() == 1, + "device switched with list A in flight: the old device's objects, marker buffer and device are all kept"); + old.run(a); // the old device's queue runs A later + c = ps.pump(is_removed, &log); + check(c == 2 && heap.releases == 1 && arena.releases == 1 && pso.releases == 1 && marker.releases == 1 && + device.releases == 1 && ps.empty(), + "A completed on the old device: every kept object released once (arena + marker counted), then the device"); + check(log.size() == 1, "one log line when the old device is released"); + + // the old device is removed instead: everything goes + Gpu old2; + const uint64_t b = old2.record(); + Fake h2, m2, d2; + Parked q; + q.id = 2; + q.marks = old2.marks; + q.words = old2.words.data(); + q.deferred.retire(&h2, {b}, q.marks, false); + q.last = {&m2, &d2}; + q.last_counted = 1; + ps.park(std::move(q)); + c = ps.pump(is_removed, &log); + check(c == 0 && h2.releases == 0, "second old device, list in flight, not removed: kept"); + removed = true; + c = ps.pump(is_removed, &log); + check(c == 1 && h2.releases == 1 && m2.releases == 1 && d2.releases == 1 && ps.empty(), + "old device removed (GetDeviceRemovedReason): its objects released once each"); } -void partial_pump_and_device_drop() { - wgrr_rt::RetireList l; - Fake a, b, h; - l.retire(&a, 3, 1, true); - l.retire(&b, 8, 1, true); - l.retire(&h, 8, 1, false); - int c = l.pump(3); - check(c == 1 && a.releases == 1 && b.releases == 0 && h.releases == 0 && l.size() == 2, "pump to 3 releases only serial 3"); - check(wgrr_rt::RetireList::in_flight(8, 3) && !wgrr_rt::RetireList::in_flight(3, 3), "in_flight compares serials"); - c = l.release_all(); // device dropped or removed: the frame can never run - check(c == 1 && b.releases == 1 && h.releases == 1 && l.empty(), "release_all (device dropped / removed) releases the rest once"); +void device_switch_bound() { + ParkedSet ps; + std::vector gpus(ParkedSet::kMaxParked + 1); + std::vector objs(gpus.size()); + for (size_t i = 0; i < gpus.size(); ++i) { + const uint64_t s = gpus[i].record(); // each old device has a list that never completes + Parked p; + p.id = i + 1; + p.marks = gpus[i].marks; + p.words = gpus[i].words.data(); + p.deferred.retire(&objs[i], {s}, p.marks, true); + const std::string line = ps.park(std::move(p)); + if (i == gpus.size() - 1) check(line.find("no longer tracked") != std::string::npos, "the bound is logged"); + } + int released = 0; + for (const Fake& f : objs) released += f.releases; + check(ps.size() == ParkedSet::kMaxParked && released == 0, + "more old devices than the bound: the oldest is no longer tracked, and nothing is released without proof"); } } // namespace int main() { - heap_waits_for_marker(); - completed_frame_releases_at_once(); - destroy_keeps_everything_until_complete(); - destroy_after_completion_releases_at_once(); - partial_pump_and_device_drop(); - printf("%s (%d failed)\n", g_failed ? "FAILED" : "passed", g_failed); + own_list_proves_completion(); + out_of_order_submission(); + never_submitted_list(); + slot_reuse_needs_the_holders_proof(); + device_switch_keeps_old_objects(); + device_switch_bound(); + printf("%s (%d of %d checks failed)\n", g_failed ? "FAILED" : "passed", g_failed, g_checks); return g_failed ? 1 : 0; } diff --git a/clean/rr_f/runtime/tests/test_rr_retire.py b/clean/rr_f/runtime/tests/test_rr_retire.py index c51a954b..ef27c8cc 100644 --- a/clean/rr_f/runtime/tests/test_rr_retire.py +++ b/clean/rr_f/runtime/tests/test_rr_retire.py @@ -1,8 +1,11 @@ # SPDX-License-Identifier: MIT -"""CPU test of the RR runtime's retire list (clean/rr_f/runtime/src/rr_retire.h, issue #63): an object that a frame -recorded into the game's command list may use (buffers, the shader-visible descriptor heap, the timestamp query heap) -is released only when the frame marker proves that frame complete, never because a wait ran out; device drop releases -the rest. rr_retire_test.cpp drives the list with fake objects that count Release and a fake marker value. +"""CPU test of the RR runtime's per-list completion and retire list (clean/rr_f/runtime/src/rr_retire.h, issues #63 +and #82): an object that a command list recorded by the runtime may use (buffers, the shader-visible descriptor heap, +the timestamp query heap) is released only when EVERY list that may use it has proved complete through its own marker +slot, never because a wait ran out or a later list completed. Covered: out-of-order submission (A recorded first, B +submitted and completed first: A's objects kept), a list that is never submitted, marker-slot reuse, and a device +switch with work pending (the old device's objects kept until its lists complete or it is removed, with a bound). +rr_retire_test.cpp drives it with fake objects that count Release and a fake marker buffer. The program is compiled with the first available C++17 compiler: $CXX, clang++ / g++ on PATH, MSVC cl on PATH (a developer prompt), then MSVC through vswhere and vcvars64.bat. Skipped when there is none. From d5543a274b4de1ea3998e2b5e4be7b0491855f3c Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:39:19 -0500 Subject: [PATCH 4/5] RR e2e: progress per job step, GPU-run timeout naming the last step A host run stopped by a time cap lost its buffered output, so a long step (the exact-head perf frames at 2560x1440 take most of a second each) could not be told from a hang. - rr_test_host: stdout / stderr unbuffered; one "step N at T s" line per job command and one per 100 perf frames on stderr. - e2e.py: --gpu-timeout (default 900 s) for the host run; on a failure or timeout host.log keeps what the host printed and the error names the last step; phase lines for the GPU run and the CPU reference; --perf help says to use 0 inside a short GPU time slot. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/rr_f/runtime/tests/e2e.py | 32 ++++++++++++++++------- clean/rr_f/runtime/tests/rr_test_host.cpp | 16 ++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/clean/rr_f/runtime/tests/e2e.py b/clean/rr_f/runtime/tests/e2e.py index e7366c17..e48ea27a 100644 --- a/clean/rr_f/runtime/tests/e2e.py +++ b/clean/rr_f/runtime/tests/e2e.py @@ -234,8 +234,9 @@ def write_frame(path, c): def run_gpu(out, stage, runs, perf_frames, name="e2e", leak=6, head=None, body="Z", inp="exact", front=True, mips=1, - tagloop=200): - """runs: [(scenario name, calls)]; every scenario twice (WG_RR_ALIAS=0 and aliased).""" + tagloop=200, gpu_timeout=900): + """runs: [(scenario name, calls)]; every scenario twice (WG_RR_ALIAS=0 and aliased). gpu_timeout: seconds the host + run may take (then it is stopped and the log names its last step).""" work = os.path.join(out, name) if os.path.exists(work): shutil.rmtree(work) @@ -307,12 +308,21 @@ def run_gpu(out, stage, runs, perf_frames, name="e2e", leak=6, head=None, body=" fh.write("\n".join(job) + "\n") host = os.path.join(out, "rr_test_host.exe") t0 = time.time() - r = subprocess.run(_locked([host, jf]), capture_output=True, text=True, - timeout=7200) + print(f"e2e phase: GPU run ({len(job)} job lines, timeout {gpu_timeout} s)", flush=True) + try: + r = subprocess.run(_locked([host, jf]), capture_output=True, text=True, timeout=gpu_timeout) + rc, so, se = r.returncode, r.stdout, r.stderr + except subprocess.TimeoutExpired as e: # the host's own output is unbuffered: what it printed is kept + rc = "timeout after %d s" % gpu_timeout + so = e.stdout.decode(errors="replace") if isinstance(e.stdout, bytes) else (e.stdout or "") + se = e.stderr.decode(errors="replace") if isinstance(e.stderr, bytes) else (e.stderr or "") with open(os.path.join(work, "host.log"), "w") as fh: - fh.write(r.stdout + "\n--- stderr\n" + r.stderr) - if r.returncode != 0: - raise SystemExit(f"rr_test_host failed ({r.returncode}):\n{r.stdout[-3000:]}\n{r.stderr[-3000:]}") + fh.write(so + "\n--- stderr\n" + se) + if rc != 0: + # the host's stderr has one "step N at T s" line per job command: the last one names where it stopped + steps = [l for l in se.splitlines() if l.startswith("step ") or l.startswith(" perf:")] + raise SystemExit(f"rr_test_host failed ({rc}) after {time.time() - t0:.1f} s; last step: " + f"{steps[-1] if steps else 'none'}\n{so[-3000:]}\n{se[-3000:]}") print(f"GPU run {time.time() - t0:.1f} s", flush=True) return work @@ -824,7 +834,10 @@ def main(argv=None): ap.add_argument("--calls-b", type=int, default=14) ap.add_argument("--skip-gpu", action="store_true", help="analyse an earlier run") ap.add_argument("--jobs", type=int, default=8) - ap.add_argument("--perf", type=int, default=600) + ap.add_argument("--perf", type=int, default=600, help="timing frames per perf line (0: none). The exact head's perf " + "frames at 2560x1440 take most of a second each on the GPU: use 0 inside a short GPU time slot") + ap.add_argument("--gpu-timeout", type=int, default=900, help="seconds the GPU host run may take before it is stopped " + "(its log then names the last job step)") ap.add_argument("--leak", type=int, default=6) ap.add_argument("--tagloop", type=int, default=200, help="issue #8: frames of inputs copied and released with no " "instance, before the scenarios (0: skip)") @@ -856,7 +869,7 @@ def main(argv=None): if not a.skip_gpu: work = run_gpu(a.out, stage, runs, a.perf, name, a.leak, None if a.head == "default" else a.head, None if a.body == "default" else a.body, None if a.input == "default" else a.input, mips=a.mips, - tagloop=a.tagloop) + tagloop=a.tagloop, gpu_timeout=a.gpu_timeout) rows, life, inv, al = [], [], [], [] jobs = [] metas = {} @@ -878,6 +891,7 @@ def main(argv=None): inv += [dict(r, scenario=sname) for r in li] al += [dict(r, scenario=sname) for r in alias_checks(work, sname, m0, m1)] t0 = time.time() + print(f"e2e phase: CPU reference of {len(jobs)} frames ({a.jobs} jobs)", flush=True) if a.jobs > 1: import multiprocessing as mp with mp.Pool(a.jobs) as pool: diff --git a/clean/rr_f/runtime/tests/rr_test_host.cpp b/clean/rr_f/runtime/tests/rr_test_host.cpp index 574b7813..cc6a0a55 100644 --- a/clean/rr_f/runtime/tests/rr_test_host.cpp +++ b/clean/rr_f/runtime/tests/rr_test_host.cpp @@ -63,6 +63,16 @@ #include "rr_runtime_test.h" #include "../../proxy/include/wg_settings_reader.h" +// Progress on stderr (unbuffered, so a run stopped by a time cap keeps it): one line per job command and one per 100 +// perf frames, with the seconds since the host started. A run that hangs or is stopped names the step it was in. +static double host_seconds() { + static LARGE_INTEGER fq = {}, t0 = {}; + LARGE_INTEGER t; + if (!fq.QuadPart) QueryPerformanceFrequency(&fq), QueryPerformanceCounter(&t0); + QueryPerformanceCounter(&t); + return (double)(t.QuadPart - t0.QuadPart) / (double)fq.QuadPart; +} + #pragma comment(lib, "d3d12.lib") #pragma comment(lib, "dxgi.lib") @@ -493,6 +503,7 @@ static void perf(Host& h, uint32_t Wo, uint32_t Ho, uint32_t rw, uint32_t rh, in static ID3D12CommandAllocator* al2[3] = {}; if (!al2[0]) for (int k = 0; k < 3; ++k) CK(g_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&al2[k]))); for (int i = 0; i < frames; ++i) { + if (i && i % 100 == 0) fprintf(stderr, " perf: %d of %d frames at %.1f s\n", i, frames, host_seconds()); in.jitter[0] = u(rng) - 0.5f; in.jitter[1] = u(rng) - 0.5f; WgRrFrame f; @@ -709,6 +720,9 @@ int main(int argc, char** argv) { fprintf(stderr, "usage: rr_test_host \n"); return 1; } + setvbuf(stdout, nullptr, _IONBF, 0); // nothing lost when a time cap stops the host + setvbuf(stderr, nullptr, _IONBF, 0); + host_seconds(); std::ifstream jf(argv[1]); if (!jf) return 1; IDXGIFactory6* fac = nullptr; @@ -766,10 +780,12 @@ int main(int argc, char** argv) { } Host h; std::string line; + int step = 0; while (std::getline(jf, line)) { std::istringstream s(line); std::string cmd; if (!(s >> cmd) || cmd[0] == '#') continue; + fprintf(stderr, "step %d %s at %.1f s\n", ++step, cmd.c_str(), host_seconds()); if (cmd == "runtime") { std::string dll; s >> dll; From 8802ad4766a7df0ea3a0135a5436db31b1baf0a7 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:03:39 -0500 Subject: [PATCH 5/5] RR runtime: marker values never repeat in a slot; old devices never forgotten Final review of #82: - Marker wraparound: the GPU writes only the low 32 bits of a serial and a free slot keeps its last completed value. ListMarks now remembers each slot's value and skips a serial whose low 32 bits equal it (0 for a fresh slot), so a stale value can never prove a new list after the serials wrap. CPU test forces the wrap (skip_to test hook). - Device-park cap: an old device is never forgotten without being released. Beyond 4 tracked, the oldest is released at once if its lists completed or its device is removed (GetDeviceRemovedReason); otherwise its marker slots are polled for at most 2 s and it is released when they complete; if still pending it stays tracked (soft cap, logged). RetireList::abandon is removed. CPU test: 6 switches with work pending (none completes: all kept, then released once each when they do; the oldest completes during the wait; the oldest's device removed). - The fake GPU of the CPU test maps serials to slots in a std::map (the wraparound test uses serials above 2^32). Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/rr_f/runtime/src/rr_retire.h | 108 +++++++++------- clean/rr_f/runtime/src/rr_runtime.cpp | 19 ++- clean/rr_f/runtime/tests/rr_retire_test.cpp | 134 ++++++++++++++++---- 3 files changed, 186 insertions(+), 75 deletions(-) diff --git a/clean/rr_f/runtime/src/rr_retire.h b/clean/rr_f/runtime/src/rr_retire.h index 3bf9960e..9b995374 100644 --- a/clean/rr_f/runtime/src/rr_retire.h +++ b/clean/rr_f/runtime/src/rr_retire.h @@ -17,8 +17,9 @@ // 3. no caller waits here: retiring and pumping only read the marker slots. // // A device switch (set-device with another device) keeps the old device's lists, marker slots and waiting objects -// (ParkedSet) until each list proved complete or the old device is removed. At most kMaxParked old devices are -// tracked; beyond that the oldest one's objects are left allocated without tracking (never released early). +// (ParkedSet) until each list proved complete or the old device is removed. Nothing is forgotten: beyond kMaxParked old +// devices, the oldest one is released if its lists complete within a bounded wait (or its device is removed), and kept +// tracked (with a log line) otherwise, never released early. // // Header-only and free of D3D12 so that the CPU test (clean/rr_f/runtime/tests/test_rr_retire.py) builds it with fake // objects and a fake marker buffer on any host. T is any type with Release() (IUnknown in the runtime); "counted" marks @@ -40,17 +41,20 @@ namespace wgrr_rt { // The marker slots of one device: which list holds which slot, and which lists have proved complete. class ListMarks { public: - explicit ListMarks(uint32_t slots = 1024) : occ_(slots, 0) {} + explicit ListMarks(uint32_t slots = 1024) : occ_(slots, 0), held_(slots, 0) {} // A new list: its serial (> 0), and in *slot the marker word it must write that serial into (the low 32 bits) as // its last command. 0 when every slot is held by a list that has not proved complete (nothing may be recorded then). + // The GPU writes only the low 32 bits, and a free slot still holds the value of its last completed list (0 when + // fresh): a serial whose low 32 bits equal the value already in the chosen slot is skipped (never handed out), so + // a stale value can never prove the new list, however many serials have gone by (32-bit wraparound). uint64_t begin(uint32_t* slot) { const uint32_t n = (uint32_t)occ_.size(); for (uint32_t i = 0; i < n; ++i) { const uint32_t s = (hint_ + i) % n; if (occ_[s]) continue; ++last_; - if ((uint32_t)last_ == 0) ++last_; // a fresh slot reads 0: never a serial whose low 32 bits are 0 + while ((uint32_t)last_ == held_[s]) ++last_; // differs from the slot's current value (0 for a fresh slot) occ_[s] = last_; pending_.emplace(last_, s); hint_ = (s + 1) % n; @@ -75,6 +79,7 @@ class ListMarks { for (auto it = pending_.begin(); it != pending_.end();) { if (words[it->second] == (uint32_t)it->first) { occ_[it->second] = 0; + held_[it->second] = (uint32_t)it->first; // what the free slot holds now it = pending_.erase(it); } else { ++it; @@ -88,6 +93,10 @@ class ListMarks { size_t pending() const { return pending_.size(); } uint64_t last() const { return last_; } uint32_t slots() const { return (uint32_t)occ_.size(); } + // Test hook (the wraparound test): the next serial handed out is above `serial`. + void skip_to(uint64_t serial) { + if (serial > last_) last_ = serial; + } // the serials of every list that has not proved complete std::vector pending_serials() const { std::vector v; @@ -99,6 +108,7 @@ class ListMarks { private: std::vector occ_; // the serial holding each slot, 0 = free + std::vector held_; // the value each slot holds as far as proven (0 = fresh) std::unordered_map pending_; // serial -> slot of every list not proved complete uint64_t last_ = 0; uint32_t hint_ = 0; @@ -162,14 +172,6 @@ class RetireList { return counted; } - // Forgets every entry WITHOUT releasing it (an old device beyond the ParkedSet bound: left allocated, never freed - // early). Returns the number of objects forgotten. - size_t abandon() { - const size_t n = entries_.size(); - entries_.clear(); - return n; - } - size_t size() const { return entries_.size(); } bool empty() const { return entries_.empty(); } @@ -205,25 +207,36 @@ struct Parked { template class ParkedSet { public: - static constexpr size_t kMaxParked = 4; + static constexpr size_t kMaxParked = 4; // old devices tracked before park() tries to release the oldest + static constexpr uint32_t kParkWaitMs = 2000; // the bounded wait for the oldest one's lists at that point - // Takes an old device's state. Returns a log line. Beyond kMaxParked the oldest one is abandoned: its objects are - // left allocated without tracking (memory reclaimed at process exit), never released without proof. - std::string park(Parked&& p) { - std::string msg = "device generation " + std::to_string(p.id) + " switched away with " + - std::to_string(p.marks.pending()) + " list(s) not proved complete: " + - std::to_string(p.deferred.size() + p.last.size()) + - " object(s) kept until each list completes or the device is removed"; + // Takes an old device's state; log lines go to *log. Nothing is ever forgotten: an old device leaves the set only + // when it is released (every list proved complete, or the device removed). With more than kMaxParked tracked, the + // oldest one is released at once when its marker slots show every list complete or removed(p) reports its device + // removed; otherwise its marker slots are polled for at most wait_ms (sleep_ms(1) between polls) and it is released + // when they complete; if it is still pending after that, it stays tracked (the cap is soft) and one line says so. + // Returns the number of counted objects released. + template + int park(Parked&& p, Removed removed, Sleep sleep_ms, uint32_t wait_ms, std::vector* log) { + if (log) + log->push_back("device generation " + std::to_string(p.id) + " switched away with " + + std::to_string(p.marks.pending()) + " list(s) not proved complete: " + + std::to_string(p.deferred.size() + p.last.size()) + + " object(s) kept until each list completes or the device is removed"); set_.push_back(std::move(p)); - if (set_.size() > kMaxParked) { - Parked& o = set_.front(); - const size_t n = o.deferred.abandon() + o.last.size(); - msg += "; more than " + std::to_string(kMaxParked) + " old devices kept: generation " + std::to_string(o.id) + - " (" + std::to_string(o.marks.pending()) + " list(s) never proved complete) has its " + std::to_string(n) + - " object(s) left allocated, no longer tracked"; - set_.erase(set_.begin()); + int counted = 0; + if (set_.size() <= kMaxParked) return counted; + if (try_release(0, removed, log, &counted)) return counted; + for (uint32_t t = 0; t < wait_ms; ++t) { + sleep_ms(1); + if (try_release(0, removed, log, &counted)) return counted; } - return msg; + if (log) + log->push_back("more than " + std::to_string(kMaxParked) + " old devices kept: generation " + + std::to_string(set_.front().id) + " still has " + std::to_string(set_.front().marks.pending()) + + " list(s) not proved complete after " + std::to_string(wait_ms) + + " ms; it stays tracked (" + std::to_string(set_.size()) + " old devices tracked now)"); + return counted; } // Polls each old device's marker slots and releases what has proved complete; removed(p) tells whether p's device @@ -236,21 +249,7 @@ class ParkedSet { Parked& p = set_[i]; p.marks.poll(p.words); counted += p.deferred.pump(p.marks); - const bool gone = removed(p); - if (gone || p.marks.pending() == 0) { - const size_t n = p.deferred.size() + p.last.size(); - counted += p.deferred.release_all(); // empty unless the device is removed - for (T* o : p.last) - if (o) o->Release(); - counted += p.last_counted; - if (log) - log->push_back("device generation " + std::to_string(p.id) + - (gone ? ": device removed, " : ": every list proved complete, ") + std::to_string(n) + - " kept object(s) released"); - set_.erase(set_.begin() + (ptrdiff_t)i); - } else { - ++i; - } + if (!try_release(i, removed, log, &counted)) ++i; } return counted; } @@ -262,7 +261,30 @@ class ParkedSet { for (const Parked& p : set_) n += p.deferred.size() + p.last.size(); return n; } + private: + // Releases old device i in full (and removes it from the set) when every one of its lists proved complete or its + // device is removed. True when released. + template + bool try_release(size_t i, Removed& removed, std::vector* log, int* counted) { + Parked& p = set_[i]; + p.marks.poll(p.words); + const bool gone = p.marks.pending() != 0 && removed(p); + if (!gone && p.marks.pending() != 0) return false; + const size_t n = p.deferred.size() + p.last.size(); + *counted += p.deferred.pump(p.marks); // every object whose lists all proved complete + *counted += p.deferred.release_all(); // the rest: only when the device is removed (else nothing is left) + for (T* o : p.last) + if (o) o->Release(); + *counted += p.last_counted; + if (log) + log->push_back("device generation " + std::to_string(p.id) + + (gone ? ": device removed, " : ": every list proved complete, ") + std::to_string(n) + + " kept object(s) released"); + set_.erase(set_.begin() + (ptrdiff_t)i); + return true; + } + std::vector> set_; }; diff --git a/clean/rr_f/runtime/src/rr_runtime.cpp b/clean/rr_f/runtime/src/rr_runtime.cpp index 69535587..aa5ce6c4 100644 --- a/clean/rr_f/runtime/src/rr_runtime.cpp +++ b/clean/rr_f/runtime/src/rr_runtime.cpp @@ -542,6 +542,11 @@ void note_transient_use(const void* res, uint64_t serial) { for (Runtime::Transient& t : R().transient) if (t.r == res && t.gen == R().gen) prune_done(t.users, R().marks), t.users.push_back(serial); } +// an old device of an earlier device switch is removed (its parked state: the device reference goes last) +bool parked_removed(Parked& p) { + ID3D12Device* d = p.last.empty() ? nullptr : (ID3D12Device*)p.last.back(); + return d && FAILED(d->GetDeviceRemovedReason()); +} // releases deferred objects whose lists have all proved complete; once the device is removed nothing of it runs any more // and everything left is released (issue #63: kept objects go at completion or device removal, never on a timeout). // The old devices of earlier device switches are pumped the same way. @@ -556,12 +561,7 @@ void pump() { } if (!rt.parked.empty()) { std::vector lines; - rt.live_resources -= rt.parked.pump( - [](Parked& p) { - ID3D12Device* d = p.last.empty() ? nullptr : (ID3D12Device*)p.last.back(); - return d && FAILED(d->GetDeviceRemovedReason()); - }, - &lines); + rt.live_resources -= rt.parked.pump(parked_removed, &lines); for (const std::string& s : lines) log_line(s); } } @@ -1178,7 +1178,12 @@ void drop_device() { p.last = {rt.marker, rt.dev}; // the marker buffer the lists write (still mapped), then the device reference p.last_counted = 1; rt.marker = nullptr, rt.marker_map = nullptr, rt.dev = nullptr; - log_line(rt.parked.park(std::move(p))); + // beyond the cap of tracked old devices: the oldest one is released if its lists complete within a bounded wait + // (at most 2 s, under the runtime lock, only at this device switch) or its device is removed; else kept, logged + std::vector lines; + rt.live_resources -= rt.parked.park(std::move(p), parked_removed, [](uint32_t ms) { Sleep(ms); }, + ParkedSet::kParkWaitMs, &lines); + for (const std::string& s : lines) log_line(s); rt.marks = ListMarks(kMarkSlots); rt.deferred = RetireList(); } else { diff --git a/clean/rr_f/runtime/tests/rr_retire_test.cpp b/clean/rr_f/runtime/tests/rr_retire_test.cpp index 5fed58c8..3f32329e 100644 --- a/clean/rr_f/runtime/tests/rr_retire_test.cpp +++ b/clean/rr_f/runtime/tests/rr_retire_test.cpp @@ -7,6 +7,7 @@ // Original WrongGPU code. #include +#include #include #include "rr_retire.h" @@ -34,14 +35,13 @@ void check(bool ok, const char* what) { struct Gpu { ListMarks marks; std::vector words; - std::vector slot_of; // by serial - explicit Gpu(uint32_t n = 16) : marks(n), words(n, 0), slot_of(1, 0) {} + std::map slot_of; // serial -> slot (a map: the wraparound test uses serials above 2^32) + explicit Gpu(uint32_t n = 16) : marks(n), words(n, 0) {} uint64_t record() { // the runtime records a list: serial and slot uint32_t s = 0; const uint64_t serial = marks.begin(&s); if (serial) { - if (slot_of.size() <= serial) slot_of.resize((size_t)serial + 1, 0); - slot_of[(size_t)serial] = s; + slot_of[serial] = s; } return serial; } @@ -165,11 +165,13 @@ void device_switch_keeps_old_objects() { p.deferred.retire(&pso, {a}, p.marks, false); p.last = {&marker, &device}; p.last_counted = 1; - ps.park(std::move(p)); std::vector log; bool removed = false; auto is_removed = [&](Parked&) { return removed; }; - int c = ps.pump(is_removed, &log); + auto no_sleep = [](uint32_t) {}; + int c = ps.park(std::move(p), is_removed, no_sleep, 0, &log); + log.clear(); + c += ps.pump(is_removed, &log); check(c == 0 && heap.releases + arena.releases + pso.releases + marker.releases + device.releases == 0 && ps.size() == 1, "device switched with list A in flight: the old device's objects, marker buffer and device are all kept"); old.run(a); // the old device's queue runs A later @@ -190,8 +192,8 @@ void device_switch_keeps_old_objects() { q.deferred.retire(&h2, {b}, q.marks, false); q.last = {&m2, &d2}; q.last_counted = 1; - ps.park(std::move(q)); - c = ps.pump(is_removed, &log); + c = ps.park(std::move(q), is_removed, no_sleep, 0, &log); + c += ps.pump(is_removed, &log); check(c == 0 && h2.releases == 0, "second old device, list in flight, not removed: kept"); removed = true; c = ps.pump(is_removed, &log); @@ -199,24 +201,105 @@ void device_switch_keeps_old_objects() { "old device removed (GetDeviceRemovedReason): its objects released once each"); } -void device_switch_bound() { - ParkedSet ps; - std::vector gpus(ParkedSet::kMaxParked + 1); - std::vector objs(gpus.size()); - for (size_t i = 0; i < gpus.size(); ++i) { - const uint64_t s = gpus[i].record(); // each old device has a list that never completes +void wraparound_stale_marker() { + // The #82 final review: the GPU writes only the low 32 bits of a serial and a free slot keeps its last value. A slot + // that stays free while the serials go round 2^32 must not let that stale value prove its next holder. + Gpu g(2); + const uint64_t x = g.record(); // slot 0 + g.run(x); + g.poll(); // x complete: slot 0 free, still holding (uint32_t)x + const uint64_t y = g.record(); // slot 1, kept in flight so that the next list goes to slot 0 + check(g.slot_of[(size_t)y] == 1 && !g.marks.done(y), "list Y holds slot 1, in flight"); + g.marks.skip_to(x + 0xffffffffull); // the next serial would be x + 2^32: the same low 32 bits as slot 0's value + const uint64_t z = g.record(); + check(z != 0 && g.slot_of[(size_t)z] == 0, "the next list takes slot 0 (the slot with the stale value)"); + check((uint32_t)z != (uint32_t)x && z > x + 0xffffffffull, + "after 2^32 serials: the serial matching slot 0's stale value was skipped"); + g.poll(); // Z has not run; slot 0 still holds x's value + check(!g.marks.done(z), "the stale value of slot 0 does not prove the new list (never submitted)"); + g.run(z); + g.poll(); + check(g.marks.done(z) && !g.marks.done(y), "Z completes when it writes its own value; Y still in flight"); + + // a fresh slot reads 0: a serial whose low 32 bits are 0 is never handed to it + Gpu h(4); + h.marks.skip_to(0xffffffffull); + const uint64_t w = h.record(); + check(w != 0 && (uint32_t)w != 0 && h.words[h.slot_of[(size_t)w]] == 0, "a fresh slot never gets a serial ending in 32 zero bits"); + h.poll(); + check(!h.marks.done(w), "a fresh slot's 0 does not prove its first list"); +} + +// one old device with a list in flight (never completing on its own) and one counted object +struct OldDevice { + Gpu gpu; + uint64_t list = 0; + Fake obj, marker, device; + Parked make(uint64_t id) { + list = gpu.record(); Parked p; - p.id = i + 1; - p.marks = gpus[i].marks; - p.words = gpus[i].words.data(); - p.deferred.retire(&objs[i], {s}, p.marks, true); - const std::string line = ps.park(std::move(p)); - if (i == gpus.size() - 1) check(line.find("no longer tracked") != std::string::npos, "the bound is logged"); + p.id = id; + p.marks = gpu.marks; + p.words = gpu.words.data(); + p.deferred.retire(&obj, {list}, p.marks, true); + p.last = {&marker, &device}; + p.last_counted = 1; + return p; + } + int releases() const { return obj.releases + marker.releases + device.releases; } +}; + +void device_switch_soft_cap() { + // six device switches, each with a list still in flight on the device switched away from + const size_t N = 6; + std::vector log; + { // none completes, none removed: all six stay tracked (the cap is soft), nothing released, each wait bounded + ParkedSet ps; + std::vector d(N); + int sleeps = 0, c = 0; + auto never = [](Parked&) { return false; }; + auto count_sleep = [&](uint32_t ms) { sleeps += (int)ms; }; + for (size_t i = 0; i < N; ++i) c += ps.park(d[i].make(i + 1), never, count_sleep, 2000, &log); + int rel = 0; + for (const OldDevice& o : d) rel += o.releases(); + check(ps.size() == N && rel == 0 && c == 0, + "6 switches with work pending, nothing completes: all 6 old devices stay tracked, nothing released"); + check(sleeps == 2 * 2000, "the 5th and 6th switch each waited at most 2000 ms (bounded) for the oldest"); + bool soft = false; + for (const std::string& s : log) soft = soft || s.find("stays tracked") != std::string::npos; + check(soft, "the soft cap is logged"); + for (OldDevice& o : d) o.gpu.run(o.list); // later, every old device's list completes + c = ps.pump(never, &log); + bool once = true; + for (const OldDevice& o : d) once = once && o.obj.releases == 1 && o.marker.releases == 1 && o.device.releases == 1; + check(ps.empty() && once && c == (int)(2 * N), "their lists complete later: every object of all 6 released exactly once"); + } + { // the oldest one's list completes during the bounded wait of the 5th switch: it is released then + ParkedSet ps; + std::vector d(N); + auto never = [](Parked&) { return false; }; + int calls = 0; + auto gpu_runs_oldest = [&](uint32_t) { if (++calls == 3) d[0].gpu.run(d[0].list); }; + int c = 0; + for (size_t i = 0; i < 5; ++i) c += ps.park(d[i].make(i + 1), never, gpu_runs_oldest, 2000, &log); + check(ps.size() == 4 && d[0].obj.releases == 1 && d[0].marker.releases == 1 && d[0].device.releases == 1 && c == 2, + "5th switch: the oldest's list completes within the wait, it is released once (4 tracked)"); + check(calls == 3, "the wait ended as soon as the oldest completed"); + int rel = 0; + for (size_t i = 1; i < 5; ++i) rel += d[i].releases(); + check(rel == 0, "the other old devices are untouched"); + } + { // the oldest one's device is removed: released at the 5th switch without waiting + ParkedSet ps; + std::vector d(N); + int sleeps = 0; + auto oldest_removed = [](Parked& p) { return p.id == 1; }; + auto count_sleep = [&](uint32_t ms) { sleeps += (int)ms; }; + for (size_t i = 0; i < 4; ++i) ps.park(d[i].make(i + 1), [](Parked&) { return false; }, count_sleep, 2000, &log); + const int c = ps.park(d[4].make(5), oldest_removed, count_sleep, 2000, &log); + check(ps.size() == 4 && d[0].releases() == 3 && c == 2 && sleeps == 0, + "5th switch, the oldest's device removed: released at once, no wait"); } - int released = 0; - for (const Fake& f : objs) released += f.releases; - check(ps.size() == ParkedSet::kMaxParked && released == 0, - "more old devices than the bound: the oldest is no longer tracked, and nothing is released without proof"); } } // namespace @@ -227,7 +310,8 @@ int main() { never_submitted_list(); slot_reuse_needs_the_holders_proof(); device_switch_keeps_old_objects(); - device_switch_bound(); + wraparound_stale_marker(); + device_switch_soft_cap(); printf("%s (%d of %d checks failed)\n", g_failed ? "FAILED" : "passed", g_failed, g_checks); return g_failed ? 1 : 0; }