nerve/cli/commands/
memory.rs

1//! Memory Command
2//!
3//! Provides memory buffer diagnostics and monitoring capabilities.
4
5use clap::Args;
6use crate::cli::commands::CliCommand;
7use crate::cli::{CliConfig, CliError};
8use crate::integration::core::NerveSystem;
9
10/// Memory command arguments
11#[derive(Args, Debug)]
12pub struct MemoryCommand {
13    /// Show buffer utilization details
14    #[arg(short, long)]
15    pub utilization: bool,
16
17    /// Show QoS policy analysis
18    #[arg(short, long)]
19    pub qos: bool,
20
21    /// Show message age tracking
22    #[arg(short, long)]
23    pub age: bool,
24
25    /// Show memory pool statistics
26    #[arg(short, long)]
27    pub pool: bool,
28
29    /// Show all memory information
30    #[arg(short = 'A', long)]
31    pub all: bool,
32}
33
34impl CliCommand for MemoryCommand {
35    fn execute(&self, config: &CliConfig) -> Result<(), CliError> {
36        if config.verbose {
37            println!("💾 Executing memory command...");
38        }
39
40        // Create a NerveSystem instance
41        let system = NerveSystem::new()
42            .map_err(|e| CliError::SystemError(format!("Failed to create system: {}", e)))?;
43
44        // Get memory components
45        let _memory_pool = system.memory_pool();
46        let _message_buffer = system.message_buffer();
47
48        println!("💾 Memory System Diagnostics");
49        println!("============================");
50        println!();
51
52        // Show basic memory information
53        self.show_basic_info(&system, config)?;
54
55        // Show detailed information based on flags
56        if self.utilization || self.all {
57            self.show_utilization_info(&system, config)?;
58        }
59
60        if self.qos || self.all {
61            self.show_qos_analysis(&system, config)?;
62        }
63
64        if self.age || self.all {
65            self.show_age_tracking(&system, config)?;
66        }
67
68        if self.pool || self.all {
69            self.show_pool_statistics(&system, config)?;
70        }
71
72        Ok(())
73    }
74}
75
76impl MemoryCommand {
77    /// Show basic memory information
78    fn show_basic_info(&self, system: &NerveSystem, _config: &CliConfig) -> Result<(), CliError> {
79        let memory_pool = system.memory_pool();
80        let message_buffer = system.message_buffer();
81
82        println!("📊 Basic Memory Information:");
83        println!("  Memory Pool: {} strong references", std::sync::Arc::strong_count(memory_pool));
84        println!("  Message Buffer: {} strong references", std::sync::Arc::strong_count(message_buffer));
85        println!();
86
87        // System configuration
88        let config = system.config();
89        println!("⚙️ System Configuration:");
90        println!("  Max Memory: {} bytes", config.max_memory_bytes);
91        println!("  Max Queue Size: {}", config.max_queue_size);
92        println!();
93
94        Ok(())
95    }
96
97    /// Show buffer utilization information
98    fn show_utilization_info(&self, system: &NerveSystem, _config: &CliConfig) -> Result<(), CliError> {
99        let message_buffer = system.message_buffer();
100
101        println!("📈 Buffer Utilization:");
102
103        // Use only methods available in the MessageBuffer trait
104        println!("  Current Messages: {}", message_buffer.len());
105        println!("  Buffer Capacity: {}", message_buffer.capacity());
106        println!("  Utilization: {:.1}%", (message_buffer.len() as f64 / message_buffer.capacity() as f64) * 100.0);
107        println!("  Health: {:?}", if message_buffer.is_full() { "Full" } else { "Healthy" });
108        println!();
109
110        println!("  Note: Detailed statistics not available in extracted component");
111        println!();
112
113        Ok(())
114    }
115
116    /// Show QoS policy analysis
117    fn show_qos_analysis(&self, system: &NerveSystem, _config: &CliConfig) -> Result<(), CliError> {
118        let message_buffer = system.message_buffer();
119
120        println!("🎯 QoS Policy Analysis:");
121        println!("  Note: QoS policy information not available in extracted component");
122        println!();
123
124        // Show all QoS policies with descriptions
125        println!("  Available QoS Policies:");
126        println!("    BestEffort: Messages may be dropped under load");
127        println!("    Reliable: Oldest messages dropped when full");
128        println!("    Guaranteed: Error returned when full");
129        println!("    RealTime: Oldest messages overwritten when full");
130        println!();
131
132        // Show buffer mode information
133        println!("  Buffer Modes:");
134        println!("    HighPerformance: Optimized for speed (13-16μs)");
135        println!("    FullFeatures: Complete feature set (34-35μs)");
136        println!();
137
138        // Show current buffer statistics
139        println!("  Current Buffer Statistics:");
140        println!("    Capacity: {} messages", message_buffer.capacity());
141        println!("    Current Count: {} messages", message_buffer.len());
142        println!("    Utilization: {:.1}%", (message_buffer.len() as f64 / message_buffer.capacity() as f64) * 100.0);
143        println!("    Health: {:?}", if message_buffer.is_full() { "Full" } else { "Healthy" });
144        println!();
145
146        Ok(())
147    }
148
149    /// Show message age tracking
150    fn show_age_tracking(&self, system: &NerveSystem, _config: &CliConfig) -> Result<(), CliError> {
151        let message_buffer = system.message_buffer();
152
153        println!("⏱️ Message Age Tracking:");
154
155        if message_buffer.len() == 0 {
156            println!("  No messages in buffer");
157        } else {
158            println!("  Message Count: {} messages", message_buffer.len());
159            println!("  Age tracking: Not available in extracted component");
160        }
161        println!();
162
163        Ok(())
164    }
165
166    /// Show memory pool statistics
167    fn show_pool_statistics(&self, _system: &NerveSystem, _config: &CliConfig) -> Result<(), CliError> {
168        println!("🏊 Memory Pool Statistics:");
169        println!("  Note: Memory pool statistics would show allocation patterns and utilization");
170        println!("  Current implementation provides placeholder for future enhancement");
171        println!();
172        Ok(())
173    }
174}