kernel: keep guest threads below the GPU thread and run core-pinned spinning workers one step lower #1050
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1050
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1050"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@tvhdev on GitHub · 2026-10-04 17:27 UTC · original
tvhdev:feat/host-thread-priority→main· +108 −28 · 2 files · 2 commits · openSymptom
Demon's Souls (PPSA01341 1.07) on Windows stalls for seconds at a time at 100 % CPU, with the frame
counter standing still. Moving the focus away from the emulator window (Alt+Tab) ends the stall at once,
and it comes back later. The command processor thread (Thread_Gpu) is the one that stands still: in a
CPU measurement it gets 0 ms/s during the stall while 13 "BPE JobWorkerThread" threads use 7 to 8 of
the 8 host cores.
Cause
Two things add up.
PthreadAttrSetschedparammaps the guest priority to a host priority: <= 478 to +2, >= 733 to -2,everything else to 0. A guest thread the game marks as important therefore outranks every thread of
the emulator, and the guest's normal band shares the default priority with the emulator's own threads.
0x1000), spinning on a lock-free job table. On the console each owns its core. Here all of them share 8
host cores in one priority band with the render thread (HighGraphics, 699) and the loaders, which are
pinned to other cores (0xa00) and produce the work the workers wait for. The spinners take the cores
and the command processor, which alone turns the guest's command buffers into GPU work, gets 0.39 to
0.45 of a core.
Why the foreground window matters is not measured. The stalls were seen only with the emulator in
front (7 samples with the emulator focused, none with another window focused), which would fit the
longer time slices Windows gives the foreground process, but that is a guess.
Fix
THREAD_PRIORITY_HIGHEST(Windows only).at or below the host's normal priority and the emulator's own threads stay above them.
decided once at thread start, because the main thread pins itself to core 0 for a while and must not
drop with it. The priority the guest reads back stays the band it asked for.
SetThreadPriority), becausePthreadAttrSetinheritscheddeliberately keepsinherit_schedout of the native attribute, so whetherwinpthreads honours the attribute at creation is not something this code can rely on.
PthreadGetPriorityForKernel(used for the order of semaphore waiters) now returns the class of theguest's own priority instead of reading the host priority back. With the new band host priority 0
means both "default" and "high", so reading it back would have reported 256 for every thread that
was never given a priority. On other platforms it still returns 700.
All of it is behind
KYTY_PLATFORM_WINDOWS. There are no environment switches and no extra logging.Verification
Windows 11, RTX 4070 Ti, i7-9700K (8 threads), Demon's Souls PPSA01341 1.07, same scene each time.
Frame rate on an unchanged build varies by a factor of three between runs, so only paired A/B in one
run says anything. Core-pinned rule, the 13 workers set from outside between phases, one run of about
six minutes in the same scene:
With the rule built in, a 75 s scene: 3.81 fps, no stall with an idle Thread_Gpu, Thread_Gpu 0.83 cores,
workers 5.9 cores in total.
The earlier step (command processor at HIGHEST, guest band 0 / -1 / -2) gave the command processor
0.81 of a core instead of 0.45 in one unpaired comparison, with the frame rate going from 1.9 to 3.5;
as said above, that number alone is not evidence.
Not verified:
pthread.cppandgraphicsRun.cppagainstcurrent main with the project's compile options, and the same changes have run in my working tree
for several days.
One commit, about 90 changed lines in two files.
@coderabbitai[bot] on GitHub · 2026-10-04 17:34 UTC · original · edited 2026-10-04 17:45 UTC
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
7a29786c-121f-4438-9d9f-7f844667b8a8📥 Commits
Reviewing files that changed from the base of the PR and between
f7b0b3b56band356568c1bb.📒 Files selected for processing (2)
src/graphics/guest_gpu/graphicsRun.cppsrc/kernel/pthread.cpp🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
📝 Walkthrough
Walkthrough
Windows guest thread priorities now map to host priority bands. Threads that start with single-core affinity receive a host priority one step lower. Thread startup and priority changes use this mapping. Priority reporting uses the stored guest priority on Windows and returns 700 on other platforms. The GPU thread attempts to use
THREAD_PRIORITY_HIGHESTon Windows and reports the Windows error code if the call fails.Priority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
35656The change adjusts Windows thread priorities to reduce stalls in one title. No concrete merge-blocking risk was found. Non-Windows behavior is unchanged, since priority reporting still returns 700 there.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
f7b0bThe changes reduce guest-controlled priority elevation. The main remaining risk is inconsistent scheduling state during concurrent priority changes, with likely impact on emulator responsiveness rather than increased privileges.
Retained concerns
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-10-04 17:35 UTC · original · reviewed
🧹 Nitpick comments (1)
ℹ️ Review info
⚙️ Run configuration
eeb402d6-02fd-4758-9485-80be640b5354📥 Commits
Reviewing files that changed from the base of the PR and between
719e02574aandf7b0b3b56b.📒 Files selected for processing (2)
src/graphics/guest_gpu/graphicsRun.cppsrc/kernel/pthread.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.