use crate::error::{IronError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GpuType {
Integrated,
Discrete,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuFrequency {
pub current: Option<u32>,
pub min: Option<u32>,
pub max: Option<u32>,
pub governor: String,
pub gpc: Option<Vec<u32>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuStatus {
pub load: Option<f32>,
pub railgate: Option<bool>,
pub tpc_pg_mask: Option<bool>,
pub scaling_3d: Option<bool>,
pub memory_used: Option<u64>,
pub memory_total: Option<u64>,
pub memory_free: Option<u64>,
pub temperature: Option<f32>,
pub power_draw: Option<f32>,
pub power_limit: Option<f32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuInfo {
pub gpu_type: GpuType,
pub status: GpuStatus,
pub frequency: GpuFrequency,
pub power_control: String,
}
#[derive(Debug, Default)]
pub struct GpuStats {
gpus: HashMap<String, GpuInfo>,
}
impl GpuStats {
pub fn new() -> Self {
Self {
gpus: HashMap::new(),
}
}
pub fn gpus(&self) -> &HashMap<String, GpuInfo> {
&self.gpus
}
pub fn get_gpu(&self, name: &str) -> Option<&GpuInfo> {
self.gpus.get(name)
}
pub fn gpus_mut(&mut self) -> &mut HashMap<String, GpuInfo> {
&mut self.gpus
}
pub fn set_3d_scaling(&mut self, name: &str, enabled: bool) -> Result<()> {
if !self.gpus.contains_key(name) {
return Err(IronError::DeviceNotFound(format!(
"GPU '{}' not found",
name
)));
}
#[cfg(target_os = "linux")]
{
use crate::platform::linux::jetson::set_gpu_3d_scaling;
set_gpu_3d_scaling(name, enabled)
}
#[cfg(not(target_os = "linux"))]
{
let _ = enabled; Err(IronError::UnsupportedPlatform(
"3D scaling control only available on Linux Jetson devices".to_string(),
))
}
}
pub fn set_railgate(&mut self, name: &str, enabled: bool) -> Result<()> {
if !self.gpus.contains_key(name) {
return Err(IronError::DeviceNotFound(format!(
"GPU '{}' not found",
name
)));
}
#[cfg(target_os = "linux")]
{
use crate::platform::linux::jetson::set_gpu_railgate;
set_gpu_railgate(name, enabled)
}
#[cfg(not(target_os = "linux"))]
{
let _ = enabled; Err(IronError::UnsupportedPlatform(
"Railgate control only available on Linux Jetson devices".to_string(),
))
}
}
}