use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use super::allocation::AllocationInfo;
use super::scope::ScopeLifecycleMetrics;
#[derive(Debug, Clone, Default, Serialize)]
pub struct MemoryStats {
pub total_allocations: usize,
pub total_allocated: usize,
pub active_allocations: usize,
pub active_memory: usize,
pub peak_allocations: usize,
pub peak_memory: usize,
pub total_deallocations: usize,
pub total_deallocated: usize,
pub leaked_allocations: usize,
pub leaked_memory: usize,
pub fragmentation_analysis: FragmentationAnalysis,
pub lifecycle_stats: ScopeLifecycleMetrics,
pub allocations: Vec<AllocationInfo>,
pub system_library_stats: SystemLibraryStats,
pub concurrency_analysis: ConcurrencyAnalysis,
}
impl MemoryStats {
pub fn new() -> Self {
Self {
total_allocations: 0,
total_allocated: 0,
active_allocations: 0,
active_memory: 0,
peak_allocations: 0,
peak_memory: 0,
total_deallocations: 0,
total_deallocated: 0,
leaked_allocations: 0,
leaked_memory: 0,
fragmentation_analysis: FragmentationAnalysis::default(),
lifecycle_stats: ScopeLifecycleMetrics::default(),
allocations: Vec::new(),
system_library_stats: SystemLibraryStats::default(),
concurrency_analysis: ConcurrencyAnalysis::default(),
}
}
}
impl From<crate::core::types::MemoryStats> for MemoryStats {
fn from(old: crate::core::types::MemoryStats) -> Self {
Self {
total_allocations: old.total_allocations,
total_allocated: old.total_allocated,
active_allocations: old.active_allocations,
active_memory: old.active_memory,
peak_allocations: old.peak_allocations,
peak_memory: old.peak_memory,
total_deallocations: old.total_deallocations,
total_deallocated: old.total_deallocated,
leaked_allocations: old.leaked_allocations,
leaked_memory: old.leaked_memory,
fragmentation_analysis: FragmentationAnalysis {
fragmentation_ratio: old.fragmentation_analysis.fragmentation_ratio,
largest_free_block: old.fragmentation_analysis.largest_free_block,
smallest_free_block: old.fragmentation_analysis.smallest_free_block,
free_block_count: old.fragmentation_analysis.free_block_count,
total_free_memory: old.fragmentation_analysis.total_free_memory,
external_fragmentation: old.fragmentation_analysis.external_fragmentation,
internal_fragmentation: old.fragmentation_analysis.internal_fragmentation,
},
lifecycle_stats: ScopeLifecycleMetrics::default(),
allocations: old.allocations.into_iter().map(|a| a.into()).collect(),
system_library_stats: SystemLibraryStats::default(),
concurrency_analysis: ConcurrencyAnalysis::default(),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct MemoryTypeInfo {
pub type_name: String,
pub total_size: usize,
pub allocation_count: usize,
pub average_size: usize,
pub largest_allocation: usize,
pub smallest_allocation: usize,
pub active_instances: usize,
pub leaked_instances: usize,
}
#[derive(Debug, Clone, Serialize)]
pub struct TypeMemoryUsage {
pub type_name: String,
pub total_size: usize,
pub allocation_count: usize,
pub average_size: f64,
pub peak_size: usize,
pub current_size: usize,
pub efficiency_score: f64,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FragmentationAnalysis {
pub fragmentation_ratio: f64,
pub largest_free_block: usize,
pub smallest_free_block: usize,
pub free_block_count: usize,
pub total_free_memory: usize,
pub external_fragmentation: f64,
pub internal_fragmentation: f64,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct SystemLibraryStats {
pub std_collections: LibraryUsage,
pub async_runtime: LibraryUsage,
pub network_io: LibraryUsage,
pub file_system: LibraryUsage,
pub serialization: LibraryUsage,
pub regex_engine: LibraryUsage,
pub crypto_security: LibraryUsage,
pub database: LibraryUsage,
pub graphics_ui: LibraryUsage,
pub http_stack: LibraryUsage,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct LibraryUsage {
pub allocation_count: usize,
pub total_bytes: usize,
pub peak_bytes: usize,
pub average_size: f64,
pub categories: HashMap<String, usize>,
pub hotspot_functions: Vec<String>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ConcurrencyAnalysis {
pub thread_safety_allocations: usize,
pub shared_memory_bytes: usize,
pub mutex_protected: usize,
pub arc_shared: usize,
pub rc_shared: usize,
pub channel_buffers: usize,
pub thread_local_storage: usize,
pub atomic_operations: usize,
pub lock_contention_risk: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memory_stats_creation() {
let stats = MemoryStats::new();
assert_eq!(stats.total_allocations, 0);
assert_eq!(stats.total_allocated, 0);
assert_eq!(stats.active_allocations, 0);
}
#[test]
fn test_fragmentation_analysis_default() {
let frag = FragmentationAnalysis::default();
assert_eq!(frag.fragmentation_ratio, 0.0);
assert_eq!(frag.largest_free_block, 0);
assert_eq!(frag.free_block_count, 0);
}
#[test]
fn test_system_library_stats_default() {
let stats = SystemLibraryStats::default();
assert_eq!(stats.std_collections.allocation_count, 0);
assert_eq!(stats.async_runtime.total_bytes, 0);
assert_eq!(stats.network_io.peak_bytes, 0);
}
#[test]
fn test_library_usage_default() {
let usage = LibraryUsage::default();
assert_eq!(usage.allocation_count, 0);
assert_eq!(usage.total_bytes, 0);
assert_eq!(usage.average_size, 0.0);
assert!(usage.categories.is_empty());
assert!(usage.hotspot_functions.is_empty());
}
#[test]
fn test_concurrency_analysis_default() {
let analysis = ConcurrencyAnalysis::default();
assert_eq!(analysis.thread_safety_allocations, 0);
assert_eq!(analysis.shared_memory_bytes, 0);
assert_eq!(analysis.mutex_protected, 0);
}
}