Repository navigation
gh-149584: _remote_debugging: Map remote pages with mach_vm_remap on macOS instead of copying them - #161011
gh-149584: _remote_debugging: Map remote pages with mach_vm_remap on macOS instead of copying them#161011maurycy wants to merge 6 commits into
_remote_debugging: Map remote pages with mach_vm_remap on macOS instead of copying them#161011Conversation
| } | ||
|
|
||
| static int | ||
| alias_maybe_probe_entry(proc_handle_t *handle, alias_page_entry_t *entry) |
There was a problem hiding this comment.
mach_vm_region_recurse is expensive, hence adaptive probing here, as the exact churn depends on the target
99d1803 to
69c085e
Compare
69c085e to
d561805
Compare
| _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) { | ||
| #ifdef MS_WINDOWS | ||
| #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX | ||
| _Py_RemoteDebug_AliasCacheClear(handle); |
There was a problem hiding this comment.
This makes the macOS branch below unreachable, so we no longer call mach_port_deallocate(). That leaks the task port reference even for callers that never use the alias cache. Can we keep the alias cleanup and the port release in the same branch?
| alias_maybe_probe_entry(proc_handle_t *handle, alias_page_entry_t *entry) | ||
| { | ||
| alias_read_cache_t *cache = &handle->alias; | ||
| if ((++cache->probe_counter & cache->probe_mask) != 0) { |
There was a problem hiding this comment.
Can we make the probe counter per entry, or rotate through the cached pages? With cache hits in the order A, B, A, B, every probe selects B because all probe intervals are even. If the target replaces A, we can keep the stale alias indefinitely.
I checked the actual C logic with mocked region queries. One million A/B pairs gave zero probes for A and 126 for B. We need a bound on how long each active entry can go without a probe.
| FALSE, | ||
| &cur_protection, | ||
| &max_protection, | ||
| VM_INHERIT_NONE); |
There was a problem hiding this comment.
What happens if the profiler forks after an unwinder fills this cache? The child inherits the cache entries, but VM_INHERIT_NONE excludes the mapped pages. A cache hit can then reach memcpy() with an unmapped pointer.
Can we reject inherited unwinders or reset their cache before any read? I checked this with a Linux model of non-inherited pages, so this still needs a native macOS regression test.
The PR adds
vm_remapsupport toprofiling.sampling(Tachyon) on macOS, instead of always usingmach_vm_read_overwrite().In plain English :-) instead of copying remote proces memory, just map the same page and read from it directly. Direct
memcpy()is much cheaper than a syscall. The trick works especially for typically stably mapped objects, hence explicit syscall read in #160888 for data stack.The performance gain is relatively good:
maine891a91 + #160888)On top of this, there's little loss in accuracy:
Oracle, this PR (12 cases, 3 runs, 0.5 s each)
v.
Oracle, baseline (12 cases, 3 runs, 0.5 s each)
(this is where #153804 shines in my opinion.)
To make things even better, the CPU usage is also relatively better:
(everything on PGO+LTO builds.)
Some considerations:
vm_remapis a Mach 3, so it's at least 1992? The XNU kernel is extremely helpful, but there's also https://github.com/apple-oss-distributions/xnu/blob/f6217f891ac0bb64f3d375211650a4c1ff8ca1ea/osfmk/man/vm_remap.html etc. I used Fable 5.1, and previous Claude models, a lot when asking questions about the kernel. I recommend doing the same._Py_RemoteDebug_ReadRemoteMemoryand_Py_RemoteDebug_PagedReadRemoteMemoryare increasingly leaky abstractions; the optimal way of reading depends on what (simply: datastacks have different volatility / are less stable than interpreters etc.), the operating system; this is visible more since gh-149584: Fix excessive overhead in the Tachyon profiler regarding the cache behavior #149649 We can reorganize this as a follow upThe Oracle redone with changes from #161674
_remote_debugging: reading whole pages over and over #149584