torsh-backend 0.1.2

Backend abstraction layer for ToRSh
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//! Pool management and analytics for SciRS2 integration
//!
//! This module handles memory pool information, advanced analytics,
//! health assessment, and optimization recommendations.

// Framework infrastructure - components designed for future use
#![allow(dead_code)]
use super::config::{HealthIndicator, HealthTrend, PoolOptimizationRecommendation, RiskFactor};
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// SciRS2 memory pool information
///
/// Detailed information about SciRS2 memory pools with performance
/// analytics and optimization recommendations.
#[derive(Debug, Clone)]
pub struct ScirS2PoolInfo {
    /// Pool identifier
    pub pool_id: String,

    /// Pool type (e.g., "tensor", "buffer", "scratch")
    pub pool_type: String,

    /// Pool capacity in bytes
    pub capacity: usize,

    /// Pool utilization ratio (0.0 to 1.0)
    pub utilization: f64,

    /// Pool performance metrics
    pub performance_metrics: HashMap<String, f64>,

    /// Advanced pool analytics
    pub advanced_analytics: PoolAdvancedAnalytics,

    /// Pool health assessment
    pub health_assessment: PoolHealthAssessment,

    /// Optimization recommendations
    pub optimization_recommendations: Vec<PoolOptimizationRecommendation>,
}

/// Advanced pool analytics
#[derive(Debug, Clone)]
pub struct PoolAdvancedAnalytics {
    /// Allocation patterns
    pub allocation_patterns: PoolAllocationPatterns,

    /// Usage efficiency over time
    pub efficiency_timeline: Vec<(Instant, f64)>,

    /// Peak usage times
    pub peak_usage_times: Vec<Instant>,

    /// Idle periods
    pub idle_periods: Vec<(Instant, Duration)>,

    /// Memory turnover rate
    pub turnover_rate: f64,
}

/// Pool allocation patterns
#[derive(Debug, Clone)]
pub struct PoolAllocationPatterns {
    /// Allocation frequency distribution
    pub frequency_distribution: HashMap<usize, u64>, // size -> count

    /// Temporal allocation patterns
    pub temporal_patterns: Vec<TemporalPattern>,

    /// Spatial allocation clustering
    pub spatial_clustering: SpatialClustering,

    /// Predictive allocation model
    pub predictive_model: Option<PoolPredictiveModel>,
}

/// Temporal allocation pattern
#[derive(Debug, Clone)]
pub struct TemporalPattern {
    /// Pattern name
    pub name: String,

    /// Pattern period
    pub period: Duration,

    /// Pattern strength (0.0 to 1.0)
    pub strength: f64,

    /// Pattern phase offset
    pub phase_offset: Duration,
}

/// Spatial allocation clustering
#[derive(Debug, Clone)]
pub struct SpatialClustering {
    /// Number of clusters detected
    pub cluster_count: usize,

    /// Cluster centers
    pub cluster_centers: Vec<usize>,

    /// Cluster densities
    pub cluster_densities: Vec<f64>,

    /// Inter-cluster distances
    pub inter_cluster_distances: Vec<f64>,
}

/// Pool predictive model
#[derive(Debug, Clone)]
pub struct PoolPredictiveModel {
    /// Model type
    pub model_type: String,

    /// Model accuracy
    pub accuracy: f64,

    /// Prediction horizon
    pub prediction_horizon: Duration,

    /// Next predicted allocations
    pub next_predicted_allocations: Vec<PredictedAllocation>,
}

/// Predicted allocation
#[derive(Debug, Clone)]
pub struct PredictedAllocation {
    /// Expected size
    pub size: usize,

    /// Expected time
    pub time: Instant,

    /// Confidence level
    pub confidence: f64,

    /// Allocation type
    pub allocation_type: String,
}

/// Pool health assessment
#[derive(Debug, Clone)]
pub struct PoolHealthAssessment {
    /// Overall health score (0.0 to 1.0)
    pub health_score: f64,

    /// Health indicators
    pub health_indicators: Vec<HealthIndicator>,

    /// Risk factors
    pub risk_factors: Vec<RiskFactor>,

    /// Health trend
    pub health_trend: HealthTrend,
}

impl ScirS2PoolInfo {
    /// Create new pool information
    pub fn new(pool_id: String, pool_type: String, capacity: usize) -> Self {
        Self {
            pool_id,
            pool_type,
            capacity,
            utilization: 0.0,
            performance_metrics: HashMap::new(),
            advanced_analytics: PoolAdvancedAnalytics::default(),
            health_assessment: PoolHealthAssessment::default(),
            optimization_recommendations: Vec::new(),
        }
    }

