Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dir> <seconds>` 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 `<key>.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 `<key>.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 `<key>.fail` is newer than its
ready marker is not retried during the run (the runtime writes each request once and never rewrites
Expand Down
8 changes: 4 additions & 4 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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<Slot> uds, srvs, uavs;
x2w_startup::TranslatedSamplers samplers; // static ones only
x2w_startup::ComPtr<ID3D12RootSignature> root;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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<Srv> srvs;
std::vector<PixelUav> uavs; // manifest 'uav N tex2d table P O' (#1996 F): u0.. after the SRVs
std::vector<Ud> uds;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 43 additions & 0 deletions src/startup_stubs/manifest_retry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// When to look again for a shader cache manifest (<key>.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 <windows.h>

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
5 changes: 5 additions & 0 deletions tests/cmake/startup_manifest_retry.cmake
Original file line number Diff line number Diff line change
@@ -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)
75 changes: 75 additions & 0 deletions tests/startup_manifest_retry_check.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>

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;
}
Loading