Add mem_budget: one place that says how much room the card and the system have.

A thread samples every two seconds the video memory budget against this process's use (vk_mem_vram) and the system's
commit headroom and free physical memory (plat_commit_info, plat_mem_info), and turns each side into a level: roomy,
normal, tight or critical, with five points of hysteresis upward and a 256 MiB floor. Changes are logged, and the
memory report's system line ends with both levels. MEM_BUDGET_TEST_VRAM_MB and MEM_BUDGET_TEST_RAM_MB pretend a side
is that small, so that what gives memory back can be tried on a large machine; the texture cache's rules are unchanged.
tests/mem_budget_check.py checks the levels and the hysteresis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 07:53:47 -05:00
parent 3573a96f46
commit 0c153c124e
8 changed files with 321 additions and 2 deletions

View File

@@ -81,6 +81,7 @@ static const char *const known[] = {
"TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", "TEXTURE_DETAIL", "VRAM_USE", // vram_use: the removed Video memory use row, read by nothing
"RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_FINI_IDLE", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS",
"VMA_BLOCK_MB", "SYNC_WAKE_ALL", "LOG_ASYNC", "DEVICE_LOST_AT", "STAGING_FAIL_AT", "VRAM_BALLAST_MB",
"MEM_BUDGET_TEST_VRAM_MB", "MEM_BUDGET_TEST_RAM_MB",
"TEX_COMPACT", "TEX_COMPACT_MIN_MB", "TEX_COMPACT_FRAME_MB", // texture compaction (vk_texture.c); no menu row
"STANDIN_FILTER", // the filter of the scene upscaler's stand-in (vk_tonemap.c); no menu row
"COOP_STEAM", "COOP_SHARE", "COOP_NAMES", "COOP_CHAT", // co-op (coop.c, coop_game.c, ui/coop_page.cpp)
@@ -163,6 +164,7 @@ static const struct { const char *key, *section; } sections[] = {
{"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"},
{"WAIT_WATCH_S", "diagnostics"},
{"VMA_BLOCK_MB", "diagnostics"}, {"DEVICE_LOST_AT", "diagnostics"}, {"STAGING_FAIL_AT", "diagnostics"}, {"VRAM_BALLAST_MB", "diagnostics"},
{"MEM_BUDGET_TEST_VRAM_MB", "diagnostics"}, {"MEM_BUDGET_TEST_RAM_MB", "diagnostics"},
{"TEX_COMPACT", "performance"}, {"TEX_COMPACT_MIN_MB", "performance"}, {"TEX_COMPACT_FRAME_MB", "performance"},
};
@@ -416,6 +418,10 @@ static const CfgMeta metas[] = {
{"DRAW_CRUMBS", "0", "0|1", RESTART}, // vk_crumbs.c draw marks: a mark at each change of shaders inside a pass
{"STAGING_FAIL_AT", "0", "0..1000000000", RESTART}, // vk_memory.c staging_fail_now: a test of give_back
{"VRAM_BALLAST_MB", "0", "0..131072", RESTART}, // vk_memory.c vk_mem_ballast: a test of a smaller card
// mem_budget.h: a test that pretends the video memory or the system memory is that many MiB, for the levels the
// owners that give memory back go by; 0 as it is
{"MEM_BUDGET_TEST_VRAM_MB", "0", "0..1048576", RESTART},
{"MEM_BUDGET_TEST_RAM_MB", "0", "0..1048576", RESTART},
{"DEVICE_LOST_AT", "0", "0..1000000000", RESTART}, // vk_sync.c lost_at_due: a test of the lost device's end
{"INPUT_RECORD", "", NULL, RESTART},
};

128
runtime/mem_budget.c Normal file
View File

@@ -0,0 +1,128 @@
// The room the card and the system have, as a level per side (mem_budget.h): sampled every MEM_BUDGET_PERIOD_MS on the
// "mem budget" thread, logged when a level changes, read by the owners of memory that can be given back (gfx_shader.c
// "SPIR-V at rest" compresses sooner when the system is tight) and shown in the memory report (vk_memory.c system_log).
// A sample is one driver query (vk_mem_vram) and two system calls (plat_commit_info, plat_mem_info): well under a
// millisecond, never on a drawing thread.
#include <stdio.h>
#include "cfg.h"
#include "host.h"
#include "mem_budget.h"
#include "platform/platform.h"
#include "vk.h"
static struct {
PlatOnce once;
PlatMutex vram_lock; // held over a video memory sample: mem_budget_vram(0) waits for it
int vram_on; // the device is up (under vram_lock)
uint64_t test_mb[MEM_SIDES]; // MEM_BUDGET_TEST_VRAM_MB, MEM_BUDGET_TEST_RAM_MB; 0 off
volatile int32_t level[MEM_SIDES];
volatile int32_t known[MEM_SIDES];
volatile uint32_t free_pm[MEM_SIDES];
} B = {PLAT_ONCE_INIT, PLAT_MUTEX_INIT, 0, {0, 0}, {MEM_NORMAL, MEM_NORMAL}, {0, 0}, {0, 0}};
static const char *const side_name[MEM_SIDES] = {"video memory", "system memory"};
// The video memory's room: the driver's budget (at most MEM_BUDGET_TEST_VRAM_MB) less this process's use. 0 while the
// device is not up or the driver gives no figure.
static int sample_vram(MemRoom *r, char *what, size_t cap) {
uint64_t used = 0, budget = 0;
int exact = 0, ok = 0;
plat_mutex_lock(&B.vram_lock);
if (B.vram_on) ok = vk_mem_vram(&used, &budget, &exact);
plat_mutex_unlock(&B.vram_lock);
if (!ok || !budget) return 0;
uint64_t u = used >> 20, l = budget >> 20, t = B.test_mb[MEM_VRAM];
if (t && t < l) l = t;
*r = mem_room(u, l);
snprintf(what, cap, "%llu of %llu MiB in use%s%s", (unsigned long long)u, (unsigned long long)l,
exact ? "" : ", the allocator's own estimate", t ? ", MEM_BUDGET_TEST_VRAM_MB" : "");
return 1;
}
// The system's room: the lower of the commit's headroom and the physical memory free; with MEM_BUDGET_TEST_RAM_MB
// that many MiB less this process's commit.
static int sample_ram(MemRoom *r, char *what, size_t cap) {
double proc, used, limit;
float pused, ptotal, pws;
plat_commit_info(&proc, &used, &limit);
plat_mem_info(&pused, &ptotal, &pws);
uint64_t t = B.test_mb[MEM_RAM];
if (t) {
if (proc < 0) return 0;
*r = mem_room((uint64_t)proc, t);
snprintf(what, cap, "this process's commit %.0f of %llu MiB, MEM_BUDGET_TEST_RAM_MB", proc, (unsigned long long)t);
return 1;
}
int have_c = used >= 0 && limit > 0, have_p = pused >= 0 && ptotal > 0;
if (!have_c && !have_p) return 0;
MemRoom none = {1000, UINT64_MAX};
MemRoom c = have_c ? mem_room((uint64_t)used, (uint64_t)limit) : none;
MemRoom p = have_p ? mem_room((uint64_t)pused, (uint64_t)ptotal) : none;
*r = mem_room_min(c, p);
snprintf(what, cap, "commit %.0f of %.0f MiB, physical memory %.0f of %.0f MiB in use", used, limit, (double)pused,
(double)ptotal);
return 1;
}
static void budget_main(void *arg) {
(void)arg;
for (;;) {
for (int side = 0; side < MEM_SIDES; side++) {
MemRoom r;
char what[200];
if (!(side == MEM_VRAM ? sample_vram(&r, what, sizeof what) : sample_ram(&r, what, sizeof what))) continue;
int known = __atomic_load_n(&B.known[side], __ATOMIC_RELAXED), was = B.level[side];
int now = mem_level_next(known ? was : MEM_ROOMY, r); // the first sample: its level as it is
__atomic_store_n(&B.free_pm[side], r.free_pm, __ATOMIC_RELAXED);
__atomic_store_n(&B.level[side], now, __ATOMIC_RELAXED);
__atomic_store_n(&B.known[side], 1, __ATOMIC_RELEASE);
if (!known || now != was) {
char from[32] = "";
if (known) snprintf(from, sizeof from, ", was %s", mem_level_name[was]);
rt_log("mem_budget: %s %s, %.1f%% free (%s%s)", side_name[side], mem_level_name[now], r.free_pm / 10.0,
what, from);
}
}
plat_sleep_ms(MEM_BUDGET_PERIOD_MS);
}
}
static void budget_start(void *arg) {
(void)arg;
int tv = cfg_int("NX2NAVI_MEM_BUDGET_TEST_VRAM_MB"), tr = cfg_int("NX2NAVI_MEM_BUDGET_TEST_RAM_MB");
B.test_mb[MEM_VRAM] = tv > 0 ? (uint64_t)tv : 0, B.test_mb[MEM_RAM] = tr > 0 ? (uint64_t)tr : 0;
if (B.test_mb[MEM_VRAM] || B.test_mb[MEM_RAM])
rt_log("mem_budget: a test pretends a smaller budget (video memory %llu MiB, system memory %llu MiB; 0: as it is): "
"the levels follow it, the texture cache's own rules do not", (unsigned long long)B.test_mb[MEM_VRAM],
(unsigned long long)B.test_mb[MEM_RAM]);
if (!plat_thread_spawn(budget_main, NULL, "mem budget", 0))
rt_log("mem_budget: cannot start its thread; every level stays normal");
}
void mem_budget_vram(int on) {
plat_mutex_lock(&B.vram_lock);
B.vram_on = on;
plat_mutex_unlock(&B.vram_lock);
if (on) plat_once(&B.once, budget_start, NULL);
}
int mem_budget_level(int side) {
if (side < 0 || side >= MEM_SIDES) return MEM_NORMAL;
return __atomic_load_n(&B.level[side], __ATOMIC_RELAXED);
}
void mem_budget_text(char *out, unsigned cap) {
size_t k = 0;
if (cap) out[0] = 0;
for (int side = 0; side < MEM_SIDES && k < cap; side++) {
int n;
if (__atomic_load_n(&B.known[side], __ATOMIC_ACQUIRE))
n = snprintf(out + k, cap - k, "%s%s %s (%.1f%% free)", side ? ", " : "", side_name[side],
mem_level_name[__atomic_load_n(&B.level[side], __ATOMIC_RELAXED)],
__atomic_load_n(&B.free_pm[side], __ATOMIC_RELAXED) / 10.0);
else n = snprintf(out + k, cap - k, "%s%s not sampled yet", side ? ", " : "", side_name[side]);
if (n < 0) break;
k += (size_t)n;
}
}

66
runtime/mem_budget.h Normal file
View File

@@ -0,0 +1,66 @@
// How much room the card and the system have, as one level per side (mem_budget.c): roomy, normal, tight or critical.
// Owners of memory that can be given back read the level and give back earlier when room is short; what they keep
// while there is room is theirs to decide. Header-only part: the arithmetic, which tests/mem_budget_check.c runs.
//
// A side's room is its free share in per mille: for the video memory the driver's budget less this process's use of
// the device-local heaps (vk_mem_vram); for the system the lower of the commit's headroom (the commit limit less the
// system's commit in use: an allocation fails at the limit) and the physical memory free (plat_commit_info,
// plat_mem_info). The levels begin at fixed shares going down, and a side climbs back to a better level only once its
// share is MEM_HYST_PM above the line it fell through, so a share that hovers at a line does not flip the level at
// every sample. Below MEM_FLOOR_MB free a side is critical whatever its share.
#pragma once
#include <stdint.h>
enum { MEM_ROOMY, MEM_NORMAL, MEM_TIGHT, MEM_CRITICAL, MEM_LEVELS };
enum { MEM_VRAM, MEM_RAM, MEM_SIDES };
// The free share (per mille) above which a level holds, from the best down: roomy above 400 (40% free), normal above
// 200, tight above 80; critical at 80 or less. The 20% line is where vk_texture.c's VRAM pressure rule
// (tex_vram_pressure, 80% of the budget in use) starts to act on released textures.
static const uint32_t mem_level_line[MEM_LEVELS - 1] = {400, 200, 80};
#define MEM_HYST_PM 50 // a level is left for a better one 5 points above the line it fell through
#define MEM_FLOOR_MB 256 // less free than this is critical at any share
static const char *const mem_level_name[MEM_LEVELS] = {"roomy", "normal", "tight", "critical"};
typedef struct {
uint32_t free_pm; // free share of the limit, per mille
uint64_t free_mb; // free MiB
} MemRoom;
// The room of a side with used of limit in use (any one unit, here MiB); none when used reaches the limit.
static inline MemRoom mem_room(uint64_t used, uint64_t limit) {
MemRoom r = {0, 0};
if (!limit || used >= limit) return r;
r.free_mb = limit - used;
r.free_pm = (uint32_t)(r.free_mb * 1000 / limit);
return r;
}
// The tighter of two measures of one side: the lower share, and the lower free amount.
static inline MemRoom mem_room_min(MemRoom a, MemRoom b) {
MemRoom r = {a.free_pm < b.free_pm ? a.free_pm : b.free_pm, a.free_mb < b.free_mb ? a.free_mb : b.free_mb};
return r;
}
// The level a side moves to from `level` at room r: down to a worse level at once, up only past the hysteresis.
static inline int mem_level_next(int level, MemRoom r) {
int raw = 0;
while (raw < MEM_LEVELS - 1 && r.free_pm <= mem_level_line[raw]) raw++;
if (r.free_mb < MEM_FLOOR_MB) raw = MEM_CRITICAL;
if (raw >= level) return raw;
int n = level; // climb one level at a time while the share clears the line above with room to spare
while (n > raw && r.free_pm > mem_level_line[n - 1] + MEM_HYST_PM) n--;
return n;
}
// ---- mem_budget.c: the levels, sampled every MEM_BUDGET_PERIOD_MS on a thread of their own, off the drawing threads.
// Each change of a side's level is logged with its figures. MEM_BUDGET_TEST_VRAM_MB and MEM_BUDGET_TEST_RAM_MB (0:
// off) pretend a side is that small, for testing what gives memory back on a large machine: the video memory budget
// becomes at most that many MiB against this process's use, and the system's room becomes that many MiB less this
// process's commit. They change the levels only, never the texture cache's own rules.
#define MEM_BUDGET_PERIOD_MS 2000
void mem_budget_vram(int on); // vk_device.c: 1 once the device is up (the first call starts the thread), 0 before
// it goes (waits for a sample under way)
int mem_budget_level(int side); // MEM_ROOMY .. MEM_CRITICAL of MEM_VRAM or MEM_RAM; MEM_NORMAL before its first sample
void mem_budget_text(char *out, unsigned cap); // "video memory normal (31.2% free), system memory roomy (55.0% free)"

View File

@@ -11,6 +11,7 @@
#include "cfg.h"
#include "host.h"
#include "mem_budget.h"
#include "vk.h"
#include "platform/platform.h"
#include "vk_dlaa.h"
@@ -763,7 +764,8 @@ int vk_init(void) {
plat_mutex_lock(&g_lock);
if (!g_ready) {
memset(&g_vk, 0, sizeof g_vk);
if (create_instance() && pick_device() && create_device()) g_ready = 1, vk_headroom_start(), vk_dlaa_init();
if (create_instance() && pick_device() && create_device())
g_ready = 1, vk_headroom_start(), vk_dlaa_init(), mem_budget_vram(1);
else destroy_all();
}
int ok = g_ready;
@@ -773,7 +775,7 @@ int vk_init(void) {
void vk_shutdown(void) {
plat_mutex_lock(&g_lock);
if (g_ready) { destroy_all(); g_ready = 0; }
if (g_ready) { mem_budget_vram(0); destroy_all(); g_ready = 0; }
plat_mutex_unlock(&g_lock);
}

View File

@@ -18,6 +18,7 @@
#include "gfx.h"
#include "host.h"
#include "logq.h"
#include "mem_budget.h"
#include "mem_growth.h"
#include "nvn_api.h"
#include "nvn_pool_index.h"
@@ -5528,6 +5529,9 @@ static void system_log(uint64_t frame, const ReportFig *f, const VkPipeMem *pm,
"%.0f, %s) + other %.0f (the driver's own memory, thread stacks, the program's data; %u other heaps)",
pcache / M, pm->cache[0] / M, pm->cache[1] / M, pm->cache_asked ? "as last asked" : "as loaded at the boot",
other / M, hi.other_heaps);
char room[160];
mem_budget_text(room, sizeof room); // the levels the owners that give memory back go by (mem_budget.h)
mg_put(line, sizeof line, &k, "; room: %s", room);
rt_log("%s", line);
*out = (SysFig){priv, crt, shaders, pipes, texhost, pcache, other > 0 ? (uint64_t)other : 0};
}