From 01712150a4d97fd631b81e5d0e5e6972f718fc35 Mon Sep 17 00:00:00 2001 From: Wellington Dias Date: Sat, 31 Oct 2026 02:03:34 -0300 Subject: [PATCH] Read the overlay's GPU load and VRAM from the DRM card on the game GPU's PCI bus, so a machine with an AMD iGPU and an NVIDIA GPU no longer shows the idle iGPU's numbers. --- runtime/perfmon.c | 2 +- runtime/platform/linux.c | 19 +++++++++++++------ runtime/platform/platform.h | 5 +++-- runtime/platform/win32.c | 3 ++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/runtime/perfmon.c b/runtime/perfmon.c index 1b2b2de1..3a5e2f28 100644 --- a/runtime/perfmon.c +++ b/runtime/perfmon.c @@ -304,7 +304,7 @@ static void sampler(void *arg) { sysinfo_cpu_name(s.cpu_name, sizeof s.cpu_name); if (!nv.tried) nvml_open(s.gpu_name); if (!nv.ok && !adl.tried) adl_open(s.gpu_name, bus); - PlatLoad *l = plat_load_open(have_luid ? luid : NULL); + PlatLoad *l = plat_load_open(have_luid ? luid : NULL, bus); plat_mutex_lock(&g_lock); while (g_smp.want) { plat_mutex_unlock(&g_lock); diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index cc9d07a9..6ea783ea 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1442,9 +1442,10 @@ static void find_cpu_temp(char *out, size_t cap) { closedir(d); } -// The DRM card with gpu_busy_percent (amdgpu); with several, the one with the most VRAM in use, as the game's device -// is not known by LUID on Linux. -static void find_gpu(char *out, size_t cap) { +// The DRM card with gpu_busy_percent (amdgpu) on PCI bus bus, the game's device. With bus -1 (no VK_EXT_pci_bus_info): +// of several, the one with the most VRAM in use. None when the game's GPU has no such file (NVIDIA): the overlay then +// takes its load and VRAM from NVML, not from an idle integrated GPU. +static void find_gpu(char *out, size_t cap, int bus) { out[0] = 0; uint64_t best = 0; DIR *d = opendir("/sys/class/drm"); @@ -1456,6 +1457,12 @@ static void find_gpu(char *out, size_t cap) { snprintf(path, sizeof path, "%s/gpu_busy_percent", dev); uint64_t v, used = 0; if (!read_u64_file(path, &v)) continue; + if (bus >= 0) { // device links to the PCI function, "0000:03:00.0": domain, bus, device, function + char real[PATH_MAX]; + unsigned dom, b; + const char *base = realpath(dev, real) ? strrchr(real, '/') : NULL; + if (!base || sscanf(base + 1, "%x:%x:", &dom, &b) != 2 || (int)b != bus) continue; + } snprintf(path, sizeof path, "%s/mem_info_vram_used", dev); read_u64_file(path, &used); if (!out[0] || used > best) snprintf(out, cap, "%s", dev), best = used; @@ -1463,12 +1470,12 @@ static void find_gpu(char *out, size_t cap) { closedir(d); } -PlatLoad *plat_load_open(const uint8_t *gpu_luid) { - (void)gpu_luid; +PlatLoad *plat_load_open(const uint8_t *gpu_luid, int pci_bus) { + (void)gpu_luid; // Linux has no adapter LUIDs; the PCI bus names the card PlatLoad *l = calloc(1, sizeof *l); if (!l) return NULL; find_cpu_temp(l->temp, sizeof l->temp); - find_gpu(l->gpu, sizeof l->gpu); + find_gpu(l->gpu, sizeof l->gpu, pci_bus); return l; } diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index fcd5e57a..4836d4d9 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -357,8 +357,9 @@ void *plat_lib_sym(void *lib, const char *name); // the system does not give is -1. typedef struct PlatLoad PlatLoad; // Opens the counters: CPU times, the thermal zones, and the GPU engine and memory counters of the adapter whose LUID is -// gpu_luid (8 bytes, Vulkan's deviceLUID; NULL: the busiest adapter). Never NULL. -PlatLoad *plat_load_open(const uint8_t *gpu_luid); +// gpu_luid (8 bytes, Vulkan's deviceLUID; NULL: the busiest adapter) or, where the system names GPUs by PCI address, +// on PCI bus pci_bus (-1: unknown). Never NULL. +PlatLoad *plat_load_open(const uint8_t *gpu_luid, int pci_bus); void plat_load_close(PlatLoad *l); // Since the previous call on l (the first call gives -1): CPU use of the machine and of this process, percent of all // logical processors, and the hottest ACPI thermal zone in degrees Celsius. diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 5981a63e..d2684418 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1128,7 +1128,8 @@ struct PlatLoad { wchar_t luid[40]; // "luid_0x_0x" of the adapter, lower case; empty for any }; -PlatLoad *plat_load_open(const uint8_t *gpu_luid) { +PlatLoad *plat_load_open(const uint8_t *gpu_luid, int pci_bus) { + (void)pci_bus; // the LUID names the adapter PlatLoad *l = calloc(1, sizeof *l); if (!l) return NULL; if (gpu_luid) { // Vulkan's deviceLUID is the LUID struct's memory: LowPart, then HighPart