From 36d82780012e6617b5ca9de00400621047abdb8b Mon Sep 17 00:00:00 2001 From: JB2WIN Date: Thu, 26 Nov 2026 21:53:14 +0100 Subject: [PATCH] startup: back off the retry for an absent shader cache manifest The graphics and compute translation paths looked for a missing .txt again once per second per key, each time opening a cache file while holding draw_lock. With many keys that never get a translation, every draw and Dispatch waited on that file I/O. The wait now starts at 1 s after the first miss and doubles up to 32 s; a found manifest resets it. Both paths share x2w_startup::ManifestRetry. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- docs/startup-runtime.md | 6 +- src/startup_stubs/d3d12_compute_dispatch.inl | 8 +-- src/startup_stubs/d3d12_draw.inl | 14 ++-- src/startup_stubs/d3d12_xs_device.cpp | 1 + src/startup_stubs/manifest_retry.h | 43 +++++++++++ tests/cmake/startup_manifest_retry.cmake | 5 ++ tests/startup_manifest_retry_check.cpp | 75 ++++++++++++++++++++ 7 files changed, 140 insertions(+), 12 deletions(-) create mode 100644 src/startup_stubs/manifest_retry.h create mode 100644 tests/cmake/startup_manifest_retry.cmake create mode 100644 tests/startup_manifest_retry_check.cpp diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 2563133bb5..d526cd9cb5 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -873,7 +873,11 @@ the debugger and stops it, with its worker processes, when the debugger returns without `-Render` is ignored with a warning). The watcher repeats the `--pending` scan every 2 seconds (`--watch ` sets another interval), so a pipeline the title creates during the run is translated within seconds, and the runtime picks it up on a later draw instead of on the -next run. The cache persists: a later run reads every published `.txt` and translates only new +next run. The runtime looks for an absent manifest again 1 second after the first miss and doubles +the wait with each further miss up to 32 seconds. A translation published during the run is therefore +used on a draw or Dispatch at most 32 seconds later, and a key that never gets a translation costs one +cache lookup every 32 seconds instead of one per second (each lookup holds `draw_lock`, which every draw +and Dispatch takes). The cache persists: a later run reads every published `.txt` and translates only new requests. The watcher's first pass retries every earlier failure once, so a translator update reaches requests that failed in an earlier run; after that, a request whose `.fail` is newer than its ready marker is not retried during the run (the runtime writes each request once and never rewrites diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 7115c28500..880a739453 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -27,7 +27,7 @@ struct ComputeTranslation { }; u64 key = 0; int state = 0; // 0 not loaded, 1 ready, 2 unavailable - ULONGLONG retry_manifest_at = 0; // only an absent manifest can become ready later + x2w_startup::ManifestRetry manifest_retry; // only an absent manifest can become ready later std::vector uds, srvs, uavs; x2w_startup::TranslatedSamplers samplers; // static ones only x2w_startup::ComPtr root; @@ -73,10 +73,10 @@ const char* load_compute_translation(ComputeTranslation& tr) wchar_t path[MAX_PATH]; std::string manifest; if (!cache_path(path, L"", tr.key, L".txt") || !read_text(path, manifest)) { - tr.retry_manifest_at = GetTickCount64() + 1000; + tr.manifest_retry.missed(GetTickCount64()); return nullptr; } - tr.retry_manifest_at = 0; + tr.manifest_retry.found(); unsigned declared_uds = 0, line_number = 0; for (std::size_t at = 0; at < manifest.size(); ++line_number) { std::size_t end = manifest.find('\n', at); @@ -203,7 +203,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& reason = "the compute root set is not the one the pipeline was created with"; else if (!ensure_draw_heap(self->own().list_device)) reason = "no descriptor heap on this device"; else { - if (!tr->state || (tr->retry_manifest_at && GetTickCount64() >= tr->retry_manifest_at)) { + if (!tr->state || tr->manifest_retry.due(GetTickCount64())) { tr->unmodelled = load_compute_translation(*tr); if (tr->unmodelled) log("x2w-draw compute pipeline %016llx not translated: %s", key, tr->unmodelled); } diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index e1107e281b..65300b8bd0 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -137,7 +137,7 @@ struct Translation { enum UdKind { rect, base_vertex, start_instance, root_constant, stride, mrt_bits, d16_unorm16, desc_word3 }; struct Ud { int kind, a, b; }; int state = 0; // 1 ready, 2 unavailable - ULONGLONG retry_manifest_at = 0; // Only an absent manifest can become ready on a later draw. + x2w_startup::ManifestRetry manifest_retry; // Only an absent manifest can become ready on a later draw. std::vector srvs; std::vector uavs; // manifest 'uav N tex2d table P O' (#1996 F): u0.. after the SRVs std::vector uds; @@ -265,18 +265,18 @@ void load_translation(Translation& tr, const std::string& rtv_request = {}) std::string manifest; if (!cache_path(path, L"", tr.key, L".txt")) return; if (!read_text(path, manifest)) { - if (!tr.retry_manifest_at) { + if (!tr.manifest_retry.pending()) { InterlockedIncrement(&translation_missing); static LONG missing; if (rtv_request.empty() && first_calls(missing, 12)) log("x2w-draw no translation for pipeline %016llx", tr.key); } - tr.retry_manifest_at = GetTickCount64() + 1000; + tr.manifest_retry.missed(GetTickCount64()); return; } // The writer publishes this marker last. A malformed published result stays rejected. - const bool previously_missing = tr.retry_manifest_at != 0; - tr.retry_manifest_at = 0; + const bool previously_missing = tr.manifest_retry.pending(); + tr.manifest_retry.found(); bool matched_rtv = false; for (std::size_t at = 0; at < manifest.size();) { std::size_t end = manifest.find('\n', at); @@ -896,7 +896,7 @@ Translation* translation_for_targets(Translation* base, const Pass& pass) { if (!base) return nullptr; if (std::none_of(pass.target_formats, pass.target_formats + pass.target_count, needs_rtv_variant)) { - if (base->retry_manifest_at && GetTickCount64() >= base->retry_manifest_at) + if (base->manifest_retry.due(GetTickCount64())) load_translation(*base); return base; } @@ -913,7 +913,7 @@ Translation* translation_for_targets(Translation* base, const Pass& pass) publish_shader_request(key, L".rtv", request.data(), request.size()); log("x2w-draw no RTV translation for %016llx (%s)", key, request.c_str()); } - } else if (entry->retry_manifest_at && GetTickCount64() >= entry->retry_manifest_at) { + } else if (entry->manifest_retry.due(GetTickCount64())) { load_translation(*entry, request); } return entry; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index 56d4535571..b51278e604 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -17,6 +17,7 @@ #include "xmem_page_batch.h" #include "startup_stubs/local_log.h" #include "startup_stubs/logical_view_table.h" +#include "startup_stubs/manifest_retry.h" #include "startup_stubs/list_compute_root.h" #include "startup_stubs/overlay_window.h" #include "startup_stubs/draw_bmp.h" diff --git a/src/startup_stubs/manifest_retry.h b/src/startup_stubs/manifest_retry.h new file mode 100644 index 0000000000..71d2824e49 --- /dev/null +++ b/src/startup_stubs/manifest_retry.h @@ -0,0 +1,43 @@ +// When to look again for a shader cache manifest (.txt) that was absent. +// +// The graphics and compute translation paths look for a missing manifest again on a later draw or Dispatch, because +// the translator (run.ps1 -Translate) can publish it while the title runs. Each look opens a file in the shader cache +// while the caller holds draw_lock, which every draw and Dispatch takes. With a fixed 1 s retry, many keys that +// never get a translation each cost one file open per second, and every other list call waits on that lock. +// +// The wait starts at 1 s after the first miss and doubles with each further miss up to 32 s. A translation published +// during the run is still found, within 32 s at the latest; a key that is never translated costs one look every 32 s. +#pragma once + +#include + +namespace x2w_startup { + +constexpr ULONGLONG manifest_retry_first_ms = 1000; +constexpr unsigned manifest_retry_max_doublings = 5; // 1 s, 2 s, 4 s, 8 s, 16 s, then 32 s + +// The wait after the (misses + 1)-th miss in a row. +constexpr ULONGLONG manifest_retry_delay_ms(unsigned misses) noexcept +{ + return manifest_retry_first_ms << (misses < manifest_retry_max_doublings ? misses : manifest_retry_max_doublings); +} + +struct ManifestRetry { + ULONGLONG at = 0; // GetTickCount64 value of the next look, 0 when none is pending + unsigned misses = 0; // misses in a row since the manifest was last found + + bool pending() const noexcept { return at != 0; } + bool due(ULONGLONG now) const noexcept { return at != 0 && now >= at; } + void missed(ULONGLONG now) noexcept + { + at = now + manifest_retry_delay_ms(misses); + if (misses < manifest_retry_max_doublings) ++misses; + } + void found() noexcept + { + at = 0; + misses = 0; + } +}; + +} // namespace x2w_startup diff --git a/tests/cmake/startup_manifest_retry.cmake b/tests/cmake/startup_manifest_retry.cmake new file mode 100644 index 0000000000..dfb9e53e57 --- /dev/null +++ b/tests/cmake/startup_manifest_retry.cmake @@ -0,0 +1,5 @@ +# The retry schedule for an absent shader cache manifest (startup_stubs/manifest_retry.h). +add_executable(startup_manifest_retry_check tests/startup_manifest_retry_check.cpp) +target_include_directories(startup_manifest_retry_check PRIVATE src) +target_compile_features(startup_manifest_retry_check PRIVATE cxx_std_17) +add_test(NAME startup_manifest_retry COMMAND startup_manifest_retry_check) diff --git a/tests/startup_manifest_retry_check.cpp b/tests/startup_manifest_retry_check.cpp new file mode 100644 index 0000000000..b3f0acf198 --- /dev/null +++ b/tests/startup_manifest_retry_check.cpp @@ -0,0 +1,75 @@ +// The retry schedule for an absent shader cache manifest (startup_stubs/manifest_retry.h). +// The first look again comes 1 s after the first miss, each further miss doubles the wait, and the wait +// stops growing at 32 s. A found manifest starts the schedule over. +#include "startup_stubs/manifest_retry.h" + +#include + +namespace { + +int failures = 0; + +void expect(bool ok, const char* what) +{ + if (!ok) { + std::printf("FAIL: %s\n", what); + ++failures; + } +} + +} // namespace + +int main() +{ + using x2w_startup::ManifestRetry; + using x2w_startup::manifest_retry_delay_ms; + + expect(manifest_retry_delay_ms(0) == 1000, "first miss waits 1 s"); + expect(manifest_retry_delay_ms(1) == 2000, "second miss waits 2 s"); + expect(manifest_retry_delay_ms(4) == 16000, "fifth miss waits 16 s"); + expect(manifest_retry_delay_ms(5) == 32000, "sixth miss waits 32 s"); + expect(manifest_retry_delay_ms(6) == 32000, "the wait stops at 32 s"); + expect(manifest_retry_delay_ms(64) == 32000, "a large miss count does not overflow the shift"); + expect(manifest_retry_delay_ms(~0u) == 32000, "the largest miss count stays at 32 s"); + + ManifestRetry retry; + expect(!retry.pending(), "a fresh schedule has no retry pending"); + expect(!retry.due(0) && !retry.due(~0ull), "a fresh schedule is never due"); + + retry.missed(10000); + expect(retry.pending(), "a miss leaves a retry pending"); + expect(!retry.due(10999), "not due before 1 s"); + expect(retry.due(11000), "due at 1 s"); + + retry.missed(11000); + expect(!retry.due(12999) && retry.due(13000), "second miss: due after 2 s"); + + for (int i = 0; i < 20; ++i) retry.missed(100000); + expect(!retry.due(131999) && retry.due(132000), "many misses: due after 32 s"); + + retry.found(); + expect(!retry.pending() && !retry.due(~0ull), "a found manifest clears the retry"); + retry.missed(200000); + expect(!retry.due(200999) && retry.due(201000), "a later miss starts again at 1 s"); + + // One key that never gets a translation: over ten minutes the old fixed 1 s retry looked it up about 600 times; + // the schedule looks it up 23 times (6 lookups in the first 63 s, then one per 32 s). + { + ManifestRetry key; + unsigned lookups = 0; + unsigned long long now = 0; + key.missed(now); + ++lookups; + for (; now <= 600000; now += 50) + if (key.due(now)) { + key.missed(now); + ++lookups; + } + expect(lookups <= 25, "an absent key is looked up at most 25 times in ten minutes"); + expect(lookups >= 20, "an absent key is still looked up at least 20 times in ten minutes"); + } + + if (failures) return 1; + std::printf("startup_manifest_retry: all checks passed\n"); + return 0; +}