fix(agc): keep the pipeline journal's newest pipelines when it is full #9836

Merged
00xDisaster merged 1 commit from upstream-pr/9836 into main 2026-10-24 12:13:51 +00:00
00xDisaster commented 2026-10-22 16:53:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 00xDisaster on 2026-10-22T16:53:52Z.


What

The pipeline journal could lose the pipelines a title needs most, in two ways.

  • The replay kept the first APS5_PIPELINE_JOURNAL_ENTRIES entries of the file, so a title with more pipelines than the bound never replayed the ones it built last: the pipelines of the level the save was in were compiled again at every level entry, under the GPU mutex.
  • Once the file reached APS5_PIPELINE_JOURNAL_MAX_MIB the writer refused every later record, so a level that loaded after the cap was reached was never recorded at all and paid its builds on every boot (#8309).

This change:

  • The replay keeps the last entries: the pipelines a title built last are the ones its current level needs.
  • The writer rewrites the file when it reaches its size cap. The newest pipelines and the modules they name are kept, the older records are dropped, and the file is replaced atomically. The rewrite targets three quarters of the cap, leaving room for the next records instead of rewriting again after every new pipeline. A pipeline whose module the file does not hold is dropped as well, since the replay would reject it. A record that is larger than the cap is not recorded and the file is left as it is.
  • The rewrite scans record headers and streams payload hashes, then copies the kept spans with positioned reads and chunked writes into the temporary file. It no longer holds the file plus a second copy in the writer thread.

The rewrite happens in the journal's writer thread, under the file lock it already holds, and the counters report what it dropped:

[pipeline-journal] <path> reached its size cap; 37 records (0.9 MiB) were dropped and the file was rewritten to 0.7 MiB

APS5_PIPELINE_JOURNAL_MAX_MIB and APS5_PIPELINE_JOURNAL_ENTRIES keep their meaning (the size cap and the number of entries replayed); the debt entry and ENVIRONMENT.md describe the new behaviour.

The branch is rebased on main (a701f9f788) and changes no .github/workflows file. It has no required dependencies: #8213 and #8365 are not in this branch and are not listed under Depends on. Intended merge order is this PR first; if either of those lands first, this PR will rebase. #8542's live head 98efc1f040 still keeps the first entries per kind; @milescoviello and I agreed in the #8542 thread that the changes compose and that #8542 will rebase onto this PR, applying its per-kind graphics/compute bounds to the newest entries this PR keeps (agreement, confirmation); the confirmation was on the closed #9440, which carries the same journal change as this PR. After rebasing onto this PR, #8542's per-kind selection should count from the end of the newest-entry list (keep the last N of each kind); no code change is needed in this PR.

Tested

Linux (CachyOS, kernel 7.3.0-2-cachyos), GCC 16.2.1, NVIDIA GeForce GTX 1660 SUPER (driver 615.78.08, Vulkan 1.4.351), base upstream/main a701f9f788e18f083c353691634f9aa1a04afa76; final head 62827a5ca0a6c2520e524563b8e3761212e629ce. AGC runs use ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation.

  • agc_driver_pipeline_journal gains five cases:

    • the replay of a journal with five entries and APS5_PIPELINE_JOURNAL_ENTRIES=2 builds the two newest entries and not the graphics entry of the first level; the newest program is then found in the store at its dispatch and the oldest is built there. With main's PipelineJournal.cpp and this PR's test kept, it fails: AGC graphics: the replay did not build the two newest entries: built 1 compute and 1 graphics, 0 rejected, 0 present, 0 skipped. With this change the binary prints pipeline journal record and replay passed.
    • a journal that reaches the cap is rewritten: the file stays under the cap, the newest pipeline survives the rewrite and the oldest does not. With main's writer the file keeps growing and the record is refused (the journal was not rewritten when it reached its cap).
    • after that rewrite, three consecutive compute records fit under the cap without another rewrite; the test checks that all three were recorded, no write failed and evictedRecords did not move. With main's writer it fails: AGC graphics: three consecutive records after a rewrite triggered another rewrite.
    • the record that triggers the rewrite keeps the modules it shares with the records the rewrite drops: the journal is filled past its cap with records no pipeline needs, a second draw with the same shaders and a different blend state writes a record that shares the older record's modules, and the replay after the rewrite builds it. Without the fix the run reports the record written after the eviction was not replayed: 0 graphics built, 1 rejected and the load line reads loaded 1 pipelines and 0 modules.
    • a record that cannot fit under the cap leaves the journal alone: a compute program whose result carries 1.6 MB of user words is recorded into a journal that already holds a pipeline. Without the fix the journal shrinks to its 32-byte header (the oversized record emptied the journal: 39888 bytes became 32) and the record is lost; with the fix the file keeps its size, the skipped record is counted as a write failure and the pipeline that was in the journal still replays.
  • Peak RSS at the default 1 GiB cap, measured with a temporary benchmark (not committed; the writer code is unchanged by the rebase) that filled a journal to 1,025 MiB (1,074,817,224 bytes) and then recorded one more compute pipeline. Both runs loaded the same journal, so the difference is the eviction copy:

    Writer Peak RSS (ru_maxrss) Eviction Journal after
    old (file plus second copy) 2,267,164 KiB (2.16 GiB) 36 records, 1.1 MiB 1,073,701,480 bytes (1,024.0 MiB)
    new (scan, then positioned copy) 1,221,052 KiB (1.16 GiB) 8,216 records, 257.1 MiB 805,266,600 bytes (767.9 MiB)

    That is 1,046,112 KiB (about 1.00 GiB) less peak RSS. The old writer stopped 39 KiB under the cap, so the next record rewrote again; the new writer leaves 256.0 MiB of headroom at the 75% watermark.

  • ctest --test-dir build -R '^agc_driver_(pipeline_journal|graphics)$' --output-on-failure on the final head: 2/2 passed in 16.28 s.

  • On the final head 62827a5ca0 (base a701f9f788), agc_driver_pipeline_journal passes (pipeline journal record and replay passed) and agc_driver_graphics passes (Graphics validation tests passed; it holds the journal parse tests).

  • Khronos validation layer on 62827a5ca0: the final-head logs contain no was not found but was requested by env var VK_INSTANCE_LAYERS line and no VUID- line.

  • tools/check_conventions.py --base upstream/main passes; git diff --check passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #8309
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce GTX 1660 SUPER, driver 615.78.08, CachyOS 7.3.0-2 (AGC, pipeline journal)
  • AI-assisted: no
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4971015565 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9836). Originally posted by [00xDisaster](https://github.com/00xDisaster) on 2026-10-22T16:53:52Z. --- ### What The pipeline journal could lose the pipelines a title needs most, in two ways. - The replay kept the **first** `APS5_PIPELINE_JOURNAL_ENTRIES` entries of the file, so a title with more pipelines than the bound never replayed the ones it built last: the pipelines of the level the save was in were compiled again at every level entry, under the GPU mutex. - Once the file reached `APS5_PIPELINE_JOURNAL_MAX_MIB` the writer refused every later record, so a level that loaded after the cap was reached was never recorded at all and paid its builds on every boot (#8309). This change: - The replay keeps the **last** entries: the pipelines a title built last are the ones its current level needs. - The writer **rewrites the file** when it reaches its size cap. The newest pipelines and the modules they name are kept, the older records are dropped, and the file is replaced atomically. The rewrite targets **three quarters of the cap**, leaving room for the next records instead of rewriting again after every new pipeline. A pipeline whose module the file does not hold is dropped as well, since the replay would reject it. A record that is larger than the cap is not recorded and the file is left as it is. - The rewrite scans record headers and streams payload hashes, then copies the kept spans with positioned reads and chunked writes into the temporary file. It no longer holds the file plus a second copy in the writer thread. The rewrite happens in the journal's writer thread, under the file lock it already holds, and the counters report what it dropped: ``` [pipeline-journal] <path> reached its size cap; 37 records (0.9 MiB) were dropped and the file was rewritten to 0.7 MiB ``` `APS5_PIPELINE_JOURNAL_MAX_MIB` and `APS5_PIPELINE_JOURNAL_ENTRIES` keep their meaning (the size cap and the number of entries replayed); the debt entry and `ENVIRONMENT.md` describe the new behaviour. The branch is rebased on `main` (`a701f9f788`) and changes no `.github/workflows` file. It has no required dependencies: #8213 and #8365 are not in this branch and are not listed under `Depends on`. Intended merge order is this PR first; if either of those lands first, this PR will rebase. #8542's live head `98efc1f040` still keeps the first entries per kind; @milescoviello and I agreed in the #8542 thread that the changes compose and that #8542 will rebase onto this PR, applying its per-kind graphics/compute bounds to the newest entries this PR keeps ([agreement](https://github.com/boykopovar/AnyPS5/pull/8542#issuecomment-6329379381), [confirmation](https://github.com/boykopovar/AnyPS5/pull/8542#issuecomment-6329629780)); the confirmation was on the closed #9440, which carries the same journal change as this PR. After rebasing onto this PR, #8542's per-kind selection should count from the end of the newest-entry list (keep the last N of each kind); no code change is needed in this PR. ### Tested Linux (CachyOS, kernel 7.3.0-2-cachyos), GCC 16.2.1, NVIDIA GeForce GTX 1660 SUPER (driver 615.78.08, Vulkan 1.4.351), base `upstream/main` `a701f9f788e18f083c353691634f9aa1a04afa76`; final head `62827a5ca0a6c2520e524563b8e3761212e629ce`. AGC runs use `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. - `agc_driver_pipeline_journal` gains five cases: - the replay of a journal with five entries and `APS5_PIPELINE_JOURNAL_ENTRIES=2` builds the two newest entries and not the graphics entry of the first level; the newest program is then found in the store at its dispatch and the oldest is built there. With `main`'s `PipelineJournal.cpp` and this PR's test kept, it fails: `AGC graphics: the replay did not build the two newest entries: built 1 compute and 1 graphics, 0 rejected, 0 present, 0 skipped`. With this change the binary prints `pipeline journal record and replay passed`. - a journal that reaches the cap is rewritten: the file stays under the cap, the newest pipeline survives the rewrite and the oldest does not. With `main`'s writer the file keeps growing and the record is refused (`the journal was not rewritten when it reached its cap`). - after that rewrite, three consecutive compute records fit under the cap without another rewrite; the test checks that all three were recorded, no write failed and `evictedRecords` did not move. With `main`'s writer it fails: `AGC graphics: three consecutive records after a rewrite triggered another rewrite`. - the record that triggers the rewrite keeps the modules it shares with the records the rewrite drops: the journal is filled past its cap with records no pipeline needs, a second draw with the same shaders and a different blend state writes a record that shares the older record's modules, and the replay after the rewrite builds it. Without the fix the run reports `the record written after the eviction was not replayed: 0 graphics built, 1 rejected` and the load line reads `loaded 1 pipelines and 0 modules`. - a record that cannot fit under the cap leaves the journal alone: a compute program whose result carries 1.6 MB of user words is recorded into a journal that already holds a pipeline. Without the fix the journal shrinks to its 32-byte header (`the oversized record emptied the journal: 39888 bytes became 32`) and the record is lost; with the fix the file keeps its size, the skipped record is counted as a write failure and the pipeline that was in the journal still replays. - Peak RSS at the default 1 GiB cap, measured with a temporary benchmark (not committed; the writer code is unchanged by the rebase) that filled a journal to 1,025 MiB (`1,074,817,224` bytes) and then recorded one more compute pipeline. Both runs loaded the same journal, so the difference is the eviction copy: | Writer | Peak RSS (`ru_maxrss`) | Eviction | Journal after | |---|---:|---|---:| | old (file plus second copy) | `2,267,164 KiB` (2.16 GiB) | 36 records, 1.1 MiB | `1,073,701,480` bytes (1,024.0 MiB) | | new (scan, then positioned copy) | `1,221,052 KiB` (1.16 GiB) | 8,216 records, 257.1 MiB | `805,266,600` bytes (767.9 MiB) | That is `1,046,112 KiB` (about 1.00 GiB) less peak RSS. The old writer stopped 39 KiB under the cap, so the next record rewrote again; the new writer leaves 256.0 MiB of headroom at the 75% watermark. - `ctest --test-dir build -R '^agc_driver_(pipeline_journal|graphics)$' --output-on-failure` on the final head: 2/2 passed in 16.28 s. - On the final head `62827a5ca0` (base `a701f9f788`), `agc_driver_pipeline_journal` passes (`pipeline journal record and replay passed`) and `agc_driver_graphics` passes (`Graphics validation tests passed`; it holds the journal parse tests). - Khronos validation layer on `62827a5ca0`: the final-head logs contain no `was not found but was requested by env var VK_INSTANCE_LAYERS` line and no `VUID-` line. - `tools/check_conventions.py --base upstream/main` passes; `git diff --check` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #8309 - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce GTX 1660 SUPER, driver 615.78.08, CachyOS 7.3.0-2 (AGC, pipeline journal) - [x] AI-assisted: no
MarcZX10 commented 2026-10-22 17:15:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T17:15:47Z.

Review: DISMISSED


The current diff removes every workflow under .github/workflows/, including the build/test, conventions, PR intake/overlap, labels and release workflows. Those deletions are unrelated to the pipeline-journal change and are not described in the PR body; merging them would remove the repository’s CI and contribution gates. Please restore the workflow files and keep this change scoped to the journal. The current conventions/intake/label checks pass, but the hosted build/test jobs are not present in the current check run, so those checks do not validate this deletion.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618085734 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9836#pullrequestreview-5618085734). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T17:15:47Z. **Review:** DISMISSED --- The current diff removes every workflow under `.github/workflows/`, including the build/test, conventions, PR intake/overlap, labels and release workflows. Those deletions are unrelated to the pipeline-journal change and are not described in the PR body; merging them would remove the repository’s CI and contribution gates. Please restore the workflow files and keep this change scoped to the journal. The current conventions/intake/label checks pass, but the hosted build/test jobs are not present in the current check run, so those checks do not validate this deletion.
MarcZX10 commented 2026-10-22 17:54:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T17:54:54Z.

Review: DISMISSED


The committed head removes 17 repository workflows, including .github/workflows/build.yml, pr-intake.yml, conventions.yml, release, labels, overlap, and Windows test workflows (706 lines from build.yml alone). These deletions are in the PR file diff but are not described in the summary or checklist and are unrelated to pipeline-journal behavior; they would disable repository automation if merged. Please restore those workflow files and rebase onto current main before this change is considered. The branch parent is 8499ea52; current base/main has moved past it, and the PR currently reports the workflow removals. I have not run the AGC tests locally; the host's CPU/Vulkan limitations prevent a reliable runtime check. Existing PR checks are conventions/intake/labels; product jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618570728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9836#pullrequestreview-5618570728). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T17:54:54Z. **Review:** DISMISSED --- The committed head removes 17 repository workflows, including `.github/workflows/build.yml`, `pr-intake.yml`, `conventions.yml`, release, labels, overlap, and Windows test workflows (706 lines from `build.yml` alone). These deletions are in the PR file diff but are not described in the summary or checklist and are unrelated to pipeline-journal behavior; they would disable repository automation if merged. Please restore those workflow files and rebase onto current `main` before this change is considered. The branch parent is `8499ea52`; current base/main has moved past it, and the PR currently reports the workflow removals. I have not run the AGC tests locally; the host's CPU/Vulkan limitations prevent a reliable runtime check. Existing PR checks are conventions/intake/labels; product jobs are skipped.
adriadam10 commented 2026-10-22 19:07:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-22T19:07:36Z.

Review: CHANGES_REQUESTED


Thanks, the workflows are restored and the diff is scoped to the journal now. The four new tests fail without the change and pin what they say. Remaining points:

  • PipelineJournal.cpp:802 (evictUnderLock): the rewrite keeps records up to maxBytes - reserve, where reserve is only the pending record's size. The file is left almost exactly at the cap, so the next record of a different size is over the cap again and rewrites the whole file (up to 1 GiB) for a few records dropped. Under a level load that is a full rewrite per pipeline, under the file lock. Rewrite down to a lower watermark (e.g. keep the newest 3/4 of the cap) so rewrites are rare, and add a test that several consecutive records after a rewrite cause one rewrite, not one each.
  • PipelineJournal.cpp:792-839: the rewrite reads the whole file into memory and builds a second copy, so peak memory is up to about 2x the cap in the writer thread (docs/rules/memory.md). Copy the kept spans with positioned reads/streamed writes into the temporary file, and give the peak RSS before/after on a journal at the default cap.
  • Depends on / overlap: the body lists #8213 and #8365 as dependencies, but the branch is based on main and changes the same parse they change, so whichever lands first conflicts with this one. Either stack the branch on them or drop them from Depends on and say which merges first. #8542 changes the same replay truncation (PipelineJournal.cpp:1093) to keep the first entries per kind, while this PR keeps the last ones; those two need one agreed behaviour (this PR's, if the newest pipelines are what a level needs, then #8542's split bound has to follow it).

Happy to re-review once these are addressed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5619484429 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9836#pullrequestreview-5619484429). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-22T19:07:36Z. **Review:** CHANGES_REQUESTED --- Thanks, the workflows are restored and the diff is scoped to the journal now. The four new tests fail without the change and pin what they say. Remaining points: - `PipelineJournal.cpp:802` (`evictUnderLock`): the rewrite keeps records up to `maxBytes - reserve`, where `reserve` is only the pending record's size. The file is left almost exactly at the cap, so the next record of a different size is over the cap again and rewrites the whole file (up to 1 GiB) for a few records dropped. Under a level load that is a full rewrite per pipeline, under the file lock. Rewrite down to a lower watermark (e.g. keep the newest 3/4 of the cap) so rewrites are rare, and add a test that several consecutive records after a rewrite cause one rewrite, not one each. - `PipelineJournal.cpp:792-839`: the rewrite reads the whole file into memory and builds a second copy, so peak memory is up to about 2x the cap in the writer thread (docs/rules/memory.md). Copy the kept spans with positioned reads/streamed writes into the temporary file, and give the peak RSS before/after on a journal at the default cap. - Depends on / overlap: the body lists #8213 and #8365 as dependencies, but the branch is based on `main` and changes the same parse they change, so whichever lands first conflicts with this one. Either stack the branch on them or drop them from Depends on and say which merges first. #8542 changes the same replay truncation (`PipelineJournal.cpp:1093`) to keep the first entries per kind, while this PR keeps the last ones; those two need one agreed behaviour (this PR's, if the newest pipelines are what a level needs, then #8542's split bound has to follow it). Happy to re-review once these are addressed.
MarcZX10 commented 2026-10-22 20:28:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T20:28:45Z.

Review: COMMENTED


I re-reviewed the updated head 63bb981a71bea44a63773ba87d6b661972600feb and the current diff. The prior blockers are addressed in the diff: the workflow deletions are gone, the writer now streams kept spans and rewrites to a 75% watermark, the pending batch is retried against the rewritten index, and the branch documents the overlap/merge order. The added tests cover newest-entry replay, watermark behavior, module retention for the triggering record, and oversized records. The reported Vulkan validation and RSS runs are author-provided; I did not run those locally. The current hosted checks I can see are conventions and labels only, so I am not treating them as product build/test results. I found no new actionable issue in this pass. Please keep the interaction with #8542 coordinated because its current per-kind/first-entry policy conflicts with this PR’s global newest-entry policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5620434835 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9836#pullrequestreview-5620434835). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T20:28:45Z. **Review:** COMMENTED --- I re-reviewed the updated head `63bb981a71bea44a63773ba87d6b661972600feb` and the current diff. The prior blockers are addressed in the diff: the workflow deletions are gone, the writer now streams kept spans and rewrites to a 75% watermark, the pending batch is retried against the rewritten index, and the branch documents the overlap/merge order. The added tests cover newest-entry replay, watermark behavior, module retention for the triggering record, and oversized records. The reported Vulkan validation and RSS runs are author-provided; I did not run those locally. The current hosted checks I can see are conventions and labels only, so I am not treating them as product build/test results. I found no new actionable issue in this pass. Please keep the interaction with #8542 coordinated because its current per-kind/first-entry policy conflicts with this PR’s global newest-entry policy.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9836
No description provided.