Repository navigation
perf(agc): query guest page access with PROCMAP_QUERY on Linux - #157
Conversation
|
A note on the gap case in the Linux When the cursor is in a gap between mappings, the Linux path reports the whole rest of the range unmapped and returns. That includes mapped pages after the gap. Main's line scan does this, and here A concrete case: map three anonymous RW pages,
The same skip hits In this PR's structure the fix is small. When no region contains the cursor, report the gap up to the next region above it and carry on, as the VirtualQuery path does with a free region: if (region == nullptr) {
const auto above = std::upper_bound(snapshot->regions.begin(), snapshot->regions.end(), cursor, [](std::uintptr_t value, const MappedRegion& mapped) { return value < mapped.first; });
const auto next = above == snapshot->regions.end() ? end : std::min(end, above->first);
if (!emit(PageRun{cursor, next, false, false})) return true;
cursor = next;
continue;
}With that change on top of this PR, the three queries return the expected results and There's also a cost note, since this is a perf PR. A cursor in a gap takes the reload path, so each such query still reads all of /proc/self/maps. In PPSA21564 on my integration branch, the lighting programs read near-null addresses (0x18, 0x30, 0x58) about 1500 times a second during resource capture. The maps there are about 5700 lines, mostly NVIDIA mappings. I have this fix against main's loop as about 10 lines plus a test. I can adapt it to your structure and send it as a follow-up once this lands, or it can be folded in here, whichever you prefer. |
|
#442 has landed since my earlier comment, so this now conflicts in GuestMemory.cpp. When you rebase, please keep its gap handling (emit the gap up to the next region and continue, as in the snippet above). For the reload cost I mentioned, one option: when the snapshot was just reloaded and is still the current generation, treat "no region contains the cursor" as a real gap instead of reloading again on every query. Happy to re-test on my integration branch once it is rebased. |
|
Thanks, and sorry for the delay. While rebasing I found that the cache itself no longer holds on current main: Two options I see: (a) use the snapshot only for addresses inside guest-registered allocations and read |
|
You're right, and the #459 test is right too: host mappings (thread stacks and their guard pages, driver allocations) change without the guest allocation generation moving, so a snapshot keyed on it can't answer for them, and my suggestion to trust a just-reloaded snapshot on a miss fails the same way, as you found. Of your two options, (a) still leaves the writable bit stale, as you say, and (b) is exact only while One option that is exact without a cache: the If you'd rather not take that on, closing this is fine with me too. |
d9d6edb to
d1fa963
Compare
|
Thanks, Measurements are in the updated description: same answers as the line scan, ~0.5 µs instead of ~760 µs per lookup, and in a 100 s Minecraft run the |
|
Thanks, this is what I had in mind. I checked the query path against the line scan:
A small program on 7.2 gives the expected answers for each case: an RW page, a hole left by One thing affects what this PR actually speeds up. The fast path is compiled only when Since the runtime probe already handles old kernels, the header check could also fall back to a local definition when the macro is missing:
The struct is size-versioned, so the kernel accepts that layout on any version that has the ioctl. Separately, nothing tests the fallback on a machine where the ioctl works. Something like an environment switch that skips the probe would let |
d1fa963 to
a1427b8
Compare
|
Thanks for checking it against the line scan. Both points are in now (rebased onto current
Tested on Linux 7.2 (CachyOS, GCC), ctest 359/359 in three setups: system headers, the local definitions (with the |
|
The Linux job failed on a timeout in |
|
Tested Before: the game died about 2 minutes in, inside a deliberate crash by Unreal's GC ( The busy queue worker's stack ended in With this PR: no abort in an 8-minute run. The game presents steadily (about 4 frames per 10 s, still CPU-bound elsewhere) and reaches its boot-flow movie for the first time: That is about 127× as many verifies in 2% of the time. No verify failures and no new errors in the log. |
|
Thanks a lot for testing it on LEGO Horizon Adventures, and for the before/after numbers. Seeing the GC abort go away and the game reach its boot-flow movie is a much better proof than my Minecraft runs, and it confirms the gain on RDNA4 as well. |
|
Measured with a game on Linux, in case it helps the review. Demon's Souls (PPSA01341 v1.07) on Linux 7.2, RTX 5070 Ti. During its intro movie (Bink 2 decoded in compute), almost all CPU time on cache hits went to dispatch validation, and in validation to
Same build otherwise, same run directory, |
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
|
Integrate upstream PR boykopovar#157 at a1427b8. Query individual VMAs through the kernel and retain the text scan on older kernels. Local validation: graphics, shader-memory and write-tracking tests pass with the ioctl path and with APS5_NO_PROCMAP_QUERY=1. The Skyrim performance capture identified repeated maps parsing as a CPU hotspot. AI-assisted upstream implementation and local integration. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
Replying to the invitation when #1114 was closed as a duplicate of this one: #157 does not miss the case I hit. I verified it on both code paths. The case was the guard-page assertion in Linux 7.2.9 (CachyOS, GCC, Release), NVIDIA RTX 3080 Ti: Kernel here is 7.2.9, so the Why it does not have the problem the other one had, in the terms the failure showed. The stale hit came from a cache refreshed only on a miss: a region that still covered the queried address was a hit, so nothing was re-read, and a page that had since become The forced-fallback run is the part I would keep in the PR: it makes the line scan testable on a kernel that does have the ioctl, and 359/359 with the fallback forced means the two paths agree on the case that mattered rather than only the fast path being exercised. That is everything I had. Nothing outstanding from my side. Written with AI assistance. |
Integrate upstream PR boykopovar#157 at a1427b8. Query individual VMAs through the kernel and retain the text scan on older kernels. Local validation: graphics, shader-memory and write-tracking tests pass with the ioctl path and with APS5_NO_PROCMAP_QUERY=1. The Skyrim performance capture identified repeated maps parsing as a CPU hotspot. AI-assisted upstream implementation and local integration. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
Tested this on current main: ctest 410/410, and in a title whose mappings grow over the run it removes the slowdown completely (it held 60 fps where main falls to single digits). One thing before merging: in |
describePages parsed the whole /proc/self/maps for every run it reported. On kernels with the PROCMAP_QUERY ioctl (Linux 6.11+) each run is now one ioctl on an open /proc/self/maps fd with PROCMAP_QUERY_COVERING_OR_NEXT_VMA, which returns the covering or next VMA and its read/write flags straight from the kernel, so the answer is exact for host mappings too. A VMA starting above the cursor is a gap up to its start and ENOENT is a gap to the end. Kernels without the ioctl keep the line scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
a1427b8 to
18bc4b6
Compare
|
Pushed the fix Dean asked for, so this doesn't have to wait:
Linux, GCC 16.2.1, RADV gfx1035: full ctest 442/442. With |
What
On Linux
describePages(GuestMemory.cpp) opened and parsed the whole/proc/self/mapsfor every run it reported (the arena/image page spans that answer most queries on Windows are empty there), about 0.5–0.8 ms per lookup.On kernels with the
PROCMAP_QUERYioctl (Linux 6.11+) each run is now one ioctl on an open/proc/self/mapsfd withPROCMAP_QUERY_COVERING_OR_NEXT_VMA: the kernel returns the VMA covering the cursor, or the next one above it, with its read/write flags. No snapshot is kept, so host-only mapping changes (thread stacks, guard pages, driver allocations) are seen immediately. A VMA starting above the cursor is a gap up tovma_start, andENOENTis a gap to the end, matching the gap handling from #442. The fd is opened and probed once; if the probe fails (ENOTTYon older kernels), the line scan stays as the fallback. When<linux/fs.h>predates 6.11 (ubuntu-latesthas 6.8), the v1struct procmap_query, the flags used andPROCMAP_QUERYare defined locally, so the fast path is still compiled and the runtime probe decides.APS5_NO_PROCMAP_QUERYskips the probe, so the line scan can be tested on a kernel that has the ioctl.Tested
Linux 7.2 (CachyOS, GCC): full build,
ctest359/359 with the system headers, with the local definitions (linux/fs.hinclude disabled) and withAPS5_NO_PROCMAP_QUERY=1.Standalone comparison against the line scan over 2003 addresses in a process with ~4000 mixed-protection VMAs: identical readable/writable answers and run ends (the only difference is the end of the gap above
0x7fffffffe000, where the line scan stops at[vsyscall]; both report it inaccessible), 763 µs vs 0.54 µs per lookup.Minecraft (on a local branch with more rendering work), 100 s runs:
[guestmem] verifytotal 6.7 s with this change, against 52.1 s with the previous maps snapshot cache; no verify failures in either run.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code