From 07fb0e7113fc4eb2f2a0a03bebef2ca10e6ebcf4 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 2 Nov 2026 02:15:34 +0100 Subject: [PATCH] Arena RAM fallback: budget form with an 8 GiB headroom, and the Windows copy trims the mapped pages The fallback's RAM copy (previous commit) now uses the --resident-budget-gib form: the budget is what is available minus the headroom (default 8 GiB, STRATA_RESIDENT_HEADROOM_GIB changes it), so the experts outside the copy are read unbuffered instead of through the mapped view whose pages grow the working set until available RAM is near zero. On Windows the complement copy releases the mapped pages to the standby list every 4 layers (the VirtualUnlock call that already ran once after the copy): available RAM fell to 0.9 GiB for ~10 s during the copy on a 64 GB PC with 44.6 GiB available. No data changes. Measured on the RTX 5070 PC (128K, 44.6 GiB available): lowest available RAM 4.05 GiB, 2,000-token answer 25.2 tok/s (9.4-10.1 without the fix). --- src/core/expert_source.cpp | 8 ++++++++ src/program/generate.cpp | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index cc479a1d57..8039dc2da9 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -4110,6 +4110,14 @@ bool FileExpertSource::pin_cache_complement( } #endif const int64_t done = layers_done.fetch_add(1) + 1; +#if defined(_WIN32) + // R5070: the copy reads through the mapped view, and Windows counts those file pages in this process's working + // set, not as available: on the owner's 64 GB PC (8 GiB headroom asked for) available RAM fell to 0.9 GiB for + // ~10 s during the copy. Every 4 layers the mapped pages go to the standby list (VirtualUnlock on pages that + // are not locked does exactly that; the same call runs once after the copy below). No data changes. + if (done % 4 == 0 && direct_.empty() && maps_.empty() && !unmapped_) (void) VirtualUnlock((LPVOID) base_, (SIZE_T) mapped_bytes_); + if (done % 4 == 0 && direct_.empty()) for (const Map& m : maps_) if (!m.closed) (void) VirtualUnlock((LPVOID) m.base, (SIZE_T) m.bytes); +#endif if (done % 8 == 0 || done == n_layers_) std::fprintf(stderr, "FileExpertSource: copied cache complement through layer %lld/%lld (%.2f GiB, %.0f s)\n", (long long) done, (long long) n_layers_, (double) copied.load() / 1073741824.0, diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 791e0ed134..8f98b936b5 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -6825,7 +6825,7 @@ int main(int argc, char** argv) { const bool ram_fallback_resident_off = [] { const char* v = std::getenv("STRATA_ARENA_FALLBACK_RESIDENT"); return v != nullptr && v[0] == '0'; }(); const uint64_t ram_fallback_headroom = [] { const char* v = std::getenv("STRATA_RESIDENT_HEADROOM_GIB"); - return (v != nullptr && std::atof(v) >= 0.0) ? (uint64_t) (std::atof(v) * 1073741824.0) : (4ull << 30); + return (v != nullptr && std::atof(v) >= 0.0) ? (uint64_t) (std::atof(v) * 1073741824.0) : (8ull << 30); }(); for (bool again = true; again;) { again = false; @@ -6844,7 +6844,7 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: the experts cannot be read from the file either (%s): loading the expert arena " "as before, short of RAM\n", err.c_str()); o.mmap_experts = false; - if (ram_fallback_resident) o.resident_cpu_experts = o.resident_pin = o.resident_soft = ram_fallback_resident = false; + if (ram_fallback_resident) { o.resident_cpu_experts = o.resident_pin = o.resident_soft = ram_fallback_resident = false; o.resident_budget = 0; } ram_fallback_taken = false; ram_fallback_failed = true; again = true; @@ -7066,6 +7066,10 @@ int main(int argc, char** argv) { if (!o.expert_profile.empty() && !o.resident_cpu_experts && !ram_fallback_resident_off) { o.resident_cpu_experts = o.resident_pin = o.resident_soft = true; o.resident_headroom = ram_fallback_headroom; + // the budget form (as --resident-budget-gib): the hottest experts in what is available minus the headroom, and the rest + // read unbuffered, not through the mapped view (whose pages grow the working set until available RAM is near zero) + if (const auto hm = strata::core::sample_host_memory(); hm && hm->available > o.resident_headroom + (1ull << 30)) + o.resident_budget = hm->available - o.resident_headroom; ram_fallback_resident = true; } again = true;