From 4ff5cd4050381ad65766169df5b02f7f6a820bbe Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 15 Oct 2026 22:27:30 +0200 Subject: [PATCH] pin budget: test knobs (simulated ceiling, 'driver took everything'), report lines, smaller default reserve, pin_budget_test --- CMakeLists.txt | 5 ++ include/strata/core/pin_budget.hpp | 27 +++++++++-- src/core/pin_budget_test.cpp | 77 ++++++++++++++++++++++++++++++ src/program/generate.cpp | 2 + 4 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 src/core/pin_budget_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 72a13be8..4228edd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1127,6 +1127,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_link_libraries(platform_memory_test PRIVATE strata_core ${_strata_gpu_runtime_target}) add_test(NAME platform_memory_test COMMAND platform_memory_test) + # The pinned-memory headroom (pin_budget.hpp) against a simulated ceiling; skips without a GPU. + add_executable(pin_budget_test src/core/pin_budget_test.cpp) + target_link_libraries(pin_budget_test PRIVATE strata_core ${_strata_gpu_runtime_target}) + add_test(NAME pin_budget_test COMMAND pin_budget_test) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") add_executable(pinned_shared_test src/core/pinned_shared_test.cpp) target_link_libraries(pinned_shared_test PRIVATE strata_core ${_strata_gpu_runtime_target}) diff --git a/include/strata/core/pin_budget.hpp b/include/strata/core/pin_budget.hpp index 649667dc..1a31a831 100644 --- a/include/strata/core/pin_budget.hpp +++ b/include/strata/core/pin_budget.hpp @@ -13,9 +13,9 @@ // the engine is up; the allocation then releases it and retries once (host_alloc_guarded). // A reserve that cannot be had is shrunk, then skipped: this never fails a start. // -// Defaults: Windows 768 + 192 MiB; Linux off (the NVIDIA driver there pins past RLIMIT_MEMLOCK and has no such +// Defaults: Windows 640 + 160 MiB; Linux off (the NVIDIA driver there pins past RLIMIT_MEMLOCK and has no such // ceiling; checked with a 4 GiB cudaHostRegister under `ulimit -l 1 GiB`). STRATA_PIN_RESERVE_MIB=N sets the soft -// reserve (0 = off) on any OS; the hard one is a fifth of it, at least 64 MiB. +// reserve (0 = off) on any OS; the hard one is a quarter of it, at least 64 MiB. // // STRATA_TEST_PIN_BUDGET_MIB=N is a TEST knob: it makes the engine's own pinned allocations and registrations that go // through these wrappers (the arena's, the verify window's, the session's, the KV copy's) fail once N MiB are @@ -90,9 +90,9 @@ inline uint64_t soft_reserve_default_mib() { if (const char* e = std::getenv("STRATA_PIN_RESERVE_MIB"); e != nullptr && *e != '\0') return (uint64_t) std::strtoull(e, nullptr, 10); #if defined(_WIN32) - return 768; + return 640; #else - return st().sim_budget != 0 ? 768 : 0; // Linux: only when the test ceiling is on + return st().sim_budget != 0 ? 640 : 0; // Linux: only when the test ceiling is on #endif } } // namespace pin_detail @@ -168,8 +168,25 @@ inline void pin_reserve_release_hard() { s.hard = nullptr; s.hard_bytes = 0; } -inline void pin_note_arena(uint64_t pinned_bytes) { pin_detail::st().arena_pinned = pinned_bytes; } +inline void pin_note_arena(uint64_t pinned_bytes) { + auto& s = pin_detail::st(); + std::lock_guard g(s.mu); + s.arena_pinned = pinned_bytes; + // TEST knob: the driver took everything but N KiB (what WDDM did on the 64 GB PC: the arena pinned to the ceiling) + if (s.sim_budget != 0) + if (const char* e = std::getenv("STRATA_TEST_PIN_LEFT_KIB"); e != nullptr && *e != 0) + s.sim_budget = s.sim_used + ((uint64_t) std::strtoull(e, nullptr, 10) << 10); +} inline uint64_t pin_sim_peak() { return pin_detail::st().sim_peak; } +/// Test knob only: one line with the simulated ceiling, what is pinned now and the peak. +inline void pin_sim_report(const char* when) { + auto& s = pin_detail::st(); + std::lock_guard g(s.mu); + if (s.sim_budget == 0) return; + std::fprintf(stderr, "strata: test pinned budget %llu MiB: %llu MiB charged, peak %llu MiB (%s)\n", + (unsigned long long) (s.sim_budget >> 20), (unsigned long long) (s.sim_used >> 20), + (unsigned long long) (s.sim_peak >> 20), when); +} /// cudaHostAlloc for a buffer the engine cannot run without. On a refusal it hands the reserves back and tries once /// more; the caller still reports a second refusal. diff --git a/src/core/pin_budget_test.cpp b/src/core/pin_budget_test.cpp new file mode 100644 index 00000000..1c28f015 --- /dev/null +++ b/src/core/pin_budget_test.cpp @@ -0,0 +1,77 @@ +// pin_budget_test: the pinned-memory headroom (include/strata/core/pin_budget.hpp) with the simulated ceiling. +// Needs a GPU for the pinned calls; without one it prints SKIP and passes. +#include "strata/core/pin_budget.hpp" + +#include +#include +#include + +static int g_fail = 0; +#define CHECK(c) \ + do { \ + if (!(c)) { std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++g_fail; } \ + } while (0) + +#ifdef _WIN32 +#define SETENV(k, v) _putenv_s(k, v) +#else +#define SETENV(k, v) setenv(k, v, 1) +#endif + +int main() { + // the singleton reads its environment on first use: set everything first + SETENV("STRATA_TEST_PIN_BUDGET_MIB", "100"); + SETENV("STRATA_PIN_RESERVE_MIB", "32"); // soft 32 MiB, hard 64 MiB (a quarter of 32 is below the 64 MiB floor) + using namespace strata::core; + if (cudaFree(0) != cudaSuccess) { + std::printf("pin_budget_test: SKIP (no GPU)\n"); + return 0; + } + (void) cudaGetLastError(); + + // 1. a registration beyond the ceiling is refused, one inside it works and is charged + { + void* big = std::malloc(160u << 20); + void* small = std::malloc(8u << 20); + CHECK(pin_register(big, 160u << 20, cudaHostRegisterPortable) == cudaErrorMemoryAllocation); + CHECK(pin_register(small, 8u << 20, cudaHostRegisterPortable) == cudaSuccess); + CHECK(pin_detail::st().sim_used == (8u << 20)); + CHECK(pin_unregister(small) == cudaSuccess); + CHECK(pin_detail::st().sim_used == 0); + std::free(big); + std::free(small); + } + // 2. without a reserve a refused guarded allocation stays refused + { + void* p = nullptr; + CHECK(host_alloc_guarded(&p, 200u << 20, cudaHostAllocMapped) != cudaSuccess); + (void) cudaGetLastError(); + } + // 3. the reserves are held, the arena (here: a registration) takes what is left, a required buffer is then + // refused, the held headroom is handed back and the retry works + { + pin_reserve_take(); + CHECK(pin_detail::st().hard != nullptr && pin_detail::st().hard_bytes == (64u << 20)); + CHECK(pin_detail::st().soft != nullptr && pin_detail::st().soft_bytes == (32u << 20)); + CHECK(pin_detail::st().sim_used == (96u << 20)); + void* arena = std::malloc(4u << 20); + CHECK(pin_register(arena, 4u << 20, cudaHostRegisterPortable) == cudaSuccess); // 100 MiB used: the ceiling + pin_reserve_release_soft(); + CHECK(pin_detail::st().soft == nullptr); + CHECK(pin_detail::st().sim_used == (68u << 20)); + void* a = nullptr; + CHECK(host_alloc_guarded(&a, 16u << 20, cudaHostAllocMapped) == cudaSuccess); // fits beside the hard reserve + void* b = nullptr; + CHECK(host_alloc_guarded(&b, 40u << 20, cudaHostAllocMapped) == cudaSuccess); // refused first, then the hard reserve goes + CHECK(pin_detail::st().hard == nullptr); + CHECK(pin_detail::st().hard_retries == 1); + void* c = nullptr; + CHECK(host_alloc_guarded(&c, 60u << 20, cudaHostAllocMapped) != cudaSuccess); // truly out: reported, not hidden + (void) cudaGetLastError(); + CHECK(pin_exhausted_hint().find("[pinned-memory-exhausted]") != std::string::npos); + (void) pin_unregister(arena); + std::free(arena); + } + std::printf(g_fail == 0 ? "pin_budget_test: OK\n" : "pin_budget_test: %d FAILED\n", g_fail); + return g_fail == 0 ? 0 : 1; +} diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 4c9aab0e..ba697c2e 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -5231,6 +5231,7 @@ int main(int argc, char** argv) { const bool arena_ok = arena_src.open(o.pack, g.n_layers, g.n_expert, /*threads=*/6, err, pin_limit, o.shared_expert_arena); strata::core::pin_note_arena(arena_src.pinned_arena_bytes()); + strata::core::pin_sim_report("after the expert arena"); strata::core::pin_reserve_release_soft(); if (!arena_ok) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); @@ -10035,6 +10036,7 @@ int main(int argc, char** argv) { } }).detach(); } + strata::core::pin_sim_report("at READY"); strata::core::pin_reserve_release_hard(); // the load is over: nothing else needs the headroom std::printf("READY %lld stop\n", (long long) o.max_context); // "stop": this engine honours STOP std::fflush(stdout);