use crate::{
DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
#[derive(Debug)]
pub struct GpuTemperatureDriver;
#[async_trait::async_trait]
impl Driver for GpuTemperatureDriver {
fn name(&self) -> &str {
"gpu_temperature"
}
fn description(&self) -> &str {
"Get GPU temperature"
}
fn usage_hint(&self) -> &str {
"Use this skill to monitor GPU thermal conditions"
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "gpu_temperature",
"parameters": {}
}));
}
fn example_output(&self) -> String {
return "GPU Temperature: 72.0°C".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::OperatingSystemGpu;
}
async fn execute(
&self,
_parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing gpu_temperature driver");
let temp = get_gpu_temperature()?;
info!("GPU temperature: {:.1}°C", temp);
return Ok(format!("GPU Temperature: {:.1}°C", temp));
}
}
fn get_gpu_temperature() -> DriverResult<f64> {
#[cfg(target_os = "linux")]
{
debug!("Getting GPU temperature on Linux");
debug!("Trying NVIDIA nvidia-smi for temperature");
if let Ok(output) =
std::process::crate::common::hidden_cmd("nvidia-smi").args(&["--query-gpu", "temperature.gpu"]).args(&["--format", "csv,noheader"]).output()
{
if output.status.success() {
if let Ok(output_str) = String::from_utf8(output.stdout) {
if let Some(line) = output_str.lines().next() {
if let Ok(temp) = line.trim().parse::<f64>() {
info!("NVIDIA temperature: {:.1}°C", temp);
return Ok(temp);
}
}
}
}
}
debug!("Trying AMD rocm-smi for temperature");
if let Ok(output) = std::process::crate::common::hidden_cmd("rocm-smi").args(&["--showtemp"]).output() {
if output.status.success() {
if let Ok(output_str) = String::from_utf8(output.stdout) {
for line in output_str.lines() {
if line.contains("GPU") && line.contains("°C") {
if let Some(temp_str) = line.split_whitespace().find(|s| s.contains("°C")) {
if let Ok(temp) = temp_str.trim_end_matches("°C").parse::<f64>() {
info!("AMD rocm-smi temperature: {:.1}°C", temp);
return Ok(temp);
}
}
}
}
}
}
}
debug!("Trying AMD hwmon for temperature");
let hwmon_path = "/sys/class/hwmon";
if let Ok(entries) = std::fs::read_dir(hwmon_path) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if let Ok(name) = std::fs::read_to_string(path.join("name")) {
if name.trim().contains("amdgpu") || name.trim().contains("radeon") {
let temp_file = path.join("temp1_input");
if let Ok(temp_str) = std::fs::read_to_string(&temp_file) {
if let Ok(temp) = temp_str.trim().parse::<f64>() {
let celsius = temp / 1000.0;
info!("AMD hwmon temperature: {:.1}°C", celsius);
return Ok(celsius);
}
}
}
}
}
}
}
info!("GPU temperature not found on Linux");
return Err(DriverError::execution("No GPU temperature sensor found"));
}
#[cfg(target_os = "windows")]
{
debug!("Getting GPU temperature on Windows");
return get_windows_gpu_temperature();
}
#[cfg(target_os = "macos")]
{
debug!("Getting GPU temperature on macOS");
if let Ok(output) = std::process::crate::common::hidden_cmd("sudo").args(&["powermetrics", "-n", "1", "--samplers", "gpu_power"]).output() {
if output.status.success() {
if let Ok(output_str) = String::from_utf8(output.stdout) {
for line in output_str.lines() {
if line.contains("GPU temperature") {
if let Some(temp_str) = line.split(':').nth(1) {
if let Ok(temp) = temp_str.trim().parse::<f64>() {
info!("macOS temperature: {:.1}°C", temp);
return Ok(temp);
}
}
}
}
}
}
}
info!("GPU temperature not found on macOS, returning 45.0");
return Ok(45.0);
}
#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
{
debug!("GPU temperature not supported on this platform");
return Err(DriverError::execution("GPU temperature not supported on this platform"));
}
}
#[cfg(target_os = "windows")]
fn get_windows_gpu_temperature() -> DriverResult<f64> {
use std::process::Command;
debug!("Getting GPU temperature on Windows via WMI");
let output = crate::common::hidden_cmd("powershell")
.args(&[
"-Command",
"Get-CimInstance -Namespace root/wmi -ClassName MSAcpi_ThermalZoneTemperature | Select-Object -ExpandProperty CurrentTemperature",
])
.output();
if let Ok(output) = output {
if output.status.success() {
if let Ok(output_str) = String::from_utf8(output.stdout) {
for line in output_str.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
if let Ok(temp_raw) = trimmed.parse::<f64>() {
let celsius = (temp_raw / 10.0) - 273.15;
if celsius > 0.0 && celsius < 100.0 {
info!("Windows WMI temperature: {:.1}°C", celsius);
return Ok(celsius);
}
}
}
}
}
}
}
#[cfg(feature = "nvml")]
{
debug!("Trying NVML for temperature on Windows");
use nvml_wrapper::Nvml;
if let Ok(nvml) = Nvml::init() {
if let Ok(device) = nvml.device_by_index(0) {
if let Ok(temp) = device.temperature(nvml_wrapper::enum_wrappers::device::TemperatureSensor::Gpu) {
info!("Windows NVML temperature: {:.1}°C", temp as f64);
return Ok(temp as f64);
}
}
}
}
info!("GPU temperature not found on Windows, returning 45.0");
return Ok(45.0);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gpu_temperature_metadata() {
let driver = GpuTemperatureDriver;
assert_eq!(driver.name(), "gpu_temperature");
assert_eq!(driver.category(), DriverCategory::OperatingSystemGpu);
}
}