1use crate::{MemoryProfiler, ProfileEvent, Profiler, TorshResult};
7use std::collections::HashMap;
8use std::sync::{Arc, Mutex};
9use std::thread;
10use std::time::{Duration, SystemTime, UNIX_EPOCH};
11use torsh_core::TorshError;
12
13use super::types::{
14 DashboardAlert, DashboardConfig, DashboardData, MemoryMetrics, OperationSummary,
15 PerformanceMetrics, SystemMetrics,
16};
17
18pub fn data_collection_loop(
24 data_history: Arc<Mutex<Vec<DashboardData>>>,
25 _alerts: Arc<Mutex<Vec<DashboardAlert>>>,
26 config: DashboardConfig,
27 running: Arc<Mutex<bool>>,
28 profiler: Arc<Profiler>,
29 memory_profiler: Arc<MemoryProfiler>,
30) {
31 while {
32 let is_running = running.lock().map(|r| *r).unwrap_or(false);
33 is_running
34 } {
35 if let Ok(data) = collect_dashboard_data(&profiler, &memory_profiler) {
37 if let Ok(mut history) = data_history.lock() {
38 history.push(data);
39
40 if history.len() > config.max_data_points {
42 history.remove(0);
43 }
44 }
45 }
46
47 thread::sleep(Duration::from_secs(config.refresh_interval));
49 }
50}
51
52pub fn collect_dashboard_data(
54 profiler: &Profiler,
55 memory_profiler: &MemoryProfiler,
56) -> TorshResult<DashboardData> {
57 let timestamp = SystemTime::now()
58 .duration_since(UNIX_EPOCH)
59 .map_err(|_| TorshError::RuntimeError("Failed to get timestamp".to_string()))?
60 .as_secs();
61
62 let performance_metrics = collect_performance_metrics(profiler)?;
64
65 let memory_metrics = collect_memory_metrics(memory_profiler)?;
67
68 let system_metrics = collect_system_metrics()?;
70
71 let top_operations = get_top_operations(profiler)?;
73
74 Ok(DashboardData {
75 timestamp,
76 performance_metrics,
77 memory_metrics,
78 system_metrics,
79 alerts: Vec::new(), top_operations,
81 })
82}
83
84pub fn collect_performance_metrics(profiler: &Profiler) -> TorshResult<PerformanceMetrics> {
90 let events = profiler.events();
91
92 if events.is_empty() {
93 return Ok(PerformanceMetrics {
94 total_operations: 0,
95 average_duration_ms: 0.0,
96 operations_per_second: 0.0,
97 total_flops: 0,
98 gflops_per_second: 0.0,
99 cpu_utilization: 0.0,
100 thread_count: 0,
101 });
102 }
103
104 let total_operations = events.len() as u64;
105 let total_duration_us: u64 = events.iter().map(|e| e.duration_us).sum();
106 let average_duration_ms = (total_duration_us as f64) / (events.len() as f64) / 1000.0;
107
108 let total_flops: u64 = events.iter().map(|e| e.flops.unwrap_or(0)).sum();
109
110 let total_time_seconds = total_duration_us as f64 / 1_000_000.0;
111 let operations_per_second = if total_time_seconds > 0.0 {
112 total_operations as f64 / total_time_seconds
113 } else {
114 0.0
115 };
116
117 let gflops_per_second = if total_time_seconds > 0.0 {
118 (total_flops as f64) / total_time_seconds / 1_000_000_000.0
119 } else {
120 0.0
121 };
122
123 let unique_threads: std::collections::HashSet<_> = events.iter().map(|e| e.thread_id).collect();
124 let thread_count = unique_threads.len();
125
126 let cpu_utilization = (thread_count as f64) / (num_cpus::get() as f64) * 100.0;
127
128 Ok(PerformanceMetrics {
129 total_operations,
130 average_duration_ms,
131 operations_per_second,
132 total_flops,
133 gflops_per_second,
134 cpu_utilization,
135 thread_count,
136 })
137}
138
139pub fn collect_advanced_performance_metrics(
141 profiler: &Profiler,
142) -> TorshResult<AdvancedPerformanceMetrics> {
143 let events = profiler.events();
144
145 if events.is_empty() {
146 return Ok(AdvancedPerformanceMetrics::default());
147 }
148
149 let mut durations: Vec<u64> = events.iter().map(|e| e.duration_us).collect();
151 durations.sort_unstable();
152
153 let p50 = percentile(&durations, 50.0);
154 let p95 = percentile(&durations, 95.0);
155 let p99 = percentile(&durations, 99.0);
156
157 let total_cpu_time_us: u64 = events.iter().map(|e| e.duration_us).sum();
159 let wall_clock_time_us = events
160 .iter()
161 .map(|e| e.start_us + e.duration_us)
162 .max()
163 .unwrap_or(0)
164 - events.iter().map(|e| e.start_us).min().unwrap_or(0);
165
166 let parallel_efficiency = if wall_clock_time_us > 0 {
167 (total_cpu_time_us as f64) / (wall_clock_time_us as f64)
168 } else {
169 0.0
170 };
171
172 let cache_hit_rate = 0.0; Ok(AdvancedPerformanceMetrics {
176 percentile_50_ms: p50 as f64 / 1000.0,
177 percentile_95_ms: p95 as f64 / 1000.0,
178 percentile_99_ms: p99 as f64 / 1000.0,
179 parallel_efficiency,
180 cache_hit_rate,
181 total_cpu_time_seconds: total_cpu_time_us as f64 / 1_000_000.0,
182 wall_clock_time_seconds: wall_clock_time_us as f64 / 1_000_000.0,
183 })
184}
185
186pub fn collect_memory_metrics(memory_profiler: &MemoryProfiler) -> TorshResult<MemoryMetrics> {
192 let stats = memory_profiler.get_stats()?;
193
194 Ok(MemoryMetrics {
195 current_usage_mb: stats.allocated as f64 / (1024.0 * 1024.0),
196 peak_usage_mb: stats.peak as f64 / (1024.0 * 1024.0),
197 total_allocations: stats.allocations as u64,
198 total_deallocations: stats.deallocations as u64,
199 active_allocations: stats.allocations.saturating_sub(stats.deallocations) as u64,
200 fragmentation_ratio: calculate_fragmentation_ratio(&stats),
201 allocation_rate: calculate_allocation_rate(&stats),
202 })
203}
204
205fn calculate_fragmentation_ratio(stats: &crate::MemoryStats) -> f64 {
207 if stats.allocated > 0 {
210 1.0 - (stats.allocated as f64 / stats.peak as f64)
211 } else {
212 0.0
213 }
214}
215
216fn calculate_allocation_rate(stats: &crate::MemoryStats) -> f64 {
218 stats.allocations as f64 / 60.0 }
222
223pub fn collect_detailed_memory_metrics(
225 memory_profiler: &MemoryProfiler,
226) -> TorshResult<DetailedMemoryMetrics> {
227 let stats = memory_profiler.get_stats()?;
228
229 let allocation_trend = calculate_allocation_trend(memory_profiler)?;
231 let deallocation_trend = calculate_deallocation_trend(memory_profiler)?;
232
233 let efficiency = if stats.allocations > 0 {
235 stats.deallocations as f64 / stats.allocations as f64
236 } else {
237 0.0
238 };
239
240 Ok(DetailedMemoryMetrics {
241 heap_usage_mb: stats.allocated as f64 / (1024.0 * 1024.0),
242 stack_usage_mb: 0.0, allocation_trend,
244 deallocation_trend,
245 memory_efficiency: efficiency,
246 largest_allocation_mb: 0.0, memory_pressure: calculate_memory_pressure(&stats),
248 })
249}
250
251pub fn collect_system_metrics() -> TorshResult<SystemMetrics> {
257 #[cfg(target_os = "linux")]
261 {
262 return collect_linux_system_metrics();
263 }
264
265 #[cfg(target_os = "macos")]
266 {
267 return collect_macos_system_metrics();
268 }
269
270 #[cfg(target_os = "windows")]
271 {
272 return collect_windows_system_metrics();
273 }
274
275 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
277 {
278 Ok(SystemMetrics {
279 uptime_seconds: 0,
280 load_average: 0.0,
281 available_memory_mb: get_available_memory_mb(),
282 disk_usage_percent: get_disk_usage_percent(),
283 network_io_mbps: None,
284 })
285 }
286}
287
288#[cfg(target_os = "linux")]
289fn collect_linux_system_metrics() -> TorshResult<SystemMetrics> {
290 use std::fs;
291
292 let uptime_seconds = fs::read_to_string("/proc/uptime")
294 .ok()
295 .and_then(|content| {
296 content
297 .split_whitespace()
298 .next()
299 .and_then(|s| s.parse::<f64>().ok())
300 })
301 .unwrap_or(0.0) as u64;
302
303 let load_average = fs::read_to_string("/proc/loadavg")
305 .ok()
306 .and_then(|content| {
307 content
308 .split_whitespace()
309 .next()
310 .and_then(|s| s.parse::<f64>().ok())
311 })
312 .unwrap_or(0.0);
313
314 Ok(SystemMetrics {
315 uptime_seconds,
316 load_average,
317 available_memory_mb: get_available_memory_mb(),
318 disk_usage_percent: get_disk_usage_percent(),
319 network_io_mbps: get_network_io_mbps(),
320 })
321}
322
323#[cfg(target_os = "macos")]
324fn collect_macos_system_metrics() -> TorshResult<SystemMetrics> {
325 Ok(SystemMetrics {
327 uptime_seconds: get_macos_uptime(),
328 load_average: get_macos_load_average(),
329 available_memory_mb: get_available_memory_mb(),
330 disk_usage_percent: get_disk_usage_percent(),
331 network_io_mbps: get_network_io_mbps(),
332 })
333}
334
335#[cfg(target_os = "windows")]
336fn collect_windows_system_metrics() -> TorshResult<SystemMetrics> {
337 Ok(SystemMetrics {
339 uptime_seconds: get_windows_uptime(),
340 load_average: get_windows_cpu_usage(),
341 available_memory_mb: get_available_memory_mb(),
342 disk_usage_percent: get_disk_usage_percent(),
343 network_io_mbps: get_network_io_mbps(),
344 })
345}
346
347pub fn get_top_operations(profiler: &Profiler) -> TorshResult<Vec<OperationSummary>> {
353 let events = profiler.events();
354
355 if events.is_empty() {
356 return Ok(Vec::new());
357 }
358
359 let mut operation_stats: HashMap<String, (u64, u64)> = HashMap::new();
360 let total_duration: u64 = events.iter().map(|e| e.duration_us).sum();
361
362 for event in events {
364 let entry = operation_stats.entry(event.name.clone()).or_insert((0, 0));
365 entry.0 += 1; entry.1 += event.duration_us; }
368
369 let mut summaries: Vec<OperationSummary> = operation_stats
371 .into_iter()
372 .map(|(name, (count, total_duration_us))| {
373 let total_duration_ms = total_duration_us as f64 / 1000.0;
374 let average_duration_ms = total_duration_ms / count as f64;
375 let percentage_of_total = if total_duration > 0 {
376 (total_duration_us as f64 / total_duration as f64) * 100.0
377 } else {
378 0.0
379 };
380
381 OperationSummary {
382 name: name.clone(),
383 category: categorize_operation(&name),
384 count,
385 total_duration_ms,
386 average_duration_ms,
387 percentage_of_total,
388 }
389 })
390 .collect();
391
392 summaries.sort_by(|a, b| {
393 b.total_duration_ms
394 .partial_cmp(&a.total_duration_ms)
395 .unwrap_or(std::cmp::Ordering::Equal)
396 });
397
398 summaries.truncate(10);
400
401 Ok(summaries)
402}
403
404pub fn analyze_operation_patterns(profiler: &Profiler) -> TorshResult<OperationPatternAnalysis> {
406 let events = profiler.events();
407
408 if events.is_empty() {
409 return Ok(OperationPatternAnalysis::default());
410 }
411
412 let mut patterns = HashMap::new();
414 for event in events {
415 let category = categorize_operation(&event.name);
416 let entry = patterns.entry(category).or_insert(Vec::new());
417 entry.push(event);
418 }
419
420 let mut category_analysis = HashMap::new();
422 for (category, events) in patterns {
423 let total_duration: u64 = events.iter().map(|e| e.duration_us).sum();
424 let avg_duration = total_duration as f64 / events.len() as f64;
425 let total_flops: u64 = events.iter().map(|e| e.flops.unwrap_or(0)).sum();
426
427 category_analysis.insert(
428 category.clone(),
429 CategoryMetrics {
430 operation_count: events.len() as u64,
431 total_duration_ms: total_duration as f64 / 1000.0,
432 average_duration_ms: avg_duration / 1000.0,
433 total_flops,
434 },
435 );
436 }
437
438 Ok(OperationPatternAnalysis {
439 categories: category_analysis.clone(),
440 total_unique_operations: events.len() as u64,
441 dominant_category: find_dominant_category(&category_analysis),
442 })
443}
444
445fn percentile(sorted_data: &[u64], percentile: f64) -> u64 {
451 if sorted_data.is_empty() {
452 return 0;
453 }
454
455 let index = (percentile / 100.0) * (sorted_data.len() - 1) as f64;
456 let lower = index.floor() as usize;
457 let upper = index.ceil() as usize;
458
459 if lower == upper {
460 sorted_data[lower]
461 } else {
462 let weight = index - lower as f64;
463 let lower_val = sorted_data[lower] as f64;
464 let upper_val = sorted_data[upper] as f64;
465 (lower_val + weight * (upper_val - lower_val)) as u64
466 }
467}
468
469fn categorize_operation(name: &str) -> String {
471 if name.contains("conv") || name.contains("convolution") {
472 "Convolution".to_string()
473 } else if name.contains("matmul") || name.contains("gemm") {
474 "Matrix Operations".to_string()
475 } else if name.contains("add") || name.contains("sub") || name.contains("mul") {
476 "Arithmetic".to_string()
477 } else if name.contains("relu") || name.contains("sigmoid") || name.contains("tanh") {
478 "Activation".to_string()
479 } else if name.contains("batch_norm") || name.contains("layer_norm") {
480 "Normalization".to_string()
481 } else if name.contains("dropout") {
482 "Regularization".to_string()
483 } else {
484 "Other".to_string()
485 }
486}
487
488fn calculate_allocation_trend(memory_profiler: &MemoryProfiler) -> TorshResult<f64> {
490 Ok(0.0)
493}
494
495fn calculate_deallocation_trend(memory_profiler: &MemoryProfiler) -> TorshResult<f64> {
497 Ok(0.0)
500}
501
502fn calculate_memory_pressure(stats: &crate::MemoryStats) -> f64 {
504 if stats.peak > 0 {
506 stats.allocated as f64 / stats.peak as f64
507 } else {
508 0.0
509 }
510}
511
512fn find_dominant_category(analysis: &HashMap<String, CategoryMetrics>) -> String {
514 analysis
515 .iter()
516 .max_by(|a, b| {
517 a.1.total_duration_ms
518 .partial_cmp(&b.1.total_duration_ms)
519 .unwrap_or(std::cmp::Ordering::Equal)
520 })
521 .map(|(category, _)| category.clone())
522 .unwrap_or_else(|| "Unknown".to_string())
523}
524
525#[cfg(target_os = "macos")]
527fn get_macos_uptime() -> u64 {
528 0
530}
531
532#[cfg(target_os = "macos")]
533fn get_macos_load_average() -> f64 {
534 0.0
536}
537
538#[cfg(target_os = "windows")]
539fn get_windows_uptime() -> u64 {
540 0
542}
543
544#[cfg(target_os = "windows")]
545fn get_windows_cpu_usage() -> f64 {
546 0.0
548}
549
550fn get_available_memory_mb() -> f64 {
551 8192.0 }
555
556fn get_disk_usage_percent() -> f64 {
557 50.0 }
560
561fn get_network_io_mbps() -> Option<f64> {
568 None
569}
570
571#[derive(Debug, Clone)]
577pub struct AdvancedPerformanceMetrics {
578 pub percentile_50_ms: f64,
579 pub percentile_95_ms: f64,
580 pub percentile_99_ms: f64,
581 pub parallel_efficiency: f64,
582 pub cache_hit_rate: f64,
583 pub total_cpu_time_seconds: f64,
584 pub wall_clock_time_seconds: f64,
585}
586
587impl Default for AdvancedPerformanceMetrics {
588 fn default() -> Self {
589 Self {
590 percentile_50_ms: 0.0,
591 percentile_95_ms: 0.0,
592 percentile_99_ms: 0.0,
593 parallel_efficiency: 0.0,
594 cache_hit_rate: 0.0,
595 total_cpu_time_seconds: 0.0,
596 wall_clock_time_seconds: 0.0,
597 }
598 }
599}
600
601#[derive(Debug, Clone)]
603pub struct DetailedMemoryMetrics {
604 pub heap_usage_mb: f64,
605 pub stack_usage_mb: f64,
606 pub allocation_trend: f64,
607 pub deallocation_trend: f64,
608 pub memory_efficiency: f64,
609 pub largest_allocation_mb: f64,
610 pub memory_pressure: f64,
611}
612
613#[derive(Debug, Clone)]
615pub struct OperationPatternAnalysis {
616 pub categories: HashMap<String, CategoryMetrics>,
617 pub total_unique_operations: u64,
618 pub dominant_category: String,
619}
620
621impl Default for OperationPatternAnalysis {
622 fn default() -> Self {
623 Self {
624 categories: HashMap::new(),
625 total_unique_operations: 0,
626 dominant_category: "Unknown".to_string(),
627 }
628 }
629}
630
631#[derive(Debug, Clone)]
633pub struct CategoryMetrics {
634 pub operation_count: u64,
635 pub total_duration_ms: f64,
636 pub average_duration_ms: f64,
637 pub total_flops: u64,
638}
639
640pub struct MetricsCollector {
646 pub config: MetricsCollectorConfig,
648}
649
650#[derive(Debug, Clone)]
652pub struct MetricsCollectorConfig {
653 pub enable_memory_tracking: bool,
655 pub enable_system_metrics: bool,
657 pub sample_rate: f64,
659}
660
661impl Default for MetricsCollectorConfig {
662 fn default() -> Self {
663 Self {
664 enable_memory_tracking: true,
665 enable_system_metrics: true,
666 sample_rate: 1.0,
667 }
668 }
669}
670
671impl MetricsCollector {
672 pub fn new() -> Self {
674 Self {
675 config: MetricsCollectorConfig::default(),
676 }
677 }
678
679 pub fn new_with_config(config: MetricsCollectorConfig) -> Self {
681 Self { config }
682 }
683
684 pub fn collect_dashboard_data(
686 &self,
687 profiler: &Profiler,
688 memory_profiler: &MemoryProfiler,
689 ) -> TorshResult<DashboardData> {
690 collect_dashboard_data(profiler, memory_profiler)
691 }
692
693 pub fn collect_performance_metrics(
695 &self,
696 profiler: &Profiler,
697 ) -> TorshResult<PerformanceMetrics> {
698 collect_performance_metrics(profiler)
699 }
700
701 pub fn collect_memory_metrics(
703 &self,
704 memory_profiler: &MemoryProfiler,
705 ) -> TorshResult<MemoryMetrics> {
706 collect_memory_metrics(memory_profiler)
707 }
708
709 pub fn collect_system_metrics(&self) -> TorshResult<SystemMetrics> {
711 collect_system_metrics()
712 }
713}
714
715impl Default for MetricsCollector {
716 fn default() -> Self {
717 Self::new()
718 }
719}