feat(agc): capture and replay frames on Linux #11157

Merged
janzezno merged 1 commit from upstream-pr/11157 into main 2026-10-27 08:20:56 +00:00
janzezno commented 2026-10-25 20:01:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by janzezno on 2026-10-25T20:01:18Z.


What

APS5_FRAME_CAPTURE and agc_frame_replay only work on Windows. On Linux a capture throws at its first frame boundary with frame capture: guest memory capture is implemented on Windows only, so a title that misrenders or hangs on Linux can't be captured and replayed offline. Everything else the recorder uses already runs on Linux (the write watch, ReadCommitted, the guest arena and the allocation registry). The missing parts were the VirtualQuery walk in FrameRecorder::committedRanges and the VirtualAlloc calls in the replay tool. This change adds Linux versions of those and runs the capture tests on Linux.

Recorder (committedRanges, Linux branch). It walks each readable registry allocation's VMAs with a new HostMapping::Walk (PROCMAP_QUERY when the kernel has it, else one /proc/self/maps pass). HostMapping::Mapping gains the r/w/x/shared bits and the inode, filled on both paths. Classification follows the Windows branch:

  • An allocation outside the arena is Host.
  • A PROT_NONE VMA is the Linux form of reserved and uncommitted memory (the arena reserves and resets with PROT_NONE), so it is left out.
  • Inside the arena, a range the write watch covers is Watched, other private anonymous memory is Unwatched, and the rest (shared and file-backed) is Section. One difference from Windows: on Linux libkernel registers direct memory views with the write watch (AddMapping → WatchMapping). Those views are watched, so CPU stores to direct memory during the frames are recorded, which Windows section views can't do.
  • protection stores the Windows PAGE_* value of the VMA bits. That is the protection vocabulary the arena API already uses on Linux, so the field means the same thing in captures from either host.
  • A file mapping can extend past the end of its file, and reading those pages raises SIGBUS. ccleste's bundled Mesa maps its radv-shader-cache/.../index file that way, and the first Linux capture died in ReadCommitted. /proc/self/map_files needs CAP_SYS_ADMIN, so the file size can't be read from there. Instead the recorder probes pages with process_vm_readv on its own process (a page past the end gives EFAULT, not a signal), binary-searches the boundary and records the pages past it as an unreadable range. Any other probe error (for example EPERM under a seccomp or ptrace policy) fails the capture instead of dropping memory.
  • A capture now throws when it starts if the guest write watch is unavailable (APS5_NO_WRITE_WATCH, or a host without one), on both platforms. Without the watch a capture used to start and silently miss every CPU store during the frames.

Replay tool.

  • committedWritable: Linux version through HostMapping::Walk.
  • mapHostRanges: maps page-aligned envelopes with MAP_FIXED_NOREPLACE. Envelopes merge only where two ranges share a page, so an occupied CPU range doesn't block a free GPU range next to it. Windows merges touching 64 KiB envelopes because of its allocation granularity; Linux doesn't need to.
  • Arena ranges: a registry allocation need not start on a page (ccleste has a 32-byte one at 0x200538020). On Linux the arena commit is a MAP_FIXED mmap, which failed with EINVAL. An unaligned range now has its pages made writable with GuestArenaSetProtection; the exact bytes are still marked used. Windows keeps GuestArenaCommit.
  • Restore now decides which ranges the replaying process already maps writable before it commits any range. Before, a second range in a page the first range had just committed counted as the process's own memory, and was skipped (CPU) or zeroed (GPU). The new agc_driver_frame_replay_shared_page test fails with each half of this reverted: guest arena commit failed: Invalid argument, and a range sharing a page with an earlier one was skipped.

