From 7db7d2527dca7ec9a321b561dcb50d08b020a736 Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Mon, 11 May 2026 17:59:49 +0000 Subject: [PATCH] gpu_hw_d3d11 + gpu_hw_d3d12 + gpu_hw_vulkan: lower background shader-compile worker priority The three backends with a background worker thread for shader precompilation (D3D11, D3D12, Vulkan) currently spawn the worker at default priority and let it run flat-out until the matrix is filled. On a high-core-count machine with spare cycles this is fine - the OS scheduler just gives the worker an unused core. But on more constrained hardware the worker competes directly with the runloop, CPU emulator, audio mixer, and frontend render thread, all at the same priority. The OS time-slices between them; with a heavy texture filter and a 144-2160 cell matrix that's tens of seconds of contention immediately after boot or texture-filter change. Even though the lock-free fast-path commits (3d9d624, a29d202) eliminated the mutex-contention stall on the draw side, raw CPU pressure can still cause frame drops and audio glitches on low-spec setups. Adds a tiny portable helper, common/thread_priority.h, with a single LowerCurrentThreadPriority() function that drops the calling thread to a 'below normal' scheduling class. Wired into all three workers' entry points - it's the first thing they do. Platform support: Windows: SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL). Straightforward, per-thread, well-supported. Linux: setpriority(PRIO_PROCESS, 0, +5). On Linux/NPTL this targets the calling THREAD rather than the whole process - a long-standing Linux deviation from POSIX, documented in setpriority(2). Verified empirically on this machine: a worker that called the helper saw its nice rise 0 -> 5 while the main thread it forked from stayed at 0. +5 is the conventional 'background work' offset, far below the default 20 RLIMIT_NICE ceiling so no privileges needed. pthread_setschedparam with SCHED_OTHER is deliberately NOT used on Linux - SCHED_OTHER only accepts priority 0 there, so it would be a no-op. macOS / *BSD: pthread_setschedparam with SCHED_OTHER, target priority halfway between current and sched_get_priority_min. Halfway rather than absolute minimum because going all the way to min has historically caused priority-inversion deadlocks when a low-priority thread holds a mutex a high-priority thread is waiting on. BELOW_NORMAL-ish, not idle. Per-thread by virtue of pthread_setschedparam targeting pthread_self(). Other Unices: same path as macOS/BSDs. Reasonable best-effort default; failure (return false) keeps the worker at default priority - no incorrect behaviour. We deliberately avoid the POSIX-portable setpriority() with PRIO_PROCESS on non-Linux platforms because POSIX defines it as a per-process attribute, which would lower the priority of the runloop and audio threads too - the exact opposite of what we want. All variants return bool. Failure is silent - the worker just keeps running at whatever priority the platform gave it. The helper has no side effects beyond the priority change itself. OpenGL backend is intentionally unchanged: it has no background worker thread (the libretro hardware-renderer protocol exposes a single GL context bound to the runloop, so 'Lazy' mode degrades to 'Disabled' per commit 5c9cf3e). Lowering the runloop's priority would obviously be bad. Build verification: - linux platform=unix: clean, zero warnings, .so 8040152 (+48 bytes for the setpriority PLT call and the inline helper body in the Vulkan worker). 25 retro_* exports unchanged. - x86_64-w64-mingw32-g++ -fsyntax-only on gpu_hw_d3d11.cpp, gpu_hw_d3d12.cpp (with the existing ID3D12Debug stub workaround), and gpu_hw_vulkan.cpp: all clean. The WIN32_LEAN_AND_MEAN define is guarded with ifndef to avoid redefinition against windows_headers.h which defines it as 1. - Runtime smoke test on Linux: spawned a worker that called LowerCurrentThreadPriority(), confirmed worker nice 0 -> 5 while main thread nice stayed at 0. Per-thread granularity validated. Files added/touched: - src/common/thread_priority.h: new header, single inline function. Self-contained, no other dependencies. - src/core/gpu_hw_vulkan.cpp: include + call at top of ShaderCompileThreadEntryPoint. - src/core/gpu_hw_d3d11.cpp: include + call at top of ShaderCompileThreadEntryPoint. - src/core/gpu_hw_d3d12.cpp: include + call at top of ShaderCompileThreadEntryPoint. --- src/common/thread_priority.h | 122 +++++++++++++++++++++++++++++++++++ src/core/gpu_hw_d3d11.cpp | 8 +++ src/core/gpu_hw_d3d12.cpp | 8 +++ src/core/gpu_hw_vulkan.cpp | 8 +++ 4 files changed, 146 insertions(+) create mode 100644 src/common/thread_priority.h diff --git a/src/common/thread_priority.h b/src/common/thread_priority.h new file mode 100644 index 00000000..f750660a --- /dev/null +++ b/src/common/thread_priority.h @@ -0,0 +1,122 @@ +// Portable best-effort current-thread priority adjustment. +// +// Used by the GPU backend shader-compile workers to lower their +// scheduling priority to "below normal" so the runloop / CPU +// emulation / audio threads stay responsive on CPU-contended +// systems. On a high-core-count machine with spare cycles the +// effect is barely observable: the worker still runs flat-out +// when nothing else competes for the core. On a 4-core or lower +// machine, the lowered worker yields to default-priority threads +// when they need a core, so the runloop keeps its frame budget. +// +// Failure is silent. Priority adjustment is best-effort - if the +// platform refuses (insufficient privileges, unsupported scheduler, +// etc.) the worker continues at default priority and the only cost +// is a small amount of contention on low-end hardware. +// +// All variants here operate on the CALLING thread, so this helper +// must be invoked from inside the worker function itself (at the +// top of the entry-point routine), not from the spawner. +// +// Platform notes: +// +// Windows: SetThreadPriority. Reliable, per-thread. +// +// Linux: setpriority(PRIO_PROCESS, 0, ...) actually operates per +// thread under NPTL despite the function name. This is a +// long-standing Linux/NPTL deviation from POSIX (documented in +// the setpriority(2) man page). We rely on that explicitly - +// using pthread_setschedparam with SCHED_OTHER on Linux is a +// no-op because SCHED_OTHER only accepts priority 0. +// +// macOS / *BSD: pthread_setschedparam under SCHED_OTHER. Honours +// a sched_priority within a narrow band; we move halfway from +// the current value toward the minimum. Going to absolute min +// has historically risked priority-inversion deadlocks on +// mutex-heavy workloads. +// +// Other Unices (Solaris, Haiku, etc.): the macOS/*BSD path is a +// reasonable best-effort default. If it returns an error the +// caller just gets default priority - no incorrect behaviour. +// +// NB: We deliberately do NOT use the POSIX-portable setpriority() +// with PRIO_PROCESS on non-Linux platforms, because POSIX defines +// it as a per-process attribute and applying it would lower the +// priority of the runloop and audio threads too - the exact +// opposite of what we want. + +#pragma once + +#if defined(_WIN32) + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include + +#elif defined(__linux__) + +#include +#include +#include + +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \ + defined(__DragonFly__) || defined(__unix__) + +#include +#include + +#endif + +namespace ThreadPriority { + +// Lower the priority of the calling thread to "below normal". +// +// Returns true on success, false on failure. Failure is non-fatal; +// the caller should generally ignore the return value unless +// logging is desired. Either way the thread keeps running, just at +// whatever priority the platform gave it. +inline bool LowerCurrentThreadPriority() +{ +#if defined(_WIN32) + return SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL) != 0; +#elif defined(__linux__) + // setpriority(PRIO_PROCESS, 0, +5) on Linux/NPTL targets the + // calling THREAD, not the whole process - this is documented and + // relied upon by many applications. +5 nice is the conventional + // offset for "background work that should yield to interactive + // threads but still make forward progress". + // + // RLIMIT_NICE caps how high (numerically) we can go without + // privileges; +5 is far below the default 20 ceiling and works + // for every reasonable launching shell. + return setpriority(PRIO_PROCESS, 0, 5) == 0; +#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \ + defined(__DragonFly__) || defined(__unix__) + // pthread_setschedparam under SCHED_OTHER. The range of valid + // priorities is platform-specific; pick a value halfway between + // the current priority and the minimum. Halfway rather than + // absolute-min because going to min has been observed to cause + // priority-inversion deadlocks when the worker holds a mutex a + // higher-priority thread is waiting on. + sched_param sp = {}; + int policy = 0; + if (pthread_getschedparam(pthread_self(), &policy, &sp) != 0) + return false; + const int low = sched_get_priority_min(policy); + if (low < 0) + return false; + const int current = sp.sched_priority; + const int target = (low + current) / 2; + if (target == current) + return false; // already at min, nothing to do + sp.sched_priority = target; + return pthread_setschedparam(pthread_self(), policy, &sp) == 0; +#else + // Unknown platform - silently no-op rather than break the build. + // The caller still gets default-priority scheduling. + return false; +#endif +} + +} // namespace ThreadPriority diff --git a/src/core/gpu_hw_d3d11.cpp b/src/core/gpu_hw_d3d11.cpp index 4030f576..40b9346b 100644 --- a/src/core/gpu_hw_d3d11.cpp +++ b/src/core/gpu_hw_d3d11.cpp @@ -3,6 +3,7 @@ #include "common/display.hlsl.h" #include "common/log.h" #include "common/state_wrapper.h" +#include "common/thread_priority.h" #include "common/timer.h" #include "gpu_hw_shadergen.h" #include "gpu_sw_backend.h" @@ -1179,6 +1180,13 @@ void GPU_HW_D3D11::StopShaderCompileThread() void GPU_HW_D3D11::ShaderCompileThreadEntryPoint() { + // Lower this worker's scheduling priority to "below normal" so + // it doesn't compete with the runloop / CPU emulation / audio + // threads on CPU-contended systems. Best-effort: if the platform + // refuses we just keep going at default priority. See + // common/thread_priority.h for the per-platform mechanics. + ThreadPriority::LowerCurrentThreadPriority(); + // Walk the matrix in (render, texture, dither, interlace) order and // call GetBatchPixelShader on each cell. Each call takes // m_batch_shader_mutex internally; we don't hold a lock across the diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index a00ec5f3..988a63fb 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -6,6 +6,7 @@ #include "common/d3d12/shader_cache.h" #include "common/d3d12/util.h" #include "common/log.h" +#include "common/thread_priority.h" #include "common/timer.h" #include "gpu_hw_shadergen.h" #include "host_display.h" @@ -768,6 +769,13 @@ void GPU_HW_D3D12::StopShaderCompileThread() void GPU_HW_D3D12::ShaderCompileThreadEntryPoint() { + // Lower this worker's scheduling priority to "below normal" so + // it doesn't compete with the runloop / CPU emulation / audio + // threads on CPU-contended systems. Best-effort: if the platform + // refuses we just keep going at default priority. See + // common/thread_priority.h for the per-platform mechanics. + ThreadPriority::LowerCurrentThreadPriority(); + // Walks the PSO matrix in (depth_test, render_mode, // transparency_mode, texture_mode, dithering, interlacing) order // - the same order CompilePipelines uses for the Enabled precompile diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index 105e4cc7..15a462f2 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -1,6 +1,7 @@ #include "gpu_hw_vulkan.h" #include "common/log.h" #include "common/state_wrapper.h" +#include "common/thread_priority.h" #include "common/timer.h" #include "common/vulkan/builders.h" #include "common/vulkan/context.h" @@ -1943,6 +1944,13 @@ void GPU_HW_Vulkan::StopShaderCompileThread() void GPU_HW_Vulkan::ShaderCompileThreadEntryPoint() { + // Lower this worker's scheduling priority to "below normal" so + // it doesn't compete with the runloop / CPU emulation / audio + // threads on CPU-contended systems. Best-effort: if the platform + // refuses we just keep going at default priority. See + // common/thread_priority.h for the per-platform mechanics. + ThreadPriority::LowerCurrentThreadPriority(); + // Same nesting as the Enabled-mode precompile loop above. The quit // flag is checked between cells so DestroyPipelines can stop the // worker within at most one PSO compile of latency (Vulkan PSO