diff --git a/docs/README-BuildKit.md b/docs/README-BuildKit.md index c5330081..6328d01e 100644 --- a/docs/README-BuildKit.md +++ b/docs/README-BuildKit.md @@ -105,6 +105,12 @@ the folder unless the environment sets `__GL_SHADER_DISK_CACHE_PATH`, and a limi folder and limit; the log says which (`vk: NVIDIA's shader disk cache`). Deleting the folder costs one slow start; `./ocarina --warm-shaders` fills it again. +The game reads the memory it may use: the limit of its cgroup v2 (`systemd-run --user --scope -p MemoryMax=16G +ocarina`, a container's) and the machine's `MemAvailable`, whichever leaves less. Linux does not fail an allocation when +memory runs short, it ends the process, so while less than the larger of 4 GB and a tenth of that limit is left the game +drops stale textures and evicts idle ones. The log names the limits at the first check (`platform: memory limits`). That +cannot give back what the game needs after its start: about 10 GB in Kokiri Forest at a 2160p game resolution. + `tools/make_appimage.py` packs this kit into one `NX2navi-Linux--x86_64.AppImage` with its own Python, for players without a source checkout: it checks for the packages above, installs zbic with pip on its first run, picks `--jobs` from the memory, builds into `~/Games/Ocarina` (or the folder given after the dump) and writes `play.sh` diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 66978fbe..66cd4135 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -113,7 +113,8 @@ # 1: very large compute shaders that no cache holds are built on one background thread after the start. 0: each is # built when the game first uses it, which can hold that frame for a second or two. # lib_prebuild_big_bg = 1 -# The commit headroom in MB those background builds wait for (0: no check). +# The commit headroom in MB those background builds wait for (0: no check). On Linux the headroom is the smaller of +# the room left under the cgroup's memory limit (memory.max, memory.high) and the memory available on the machine. # big_build_free_mb = 8192 # How far those background builds may raise the process commit in MB before the rest wait for their first use instead # (0: no limit). Each large shader costs the driver 1-4 GB; on a 31 GB machine four of them took the process from 15 to @@ -190,7 +191,8 @@ # tex_vram_pressure = 80 # Megabytes this process may commit before textures give way as they do while the system's commit runs low (Windows # counts video memory against the commit). Default: the installed memory, at least 24576, so that it does not depend -# on the size of the page file. 0: no bound of its own; only the system's commit counts. +# on the size of the page file. 0: no bound of its own; only the system's commit counts. On Linux it counts the +# process's resident anonymous memory, which stays under the installed memory, so there only a smaller value holds. # tex_proc_commit_mb = 32768 # 1: a texture the game releases or takes back, with nothing else changed at its address, puts the texture table's # slots there back in a state they were in before, and they are not all looked up again (large towns release and take diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 60cf2c6d..e9c2f21e 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -1795,39 +1796,230 @@ void plat_process_memory(uint64_t *resident, uint64_t *private_bytes) { fclose(f); } -// Linux overcommits by default, so CommitLimit binds only with vm.overcommit_memory = 2; Committed_AS is what the -// system has promised. The process's share is its resident anonymous memory, the nearest to Windows' private bytes. -void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb) { - *proc_mb = *used_mb = *limit_mb = -1; +// ---- memory limits +// +// Linux overcommits, so memory that runs short does not fail an allocation: the OOM killer ends the process, when it +// outgrew its cgroup's memory.max or when the machine's RAM is gone, and Committed_AS against CommitLimit (which binds +// only with vm.overcommit_memory = 2) says neither. plat_commit_info reports the limit with the least headroom of +// - each cgroup v2 level from this process's cgroup up to the root that sets memory.max or memory.high (the smaller; +// the kernel throttles at memory.high), against that level's memory.current less its file cache (memory.stat file +// without shmem): the kernel drops the cache before it ends anything, so a cache that filled the limit is no +// shortage; +// - the machine's RAM: MemTotal against MemTotal less MemAvailable; +// - with vm.overcommit_memory = 2 only, where an allocation does fail: CommitLimit against Committed_AS. +// The process's share is its resident anonymous memory, the nearest to Windows' private bytes. The sweeps that ask +// run several times a second, so a read is reused for 250 ms. The first read keeps every limit it found as text, which +// the runtime logs (plat_commit_note): stderr alone does not reach the log file when the launcher starts the game. + +static char cg_leaf[PATH_MAX]; // this process's cgroup v2 directory ("" when it has none) +static size_t cg_floor; // the length of the cgroup2 mount point at the start of cg_leaf + +// The process's cgroup v2 directory: its path from /proc/self/cgroup below the root of the cgroup2 mount that +// /proc/self/mountinfo shows (a container's own cgroup is mounted as the root), else the nearest directory above it +// that exists. +static void cg_locate(void) { + char path[PATH_MAX] = "", mnt[PATH_MAX] = "", root[PATH_MAX] = "", line[2 * PATH_MAX]; + FILE *f = fopen("/proc/self/cgroup", "r"); + if (f) { + while (fgets(line, sizeof line, f)) + if (!strncmp(line, "0::", 3)) { + line[strcspn(line, "\n")] = 0; + snprintf(path, sizeof path, "%s", line + 3); + } + fclose(f); + } + f = path[0] ? fopen("/proc/self/mountinfo", "r") : NULL; + while (f && fgets(line, sizeof line, f)) { + char r[PATH_MAX], m[PATH_MAX]; + if (!strstr(line, " - cgroup2 ") || sscanf(line, "%*d %*d %*s %4095s %4095s", r, m) != 2) continue; + snprintf(root, sizeof root, "%s", r); + snprintf(mnt, sizeof mnt, "%s", m); + break; + } + if (f) fclose(f); + if (!mnt[0]) return; + size_t rl = strlen(root); + const char *rel = path; + // A mount of a cgroup below the root of the hierarchy shows the process's path without that prefix. + if (rl > 1 && !strncmp(path, root, rl) && (!path[rl] || path[rl] == '/')) rel = path + rl; + else if (rl > 1) rel = ""; + if (!strcmp(rel, "/")) rel = ""; + snprintf(cg_leaf, sizeof cg_leaf, "%s%s", mnt, rel); + cg_floor = strlen(mnt); + struct stat st; + while (stat(cg_leaf, &st) || !S_ISDIR(st.st_mode)) { // not shown by the mount: the nearest directory above + char *s = strrchr(cg_leaf, '/'); + if (!s || (size_t)(s - cg_leaf) < cg_floor) { + cg_leaf[0] = 0; + return; + } + *s = 0; + } +} + +// A cgroup file into buf as text; 0 when it is missing (the controller is not enabled there). +static int cg_file(const char *dir, const char *name, char *buf, size_t n) { + char p[PATH_MAX + 32]; + snprintf(p, sizeof p, "%s/%s", dir, name); + int fd = open(p, O_RDONLY | O_CLOEXEC); + if (fd < 0) return 0; + ssize_t r = read(fd, buf, n - 1); + close(fd); + if (r <= 0) return 0; + buf[r] = 0; + return 1; +} + +// A byte limit file: its value in bytes, -1 for "max" or when missing. +static double cg_bytes(const char *dir, const char *name) { + char b[64]; + if (!cg_file(dir, name, b, sizeof b) || !strncmp(b, "max", 3)) return -1; + return (double)strtoull(b, NULL, 10); +} + +// A memory.stat counter in bytes, 0 when it is missing. +static double cg_stat(const char *stat, const char *key) { + size_t kl = strlen(key); + for (const char *p = stat; p; p = strchr(p, '\n')) { + if (*p == '\n') p++; + if (!strncmp(p, key, kl) && p[kl] == ' ') return (double)strtoull(p + kl + 1, NULL, 10); + } + return 0; +} + +typedef struct { + double proc, used, limit; // MB; -1 when unknown + const char *who; // the limit that is the tightest, for the first read's log line + uint64_t at; // plat_now_ms of the read, 0 before the first +} CommitSnap; + +// Makes (limit, used) the snapshot's when its headroom is smaller than the one held. +static void commit_pick(CommitSnap *s, double limit, double used, const char *who) { + if (s->limit < 0 || limit - used < s->limit - s->used) s->limit = limit, s->used = used, s->who = who; +} + +// Appends one limit found to the note of the first read ("; " between them; a full note takes no more). +static void note_add(char *note, const char *fmt, ...) __attribute__((format(printf, 2, 3))); +#define NOTE_MAX 2048 +static void note_add(char *note, const char *fmt, ...) { + size_t n = strlen(note); + if (n + 3 >= NOTE_MAX) return; + if (n) memcpy(note + n, "; ", 3), n += 2; + va_list ap; + va_start(ap, fmt); + vsnprintf(note + n, NOTE_MAX - n, fmt, ap); + va_end(ap); +} + +// The cgroups from the process's up to the root that limit memory. +static void commit_cgroups(CommitSnap *s, char *note) { + char d[PATH_MAX]; + snprintf(d, sizeof d, "%s", cg_leaf); + while (d[0]) { + double max = cg_bytes(d, "memory.max"), high = cg_bytes(d, "memory.high"); + int use_high = max < 0 || (high >= 0 && high < max); + double lim = use_high ? high : max; + double cur = lim < 0 ? -1 : cg_bytes(d, "memory.current"); + if (cur >= 0) { + char st[4096] = ""; // memory.stat starts with anon and file; the counters needed lie in its first lines + cg_file(d, "memory.stat", st, sizeof st); + double cache = cg_stat(st, "file") - cg_stat(st, "shmem"); + cache = cache < 0 ? 0 : cache > cur ? cur : cache; + commit_pick(s, lim / 1048576.0, (cur - cache) / 1048576.0, + use_high ? "cgroup memory.high" : "cgroup memory.max"); + if (note) + note_add(note, "cgroup %s %s %.0f MB (%.0f MB in use, %.0f MB of it file cache)", + d[cg_floor] ? d + cg_floor : "/", use_high ? "memory.high" : "memory.max", lim / 1048576.0, + cur / 1048576.0, cache / 1048576.0); + } + char *sl = strrchr(d, '/'); + if (strlen(d) <= cg_floor || !sl || (size_t)(sl - d) < cg_floor) break; + *sl = 0; + } +} + +// The machine's RAM, and the system's commit when vm.overcommit_memory = 2. +static void commit_machine(CommitSnap *s, char *note) { + static int overcommit = -1; + unsigned long long total = 0, avail = 0, cl = 0, as = 0, k; char line[128]; - unsigned long long k; FILE *f = fopen("/proc/meminfo", "r"); if (f) { while (fgets(line, sizeof line, f)) { - if (sscanf(line, "CommitLimit: %llu kB", &k) == 1) *limit_mb = (double)(k >> 10); - else if (sscanf(line, "Committed_AS: %llu kB", &k) == 1) *used_mb = (double)(k >> 10); + if (sscanf(line, "MemTotal: %llu kB", &k) == 1) total = k; + else if (sscanf(line, "MemAvailable: %llu kB", &k) == 1) avail = k; + else if (sscanf(line, "CommitLimit: %llu kB", &k) == 1) cl = k; + else if (sscanf(line, "Committed_AS: %llu kB", &k) == 1) as = k; } fclose(f); } + if (total && avail && avail <= total) { + commit_pick(s, (double)total / 1024.0, (double)(total - avail) / 1024.0, "RAM"); + if (note) note_add(note, "RAM %.0f MB (%.0f MB available)", (double)total / 1024.0, (double)avail / 1024.0); + } + if (overcommit < 0) { + FILE *o = fopen("/proc/sys/vm/overcommit_memory", "r"); + if (!o || fscanf(o, "%d", &overcommit) != 1) overcommit = 0; + if (o) fclose(o); + } + if (overcommit == 2 && cl) { + commit_pick(s, (double)cl / 1024.0, (double)as / 1024.0, "commit limit"); + if (note) + note_add(note, "commit limit %.0f MB (%.0f MB committed, vm.overcommit_memory = 2)", (double)cl / 1024.0, + (double)as / 1024.0); + } +} + +static char commit_note[NOTE_MAX + 128]; // the first read's limits (plat_commit_note); not written after it + +static void commit_read(CommitSnap *s, int first) { + char note[NOTE_MAX] = ""; + s->proc = s->used = s->limit = -1; + s->who = NULL; + if (first) cg_locate(); + commit_cgroups(s, first ? note : NULL); + commit_machine(s, first ? note : NULL); uint64_t resident, private_bytes; plat_process_memory(&resident, &private_bytes); - if (private_bytes) *proc_mb = (double)(private_bytes >> 20); + if (private_bytes) s->proc = (double)(private_bytes >> 20); + if (first) { + if (note[0]) + snprintf(commit_note, sizeof commit_note, + "memory limits: %s; the one with the least headroom counts (now: %s)", note, s->who); + else snprintf(commit_note, sizeof commit_note, "no memory limit readable"); + } +} + +void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb) { + static PlatMutex lock = PLAT_MUTEX_INIT; + static CommitSnap snap; + plat_mutex_lock(&lock); + uint64_t now = plat_now_ms(); + if (!snap.at || now - snap.at >= 250) { + commit_read(&snap, !snap.at); + snap.at = now ? now : 1; + } + *proc_mb = snap.proc, *used_mb = snap.used, *limit_mb = snap.limit; + plat_mutex_unlock(&lock); } int plat_commit_enforced(void) { - static int v = -1; // read once - if (v < 0) { - int mode = 0; - FILE *f = fopen("/proc/sys/vm/overcommit_memory", "r"); - if (f) { - if (fscanf(f, "%d", &mode) != 1) mode = 0; - fclose(f); - } - v = mode == 2; - } - return v; + double proc, used, limit; + plat_commit_info(&proc, &used, &limit); + return limit > 0; } +// The first read wrote the note under plat_commit_info's lock; taking that lock here orders the copy after it. +int plat_commit_note(char *out, size_t cap) { + double proc, used, limit; + plat_commit_info(&proc, &used, &limit); + snprintf(out, cap, "%s", commit_note); + return out[0] != 0; +} + +// The cgroup's memory.current and MemAvailable count system memory only: a graphics card's own memory is in neither. +int plat_commit_counts_vram(void) { return 0; } + // ---- child processes struct PlatChild { diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 09e9dad3..b02c7d75 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -457,9 +457,20 @@ void plat_gpu_load(PlatLoad *l, float *load_pct, float *vram_used_mb); void plat_mem_info(float *used_mb, float *total_mb, float *proc_mb); // Commit charge in MB: this process's private bytes, the system's commit in use and its limit (RAM plus page files). // An allocation fails when the system's reaches the limit, however much physical memory is free. -1 when unknown. +// Linux overcommits and ends the process instead (the OOM killer), so there the limit is the one with the least +// headroom of the cgroup v2 limits (memory.max, memory.high) the process lives under and the machine's RAM +// (MemAvailable against MemTotal), with vm.overcommit_memory = 2 also CommitLimit. void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb); -// Whether an allocation fails at that limit: always on Windows; on Linux only with vm.overcommit_memory = 2. +// Whether the limit is one to stay below: always on Windows (an allocation fails at it); on Linux whenever the kernel +// reports a memory limit. 0 when none is known. int plat_commit_enforced(void); +// The limits the first plat_commit_info call found, for the log, as text into out ("memory limits: cgroup ... memory.max +// 12288 MB (...); RAM ...; the one with the least headroom counts (now: ...)"); 0 when there is nothing to add to the +// numbers (Windows, whose one limit is the system's commit). +int plat_commit_note(char *out, size_t cap); +// Whether a discrete GPU's own memory counts against that limit: on Windows the driver charges device memory to the +// system's commit; Linux's limits count system memory only. +int plat_commit_counts_vram(void); // This process's memory in bytes: resident (the working set) and private (Windows: committed private bytes; Linux: the // resident anonymous pages). 0 when unknown. void plat_process_memory(uint64_t *resident, uint64_t *private_bytes); diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index d33dac69..363cccc5 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1888,6 +1888,15 @@ void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb) { int plat_commit_enforced(void) { return 1; } +// The numbers name the one limit there is, the system's commit. +int plat_commit_note(char *out, size_t cap) { + if (cap) out[0] = 0; + return 0; +} + +// The driver charges the device memory it allocates to the system's commit. +int plat_commit_counts_vram(void) { return 1; } + void plat_process_memory(uint64_t *resident, uint64_t *private_bytes) { PROCESS_MEMORY_COUNTERS_EX pm = {.cb = sizeof pm}; *resident = *private_bytes = 0; diff --git a/runtime/vk_headroom.c b/runtime/vk_headroom.c index 7bd24714..481e8eed 100644 --- a/runtime/vk_headroom.c +++ b/runtime/vk_headroom.c @@ -19,8 +19,11 @@ // until the next free of its type, and these types seldom free anything. // Under VRAM pressure no headroom is kept: the spare block counts against the budget that decides whether a pool gets // its VRAM mirror (vk_memory.c), so while use plus two blocks passes 85% of the budget the anchor goes and nothing new -// is allocated. Nor while the system's commit is low (vk_texture_commit_low): the driver charges device memory to it, -// and a spare block of each type held up to 256 MiB of it empty when an allocation that failed there ended the game. +// is allocated. Nor while the system's commit is low (vk_texture_commit_low) where device memory counts against it: +// on Windows the driver charges device memory to the commit, and a spare block of each type held up to 256 MiB of it +// empty when an allocation that failed there ended the game; an integrated GPU's memory is the machine's RAM. Linux's +// limits (plat_commit_counts_vram) do not count a discrete card's own memory, so dropping its spare would free nothing +// they count and only put the block allocations back on the render thread. // NX2NAVI_VRAM_HEADROOM=0 turns it off. #include "cfg.h" @@ -106,7 +109,9 @@ static VkDeviceSize block_size(const VkCtx *c, uint32_t type) { } static int room(const VkCtx *c, uint32_t type, VkDeviceSize block) { - if (vk_texture_commit_low()) return 0; + if (vk_texture_commit_low() && + (plat_commit_counts_vram() || c->props.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)) + return 0; VmaBudget b[VK_MAX_MEMORY_HEAPS]; vmaGetHeapBudgets(c->vma, b); const VmaBudget *h = &b[c->mem_props.memoryTypes[type].heapIndex]; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 4c01d53f..8faa319a 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1105,7 +1105,9 @@ static int idle(const Tex *e, uint64_t now); // commit then ran out at an area load (device lost); only entries released STALE_FRAMES presents ago go, so the // transient ones the game takes back within frames stay. NX2NAVI_TEX_COMMIT_FREE_MB (default the larger of 4 GiB and a tenth of the commit // limit; 0 = off) lowers that bound to COMMIT_LOW_KEEP while less of the system's commit is free: an allocation that -// fails there ends the game. Both drop only stale entries (drop_stale), a few per check like the VRAM pressure. +// fails there ends the game. On Linux nothing fails at the limit: the OOM killer ends the game when its cgroup or the +// machine runs out of memory, and plat_commit_info reports the tighter of the two. Both drop only stale entries +// (drop_stale), a few per check like the VRAM pressure. // // NX2NAVI_TEX_PROC_COMMIT_MB (default the installed memory, at least PROC_COMMIT_MIN_MB; 0 = off) counts the commit as // low too while this process's own commit is above it. The system's figure depends on the page file and on what else @@ -1114,7 +1116,9 @@ static int idle(const Tex *e, uint64_t now); // more commit than the PC has memory: beyond that it lives on the page file, whatever its size. The bound holds until // the process is a thirty-second under it, so that it does not flip with every texture freed. The floor keeps it out // of the way on PCs with less memory, where a bound at the installed memory could hold all through play: the system's -// figure decides there, as before. +// figure decides there, as before. On Linux the process's figure is its resident anonymous memory, which cannot pass +// the installed memory, so only a bound set below that holds there; a cgroup's limit and the machine's RAM act through +// the system's figure. #define COMMIT_LOW_KEEP ((uint64_t)256 << 20) #define PROC_COMMIT_MIN_MB 24576 static uint64_t released_cap; // bytes; 0 = not read yet, UINT64_MAX = no limit @@ -1154,7 +1158,9 @@ static int commit_is_low(void) { plat_mem_info(&ram_used, &ram_total, &ram_proc); proc_commit_mb = ram_total > PROC_COMMIT_MIN_MB ? (long long)ram_total : PROC_COMMIT_MIN_MB; } - if (!plat_commit_enforced()) commit_free_mb = proc_commit_mb = 0; // a limit no allocation fails at + if (!plat_commit_enforced()) commit_free_mb = proc_commit_mb = 0; // no limit known + char note[2048]; // the platform layer's own lines go to stderr only, so the log file gets this through here + if (plat_commit_note(note, sizeof note)) rt_log("platform: %s", note); if (proc_commit_mb) rt_log("vk: textures give way as under low commit while this process's commit is above %lld MB " "(NX2NAVI_TEX_PROC_COMMIT_MB; 0 = off)", proc_commit_mb);