Repository navigation
obs-gpu-status: NVIDIA GPU utilization through NVML - #1233
Merged
Merged
Conversation
nvml::Nvml resolves NVML's functions by name from a Symbols source and calls them; platform code only opens the library. The tests give it a fake symbol table of real extern "C" functions (nvmlInit_v2, nvmlDeviceGetHandleByPciBusId_v2, nvmlDeviceGetUtilizationRates), so the resolve-and-call code runs as it does against libnvidia-ml. Nvml::load() is a stub that returns None, so the cases that need a loaded NVML fail: 37 % maps to Percent(37), a PCI address NVML does not know, an adapter without a PCI address, and a reading above 100 %. A missing library, a missing symbol and a failing nvmlInit_v2 already give Unavailable. The Linux candidates include NixOS's /run/opengl-driver/lib. Refs obs-rust#1021 Co-Authored-By: Claude Opus 5.5 <[email protected]>
nvml::Nvml::load() resolves nvmlInit_v2, nvmlDeviceGetHandleByPciBusId_v2 and nvmlDeviceGetUtilizationRates by name and runs nvmlInit_v2; a missing symbol or a failing init gives None. Nvml::utilization() looks the render adapter's PCI address up (NVML takes the sysfs form, 0000:2b:00.0) and returns the whole-GPU busy percent. Other adapters, an address NVML does not know and a reading above 100 % are Unavailable. platform/nvml_lib.rs opens the first candidate that loads and initialises, through libloading (now a dependency on every platform): libnvidia-ml.so.1, then /run/opengl-driver/lib/libnvidia-ml.so.1 (NixOS) on Linux; nvml.dll on Windows; none on macOS. platform::nvml() loads it once per process, and platform::gpu_utilization() combines it with the render adapter. The probe example prints the utilization and how long the first and a second call take. Windows needs the LUID mapped to a PCI address (D3DKMT KMTQAITYPE_ADAPTERADDRESS) before NVML can find the device; that is a follow-up. The device probe still runs nvidia-smi once per process (obs-rust#1172), not as polling. Refs obs-rust#1021 Co-Authored-By: Claude Opus 5.5 <[email protected]>
NVML gives per-process samples through nvmlDeviceGetProcessesUtilizationInfo (versioned struct) and the older nvmlDeviceGetProcessUtilization. Both use a size protocol (a null buffer gets NVML_ERROR_INSUFFICIENT_SIZE and a count) and a last-seen time stamp that limits the call to newer samples. On driver 595.104.02 (RTX 3060) the size query reports the driver's buffer capacity (72), not the sample count, and only the newer function returns the graphics processes (OBS, Xorg); the older one returns NVML_ERROR_NOT_FOUND. The fakes do the same. Nvml::process_utilization() is a stub that returns None, so the share cases fail: OBS's samples 20 % and 30 % (another process 50 %) must give 25 % through either function, the next call must ask only for samples after the newest seen (200), and the newer function must win where the older one finds nothing. A PID without samples and a driver without either symbol give None; the whole-GPU value works without them. Refs obs-rust#1021 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Nvml::process_utilization() reads per-process samples newer than the last one seen, through nvmlDeviceGetProcessesUtilizationInfo (struct version 1) or, on drivers without it, nvmlDeviceGetProcessUtilization. The size query's count is only a buffer size: the fill call sets the real count. The share is the mean SM utilization of the PID's samples in the window. Without either symbol, or without a sample for the PID, it is None, and the whole-GPU value still works. The driver reports PIDs in the caller's PID namespace: in an LXC container OBS finds its own samples under its container PID (checked on driver 595.104.02, RTX 3060). platform::obs_gpu_share() reads it for std::process::id(). The probe prints the share of a PID given after the hint. Refs obs-rust#1021 Co-Authored-By: Claude Opus 5.5 <[email protected]>
vpacher
force-pushed
the
feat/gpu-nvml
branch
from
October 17, 2026 12:28
b51fda5 to
64ad72d
Compare
3 of 6 tasks
2 tasks
This was referenced Oct 17, 2026
8 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1021
What and why
#1004 measured NVML at 0.68 ms per device, against 87 ms for spawning
nvidia-smi. This PR adds NVML as the utilization source for NVIDIA GPUs. It finds the device by the render adapter's PCI address from #1194. It returns the sharedobs_gpu_status::Utilizationfrom #1222, the same type that fdinfo (#1224) and PDH (#1228) use.This PR:
nvml::Nvml. It resolves NVML functions by name from aSymbolssource and calls them, so a fake symbol table tests the real calls.Nvml::load()resolvesnvmlInit_v2,nvmlDeviceGetHandleByPciBusId_v2andnvmlDeviceGetUtilizationRates. A missing symbol or a failed init givesNone.Nvml::utilization()returns the whole-GPU percent. Other adapters, an unknown address and a value above 100 giveUnavailable.Nvml::process_utilization()for the tooltip (OBS's own share). It usesnvmlDeviceGetProcessesUtilizationInfo(struct version 1), ornvmlDeviceGetProcessUtilizationon older drivers. It reads only samples newer than the last one seen for that PID. The share is the mean SM utilization of the PID's samples.platform/nvml_lib.rs. It opens the library throughlibloading:libnvidia-ml.so.1, then/run/opengl-driver/lib/libnvidia-ml.so.1(NixOS) on Linux, andnvml.dllon Windows. macOS has no candidates.platform::nvml()loads it once per process.platform::gpu_utilization()andplatform::obs_gpu_share()combine it with the render adapter.probeexample. It prints the utilization, the time of the first call (with the NVML load) and of later calls, and the share of a PID given after the hint.Not in this PR:
D3DKMTQueryAdapterInfo(KMTQAITYPE_ADAPTERADDRESS)). Until then,gpu_utilization()on Windows givesUnavailable. For this reason this PR uses Refs, not Fixes.lib.rs,platform/mod.rsandtests/gpu_status/main.rs. Both sides are kept; nothing else changed.RED → GREEN
Nvml::load()stub that returnsNone. 4 fail (37 % →Percent(37), unknown PCI address, adapter without a PCI address, value above 100). The cases for a missing library, a missing symbol and a failednvmlInit_v2pass with the stub.None. 4 fail: samples of 20 % and 30 % for OBS (50 % for another process) must give 25 % through each function, the next call must ask only for samples after the newest seen, and the newer function must win where the older one finds nothing.-D warningsis clean on macOS and forx86_64-pc-windows-msvc. fmt is clean.Real host
Debian 13 LXC container with an RTX 3060 (driver 595.104.02), not NixOS. The
/run/opengl-driver/libpath is covered by a test. I ran the probe there before the rebase onto #1222. The rebase changes only the return type to the sharedUtilization.nvmlDeviceGetProcessesUtilizationInfo. The older function givesNVML_ERROR_NOT_FOUND, and the size query returns the buffer capacity (72), not the sample count. The fakes do the same.Checklist
master(or the description starts with a Stacked: note)-W..., ratchets): built with scripting ON (a flag change under cmake/ or build-aux/ runs the full builds) and cross-checked with clang and MSVC (not applicable: no flag changes)🤖 Generated with Claude Code