#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptimizationLevel {
Conservative,
Balanced,
Aggressive,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MonitoringLevel {
Basic,
Comprehensive,
Detailed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryTrackingLevel {
Basic,
Comprehensive,
Detailed,
}
pub mod config;
pub mod event_system;
pub mod integration;
pub mod monitoring;
pub mod optimization;
pub mod pool_management;
pub mod statistics;
pub use config::{
validate_config, AdvancedIntegrationConfig, AlertSeverity, ComparisonType, IntegrationStatus,
OptimizationType, PoolOptimizationRecommendation, ProfilingDetailLevel,
ScirS2IntegrationConfig,
};
pub use statistics::{
AggregateMetrics, AllocationUsageStats, AllocatorAdvancedMetrics, AllocatorPerformanceProfile,
AllocatorStatsAggregator, MemoryStateSnapshot, PerformanceSnapshot, ScirS2AllocatorStats,
};
pub use pool_management::{
PoolAdvancedAnalytics, PoolHealthAssessment, PoolStatsAggregator, ScirS2PoolInfo,
SystemPoolMetrics,
};
pub use event_system::{
AllocationEventContext, DeallocationEventContext, EventFilter, EventSeverity, EventStatistics,
MemoryPressureContext, OptimizationContext, PoolCreationContext, PoolDestructionContext,
ScirS2Event, ScirS2EventProcessor,
};
pub use monitoring::{
ActiveAlert, AlertCondition, DashboardData, MonitoringConfig, MonitoringDataPoint,
ScirS2MonitoringSystem,
};
pub use optimization::{
AdvancedScirS2Features, AnomalyDetector, AutoOptimizationSystem, DetectedAnomaly, MLModel,
OptimizationMetrics, OptimizationResult, OptimizationTask, PredictiveAllocationEngine,
ScirS2OptimizationEngine,
};
pub use integration::{AggregateStatistics, IntegrationMetrics, ScirS2Integration};
pub use config::{CleanupStatus, FragmentationType, PressureTrend, UtilizationChangeReason};
pub fn default_integration() -> ScirS2Integration {
ScirS2Integration::new(ScirS2IntegrationConfig::default())
}
pub fn production_integration() -> ScirS2Integration {
let config = ScirS2IntegrationConfig {
enable_realtime_sync: true,
enable_event_callbacks: true,
track_allocation_patterns: true,
enable_optimization_suggestions: true,
advanced_config: AdvancedIntegrationConfig {
enable_predictive_modeling: true,
enable_automated_optimization: true,
enable_health_monitoring: true,
enable_performance_profiling: true,
profiling_detail_level: ProfilingDetailLevel::Comprehensive,
optimization_aggressiveness: 0.8, ..Default::default()
},
..Default::default()
};
ScirS2Integration::new(config)
}
pub fn development_integration() -> ScirS2Integration {
use std::time::Duration;
let config = ScirS2IntegrationConfig {
enable_realtime_sync: false,
sync_interval: Duration::from_secs(30),
enable_event_callbacks: false,
track_allocation_patterns: false,
enable_optimization_suggestions: false,
advanced_config: AdvancedIntegrationConfig::conservative(),
};
ScirS2Integration::new(config)
}
pub fn validate_integration_health(integration: &ScirS2Integration) -> HealthReport {
let status = integration.get_status();
let metrics = integration.get_integration_metrics();
let suggestions = integration.get_optimization_suggestions();
let mut issues = Vec::new();
let mut warnings = Vec::new();
let mut recommendations = Vec::new();
if !integration.is_healthy() {
issues.push("Integration is not healthy".to_string());
}
if let Some(last_sync) = status.last_sync {
let sync_age = std::time::Instant::now().duration_since(last_sync);
if sync_age > integration.config.sync_interval * 2 {
warnings.push("Last synchronization is old".to_string());
}
} else {
warnings.push("No synchronization performed yet".to_string());
}
if metrics.success_rate < 0.9 {
if metrics.success_rate < 0.7 {
issues.push(format!(
"Low success rate: {:.1}%",
metrics.success_rate * 100.0
));
} else {
warnings.push(format!(
"Moderate success rate: {:.1}%",
metrics.success_rate * 100.0
));
}
}
if suggestions.len() > 10 {
recommendations
.push("High number of optimization suggestions - consider applying them".to_string());
}
for (name, stats) in integration.get_all_allocator_stats() {
if !stats.is_healthy() {
issues.push(format!("Allocator '{}' is not healthy", name));
}
if stats.memory_efficiency < 0.8 {
warnings.push(format!(
"Allocator '{}' has low efficiency: {:.1}%",
name,
stats.memory_efficiency * 100.0
));
}
}
for (id, pool) in integration.get_all_pools() {
if !pool.is_healthy() {
issues.push(format!("Pool '{}' is not healthy", id));
}
if pool.utilization > 0.9 {
warnings.push(format!(
"Pool '{}' has high utilization: {:.1}%",
id,
pool.utilization * 100.0
));
}
}
let overall_status = if !issues.is_empty() {
OverallHealthStatus::Unhealthy
} else if !warnings.is_empty() {
OverallHealthStatus::Warning
} else {
OverallHealthStatus::Healthy
};
HealthReport {
overall_status,
issues,
warnings,
recommendations,
health_score: status.health_score,
uptime: metrics.uptime,
total_events_processed: metrics.total_events_processed,
success_rate: metrics.success_rate,
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum OverallHealthStatus {
Healthy,
Warning,
Unhealthy,
}
#[derive(Debug, Clone)]
pub struct HealthReport {
pub overall_status: OverallHealthStatus,
pub issues: Vec<String>,
pub warnings: Vec<String>,
pub recommendations: Vec<String>,
pub health_score: f64,
pub uptime: std::time::Duration,
pub total_events_processed: u64,
pub success_rate: f64,
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn test_default_integration_creation() {
let integration = default_integration();
assert!(!integration.active);
assert_eq!(integration.allocator_stats.read().len(), 0);
assert_eq!(integration.pool_info.read().len(), 0);
}
#[test]
fn test_production_integration_creation() {
let integration = production_integration();
assert!(!integration.active);
assert!(integration.config.enable_realtime_sync);
assert!(integration.config.enable_optimization_suggestions);
}
#[test]
fn test_development_integration_creation() {
let integration = development_integration();
assert!(!integration.active);
assert!(!integration.config.enable_realtime_sync);
assert!(!integration.config.enable_optimization_suggestions);
}
#[test]
fn test_integration_activation() {
let mut integration = default_integration();
let result = integration.activate();
assert!(result.is_ok());
assert!(integration.active);
}
#[test]
fn test_integration_configuration_validation() {
let invalid_config = ScirS2IntegrationConfig {
sync_interval: Duration::from_secs(0),
..Default::default()
};
let result = validate_config(&invalid_config);
assert!(result.is_err());
}
#[test]
fn test_event_processing() {
let mut integration = default_integration();
let _ = integration.activate();
let event = ScirS2Event::Allocation {
ptr: std::ptr::null_mut(),
size: 1024,
allocator: "test_allocator".to_string(),
allocation_context: AllocationEventContext {
thread_id: 1,
reason: "test allocation".to_string(),
performance_snapshot: PerformanceSnapshot::new(),
memory_snapshot: MemoryStateSnapshot::new(),
},
};
integration.process_event(event);
let metrics = integration.get_integration_metrics();
assert!(metrics.total_events_processed > 0);
}
#[test]
fn test_allocator_statistics_tracking() {
let mut integration = default_integration();
let _ = integration.activate();
for i in 0..5 {
let event = ScirS2Event::Allocation {
ptr: std::ptr::null_mut(),
size: 1024 * (i + 1),
allocator: "test_allocator".to_string(),
allocation_context: AllocationEventContext {
thread_id: 1,
reason: format!("test allocation {}", i),
performance_snapshot: PerformanceSnapshot::new(),
memory_snapshot: MemoryStateSnapshot::new(),
},
};
integration.process_event(event);
}
let stats = integration.get_allocator_stats("test_allocator");
assert!(stats.is_some());
}
#[test]
fn test_optimization_suggestions() {
let mut integration = ScirS2Integration::new(ScirS2IntegrationConfig {
enable_optimization_suggestions: true,
..Default::default()
});
let _ = integration.activate();
let mut poor_stats = ScirS2AllocatorStats::new("inefficient_allocator".to_string());
poor_stats.memory_efficiency = 0.5; poor_stats.allocation_failures = 5;
integration
.allocator_stats
.write()
.insert("inefficient_allocator".to_string(), poor_stats);
let suggestions = integration.get_optimization_suggestions();
assert!(!suggestions.is_empty());
let efficiency_suggestion = suggestions
.iter()
.any(|s| s.description.contains("efficiency"));
let failure_suggestion = suggestions
.iter()
.any(|s| s.description.contains("failures"));
assert!(efficiency_suggestion);
assert!(failure_suggestion);
}
#[test]
fn test_health_validation() {
let integration = default_integration();
let health_report = validate_integration_health(&integration);
match health_report.overall_status {
OverallHealthStatus::Healthy | OverallHealthStatus::Warning => {}
OverallHealthStatus::Unhealthy => panic!("Fresh integration should not be unhealthy"),
}
assert!(health_report.health_score >= 0.0);
assert!(health_report.health_score <= 1.0);
}
#[test]
fn test_synchronization() {
let mut integration = default_integration();
let _ = integration.activate();
let result = integration.sync_statistics();
assert!(result.is_ok());
assert!(integration.get_status().last_sync.is_some());
}
#[test]
fn test_pool_management() {
let mut integration = default_integration();
let _ = integration.activate();
let _ = integration.sync_statistics();
let pools = integration.get_all_pools();
assert!(!pools.is_empty());
assert!(pools.contains_key("tensor_pool_1"));
}
#[test]
fn test_integration_deactivation() {
let mut integration = default_integration();
let _ = integration.activate();
assert!(integration.active);
integration.deactivate();
assert!(!integration.active);
assert!(integration.get_status().last_sync.is_none());
}
#[test]
fn test_configuration_updates() {
let mut integration = default_integration();
let _ = integration.activate();
let new_config = ScirS2IntegrationConfig {
enable_optimization_suggestions: true,
enable_realtime_sync: true, ..integration.config.clone()
};
let result = integration.update_config(new_config.clone());
assert!(result.is_ok());
assert!(integration.config.enable_optimization_suggestions);
}
#[test]
fn test_buffer_flushing() {
let mut integration = default_integration();
let _ = integration.activate();
for i in 0..10 {
let event = ScirS2Event::Allocation {
ptr: std::ptr::null_mut(),
size: 1024,
allocator: format!("allocator_{}", i % 3),
allocation_context: AllocationEventContext {
thread_id: 1,
reason: "Test allocation".to_string(),
performance_snapshot: PerformanceSnapshot::new(),
memory_snapshot: MemoryStateSnapshot::new(),
},
};
integration.process_event(event);
}
integration.flush_all();
assert!(integration.active);
}
#[test]
fn test_aggregate_statistics() {
let mut integration = default_integration();
let _ = integration.activate();
let event = ScirS2Event::Allocation {
ptr: std::ptr::null_mut(),
size: 1024,
allocator: "test_allocator".to_string(),
allocation_context: AllocationEventContext {
thread_id: 1,
reason: "Test allocation".to_string(),
performance_snapshot: PerformanceSnapshot::new(),
memory_snapshot: MemoryStateSnapshot::new(),
},
};
integration.process_event(event);
let _ = integration.sync_statistics();
let aggregate_stats = integration.get_aggregate_statistics();
assert!(aggregate_stats.monitoring_active);
let _ = aggregate_stats.total_optimization_suggestions;
}
#[test]
fn test_performance_metrics() {
let mut integration = production_integration();
let _ = integration.activate();
for i in 0..20 {
let event = ScirS2Event::Allocation {
ptr: std::ptr::null_mut(),
size: 1024 * (i + 1),
allocator: "performance_test".to_string(),
allocation_context: AllocationEventContext {
thread_id: 1,
reason: "Test allocation".to_string(),
performance_snapshot: PerformanceSnapshot::new(),
memory_snapshot: MemoryStateSnapshot::new(),
},
};
integration.process_event(event);
}
let _ = integration.sync_statistics();
let metrics = integration.get_integration_metrics();
assert!(metrics.total_events_processed >= 20);
assert!(metrics.success_rate >= 0.0);
assert!(metrics.success_rate <= 1.0);
}
#[test]
fn test_comprehensive_integration_workflow() {
let mut integration = production_integration();
let activate_result = integration.activate();
assert!(activate_result.is_ok());
assert!(integration.active);
let events = vec![
ScirS2Event::Allocation {
ptr: std::ptr::null_mut(),
size: 2048,
allocator: "tensor_allocator".to_string(),
allocation_context: AllocationEventContext {
thread_id: 1,
reason: "Test allocation".to_string(),
performance_snapshot: PerformanceSnapshot::new(),
memory_snapshot: MemoryStateSnapshot::new(),
},
},
ScirS2Event::PoolCreated {
pool_id: "custom_pool".to_string(),
capacity: 1024 * 1024,
pool_type: "custom".to_string(),
creation_context: PoolCreationContext {
creation_reason: "test pool".to_string(),
initial_config: std::collections::HashMap::new(),
expected_usage: "testing".to_string(),
},
},
ScirS2Event::MemoryPressure {
level: crate::memory_profiler::allocation::PressureLevel::Medium,
available_memory: 1024 * 1024 * 100,
pressure_context: MemoryPressureContext {
system_memory_usage: 1024 * 1024 * 500,
scirs2_memory_usage: 1024 * 1024 * 50,
active_allocators: vec!["tensor_allocator".to_string()],
pressure_trend: PressureTrend::Increasing,
},
},
];
for event in events {
integration.process_event(event);
}
let sync_result = integration.sync_statistics();
assert!(sync_result.is_ok());
let _suggestions = integration.get_optimization_suggestions();
let health_report = validate_integration_health(&integration);
assert!(health_report.health_score >= 0.0);
let metrics = integration.get_integration_metrics();
assert!(metrics.total_events_processed >= 3);
let _dashboard = integration.get_dashboard_data();
let new_config = ScirS2IntegrationConfig {
enable_realtime_sync: false, ..integration.config.clone()
};
let config_result = integration.update_config(new_config);
assert!(config_result.is_ok());
integration.flush_all();
integration.deactivate();
assert!(!integration.active);
}
}