From 962f8dfd1b13f12d21013a55d820b93318ac84d9 Mon Sep 17 00:00:00 2001 From: "Alexander Cusaac (MightyChubz)" Date: Wed, 3 Jun 2026 16:47:37 -0400 Subject: [PATCH] refactor: Add fallback to `sysinfo()` if `/proc/meminfo` can't be read This is likely to never _actually_ execute, and the possibility of this failing is extremely slim, but if this _does_ fail, then this fallback should catch it, as long as I'm understanding things correctly. If the fallback fails, we just go back to the original assert that existed before. --- core/sys/info/platform_linux.odin | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/core/sys/info/platform_linux.odin b/core/sys/info/platform_linux.odin index 7ef58cbb6..a693e92fc 100644 --- a/core/sys/info/platform_linux.odin +++ b/core/sys/info/platform_linux.odin @@ -86,8 +86,21 @@ _ram_stats :: proc() -> (total_ram, free_ram, total_swap, free_swap: i64, ok: bo fd, errno := linux.open("/proc/meminfo", {}) if errno != .NONE { // This should never happen since something would be wrong with the system - // if /proc/meminfo wasn't able to be opened for any reason - return 0, 0, 0, 0, false + // if /proc/meminfo wasn't able to be opened for any reason. But, in the + // event that this _does_ happen, let's just try to recover through the + // syscall + sys_info: linux.Sys_Info + sysinfo_errno := linux.sysinfo(&sys_info) + assert_contextless(sysinfo_errno == .NONE, "If this has failed, there is no recovery from this") + + total_ram = i64(sys_info.totalram) * i64(sys_info.mem_unit) + free_ram = i64(sys_info.freeram) * i64(sys_info.mem_unit) + total_swap = i64(sys_info.totalswap) * i64(sys_info.mem_unit) + free_swap = i64(sys_info.freeswap) * i64(sys_info.mem_unit) + + ok = true + + return } defer linux.close(fd) @@ -96,7 +109,18 @@ _ram_stats :: proc() -> (total_ram, free_ram, total_swap, free_swap: i64, ok: bo meminfo_buf: [4096]u8 n, read_errno := linux.read(fd, meminfo_buf[:]) if read_errno != .NONE { - return 0, 0, 0, 0, false + sys_info: linux.Sys_Info + sysinfo_errno := linux.sysinfo(&sys_info) + assert_contextless(sysinfo_errno == .NONE, "If this has failed, there is no recovery from this") + + total_ram = i64(sys_info.totalram) * i64(sys_info.mem_unit) + free_ram = i64(sys_info.freeram) * i64(sys_info.mem_unit) + total_swap = i64(sys_info.totalswap) * i64(sys_info.mem_unit) + free_swap = i64(sys_info.freeswap) * i64(sys_info.mem_unit) + + ok = true + + return } meminfo := string(meminfo_buf[:n])