Skip to content

feat(libc): track guest memory with windows write-watch tokens - #13784

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Celegans12:feat/libc-owned-memory-blocks
Nov 4, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Celegans12:feat/libc-owned-memory-blocks

Conversation

@Celegans12

@Celegans12 Celegans12 commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What

libc has no host-side token for checking whether cached guest bytes changed. Add GuestArenaArmOwned_nid_no_patch and GuestArenaOwnedUnchanged_nid_no_patch over whole 64 KiB blocks, using Windows GetWriteWatch.

Arming preserves consumed dirty pages for collection and eviction. Mapping changes, pins, host access, protection notifications and clearing collections invalidate tokens under the mapping mutex. Guest page protection stays unchanged. Linux and other unsupported platforms return false; private memory requires flag 1. Pending and newly reported pages are merged using a sorted lookup before collection returns them. The token and pending maps are capped independently at 65,536 blocks. Token pressure invalidates lowest-address proofs; pending pressure refuses new blocks before resetting their writes. Existing pending blocks can still be armed. The byte-only contract and unmeasured title impact are recorded in libc debt.

This rebase incorporates merged #13220's standalone write-watch registration fix, which addresses the inherited configure failure in the old macOS jobs. Token behavior is unchanged. Driver consumers, GPU stamps and epochs remain separate; no title performance claim. #11552 and #6926 supply different APIs. Adjacent libc debt entries from #12184/#12849 must be retained together. The current overlap scan left 20 shared-file comparisons unresolved. #10025 adds zero-page mapping and must preserve token invalidation; #5021 changes test registration. The other unresolved changes concern independent platform, registration or debt work.

Tested

Based on main 66e1f7f. Windows 11, MinGW-w64 GCC 15.2.0, Release, -DBUILD_TESTING=ON: eight mapping tests passed three times, including guest_owned_memory_limits. Its eight-entry fixture checks pending saturation, refusal before reset, reuse after collection and token eviction; removing either bound made it fail. A synthetic 512-block workload reproduced unbounded growth on the previous head. A separate 8,192-page collection improved from 6.1–6.4 ms to 0.35–0.37 ms before the rebase.

A current-main merge with the dependent draw and storage changes passed 10 selected Windows tests and seven Linux tests three times each. Linux does not exercise Windows write-watch. Earlier Ubuntu 24.04/GCC 13.3 host tests passed three times; unsupported platforms refuse ownership. Not tested: macOS, titles, console behavior or title performance. These synthetic results do not establish frame impact.

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: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 12184 docs/dev/debt/libc.md L8
PR 12849 docs/dev/debt/libc.md L8

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1835 core/libs/prx/libc/src/GuestArena.cpp
PR 5021 core/libs/prx/libc/CMakeLists.txt
core/libs/prx/libc/src/GuestArena.cpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6819 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7081 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 7715 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8150 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 8965 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9929 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 10025 core/libs/prx/libc/include/GuestArena.hpp
core/libs/prx/libc/include/WindowsMappings.hpp
core/libs/prx/libc/src/GuestArena.cpp
PR 10113 docs/dev/debt/libc.md
PR 10226 docs/dev/debt/libc.md
PR 10245 docs/dev/debt/libc.md
PR 10250 docs/dev/debt/libc.md
PR 10253 docs/dev/debt/libc.md
PR 10265 docs/dev/debt/libc.md
PR 10274 docs/dev/debt/libc.md
PR 10325 docs/dev/debt/libc.md
PR 10328 docs/dev/debt/libc.md
PR 10345 docs/dev/debt/libc.md
PR 10349 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10506 docs/dev/debt/libc.md
PR 10510 docs/dev/debt/libc.md
PR 10512 docs/dev/debt/libc.md
PR 10565 docs/dev/debt/libc.md
PR 10987 docs/dev/debt/libc.md
PR 11260 docs/dev/debt/libc.md
PR 11262 docs/dev/debt/libc.md
PR 11266 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11632 docs/dev/debt/libc.md
PR 11634 docs/dev/debt/libc.md
PR 11724 docs/dev/debt/libc.md
PR 11726 docs/dev/debt/libc.md
PR 11831 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 11907 docs/dev/debt/libc.md
PR 12122 docs/dev/debt/libc.md
PR 12397 docs/dev/debt/libc.md
PR 12514 docs/dev/debt/libc.md
PR 12560 docs/dev/debt/libc.md
PR 12574 docs/dev/debt/libc.md
PR 12593 docs/dev/debt/libc.md
PR 12622 docs/dev/debt/libc.md
PR 12627 docs/dev/debt/libc.md
PR 12682 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12688 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12700 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 12965 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 13119 docs/dev/debt/libc.md
PR 13390 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 13804 docs/dev/debt/libc.md
PR 14212 docs/dev/debt/libc.md
PR 14585 core/libs/prx/libc/CMakeLists.txt
docs/dev/debt/libc.md
PR 14610 docs/dev/debt/libc.md

@CnRJay CnRJay left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correction (2026-11-01): this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows Unblocks: none; a title is only required when the PR fixes a title. The technical points below are unaffected.

