runtime: honor LEONIDA_TITLE_MEMORY_MB in XSystemGetAppMemoryUsageLimit and XMemGetWorkingSetStatistics (RT-TITLE-MEMORY-CAP) #441

Manually merged
github merged 2 commits from gh-pr/441/head into gh-pr/441/base 2026-11-26 09:00:23 +00:00
Owner

@⁠jujumoneylol on GitHub · 2026-11-24 05:22 UTC · original
jujumoneylol:pr/title-memory-cap → main · +93 −2 · 5 files · 2 commits · merged 2026-11-24 22:45 UTC

Claimed in the coordination issue as: RT-TITLE-MEMORY-CAP (#14)

What and why

Implements @⁠KoLolX3's finding in #14: both XSystemGetAppMemoryUsageLimit (runtime/shims/xgameruntime.cpp) and XMemGetWorkingSetStatistics (runtime/shims/xmem.cpp) previously always reported total host physical RAM (ullTotalPhys), causing memory-constrained hosts (16 GB systems or cgroup-capped runs) to tell the title it has more memory than it can safely allocate.

  • Honors LEONIDA_TITLE_MEMORY_MB in both XSystemGetAppMemoryUsageLimit() and XMemGetWorkingSetStatistics() (total memory at +0).
  • Clamps valid positive integer values to 2048..65536 MiB (2 GiB..64 GiB).
  • Strictly ignores unset, empty, negative, overflowing, non-numeric, or trailing-garbage values (falling back to GlobalMemoryStatusEx().ullTotalPhys / 12 GiB fallback unchanged).
  • Adds unit tests for default, valid override (6144), lower/upper clamping (512 → 2048, 131072 → 65536), and malformed values in runtime/test/xgameruntime_test.cpp and runtime/test/xmem_test.cpp.

Tested

  • Built xgameruntime_test and xmem_test with MSVC 19.44 (vcvars64) + Ninja on top of main 8f481cf16.
  • xgameruntime_test: OK (166 checks).
  • xmem_test: OK (222 checks).
  • py -3.13 -I scripts/check_no_game_data.py runtime/shims/xgameruntime.cpp runtime/shims/xmem.cpp runtime/test/xgameruntime_test.cpp runtime/test/xmem_test.cpp: clean.

GPUs tested: n/a (CPU-only runtime shim & unit tests on Windows 11 26200)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — runtime shim only)
**@⁠jujumoneylol** on GitHub · 2026-11-24 05:22 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/441) `jujumoneylol:pr/title-memory-cap` → `main` · +93 −2 · 5 files · 2 commits · merged 2026-11-24 22:45 UTC Claimed in the coordination issue as: RT-TITLE-MEMORY-CAP (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Implements @⁠KoLolX3's finding in #14: both `XSystemGetAppMemoryUsageLimit` (`runtime/shims/xgameruntime.cpp`) and `XMemGetWorkingSetStatistics` (`runtime/shims/xmem.cpp`) previously always reported total host physical RAM (`ullTotalPhys`), causing memory-constrained hosts (16 GB systems or cgroup-capped runs) to tell the title it has more memory than it can safely allocate. - Honors `LEONIDA_TITLE_MEMORY_MB` in both `XSystemGetAppMemoryUsageLimit()` and `XMemGetWorkingSetStatistics()` (total memory at `+0`). - Clamps valid positive integer values to `2048..65536` MiB (`2 GiB..64 GiB`). - Strictly ignores unset, empty, negative, overflowing, non-numeric, or trailing-garbage values (falling back to `GlobalMemoryStatusEx().ullTotalPhys` / 12 GiB fallback unchanged). - Adds unit tests for default, valid override (`6144`), lower/upper clamping (`512` → `2048`, `131072` → `65536`), and malformed values in `runtime/test/xgameruntime_test.cpp` and `runtime/test/xmem_test.cpp`. ### Tested - Built `xgameruntime_test` and `xmem_test` with MSVC 19.44 (`vcvars64`) + Ninja on top of `main` `8f481cf16`. - `xgameruntime_test`: OK (166 checks). - `xmem_test`: OK (222 checks). - `py -3.13 -I scripts/check_no_game_data.py runtime/shims/xgameruntime.cpp runtime/shims/xmem.cpp runtime/test/xgameruntime_test.cpp runtime/test/xmem_test.cpp`: clean. GPUs tested: n/a (CPU-only runtime shim & unit tests on Windows 11 26200) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — runtime shim only)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:00:22 +00:00
github manually merged commit 6cb16a4fa8 into gh-pr/441/base 2026-11-26 09:00:23 +00:00
Author
Owner

@⁠Kokonotcu on GitHub · 2026-11-24 05:38 UTC · original

Same topic from our side (not opening a second PR): the override is useful, but the default still tells the title it owns all host RAM, so an untuned 16 GB host still oversizes its pools. What we run locally for both functions is one value computed once:

  • the Series X title budget, 13.5 GiB,
  • capped at host RAM minus max(4 GiB, a quarter of it),
  • never below 2 GiB,
  • LEONIDA_TITLE_MEMORY_MB overrides it,
  • 12 GiB if the host gives no answer.

That gives 13.5 GiB on 32/64 GiB hosts, 12 GiB on 16, 4 GiB on 8. It also makes the two functions agree when GlobalMemoryStatusEx fails (today XMem falls back to 12 GiB while XSystemGetAppMemoryUsageLimit returns 0).

If you want it on top of this PR: github.com/Kokonotcu/leonida@c88d29c82 (shims/title_memory.h, budget_for(physical, override_mb) plus host-size tests in xmem_test). Happy to rebase it onto #441 once this lands.

🤖 Generated with Claude Code

**@⁠Kokonotcu** on GitHub · 2026-11-24 05:38 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/441#issuecomment-7123130945) Same topic from our side (not opening a second PR): the override is useful, but the default still tells the title it owns all host RAM, so an untuned 16 GB host still oversizes its pools. What we run locally for both functions is one value computed once: - the Series X title budget, 13.5 GiB, - capped at host RAM minus max(4 GiB, a quarter of it), - never below 2 GiB, - `LEONIDA_TITLE_MEMORY_MB` overrides it, - 12 GiB if the host gives no answer. That gives 13.5 GiB on 32/64 GiB hosts, 12 GiB on 16, 4 GiB on 8. It also makes the two functions agree when `GlobalMemoryStatusEx` fails (today `XMem` falls back to 12 GiB while `XSystemGetAppMemoryUsageLimit` returns 0). If you want it on top of this PR: https://github.com/Kokonotcu/leonida/commit/c88d29c82 (`shims/title_memory.h`, `budget_for(physical, override_mb)` plus host-size tests in `xmem_test`). Happy to rebase it onto #441 once this lands. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠jujumoneylol on GitHub · 2026-11-24 05:44 UTC · original

Thanks @⁠Kokonotcu — really appreciate you coordinating instead of duplicating! Keeping #441 focused on the opt-in LEONIDA_TITLE_MEMORY_MB override keeps it small and easy to review, and rebasing your Series X default budget + GlobalMemoryStatusEx fallback alignment on top once #441 lands sounds great.

**@⁠jujumoneylol** on GitHub · 2026-11-24 05:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/441#issuecomment-7123222896) Thanks @⁠Kokonotcu — really appreciate you coordinating instead of duplicating! Keeping #441 focused on the opt-in `LEONIDA_TITLE_MEMORY_MB` override keeps it small and easy to review, and rebasing your Series X default budget + `GlobalMemoryStatusEx` fallback alignment on top once #441 lands sounds great.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 08:54 UTC · original

Tested on Windows 11 (VS 2022, main 1c93c86): the xgameruntime/xmem ctests pass (8/8) and the new tests fail without the change, so the code looks right. One thing blocks it: tests/test_docs_env_vars.py::test_every_variable_is_named_in_a_document fails, because LEONIDA_TITLE_MEMORY_MB isn't named in any .md. A line in the docs (e.g. where the other runtime env vars are listed) will fix it; happy to re-test and merge after that.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-24 08:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/441#issuecomment-7126710007) Tested on Windows 11 (VS 2022, main 1c93c86): the xgameruntime/xmem ctests pass (8/8) and the new tests fail without the change, so the code looks right. One thing blocks it: `tests/test_docs_env_vars.py::test_every_variable_is_named_in_a_document` fails, because `LEONIDA_TITLE_MEMORY_MB` isn't named in any .md. A line in the docs (e.g. where the other runtime env vars are listed) will fix it; happy to re-test and merge after that. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠jujumoneylol on GitHub · 2026-11-24 21:51 UTC · original

Thanks for testing and catching that, @⁠QemmHD!

Added LEONIDA_TITLE_MEMORY_MB to the environment-overrides table in runtime/README.md in 6cb16a4fa and verified tests/test_docs_env_vars.py (4/4 tests) and scripts/check_no_game_data.py both pass. Ready for re-test/merge!

AI-assisted doc update and check.

**@⁠jujumoneylol** on GitHub · 2026-11-24 21:51 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/441#issuecomment-7145486053) Thanks for testing and catching that, @⁠QemmHD! Added `LEONIDA_TITLE_MEMORY_MB` to the environment-overrides table in `runtime/README.md` in `6cb16a4fa` and verified `tests/test_docs_env_vars.py` (4/4 tests) and `scripts/check_no_game_data.py` both pass. Ready for re-test/merge! AI-assisted doc update and check.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 22:45 UTC · original

Merging.

Why merged: RT-TITLE-MEMORY-CAP: LEONIDA_TITLE_MEMORY_MB (MiB, clamped to 2048..65536) overrides what XSystemGetAppMemoryUsageLimit and XMemGetWorkingSetStatistics report. I held this earlier for one reason: the env var wasn't documented, so test_docs_env_vars failed. The author fixed that in 6cb16a4fa (runtime/README.md overrides table). Kokonotcu's idea of a different default budget was accepted as a follow-up.

Why it's good: the env read is bounded: GetEnvironmentVariableA into a 32-byte buffer, then strtoull, rejecting a sign, trailing text, 0 and overflow. Tested on Windows 11 / RTX 4070 SUPER, VS 2022 + Ninja RelWithDebInfo, merged onto main 2a5ff9714 together with #992:

  • the repo-consistency set (now including test_ctest_registration) gives 578 passed / 5 skipped / 0 failed;
  • docs tests give 47 passed, including test_docs_env_vars;
  • full ctest gives 316/317, where the only failure is main's pre-existing d3d12_depth_resolve, and xmem and xgameruntime pass;
  • d3d12_* tests under validation give 119/120 with 0 VUIDs.

git merge-tree against current main 89d7dfcf5: no conflicts.

Small follow-ups, not blockers: the README row says "Caps", but a value above host RAM raises the reported limit (up to 64 GiB). The parsing is duplicated in xgameruntime.cpp and xmem.cpp, so a shared helper would keep them in sync. Separately, the xmem check "mapping them does not charge them" measures system-wide commit charge, so it can flake on a heavily loaded machine. It passed 120/120 repeats on an idle one, and that's unrelated to this PR.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-24 22:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/441#issuecomment-7146617965) Merging. **Why merged:** RT-TITLE-MEMORY-CAP: `LEONIDA_TITLE_MEMORY_MB` (MiB, clamped to 2048..65536) overrides what `XSystemGetAppMemoryUsageLimit` and `XMemGetWorkingSetStatistics` report. I held this earlier for one reason: the env var wasn't documented, so `test_docs_env_vars` failed. The author fixed that in 6cb16a4fa (runtime/README.md overrides table). Kokonotcu's idea of a different default budget was accepted as a follow-up. **Why it's good:** the env read is bounded: `GetEnvironmentVariableA` into a 32-byte buffer, then `strtoull`, rejecting a sign, trailing text, 0 and overflow. Tested on Windows 11 / RTX 4070 SUPER, VS 2022 + Ninja RelWithDebInfo, merged onto main 2a5ff9714 together with #992: - the repo-consistency set (now including test_ctest_registration) gives 578 passed / 5 skipped / 0 failed; - docs tests give 47 passed, including test_docs_env_vars; - full ctest gives 316/317, where the only failure is main's pre-existing `d3d12_depth_resolve`, and xmem and xgameruntime pass; - `d3d12_*` tests under validation give 119/120 with 0 VUIDs. git merge-tree against current main 89d7dfcf5: no conflicts. Small follow-ups, not blockers: the README row says "Caps", but a value above host RAM raises the reported limit (up to 64 GiB). The parsing is duplicated in xgameruntime.cpp and xmem.cpp, so a shared helper would keep them in sync. Separately, the xmem check "mapping them does not charge them" measures system-wide commit charge, so it can flake on a heavily loaded machine. It passed 120/120 repeats on an idle one, and that's unrelated to this PR. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.