From 31fff747767155a039f8a0a3f276fc36e35a671a Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 07:23:04 +0300 Subject: [PATCH] Log the memory commit charge with every fatal error #94 ended on a realloc of 1 MiB that failed with 64 GB of commit allowed, and the log could not tell whether the process had grown towards the limit or the system ran short. rt_fatal now adds one line with the process's private bytes and the system's commit in use and limit (plat_commit_info). Co-Authored-By: Claude Opus 5.5 --- runtime/dispatch.c | 6 ++++++ runtime/platform/platform.h | 3 +++ runtime/platform/win32.c | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/runtime/dispatch.c b/runtime/dispatch.c index e9bc46a9..71e326ab 100644 --- a/runtime/dispatch.c +++ b/runtime/dispatch.c @@ -12,6 +12,7 @@ // is also fixed at init; they are looked up only after the flat table misses. #include "dispatch.h" #include "host.h" +#include "platform/platform.h" #include #include @@ -51,6 +52,11 @@ void rt_fatal(const char *fmt, ...) { rt_vlogf(fmt, ap); rt_log_raw("\n", 1); va_end(ap); + // A failed allocation (malloc, realloc, VirtualAlloc) means the commit limit was reached: this line tells a leak of + // ours (process private bytes near the limit) from a machine short of page file. + double proc, used, limit; + plat_commit_info(&proc, &used, &limit); + rt_log("fatal: memory commit: this process %.0f MB, the system %.0f of %.0f MB", proc, used, limit); fflush(stdout); fflush(stderr); _Exit(70); diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 17898d8f..c5761651 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -116,6 +116,9 @@ void plat_cpu_load(PlatLoad *l, float *total_pct, float *proc_pct, float *temp_c void plat_gpu_load(PlatLoad *l, float *load_pct, float *vram_used_mb); // Physical memory in MB: in use and installed on the machine, and this process's working set. 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. +void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb); // ---- child processes: a program with stdin and stdout piped to us (stderr discarded), no console window. typedef struct PlatChild PlatChild; diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index c053d4d5..6ab2fbdb 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -613,6 +613,18 @@ void plat_mem_info(float *used_mb, float *total_mb, float *proc_mb) { if (K32GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof pmc)) *proc_mb = (float)(pmc.WorkingSetSize >> 20); } +void plat_commit_info(double *proc_mb, double *used_mb, double *limit_mb) { + *proc_mb = *used_mb = *limit_mb = -1; + MEMORYSTATUSEX ms = {.dwLength = sizeof ms}; + if (GlobalMemoryStatusEx(&ms)) { + *limit_mb = (double)(ms.ullTotalPageFile >> 20); + *used_mb = (double)((ms.ullTotalPageFile - ms.ullAvailPageFile) >> 20); + } + PROCESS_MEMORY_COUNTERS_EX pmc; + if (K32GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&pmc, sizeof pmc)) + *proc_mb = (double)(pmc.PrivateUsage >> 20); +} + // ---- child processes struct PlatChild {