Skip to content

metal: prove cached uploads with page write tracking (draft, menu regression) - #513

Draft
RINNECODER wants to merge 2 commits into
mainfrom
perf/metal-texture-dirty
Draft

RINNECODER wants to merge 2 commits into
mainfrom
perf/metal-texture-dirty

Conversation

@RINNECODER

@RINNECODER RINNECODER commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

Draft. Do not merge: the menu capture regresses (see Blocker). #507 is merged; this is rebased onto main.

What

After every submission, wait or flip, the Metal image cache expires its entries. Each uploaded surface then re-proved itself by hashing all of its guest bytes, about 870 MiB per frame on the live-map capture. This PR proves uploaded surfaces with libc's page write watch instead. The direct-memory mappings are already registered with it.

  • An upload collects its surface's pages before the GPU reads them, and the cache entry keeps that generation.
  • The proof collects again and checks that no 64 KiB block of the surface was stamped since then (GuestImageWriteTracker.hpp). The stamps are shared, so overlapping surfaces that are collected separately still see each other's stores.
  • GPU writes from our own work still invalidate entries, as before. A surface the CPU writes goes back to hashing for 256 expiries.
  • Metal refuses a no-copy alias over pages of mixed protection. When that happens, the alias is retried under a host-write guard.
  • The replay registers its ranges with the watch, as the game's mappings are.
  • ANYPS5_METAL_IMAGE_TRACK=0 turns all of this off.

A/B (live-map capture, 10 interleaved pairs, A = #507, repeat 2)

A (#507) B (this)
worker instructions (M) 4283 (IQR 1.6%) 3462 (IQR 0.9%), -19%
worker CPU (ms) 482 410
wall (ms) 1326 1283
expect-hash pass 9/9 (one run timed out in warm-up) 8/10; both misses are the known 1-px flake at 508,770

Image-cache stats (GPU timing, 16 flips): hashed 13887 → 8170 MiB, with 409 tracked proofs and 24 tracked rejections.

Correctness

  • New rosetta_image_cache_tracking test (real watched pages and write faults). It checks that a 1-byte store, a guarded host write, memcpy and memset stores between submissions all force a re-upload, and that two caches over shared pages both see a store.
  • It fails 7 of 15 checks when tracked proofs ignore the tracker (mutation), and fails with APS5_NO_WRITE_WATCH=1. The existing rosetta_image_cache test still passes.
  • Live map: the frame is pixel-exact apart from the known flake.

Blocker

On the menu capture, timed repeat 2 is wrong in 134,964 pixels (max delta 183, on the 3D character). Later repeats are correct, and --expect-png against the last repeat passes. This is deterministic and does not happen with tracking off.

Bisected:

  • The cause is the write protection of a single 128 MiB surface. Protecting its pages without using the tracker for any proof (all proofs hashed) still breaks the frame.
  • The only CPU writer to those pages in that repeat is the replay's page-delta hook. The hook's bytes land correctly.
  • It is not: a tracked proof passing over changed bytes (checked every tracked proof against a hash), the alias retry, MAP_PRIVATE vs MAP_SHARED backing, or hook timing.
  • Standalone Metal tests show that GPU reads and writes through no-copy aliases stay coherent with CPU writes made via the fault handler.

Root cause unknown. Until it is found, page protection on GPU-aliased guest memory is not proven safe.

Codex review findings

Finding Severity Status
No chatgpt-codex-connector review after 10+ min (draft PRs appear to be skipped) - -

Depends on #507 (merged).

🤖 Generated with Claude Code

https://claude.ai/code/session_01DgW4n3qYgi5jAAohEmXRQj

RINNECODER and others added 2 commits October 15, 2026 01:39
After every submission, wait or flip the image cache expires its entries
and each uploaded surface re-proved itself with a hash of all its guest
bytes (about 870 MiB per frame on a live map scene). The Metal path now
uses libc's page write watch (the direct-memory mappings are already
registered): an upload collects its surface's pages before the GPU reads
them and keeps the generation; the proof collects again and checks that
no 64 KiB block of the surface was stamped since. Stamps are shared, so
overlapping surfaces collected separately still see each other's stores.

Exact for CPU stores of any size, including guarded host writes (file
reads). GPU writes from our own work keep invalidating entries as before.
A surface the CPU did write is hashed again for a while, so a texture
streamed every frame does not take a write fault per page per frame.
Ranges the watch does not cover are hashed as before.
ANYPS5_METAL_IMAGE_TRACK=0 turns it off.

Metal refuses a no-copy alias over pages of mixed protection. When the
refused range is watched, the alias is retried under a host-write guard,
which unprotects the pages and marks them written so no store is missed.
Existing aliases keep working when their pages are protected again.

The frame replay registers its mapped ranges with the watch, as the
game's mappings are, so its own stores reach the tracker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 15, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant