runtime: keep the physical-page slab bookkeeping in a portable header and report slab counts #1749

Merged
tedin7 merged 2 commits from upstream-pr/1756 into main 2026-11-24 21:37:46 +00:00
tedin7 commented 2026-11-24 18:09:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T18:09:37Z.


Splits the slab bookkeeping of the physical pages out of xmem_mapped_pages.cpp into a header that needs no Windows, and reports it. Refs #1199 (item 2 stays open).

Changes

  • src/xmem_slab_slots.h (standard C++17 only): x2w_xmem::SlabSlots<Handle> keeps the slabs, how many slots of the newest slab were handed out, and an ordered set of released slots. Released slots are reused lowest first. A new slab of 4096 slots is opened through a callback; if the callback fails nothing changes, and if recording the slab throws the new slab is discarded and the exception continues. Counters: slabs, slots in use, slots free.
  • src/xmem_mapped_pages.cpp uses it. The Windows calls (CreateFileMappingW, MapViewOfFile) are unchanged and stay in this file. One difference: released slots are ordered by (slab creation order, offset) instead of (handle value, offset); the handle order was arbitrary, this one is deterministic.
  • x2w_xmem_page_stats is unchanged. New export x2w_xmem_slab_stats(slabs, in_use, free), listed in xmem_shim.def as @16.
  • tests/xmem_slab_slots_check.cpp (registered as xmem_slab_slots): a fake slab factory checks the 4096-slot boundary, lowest-first adjacent reuse, reuse preferring the older slab, freeing everything keeping the slabs open, a failing factory leaving state unchanged (first and second slab), unwinding a fresh slot (including slot 0 of a new slab), refused invalid releases, and exact counters throughout. tests/xmem_page_stats_check.py also checks the slab counters through grant, map, unmap, release and a page-token write.
  • docs/startup-runtime.md explains the units and updates the build/test snippet (xmem_slab_slots_check + xmem_slab_slots). The heartbeat label (sections) is left alone; the doc says what it counts now.

Not claimed

This does not reduce commit. A slab is still charged in full when it is created (default CreateFileMappingW flags, untouched) and closed only at process exit, so freeing pages returns slots, not commit. The new counters are the before/after units for later work.

Verified (Windows Server 2025, MSVC 14.44.35207, Debug & Release /O2)

  • Debug (--preset dev): built all 42 direct targets (xmem_shim, xmem_shim_test_controls, xmem_slab_slots_check, startup_stub_*, xgameruntime_*, cpu_*); ctest --preset dev 22/22 passed (30.31s), including xmem_slab_slots (0.03s), xmem_page_stats (0.08s), xmem_shim_mapped_pages (0.18s), xmem_shim_page_runs (9.92s), startup_heartbeat_commit (30.21s).
  • Release /O2 (--preset release): built all 42 direct targets; ctest --preset release 22/22 passed (30.28s), including xmem_slab_slots (0.02s), xmem_page_stats (0.10s), xmem_shim_mapped_pages (0.22s), xmem_shim_page_runs (9.74s), startup_heartbeat_commit (30.20s). Covers ctest --preset release -R "^(xmem_slab_slots|xmem_page_stats)$" -V.
  • Mutation checks on Windows:
    1. src/xmem_slab_slots.h (slots_in_use() ignoring free_.size()): killed by both xmem_slab_slots and xmem_page_stats ((1, 2, 4094) vs expected (1, 0, 4096)).
    2. src/xmem_mapped_pages.cpp (free_pages() skipping memory.slots.release(...)): killed by xmem_page_stats ((1, 2, 4094) vs expected (1, 0, 4096)).