    /// Update pool utilization
    pub fn update_utilization(&mut self, new_utilization: f64) {
        let _old_utilization = self.utilization;
        self.utilization = new_utilization.clamp(0.0, 1.0);

        // Record utilization change in efficiency timeline
        self.advanced_analytics
            .efficiency_timeline
            .push((Instant::now(), self.utilization));

        // Trim efficiency timeline to keep last 1000 entries
        if self.advanced_analytics.efficiency_timeline.len() > 1000 {
            self.advanced_analytics.efficiency_timeline.remove(0);
        }

        // Check for peak usage
        if self.utilization > 0.9 {
            self.advanced_analytics
                .peak_usage_times
                .push(Instant::now());
            // Keep only recent peak usage times (last 100)
            if self.advanced_analytics.peak_usage_times.len() > 100 {
                self.advanced_analytics.peak_usage_times.remove(0);
            }
        }

        // Update health assessment
        self.update_health_assessment();
    }

    /// Record allocation in the pool
    pub fn record_allocation(&mut self, size: usize) {
        // Update frequency distribution
        *self
            .advanced_analytics
            .allocation_patterns
            .frequency_distribution
            .entry(size)
            .or_insert(0) += 1;

        // Update performance metrics
        self.performance_metrics
            .insert("last_allocation_size".to_string(), size as f64);
        self.performance_metrics.insert(
            "last_allocation_time".to_string(),
            Instant::now()
                .duration_since(Instant::now() - Duration::from_secs(1))
                .as_secs_f64(),
        );
    }

    /// Analyze allocation patterns and detect temporal patterns
    pub fn analyze_patterns(&mut self) {
        // Simplified pattern detection
        let mut patterns = Vec::new();

        // Detect periodic patterns in allocation frequency
        if let Some(pattern) = self.detect_periodic_pattern() {
            patterns.push(pattern);
        }

        // Detect burst patterns
        if let Some(pattern) = self.detect_burst_pattern() {
            patterns.push(pattern);
        }

        self.advanced_analytics
            .allocation_patterns
            .temporal_patterns = patterns;

        // Update spatial clustering
        self.update_spatial_clustering();
    }

    /// Generate optimization recommendations based on current state
    pub fn generate_recommendations(&mut self) {
        self.optimization_recommendations.clear();

        // High utilization recommendation
        if self.utilization > 0.9 {
            self.optimization_recommendations.push(
                PoolOptimizationRecommendation::high_utilization(&self.pool_id, self.utilization),
            );
        }

        // Low utilization recommendation
        if self.utilization < 0.3 {
            self.optimization_recommendations.push(
                PoolOptimizationRecommendation::low_utilization(&self.pool_id, self.utilization),
            );
        }

        // Fragmentation recommendation
        if self.get_fragmentation_level() > 0.3 {
            self.optimization_recommendations.push(
                PoolOptimizationRecommendation::high_fragmentation(&self.pool_id),
            );
        }

        // Performance recommendation
        if let Some(throughput) = self.performance_metrics.get("throughput") {
            if *throughput < 100.0 {
                self.optimization_recommendations.push(
                    PoolOptimizationRecommendation::low_performance(&self.pool_id, *throughput),
                );
            }
        }
    }

    /// Get current memory usage in bytes
    pub fn used_memory(&self) -> usize {
        (self.capacity as f64 * self.utilization) as usize
    }

    /// Get available memory in bytes
    pub fn available_memory(&self) -> usize {
        self.capacity - self.used_memory()
    }

    /// Get fragmentation level (0.0 to 1.0)
    pub fn get_fragmentation_level(&self) -> f64 {
        // Simplified fragmentation calculation based on cluster analysis
        if self
            .advanced_analytics
            .allocation_patterns
            .spatial_clustering
            .cluster_count
            > 1
        {
            1.0 - (1.0
                / self
                    .advanced_analytics
                    .allocation_patterns
                    .spatial_clustering
                    .cluster_count as f64)
        } else {
            0.0
        }
    }

    /// Check if pool is healthy
    pub fn is_healthy(&self) -> bool {
        self.health_assessment.health_score > 0.7
    }

    /// Get pool efficiency over last N minutes
    pub fn efficiency_over_period(&self, minutes: u64) -> f64 {
        let cutoff = Instant::now() - Duration::from_secs(minutes * 60);
        let recent_entries: Vec<_> = self
            .advanced_analytics
            .efficiency_timeline
            .iter()
            .filter(|(timestamp, _)| *timestamp > cutoff)
            .map(|(_, efficiency)| *efficiency)
            .collect();

        if recent_entries.is_empty() {
            self.utilization
        } else {
            recent_entries.iter().sum::<f64>() / recent_entries.len() as f64
        }
    }

