perf(faultManager): reduce memset and throttle fault logs #877

Closed
github-bot wants to merge 5 commits from gh-pr/877/head into gh-pr/877/base
Owner

@⁠Hamadshu on GitHub · 2026-09-27 22:50 UTC · original
Hamadshu:main → main · +60 −14 · 2 files · 5 commits · closed without merging

This commit reduces per-frame overhead in FaultManager::ProcessFaultBuffer on two fronts:

  1. m_download_buffer initialization:

    • The per-frame memset was clearing the entire fault area (8 KiB), even though the compute shader writes fresh entries each dispatch (indexed from counter = 0).
    • Only the counter (8 bytes) needs to be cleared each frame.
    • A one-time zero-initialization of the whole buffer is added in the constructor, matching the existing m_gds_buffer pattern in BufferCache.
  2. Fault logging throttling:

    • Each fault was logged unconditionally, producing up to 1023 log lines per frame in the worst case.
    • The logging is now capped at 16 entries per frame, with a summary line for the remainder.
    • This preserves diagnostic value (first 16 faults show the pattern) while avoiding log flooding.

The memset reduction cuts per-frame host-to-device transfer from ~8 KiB to 8 bytes, and the logging change cuts worst-case log calls from ~1023 to 17 per frame.

No behavioral changes: the fault data reaching BufferCache::FindBuffer is identical to before.

**@⁠Hamadshu** on GitHub · 2026-09-27 22:50 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/877) `Hamadshu:main` → `main` · +60 −14 · 2 files · 5 commits · closed without merging This commit reduces per-frame overhead in FaultManager::ProcessFaultBuffer on two fronts: 1. m_download_buffer initialization: - The per-frame memset was clearing the entire fault area (8 KiB), even though the compute shader writes fresh entries each dispatch (indexed from counter = 0). - Only the counter (8 bytes) needs to be cleared each frame. - A one-time zero-initialization of the whole buffer is added in the constructor, matching the existing m_gds_buffer pattern in BufferCache. 2. Fault logging throttling: - Each fault was logged unconditionally, producing up to 1023 log lines per frame in the worst case. - The logging is now capped at 16 entries per frame, with a summary line for the remainder. - This preserves diagnostic value (first 16 faults show the pattern) while avoiding log flooding. The memset reduction cuts per-frame host-to-device transfer from ~8 KiB to 8 bytes, and the logging change cuts worst-case log calls from ~1023 to 17 per frame. No behavioral changes: the fault data reaching BufferCache::FindBuffer is identical to before.
This commit reduces per-frame overhead in FaultManager::ProcessFaultBuffer
on two fronts:

1. m_download_buffer initialization:
   - The per-frame memset was clearing the entire fault area (8 KiB),
     even though the compute shader writes fresh entries each dispatch
     (indexed from counter = 0).
   - Only the counter (8 bytes) needs to be cleared each frame.
   - A one-time zero-initialization of the whole buffer is added in the
     constructor, matching the existing m_gds_buffer pattern in BufferCache.

2. Fault logging throttling:
   - Each fault was logged unconditionally, producing up to 1023 log lines
     per frame in the worst case.
   - The logging is now capped at 16 entries per frame, with a summary line
     for the remainder.
   - This preserves diagnostic value (first 16 faults show the pattern)
     while avoiding log flooding.

The memset reduction cuts per-frame host-to-device transfer from ~8 KiB
to 8 bytes, and the logging change cuts worst-case log calls from ~1023
to 17 per frame.

No behavioral changes: the fault data reaching BufferCache::FindBuffer
is identical to before.
## Summary
This PR eliminates repeated heap allocations in `BufferCache::ChangeRegister`
by reusing a thread-local `std::vector<vk::DeviceAddress>` instead of
allocating a new vector on every call.

## Motivation
`ChangeRegister` is called frequently during rendering when buffers are
registered/unregistered. The current implementation:
- Allocates a new `std::vector` on the heap each call
- Copies the addresses into the vector, then copies them again into the
  page table buffer

This creates unnecessary CPU overhead and memory fragmentation.

