Skip to content

startup: translate in-process compute programs on worker threads (opt-in) - #3571

Closed
JohnConnorNPC wants to merge 4 commits into
R0Y00:mainfrom
JohnConnorNPC:startup/native-compute-worker-v2
Closed

JohnConnorNPC wants to merge 4 commits into
R0Y00:mainfrom
JohnConnorNPC:startup/native-compute-worker-v2

Conversation

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Stacked on the cache-index PR from the same series: the first two commits here are that PR's. Rebased onto current main; one conflict in the compute dispatch translation (now calls compile_native_shader) was resolved by hand. This rebase was not rebuilt or re-run; the measurements below were taken before it.

Summary

With X2W_NATIVE_SHADERS, the first Dispatch of a compute key translates it in the stand-in (frontend + DXC) while
holding draw_lock. A translation takes from milliseconds to several seconds. When a new scene brings hundreds of new
keys, draw_lock is held for most of the wall time, and draws and Dispatches on other threads wait behind it.

X2W_NATIVE_COMPUTE_WORKERS=<1-8> moves that translation onto worker threads (src/startup_stubs/native_compute_worker.h):

  • Dispatch queues a key once and polls it on later Dispatches without waiting. Until a worker has finished, that key's
    Dispatches are refused with in-process translation still running (counted as untranslated).
  • Ready: the worker's result (entry values, views, samplers, root, pipeline) is moved into the key's entry. Refused:
    the key reads its published manifest in that Dispatch, as before. The native-first order is kept: no published
    manifest is read while a key is still translating.
  • Each worker has its own DXC compiler. The translator's statics are atomics and one mutex-guarded cache; frozen
    requests are locked; the D3D12 device is free-threaded; DXC and frontend cache publishes use unique temporary names
    and accept an identical winner.
  • x2w-draw native compute workers <n>: ... every 10 s: queued, translated, refused, waiting, most at once, longest
    wait, time translating, and Dispatches refused while their key was translating.
  • Unset, 0 or any other value: unchanged. The Dispatch translates, and its first Dispatch is recorded, as
    startup_compute_dispatch_native_check step 1 requires. That is why the switch is opt-in.

Trade-off

The first Dispatches of a key that would translate are now skipped until a worker has it. Before, they waited (with
the whole stand-in) and then ran. In the measured run below (one worker, the same mechanism):

  • 1,012,178 Dispatches were refused while their key was translating, all between 180 s and 670 s. The peak was
    480-510 s, at 11-14k per second.
  • Presents crawled at ~0.6 fps from 184 s to 480 s while the worker was ~100% busy with 912 translations
    (487.8 s). In the first 600 logged keys, the queue wait reached 240 s and a key was adopted on average 95 s after
    it was queued.
  • Refused translations cost the most: 479 of those 600 keys were refused, at 469 ms on average against 180 ms for
    the 121 that translated, which is 91% of the worker's time.

Ideas to reduce it, not in this PR:

  • more workers (the switch takes up to 8; with 3, refused Dispatches fell from 1.0 M to 36 k, below);
  • a priority for the keys whose Dispatches are refused most often;
  • persisting refusals per key and native revision (proposal; it changes cache contents);
  • a cap on queued keys, translating the rest inline.

Tests

Wine only (Proton wine 11.0, clang-cl cross build). Windows CI has not run. No device fixture covers the switch
yet. The existing native compute fixtures run unchanged with the switch unset, but were not run locally.

test result
startup_native_compute_worker exit 0, 0 failed: (a) 3 submits + 1000 polls in 0.1 ms with the work held; (e) 3 workers ran 3 at once, 1 ran 1
..._inline_control (work in submit(), as before) fails (a) only: 3003 ms while the work was held
..._one_worker_control fails (e) only

Measured (local game run, one console executable under test, on 26df182 with the commits cherry-picked)

run presents stop run ends (same access violation) draw_lock max in Dispatch translation
A1, A2 (base) 181.2 s, 181.1 s 216.2 s, 217.0 s not measured
C1, C2 (+ PR-1 first commit) 166.4 s, 191.3 s 202.1 s, 231.2 s 1122 ms, 1713 ms
D1, D2 (+ worker, 1 thread) crawl 184-480 s / 197-543 s, then 10-12 fps 1881.7 s, 2113.3 s 122.7 ms per call
E1 (+ this commit, 3 workers, + PR-1 second commit) crawl 197-525 s, then 11-13 fps 2729.5 s (host out of memory) 600 ms per 10 s window
  • D1/D2 used a local experiment equivalent to this commit: one worker, switch always on. E1 ran this commit.
  • E1 with 3 workers: 35,746 Dispatches were refused while their key was translating (D1: 1,012,178). The longest queue
    wait was 95 s (D1: 240 s), and the queue was empty about 130 s after the burst began.
  • The crawl window did not move with 3 workers. In E1, draw_lock was nearly free during it (Dispatch translation
    0-3 s per 10 s). It matches the title's streaming phase: storage decompression requests rise and then flatten exactly
    where frames recover. That is an inference from timing; what bounds that phase was not measured.

