4 Commits

Author SHA1 Message Date
4442c5b646 runtime: restore kPromote=6/8, drop frame_hot over-copying, and wire region-batched MEM_WRITE_WATCH
- TitleWritePolicy (runtime/src/title_write_policy.h): restore kPromote = 6
  (of kHistory = 8) and cleanly remove the frame_hot / present_frame() tier
  and its dead fields/counters so 1/frame pages never trigger per-submission
  over-copying or copies on empty submissions. Keep O(dirty + hot + recent)
  active lists and sorted Guard::arm_batch() batching.
- HotColdTracking (runtime/src/title_mirror_hot_cold.h) & d3d12_vulkan_device.cpp:
  remove the dead on_present() / last_present_serial hook (coordinated with
  gate-async-fixer) and unused write_watched_* getters, and cache
  region-batched MEM_WRITE_WATCH runs (bridging verified MEM_COMMIT gaps <=
  1 MiB, capped at 64 MiB per GetWriteWatch call, never bridging uncommitted
  holes, with O(1) unordered_set dirty filtering and conservative dirty
  recording + split-retry on mid-run refusal so no journaled write is lost)
  so steady-state collect() runs in O(regions + dirty) without walking
  ww_pages_.
- TitleHeap (runtime/src/title_heap.h) & xmem.cpp: upgrade for_each_committed()
  to emit maximal coalesced committed extents (exact complement of free_runs_,
  O(free_runs_.size()) instead of scanning all 32k span slots, never spanning
  uncommitted holes) and wire MEM_WRITE_WATCH with silent MEM_RESERVE fallback
  + title_write_watch registration/marks in mirror_heap_ready() (never
  combined with MEM_LARGE_PAGES, MEM_PHYSICAL, or section views).
- Tests: update title_write_policy_test.cpp (kPromote = 6/8 and zero copies
  of 1/frame pages on empty submissions) and title_heap_test.cpp (coalesced
  for_each_committed() tracking around decommitted holes).

AI-assisted: Antigravity, Gemini 4 Neon
2026-11-27 11:44:26 -05:00
37fcb9f7c0 Runtime: two-tier mirror hot/cold policy, batched guards, fast fill & filter_pieces hook
- TitleWritePolicy (runtime/src/title_write_policy.h): lower submission-hot
  threshold (kPromote 6 -> 3 of 8), add O(active) present_frame() frame-hot
  tier (kFramePromote = 3 of 4, kFrameProbe = 16, unguarded on low-sub frames
  <= 4 subs/frame and guarded on high-sub frames so mid-frame CPU writes are
  never missed and 1-write/frame pages never copy 30x/frame), and sort/batch
  page arms via optional Guard::arm_batch().
- HotColdTracking (runtime/src/title_mirror_hot_cold.h): expose on_present(),
  coalesce contiguous 64 KiB VirtualProtect re-arms in CellGuard::arm_batch(),
  and route MEM_WRITE_WATCH pages through title_write_watch::ask() with zero
  guard faults or hot_tiles conflicts.
- TitleMirror (runtime/src/title_mirror.h): add optional TitleMirrorHooks::
  filter_pieces hook passing fill_scratch_ and the 256-bit changed-piece mask
  from fill() (eliminating duplicate 64 KiB guarded copies), and coalesce
  contiguous changed pieces into single WC-friendly memcpy bursts while
  preserving exact home.sums conflict detection.
- TitleHeap (runtime/src/title_heap.h): add portable reserve() helper for
  MEM_WRITE_WATCH with silent fallback and write_watched() tracking.
- Tests: add unit tests in title_write_policy_test.cpp,
  title_mirror_hooks_test.cpp, and title_heap_test.cpp.

AI-assisted: Antigravity (Gemini 4 Neon)
2026-11-27 10:52:32 -05:00
0ae59b2423 Fix TitleMirror write-back remap corruption and harden TitleHeap
- TitleMirror (runtime/src/title_mirror.h):
  - Add has_pending(va) and skip pages with unflushed pending_ binds in
    homes_in(), homes_in_ranges(), write_back(), and write_back_batch() so
    write-back never copies stale/unbound sparse chunk bindings over live CPU
    memory.
  - Pass each page's physical frame address (mapped->second + k * kPiece) to
    the write-back callback instead of its virtual address so concurrent CPU
    remaps cannot receive a previous mapping's bytes.
  - Replace the unconditional O(homes_.size()) refilter scan at the end of
    write_back() with an O(changed) local list of touched aliased homes.
  - Add HomeRef, homes_in_ranges(), and write_back_batch() for batched
    multi-range GPU readback with concurrent-remap frame validation.
  - Copy a full 64 KiB page in one hooks_.copy call inside fill() via a
    reusable member scratch buffer, falling back to 256-byte pieces only if
    the whole-page copy fails.
- TitleHeap (runtime/src/title_heap.h):
  - Add a per-span live-slot occupancy bitmap so release() rejects small-block
    double-frees without duplicating the freelist, and size_of()/release()
    reject freed slots, interior pointers, and unused span tails.
- Unit tests (title_mirror_hooks_test, title_heap_test):
  - Add regressions covering pending_ skip, physical-frame destination,
    O(changed) refilter scaling over 10k+ homes, 1-shot fill + per-piece
    fallback, batched multi-range coalescing/conflicts/remap skip, and
    TitleHeap double-free / interior-pointer / tail-slot rejection.

AI-assisted: Antigravity, Gemini 4 Neon
2026-11-27 09:36:10 -05:00
5cf7f89886 page mirror: the XMemAlloc heap in the mirror's range; pages written on both sides merged at the 256-byte piece (RT-PAGE-MIRROR)
The XMemAlloc heap (src/title_heap.h) is the runtime's own over a 32 GiB reservation that xmem places in the
mirror's range. It does not use RtlCreateHeap over a given base, which faults under Wine. Size classes of 64 bytes up
to 1 KiB, then powers of two up to 32 KiB, in 1 MiB spans. Larger blocks are runs of 64 KiB pages, committed when
handed out and decommitted when freed. Freed blocks are kept on lists outside the title's memory, and one lock covers
every call. Committed pages are fed to the mirror as they are committed, so a pointer the title writes into a block
reads the block's bytes without anything naming the range first. The title sees the same as before: 64-byte
alignment, size 0 as 1 byte, and XMemFree of a block from either entry point. When the range is full, blocks come
from the host's heap as before. With LEONIDA_TITLE_HEAP=0 they come from the CRT.

Pages written on both sides: each home keeps a 64-bit sum per 256-byte piece of what both sides last agreed on.
- An upload copies only the pieces the title changed, so a piece only the GPU wrote keeps the GPU's bytes.
- Write-back copies only the pieces the GPU changed while the title's piece still matches the sum.
- A piece both sides changed keeps the title's bytes and is counted.
The mirror's 10 s line gives uploaded and written-back KiB per submission and per frame, and the count of pieces
written on both sides.

Tests:
- title_heap: classes, large blocks, and 20000 random allocations and frees checked against a reference.
- d3d12_page_mirror part 7: one page, the GPU writes piece A and the title piece B in one window, and both survive.
- d3d12_page_mirror part 8: the GPU reads XMemAlloc blocks through pointers nothing named, and a freed large block
  reads zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 06:18:52 -05:00