use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Vendor {
Nvidia,
Amd,
Intel,
Apple,
}
impl fmt::Display for Vendor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Vendor::Nvidia => write!(f, "NVIDIA"),
Vendor::Amd => write!(f, "AMD"),
Vendor::Intel => write!(f, "Intel"),
Vendor::Apple => write!(f, "Apple"),
}
}
}
pub trait Device: Send + Sync {
fn vendor(&self) -> Vendor;
fn index(&self) -> u32;
fn name(&self) -> Result<String, Error>;
fn uuid(&self) -> Result<String, Error>;
fn pci_info(&self) -> Result<PciInfo, Error>;
fn driver_version(&self) -> Result<String, Error>;
fn temperature(&self) -> Result<Temperature, Error>;
fn power(&self) -> Result<Power, Error>;
fn clocks(&self) -> Result<Clocks, Error>;
fn utilization(&self) -> Result<Utilization, Error>;
fn memory(&self) -> Result<Memory, Error>;
fn fan_speed(&self) -> Result<Option<FanSpeed>, Error>;
fn performance_state(&self) -> Result<Option<String>, Error>;
fn processes(&self) -> Result<Vec<Box<dyn GpuProcess>>, Error>;
fn nvlink_status(&self) -> Result<Vec<NvLinkStatus>, Error> {
Err(Error::NotSupported)
}
fn mig_mode(&self) -> Result<MigMode, Error> {
Err(Error::NotSupported)
}
fn ecc_errors(&self) -> Result<EccErrors, Error> {
Err(Error::NotSupported)
}
fn compute_mode(&self) -> Result<Option<ComputeMode>, Error> {
Ok(None)
}
fn persistence_mode(&self) -> Result<Option<bool>, Error> {
Ok(None)
}
fn set_power_limit(&mut self, _watts: f32) -> Result<(), Error> {
Err(Error::NotSupported)
}
fn lock_gpu_clocks(&mut self, _min_mhz: u32, _max_mhz: u32) -> Result<(), Error> {
Err(Error::NotSupported)
}
fn reset_gpu_clocks(&mut self) -> Result<(), Error> {
Err(Error::NotSupported)
}
fn set_persistence_mode(&mut self, _enabled: bool) -> Result<(), Error> {
Err(Error::NotSupported)
}
fn set_compute_mode(&mut self, _mode: ComputeMode) -> Result<(), Error> {
Err(Error::NotSupported)
}
}
pub trait GpuProcess: Send + Sync {
fn pid(&self) -> u32;
fn name(&self) -> Result<String, Error>;
fn process_type(&self) -> ProcessType;
fn gpu_memory_used(&self) -> Result<u64, Error>;
fn sm_utilization(&self) -> Result<Option<f32>, Error> {
Ok(None)
}
fn encoder_utilization(&self) -> Result<Option<f32>, Error> {
Ok(None)
}
fn decoder_utilization(&self) -> Result<Option<f32>, Error> {
Ok(None)
}
fn cpu_utilization(&self) -> Result<Option<f32>, Error> {
Ok(None)
}
fn host_memory_used(&self) -> Result<Option<u64>, Error> {
Ok(None)
}
fn running_time(&self) -> Result<Option<u64>, Error> {
Ok(None)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PciInfo {
pub domain: u32,
pub bus: u8,
pub device: u8,
pub function: u8,
pub bus_id: String,
pub pcie_generation: Option<u32>,
pub pcie_link_width: Option<u32>,
}
impl PciInfo {
pub fn parse_address(address: &str) -> Option<(u32, u8, u8, u8)> {
let mut parts = address.trim().split(':');
let (domain, bus, slot) = (parts.next()?, parts.next()?, parts.next()?);
if parts.next().is_some() {
return None;
}
let (device, function) = slot.split_once('.')?;
if !matches!(domain.len(), 4 | 8)
|| bus.len() != 2
|| device.len() != 2
|| function.len() != 1
{
return None;
}
Some((
u32::from_str_radix(domain, 16).ok()?,
u8::from_str_radix(bus, 16).ok()?,
u8::from_str_radix(device, 16).ok()?,
u8::from_str_radix(function, 16).ok()?,
))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Temperature {
pub edge: Option<f32>,
pub junction: Option<f32>,
pub memory: Option<f32>,
pub hotspot: Option<f32>,
pub vr_gfx: Option<f32>,
pub vr_soc: Option<f32>,
pub vr_mem: Option<f32>,
pub hbm: Option<Vec<f32>>,
pub thresholds: Option<TemperatureThresholds>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemperatureThresholds {
pub slowdown: Option<f32>,
pub shutdown: Option<f32>,
pub critical: Option<f32>,
pub memory_critical: Option<f32>,
}
impl Temperature {
pub fn primary(&self) -> Option<f32> {
self.junction.or(self.hotspot).or(self.edge).or(self.memory)
}
pub fn max(&self) -> Option<f32> {
let mut temps = Vec::new();
if let Some(t) = self.edge {
temps.push(t);
}
if let Some(t) = self.junction {
temps.push(t);
}
if let Some(t) = self.memory {
temps.push(t);
}
if let Some(t) = self.hotspot {
temps.push(t);
}
if let Some(t) = self.vr_gfx {
temps.push(t);
}
if let Some(t) = self.vr_soc {
temps.push(t);
}
if let Some(t) = self.vr_mem {
temps.push(t);
}
if let Some(hbm_temps) = &self.hbm {
temps.extend(hbm_temps);
}
temps.into_iter().reduce(f32::max)
}
pub fn status(&self) -> TemperatureStatus {
let Some(temp) = self.primary() else {
return TemperatureStatus::Unknown;
};
let Some(thresholds) = &self.thresholds else {
return TemperatureStatus::Normal;
};
if let Some(critical) = thresholds.critical {
if temp >= critical {
return TemperatureStatus::Critical;
}
}
if let Some(shutdown) = thresholds.shutdown {
if temp >= shutdown {
return TemperatureStatus::Shutdown;
}
}
if let Some(slowdown) = thresholds.slowdown {
if temp >= slowdown {
return TemperatureStatus::Throttling;
}
}
TemperatureStatus::Normal
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TemperatureStatus {
Normal,
Throttling,
Shutdown,
Critical,
Unknown,
}
impl fmt::Display for TemperatureStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TemperatureStatus::Normal => write!(f, "Normal"),
TemperatureStatus::Throttling => write!(f, "Throttling"),
TemperatureStatus::Shutdown => write!(f, "Shutdown Warning"),
TemperatureStatus::Critical => write!(f, "CRITICAL"),
TemperatureStatus::Unknown => write!(f, "Unknown"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Power {
pub current: Option<f32>,
pub average: Option<f32>,
pub limit: Option<f32>,
pub default_limit: Option<f32>,
pub min_limit: Option<f32>,
pub max_limit: Option<f32>,
pub enforced_limit: Option<f32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Clocks {
pub graphics: Option<u32>,
pub memory: Option<u32>,
pub sm: Option<u32>,
pub video: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Utilization {
pub gpu: Option<f32>,
pub memory: Option<f32>,
pub encoder: Option<f32>,
pub decoder: Option<f32>,
pub jpeg: Option<f32>,
pub ofa: Option<f32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Memory {
pub total: Option<u64>,
pub used: Option<u64>,
pub free: Option<u64>,
pub bar1_total: Option<u64>,
pub bar1_used: Option<u64>,
}
impl Memory {
pub fn utilization_percent(&self) -> Option<f32> {
let (total, used) = (self.total?, self.used?);
if total == 0 {
return None;
}
Some((used as f64 / total as f64 * 100.0) as f32)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FanSpeed {
Rpm(u32),
Percent(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProcessType {
Compute,
Graphics,
Mixed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ComputeMode {
Default,
ExclusiveThread,
Prohibited,
ExclusiveProcess,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NvLinkStatus {
pub link_id: u32,
pub state: LinkState,
pub version: u32,
pub remote_device_type: String,
pub remote_pci_bus_id: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LinkState {
Active,
Inactive,
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigMode {
pub current: bool,
pub pending: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EccErrors {
pub volatile_single_bit: u64,
pub volatile_double_bit: u64,
pub aggregate_single_bit: u64,
pub aggregate_double_bit: u64,
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Operation not supported on this device")]
NotSupported,
#[error("Operation not supported on this device: {0}")]
NotSupportedBecause(String),
#[error("No GPU devices found")]
NoDevicesFound,
#[error("Device initialization failed: {0}")]
InitializationFailed(String),
#[error("Failed to query device: {0}")]
QueryFailed(String),
#[error("Failed to control device: {0}")]
ControlFailed(String),
#[error("Insufficient permissions: {0}")]
PermissionDenied(String),
#[error("Invalid argument: {0}")]
InvalidArgument(String),
#[cfg(feature = "nvidia")]
#[error("NVML error: {0}")]
NvmlError(#[from] nvml_wrapper::error::NvmlError),
#[error("Unknown error: {0}")]
Unknown(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pci_addresses_split_into_their_four_parts() {
assert_eq!(PciInfo::parse_address("0000:03:00.0"), Some((0, 3, 0, 0)));
assert_eq!(
PciInfo::parse_address("0000:c1:1f.7"),
Some((0, 0xc1, 0x1f, 7))
);
assert_eq!(
PciInfo::parse_address("00000000:01:00.1"),
Some((0, 1, 0, 1))
);
assert_eq!(PciInfo::parse_address("pci0000:00"), None);
assert_eq!(PciInfo::parse_address("card0"), None);
assert_eq!(PciInfo::parse_address("0000:03:00"), None);
}
#[test]
fn test_temperature_primary_junction_first() {
let temp = Temperature {
edge: Some(55.0),
junction: Some(65.0),
memory: Some(50.0),
hotspot: Some(70.0),
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: None,
};
assert_eq!(temp.primary(), Some(65.0));
}
#[test]
fn test_temperature_primary_fallback_hotspot() {
let temp = Temperature {
edge: Some(55.0),
junction: None,
memory: None,
hotspot: Some(70.0),
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: None,
};
assert_eq!(temp.primary(), Some(70.0));
}
#[test]
fn test_temperature_primary_fallback_edge() {
let temp = Temperature {
edge: Some(55.0),
junction: None,
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: None,
};
assert_eq!(temp.primary(), Some(55.0));
}
#[test]
fn test_temperature_primary_none() {
let temp = Temperature {
edge: None,
junction: None,
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: None,
};
assert_eq!(temp.primary(), None);
}
#[test]
fn test_temperature_max() {
let temp = Temperature {
edge: Some(55.0),
junction: Some(65.0),
memory: Some(50.0),
hotspot: Some(80.0),
vr_gfx: Some(72.0),
vr_soc: None,
vr_mem: None,
hbm: Some(vec![60.0, 62.0, 85.0]),
thresholds: None,
};
assert_eq!(temp.max(), Some(85.0));
}
#[test]
fn test_temperature_max_none_when_empty() {
let temp = Temperature {
edge: None,
junction: None,
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: None,
};
assert_eq!(temp.max(), None);
}
#[test]
fn test_temperature_status_normal() {
let temp = Temperature {
edge: None,
junction: Some(50.0),
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: Some(TemperatureThresholds {
slowdown: Some(80.0),
shutdown: Some(95.0),
critical: Some(100.0),
memory_critical: None,
}),
};
assert_eq!(temp.status(), TemperatureStatus::Normal);
}
#[test]
fn test_temperature_status_throttling() {
let temp = Temperature {
edge: None,
junction: Some(85.0),
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: Some(TemperatureThresholds {
slowdown: Some(80.0),
shutdown: Some(95.0),
critical: Some(100.0),
memory_critical: None,
}),
};
assert_eq!(temp.status(), TemperatureStatus::Throttling);
}
#[test]
fn test_temperature_status_shutdown() {
let temp = Temperature {
edge: None,
junction: Some(96.0),
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: Some(TemperatureThresholds {
slowdown: Some(80.0),
shutdown: Some(95.0),
critical: Some(100.0),
memory_critical: None,
}),
};
assert_eq!(temp.status(), TemperatureStatus::Shutdown);
}
#[test]
fn test_temperature_status_critical() {
let temp = Temperature {
edge: None,
junction: Some(105.0),
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: Some(TemperatureThresholds {
slowdown: Some(80.0),
shutdown: Some(95.0),
critical: Some(100.0),
memory_critical: None,
}),
};
assert_eq!(temp.status(), TemperatureStatus::Critical);
}
#[test]
fn test_temperature_status_unknown_when_no_primary() {
let temp = Temperature {
edge: None,
junction: None,
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: Some(TemperatureThresholds {
slowdown: Some(80.0),
shutdown: Some(95.0),
critical: Some(100.0),
memory_critical: None,
}),
};
assert_eq!(temp.status(), TemperatureStatus::Unknown);
}
#[test]
fn test_temperature_status_normal_when_no_thresholds() {
let temp = Temperature {
edge: None,
junction: Some(90.0),
memory: None,
hotspot: None,
vr_gfx: None,
vr_soc: None,
vr_mem: None,
hbm: None,
thresholds: None,
};
assert_eq!(temp.status(), TemperatureStatus::Normal);
}
#[test]
fn test_memory_utilization_percent() {
let mem = Memory {
total: Some(8 * 1024 * 1024 * 1024), used: Some(4 * 1024 * 1024 * 1024), free: Some(4 * 1024 * 1024 * 1024),
bar1_total: None,
bar1_used: None,
};
assert!((mem.utilization_percent().expect("both operands present") - 50.0).abs() < 0.01);
}
#[test]
fn test_memory_utilization_zero_total() {
let mem = Memory {
total: Some(0),
used: Some(0),
free: Some(0),
bar1_total: None,
bar1_used: None,
};
assert_eq!(mem.utilization_percent(), None);
}
#[test]
fn an_unread_total_yields_no_utilization() {
let mem = Memory {
total: None,
used: Some(1024),
free: None,
bar1_total: None,
bar1_used: None,
};
assert_eq!(
mem.utilization_percent(),
None,
concat!(
"a percentage needs both operands; inventing one is how a ",
"failed read becomes a plausible-looking number"
)
);
}
#[test]
fn test_memory_utilization_full() {
let mem = Memory {
total: Some(1024),
used: Some(1024),
free: Some(0),
bar1_total: None,
bar1_used: None,
};
assert!((mem.utilization_percent().expect("both present") - 100.0).abs() < 0.01);
}
#[test]
fn test_vendor_display() {
assert_eq!(Vendor::Nvidia.to_string(), "NVIDIA");
assert_eq!(Vendor::Amd.to_string(), "AMD");
assert_eq!(Vendor::Intel.to_string(), "Intel");
assert_eq!(Vendor::Apple.to_string(), "Apple");
}
#[test]
fn test_temperature_status_display() {
assert_eq!(TemperatureStatus::Normal.to_string(), "Normal");
assert_eq!(TemperatureStatus::Throttling.to_string(), "Throttling");
assert_eq!(TemperatureStatus::Shutdown.to_string(), "Shutdown Warning");
assert_eq!(TemperatureStatus::Critical.to_string(), "CRITICAL");
assert_eq!(TemperatureStatus::Unknown.to_string(), "Unknown");
}
}