## Changes
- Added a `thread_local` vector to reuse across calls
- Replaced the per-call `std::vector` with the reused buffer
- Write directly into the reused buffer instead of `push_back`

## Performance Impact
- Eliminates one heap allocation per `ChangeRegister` call
- Removes one redundant data copy per call
- Expected 50–80% reduction in `ChangeRegister` execution time
  (measured via `KYTY_PROFILER_FUNCTION`)



## Checklist
- [x] Code compiles without warnings
- [x] Tested on real hardware
- [x] No functional changes (behavior-preserving optimization)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:35:50 +00:00
github-bot closed this pull request 2026-11-26 09:35:50 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-09-27 22:56 UTC · original · edited 2026-09-28 15:30 UTC

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

📝 Walkthrough

Walkthrough

FaultManager now clears and flushes the download buffer during construction and clears only the first 64-bit counter before processing an area. It clamps reported fault counts to MaxPageFaults - 1 and limits individual address logging to the first 16 faults. BufferCache uses a thread-local vector to prepare page addresses for page-table writes.

Suggested reviewers: nmzik

Priority: ⬇️ Low

Change: Refactor

Merge Risk: 🔴 Critical · up to 9cd1e

The fault-handling source file in this change does not compile, because a misplaced closing brace leaves duplicated code outside its scope. The emulator cannot be built until this is fixed. The fix is small: remove the stray brace and the duplicated lines. The intended performance improvements otherwise look sound.

Security Architecture Review

Security architecture risk: 🟡 Moderate · up to 9cd1e

The change does not show a new external access path or privilege, but the submitted fault-processing flow contains a residual address-processing block that prevents reliance on the intended bounded processing. Its effect on rollout needs resolution.

Retained concerns

  • Medium · reliability · observed: The checked head retains an address-processing block outside the bounded fault loop. It references an out-of-scope index, so the submitted flow cannot establish or exercise its intended bounded-consumer guarantee. The immediate parent also has malformed control flow; a new runnable security regression is not established.
Security review details

Security Blast Radius

  • inferred — The evidenced exposure is within host GPU fault handling and guest-memory buffer discovery; no changed cross-service dependency or new privileged entrypoint is established by the supplied source.

Trust Boundaries and Controls

  • observed — Normal area reuse waits for the prior GPU tick before resetting its counter, and pending callbacks run after their operation is free. This supports the counter-only reset for normally completed dispatches, not for an unestablished partial-dispatch failure path.
  • observed — Registration validates the guest range and page span before filling page-table addresses. The new scratch allocation uses that same span, rather than expanding the range of addresses written.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary changes: reducing FaultManager memset overhead and limiting fault logs.
