perf: every guest malloc and free makes the next draw rebuild a lease over the whole allocation registry (Godot PS5, about 2.4 ms per draw) #26456

Closed
opened 2026-11-23 23:45:42 +00:00 by consolelogout · 0 comments
consolelogout commented 2026-11-23 23:45:42 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by consolelogout on 2026-11-23T23:45:42Z.


Title and scene

Godot PS5 (PPSA99847, v0.2.10, content version 01.000.021) from https://github.com/Yahballon/PS5_Godot, SHA-256 checked against the release asset digest. Its built-in Pong, idle on the court after start, no pad input. Converted as in its bring-up thread (#24732): relinker --windows --to-intel --registry --trap-sdk-syscall-stubs after tools/unwrap_self.py on eboot.bin and sce_module/libc.prx.

What is slow or what changed

Pong runs at about 28 fps (frame p50 35.6 ms) with the GPU busy 8.3 ms per present. The draw committer is the bound: 86 % busy, Graphics.Draw 19.9 ms per frame. #24732 reported 25 to 28 fps on another machine.

Where the draw time goes. With APS5_PROFILE_DRAW=1, per 10 s: about 3,100 draws; 780 are draw-recipe hits at 73 us, about 2,350 are template hits at 2,381 us. Of the draw phases, vertex is 2,686 ms and lookup 2,748 ms. [rescache] revalidate reports 3,131 calls, 2,740 ms, all on the fast path. [recipe] draw reports 780 hits and 2,340 attaches with no pre-check misses.

Stacks. APS5_SAMPLE_COMMITTER, symbolized with tools/symbolize_samples.py --demangle: 58 % of the committer's samples are under commitDraw, and by self time:

  • refreshImports 24 %;
  • GuestAllocationsAcquire_nid_postfix 20 %;
  • std::_Rb_tree_increment 9 %, under GuestAllocationsAcquire.

Both come from two places on each draw:

  • the vertex phase: prepareDrawInputs > InPlaceDrawInput > HostImportFor;
  • the lookup phase: resolveDrawResources > ShaderResources::Revalidate > revalidateFull > HostImportsReconciledUnchanged.

Each takes a lease and calls refreshImports whenever GuestAllocationsGeneration moved since the last refresh (importsStale, GuestBufferMemory.cpp:2991, and :8478).

What moves the generation. I added a temporary counter around generation.fetch_add in GuestAllocationsEnd and GuestAllocationsInvalidate, keyed by return address, plus a count of GuestAllocationsAcquire calls that rebuilt the lease against those that reused readableLease. Per 10 s:

  • 47,788 to 49,558 generation bumps from GuestAllocationsEnd, 0 from GuestAllocationsInvalidate;
  • by call site: GuestHeapAllocate_nid_postfix 24,364, ApplicationHeap.cpp defaultFree (GuestHeapFree_nid_postfix) 24,364, GuestHeapReallocate_nid_postfix 9, and about 270 each from libkernel DoMapDirect, DoMunmap and sceKernelReleaseDirectMemory;
  • 529 to 550 lease rebuilds, 0 reuses;
  • 155,948 ranges in the registry.

So the title's own heap traffic, about 2,400 mallocs and 2,400 frees per second, each registered as a range (GuestHeap.cpp, allocate and free through a GuestAllocations::Mutation), moves the generation between almost every pair of draws. Each GuestAllocationsAcquire then copies the shared_ptr of every readable range, up to about 156,000, into a new lease (readableLease is reset in GuestAllocationsBegin and GuestAllocationsEnd), and refreshImports diffs that lease against state.refreshedRanges (changedSpans) before it can tell nothing relevant changed. GuestHeapUsableSize_nid_postfix also opens a mutation and bumps the generation, although it only reads.

How much is at stake. As a measurement only, I let the heap's mutations skip the generation bump (a thread-local flag set in the GuestHeap* functions and read in GuestAllocationsEnd). This is not safe: heap blocks can be host-imported, since continuesRun and desiredRun do not look at gpu. Same build, 60 s each:

presents per 10 s lookup ms per 10 s vertex ms per 10 s lease rebuilds per 10 s
main 252, 256, 257, 261, 264 2,737 2,685 529
heap mutations skip the bump 418, 436, 454, 451, 459 42 4,086 456

