use scirs2_core::numeric::Float;
use std::collections::{HashMap, VecDeque};
use std::fmt::Debug;
use std::time::SystemTime;
use crate::error::Result;
use crate::ResourceUsage;
#[derive(Debug)]
pub struct ResourceMonitor<T: Float + Debug + Send + Sync + 'static> {
current_usage: ResourceUsage,
usage_history: VecDeque<ResourceUsageSnapshot<T>>,
limits: ResourceLimits<T>,
config: MonitoringConfig,
}
#[derive(Debug, Clone)]
pub struct ResourceUsageSnapshot<T: Float + Debug + Send + Sync + 'static> {
pub timestamp: SystemTime,
pub memory_mb: T,
pub cpu_usage_percent: T,
pub gpu_usage_percent: T,
pub network_io_mbps: T,
pub disk_io_mbps: T,
}
#[derive(Debug, Clone)]
pub struct ResourceLimits<T: Float + Debug + Send + Sync + 'static> {
pub max_memory_mb: T,
pub max_cpu_percent: T,
pub max_gpu_memory_mb: T,
pub max_evaluation_time_seconds: T,
}
#[derive(Debug, Clone)]
pub struct MonitoringConfig {
pub monitoring_interval_ms: u64,
pub history_size_limit: usize,
pub enable_detailed_monitoring: bool,
pub alert_thresholds: HashMap<String, f64>,
}
impl<T: Float + Debug + Default + Send + Sync> ResourceMonitor<T> {
pub(crate) fn new() -> Self {
Self {
current_usage: ResourceUsage {
memory_usage: 0,
compute_time: 0.0,
energy_consumption: 0.0,
memory_gb: 0.0,
cpu_time_seconds: 0.0,
gpu_time_seconds: 0.0,
energy_kwh: 0.0,
cost_usd: 0.0,
network_gb: 0.0,
},
usage_history: VecDeque::new(),
limits: ResourceLimits {
max_memory_mb: scirs2_core::numeric::NumCast::from(8192.0)
.unwrap_or_else(|| T::zero()),
max_cpu_percent: scirs2_core::numeric::NumCast::from(90.0)
.unwrap_or_else(|| T::zero()),
max_gpu_memory_mb: scirs2_core::numeric::NumCast::from(16384.0)
.unwrap_or_else(|| T::zero()),
max_evaluation_time_seconds: scirs2_core::numeric::NumCast::from(3600.0)
.unwrap_or_else(|| T::zero()),
},
config: MonitoringConfig {
monitoring_interval_ms: 1000,
history_size_limit: 1000,
enable_detailed_monitoring: true,
alert_thresholds: HashMap::new(),
},
}
}
pub(crate) fn start_monitoring(&mut self) -> Result<()> {
Ok(())
}
pub fn stop_monitoring(&mut self) {
}
pub fn get_current_usage(&self) -> &ResourceUsage {
&self.current_usage
}
pub fn get_limits(&self) -> &ResourceLimits<T> {
&self.limits
}
pub fn set_limits(&mut self, limits: ResourceLimits<T>) {
self.limits = limits;
}
pub fn get_config(&self) -> &MonitoringConfig {
&self.config
}
pub fn set_config(&mut self, config: MonitoringConfig) {
self.config = config;
}
pub fn record_snapshot(&mut self, snapshot: ResourceUsageSnapshot<T>) {
if self.usage_history.len() >= self.config.history_size_limit {
self.usage_history.pop_front();
}
self.usage_history.push_back(snapshot);
}
pub fn get_history(&self) -> &VecDeque<ResourceUsageSnapshot<T>> {
&self.usage_history
}
pub fn clear_history(&mut self) {
self.usage_history.clear();
}
pub fn check_limits(&self) -> bool {
let memory_mb: f64 = self.current_usage.memory_gb * 1024.0;
let max_memory: f64 =
scirs2_core::numeric::NumCast::from(self.limits.max_memory_mb).unwrap_or(8192.0);
memory_mb <= max_memory
}
}