scirs2_sparse/realtime_performance_monitor/
monitor.rs1use super::alerts::{Alert, AlertManager};
7use super::config::PerformanceMonitorConfig;
8use super::history::PerformanceHistory;
9use super::metrics::{PerformanceSample, ProcessorType, SystemMetrics};
10use crate::adaptive_memory_compression::MemoryStats;
11use crate::error::SparseResult;
12use crate::neural_adaptive_sparse::NeuralProcessorStats;
13use crate::quantum_inspired_sparse::QuantumProcessorStats;
14use crate::quantum_neural_hybrid::QuantumNeuralHybridStats;
15use std::collections::HashMap;
16use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
17use std::sync::{Arc, Mutex};
18use std::time::{Duration, SystemTime, UNIX_EPOCH};
19
20pub struct RealTimePerformanceMonitor {
22 config: PerformanceMonitorConfig,
23 monitoring_active: Arc<AtomicBool>,
24 sample_counter: AtomicUsize,
25 performance_history: Arc<Mutex<PerformanceHistory>>,
26 system_metrics: Arc<Mutex<SystemMetrics>>,
27 alert_manager: Arc<Mutex<AlertManager>>,
28 processor_registry: Arc<Mutex<ProcessorRegistry>>,
29}
30
31pub struct ProcessorRegistry {
33 quantum_processors: HashMap<String, Box<dyn QuantumProcessorMonitor>>,
34 neural_processors: HashMap<String, Box<dyn NeuralProcessorMonitor>>,
35 hybrid_processors: HashMap<String, Box<dyn HybridProcessorMonitor>>,
36 memory_compressors: HashMap<String, Box<dyn MemoryCompressorMonitor>>,
37}
38
39pub trait QuantumProcessorMonitor: Send + Sync {
41 fn get_stats(&self) -> QuantumProcessorStats;
42 fn get_id(&self) -> &str;
43}
44
45pub trait NeuralProcessorMonitor: Send + Sync {
46 fn get_stats(&self) -> NeuralProcessorStats;
47 fn get_id(&self) -> &str;
48}
49
50pub trait HybridProcessorMonitor: Send + Sync {
51 fn get_stats(&self) -> QuantumNeuralHybridStats;
52 fn get_id(&self) -> &str;
53}
54
55pub trait MemoryCompressorMonitor: Send + Sync {
56 fn get_stats(&self) -> MemoryStats;
57 fn get_id(&self) -> &str;
58}
59
60#[derive(Debug, Clone)]
62pub struct MonitoringSummary {
63 pub monitoring_active: bool,
64 pub total_samples: usize,
65 pub active_alerts: usize,
66 pub registered_processors: usize,
67 pub uptime_seconds: u64,
68 pub average_sampling_rate: f64,
69 pub system_health_score: f64,
70}
71
72impl RealTimePerformanceMonitor {
73 pub fn new(config: PerformanceMonitorConfig) -> Self {
75 let performance_history = PerformanceHistory::new(config.max_samples);
76 let system_metrics = SystemMetrics::new();
77 let alert_manager = AlertManager::new(config.max_alert_history);
78 let processor_registry = ProcessorRegistry::new();
79
80 Self {
81 config,
82 monitoring_active: Arc::new(AtomicBool::new(false)),
83 sample_counter: AtomicUsize::new(0),
84 performance_history: Arc::new(Mutex::new(performance_history)),
85 system_metrics: Arc::new(Mutex::new(system_metrics)),
86 alert_manager: Arc::new(Mutex::new(alert_manager)),
87 processor_registry: Arc::new(Mutex::new(processor_registry)),
88 }
89 }
90
91 pub fn start_monitoring(&self) -> SparseResult<()> {
93 if self.monitoring_active.swap(true, Ordering::Relaxed) {
94 return Ok(()); }
96
97 let monitoring_active = Arc::clone(&self.monitoring_active);
98 let config = self.config.clone();
99 let performance_history = Arc::clone(&self.performance_history);
100 let system_metrics = Arc::clone(&self.system_metrics);
101 let alert_manager = Arc::clone(&self.alert_manager);
102 let processor_registry = Arc::clone(&self.processor_registry);
103 let sample_counter = AtomicUsize::new(0);
104
105 std::thread::spawn(move || {
107 let interval = Duration::from_millis(config.monitoring_interval_ms);
108
109 while monitoring_active.load(Ordering::Relaxed) {
110 let start_time = std::time::Instant::now();
111
112 if config.enable_system_metrics {
114 if let Ok(mut metrics) = system_metrics.lock() {
115 Self::update_system_metrics(&mut metrics);
116 }
117 }
118
119 if let Ok(registry) = processor_registry.lock() {
121 let samples = Self::collect_processor_samples(®istry);
122
123 for sample in samples {
124 sample_counter.fetch_add(1, Ordering::Relaxed);
125
126 if let Ok(mut history) = performance_history.lock() {
128 history.add_sample(sample.clone());
129 }
130
131 if config.enable_alerts {
133 if let Ok(mut alerts) = alert_manager.lock() {
134 alerts.process_sample(&sample, None);
135 }
136 }
137 }
138 }
139
140 if sample_counter.load(Ordering::Relaxed).is_multiple_of(1000) {
142 if let Ok(mut history) = performance_history.lock() {
143 let retention_time = config.optimization_interval_s * 1000 * 10; history.cleanup_old_samples(retention_time);
145 }
146 }
147
148 let elapsed = start_time.elapsed();
150 if elapsed < interval {
151 std::thread::sleep(interval - elapsed);
152 }
153 }
154 });
155
156 Ok(())
157 }
158
159 pub fn stop_monitoring(&self) {
161 self.monitoring_active.store(false, Ordering::Relaxed);
162 }
163
164 pub fn is_monitoring_active(&self) -> bool {
166 self.monitoring_active.load(Ordering::Relaxed)
167 }
168
169 pub fn register_quantum_processor<T>(&self, processor: T) -> SparseResult<()>
171 where
172 T: QuantumProcessorMonitor + 'static,
173 {
174 if let Ok(mut registry) = self.processor_registry.lock() {
175 let id = processor.get_id().to_string();
176 registry.quantum_processors.insert(id, Box::new(processor));
177 }
178 Ok(())
179 }
180
181 pub fn register_neural_processor<T>(&self, processor: T) -> SparseResult<()>
183 where
184 T: NeuralProcessorMonitor + 'static,
185 {
186 if let Ok(mut registry) = self.processor_registry.lock() {
187 let id = processor.get_id().to_string();
188 registry.neural_processors.insert(id, Box::new(processor));
189 }
190 Ok(())
191 }
192
193 pub fn register_hybrid_processor<T>(&self, processor: T) -> SparseResult<()>
195 where
196 T: HybridProcessorMonitor + 'static,
197 {
198 if let Ok(mut registry) = self.processor_registry.lock() {
199 let id = processor.get_id().to_string();
200 registry.hybrid_processors.insert(id, Box::new(processor));
201 }
202 Ok(())
203 }
204
205 pub fn register_memory_compressor<T>(&self, compressor: T) -> SparseResult<()>
207 where
208 T: MemoryCompressorMonitor + 'static,
209 {
210 if let Ok(mut registry) = self.processor_registry.lock() {
211 let id = compressor.get_id().to_string();
212 registry.memory_compressors.insert(id, Box::new(compressor));
213 }
214 Ok(())
215 }
216
217 pub fn get_monitoring_summary(&self) -> MonitoringSummary {
219 let monitoring_active = self.is_monitoring_active();
220 let total_samples = self.sample_counter.load(Ordering::Relaxed);
221
222 let active_alerts = if let Ok(alerts) = self.alert_manager.lock() {
223 alerts.get_active_alerts().len()
224 } else {
225 0
226 };
227
228 let registered_processors = if let Ok(registry) = self.processor_registry.lock() {
229 registry.total_processor_count()
230 } else {
231 0
232 };
233
234 let system_health_score = if let Ok(metrics) = self.system_metrics.lock() {
235 metrics.health_score()
236 } else {
237 0.0
238 };
239
240 MonitoringSummary {
241 monitoring_active,
242 total_samples,
243 active_alerts,
244 registered_processors,
245 uptime_seconds: 0, average_sampling_rate: 0.0, system_health_score,
248 }
249 }
250
251 pub fn get_recent_samples(&self, count: usize) -> Vec<PerformanceSample> {
253 if let Ok(history) = self.performance_history.lock() {
254 history
255 .get_recent_samples(count)
256 .into_iter()
257 .cloned()
258 .collect()
259 } else {
260 Vec::new()
261 }
262 }
263
264 pub fn get_active_alerts(&self) -> Vec<Alert> {
266 if let Ok(alerts) = self.alert_manager.lock() {
267 alerts.get_active_alerts().into_iter().cloned().collect()
268 } else {
269 Vec::new()
270 }
271 }
272
273 pub fn get_system_metrics(&self) -> Option<SystemMetrics> {
275 self.system_metrics
276 .lock()
277 .ok()
278 .map(|metrics| metrics.clone())
279 }
280
281 pub fn record_sample(&self, sample: PerformanceSample) -> SparseResult<()> {
283 self.sample_counter.fetch_add(1, Ordering::Relaxed);
284
285 if let Ok(mut history) = self.performance_history.lock() {
287 history.add_sample(sample.clone());
288 }
289
290 if self.config.enable_alerts {
292 if let Ok(mut alerts) = self.alert_manager.lock() {
293 alerts.process_sample(&sample, None);
294 }
295 }
296
297 Ok(())
298 }
299
300 pub fn get_processor_summary(&self) -> Vec<super::history::ProcessorSummary> {
302 if let Ok(history) = self.performance_history.lock() {
303 history.get_processor_summary()
304 } else {
305 Vec::new()
306 }
307 }
308
309 pub fn clear_data(&self) -> SparseResult<()> {
311 if let Ok(mut history) = self.performance_history.lock() {
312 history.clear();
313 }
314
315 if let Ok(mut alerts) = self.alert_manager.lock() {
316 alerts.clear_all_alerts();
317 }
318
319 self.sample_counter.store(0, Ordering::Relaxed);
320
321 Ok(())
322 }
323
324 fn collect_processor_samples(registry: &ProcessorRegistry) -> Vec<PerformanceSample> {
327 let mut samples = Vec::new();
328
329 for (id, processor) in ®istry.quantum_processors {
331 let stats = processor.get_stats();
332 let sample = Self::quantum_stats_to_sample(id, &stats);
333 samples.push(sample);
334 }
335
336 for (id, processor) in ®istry.neural_processors {
338 let stats = processor.get_stats();
339 let sample = Self::neural_stats_to_sample(id, &stats);
340 samples.push(sample);
341 }
342
343 for (id, processor) in ®istry.hybrid_processors {
345 let stats = processor.get_stats();
346 let sample = Self::hybrid_stats_to_sample(id, &stats);
347 samples.push(sample);
348 }
349
350 for (id, compressor) in ®istry.memory_compressors {
352 let stats = compressor.get_stats();
353 let sample = Self::memory_stats_to_sample(id, &stats);
354 samples.push(sample);
355 }
356
357 samples
358 }
359
360 fn quantum_stats_to_sample(id: &str, stats: &QuantumProcessorStats) -> PerformanceSample {
361 PerformanceSample::new(ProcessorType::QuantumInspired, id.to_string())
362 .with_execution_time(stats.evolution_time * 1000.0) .with_throughput(stats.operations_count as f64)
364 .with_cache_hit_ratio(stats.cache_efficiency)
365 .with_error_rate(stats.decoherence_rate)
366 .with_quantum_coherence(stats.average_logical_fidelity)
367 }
368
369 fn neural_stats_to_sample(id: &str, stats: &NeuralProcessorStats) -> PerformanceSample {
370 PerformanceSample::new(ProcessorType::NeuralAdaptive, id.to_string())
371 .with_throughput(stats.total_operations as f64)
372 .with_cache_hit_ratio(stats.pattern_memory_hit_rate)
373 .with_neural_confidence(stats.neural_network_accuracy)
374 .with_custom_metric(
375 "performance_improvement".to_string(),
376 stats.average_performance_improvement,
377 )
378 .with_custom_metric("rl_reward".to_string(), stats.rl_agent_reward)
379 .with_custom_metric(
380 "attention_score".to_string(),
381 stats.transformer_attention_score,
382 )
383 }
384
385 fn hybrid_stats_to_sample(id: &str, stats: &QuantumNeuralHybridStats) -> PerformanceSample {
386 PerformanceSample::new(ProcessorType::QuantumNeuralHybrid, id.to_string())
387 .with_throughput(stats.total_operations as f64)
388 .with_memory_usage(stats.memory_utilization * 100.0) .with_quantum_coherence(stats.quantum_coherence)
390 .with_neural_confidence(stats.neural_confidence)
391 .with_custom_metric("hybrid_synchronization".to_string(), stats.hybrid_synchronization)
392 .with_custom_metric("entanglement_strength".to_string(), stats.entanglement_strength)
393 .with_custom_metric("average_performance".to_string(), stats.average_performance)
394 }
395
396 fn memory_stats_to_sample(id: &str, stats: &MemoryStats) -> PerformanceSample {
397 let compression_ratio = if stats.compression_stats.total_uncompressed_size > 0 {
398 stats.compression_stats.total_compressed_size as f64
399 / stats.compression_stats.total_uncompressed_size as f64
400 } else {
401 1.0
402 };
403
404 PerformanceSample::new(ProcessorType::MemoryCompression, id.to_string())
405 .with_memory_usage(stats.current_memory_usage as f64 / (1024.0 * 1024.0))
406 .with_cache_hit_ratio(stats.cache_hit_ratio)
407 .with_compression_ratio(compression_ratio)
408 }
409
410 fn update_system_metrics(metrics: &mut SystemMetrics) {
411 metrics.update_timestamp();
412
413 metrics.cpu_usage = Self::get_cpu_usage();
416 metrics.memory_usage = Self::get_memory_usage();
417 metrics.gpu_usage = Self::get_gpu_usage();
418 metrics.system_load = Self::get_system_load();
419 metrics.temperature = Self::get_system_temperature();
420 }
421
422 fn get_cpu_usage() -> f64 {
424 0.0
426 }
427
428 fn get_memory_usage() -> f64 {
429 0.0
431 }
432
433 fn get_gpu_usage() -> f64 {
434 0.0
436 }
437
438 fn get_system_load() -> f64 {
439 0.0
441 }
442
443 fn get_system_temperature() -> f64 {
444 0.0
446 }
447}
448
449impl ProcessorRegistry {
450 fn new() -> Self {
451 Self {
452 quantum_processors: HashMap::new(),
453 neural_processors: HashMap::new(),
454 hybrid_processors: HashMap::new(),
455 memory_compressors: HashMap::new(),
456 }
457 }
458
459 fn total_processor_count(&self) -> usize {
460 self.quantum_processors.len()
461 + self.neural_processors.len()
462 + self.hybrid_processors.len()
463 + self.memory_compressors.len()
464 }
465}
466
467#[cfg(test)]
468mod tests {
469 use super::*;
470
471 struct MockQuantumProcessor {
473 id: String,
474 }
475
476 impl QuantumProcessorMonitor for MockQuantumProcessor {
477 fn get_stats(&self) -> QuantumProcessorStats {
478 QuantumProcessorStats {
479 operations_count: 100,
480 coherence_time: 95.0,
481 decoherence_rate: 0.1,
482 entanglement_strength: 0.8,
483 cache_efficiency: 0.8,
484 error_correction_enabled: true,
485 active_error_syndromes: 2,
486 average_logical_fidelity: 0.9,
487 evolution_time: 1000.0,
488 }
489 }
490
491 fn get_id(&self) -> &str {
492 &self.id
493 }
494 }
495
496 #[test]
497 fn test_monitor_creation() {
498 let config = PerformanceMonitorConfig::default();
499 let monitor = RealTimePerformanceMonitor::new(config);
500 assert!(!monitor.is_monitoring_active());
501 }
502
503 #[test]
504 fn test_processor_registration() {
505 let config = PerformanceMonitorConfig::default();
506 let monitor = RealTimePerformanceMonitor::new(config);
507
508 let processor = MockQuantumProcessor {
509 id: "test-quantum".to_string(),
510 };
511
512 let result = monitor.register_quantum_processor(processor);
513 assert!(result.is_ok());
514
515 let summary = monitor.get_monitoring_summary();
516 assert_eq!(summary.registered_processors, 1);
517 }
518
519 #[test]
520 fn test_monitoring_summary() {
521 let config = PerformanceMonitorConfig::default();
522 let monitor = RealTimePerformanceMonitor::new(config);
523
524 let summary = monitor.get_monitoring_summary();
525 assert!(!summary.monitoring_active);
526 assert_eq!(summary.total_samples, 0);
527 assert_eq!(summary.active_alerts, 0);
528 assert_eq!(summary.registered_processors, 0);
529 }
530
531 #[test]
532 fn test_custom_sample_recording() {
533 let config = PerformanceMonitorConfig::default();
534 let monitor = RealTimePerformanceMonitor::new(config);
535
536 let sample = PerformanceSample::new(ProcessorType::QuantumInspired, "test".to_string())
537 .with_execution_time(100.0);
538
539 let result = monitor.record_sample(sample);
540 assert!(result.is_ok());
541
542 let recent_samples = monitor.get_recent_samples(10);
543 assert_eq!(recent_samples.len(), 1);
544 }
545
546 #[test]
547 fn test_data_clearing() {
548 let config = PerformanceMonitorConfig::default();
549 let monitor = RealTimePerformanceMonitor::new(config);
550
551 let sample = PerformanceSample::new(ProcessorType::QuantumInspired, "test".to_string());
553 let _ = monitor.record_sample(sample);
554
555 let result = monitor.clear_data();
557 assert!(result.is_ok());
558
559 let recent_samples = monitor.get_recent_samples(10);
560 assert_eq!(recent_samples.len(), 0);
561 }
562}