    // Private helper methods

    fn detect_periodic_pattern(&self) -> Option<TemporalPattern> {
        // Simplified periodic pattern detection
        if self.advanced_analytics.peak_usage_times.len() >= 3 {
            let intervals: Vec<Duration> = self
                .advanced_analytics
                .peak_usage_times
                .windows(2)
                .map(|w| w[1].duration_since(w[0]))
                .collect();

            // Check if intervals are roughly similar (indicating periodicity)
            if intervals.len() >= 2 {
                let avg_interval: Duration = Duration::from_secs_f64(
                    intervals.iter().map(|d| d.as_secs_f64()).sum::<f64>() / intervals.len() as f64,
                );

                Some(TemporalPattern {
                    name: "Periodic Peak Usage".to_string(),
                    period: avg_interval,
                    strength: 0.7, // Simplified strength calculation
                    phase_offset: Duration::from_secs(0),
                })
            } else {
                None
            }
        } else {
            None
        }
    }

    fn detect_burst_pattern(&self) -> Option<TemporalPattern> {
        // Detect burst patterns based on utilization spikes
        let recent_efficiency: Vec<f64> = self
            .advanced_analytics
            .efficiency_timeline
            .iter()
            .take(10)
            .map(|(_, eff)| *eff)
            .collect();

        if recent_efficiency.len() >= 5 {
            let variance = self.calculate_variance(&recent_efficiency);
            if variance > 0.1 {
                Some(TemporalPattern {
                    name: "Burst Usage".to_string(),
                    period: Duration::from_secs(60), // Assume 1-minute burst cycles
                    strength: variance.min(1.0),
                    phase_offset: Duration::from_secs(0),
                })
            } else {
                None
            }
        } else {
            None
        }
    }

    fn update_spatial_clustering(&mut self) {
        // Simplified spatial clustering based on allocation size distribution
        let mut clusters = Vec::new();
        let mut densities = Vec::new();

        for (size, count) in &self
            .advanced_analytics
            .allocation_patterns
            .frequency_distribution
        {
            if *count > 10 {
                clusters.push(*size);
                densities.push(*count as f64);
            }
        }

        // Calculate inter-cluster distances
        let mut distances = Vec::new();
        for i in 0..clusters.len() {
            for j in (i + 1)..clusters.len() {
                distances.push((clusters[j] - clusters[i]) as f64);
            }
        }

        self.advanced_analytics
            .allocation_patterns
            .spatial_clustering = SpatialClustering {
            cluster_count: clusters.len(),
            cluster_centers: clusters,
            cluster_densities: densities,
            inter_cluster_distances: distances,
        };
    }

    fn update_health_assessment(&mut self) {
        let mut health_score: f64 = 1.0;
        let mut indicators = Vec::new();
        let mut risk_factors = Vec::new();

        // Utilization health indicator
        let utilization_health = if self.utilization > 0.95 {
            health_score -= 0.3;
            0.0 // Critical utilization
        } else if self.utilization > 0.85 {
            health_score -= 0.1;
            0.5 // Warning utilization
        } else {
            1.0 // Healthy utilization
        };

        indicators.push(HealthIndicator {
            name: "Pool Utilization".to_string(),
            value: self.utilization,
            healthy_range: (0.2, 0.8),
            severity: if utilization_health < 0.5 {
                super::config::HealthSeverity::Critical
            } else if utilization_health < 1.0 {
                super::config::HealthSeverity::Warning
            } else {
                super::config::HealthSeverity::Info
            },
        });

        // Fragmentation health indicator
        let fragmentation = self.get_fragmentation_level();
        if fragmentation > 0.5 {
            health_score -= 0.2;
            risk_factors.push(RiskFactor {
                risk_type: super::config::RiskType::FragmentationIncrease,
                probability: fragmentation,
                impact: 0.7,
                mitigation_strategies: vec![
                    "Consider pool defragmentation".to_string(),
                    "Adjust allocation strategy".to_string(),
                ],
            });
        }

        // Performance health indicator
        if let Some(throughput) = self.performance_metrics.get("throughput") {
            if *throughput < 100.0 {
                health_score -= 0.15;
                indicators.push(HealthIndicator {
                    name: "Pool Throughput".to_string(),
                    value: *throughput,
                    healthy_range: (100.0, 1000.0),
                    severity: super::config::HealthSeverity::Warning,
                });
            }
        }

        // Determine health trend
        let trend = if self.advanced_analytics.efficiency_timeline.len() >= 2 {
            let recent = self
                .advanced_analytics
                .efficiency_timeline
                .last()
                .expect("efficiency_timeline should have at least 2 elements")
                .1;
            let previous = self.advanced_analytics.efficiency_timeline
                [self.advanced_analytics.efficiency_timeline.len() - 2]
                .1;

            if recent > previous + 0.05 {
                HealthTrend::Improving
            } else if recent < previous - 0.05 {
                HealthTrend::Declining
            } else {
                HealthTrend::Stable
            }
        } else {
            HealthTrend::Stable
        };

        self.health_assessment = PoolHealthAssessment {
            health_score: health_score.max(0.0),
            health_indicators: indicators,
            risk_factors,
            health_trend: trend,
        };
    }

