NVML GPU Governor
Workload-aware GPU scheduler
A Windows controller that yields one NVIDIA GPU from a background compute job to whatever the person at the machine is doing.
Problem
A background GPU compute job and an interactive desktop want the same card. Sharing it by hand means stopping the job every time something else needs the GPU.
The governor makes that call four times a second.
Execution
A Windows PowerShell 5.1 loop with C# compiled in for P/Invoke. Every 250 ms it samples the machine, picks a tier, and acts.
It calls nvml.dll directly for GPU busy percentage, core temperature, free VRAM, and clock offsets in both P0 and P2, which nvidia-smi cannot set. From Win32 it reads exclusive-fullscreen state with SHQueryUserNotificationState and idle time with GetLastInputInfo. Per-process GPU Engine performance counters give every other application’s load, with 3D and compute kept apart from hardware video decode.
- nvml.dll
- GPU busy %, core temperature, free VRAM
- Win32
- Exclusive fullscreen state and user idle seconds
- GPU Engine
- Per-PID 3D/compute load, separated from hardware video decode
- GAME / PROTECTED
- Exclusive fullscreen 3D or listed foreground process
- OFF
- VRAM or RAM below floor for 3 s (300 s minimum hold)
- LIGHT
- Other GPU load active — share ramps 0.2 to 0.9
- FULL
- Card idle — full share plus away profile with 85 C thermal latch
- Stop
- Halt background worker and restore stock GPU clocks and power limit
- Throttle
- 200 ms NtSuspendProcess / NtResumeProcess duty cycle
- Full
- Apply per-workload power limit and P0/P2 NVML clock offsets
Atomic status.json + controller.log drive the Rust ratatui dashboard and CLI.
The decision is one pure function. A 3D application in exclusive fullscreen, or a listed process, stops the job on the next tick. Other GPU load throttles it instead: a full share up to 10% load, down to a 20% share at 50%, applied as a duty cycle that suspends the job’s processes for part of every 200 ms. Drops act at once; rises wait out a 60-second hold.
Low memory stops the job outright, because throttling cannot give memory back: three seconds under the VRAM or RAM floor trips it, and it stays off for at least 300 seconds so a squeeze cannot cycle the job. With the machine idle the card moves to a faster profile, and drops back if the core holds 85 C for 30 seconds.
Because the function does no I/O, recorded sessions replay through it exactly. Four recordings and 135 checks cover it, each expectation written by hand from what happened during the recording, never from the code’s own output.
A Rust CLI and ratatui dashboard sit on top: status, mode switches that wait for the controller to confirm, and a live view redrawn every second. Cargo tests, clippy with warnings denied, and the replay gate every agent stop in the repository.
Numbers
- Control loop
- 250 ms tick
- Foreground stop
- Next tick, held 60 s after it exits
- Throttle
- Duty cycle over 200 ms, in 0.1 share steps
- Memory stop
- 3 s to trip, 300 s minimum hold
- Thermal failsafe
- 85 C for 30 s
- Decision tests
- 4 recorded sessions, 135 checks
- Rust app
- 3.6k lines, 96 tests
- Hardware API
- nvml.dll v2 · Win32 P/Invoke