Tests. agc_driver_frame_capture_eop and the zero, shader and shader_host replay-state modes are registered on Linux, plus new modes: unwatched (Windows and Linux; a capture with APS5_NO_WRITE_WATCH=1 must throw), and the Linux-only shared_page and host_neighbors (an occupied CPU host range next to a free GPU host range must skip only the CPU range). Each new mode fails with its fix reverted: a capture started without the write watch, frame replay: cannot map the GPU-visible host range at .... On Linux the write watch covers only ranges someone registers (libkernel does for title memory), while Windows watches arena commits automatically. So the two tests that commit arena memory directly register it with GuestWriteWatchRegister on Linux. tests/FrameReplay.cpp wrapped its std::system commands (the capture-and-replay case and the concurrent case #11197 added) in an extra pair of quotes for cmd.exe, which sh reads as one word; that pair is now added on Windows only.

#10711. #11197 fixed the recorder race and lifted the Windows quarantine, so agc_driver_frame_replay and agc_driver_frame_replay_concurrent are now registered on Linux next to Windows, with no quarantine option. Before #11197, the Linux build of agc_driver_frame_replay failed 24 of 50 runs pinned with taskset -c 0,1 (the replay did not reproduce the second copy); on this rebase both tests pass 50 of 50 pinned to two cores, to one core and unpinned.

Debt and BUILD.md updated: Linux support, watched direct memory views, the no-access and past-end-of-file cases. macOS still throws.

Overlap: pr_overlap reports #10760, #11435 and #11436 (below). Of the 82 others it could not compare, most share only the libSceAgcDriver CMakeLists.txt or debt file. One changes the same code: #7201 (macOS port) puts HostMapping::Find under #if defined(__APPLE__) and keeps the Linux return Mapping{...} line in its #else, which this change turns into return FromQuery(query, name). That is a one-line conflict for whichever merges second, and its macOS Mapping{begin, end, name} still compiles with the new defaulted fields. #10540 adds an extern char** environ; next to the <unistd.h> include of FrameRecorder.cpp, a line away from the include added here.

The reported overlaps, and the recorder fix this change waited for:

  • #10760 (capture trace queue) inserts a debt entry right above the "Frame capture" entry this change rewrites; only docs/dev/debt/libSceAgcDriver.md conflicts. Order agreed in #10760: independent, no Depends on, whichever is ready first merges; the second rebases and keeps both entries, "Capture trace output" right above "Frame capture".
  • #11435 (Sam-mon7, full-title replay) and #11436 (stacked on it) turn the std::system call of the capture-and-replay case in tests/FrameReplay.cpp into a replay lambda that keeps the cmd.exe outer quotes and adds --repeat 3 and an output redirect. That is a textual conflict on the same lines, and with both merged the Linux test would fail on the outer quotes unless the lambda adds them on Windows only. Order proposed in #11435: either order, no Depends on; whichever merges second makes the lambda's outer quotes Windows-only.
  • #11197 (LeandroLP23, the recorder fix for #10711) merged first, as agreed in #10711. This branch is rebased onto it: the quarantine changes are gone, and the Linux registration sits next to Windows'.

Tested

Arch Linux 7.2.11, AMD Radeon RX 7900 GRE (RADV NAVI31, Mesa 26.2.5), Ryzen 5 7500F, Release, on main 7903201bb4.

Focused tests with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest -R 'agc_frame_capture|agc_driver_frame_(capture_eop|replay)' -V:

  • main: 4/4 pass (agc_frame_capture and the replay modes order, tokens and blocking; the others are not registered on Linux), 0 VUID-, no missing-layer line.
  • this PR: 13/13 pass (agc_frame_capture, agc_driver_frame_capture_eop, replay modes order, tokens, blocking, zero, shader, shader_host, unwatched, shared_page, host_neighbors, agc_driver_frame_replay, agc_driver_frame_replay_concurrent), 0 VUID-, no missing-layer line. The replay tests copy with DMA and don't depend on the Vulkan device.

agc_driver_frame_replay and agc_driver_frame_replay_concurrent, 50 runs each under taskset -c 0,1, taskset -c 0 and unpinned: 0 failures in 300.

ctest -R 'guest|sysmodule|module|unwind|frame' -j6 (819 tests, covering the HostMapping users GuestMemory, sceKernelGetModuleInfoForUnwind and sceSysmoduleGetModuleInfoForUnwind): all pass, 2 skipped (video_out_frame_dump_queue, video_out_frame_dump_wait).

On the pre-rebase head 9aad99ba3c (main ae7d59258e; the rebase leaves the recorder and replay tool code unchanged):

ccleste PPSA59322 1.00 (homebrew, MIT, sha256 96861b81…), relinked with default options, title screen, no input, APS5_FRAME_CAPTURE_AT_FLIP=300 APS5_FRAME_CAPTURE_FRAMES=10:

  • main: aborts at frame boundary 300 with terminate called after throwing an instance of 'std::runtime_error' / what(): frame capture: guest memory capture is implemented on Windows only.
  • this PR: [frame-capture] started at frame boundary 300: 111 ranges, 163.0 MiB snapshot in 0.2 s / 10 frames, 32 calls, 33 memory deltas (6.6 MiB), 264.5 MiB raw, 28.0 MiB stored, unreadable_bytes 24576 (the shader cache index pages past the end of its file), no warnings. agc_frame_replay capture --frames out:
{"frames":10,"replay_s":0.133,"fps":75.412,"frame_ms":{"p50":2.782,"p90":7.751,"max":7.751,"each":[0,7,7,0,2,3,2,2,7]},"submits":11,"delta_mib":6.6,"pace_timeouts":0,"pace_wait_s":0.333,"stale_registrations":0,"failed_registrations":0,"skipped_mib":0.0,"threads":3,"released_waits":0,"of_them_from_earlier_writes":0,"of_them_synthesized":0,"unfinished":0,"frames_compared":3,"pixels_identical":1.000000,"max_channel_diff":0,"load_s":0.032}

All three display buffers match their reference images pixel for pixel; the replayed title screen is attached.

Not tested: Windows. The Windows recorder walk and the Windows commit path are unchanged; the write-watch check at capture start and the unwatched test also run there. The ownership decision before commits also applies on Windows and changes behaviour only for two ranges in one page. tests/FrameReplay.cpp keeps its outer quotes on Windows.

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: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 7900 GRE, RADV NAVI31, Mesa 26.2.5, Arch Linux 7.2.11
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5008167770 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11157). Originally posted by [janzezno](https://github.com/janzezno) on 2026-10-25T20:01:18Z. --- ### What `APS5_FRAME_CAPTURE` and `agc_frame_replay` only work on Windows. On Linux a capture throws at its first frame boundary with `frame capture: guest memory capture is implemented on Windows only`, so a title that misrenders or hangs on Linux can't be captured and replayed offline. Everything else the recorder uses already runs on Linux (the write watch, `ReadCommitted`, the guest arena and the allocation registry). The missing parts were the VirtualQuery walk in `FrameRecorder::committedRanges` and the VirtualAlloc calls in the replay tool. This change adds Linux versions of those and runs the capture tests on Linux. **Recorder** (`committedRanges`, Linux branch). It walks each readable registry allocation's VMAs with a new `HostMapping::Walk` (`PROCMAP_QUERY` when the kernel has it, else one `/proc/self/maps` pass). `HostMapping::Mapping` gains the r/w/x/shared bits and the inode, filled on both paths. Classification follows the Windows branch: - An allocation outside the arena is `Host`. - A `PROT_NONE` VMA is the Linux form of reserved and uncommitted memory (the arena reserves and resets with `PROT_NONE`), so it is left out. - Inside the arena, a range the write watch covers is `Watched`, other private anonymous memory is `Unwatched`, and the rest (shared and file-backed) is `Section`. One difference from Windows: on Linux libkernel registers direct memory views with the write watch (`AddMapping` → `WatchMapping`). Those views are watched, so CPU stores to direct memory during the frames are recorded, which Windows section views can't do. - `protection` stores the Windows `PAGE_*` value of the VMA bits. That is the protection vocabulary the arena API already uses on Linux, so the field means the same thing in captures from either host. - A file mapping can extend past the end of its file, and reading those pages raises SIGBUS. ccleste's bundled Mesa maps its `radv-shader-cache/.../index` file that way, and the first Linux capture died in `ReadCommitted`. `/proc/self/map_files` needs `CAP_SYS_ADMIN`, so the file size can't be read from there. Instead the recorder probes pages with `process_vm_readv` on its own process (a page past the end gives `EFAULT`, not a signal), binary-searches the boundary and records the pages past it as an unreadable range. Any other probe error (for example `EPERM` under a seccomp or ptrace policy) fails the capture instead of dropping memory. - A capture now throws when it starts if the guest write watch is unavailable (`APS5_NO_WRITE_WATCH`, or a host without one), on both platforms. Without the watch a capture used to start and silently miss every CPU store during the frames. **Replay tool.** - `committedWritable`: Linux version through `HostMapping::Walk`. - `mapHostRanges`: maps page-aligned envelopes with `MAP_FIXED_NOREPLACE`. Envelopes merge only where two ranges share a page, so an occupied CPU range doesn't block a free GPU range next to it. Windows merges touching 64 KiB envelopes because of its allocation granularity; Linux doesn't need to. - Arena ranges: a registry allocation need not start on a page (ccleste has a 32-byte one at `0x200538020`). On Linux the arena commit is a `MAP_FIXED` mmap, which failed with `EINVAL`. An unaligned range now has its pages made writable with `GuestArenaSetProtection`; the exact bytes are still marked used. Windows keeps `GuestArenaCommit`. - Restore now decides which ranges the replaying process already maps writable before it commits any range. Before, a second range in a page the first range had just committed counted as the process's own memory, and was skipped (CPU) or zeroed (GPU). The new `agc_driver_frame_replay_shared_page` test fails with each half of this reverted: `guest arena commit failed: Invalid argument`, and `a range sharing a page with an earlier one was skipped`. **Tests.** `agc_driver_frame_capture_eop` and the `zero`, `shader` and `shader_host` replay-state modes are registered on Linux, plus new modes: `unwatched` (Windows and Linux; a capture with `APS5_NO_WRITE_WATCH=1` must throw), and the Linux-only `shared_page` and `host_neighbors` (an occupied CPU host range next to a free GPU host range must skip only the CPU range). Each new mode fails with its fix reverted: `a capture started without the write watch`, `frame replay: cannot map the GPU-visible host range at ...`. On Linux the write watch covers only ranges someone registers (libkernel does for title memory), while Windows watches arena commits automatically. So the two tests that commit arena memory directly register it with `GuestWriteWatchRegister` on Linux. `tests/FrameReplay.cpp` wrapped its `std::system` commands (the capture-and-replay case and the `concurrent` case #11197 added) in an extra pair of quotes for `cmd.exe`, which `sh` reads as one word; that pair is now added on Windows only. **#10711.** #11197 fixed the recorder race and lifted the Windows quarantine, so `agc_driver_frame_replay` and `agc_driver_frame_replay_concurrent` are now registered on Linux next to Windows, with no quarantine option. Before #11197, the Linux build of `agc_driver_frame_replay` failed 24 of 50 runs pinned with `taskset -c 0,1` (`the replay did not reproduce the second copy`); on this rebase both tests pass 50 of 50 pinned to two cores, to one core and unpinned. Debt and `BUILD.md` updated: Linux support, watched direct memory views, the no-access and past-end-of-file cases. macOS still throws. Overlap: `pr_overlap` reports #10760, #11435 and #11436 (below). Of the 82 others it could not compare, most share only the libSceAgcDriver `CMakeLists.txt` or debt file. One changes the same code: #7201 (macOS port) puts `HostMapping::Find` under `#if defined(__APPLE__)` and keeps the Linux `return Mapping{...}` line in its `#else`, which this change turns into `return FromQuery(query, name)`. That is a one-line conflict for whichever merges second, and its macOS `Mapping{begin, end, name}` still compiles with the new defaulted fields. #10540 adds an `extern char** environ;` next to the `<unistd.h>` include of `FrameRecorder.cpp`, a line away from the include added here. The reported overlaps, and the recorder fix this change waited for: - #10760 (capture trace queue) inserts a debt entry right above the "Frame capture" entry this change rewrites; only `docs/dev/debt/libSceAgcDriver.md` conflicts. Order agreed in #10760: independent, no `Depends on`, whichever is ready first merges; the second rebases and keeps both entries, "Capture trace output" right above "Frame capture". - #11435 (Sam-mon7, full-title replay) and #11436 (stacked on it) turn the `std::system` call of the capture-and-replay case in `tests/FrameReplay.cpp` into a `replay` lambda that keeps the `cmd.exe` outer quotes and adds `--repeat 3` and an output redirect. That is a textual conflict on the same lines, and with both merged the Linux test would fail on the outer quotes unless the lambda adds them on Windows only. Order proposed in #11435: either order, no `Depends on`; whichever merges second makes the lambda's outer quotes Windows-only. - #11197 (LeandroLP23, the recorder fix for #10711) merged first, as agreed in #10711. This branch is rebased onto it: the quarantine changes are gone, and the Linux registration sits next to Windows'. ### Tested Arch Linux 7.2.11, AMD Radeon RX 7900 GRE (RADV NAVI31, Mesa 26.2.5), Ryzen 5 7500F, Release, on `main` `7903201bb4`. Focused tests with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ctest -R 'agc_frame_capture|agc_driver_frame_(capture_eop|replay)' -V`: - `main`: 4/4 pass (`agc_frame_capture` and the replay modes `order`, `tokens` and `blocking`; the others are not registered on Linux), 0 `VUID-`, no missing-layer line. - this PR: 13/13 pass (`agc_frame_capture`, `agc_driver_frame_capture_eop`, replay modes `order`, `tokens`, `blocking`, `zero`, `shader`, `shader_host`, `unwatched`, `shared_page`, `host_neighbors`, `agc_driver_frame_replay`, `agc_driver_frame_replay_concurrent`), 0 `VUID-`, no missing-layer line. The replay tests copy with DMA and don't depend on the Vulkan device. `agc_driver_frame_replay` and `agc_driver_frame_replay_concurrent`, 50 runs each under `taskset -c 0,1`, `taskset -c 0` and unpinned: 0 failures in 300. `ctest -R 'guest|sysmodule|module|unwind|frame' -j6` (819 tests, covering the `HostMapping` users `GuestMemory`, `sceKernelGetModuleInfoForUnwind` and `sceSysmoduleGetModuleInfoForUnwind`): all pass, 2 skipped (`video_out_frame_dump_queue`, `video_out_frame_dump_wait`). On the pre-rebase head `9aad99ba3c` (`main` `ae7d59258e`; the rebase leaves the recorder and replay tool code unchanged): ccleste PPSA59322 1.00 (homebrew, MIT, sha256 `96861b81…`), relinked with default options, title screen, no input, `APS5_FRAME_CAPTURE_AT_FLIP=300 APS5_FRAME_CAPTURE_FRAMES=10`: - `main`: aborts at frame boundary 300 with `terminate called after throwing an instance of 'std::runtime_error'` / `what(): frame capture: guest memory capture is implemented on Windows only`. - this PR: `[frame-capture] started at frame boundary 300: 111 ranges, 163.0 MiB snapshot in 0.2 s` / `10 frames, 32 calls, 33 memory deltas (6.6 MiB), 264.5 MiB raw, 28.0 MiB stored`, `unreadable_bytes` 24576 (the shader cache index pages past the end of its file), no warnings. `agc_frame_replay capture --frames out`: ```json {"frames":10,"replay_s":0.133,"fps":75.412,"frame_ms":{"p50":2.782,"p90":7.751,"max":7.751,"each":[0,7,7,0,2,3,2,2,7]},"submits":11,"delta_mib":6.6,"pace_timeouts":0,"pace_wait_s":0.333,"stale_registrations":0,"failed_registrations":0,"skipped_mib":0.0,"threads":3,"released_waits":0,"of_them_from_earlier_writes":0,"of_them_synthesized":0,"unfinished":0,"frames_compared":3,"pixels_identical":1.000000,"max_channel_diff":0,"load_s":0.032} ``` All three display buffers match their reference images pixel for pixel; the replayed title screen is attached. Not tested: Windows. The Windows recorder walk and the Windows commit path are unchanged; the write-watch check at capture start and the `unwatched` test also run there. The ownership decision before commits also applies on Windows and changes behaviour only for two ranges in one page. `tests/FrameReplay.cpp` keeps its outer quotes on Windows. ### 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: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 7900 GRE, RADV NAVI31, Mesa 26.2.5, Arch Linux 7.2.11 - [x] AI-assisted: yes
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!11157
No description provided.