    fn calculate_variance(&self, values: &[f64]) -> f64 {
        if values.is_empty() {
            return 0.0;
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;

        variance
    }
}

impl Default for PoolAdvancedAnalytics {
    fn default() -> Self {
        Self {
            allocation_patterns: PoolAllocationPatterns::default(),
            efficiency_timeline: Vec::new(),
            peak_usage_times: Vec::new(),
            idle_periods: Vec::new(),
            turnover_rate: 0.0,
        }
    }
}

impl Default for PoolAllocationPatterns {
    fn default() -> Self {
        Self {
            frequency_distribution: HashMap::new(),
            temporal_patterns: Vec::new(),
            spatial_clustering: SpatialClustering::default(),
            predictive_model: None,
        }
    }
}

impl Default for SpatialClustering {
    fn default() -> Self {
        Self {
            cluster_count: 0,
            cluster_centers: Vec::new(),
            cluster_densities: Vec::new(),
            inter_cluster_distances: Vec::new(),
        }
    }
}

impl Default for PoolHealthAssessment {
    fn default() -> Self {
        Self {
            health_score: 1.0,
            health_indicators: Vec::new(),
            risk_factors: Vec::new(),
            health_trend: HealthTrend::Stable,
        }
    }
}

impl PoolOptimizationRecommendation {
    /// Create a high utilization recommendation
    pub fn high_utilization(pool_id: &str, utilization: f64) -> Self {
        use super::config::{PoolOptimizationType, RecommendationPriority, ResourceRequirements};

        Self {
            recommendation_type: PoolOptimizationType::CapacityAdjustment,
            priority: RecommendationPriority::High,
            description: format!(
                "Pool '{}' has high utilization: {:.1}%",
                pool_id,
                utilization * 100.0
            ),
            implementation_steps: vec![
                "Analyze current allocation patterns".to_string(),
                "Increase pool capacity by 50%".to_string(),
                "Monitor utilization for 24 hours".to_string(),
                "Adjust capacity as needed".to_string(),
            ],
            expected_benefits: vec![
                "Reduced allocation latency".to_string(),
                "Lower risk of allocation failures".to_string(),
                "Improved overall performance".to_string(),
            ],
            resource_requirements: ResourceRequirements {
                additional_memory: (utilization * 1024.0 * 1024.0 * 100.0) as usize, // 100MB estimate
                cpu_overhead: 0.05,
                implementation_time: Duration::from_secs(3600), // 1 hour
                maintenance_overhead: 0.02,
            },
        }
    }

    /// Create a low utilization recommendation
    pub fn low_utilization(pool_id: &str, utilization: f64) -> Self {
        use super::config::{PoolOptimizationType, RecommendationPriority, ResourceRequirements};

        Self {
            recommendation_type: PoolOptimizationType::CapacityAdjustment,
            priority: RecommendationPriority::Medium,
            description: format!(
                "Pool '{}' has low utilization: {:.1}%",
                pool_id,
                utilization * 100.0
            ),
            implementation_steps: vec![
                "Analyze allocation patterns over 7 days".to_string(),
                "Consider reducing pool capacity by 30%".to_string(),
                "Monitor for performance degradation".to_string(),
            ],
            expected_benefits: vec![
                "Reduced memory overhead".to_string(),
                "Lower maintenance costs".to_string(),
                "Better resource utilization".to_string(),
            ],
            resource_requirements: ResourceRequirements {
                additional_memory: 0, // Reducing memory
                cpu_overhead: 0.01,
                implementation_time: Duration::from_secs(1800), // 30 minutes
                maintenance_overhead: -0.01,                    // Reduced maintenance
            },
        }
    }

