use serde::{Deserialize, Serialize};
use std::fmt;
pub mod traits;
pub use traits::{
Clocks, ComputeMode, Device, EccErrors, Error as GpuError, FanSpeed,
GpuProcess as GpuProcessTrait, LinkState, Memory, MigMode, NvLinkStatus, PciInfo, Power,
ProcessType, Temperature, TemperatureStatus, TemperatureThresholds, Utilization, Vendor,
};
#[cfg(feature = "nvidia")]
pub mod nvidia_new;
#[cfg(feature = "amd")]
pub mod amd_rocm;
#[cfg(feature = "intel")]
pub mod intel_levelzero;
#[cfg(feature = "nvidia")]
pub mod nvidia;
#[cfg(feature = "amd")]
pub mod amd;
#[cfg(feature = "intel")]
pub mod intel;
#[cfg(feature = "apple")]
pub mod apple;
#[cfg(windows)]
pub mod windows_helpers;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum GpuVendor {
Nvidia,
Amd,
Intel,
Apple,
}
impl fmt::Display for GpuVendor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GpuVendor::Nvidia => write!(f, "NVIDIA"),
GpuVendor::Amd => write!(f, "AMD"),
GpuVendor::Intel => write!(f, "Intel"),
GpuVendor::Apple => write!(f, "Apple"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GpuProcessType {
Graphics,
Compute,
GraphicsAndCompute,
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuProcess {
pub pid: u32,
pub name: String,
pub user: String,
pub process_type: GpuProcessType,
pub gpu_usage: Option<u8>,
pub memory_usage: Option<u64>,
pub memory_usage_percent: Option<u8>,
pub encoder_usage: Option<u8>,
pub decoder_usage: Option<u8>,
pub cpu_usage: Option<u8>,
pub cpu_memory: Option<u64>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GpuMemory {
pub total: Option<u64>,
pub used: Option<u64>,
pub free: Option<u64>,
pub utilization: Option<u8>,
}
impl GpuMemory {
pub fn unreported() -> Self {
Self {
total: None,
used: None,
free: None,
utilization: None,
}
}
pub fn from_total_used(total: u64, used: u64) -> Self {
Self {
total: Some(total),
used: Some(used),
free: Some(total.saturating_sub(used)),
utilization: (total > 0).then(|| ((used as f64 / total as f64) * 100.0) as u8),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GpuClocks {
pub graphics: Option<u32>,
pub graphics_max: Option<u32>,
pub memory: Option<u32>,
pub memory_max: Option<u32>,
pub sm: Option<u32>,
pub video: Option<u32>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GpuPower {
pub draw: Option<u32>,
pub limit: Option<u32>,
pub default_limit: Option<u32>,
pub usage_percent: Option<u8>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GpuThermal {
pub temperature: Option<i32>,
pub max_temperature: Option<i32>,
pub critical_temperature: Option<i32>,
pub fan_speed: Option<u8>,
pub fan_rpm: Option<u32>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PcieLinkInfo {
pub current_gen: Option<u8>,
pub max_gen: Option<u8>,
pub current_width: Option<u8>,
pub max_width: Option<u8>,
pub current_speed: Option<u32>,
pub max_speed: Option<u32>,
pub tx_throughput: Option<u64>,
pub rx_throughput: Option<u64>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GpuEngines {
pub graphics: Option<u8>,
pub compute: Option<u8>,
pub encoder: Option<u8>,
pub decoder: Option<u8>,
pub copy: Option<u8>,
pub vendor_specific: Vec<(String, u8)>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuStaticInfo {
pub index: usize,
pub vendor: GpuVendor,
pub name: String,
pub pci_bus_id: Option<String>,
pub uuid: Option<String>,
pub vbios_version: Option<String>,
pub driver_version: Option<String>,
pub compute_capability: Option<(u32, u32)>,
pub shader_cores: Option<u32>,
pub l2_cache: Option<u64>,
pub num_engines: Option<u32>,
pub integrated: Option<bool>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GpuDynamicInfo {
pub utilization: Option<u8>,
pub memory: GpuMemory,
pub clocks: GpuClocks,
pub power: GpuPower,
pub thermal: GpuThermal,
pub pcie: PcieLinkInfo,
pub engines: GpuEngines,
pub processes: Vec<GpuProcess>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuInfo {
pub static_info: GpuStaticInfo,
pub dynamic_info: GpuDynamicInfo,
}
impl GpuInfo {
pub fn index(&self) -> usize {
self.static_info.index
}
pub fn vendor(&self) -> GpuVendor {
self.static_info.vendor
}
pub fn name(&self) -> &str {
&self.static_info.name
}
pub fn utilization(&self) -> Option<u8> {
self.dynamic_info.utilization
}
pub fn temperature(&self) -> Option<i32> {
self.dynamic_info.thermal.temperature
}
pub fn memory_utilization(&self) -> Option<u8> {
self.dynamic_info.memory.utilization
}
pub fn power_draw_watts(&self) -> Option<f32> {
self.dynamic_info.power.draw.map(|mw| mw as f32 / 1000.0)
}
pub fn num_processes(&self) -> usize {
self.dynamic_info.processes.len()
}
}
pub trait Gpu: Send + Sync {
fn static_info(&self) -> Result<GpuStaticInfo, crate::Error>;
fn dynamic_info(&self) -> Result<GpuDynamicInfo, crate::Error>;
fn info(&self) -> Result<GpuInfo, crate::Error> {
Ok(GpuInfo {
static_info: self.static_info()?,
dynamic_info: self.dynamic_info()?,
})
}
fn vendor(&self) -> GpuVendor;
fn index(&self) -> usize;
fn name(&self) -> Result<String, crate::Error>;
fn processes(&self) -> Result<Vec<GpuProcess>, crate::Error>;
fn kill_process(&self, pid: u32) -> Result<(), crate::Error>;
fn set_power_limit(&mut self, limit_mw: u32) -> Result<(), crate::Error> {
let _ = limit_mw;
Err(crate::Error::NotSupported(
"Power limit control not supported for this GPU".to_string(),
))
}
fn set_fan_speed(&mut self, speed_percent: u8) -> Result<(), crate::Error> {
let _ = speed_percent;
Err(crate::Error::NotSupported(
"Fan control not supported for this GPU".to_string(),
))
}
fn set_fan_auto(&mut self, enabled: bool) -> Result<(), crate::Error> {
let _ = enabled;
Err(crate::Error::NotSupported(
"Fan control not supported for this GPU".to_string(),
))
}
fn vendor_specific_data(&self) -> Result<serde_json::Value, crate::Error> {
Ok(serde_json::Value::Null)
}
}
#[allow(dead_code)]
struct TraitGpuAdapter {
device: Box<dyn Device>,
}
impl TraitGpuAdapter {
#[allow(dead_code)]
fn new(device: Box<dyn Device>) -> Self {
Self { device }
}
#[allow(dead_code)]
fn convert_vendor(vendor: Vendor) -> GpuVendor {
match vendor {
Vendor::Nvidia => GpuVendor::Nvidia,
Vendor::Amd => GpuVendor::Amd,
Vendor::Intel => GpuVendor::Intel,
Vendor::Apple => GpuVendor::Apple,
}
}
}
impl Gpu for TraitGpuAdapter {
fn static_info(&self) -> Result<GpuStaticInfo, crate::Error> {
let name = self.device.name().unwrap_or_default();
let pci = self.device.pci_info().ok();
let uuid = self.device.uuid().ok();
let driver = self.device.driver_version().ok();
Ok(GpuStaticInfo {
index: self.device.index() as usize,
vendor: Self::convert_vendor(self.device.vendor()),
name,
pci_bus_id: pci.as_ref().map(|p| p.bus_id.clone()),
uuid,
vbios_version: None,
driver_version: driver,
compute_capability: None,
shader_cores: None,
l2_cache: None,
num_engines: None,
integrated: None,
})
}
fn dynamic_info(&self) -> Result<GpuDynamicInfo, crate::Error> {
let util = self.device.utilization().ok();
let mem = self.device.memory().ok();
let clocks = self.device.clocks().ok();
let power = self.device.power().ok();
let temp = self.device.temperature().ok();
let fan = self.device.fan_speed().ok().flatten();
let pci = self.device.pci_info().ok();
let memory = match mem {
Some(m) => match (m.total, m.used) {
(Some(total), Some(used)) => GpuMemory::from_total_used(total, used),
(total, used) => GpuMemory {
total,
used,
free: m.free,
utilization: None,
},
},
None => GpuMemory::unreported(),
};
let clocks_info = if let Some(c) = clocks {
GpuClocks {
graphics: c.graphics,
graphics_max: None,
memory: c.memory,
memory_max: None,
sm: c.sm,
video: c.video,
}
} else {
GpuClocks {
graphics: None,
graphics_max: None,
memory: None,
memory_max: None,
sm: None,
video: None,
}
};
let power_info = if let Some(p) = power {
let mw = |w: Option<f32>| w.map(|v| (v * 1000.0) as u32);
GpuPower {
draw: mw(p.current),
limit: mw(p.limit),
default_limit: mw(p.default_limit),
usage_percent: match (p.current, p.limit) {
(Some(cur), Some(lim)) if lim > 0.0 => Some(((cur / lim) * 100.0) as u8),
_ => None,
},
}
} else {
GpuPower {
draw: None,
limit: None,
default_limit: None,
usage_percent: None,
}
};
let thermal_info = if let Some(t) = temp {
GpuThermal {
temperature: t.edge.map(|e| e as i32),
max_temperature: t
.thresholds
.as_ref()
.and_then(|th| th.critical.map(|c| c as i32)),
critical_temperature: t
.thresholds
.as_ref()
.and_then(|th| th.shutdown.map(|s| s as i32)),
fan_speed: fan.as_ref().map(|f| match f {
FanSpeed::Percent(p) => *p as u8,
FanSpeed::Rpm(_) => 0u8, }),
fan_rpm: fan.as_ref().and_then(|f| match f {
FanSpeed::Rpm(r) => Some(*r),
_ => None,
}),
}
} else {
GpuThermal {
temperature: None,
max_temperature: None,
critical_temperature: None,
fan_speed: None,
fan_rpm: None,
}
};
let pcie_info = if let Some(p) = pci {
PcieLinkInfo {
current_gen: p.pcie_generation.map(|g| g as u8),
max_gen: p.pcie_generation.map(|g| g as u8),
current_width: p.pcie_link_width.map(|w| w as u8),
max_width: p.pcie_link_width.map(|w| w as u8),
current_speed: None,
max_speed: None,
tx_throughput: None,
rx_throughput: None,
}
} else {
PcieLinkInfo {
current_gen: None,
max_gen: None,
current_width: None,
max_width: None,
current_speed: None,
max_speed: None,
tx_throughput: None,
rx_throughput: None,
}
};
Ok(GpuDynamicInfo {
utilization: util.as_ref().and_then(|u| u.gpu).map(|g| g as u8),
memory,
clocks: clocks_info,
power: power_info,
thermal: thermal_info,
pcie: pcie_info,
engines: GpuEngines {
graphics: util.as_ref().and_then(|u| u.gpu).map(|g| g as u8),
compute: None,
encoder: util.as_ref().and_then(|u| u.encoder.map(|e| e as u8)),
decoder: util.as_ref().and_then(|u| u.decoder.map(|d| d as u8)),
copy: None,
vendor_specific: vec![],
},
processes: vec![],
})
}
fn vendor(&self) -> GpuVendor {
Self::convert_vendor(self.device.vendor())
}
fn index(&self) -> usize {
self.device.index() as usize
}
fn name(&self) -> Result<String, crate::Error> {
self.device
.name()
.map_err(|e| crate::Error::GpuError(e.to_string()))
}
fn processes(&self) -> Result<Vec<GpuProcess>, crate::Error> {
let trait_procs = self
.device
.processes()
.map_err(|e| crate::Error::GpuError(e.to_string()))?;
let mut processes = Vec::new();
for proc in trait_procs {
let name = proc
.name()
.unwrap_or_else(|_| format!("pid_{}", proc.pid()));
let memory_usage = proc.gpu_memory_used().ok();
let gpu_usage = proc.sm_utilization().ok().flatten().map(|u| u as u8);
let encoder_usage = proc.encoder_utilization().ok().flatten().map(|u| u as u8);
let decoder_usage = proc.decoder_utilization().ok().flatten().map(|u| u as u8);
let process_type = match proc.process_type() {
crate::gpu::traits::ProcessType::Compute => GpuProcessType::Compute,
crate::gpu::traits::ProcessType::Graphics => GpuProcessType::Graphics,
crate::gpu::traits::ProcessType::Mixed => GpuProcessType::GraphicsAndCompute,
};
processes.push(GpuProcess {
pid: proc.pid(),
name,
user: String::new(), process_type,
gpu_usage,
memory_usage,
memory_usage_percent: None,
encoder_usage,
decoder_usage,
cpu_usage: proc.cpu_utilization().ok().flatten().map(|u| u as u8),
cpu_memory: proc.host_memory_used().ok().flatten(),
});
}
Ok(processes)
}
fn kill_process(&self, _pid: u32) -> Result<(), crate::Error> {
Err(crate::Error::NotSupported(
"Process kill not implemented for this adapter".to_string(),
))
}
}
pub struct GpuCollection {
gpus: Vec<Box<dyn Gpu>>,
detection_failures: Vec<(&'static str, String)>,
}
impl GpuCollection {
pub fn new() -> Self {
Self {
gpus: Vec::new(),
detection_failures: Vec::new(),
}
}
pub fn detection_failures(&self) -> &[(&'static str, String)] {
&self.detection_failures
}
pub fn auto_detect() -> Result<Self, crate::Error> {
#[allow(unused_mut)]
let mut collection = Self::new();
#[cfg(feature = "nvidia")]
{
if let Err(e) = collection.detect_nvidia() {
collection
.detection_failures
.push(("nvidia", e.to_string()));
}
}
#[cfg(feature = "amd")]
{
if let Err(e) = collection.detect_amd() {
collection.detection_failures.push(("amd", e.to_string()));
}
}
#[cfg(feature = "intel")]
{
if let Err(e) = collection.detect_intel() {
collection.detection_failures.push(("intel", e.to_string()));
}
}
#[cfg(feature = "apple")]
{
if let Err(e) = collection.detect_apple() {
collection.detection_failures.push(("apple", e.to_string()));
}
}
if collection.is_empty() && !collection.detection_failures.is_empty() {
return Err(crate::Error::GpuDetectionFailed(
collection.detection_failures,
));
}
Ok(collection)
}
#[cfg(feature = "nvidia")]
pub fn detect_nvidia(&mut self) -> Result<(), crate::Error> {
nvidia::detect_gpus(self)?;
Ok(())
}
#[cfg(feature = "amd")]
pub fn detect_amd(&mut self) -> Result<(), crate::Error> {
#[cfg(target_os = "linux")]
let sysfs_error = {
let mut failures = Vec::new();
let result = amd_rocm::enumerate(&mut failures);
self.detection_failures
.extend(failures.into_iter().map(|f| ("amd", f)));
match result {
Ok(gpus) => {
for gpu in gpus {
self.add_gpu(Box::new(TraitGpuAdapter::new(gpu)));
}
return Ok(());
}
Err(GpuError::NoDevicesFound) => None,
Err(e) => Some(e),
}
};
#[cfg(not(target_os = "linux"))]
let sysfs_error: Option<GpuError> = None;
let before = self.len();
amd::detect_gpus(self)?;
if let Some(e) = sysfs_error {
if self.len() == before {
let msg = format!("AMD sysfs enumeration failed: {e}");
return Err(crate::Error::GpuError(msg));
}
log::debug!("AMD sysfs enumeration failed, legacy reader used: {e}");
}
Ok(())
}
#[cfg(feature = "intel")]
pub fn detect_intel(&mut self) -> Result<(), crate::Error> {
#[cfg(target_os = "linux")]
let sysfs_error = {
let mut failures = Vec::new();
let result = intel_levelzero::enumerate(&mut failures);
self.detection_failures
.extend(failures.into_iter().map(|f| ("intel", f)));
match result {
Ok(gpus) => {
for gpu in gpus {
self.add_gpu(Box::new(TraitGpuAdapter::new(gpu)));
}
return Ok(());
}
Err(GpuError::NoDevicesFound) => None,
Err(e) => Some(e),
}
};
#[cfg(not(target_os = "linux"))]
let sysfs_error: Option<GpuError> = None;
let before = self.len();
intel::detect_gpus(self)?;
if let Some(e) = sysfs_error {
if self.len() == before {
let msg = format!("Intel sysfs enumeration failed: {e}");
return Err(crate::Error::GpuError(msg));
}
log::debug!("Intel sysfs enumeration failed, legacy reader used: {e}");
}
Ok(())
}
#[cfg(feature = "apple")]
pub fn detect_apple(&mut self) -> Result<(), crate::Error> {
#[cfg(target_os = "macos")]
{
for gpu in apple::AppleGpu::detect_gpus()? {
self.add_gpu(Box::new(gpu));
}
}
Ok(())
}
pub fn add_gpu(&mut self, gpu: Box<dyn Gpu>) {
self.gpus.push(gpu);
}
pub fn gpus(&self) -> &[Box<dyn Gpu>] {
&self.gpus
}
pub fn gpus_mut(&mut self) -> &mut [Box<dyn Gpu>] {
&mut self.gpus
}
pub fn len(&self) -> usize {
self.gpus.len()
}
pub fn device_count(&self) -> usize {
self.gpus.len()
}
pub fn is_empty(&self) -> bool {
self.gpus.is_empty()
}
pub fn get(&self, index: usize) -> Option<&dyn Gpu> {
self.gpus.get(index).map(|gpu| gpu.as_ref())
}
pub fn get_mut(&mut self, index: usize) -> Option<&mut (dyn Gpu + 'static)> {
self.gpus.get_mut(index).map(|gpu| gpu.as_mut())
}
pub fn nvidia_gpus(&self) -> Vec<&dyn Gpu> {
self.gpus
.iter()
.filter(|gpu| gpu.vendor() == GpuVendor::Nvidia)
.map(|gpu| gpu.as_ref())
.collect()
}
pub fn amd_gpus(&self) -> Vec<&dyn Gpu> {
self.gpus
.iter()
.filter(|gpu| gpu.vendor() == GpuVendor::Amd)
.map(|gpu| gpu.as_ref())
.collect()
}
pub fn intel_gpus(&self) -> Vec<&dyn Gpu> {
self.gpus
.iter()
.filter(|gpu| gpu.vendor() == GpuVendor::Intel)
.map(|gpu| gpu.as_ref())
.collect()
}
pub fn snapshot_all(&self) -> Result<Vec<GpuInfo>, crate::Error> {
self.snapshot_all_partial().into_iter().collect()
}
pub fn snapshot_all_partial(&self) -> Vec<Result<GpuInfo, crate::Error>> {
if self.gpus.len() <= 1 {
return self.gpus.iter().map(|gpu| gpu.info()).collect();
}
std::thread::scope(|scope| {
let handles: Vec<_> = self
.gpus
.iter()
.map(|gpu| {
scope.spawn(move || {
#[cfg(target_os = "windows")]
let _com = wmi::COMLibrary::new().ok();
gpu.info()
})
})
.collect();
handles
.into_iter()
.map(|handle| {
handle.join().unwrap_or_else(|_| {
Err(crate::error::Error::GpuError(
"GPU query panicked".to_string(),
))
})
})
.collect()
})
}
}
impl Default for GpuCollection {
fn default() -> Self {
Self::new()
}
}