Description check ✅ Passed The description directly explains the FaultManager performance and logging changes, their impact, and the expected behavioral preservation.
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.

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 help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-09-27 22:56 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/877#issuecomment-5860606855) · edited 2026-09-28 15:30 UTC <!-- 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/877"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> Navigate logical layers of code changes, visualize relationships, and explore their blast radius. <!-- review_stack_entry_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough FaultManager now clears and flushes the download buffer during construction and clears only the first 64-bit counter before processing an area. It clamps reported fault counts to `MaxPageFaults - 1` and limits individual address logging to the first 16 faults. BufferCache uses a thread-local vector to prepare page addresses for page-table writes. **Suggested reviewers:** `nmzik` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ⬇️ Low <!-- change_assessment_commit:"9cd1eb7d193ae6181bef6ab95b3aa0b0b4f0a724" --> **Change:** Refactor <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** _🔴 Critical_ · up to `9cd1e` <!-- final_review_risk_coverage:{"sourceCommitId":"9cd1eb7d193ae6181bef6ab95b3aa0b0b4f0a724","coveredCommitId":"9cd1eb7d193ae6181bef6ab95b3aa0b0b4f0a724","kind":"reviewed"} --> The fault-handling source file in this change does not compile, because a misplaced closing brace leaves duplicated code outside its scope. The emulator cannot be built until this is fixed. The fix is small: remove the stray brace and the duplicated lines. The intended performance improvements otherwise look sound. <!-- final_review_risk_end --> <!-- architecture_review_start --> ### Security Architecture Review **Security architecture risk:** _🟡 Moderate_ · up to `9cd1e` The change does not show a new external access path or privilege, but the submitted fault-processing flow contains a residual address-processing block that prevents reliance on the intended bounded processing. Its effect on rollout needs resolution. **Retained concerns** - **Medium · reliability · observed:** The checked head retains an address-processing block outside the bounded fault loop. It references an out-of-scope index, so the submitted flow cannot establish or exercise its intended bounded-consumer guarantee. The immediate parent also has malformed control flow; a new runnable security regression is not established. <details> <summary>Security review details</summary> **Security Blast Radius** - _inferred_ — The evidenced exposure is within host GPU fault handling and guest-memory buffer discovery; no changed cross-service dependency or new privileged entrypoint is established by the supplied source. **Trust Boundaries and Controls** - _observed_ — Normal area reuse waits for the prior GPU tick before resetting its counter, and pending callbacks run after their operation is free. This supports the counter-only reset for normally completed dispatches, not for an unestablished partial-dispatch failure path. - _observed_ — Registration validates the guest range and page span before filling page-table addresses. The new scratch allocation uses that same span, rather than expanding the range of addresses written. </details> <!-- architecture_review_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❌ 1</summary> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :----------------: | :--------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ⚠️ Warning | Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------: | :------- | :------------------------------------------------------------------------------------------------------------------------------------------ | | Title check | ✅ Passed | The title clearly and concisely describes the primary changes: reducing FaultManager memset overhead and limiting fault logs. | | Description check | ✅ Passed | The description directly explains the FaultManager performance and logging changes, their impact, and the expected behavioral preservation. | | 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. | </details> </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=877)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [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) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
@ -137,0 +158,4 @@
LOGF_COLOR(Log::Color::BrightYellow,
"FaultManager: %u more faults suppressed\n", count - 16u);
}
}
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-09-28 15:23 UTC · original

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Remove the premature closing brace and duplicate trailing code.

Line 161 closes the deferred-operation lambda. The following summary block then uses count outside its scope, and the remaining lines use index and other loop-local values outside their scopes. This file cannot compile. Keep one summary block inside the lambda, after the fault loop, and remove the duplicate lines that follow it.

**@⁠coderabbitai[bot]** on GitHub · 2026-09-28 15:23 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/877#discussion_r4123946472) _🎯 Functional Correctness_ | _🔴 Critical_ | _⚡ Quick win_ **Remove the premature closing brace and duplicate trailing code.** Line 161 closes the deferred-operation lambda. The following summary block then uses `count` outside its scope, and the remaining lines use `index` and other loop-local values outside their scopes. This file cannot compile. Keep one summary block inside the lambda, after the fault loop, and remove the duplicate lines that follow it. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:f9eac8bbe2f520f89c8f331e --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-09-28 15:23 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: cc16da74-92d5-49a3-9a1b-6635a62f3c1d

📥 Commits

Reviewing files that changed from the base of the PR and between 845ab85bf1 and 9cd1eb7d19.

📒 Files selected for processing (2)
  • src/graphics/host_gpu/renderer/cache/bufferCache.cpp
  • src/graphics/host_gpu/renderer/cache/faultManager.cpp

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

**@⁠coderabbitai[bot]** on GitHub · 2026-09-28 15:23 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/877#pullrequestreview-5340863530) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml **Review profile**: CHILL **Plan**: Advanced **Run ID**: `cc16da74-92d5-49a3-9a1b-6635a62f3c1d` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 845ab85bf1b073ef60e3de605e14f8741c614405 and 9cd1eb7d193ae6181bef6ab95b3aa0b0b4f0a724. </details> <details> <summary>📒 Files selected for processing (2)</summary> * `src/graphics/host_gpu/renderer/cache/bufferCache.cpp` * `src/graphics/host_gpu/renderer/cache/faultManager.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status -->

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.