Skip to content

obs-gpu-status: NVIDIA GPU utilization through NVML - #1233

Merged
zackees merged 4 commits into
obs-rust:masterfrom
vpacher:feat/gpu-nvml
Oct 17, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
vpacher:feat/gpu-nvml

Conversation

@vpacher

@vpacher vpacher commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

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 shared obs_gpu_status::Utilization from #1222, the same type that fdinfo (#1224) and PDH (#1228) use.

This PR:

  1. Adds nvml::Nvml. It resolves NVML functions by name from a Symbols source and calls them, so a fake symbol table tests the real calls. Nvml::load() resolves nvmlInit_v2, nvmlDeviceGetHandleByPciBusId_v2 and nvmlDeviceGetUtilizationRates. A missing symbol or a failed init gives None. Nvml::utilization() returns the whole-GPU percent. Other adapters, an unknown address and a value above 100 give Unavailable.
  2. Adds Nvml::process_utilization() for the tooltip (OBS's own share). It uses nvmlDeviceGetProcessesUtilizationInfo (struct version 1), or nvmlDeviceGetProcessUtilization on 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.
  3. Adds platform/nvml_lib.rs. It opens the library through libloading: libnvidia-ml.so.1, then /run/opengl-driver/lib/libnvidia-ml.so.1 (NixOS) on Linux, and nvml.dll on Windows. macOS has no candidates. platform::nvml() loads it once per process. platform::gpu_utilization() and platform::obs_gpu_share() combine it with the render adapter.
  4. Extends the probe example. 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:

RED → GREEN

  • RED 82449a3: fake symbol table tests with a Nvml::load() stub that returns None. 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 failed nvmlInit_v2 pass with the stub.
  • GREEN def61bd: 105/105 crate tests pass.
  • RED ff8ea73: process-share tests with a stub that returns 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.
  • GREEN 64ad72d: 111/111 crate tests pass on macOS. clippy -D warnings is clean on macOS and for x86_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/lib path is covered by a test. I ran the probe there before the rebase onto #1222. The rebase changes only the return type to the shared Utilization.

  • The whole-GPU value works in the container. The first call takes about 20-30 ms (NVML load), later calls about 20 µs.
  • Driver 595 returns graphics processes only through nvmlDeviceGetProcessesUtilizationInfo. The older function gives NVML_ERROR_NOT_FOUND, and the size query returns the buffer capacity (72), not the sample count. The fakes do the same.
  • NVML reports PIDs in the caller's PID namespace, so OBS in the container finds its own samples under its container PID. OBS's share was about 22-28 % SM.
  • When few new samples arrive (for example while a VNC session loads the GPU), some calls return no share. frontend: GPU: x% readout replaces the GPU status light #1025 should keep the last share for a few seconds to prevent flicker.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-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)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: new code, no C original)

🤖 Generated with Claude Code

@vpacher
vpacher requested a review from zackees as a code owner October 17, 2026 12:02
vpacher and others added 4 commits October 17, 2026 14:27
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]>
@zackees
zackees added this pull request to the merge queue Oct 17, 2026
Merged via the queue into obs-rust:master with commit a7566a2 Oct 17, 2026
25 of 26 checks passed
@vpacher
vpacher deleted the feat/gpu-nvml branch October 17, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants