mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:18:37 +00:00
Merge pull request #441 from jujumoneylol/pr/title-memory-cap
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 in6cb16a4fa(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 main2a5ff9714together 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 main89d7dfcf5: 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 commit is contained in:
@ -239,7 +239,7 @@ Current limits, by design:
|
||||
### What `xgameruntime.dll` reports: environment overrides
|
||||
|
||||
The `xgameruntime` shim (`shims/xgameruntime.cpp`) answers the title's questions about the console and its package
|
||||
with fixed values. Five environment variables change an answer, for trying another of the title's code paths. Each
|
||||
with fixed values. Six environment variables change an answer, for trying another of the title's code paths. Each
|
||||
is read on every call, and a value that is empty or too long for its buffer is ignored.
|
||||
`tests/test_docs_env_vars.py` fails when the runtime or `make_game.py` reads a `LEONIDA_*` variable that no
|
||||
document names.
|
||||
@ -251,6 +251,7 @@ document names.
|
||||
| `LEONIDA_PACKAGE_ID` | `LeonidaPackageIdentifier00000000` | The identifier `XPackageGetCurrentProcessPackageIdentifier` returns (up to 32 characters). It is also the one identifier `XPackageMount` refuses with `E_ACCESSDENIED`, as the game's own package. |
|
||||
| `LEONIDA_CONTENT_PACKAGES` | unset | The content packages `XPackageMount` can mount: `id1=path1;id2=path2` (up to 1,023 characters). The mount path of a listed identifier is its path, as written; any other identifier is `ERROR_NOT_FOUND`, and a path longer than 259 characters is `ERROR_INSUFFICIENT_BUFFER`. |
|
||||
| `LEONIDA_PACKAGED` | `1` | `0`, or text that is not a number, makes `XPackageIsPackagedProcess` return false. Any other number keeps it true. |
|
||||
| `LEONIDA_TITLE_MEMORY_MB` | unset | Caps the title memory limit (in MiB, clamped to `2048..65536`) reported by both `XSystemGetAppMemoryUsageLimit` (`xgameruntime.cpp`) and `XMemGetWorkingSetStatistics` (`xmem.cpp`). Useful on 16 GB RAM / low-VRAM hosts to prevent oversized title pools; invalid or non-positive values are ignored and fall back to the host memory report. |
|
||||
|
||||
The two serializer functions and `D3D12XboxCreateDevice` are the only `d3d12_xs` imports used by the
|
||||
shipped DLLs (measured; see `docs/design.md` and `docs/d3d12_vulkan.md`). The game executable's own
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <condition_variable>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@ -703,8 +704,22 @@ HRESULT __stdcall XSystemGetConsoleId(size_t size, char* out, size_t* used) {
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
// uint64_t XSystemGetAppMemoryUsageLimit(). Reports total physical RAM, or 12 GB fallback for Series X.
|
||||
// uint64_t XSystemGetAppMemoryUsageLimit(). Reports total physical RAM (or 12 GB fallback for Series X),
|
||||
// optionally overridden by LEONIDA_TITLE_MEMORY_MB (clamped to 2048..65536 MiB) so constrained hosts can prevent
|
||||
// the title from sizing pools above what the machine can hold.
|
||||
uint64_t __stdcall XSystemGetAppMemoryUsageLimit() {
|
||||
char env[32]{};
|
||||
const DWORD n = GetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", env, sizeof(env));
|
||||
if (n > 0 && n < sizeof(env) && env[0] != '-' && env[0] != '+') {
|
||||
errno = 0;
|
||||
char* end = nullptr;
|
||||
unsigned long long mib = std::strtoull(env, &end, 10);
|
||||
if (errno == 0 && end != env && *end == '\0' && mib > 0) {
|
||||
if (mib < 2048ULL) mib = 2048ULL;
|
||||
if (mib > 65536ULL) mib = 65536ULL;
|
||||
return static_cast<uint64_t>(mib) * 1024ULL * 1024ULL;
|
||||
}
|
||||
}
|
||||
MEMORYSTATUSEX ms{sizeof(ms)};
|
||||
if (GlobalMemoryStatusEx(&ms)) return ms.ullTotalPhys;
|
||||
return 12ULL * 1024 * 1024 * 1024;
|
||||
|
||||
@ -19,8 +19,10 @@
|
||||
#include <psapi.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
@ -159,6 +161,19 @@ BOOL __stdcall XMemGetWorkingSetStatistics(uint32_t /*kind*/, void* stats) {
|
||||
if (GlobalMemoryStatusEx(&ms) && ms.ullTotalPhys != 0) {
|
||||
total_memory = ms.ullTotalPhys;
|
||||
}
|
||||
// Optional override (matching XSystemGetAppMemoryUsageLimit) so memory-constrained hosts can cap title pools.
|
||||
char env[32]{};
|
||||
const DWORD env_n = GetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", env, sizeof(env));
|
||||
if (env_n > 0 && env_n < sizeof(env) && env[0] != '-' && env[0] != '+') {
|
||||
errno = 0;
|
||||
char* end = nullptr;
|
||||
unsigned long long mib = std::strtoull(env, &end, 10);
|
||||
if (errno == 0 && end != env && *end == '\0' && mib > 0) {
|
||||
if (mib < 2048ULL) mib = 2048ULL;
|
||||
if (mib > 65536ULL) mib = 65536ULL;
|
||||
total_memory = static_cast<uint64_t>(mib) * 1024ULL * 1024ULL;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t* bytes = static_cast<uint8_t*>(stats);
|
||||
std::memset(bytes, 0, 16);
|
||||
|
||||
@ -1095,6 +1095,37 @@ int main() {
|
||||
check(is_packaged() == false, "XPackageIsPackagedProcess respects LEONIDA_PACKAGED=0 override");
|
||||
SetEnvironmentVariableA("LEONIDA_PACKAGED", nullptr);
|
||||
|
||||
using AppMemoryLimitFn = uint64_t(__stdcall*)();
|
||||
auto app_mem_limit = get<AppMemoryLimitFn>("XSystemGetAppMemoryUsageLimit");
|
||||
check(app_mem_limit != nullptr, "XSystemGetAppMemoryUsageLimit is registered");
|
||||
if (app_mem_limit) {
|
||||
char saved_env[64]{};
|
||||
const DWORD saved_n = GetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", saved_env, sizeof(saved_env));
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", nullptr);
|
||||
MEMORYSTATUSEX ms{sizeof(ms)};
|
||||
const uint64_t expected_default = GlobalMemoryStatusEx(&ms) ? ms.ullTotalPhys : (12ULL << 30);
|
||||
check(app_mem_limit() == expected_default, "XSystemGetAppMemoryUsageLimit reports host RAM by default");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "6144");
|
||||
check(app_mem_limit() == 6144ULL * 1024ULL * 1024ULL,
|
||||
"XSystemGetAppMemoryUsageLimit honors LEONIDA_TITLE_MEMORY_MB=6144");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "512");
|
||||
check(app_mem_limit() == 2048ULL * 1024ULL * 1024ULL,
|
||||
"XSystemGetAppMemoryUsageLimit clamps LEONIDA_TITLE_MEMORY_MB below 2048 MiB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "131072");
|
||||
check(app_mem_limit() == 65536ULL * 1024ULL * 1024ULL,
|
||||
"XSystemGetAppMemoryUsageLimit clamps LEONIDA_TITLE_MEMORY_MB above 65536 MiB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "notanumber");
|
||||
check(app_mem_limit() == expected_default,
|
||||
"XSystemGetAppMemoryUsageLimit ignores invalid LEONIDA_TITLE_MEMORY_MB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "6144mb");
|
||||
check(app_mem_limit() == expected_default,
|
||||
"XSystemGetAppMemoryUsageLimit ignores LEONIDA_TITLE_MEMORY_MB with trailing text");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "-4096");
|
||||
check(app_mem_limit() == expected_default,
|
||||
"XSystemGetAppMemoryUsageLimit ignores negative LEONIDA_TITLE_MEMORY_MB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", (saved_n > 0 && saved_n < sizeof(saved_env)) ? saved_env : nullptr);
|
||||
}
|
||||
|
||||
// Work on the thread pool, completions dispatched by the "game loop" on this thread.
|
||||
auto create = get<QueueCreateFn>("XTaskQueueCreate");
|
||||
auto close = get<QueueCloseFn>("XTaskQueueCloseHandle");
|
||||
|
||||
@ -98,6 +98,10 @@ void test_alloc(XMemAllocFn alloc, XMemFreeFn free_mem) {
|
||||
}
|
||||
|
||||
void test_working_set(XMemGetWorkingSetStatisticsFn get_stats) {
|
||||
char saved_env[64]{};
|
||||
const DWORD saved_n = GetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", saved_env, sizeof(saved_env));
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", nullptr);
|
||||
|
||||
alignas(64) uint8_t stats[64];
|
||||
std::memset(stats, 0xa5, sizeof(stats));
|
||||
// The pointer is the second argument: the first (0x20000001 here, as the FSR DLL passes it) must never be written
|
||||
@ -134,6 +138,31 @@ void test_working_set(XMemGetWorkingSetStatisticsFn get_stats) {
|
||||
}
|
||||
check(ok != FALSE && tail_untouched, "XMemGetWorkingSetStatistics leaves bytes 16..63 untouched");
|
||||
|
||||
// LEONIDA_TITLE_MEMORY_MB override and clamping (matches XSystemGetAppMemoryUsageLimit).
|
||||
auto query_total = [&]() -> uint64_t {
|
||||
alignas(64) uint8_t buf[64]{};
|
||||
if (!get_stats(kMeasuredKind, buf)) return 0;
|
||||
uint64_t out = 0;
|
||||
std::memcpy(&out, buf, sizeof(out));
|
||||
return out;
|
||||
};
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "6144");
|
||||
check(query_total() == 6144ULL * 1024ULL * 1024ULL,
|
||||
"XMemGetWorkingSetStatistics honors LEONIDA_TITLE_MEMORY_MB=6144");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "512");
|
||||
check(query_total() == 2048ULL * 1024ULL * 1024ULL,
|
||||
"XMemGetWorkingSetStatistics clamps LEONIDA_TITLE_MEMORY_MB below 2048 MiB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "131072");
|
||||
check(query_total() == 65536ULL * 1024ULL * 1024ULL,
|
||||
"XMemGetWorkingSetStatistics clamps LEONIDA_TITLE_MEMORY_MB above 65536 MiB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "invalid");
|
||||
check(query_total() == total_memory,
|
||||
"XMemGetWorkingSetStatistics ignores invalid LEONIDA_TITLE_MEMORY_MB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", "-4096");
|
||||
check(query_total() == total_memory,
|
||||
"XMemGetWorkingSetStatistics ignores negative LEONIDA_TITLE_MEMORY_MB");
|
||||
SetEnvironmentVariableA("LEONIDA_TITLE_MEMORY_MB", (saved_n > 0 && saved_n < sizeof(saved_env)) ? saved_env : nullptr);
|
||||
|
||||
check(get_stats(kMeasuredKind, nullptr) == FALSE,
|
||||
"XMemGetWorkingSetStatistics(kind, nullptr) returns FALSE without crashing");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user