#[cfg(any(all(feature = "monitoring-amdgpu-busy-percent", target_os = "linux"), all(feature = "monitoring-intel-idle-residency", target_os = "linux"), all(feature = "monitoring-nvml", target_os = "linux"), all(feature = "monitoring-gpu-engine-counters", target_os = "windows"), all(feature = "monitoring-io-accelerator", target_os = "macos")))]
use alloc::{sync::Arc, vec::Vec};
#[cfg(any(all(feature = "monitoring-amdgpu-busy-percent", target_os = "linux"), all(feature = "monitoring-intel-idle-residency", target_os = "linux"), all(feature = "monitoring-nvml", target_os = "linux"), all(feature = "monitoring-gpu-engine-counters", target_os = "windows"), all(feature = "monitoring-io-accelerator", target_os = "macos")))]
use std::sync::{Mutex, PoisonError};
use ruda_core::ir::PhysicalDevice;
#[cfg(any(all(feature = "monitoring-amdgpu-busy-percent", target_os = "linux"), all(feature = "monitoring-intel-idle-residency", target_os = "linux"), all(feature = "monitoring-nvml", target_os = "linux"), all(feature = "monitoring-gpu-engine-counters", target_os = "windows"), all(feature = "monitoring-io-accelerator", target_os = "macos")))]
use super::{opened::OpenedCounter, source::UtilizationSource};
use crate::runtime::monitoring::{DeviceUtilization, UtilizationUnavailable};
pub struct CardCounters;
#[cfg(any(all(feature = "monitoring-amdgpu-busy-percent", target_os = "linux"), all(feature = "monitoring-intel-idle-residency", target_os = "linux"), all(feature = "monitoring-nvml", target_os = "linux"), all(feature = "monitoring-gpu-engine-counters", target_os = "windows"), all(feature = "monitoring-io-accelerator", target_os = "macos")))]
static OPENED_COUNTERS: Mutex<Vec<(UtilizationSource, Arc<OpenedCounter>)>> =
Mutex::new(Vec::new());
impl CardCounters {
#[cfg(any(all(feature = "monitoring-amdgpu-busy-percent", target_os = "linux"), all(feature = "monitoring-intel-idle-residency", target_os = "linux"), all(feature = "monitoring-nvml", target_os = "linux"), all(feature = "monitoring-gpu-engine-counters", target_os = "windows"), all(feature = "monitoring-io-accelerator", target_os = "macos")))]
pub fn read(
card: Option<&PhysicalDevice>,
) -> Result<DeviceUtilization, UtilizationUnavailable> {
Self::opened_for(UtilizationSource::of_card(card)).read()
}
#[cfg(not(any(all(feature = "monitoring-amdgpu-busy-percent", target_os = "linux"), all(feature = "monitoring-intel-idle-residency", target_os = "linux"), all(feature = "monitoring-nvml", target_os = "linux"), all(feature = "monitoring-gpu-engine-counters", target_os = "windows"), all(feature = "monitoring-io-accelerator", target_os = "macos"))))]
pub fn read(
card: Option<&PhysicalDevice>,
) -> Result<DeviceUtilization, UtilizationUnavailable> {
Err(match card {
Some(card) => UtilizationUnavailable::NoCounterForCard(card.vendor),
None => UtilizationUnavailable::NoCard,
})
}
#[cfg(any(all(feature = "monitoring-amdgpu-busy-percent", target_os = "linux"), all(feature = "monitoring-intel-idle-residency", target_os = "linux"), all(feature = "monitoring-nvml", target_os = "linux"), all(feature = "monitoring-gpu-engine-counters", target_os = "windows"), all(feature = "monitoring-io-accelerator", target_os = "macos")))]
fn opened_for(source: UtilizationSource) -> Arc<OpenedCounter> {
let mut opened_counters = OPENED_COUNTERS
.lock()
.unwrap_or_else(PoisonError::into_inner);
if let Some((_, opened)) = opened_counters
.iter()
.find(|(opened_source, _)| *opened_source == source)
{
return opened.clone();
}
let opened = Arc::new(OpenedCounter::open(source.clone()));
opened_counters.push((source, opened.clone()));
opened
}
}