    /// Create a high fragmentation recommendation
    pub fn high_fragmentation(pool_id: &str) -> Self {
        use super::config::{PoolOptimizationType, RecommendationPriority, ResourceRequirements};

        Self {
            recommendation_type: PoolOptimizationType::GarbageCollectionTuning,
            priority: RecommendationPriority::High,
            description: format!("Pool '{}' has high fragmentation", pool_id),
            implementation_steps: vec![
                "Enable pool defragmentation".to_string(),
                "Adjust allocation strategy to reduce fragmentation".to_string(),
                "Consider memory compaction".to_string(),
                "Monitor fragmentation metrics".to_string(),
            ],
            expected_benefits: vec![
                "Improved memory efficiency".to_string(),
                "Reduced allocation failures".to_string(),
                "Better cache locality".to_string(),
            ],
            resource_requirements: ResourceRequirements {
                additional_memory: 1024 * 1024 * 50, // 50MB for defragmentation
                cpu_overhead: 0.15,
                implementation_time: Duration::from_secs(7200), // 2 hours
                maintenance_overhead: 0.05,
            },
        }
    }

    /// Create a low performance recommendation
    pub fn low_performance(pool_id: &str, throughput: f64) -> Self {
        use super::config::{PoolOptimizationType, RecommendationPriority, ResourceRequirements};

        Self {
            recommendation_type: PoolOptimizationType::AccessPatternOptimization,
            priority: RecommendationPriority::Medium,
            description: format!(
                "Pool '{}' has low throughput: {:.1} ops/sec",
                pool_id, throughput
            ),
            implementation_steps: vec![
                "Analyze access patterns".to_string(),
                "Optimize allocation strategy".to_string(),
                "Consider cache warming".to_string(),
                "Monitor performance improvements".to_string(),
            ],
            expected_benefits: vec![
                "Increased throughput".to_string(),
                "Reduced allocation latency".to_string(),
                "Better resource utilization".to_string(),
            ],
            resource_requirements: ResourceRequirements {
                additional_memory: 1024 * 1024 * 20, // 20MB for caching
                cpu_overhead: 0.08,
                implementation_time: Duration::from_secs(5400), // 90 minutes
                maintenance_overhead: 0.03,
            },
        }
    }
}

/// Pool statistics aggregator
pub struct PoolStatsAggregator {
    pools: HashMap<String, ScirS2PoolInfo>,
}

impl PoolStatsAggregator {
    /// Create new pool aggregator
    pub fn new() -> Self {
        Self {
            pools: HashMap::new(),
        }
    }

    /// Add or update pool information
    pub fn update_pool(&mut self, pool: ScirS2PoolInfo) {
        self.pools.insert(pool.pool_id.clone(), pool);
    }

    /// Get pool information
    pub fn get_pool(&self, pool_id: &str) -> Option<&ScirS2PoolInfo> {
        self.pools.get(pool_id)
    }

    /// Get all pools
    pub fn get_all_pools(&self) -> &HashMap<String, ScirS2PoolInfo> {
        &self.pools
    }

    /// Calculate system-wide pool metrics
    pub fn calculate_system_metrics(&self) -> SystemPoolMetrics {
        let mut total_capacity = 0;
        let mut total_used = 0;
        let mut health_scores = Vec::new();
        let mut high_utilization_count = 0;
        let mut recommendation_count = 0;

        for pool in self.pools.values() {
            total_capacity += pool.capacity;
            total_used += pool.used_memory();
            health_scores.push(pool.health_assessment.health_score);
            recommendation_count += pool.optimization_recommendations.len();

            if pool.utilization > 0.8 {
                high_utilization_count += 1;
            }
        }

        let system_utilization = if total_capacity > 0 {
            total_used as f64 / total_capacity as f64
        } else {
            0.0
        };

        let average_health = if !health_scores.is_empty() {
            health_scores.iter().sum::<f64>() / health_scores.len() as f64
        } else {
            0.0
        };

        SystemPoolMetrics {
            total_pools: self.pools.len(),
            total_capacity,
            total_used,
            system_utilization,
            average_health_score: average_health,
            high_utilization_pools: high_utilization_count,
            total_recommendations: recommendation_count,
        }
    }
}

/// System-wide pool metrics
#[derive(Debug, Clone)]
pub struct SystemPoolMetrics {
    pub total_pools: usize,
    pub total_capacity: usize,
    pub total_used: usize,
    pub system_utilization: f64,
    pub average_health_score: f64,
    pub high_utilization_pools: usize,
    pub total_recommendations: usize,
}