use alloc::format;
use alloc::string::ToString;
use std::path::PathBuf;
use ruda_core::ir::PciAddress;
use crate::runtime::monitoring::{DeviceUtilization, UtilizationUnavailable};
#[derive(Debug)]
pub struct AmdgpuBusyPercentFile {
path: PathBuf,
}
impl AmdgpuBusyPercentFile {
pub fn new(pci_address: PciAddress) -> Self {
Self {
path: PathBuf::from(format!(
"/sys/bus/pci/devices/{pci_address}/gpu_busy_percent"
)),
}
}
pub fn read(&self) -> Result<DeviceUtilization, UtilizationUnavailable> {
std::fs::read_to_string(&self.path)
.map_err(|error| error.to_string())
.and_then(|text| {
text.trim()
.parse::<f32>()
.map_err(|error| error.to_string())
})
.map(DeviceUtilization::new)
.map_err(|message| {
UtilizationUnavailable::QueryFailed(format!("{}: {message}", self.path.display()))
})
}
}