kernel: back off on repeated failed event-flag polls (cp2077) #2110
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!2110
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2110"
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?
@ArtyomZabroda on GitHub · 2026-11-04 18:03 UTC · original
ArtyomZabroda:pr/event-flag-poll-backoff→main· +71 −0 · 4 files · 1 commits · openA failed zero-timeout event-flag wait or poll returns in about 0.1 us
here, while on the ps5 it is a system call. Guests that poll a flag
in a loop therefore spin. Wwise's audio thread polls its tick flag while
dispatching empty jobs, about 400k times a second, which keeps every job
thread busy and the CPU at 100%.
Changes:
After 16 consecutive failed polls of the same flag, a thread yields
(up to 64 failures) and then sleeps 50 us after each failed poll.
A successful poll, a different flag, or more than 100 us between two
failed polls resets the count, so loops that do work between polls are
not slowed down. Waits with no timeout or a non-zero timeout are
unchanged. The first time a thread starts sleeping, the flag's name is
logged. --no-poll-backoff disables the backoff.
Before (~90-100% cpu usage)


After (~50% cpu usage):
@coderabbitai[bot] on GitHub · 2026-11-04 18:16 UTC · original
Walkthrough
The change adds a default-enabled configuration setting for event-flag poll backoff and a command-line option to disable it. Zero-timeout event-flag waits and event-flag polls report their results to per-thread tracking. Repeated failures on the same flag can yield or sleep based on the failure count and elapsed time.
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Moderate · up to
a41caConcurrent deletion can crash the emulator during poll backoff. Keep the flag alive through logging, or capture its name while protected, before merging.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 4 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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
@ -79,0 +124,4 @@if (!state.logged) {state.logged = true;LOGF("\tEventFlag poll backoff: thread %d sleeps between failed polls of %s\n",Common::Thread::GetThreadIdUnique(), flag->GetName().c_str());@coderabbitai[bot] on GitHub · 2026-11-04 18:16 UTC · original
Keep the flag alive while logging its name.
If another thread deletes the flag after
Waitreleases its mutex, this dereference can read freed memory and crash the emulator. This occurs when logging is enabled and the poll reaches its first sleep. (raw.githubusercontent.com)Copy the name while the flag is protected, or retain the flag through the logging call.
@coderabbitai[bot] on GitHub · 2026-11-04 18:16 UTC · original · reviewed
Actionable comments posted: 1
ff37a9c7-a3ba-46c1-bc93-d45c05c9ea04Reviewing files that changed from the base of the PR and between
91c5820c0canda41caf6053.src/common/emulatorConfig.cppsrc/common/emulatorConfig.hsrc/kernel/eventFlag.cppsrc/main.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@Nmzik on GitHub · 2026-11-08 08:24 UTC · original · edited 2026-11-08 08:25 UTC
@ArtyomZabroda Update:
Maybe you are actually right about the idea behind this fix.
Just so you know there is no real implementation of how this is supposed to work internally in any emulator out there including PS4. But there is some evidence suggesting that we should sleep after some time / immediately.
I did look at the FreeBSD OS source code and there are some hints about it but we don’t know how it actually works on the real PS5 (they modified syscalls). Even native KytyPS5 running on PS5 hardware doesn’t have this fix currently. Im planning to add it under a separate toggle to avoid any regressions (it affects any game)
And also while the general fix does decrease the number of polls it doesn’t improve FPS at all for me (100% usage for wise thread is wrong statement). But again there is no real implementation out there that shows how this is supposed to work internally except fpPS4 which supports your idea!
@ArtyomZabroda on GitHub · 2026-11-08 09:04 UTC · original
@Nmzik don't merge your fixes yet. I will drop a PR for ObtainBuffer optimization today and after that I will do a research on this thing again and maybe more info will show up. I will keep you updated.
@ArtyomZabroda on GitHub · 2026-11-08 09:42 UTC · original · edited 2026-11-08 09:44 UTC
@Nmzik The only thing that I don't yet understand is whether your processor actually suffers from problems that I'm facing to have fps improvements afterwards. After all your processor is way more performant (and has more cores) than mine. If it's not too much trouble could you provide more info about the fps and cpu usage across all cores during cp2077 gameplay.
@Nmzik on GitHub · 2026-11-22 13:05 UTC · original
@ArtyomZabroda latest main should fix problem with Wwise (taken from freebsd). Please retest
@ArtyomZabroda on GitHub · 2026-11-22 15:44 UTC · original · edited 2026-11-22 16:21 UTC
@Nmzik retested on fedora linux 45. haven't yet tested on windows.
Some random commit from a week ago:

Today's commit:

View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.