AGC: ImportWatched copies every non-zero page of the range, so a 3 GiB first import holds 3 GiB of host memory #7980

Closed
opened 2026-10-19 16:46:41 +00:00 by fisherjoey · 0 comments
fisherjoey commented 2026-10-19 16:46:41 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-19T16:46:41Z.


Problem

ImportWatched (core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp) copies every non-zero page of the range it imports into a host vector, and holds that copy for the whole import. When a title imports a large guest heap for the first time, host memory goes up by the size of that heap.

Demon's Souls (PPSA01341 v1.07) does its first imports when a level loads, and they are 0.5 to 3 GiB each. On Linux (RTX 5070 Ti, glibc) that showed up as host memory bursts right at the load:

  • A run that printed a backtrace for every host allocation of 256 MiB or more had a 493 MiB allocation from GuestMemory::ImportWatched <- importRunAt <- importAllocation <- GuestBufferMemory::AcquireRegistered <- prepareAddressBindings.
  • In another run, a 2.9 GiB anonymous mapping appeared in the last 10 s before the kernel's global OOM killer stopped the process (18 GiB memory cap, 6 GiB swap cap on the run).

Mechanism

From #3874 the import window compares pages after the import, so a page the write watch reports during the import can be told apart from one that still holds its bytes (a write of the same bytes, or the import's own touch). #5698 made it copy only the non-zero pages. On main (dbac9fa15):

  • ImportWatched, GuestMemory.cpp:1525-1536: when the range is covered and stays watched, every page is checked against ZeroPage, and every non-zero page is appended to saved with saved.insert(...). saved is kept * 4096 bytes.
  • WindowCopy::Holds, GuestMemory.cpp:791-802: after the import, each page the watch reports is memcmp'd against its saved copy, or against ZeroPage.

The window only needs to know, per page, whether the bytes are the same as before, and it keeps a full copy of the range to answer that. A heap the title has already filled, which is the normal case for a large first import, is almost all non-zero pages, and the zero-page filter from #5698 saves very little there.

Expected

An import should hold a small fraction of the range, not a copy of it. A 64-bit digest per 4 KiB page gives the same decision for each page (short of a 64-bit collision) at 1/512 of the range's size: 6 MiB for a 3 GiB import instead of 3 GiB. A fix with a failing-first test follows in a PR.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5927845694 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/7980). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-19T16:46:41Z. --- ### Problem `ImportWatched` (core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp) copies every non-zero page of the range it imports into a host vector, and holds that copy for the whole import. When a title imports a large guest heap for the first time, host memory goes up by the size of that heap. Demon's Souls (PPSA01341 v1.07) does its first imports when a level loads, and they are 0.5 to 3 GiB each. On Linux (RTX 5070 Ti, glibc) that showed up as host memory bursts right at the load: - A run that printed a backtrace for every host allocation of 256 MiB or more had a 493 MiB allocation from `GuestMemory::ImportWatched <- importRunAt <- importAllocation <- GuestBufferMemory::AcquireRegistered <- prepareAddressBindings`. - In another run, a 2.9 GiB anonymous mapping appeared in the last 10 s before the kernel's global OOM killer stopped the process (18 GiB memory cap, 6 GiB swap cap on the run). ### Mechanism From #3874 the import window compares pages after the import, so a page the write watch reports during the import can be told apart from one that still holds its bytes (a write of the same bytes, or the import's own touch). #5698 made it copy only the non-zero pages. On main (dbac9fa15): - `ImportWatched`, GuestMemory.cpp:1525-1536: when the range is covered and stays watched, every page is checked against `ZeroPage`, and every non-zero page is appended to `saved` with `saved.insert(...)`. `saved` is `kept * 4096` bytes. - `WindowCopy::Holds`, GuestMemory.cpp:791-802: after the import, each page the watch reports is `memcmp`'d against its saved copy, or against `ZeroPage`. The window only needs to know, per page, whether the bytes are the same as before, and it keeps a full copy of the range to answer that. A heap the title has already filled, which is the normal case for a large first import, is almost all non-zero pages, and the zero-page filter from #5698 saves very little there. ### Expected An import should hold a small fraction of the range, not a copy of it. A 64-bit digest per 4 KiB page gives the same decision for each page (short of a 64-bit collision) at 1/512 of the range's size: 6 MiB for a 3 GiB import instead of 3 GiB. A fix with a failing-first test follows in a PR. AI-assisted: yes
Sign in to join this conversation.
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#7980
No description provided.