Verified (Linux)

  • g++ -std=c++17 -O2 -Wall -Wextra -Wpedantic -Wconversion -fsanitize=address,undefined -Isrc tests/xmem_slab_slots_check.cpp: no warnings, passes.
  • git diff --check origin/main...HEAD, tests/docs_links_check.py, tests/bidi_controls_check.py: pass.
  • git merge-tree --write-tree origin/main HEAD: clean, no conflicts with main.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6588335775 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1756). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T18:09:37Z. --- Splits the slab bookkeeping of the physical pages out of `xmem_mapped_pages.cpp` into a header that needs no Windows, and reports it. Refs #1199 (item 2 stays open). ### Changes - `src/xmem_slab_slots.h` (standard C++17 only): `x2w_xmem::SlabSlots<Handle>` keeps the slabs, how many slots of the newest slab were handed out, and an ordered set of released slots. Released slots are reused lowest first. A new slab of 4096 slots is opened through a callback; if the callback fails nothing changes, and if recording the slab throws the new slab is discarded and the exception continues. Counters: slabs, slots in use, slots free. - `src/xmem_mapped_pages.cpp` uses it. The Windows calls (`CreateFileMappingW`, `MapViewOfFile`) are unchanged and stay in this file. One difference: released slots are ordered by (slab creation order, offset) instead of (handle value, offset); the handle order was arbitrary, this one is deterministic. - `x2w_xmem_page_stats` is unchanged. New export `x2w_xmem_slab_stats(slabs, in_use, free)`, listed in `xmem_shim.def` as @16. - `tests/xmem_slab_slots_check.cpp` (registered as `xmem_slab_slots`): a fake slab factory checks the 4096-slot boundary, lowest-first adjacent reuse, reuse preferring the older slab, freeing everything keeping the slabs open, a failing factory leaving state unchanged (first and second slab), unwinding a fresh slot (including slot 0 of a new slab), refused invalid releases, and exact counters throughout. `tests/xmem_page_stats_check.py` also checks the slab counters through grant, map, unmap, release and a page-token write. - `docs/startup-runtime.md` explains the units and updates the build/test snippet (`xmem_slab_slots_check` + `xmem_slab_slots`). The heartbeat label (`sections`) is left alone; the doc says what it counts now. ### Not claimed This does not reduce commit. A slab is still charged in full when it is created (default `CreateFileMappingW` flags, untouched) and closed only at process exit, so freeing pages returns slots, not commit. The new counters are the before/after units for later work. ### Verified (Windows Server 2025, MSVC 14.44.35207, Debug & Release `/O2`) - **Debug (`--preset dev`)**: built all 42 direct targets (`xmem_shim`, `xmem_shim_test_controls`, `xmem_slab_slots_check`, `startup_stub_*`, `xgameruntime_*`, `cpu_*`); `ctest --preset dev` **22/22 passed** (`30.31s`), including `xmem_slab_slots` (`0.03s`), `xmem_page_stats` (`0.08s`), `xmem_shim_mapped_pages` (`0.18s`), `xmem_shim_page_runs` (`9.92s`), `startup_heartbeat_commit` (`30.21s`). - **Release `/O2` (`--preset release`)**: built all 42 direct targets; `ctest --preset release` **22/22 passed** (`30.28s`), including `xmem_slab_slots` (`0.02s`), `xmem_page_stats` (`0.10s`), `xmem_shim_mapped_pages` (`0.22s`), `xmem_shim_page_runs` (`9.74s`), `startup_heartbeat_commit` (`30.20s`). Covers `ctest --preset release -R "^(xmem_slab_slots|xmem_page_stats)$" -V`. - **Mutation checks on Windows**: 1. `src/xmem_slab_slots.h` (`slots_in_use()` ignoring `free_.size()`): killed by both `xmem_slab_slots` and `xmem_page_stats` (`(1, 2, 4094)` vs expected `(1, 0, 4096)`). 2. `src/xmem_mapped_pages.cpp` (`free_pages()` skipping `memory.slots.release(...)`): killed by `xmem_page_stats` (`(1, 2, 4094)` vs expected `(1, 0, 4096)`). ### Verified (Linux) - `g++ -std=c++17 -O2 -Wall -Wextra -Wpedantic -Wconversion -fsanitize=address,undefined -Isrc tests/xmem_slab_slots_check.cpp`: no warnings, passes. - `git diff --check origin/main...HEAD`, `tests/docs_links_check.py`, `tests/bidi_controls_check.py`: pass. - `git merge-tree --write-tree origin/main HEAD`: clean, no conflicts with `main`. AI-assisted; commands listed were executed locally.
chatgpt-codex-connector[bot commented 2026-11-24 20:01:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T19:02:42Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T19:05:29.628305Z dca7a4a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7141513982 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1756#issuecomment-7141513982). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T19:02:42Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"dca7a4accc9ad6f7a0e9fdfb190e4209bca11f4b","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T19:05:29.628305Z">2026-11-24T19:05:29.628305Z</relative-time> | `dca7a4a` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
upstream-sync closed this pull request 2026-11-24 22:00:42 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1749
No description provided.