Under Wine a missing name in the ~274k-entry shader cache costs ~155 ms,
and the draw and Dispatch paths probed the cache root with draw_lock held:
request parts, the slot 57 base manifest, graphics and compute manifests
and the compute .cs.hlsl. These now ask the cache index first and skip
the disk for an absent name.

The index gains a change feed: one thread queues ReadDirectoryChangesW on
the cache, then makes the listing, and adds every created or renamed-to
name. req_ names (written only by runtimes) use the listing and this
process's own writes. Translator files are answered absent only while the
feed is reading and was reading before the listing began; a dropped batch
or a feed error sends them back to the disk. These probes never make or
wait for the listing, so draw_lock no longer waits for it.

A 10 s counter line reports each probe's skipped and on-disk counts, the
feed state, records, added names, lookups that found a name added after
the listing, and contradictions.
A compute manifest that is absent, stale or unmarked is looked for again
on the retry schedule, and each retry re-reads <key>.txt with draw_lock
held. With hundreds of such keys those re-reads add up to seconds of
draw_lock per 10 s, although nothing in the cache changed.

The change feed now also watches last writes and sizes, and numbers every
record whose name starts with a cache key. A retry takes a change mark
before it looks on disk and keeps it with its miss. The next retry, while
no <key>.* name has a record from that mark on, repeats the miss without
a file-system call. A created, removed, renamed or rewritten file of the
key sends it back to the disk, and so does a feed that is not reading.

The cache probes line counts these retries as "compute retry unchanged".
With X2W_NATIVE_SHADERS, the first Dispatch of a compute key translates
it in the stand-in while holding draw_lock. One translation takes up to
seconds, so a burst of new keys holds draw_lock for nearly all of the
wall time and draws and Dispatches on other threads wait behind it.

X2W_NATIVE_COMPUTE_WORKERS=<1-8> moves that translation onto worker
threads (native_compute_worker.h). Dispatch queues a key once and polls
it on later Dispatches without waiting; until a worker has finished,
that key's Dispatches are refused with "in-process translation still
running". A ready result is moved into the key's entry; a refused key
then reads its published manifest, as before. Each worker has its own
DXC compiler. Unset, the Dispatch translates as before.

A 10 s line reports the workers' queue, results, longest wait, time
translating and the Dispatches refused while a key was translating.
@lukaslw-lab

Copy link
Copy Markdown
Contributor

This overlaps #3506 (which continues #3467). Both move the in-process compute translation off draw_lock onto worker threads, and both rewrite translate_compute_natively and its call in record_translated_dispatch. Whichever merges second has to be redone on top of the other.

What each one does that the other doesn't:

#3506 #3571 at b3a48a59
Switch none: on whenever X2W_NATIVE_SHADERS is set opt-in X2W_NATIVE_COMPUTE_WORKERS=1..8
Workers one thread pool worker (d3d12_compute_dispatch.inl:714 there) 1-8 workers, each with its own DXC compiler (:631)
Dispatch of a key still translating binds the published translation if one exists, otherwise counted as untranslated always refused (native_compute, :718)
Published manifest load outside draw_lock, one load per key unchanged, under draw_lock
Native result arrives replaces the published result once per key moved into the key's entry

Your E1/D1 numbers are the case for more than one worker: 36 k refused Dispatches with three workers against 1.0 M with one. #3506's published fallback covers the other side: Dispatches of a pending key that has a published translation are drawn, not refused. #3506 has an approving review.

Proposal: rebase this onto #3506 and keep only the multi-worker part, i.e. the per-thread compiler, the worker count and the status line, as a follow-up to it. With the fallback in place, only keys with no published translation are refused while they translate, which shrinks the trade-off that keeps this switch opt-in. #3570 (the index) is independent and can stay as it is.

The cache index check now runs before the plain-key manifest probe instead of
inside it, so the guard tests/startup_gfx2hlsl_ps57in_check.py reads is the
same expression as on main. Call order and probe counts are unchanged.
@JohnConnorNPC

Copy link
Copy Markdown
Collaborator Author

Closing this one: it overlaps #3506, which already moves the in-process compute translation off draw_lock onto workers and rewrites the same translate_compute_natively path. Having two competing rewrites of that function would only force a redo for whichever lands second, so the worker change is better reviewed in #3506.

The cache-index change in #3570 is separate and stays open.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants