From 2f0473bd676422f7e4dd8ab5c2aded14a35eb8c8 Mon Sep 17 00:00:00 2001 From: Nick Stallman Date: Fri, 27 Nov 2026 07:36:40 +1100 Subject: [PATCH] launcher: wake the live shader worker for a request while jobs run While any job was running, the live worker waited on its futures with a 50 ms timeout and did not listen for the directory notification, so a request published beside a running compile waited for the next rescan. An idle worker already woke on the notification. A finished job now writes to a pipe that the notification wait polls with the inotify descriptor, so one wait serves both and the worker uses it whether or not jobs are running. Without inotify the worker waits on its futures with the bounded rescan, as before. A serial stream of 30 quick recorded ProjectScarlett requests beside one long compute compile, four workers, real xshader, DXC and dxv, three runs each: the median reply went from 50.4-50.8 ms to 1.7-2.4 ms. With no other job running it is 2.3-2.9 ms either way. This is the host worker alone, with no guest and no GPU. A new test publishes a request 10 ms after a blocked job starts and expects it to start within 20 ms. It took 35 ms before the change. The 381 Python tests and the no-GPU check pass. The full GPU check was not run. Co-Authored-By: Claude Fable 5.2 --- project_tuxbox/runtime_shaders.py | 50 +++++++++++++++++++++------- tests/test_runtime_shader_workers.py | 37 +++++++++++++++++--- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/project_tuxbox/runtime_shaders.py b/project_tuxbox/runtime_shaders.py index a6a6f710..4ae206b0 100644 --- a/project_tuxbox/runtime_shaders.py +++ b/project_tuxbox/runtime_shaders.py @@ -431,7 +431,7 @@ def remove_request(path): def serve(session, stop, report, archive=None, jobs=1, *, dxc_helper=None): # Cargo can replace the release executable while another title is running. # Keep the executable we fingerprint private to this host worker. - with TemporaryDirectory(prefix="tuxbox-xshader-") as tmp, request_notifications(session / "requests", stop) as wait: + with TemporaryDirectory(prefix="tuxbox-xshader-") as tmp, request_notifications(session / "requests", stop) as (wait, wake): pinned = None if dxc_helper: try: @@ -442,7 +442,7 @@ def serve(session, stop, report, archive=None, jobs=1, *, dxc_helper=None): except OSError as error: report(f"Could not stage persistent DXC, using the CLI: {error}") with PersistentDxc(pinned, jobs, report) if pinned else nullcontext() as persistent: - _serve_requests(session, stop, report, Path(tmp) / "xshader", wait, archive, jobs, persistent=persistent) + _serve_requests(session, stop, report, Path(tmp) / "xshader", wait, archive, jobs, persistent=persistent, wake=wake) @contextmanager @@ -455,23 +455,40 @@ def request_notifications(directory, stop): init = getattr(libc, "inotify_init1", None) fd = init(os.O_NONBLOCK | os.O_CLOEXEC) if init else -1 if fd < 0: - yield lambda: stop.wait(0.05) + yield (lambda: stop.wait(0.05)), None return try: moved_to = 0x00000080 if libc.inotify_add_watch(fd, os.fsencode(directory), moved_to) < 0: - yield lambda: stop.wait(0.05) + yield (lambda: stop.wait(0.05)), None return changes = select.poll() changes.register(fd, select.POLLIN) + # A finished job wakes the same wait as a new request, so the worker + # can listen for requests while jobs run. + finished, finish = os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC) + changes.register(finished, select.POLLIN) def wait(): # A bounded fallback scan also handles filesystems without events # and lets shutdown finish when there is no outstanding work. - if changes.poll(50): - os.read(fd, 65536) + for ready, _ in changes.poll(50): + try: + os.read(ready, 65536) + except BlockingIOError: + pass + + def wake(): + try: + os.write(finish, b"\0") + except BlockingIOError: + pass # A full pipe wakes the wait already. - yield wait + try: + yield wait, wake + finally: + os.close(finished) + os.close(finish) finally: os.close(fd) @@ -537,7 +554,7 @@ def ordered_requests(directory): return [source for _, _, source in sorted(entries)] -def _serve_requests(session, stop, report, xshader, wait, archive, jobs=1, *, persistent=None): +def _serve_requests(session, stop, report, xshader, wait, archive, jobs=1, *, persistent=None, wake=None): def bind_cache(): options = {"dxc_helper": persistent.helper} if persistent is not None else {} tools, cache = snapshot_compiler_cache(xshader, session.parent, **options) @@ -636,10 +653,19 @@ def complete(source, key, future=None, error=None): except (OSError, ValueError, subprocess.SubprocessError) as exc: complete(source, key, error=exc) continue - active[key] = (source, pool.submit(compile_one, source, key, tools, cache)) - if active: - # Wake immediately when any worker completes, with a bounded - # rescan interval for new requests. Do not spin during stop. + future = pool.submit(compile_one, source, key, tools, cache) + if wake: + # The pool joins its threads before the caller closes + # the notifications, so no callback outlives them. + future.add_done_callback(lambda _: wake()) + active[key] = (source, future) + if active and wake: + # Wake immediately when any worker completes or a request is + # published. Do not spin during stop. + wait() + elif active: + # Without notifications, wake when any worker completes, with + # a bounded rescan interval for new requests. wait_futures([future for _, future in active.values()], timeout=0.05, return_when=FIRST_COMPLETED) elif not stop.is_set(): diff --git a/tests/test_runtime_shader_workers.py b/tests/test_runtime_shader_workers.py index 75b3056d..b65b0e39 100644 --- a/tests/test_runtime_shader_workers.py +++ b/tests/test_runtime_shader_workers.py @@ -11,7 +11,7 @@ from unittest.mock import Mock, patch from project_tuxbox.runtime_shaders import ( - ShaderRefused, _serve_requests, ordered_requests, runtime_jobs, + ShaderRefused, _serve_requests, ordered_requests, runtime_jobs, serve, ) @@ -46,11 +46,14 @@ def request(self, name, suffix=".dxbc"): path.write_text(name) return path - def start(self, compile_one, jobs=2, archive=None): + def start(self, compile_one, jobs=2, archive=None, notified=False): def worker(): try: - _serve_requests(self.session, self.stop, self.logs.append, - self.root / "xshader", lambda: sleep(0.005), archive, jobs) + if notified: + serve(self.session, self.stop, self.logs.append, archive, jobs) + else: + _serve_requests(self.session, self.stop, self.logs.append, + self.root / "xshader", lambda: sleep(0.005), archive, jobs) except Exception as exc: self.errors.append(exc) @@ -112,6 +115,32 @@ def compile_one(source, key, tools, cache, session, remember=None): self.until(lambda: not any(self.requests.iterdir())) self.assertEqual(counts, Counter({p.stem: 1 for p in (slow, fast, later)})) + def test_request_published_beside_a_running_job_starts_at_once(self): + slow = self.request("slow") + fast = self.request("fast") + staged = self.root / fast.name + fast.rename(staged) + running = Event() + started = [] + + def compile_one(source, key, tools, cache, session, remember=None): + if source == slow: + running.set() + if not self.release.wait(5): + raise TimeoutError("test gate") + else: + started.append(monotonic()) + return False + + self.start(compile_one, notified=True) + self.until(running.is_set) + # Publish partway through what was a 50 ms rescan interval. + sleep(0.01) + published = monotonic() + staged.rename(fast) + self.until(lambda: started) + self.assertLess(started[0] - published, 0.02) + def test_shutdown_drains_active_jobs_without_starting_new_jobs(self): paths = [self.request(str(i)) for i in range(5)] started = []