lookup drops to almost nothing. vertex stays high because HostImportFor still takes a lease for every vertex range that has no import yet, and the lease is still rebuilt after every heap mutation, even when the generation does not move.

Related. #11795 measures a whole-registry pass per address-space rebuild in RESIDENT EVIL requiem (there from import epoch moves). #6453 is about whole-registry leases on munmap/mprotect.

Possible directions, for whoever knows this path:

  1. Let the import reconciliation learn which spans changed instead of diffing the whole registry. GuestAllocationsEnd already has the changed spans (state->changed, passed to the invalidator). A bounded log of (generation, span) would let importsStale and refreshImports advance refreshedGeneration without a lease when no changed span touches, or is next to, an import, a failed or refused run, a growth run or a pending import. refreshedRanges could stay as it is, so a later full diff still covers the skipped changes.
  2. Answer HostImportFor misses for ranges already known not to be importable without a new lease, until a change touches them.
  3. Make GuestAllocationsAcquire cheaper after a heap mutation, or keep heap blocks out of the readable lease where no import can use them.
  4. Make GuestHeapUsableSize read without a mutation.

I can work on one of these if you say which direction you'd accept.

Summary lines

{"commit":"f4c9a67f581b","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"20:10"},"gpu":"NVIDIA GeForce RTX 4080 SUPER","driver":"NVIDIA 617.42","os":"Windows 10.0.19045","cpu":"11th Gen Intel(R) Core(TM) i5-11600 @ 2.80GHz","cpu_threads":12,"memory_mib":16220,"title_id":"PPSA99847","title":"Godot PS5 Buffered Stdio Test M26 v0.2.10","content_version":"01.000.021","resolution":"1920x1080","window":{"spec":"seconds 20:10","first_present":505,"ms":10033.495},"presents":274,"frame_ms":{"p50":35.642,"p90":39.890,"p99":47.194,"max":59.297},"gpu_busy_ms_per_present":8.319,"worker_busy_pct":{"queue worker 0x0":42.703},"top_phases_ms_per_frame":{"Graphics.Draw":19.883,"Driver.Packet":15.627,"Recorder.Sync":11.205,"Graphics.Draw.draw_and_resource_release":10.161,"Graphics.Draw.index_upload":9.676,"Driver.Packet.draw":7.750,"Driver.Draw":7.740,"Driver.Packet.pipeline_drain":7.724},"trace_dropped":false,"trace":"aps5-trace-f4c9a67f581b-24740.json.gz"}

(phase_totals_ms, owned_draw_counts and graphics_track trimmed here; the full line is in the attached aps5-summary.jsonl.)

Traces

aps5-trace-f4c9a67f581b-24740.json.gz (attached).

Digest

seconds 20:10 from present 505: 274 presents, frame p50 35.6 / p90 39.9 / p99 47.2 / max 59.3 ms, GPU busy 8.3 ms per present
NVIDIA GeForce RTX 4080 SUPER, NVIDIA 617.42, Windows 10.0.19045, 1920x1080, commit f4c9a67f581b, dropped: no

| thread | busy | wait | untraced | in slices < 20 us | largest |
|---|---|---|---|---|---|
| queue worker 0x0 | 15.5 (42 %) | 20.9 (57 %) | 0.2 | 0.8 | Driver.Packet 15.5, Driver.Completion 0.0 |
| draw committer | 31.3 (86 %) | 0.0 (0 %) | 5.3 | 0.2 | Graphics.Draw 19.8, Recorder.Sync 11.2 |
| presenter | 1.4 (4 %) | 0.3 (1 %) | 34.9 | 0.1 | VideoOut.Flip 1.7, Driver.Present 0.0 |
| gpu | 8.3 (23 %) | 0.0 (0 %) | 28.3 | 0.0 | batch 8.3 |

Worker stacks

committer.collapsed.txt from APS5_SAMPLE_COMMITTER (attached; a separate 45 s run with APS5_PROFILE_DRAW=1).

Capture

  • The caches were warm (the scene ran once and the title was restarted)
  • No other APS5_TRACE_* or APS5_PROFILE_* switch was set and the log was not redirected
  • Captures compared here come from the same machine, scene and settings

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6571234504 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/26456). Originally posted by [consolelogout](https://github.com/consolelogout) on 2026-11-23T23:45:42Z. --- ### Title and scene Godot PS5 (PPSA99847, v0.2.10, content version 01.000.021) from https://github.com/Yahballon/PS5_Godot, SHA-256 checked against the release asset digest. Its built-in Pong, idle on the court after start, no pad input. Converted as in its bring-up thread (#24732): `relinker --windows --to-intel --registry --trap-sdk-syscall-stubs` after `tools/unwrap_self.py` on `eboot.bin` and `sce_module/libc.prx`. ### What is slow or what changed Pong runs at about 28 fps (frame p50 35.6 ms) with the GPU busy 8.3 ms per present. The draw committer is the bound: 86 % busy, `Graphics.Draw` 19.9 ms per frame. #24732 reported 25 to 28 fps on another machine. **Where the draw time goes.** With `APS5_PROFILE_DRAW=1`, per 10 s: about 3,100 draws; 780 are draw-recipe hits at 73 us, about 2,350 are template hits at 2,381 us. Of the draw phases, `vertex` is 2,686 ms and `lookup` 2,748 ms. `[rescache] revalidate` reports 3,131 calls, 2,740 ms, all on the fast path. `[recipe] draw` reports 780 hits and 2,340 attaches with no pre-check misses. **Stacks.** `APS5_SAMPLE_COMMITTER`, symbolized with `tools/symbolize_samples.py --demangle`: 58 % of the committer's samples are under `commitDraw`, and by self time: - `refreshImports` 24 %; - `GuestAllocationsAcquire_nid_postfix` 20 %; - `std::_Rb_tree_increment` 9 %, under `GuestAllocationsAcquire`. Both come from two places on each draw: - the `vertex` phase: `prepareDrawInputs > InPlaceDrawInput > HostImportFor`; - the `lookup` phase: `resolveDrawResources > ShaderResources::Revalidate > revalidateFull > HostImportsReconciledUnchanged`. Each takes a lease and calls `refreshImports` whenever `GuestAllocationsGeneration` moved since the last refresh (`importsStale`, `GuestBufferMemory.cpp:2991`, and `:8478`). **What moves the generation.** I added a temporary counter around `generation.fetch_add` in `GuestAllocationsEnd` and `GuestAllocationsInvalidate`, keyed by return address, plus a count of `GuestAllocationsAcquire` calls that rebuilt the lease against those that reused `readableLease`. Per 10 s: - 47,788 to 49,558 generation bumps from `GuestAllocationsEnd`, 0 from `GuestAllocationsInvalidate`; - by call site: `GuestHeapAllocate_nid_postfix` 24,364, `ApplicationHeap.cpp` `defaultFree` (`GuestHeapFree_nid_postfix`) 24,364, `GuestHeapReallocate_nid_postfix` 9, and about 270 each from libkernel `DoMapDirect`, `DoMunmap` and `sceKernelReleaseDirectMemory`; - 529 to 550 lease rebuilds, 0 reuses; - 155,948 ranges in the registry. So the title's own heap traffic, about 2,400 mallocs and 2,400 frees per second, each registered as a range (`GuestHeap.cpp`, `allocate` and `free` through a `GuestAllocations::Mutation`), moves the generation between almost every pair of draws. Each `GuestAllocationsAcquire` then copies the `shared_ptr` of every readable range, up to about 156,000, into a new lease (`readableLease` is reset in `GuestAllocationsBegin` and `GuestAllocationsEnd`), and `refreshImports` diffs that lease against `state.refreshedRanges` (`changedSpans`) before it can tell nothing relevant changed. `GuestHeapUsableSize_nid_postfix` also opens a mutation and bumps the generation, although it only reads. **How much is at stake.** As a measurement only, I let the heap's mutations skip the generation bump (a thread-local flag set in the `GuestHeap*` functions and read in `GuestAllocationsEnd`). This is not safe: heap blocks can be host-imported, since `continuesRun` and `desiredRun` do not look at `gpu`. Same build, 60 s each: | | presents per 10 s | `lookup` ms per 10 s | `vertex` ms per 10 s | lease rebuilds per 10 s | |---|---|---|---|---| | `main` | 252, 256, 257, 261, 264 | 2,737 | 2,685 | 529 | | heap mutations skip the bump | 418, 436, 454, 451, 459 | 42 | 4,086 | 456 | `lookup` drops to almost nothing. `vertex` stays high because `HostImportFor` still takes a lease for every vertex range that has no import yet, and the lease is still rebuilt after every heap mutation, even when the generation does not move. **Related.** #11795 measures a whole-registry pass per address-space rebuild in RESIDENT EVIL requiem (there from import epoch moves). #6453 is about whole-registry leases on munmap/mprotect. **Possible directions**, for whoever knows this path: 1. Let the import reconciliation learn which spans changed instead of diffing the whole registry. `GuestAllocationsEnd` already has the changed spans (`state->changed`, passed to the invalidator). A bounded log of `(generation, span)` would let `importsStale` and `refreshImports` advance `refreshedGeneration` without a lease when no changed span touches, or is next to, an import, a failed or refused run, a growth run or a pending import. `refreshedRanges` could stay as it is, so a later full diff still covers the skipped changes. 2. Answer `HostImportFor` misses for ranges already known not to be importable without a new lease, until a change touches them. 3. Make `GuestAllocationsAcquire` cheaper after a heap mutation, or keep heap blocks out of the readable lease where no import can use them. 4. Make `GuestHeapUsableSize` read without a mutation. I can work on one of these if you say which direction you'd accept. ### Summary lines ```json {"commit":"f4c9a67f581b","dirty":false,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"20:10"},"gpu":"NVIDIA GeForce RTX 4080 SUPER","driver":"NVIDIA 617.42","os":"Windows 10.0.19045","cpu":"11th Gen Intel(R) Core(TM) i5-11600 @ 2.80GHz","cpu_threads":12,"memory_mib":16220,"title_id":"PPSA99847","title":"Godot PS5 Buffered Stdio Test M26 v0.2.10","content_version":"01.000.021","resolution":"1920x1080","window":{"spec":"seconds 20:10","first_present":505,"ms":10033.495},"presents":274,"frame_ms":{"p50":35.642,"p90":39.890,"p99":47.194,"max":59.297},"gpu_busy_ms_per_present":8.319,"worker_busy_pct":{"queue worker 0x0":42.703},"top_phases_ms_per_frame":{"Graphics.Draw":19.883,"Driver.Packet":15.627,"Recorder.Sync":11.205,"Graphics.Draw.draw_and_resource_release":10.161,"Graphics.Draw.index_upload":9.676,"Driver.Packet.draw":7.750,"Driver.Draw":7.740,"Driver.Packet.pipeline_drain":7.724},"trace_dropped":false,"trace":"aps5-trace-f4c9a67f581b-24740.json.gz"} ``` (`phase_totals_ms`, `owned_draw_counts` and `graphics_track` trimmed here; the full line is in the attached `aps5-summary.jsonl`.) ### Traces `aps5-trace-f4c9a67f581b-24740.json.gz` (attached). ### Digest ``` seconds 20:10 from present 505: 274 presents, frame p50 35.6 / p90 39.9 / p99 47.2 / max 59.3 ms, GPU busy 8.3 ms per present NVIDIA GeForce RTX 4080 SUPER, NVIDIA 617.42, Windows 10.0.19045, 1920x1080, commit f4c9a67f581b, dropped: no | thread | busy | wait | untraced | in slices < 20 us | largest | |---|---|---|---|---|---| | queue worker 0x0 | 15.5 (42 %) | 20.9 (57 %) | 0.2 | 0.8 | Driver.Packet 15.5, Driver.Completion 0.0 | | draw committer | 31.3 (86 %) | 0.0 (0 %) | 5.3 | 0.2 | Graphics.Draw 19.8, Recorder.Sync 11.2 | | presenter | 1.4 (4 %) | 0.3 (1 %) | 34.9 | 0.1 | VideoOut.Flip 1.7, Driver.Present 0.0 | | gpu | 8.3 (23 %) | 0.0 (0 %) | 28.3 | 0.0 | batch 8.3 | ``` ### Worker stacks `committer.collapsed.txt` from `APS5_SAMPLE_COMMITTER` (attached; a separate 45 s run with `APS5_PROFILE_DRAW=1`). ### Capture - [x] The caches were warm (the scene ran once and the title was restarted) - [x] No other APS5_TRACE_* or APS5_PROFILE_* switch was set and the log was not redirected - [x] Captures compared here come from the same machine, scene and settings 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#26456
No description provided.