kernel: back off on repeated failed event-flag polls (cp2077) #2110

Open
github-bot wants to merge 1 commit from github-bot/gh-2110 into main AGit
Owner

@⁠ArtyomZabroda on GitHub · 2026-11-04 18:03 UTC · original
ArtyomZabroda:pr/event-flag-poll-backoff → main · +71 −0 · 4 files · 1 commits · open

A 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)
Снимок экрана 2026-11-04 161902
After (~50% cpu usage):
Снимок экрана 2026-11-04 175435

**@⁠ArtyomZabroda** on GitHub · 2026-11-04 18:03 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110) `ArtyomZabroda:pr/event-flag-poll-backoff` → `main` · +71 −0 · 4 files · 1 commits · open A 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) <img width="1276" height="749" alt="Снимок экрана 2026-11-04 161902" src="https://github.com/user-attachments/assets/a78c92e8-bc9a-4d8e-8c94-0c7e5ac8bbdf" /> After (~50% cpu usage): <img width="1279" height="747" alt="Снимок экрана 2026-11-04 175435" src="https://github.com/user-attachments/assets/e3ea2415-709b-444a-95f2-2234d34cf1bf" />
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:35 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 18:16 UTC · original

Explain Change →

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: nmzik

Change: Bug fix

Merge Risk: Moderate · up to a41ca

Concurrent 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: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning 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. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely identifies the main change: adding backoff for repeated failed event-flag polls. It matches the changeset.
Description check Passed The description directly explains the repeated polling problem, backoff behavior, reset conditions, logging, and the --no-poll-backoff option. It is fully related to the changeset.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
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
**@⁠coderabbitai[bot]** on GitHub · 2026-11-04 18:16 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110#issuecomment-6638440184) <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/2110?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> 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:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"a41caf6053e92c1c0bf3a1c554c26796aa46b5e0" --> <!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-normal-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Moderate</strong> · up to <code>a41ca</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🟡 Moderate** · up to `a41ca` --> <!-- final_review_risk_coverage:{"sourceCommitId":"a41caf6053e92c1c0bf3a1c554c26796aa46b5e0","coveredCommitId":"a41caf6053e92c1c0bf3a1c554c26796aa46b5e0","kind":"reviewed"} --> Concurrent deletion can crash the emulator during poll backoff. Keep the flag alive through logging, or capture its name while protected, before merging. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | 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. Documentat… | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely identifies the main change: adding backoff for repeated failed event-flag polls. It matches the changeset. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description directly explains the repeated polling problem, backoff behavior, reset conditions, logging, and the --no-poll-backoff option. It is fully related to the changeset. | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **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. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=2110)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <!-- tips_end -->
@ -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());
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 18:16 UTC · original

Major · Stability & Availability · Quick win

Keep the flag alive while logging its name.

If another thread deletes the flag after Wait releases 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](https://github.com/KytyPS5/KytyPS5/pull/2110#discussion_r4471472817) <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/major-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Major** · **Stability & Availability** · **Quick win** **Keep the flag alive while logging its name.** If another thread deletes the flag after `Wait` releases 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](https://raw.githubusercontent.com/KytyPS5/KytyPS5/a41caf6053e92c1c0bf3a1c554c26796aa46b5e0/src/common/logging/log.h)) Copy the name while the flag is protected, or retain the flag through the logging call. <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:61100c74eb0ceaef5df2a899 --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 18:16 UTC · original · reviewed

Actionable comments posted: 1

Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 91c5820c0c and a41caf6053.

Files selected for processing: 4
  • src/common/emulatorConfig.cpp
  • src/common/emulatorConfig.h
  • src/kernel/eventFlag.cpp
  • src/main.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-04 18:16 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110#pullrequestreview-5773462382) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** <h6> <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Review info</summary> <dl> <dd> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/91c5820c0cecb8ca024d691ca8750be811370591/.coderabbit.yaml>) - **Commit**: [91c5820](<https://github.com/KytyPS5/KytyPS5/commit/91c5820c0cecb8ca024d691ca8750be811370591>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `ff37a9c7-a3ba-46c1-bc93-d45c05c9ea04` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary> <dl> <dd> Reviewing files that changed from the base of the PR and between 91c5820c0cecb8ca024d691ca8750be811370591 and a41caf6053e92c1c0bf3a1c554c26796aa46b5e0. </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 4</summary> <dl> <dd> * `src/common/emulatorConfig.cpp` * `src/common/emulatorConfig.h` * `src/kernel/eventFlag.cpp` * `src/main.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. </dd> </dl> </details> </h6> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=184dabbb-b299-4b56-a8ca-70ef7a17e26a attempt=c8711c74-2a8b-426e-924a-a5511aa6c42e batch=1/1 -->
Author
Owner

@⁠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!

**@⁠Nmzik** on GitHub · 2026-11-08 08:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110#issuecomment-6719233557) · 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!
Author
Owner

@⁠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:04 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110#issuecomment-6719593001) @⁠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.
Author
Owner

@⁠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.

**@⁠ArtyomZabroda** on GitHub · 2026-11-08 09:42 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110#issuecomment-6719944552) · 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.
Author
Owner

@⁠Nmzik on GitHub · 2026-11-22 13:05 UTC · original

@⁠ArtyomZabroda latest main should fix problem with Wwise (taken from freebsd). Please retest

**@⁠Nmzik** on GitHub · 2026-11-22 13:05 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110#issuecomment-7079556802) @⁠ArtyomZabroda latest main should fix problem with Wwise (taken from freebsd). Please retest
Author
Owner

@⁠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:
Снимок экрана_20261122_183612

Today's commit:
Снимок экрана_20261122_184236

**@⁠ArtyomZabroda** on GitHub · 2026-11-22 15:44 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2110#issuecomment-7081532366) · 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: <img width="2560" height="1393" alt="Снимок экрана_20261122_183612" src="https://github.com/user-attachments/assets/127b1cd6-6856-4181-b667-0fa39bd93d85" /> Today's commit: <img width="1281" height="750" alt="Снимок экрана_20261122_184236" src="https://github.com/user-attachments/assets/51c23d26-55ee-47da-bf6d-422029085c61" />
This pull request has changes conflicting with the target branch.
  • src/common/emulatorConfig.cpp
  • src/common/emulatorConfig.h
  • src/kernel/eventFlag.cpp
  • src/main.cpp
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.
git fetch -u origin '+refs/pull/2110/head:github-bot/gh-2110'
git switch 'github-bot/gh-2110'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2110'
git switch 'github-bot/gh-2110'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2110'
git switch 'github-bot/gh-2110'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2110'
git switch 'main'
git merge --squash 'github-bot/gh-2110'
git switch 'main'
git merge --ff-only 'github-bot/gh-2110'
git switch 'main'
git merge 'github-bot/gh-2110'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.