// vram_guard_test: the policy (shed / reclaim / hysteresis), the guard's throttle and its sources, with made-up memory and a made-up clock. No GPU. // MIT, Copyright (c) 2026 Niko1221 and the Strata contributors. #include "strata/platform/vram_guard.hpp" #include #include #include #include using namespace strata::platform; static int g_bad = 0; #define CHECK(c) \ do { \ if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++g_bad; } \ } while (0) static const uint64_t MiB = 1ull << 20; static VramSnapshot snap(uint64_t headroom_mib) { VramSnapshot s; s.source = VramSnapshot::Driver; s.driver_total = 12000 * MiB; s.budget_bytes = s.driver_total; s.headroom_bytes = headroom_mib * MiB; s.driver_free = s.headroom_bytes; s.used_bytes = s.budget_bytes - s.headroom_bytes; return s; } static void policy_basics() { VramPolicy p{VramPolicyConfig{}}; // 192 / 352 / 512 MiB, chunk 2 MiB, dwell 3 s, step gap 1 s, quiet 5 s, step cap 256 MiB VramAdvice a = p.advise(snap(1000), 0); CHECK(a.kind == VramAdvice::None && a.sampled); a = p.advise(snap(300), 10); // inside the band CHECK(a.kind == VramAdvice::None); a = p.advise(snap(100), 20); // below low: shed up to target CHECK(a.kind == VramAdvice::Shed && a.bytes == 252 * MiB); a = p.advise(snap(191), 30); CHECK(a.kind == VramAdvice::Shed && a.bytes == 161 * MiB + MiB); // 161 MiB rounded up to the 2 MiB chunk = 162 CHECK(a.bytes % (2 * MiB) == 0); a = p.advise(snap(192), 40); // exactly low is not below it CHECK(a.kind == VramAdvice::None); a = p.advise(snap(0), 50); CHECK(a.kind == VramAdvice::Shed && a.bytes == 352 * MiB); VramSnapshot none; // no reading: no advice CHECK(p.advise(none, 60).kind == VramAdvice::None); CHECK(p.spare(snap(192)) == 0); CHECK(p.spare(snap(100)) == 0); CHECK(p.spare(snap(300)) == 108 * MiB); CHECK(p.spare(none) == 0); } static void policy_reclaim_rules() { VramPolicy p{VramPolicyConfig{}}; // above high, but the dwell (3 s) has not passed CHECK(p.advise(snap(800), 100000).kind == VramAdvice::None); CHECK(p.advise(snap(800), 102000).kind == VramAdvice::None); // after the dwell: one step, capped at 256 MiB (800 - 352 = 448 would be the whole way) VramAdvice a = p.advise(snap(800), 103000); CHECK(a.kind == VramAdvice::MayReclaim && a.bytes == 256 * MiB); // a step is applied: the next one waits the step gap p.applied(0, 256 * MiB, 103000); CHECK(p.advise(snap(800), 103500).kind == VramAdvice::None); a = p.advise(snap(600), 104000); // 600 - 352 = 248 MiB, below the cap CHECK(a.kind == VramAdvice::MayReclaim && a.bytes == 248 * MiB); // it landed on the target: inside the band, nothing, and the dwell starts over CHECK(p.advise(snap(352), 105000).kind == VramAdvice::None); CHECK(p.advise(snap(900), 106000).kind == VramAdvice::None); // dwell again CHECK(p.advise(snap(900), 108999).kind == VramAdvice::None); CHECK(p.advise(snap(900), 109000).kind == VramAdvice::MayReclaim); // a dip into the band resets the dwell VramPolicy q{VramPolicyConfig{}}; q.advise(snap(900), 0); q.advise(snap(400), 2000); CHECK(q.advise(snap(900), 3000).kind == VramAdvice::None); CHECK(q.advise(snap(900), 5999).kind == VramAdvice::None); CHECK(q.advise(snap(900), 6000).kind == VramAdvice::MayReclaim); // quiet time after pressure: no reclaim within 5 s of a sample below low, even with the dwell passed VramPolicy r{VramPolicyConfig{}}; CHECK(r.advise(snap(100), 1000).kind == VramAdvice::Shed); r.advise(snap(900), 1100); CHECK(r.advise(snap(900), 4200).kind == VramAdvice::None); // dwell passed (3.1 s), still within 5 s of the pressure CHECK(r.advise(snap(900), 5999).kind == VramAdvice::None); CHECK(r.advise(snap(900), 6000).kind == VramAdvice::MayReclaim); // the runner says how much it could take back: nothing = no offer, a little = no more than that VramPolicy v{VramPolicyConfig{}}; v.advise(snap(900), 0); CHECK(v.advise(snap(900), 3000, 0).kind == VramAdvice::None); a = v.advise(snap(900), 3100, 64 * MiB); CHECK(a.kind == VramAdvice::MayReclaim && a.bytes == 64 * MiB); // a shed the runner reports is pressure too VramPolicy t{VramPolicyConfig{}}; t.advise(snap(900), 0); t.applied(64 * MiB, 0, 3500); CHECK(t.advise(snap(900), 4000).kind == VramAdvice::None); // the report reset the dwell } // A closed loop: an outside program takes and gives memory, the runner obeys the advice. Count how often the runner changes what it holds. static void policy_no_flap() { VramPolicyConfig pc; VramPolicy p{pc}; const uint64_t total = 12000; // MiB, the card int64_t outside = 6000; // MiB taken by others int64_t ours = 5000; // MiB the runner holds (experts + K/V) int moves = 0, last_dir = 0, flips = 0; auto headroom = [&] { return static_cast(std::max(0, static_cast(total) - outside - ours)); }; uint64_t t = 0; auto step = [&](int polls) { for (int i = 0; i < polls; ++i, t += 500) { VramAdvice a = p.advise(snap(headroom()), t); if (a.kind == VramAdvice::Shed) { ours -= static_cast(a.bytes / MiB); p.applied(a.bytes, 0, t); ++moves; if (last_dir > 0) ++flips; last_dir = -1; } else if (a.kind == VramAdvice::MayReclaim) { ours += static_cast(a.bytes / MiB); p.applied(0, a.bytes, t); ++moves; if (last_dir < 0) ++flips; last_dir = 1; } } }; step(20); // 1000 MiB free, we are at 1000 headroom: reclaim down to the target CHECK(headroom() >= 192 && headroom() <= 512); const int m0 = moves; step(200); // nothing changes outside: nothing moves (a hundred seconds) CHECK(moves == m0); outside += 400; // a browser takes 400 MiB step(1); CHECK(headroom() >= 192); // shed at once, back to target const int m1 = moves; step(100); CHECK(moves == m1); // and stays put outside -= 400; // the browser closes step(4); // inside the quiet time / dwell: no reclaim yet CHECK(moves == m1); step(40); CHECK(headroom() >= 192 && headroom() <= 512); CHECK(flips == 2); // reclaim -> shed -> reclaim, nothing else // a noisy outside program (+-120 MiB every poll) stays inside the band: the runner does not move at all const int m2 = moves; for (int i = 0; i < 400; ++i) { outside += (i % 2) ? 120 : -120; step(1); } CHECK(moves == m2); } struct FakeCard { uint64_t total = 12000 * MiB; uint64_t free_b = 6000 * MiB; int calls = 0; uint64_t now = 0; }; static bool fake_mem(void* c, size_t* f, size_t* t) { auto* k = static_cast(c); ++k->calls; *f = static_cast(k->free_b); *t = static_cast(k->total); return true; } static bool fake_fail(void* c, size_t*, size_t*) { ++static_cast(c)->calls; return false; } static uint64_t fake_clock(void* c) { return static_cast(c)->now; } static VramGuardConfig fake_cfg(FakeCard* k) { VramGuardConfig c; c.driver_mem = &fake_mem; c.driver_ctx = k; c.now_ms = &fake_clock; c.clock_ctx = k; c.read_env = false; c.match_by_size = false; // a made-up card must not be matched to the real adapter of the machine the test runs on return c; } static void guard_throttle_and_sources() { FakeCard k; VramGuard g; CHECK(g.init(fake_cfg(&k))); CHECK(g.enabled()); const int c0 = k.calls; // init read the card once CHECK(c0 >= 1); k.now = 1000; VramAdvice a = g.poll(); // first poll samples CHECK(a.sampled && a.kind == VramAdvice::None); CHECK(k.calls == c0 + 1); CHECK(a.snap.source == VramSnapshot::Driver && a.snap.headroom_bytes == 6000 * MiB); k.free_b = 50 * MiB; // pressure, but inside the period: the guard does not look k.now = 1499; a = g.poll(); CHECK(!a.sampled && a.kind == VramAdvice::None && k.calls == c0 + 1); k.now = 1500; // the period has passed a = g.poll(); CHECK(a.sampled && a.kind == VramAdvice::Shed && a.bytes == 302 * MiB); CHECK(k.calls == c0 + 2); // forced sample: ignores the period, one driver call per spare_for_growth k.free_b = 400 * MiB; k.now = 1501; const int c1 = k.calls; CHECK(g.spare_for_growth() == 208 * MiB); CHECK(k.calls == c1 + 1); k.free_b = 150 * MiB; CHECK(g.spare_for_growth() == 0); CHECK(k.calls == c1 + 2); a = g.poll_now(); CHECK(a.sampled && a.kind == VramAdvice::Shed); CHECK(g.last_snapshot().headroom_bytes == 150 * MiB); // invariants of a snapshot const VramSnapshot s = g.sample(); CHECK(s.budget_bytes == s.used_bytes + s.headroom_bytes); // reclaim through the guard: the dwell and the quiet time count on the guard's own clock k.free_b = 900 * MiB; k.now = 20000; a = g.poll(); CHECK(a.kind == VramAdvice::None); // the dwell starts k.now = 23000; a = g.poll(); CHECK(a.kind == VramAdvice::MayReclaim && a.bytes == 256 * MiB); g.report_applied(0, a.bytes); k.now = 23500; CHECK(g.poll().kind == VramAdvice::None); // the step gap (1 s) g.shutdown(); CHECK(!g.enabled()); CHECK(g.poll().kind == VramAdvice::None && !g.poll().sampled); CHECK(g.spare_for_growth() == 0); // a failing driver query and no DXGI: no source, the guard says so and stays off FakeCard bad; VramGuardConfig cb = fake_cfg(&bad); cb.driver_mem = &fake_fail; VramGuard gb; CHECK(!gb.init(cb)); CHECK(!gb.enabled() && gb.poll().kind == VramAdvice::None); std::printf("vram_guard_test: status with a failing driver query: %s\n", gb.status()); // a garbage LUID (a card that is not in DXGI, a TCC card, Linux): the driver alone still works FakeCard k2; VramGuardConfig c2 = fake_cfg(&k2); for (int i = 0; i < 8; ++i) c2.luid[i] = static_cast(0xA0 + i); c2.has_luid = true; VramGuard g2; CHECK(g2.init(c2)); CHECK(g2.sample().source == VramSnapshot::Driver); std::printf("vram_guard_test: status with an unknown LUID: %s\n", g2.status()); // watermarks: a target outside the band falls back to the middle; the high mark is kept at least two chunks above low VramGuardConfig c3 = fake_cfg(&k2); c3.low_mib = 300; c3.target_mib = 100; c3.high_mib = 700; VramGuard g3; CHECK(g3.init(c3)); CHECK(g3.low_bytes() == 300 * MiB && g3.high_bytes() == 700 * MiB && g3.target_bytes() == 500 * MiB); } static void guard_env() { #if defined(_WIN32) _putenv_s("STRATA_INK_VRAM_GUARD", "0"); #else setenv("STRATA_INK_VRAM_GUARD", "0", 1); #endif FakeCard k; VramGuardConfig c = fake_cfg(&k); c.read_env = true; VramGuard g; CHECK(!g.init(c)); CHECK(!g.enabled()); CHECK(g.poll().kind == VramAdvice::None); #if defined(_WIN32) _putenv_s("STRATA_INK_VRAM_GUARD", ""); _putenv_s("STRATA_INK_VRAM_LOW_MIB", "256"); _putenv_s("STRATA_INK_VRAM_HIGH_MIB", "9999999"); // out of range: ignored #else unsetenv("STRATA_INK_VRAM_GUARD"); setenv("STRATA_INK_VRAM_LOW_MIB", "256", 1); setenv("STRATA_INK_VRAM_HIGH_MIB", "9999999", 1); #endif VramGuard g2; CHECK(g2.init(c)); CHECK(g2.low_bytes() == 256 * MiB && g2.high_bytes() == 512 * MiB && g2.target_bytes() == 352 * MiB); #if defined(_WIN32) _putenv_s("STRATA_INK_VRAM_LOW_MIB", ""); _putenv_s("STRATA_INK_VRAM_HIGH_MIB", ""); #else unsetenv("STRATA_INK_VRAM_LOW_MIB"); unsetenv("STRATA_INK_VRAM_HIGH_MIB"); #endif } // poll() inside the period must cost next to nothing: one clock read static void hot_path_cost() { FakeCard k; VramGuardConfig c = fake_cfg(&k); c.now_ms = nullptr; // the real clock: that is what the hot path pays c.period_ms = 3600 * 1000; VramGuard g; CHECK(g.init(c)); g.poll(); // the one sample const int calls = k.calls; const int N = 2000000; uint64_t sink = 0; const auto t0 = std::chrono::steady_clock::now(); for (int i = 0; i < N; ++i) sink += g.poll().kind; const auto t1 = std::chrono::steady_clock::now(); const double ns = std::chrono::duration(t1 - t0).count() / N; std::printf("vram_guard_test: poll() inside the period: %.1f ns per call (%llu)\n", ns, static_cast(sink)); CHECK(k.calls == calls); // not one more driver call CHECK(ns < 1000.0); } int main() { policy_basics(); policy_reclaim_rules(); policy_no_flap(); guard_throttle_and_sources(); guard_env(); hot_path_cost(); if (g_bad) { std::printf("vram_guard_test: %d FAILED\n", g_bad); return 1; } std::printf("vram_guard_test: ok\n"); return 0; }