Class NvmlUtil
-1 or
-1L) when NVML is unavailable or a specific query fails.
The native library is loaded once at class initialization. Each metric call pairs nvmlInit_v2 with
nvmlShutdown to correctly manage NVML's internal reference count, ensuring OSHI does not interfere with other
code in the same process that may also be managing the NVML lifecycle.
Device handles are enumerated once on first successful init and cached by PCI bus ID string for correlation with OSHI GraphicsCard instances.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringfindDevice(String pciBusId) Finds the stable PCI bus ID string for the NVML device whose bus ID contains the given fragment.static StringfindDeviceByName(String gpuName) Finds the stable PCI bus ID string for the NVML device whose name matches the given GPU name.static longgetCoreClockMhz(String deviceId) Returns GPU core clock speed in MHz, or -1 if unavailable.static doublegetFanSpeedPercent(String deviceId) Returns GPU fan speed as a percentage (0–100), or -1 if unavailable.static doublegetGpuUtilization(String deviceId) Returns GPU core utilization percentage (0–100), or -1 if unavailable.static longgetMemoryClockMhz(String deviceId) Returns GPU memory clock speed in MHz, or -1 if unavailable.static doublegetPowerDraw(String deviceId) Returns GPU power draw in watts, or -1 if unavailable.static doublegetTemperature(String deviceId) Returns GPU temperature in degrees Celsius, or -1 if unavailable.static longgetVramUsed(String deviceId) Returns VRAM used in bytes, or -1 if unavailable.static booleanReturns whether the NVML native library was successfully loaded.
-
Method Details
-
isAvailable
public static boolean isAvailable()Returns whether the NVML native library was successfully loaded. Does not indicate whether any NVIDIA GPU is present or whethernvmlInit_v2will succeed.- Returns:
- true if the NVML library is available
-
findDevice
Finds the stable PCI bus ID string for the NVML device whose bus ID contains the given fragment. The match is case-insensitive and uses substring matching to accommodate domain-qualified vs. non-qualified forms.Returns a stable string identifier rather than a device handle. Handles are only valid within a single
nvmlInit/nvmlShutdownscope; returning one across that boundary would leave the caller with a stale pointer. Callers should pass the returned string to the metric methods, which re-acquire a fresh handle internally.- Parameters:
pciBusId- PCI bus ID fragment (e.g."0000:01:00.0"or"01:00.0")- Returns:
- matched PCI bus ID string, or
nullif not found or NVML unavailable
-
findDeviceByName
Finds the stable PCI bus ID string for the NVML device whose name matches the given GPU name. Used as a fallback when PCI bus ID is unavailable.Returns a stable string identifier rather than a device handle for the same reason as
findDevice(String).- Parameters:
gpuName- GPU name string (case-insensitive substring match)- Returns:
- PCI bus ID string of the matched device, or
nullif not found or NVML unavailable
-
getGpuUtilization
Returns GPU core utilization percentage (0–100), or -1 if unavailable.- Parameters:
deviceId- stable device identifier returned byfindDevice(String)orfindDeviceByName(String)- Returns:
- utilization percentage or -1
-
getVramUsed
Returns VRAM used in bytes, or -1 if unavailable.- Parameters:
deviceId- stable device identifier returned byfindDevice(String)orfindDeviceByName(String)- Returns:
- bytes used or -1
-
getTemperature
Returns GPU temperature in degrees Celsius, or -1 if unavailable.- Parameters:
deviceId- stable device identifier returned byfindDevice(String)orfindDeviceByName(String)- Returns:
- temperature in °C or -1
-
getPowerDraw
Returns GPU power draw in watts, or -1 if unavailable.- Parameters:
deviceId- stable device identifier returned byfindDevice(String)orfindDeviceByName(String)- Returns:
- power in watts or -1
-
getCoreClockMhz
Returns GPU core clock speed in MHz, or -1 if unavailable.- Parameters:
deviceId- stable device identifier returned byfindDevice(String)orfindDeviceByName(String)- Returns:
- core clock in MHz or -1
-
getMemoryClockMhz
Returns GPU memory clock speed in MHz, or -1 if unavailable.- Parameters:
deviceId- stable device identifier returned byfindDevice(String)orfindDeviceByName(String)- Returns:
- memory clock in MHz or -1
-
getFanSpeedPercent
Returns GPU fan speed as a percentage (0–100), or -1 if unavailable.- Parameters:
deviceId- stable device identifier returned byfindDevice(String)orfindDeviceByName(String)- Returns:
- fan speed percentage or -1
-