Three things before this can be judged.

  1. No title ID and no consumer. "Unblocks: none", "no performance claim", and the only caller is the stacked #13793 (and later #13809). A new libc export pair with no title reaching it on main is the export-only shape the maintainers close. If the point is to make draw-input copies skippable, the evidence has to be on the consumer PR (trace_digest of main vs change, same scene), and this one should say which title/scene makes GuestArenaArmOwned hot.

  2. Linux gets nothing. "Linux native tokens are unsupported", so on Linux both functions return false and the consumer always falls back to the copy. Everything the PR adds is a Windows-only code path (GetWriteWatch), plus the same two exports being callable on every platform. Please state that in the PR title/What so nobody reads it as a general facility.

  3. Cost and growth in WindowsMappings.hpp:

  • ArmOwned and OwnedUnchanged take the mapping mutex, the same lock the map/unmap/protect paths use, and call GetWriteWatch (a syscall) once per 64 KiB block per call. For a draw that touches many blocks this is a syscall per block per draw on a lock shared with the allocator. Is there a number for how many blocks and calls per frame in a real scene?
  • collectOwnedWrites runs std::find(pages, pages + *count, pointer) for every pending bit, so merging pending pages into the result is O(pending x reported) inside the collection path that previously was a single GetWriteWatch. With a large dirty set that is quadratic work under the mapping mutex.
  • ownedWords / ownedPending only shrink on disown/unmap/pin. A title that arms many distinct blocks and never unmaps them keeps one map node per 64 KiB block forever. What bounds it?
  • nextOwned++ hands out tokens from a 64-bit counter; fine, but ArmOwned fails once count > UINT64_MAX - nextOwned + 1, and that failure is silent to the caller (just false).

Please add a counter or a trace phase for arm/check/refuse so the reuse rate can be read in a trace_digest, otherwise nobody can tell whether the feature fires.

@Celegans12
Celegans12 force-pushed the feat/libc-owned-memory-blocks branch from adfa2c9 to 3a3c1ca Compare November 1, 2026 23:39
@Celegans12 Celegans12 changed the title feat(libc): track guest memory with write-watch tokens feat(libc): track guest memory with windows write-watch tokens Nov 1, 2026
@Celegans12

Celegans12 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor Author

@CnRJay I investigated the pending-page merge you called out and changed it in 3a3c1ca4f3 to sort the reported pages once and look up pending pages in that sorted set. In a synthetic Windows run with 8,192 overlapping pending and reported pages, collection fell from about 6.2 ms to 0.36 ms; I haven't measured a title or frame.

I also made the Windows-only token support explicit in the title and description. The updated head passed the seven Windows mapping and write-watch tests three times each, including new overlapping and disjoint page checks with partial collection. I also measured the per-block calls in a synthetic private-memory run: arming 64 blocks averaged about 204 µs and checking them about 21 µs. I still need a title trace and the requested reuse counters to say what that costs in practice.

There is no fixed entry cap on the two maps: token entries can remain for armed blocks until invalidated, and pending dirty-page bits remain until collection or removal. Their growth follows the blocks involved, so long-lived mappings can retain entries; I haven't established a title-level bound. Token exhaustion returns false and stops new arms rather than wrapping; there is currently no separate diagnostic for it.

Would you take a look at the collection change when you have a chance? I'm continuing with the remaining concerns and the dependent branches.

Measurement

Before rebasing, I allocated watched private memory, dirtied each page, armed the blocks, dirtied the pages again and collected without reset, checking every page was returned once in order. Three runs at 8,192 pages took 6.116–6.354 ms before and 0.354–0.373 ms after. This measures collection in a synthetic all-overlap workload. In a separate clean private-memory workload, three batches of 100 arms and checks averaged 1.63–1.66 / 0.27 µs for one block and 203.96–204.23 / 20.73–20.76 µs for 64 blocks. Each operation currently calls GetWriteWatch once per block.

Expose internal proofs for fully writable, write-watched guest blocks.
Keep mapping changes, pins, host accesses and collect resets serialized
with token queries, without changing guest page protection.
Retain writes consumed during arming for later collection and eviction.
Refuse unsupported mappings and platforms conservatively; defer driver
consumers, GPU write stamps and epoch caching to subsequent changes.
Cover private and resident lifetimes, aliases and concurrent collection.

AI-assisted: yes
@Celegans12
Celegans12 force-pushed the feat/libc-owned-memory-blocks branch from 3a3c1ca to 4b26f5d Compare November 2, 2026 02:24
@Celegans12

Copy link
Copy Markdown
Contributor Author

@CnRJay I addressed the unbounded maps in 4b26f5d61b: each map is limited to 65,536 blocks. When pending pages fill the limit, a new block is refused before its write watch is reset; existing pending blocks can still be armed. Token pressure invalidates the lowest-address proofs, so callers fall back to copying. This policy may affect reuse under pressure; I haven't measured it in a title.

A standalone Windows fixture with an eight-entry limit covers saturation, mixed existing/new ranges, preserved dirty pages, collection, and token eviction. It and seven related mapping tests passed three times; disabling either limit made the fixture fail. A current-main merge of the full stack passed 10 selected Windows tests and seven Linux tests three times each. Linux cannot exercise the Windows write-watch implementation.

I also restacked the draw and storage follow-ups. Their trace counters report requested blocks and outcomes, but not a saturation reason or measured per-frame cost. Would this bound and fallback policy address your growth concern?

@oneandonlydean oneandonlydean self-assigned this Nov 4, 2026
@oneandonlydean
oneandonlydean merged commit 792f118 into boykopovar:main Nov 4, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants