mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 16:49:58 -04:00
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:
@@ -300,6 +300,13 @@ keeps per-core data without locks and relies on that, so the host models it (`ru
|
|||||||
misses and draws (the module's hash and its Binding decorations) is kept apart and needs no decode; a compute
|
misses and draws (the module's hash and its Binding decorations) is kept apart and needs no decode; a compute
|
||||||
pipeline created at a dispatch and a mesh shader's first pipeline key decode on that thread, which the memory report
|
pipeline created at a dispatch and a mesh shader's first pipeline key decode on that thread, which the memory report
|
||||||
counts. `shader_compress = 0` keeps it plain.
|
counts. `shader_compress = 0` keeps it plain.
|
||||||
|
- Room levels: a thread samples every two seconds how much room the card and the system have and turns each into a
|
||||||
|
level, roomy (over 40% free), normal (over 20%), tight (over 8%) or critical, with five points of hysteresis upward
|
||||||
|
(`runtime/mem_budget.h`, `mem_budget.c`): the card's from the driver's budget against this process's use, the
|
||||||
|
system's from the lower of the commit's headroom and the physical memory free. A change is logged, and the memory
|
||||||
|
report's `mem: system` line ends with both levels. The SPIR-V at rest is compressed after 5 s unused while the system
|
||||||
|
is tight and after 1 s while it is critical. `mem_budget_test_vram_mb` and `mem_budget_test_ram_mb` pretend a side is
|
||||||
|
that small, to test what gives memory back on a large machine; they leave the texture cache's own rules alone.
|
||||||
- Allocation: Vulkan Memory Allocator. Debugging: Vulkan validation layers, per-pass frame dumps (`NX2NAVI_DUMP`,
|
- Allocation: Vulkan Memory Allocator. Debugging: Vulkan validation layers, per-pass frame dumps (`NX2NAVI_DUMP`,
|
||||||
`vk_dump.c`), a sampling profiler (`NX2NAVI_PROFILE`, `tools/prof_report.py`) and a flight recorder
|
`vk_dump.c`), a sampling profiler (`NX2NAVI_PROFILE`, `tools/prof_report.py`) and a flight recorder
|
||||||
(`NX2NAVI_FLIGHT`). RenderDoc cannot capture, because it hides `VK_EXT_external_memory_host` from the application.
|
(`NX2NAVI_FLIGHT`). RenderDoc cannot capture, because it hides `VK_EXT_external_memory_host` from the application.
|
||||||
|
|||||||
@@ -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
|
"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",
|
"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",
|
"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
|
"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
|
"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)
|
"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"},
|
{"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"},
|
||||||
{"WAIT_WATCH_S", "diagnostics"},
|
{"WAIT_WATCH_S", "diagnostics"},
|
||||||
{"VMA_BLOCK_MB", "diagnostics"}, {"DEVICE_LOST_AT", "diagnostics"}, {"STAGING_FAIL_AT", "diagnostics"}, {"VRAM_BALLAST_MB", "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"},
|
{"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
|
{"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
|
{"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
|
{"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
|
{"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},
|
{"INPUT_RECORD", "", NULL, RESTART},
|
||||||
};
|
};
|
||||||
|
|||||||
128
runtime/mem_budget.c
Normal file
128
runtime/mem_budget.c
Normal 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
66
runtime/mem_budget.h
Normal 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)"
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "cfg.h"
|
#include "cfg.h"
|
||||||
#include "host.h"
|
#include "host.h"
|
||||||
|
#include "mem_budget.h"
|
||||||
#include "vk.h"
|
#include "vk.h"
|
||||||
#include "platform/platform.h"
|
#include "platform/platform.h"
|
||||||
#include "vk_dlaa.h"
|
#include "vk_dlaa.h"
|
||||||
@@ -763,7 +764,8 @@ int vk_init(void) {
|
|||||||
plat_mutex_lock(&g_lock);
|
plat_mutex_lock(&g_lock);
|
||||||
if (!g_ready) {
|
if (!g_ready) {
|
||||||
memset(&g_vk, 0, sizeof g_vk);
|
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();
|
else destroy_all();
|
||||||
}
|
}
|
||||||
int ok = g_ready;
|
int ok = g_ready;
|
||||||
@@ -773,7 +775,7 @@ int vk_init(void) {
|
|||||||
|
|
||||||
void vk_shutdown(void) {
|
void vk_shutdown(void) {
|
||||||
plat_mutex_lock(&g_lock);
|
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);
|
plat_mutex_unlock(&g_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#include "gfx.h"
|
#include "gfx.h"
|
||||||
#include "host.h"
|
#include "host.h"
|
||||||
#include "logq.h"
|
#include "logq.h"
|
||||||
|
#include "mem_budget.h"
|
||||||
#include "mem_growth.h"
|
#include "mem_growth.h"
|
||||||
#include "nvn_api.h"
|
#include "nvn_api.h"
|
||||||
#include "nvn_pool_index.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)",
|
"%.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",
|
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);
|
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);
|
rt_log("%s", line);
|
||||||
*out = (SysFig){priv, crt, shaders, pipes, texhost, pcache, other > 0 ? (uint64_t)other : 0};
|
*out = (SysFig){priv, crt, shaders, pipes, texhost, pcache, other > 0 ? (uint64_t)other : 0};
|
||||||
}
|
}
|
||||||
|
|||||||
65
tests/mem_budget_check.c
Normal file
65
tests/mem_budget_check.c
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
// runtime/mem_budget.h: a side's room as a free share and the level it gives, roomy, normal, tight or critical, with
|
||||||
|
// the hysteresis that keeps a share hovering at a line from flipping the level. Built and run by
|
||||||
|
// tests/mem_budget_check.py; prints each failed check and exits 1 on any failure.
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "mem_budget.h"
|
||||||
|
|
||||||
|
static int fails;
|
||||||
|
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
|
||||||
|
|
||||||
|
static MemRoom room_pm(uint32_t pm) { // a share on a 32 GiB side
|
||||||
|
MemRoom r = {pm, 32768ull * pm / 1000};
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
// the room itself
|
||||||
|
MemRoom r = mem_room(24000, 32000);
|
||||||
|
CHECK(r.free_pm == 250 && r.free_mb == 8000, "8000 of 32000 free: 250 per mille (%u, %llu)", r.free_pm,
|
||||||
|
(unsigned long long)r.free_mb);
|
||||||
|
r = mem_room(33000, 32000);
|
||||||
|
CHECK(r.free_pm == 0 && r.free_mb == 0, "use past the limit: no room");
|
||||||
|
r = mem_room(10, 0);
|
||||||
|
CHECK(r.free_pm == 0 && r.free_mb == 0, "no limit known: no room");
|
||||||
|
MemRoom a = {300, 9000}, b = {500, 4000};
|
||||||
|
r = mem_room_min(a, b);
|
||||||
|
CHECK(r.free_pm == 300 && r.free_mb == 4000, "the tighter share and the smaller amount");
|
||||||
|
|
||||||
|
// levels from a first sample (from roomy, as mem_budget.c starts)
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(550)) == MEM_ROOMY, "55%% free: roomy");
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(400)) == MEM_NORMAL, "40%% free: normal (the line belongs below)");
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(201)) == MEM_NORMAL, "20.1%% free: normal");
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(200)) == MEM_TIGHT, "20%% free: tight");
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(81)) == MEM_TIGHT, "8.1%% free: tight");
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(80)) == MEM_CRITICAL, "8%% free: critical");
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(0)) == MEM_CRITICAL, "nothing free: critical");
|
||||||
|
MemRoom small = {600, 200}; // a large share of a tiny side
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, small) == MEM_CRITICAL, "less than MEM_FLOOR_MB free is critical at any share");
|
||||||
|
|
||||||
|
// down at once, up only past the line by MEM_HYST_PM
|
||||||
|
CHECK(mem_level_next(MEM_NORMAL, room_pm(150)) == MEM_TIGHT, "normal to tight at once");
|
||||||
|
CHECK(mem_level_next(MEM_NORMAL, room_pm(50)) == MEM_CRITICAL, "normal to critical at once");
|
||||||
|
CHECK(mem_level_next(MEM_TIGHT, room_pm(220)) == MEM_TIGHT, "tight stays tight at 22%%");
|
||||||
|
CHECK(mem_level_next(MEM_TIGHT, room_pm(250)) == MEM_TIGHT, "tight stays tight at 25%% (the line plus 5 points)");
|
||||||
|
CHECK(mem_level_next(MEM_TIGHT, room_pm(251)) == MEM_NORMAL, "tight to normal past 25%%");
|
||||||
|
CHECK(mem_level_next(MEM_CRITICAL, room_pm(100)) == MEM_CRITICAL, "critical stays at 10%%");
|
||||||
|
CHECK(mem_level_next(MEM_CRITICAL, room_pm(140)) == MEM_TIGHT, "critical to tight past 13%%");
|
||||||
|
CHECK(mem_level_next(MEM_CRITICAL, room_pm(700)) == MEM_ROOMY, "critical to roomy in one step when there is room");
|
||||||
|
CHECK(mem_level_next(MEM_CRITICAL, room_pm(420)) == MEM_NORMAL, "42%%: past normal's hysteresis, not roomy's");
|
||||||
|
CHECK(mem_level_next(MEM_NORMAL, room_pm(430)) == MEM_NORMAL, "normal stays at 43%%");
|
||||||
|
CHECK(mem_level_next(MEM_NORMAL, room_pm(451)) == MEM_ROOMY, "normal to roomy past 45%%");
|
||||||
|
CHECK(mem_level_next(MEM_ROOMY, room_pm(390)) == MEM_NORMAL, "roomy to normal below 40%%");
|
||||||
|
|
||||||
|
// a share that hovers around the 20% line changes the level once, not at every sample
|
||||||
|
int level = MEM_NORMAL, changes = 0;
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
int n = mem_level_next(level, room_pm(185 + (uint32_t)(i * 7 % 31))); // 18.5% to 21.5%
|
||||||
|
changes += n != level, level = n;
|
||||||
|
}
|
||||||
|
CHECK(changes == 1 && level == MEM_TIGHT, "hovering at 20%%: one change (%d), ends tight (%d)", changes, level);
|
||||||
|
|
||||||
|
if (fails) printf("mem_budget_check: %d checks failed\n", fails);
|
||||||
|
else printf("mem_budget_check: all checks passed\n");
|
||||||
|
return fails != 0;
|
||||||
|
}
|
||||||
41
tests/mem_budget_check.py
Normal file
41
tests/mem_budget_check.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
"""Build and run tests/mem_budget_check.c: runtime/mem_budget.h, which turns the free share of the video memory and
|
||||||
|
of the system memory into a level (roomy, normal, tight, critical) with hysteresis, for the owners of memory that can
|
||||||
|
be given back.
|
||||||
|
|
||||||
|
No GPU, no game files, any Python.
|
||||||
|
|
||||||
|
Usage: mem_budget_check.py
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
||||||
|
|
||||||
|
|
||||||
|
def compiler():
|
||||||
|
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
|
||||||
|
if os.environ.get('CC'):
|
||||||
|
return os.environ['CC']
|
||||||
|
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
||||||
|
return str(MSYS_BIN / 'clang.exe')
|
||||||
|
return shutil.which('clang') or 'clang'
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
work = ROOT / 'build' / 'test' / 'mem_budget'
|
||||||
|
work.mkdir(parents=True, exist_ok=True)
|
||||||
|
exe = work / ('mem_budget_check.exe' if os.name == 'nt' else 'mem_budget_check')
|
||||||
|
env = dict(os.environ)
|
||||||
|
if os.name == 'nt' and MSYS_BIN.exists():
|
||||||
|
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
||||||
|
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
|
||||||
|
str(ROOT / 'tests' / 'mem_budget_check.c'), '-o', str(exe)], env=env)
|
||||||
|
return subprocess.run([str(exe)], env=env).returncode
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
Reference in New Issue
Block a user