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.

This commit is contained in:
Wellington Dias committed 2026-10-31 02:03:34 -03:00
1 parent aaa9610aaf
commit 01712150a4
4 files changed
+19 -10

No files matched your search

+1 -1
View File
@@ -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);
+13 -6
View File
@@ -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;
}
+3 -2
View File
@@ -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.
+2 -1
View File
@@ -1128,7 +1128,8 @@ struct PlatLoad {
wchar_t luid[40]; // "luid_0x<high>_0x<low>" 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