use alloc::string::String;
use core::fmt;
use ruda_core::ir::PciVendor;
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct DeviceUtilization {
pub busy_percent: f32,
}
impl DeviceUtilization {
pub fn new(busy_percent: f32) -> Self {
Self {
busy_percent: busy_percent.clamp(0.0, 100.0),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum UtilizationUnavailable {
RuntimeNotLinked,
RuntimeReadsNoCounter,
NoCard,
NoCounterForCard(Option<PciVendor>),
CardAddressNotReported,
DriverLibraryNotFound(String),
NoPreviousReading,
QueryFailed(String),
}
impl fmt::Display for UtilizationUnavailable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RuntimeNotLinked => f.write_str("this build does not link the device's runtime"),
Self::RuntimeReadsNoCounter => {
f.write_str("the device's runtime reads no utilization counter")
}
Self::NoCard => f.write_str("the device is no card: a software adapter"),
Self::NoCounterForCard(Some(vendor)) => {
write!(f, "no counter on this platform for a {vendor} card")
}
Self::NoCounterForCard(None) => {
f.write_str("no counter on this platform for a card of no known vendor")
}
Self::CardAddressNotReported => {
f.write_str("the runtime did not report the address the card's counter is found by")
}
Self::DriverLibraryNotFound(message) => {
write!(f, "the driver's library would not load: {message}")
}
Self::NoPreviousReading => {
f.write_str("the counter measures between two readings, and this was the first")
}
Self::QueryFailed(message) => write!(f, "the counter gave no reading: {message}"),
}
}
}
impl core::error::Error for UtilizationUnavailable {}