mabi-core 1.6.3

Mabinogion - Core abstractions and utilities for industrial protocol simulator
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
//! Memory and performance profiling module.
//!
//! This module provides comprehensive profiling capabilities for the TRAP simulator,
//! including memory usage tracking, allocation analysis, and performance monitoring.
//!
//! # Features
//!
//! - **Memory Profiling**: Track heap allocations, memory regions, and usage patterns
//! - **Leak Detection**: Identify potential memory leaks through allocation tracking
//! - **Performance Profiling**: Measure operation latencies and throughput
//! - **Report Generation**: Generate detailed profiling reports
//!
//! # Example
//!
//! ```rust,ignore
//! use mabi_core::profiling::{MemoryProfiler, ProfilerConfig};
//!
//! let config = ProfilerConfig::default();
//! let profiler = MemoryProfiler::new(config);
//!
//! // Start profiling
//! profiler.start();
//!
//! // ... run your workload ...
//!
//! // Get a snapshot
//! let snapshot = profiler.snapshot();
//! println!("Current memory: {} bytes", snapshot.current_bytes);
//!
//! // Generate report
//! let report = profiler.generate_report();
//! ```

pub mod leak_detector;
pub mod memory;
pub mod report;

pub use leak_detector::*;
pub use memory::*;
pub use report::*;

use std::sync::Arc;
use std::time::Duration;

use parking_lot::RwLock;
use serde::{Deserialize, Serialize};

/// Profiler configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfilerConfig {
    /// Enable memory profiling.
    #[serde(default = "default_true")]
    pub memory_enabled: bool,

    /// Enable leak detection.
    #[serde(default = "default_true")]
    pub leak_detection_enabled: bool,

    /// Sampling interval for periodic snapshots.
    #[serde(default = "default_sampling_interval")]
    pub sampling_interval: Duration,

    /// Maximum number of snapshots to retain.
    #[serde(default = "default_max_snapshots")]
    pub max_snapshots: usize,

    /// Allocation size threshold for tracking (bytes).
    /// Allocations smaller than this are not individually tracked.
    #[serde(default = "default_allocation_threshold")]
    pub allocation_threshold: usize,

    /// Enable stack trace capture for allocations (expensive).
    #[serde(default)]
    pub capture_stack_traces: bool,

    /// Maximum stack trace depth.
    #[serde(default = "default_stack_depth")]
    pub max_stack_depth: usize,
}

fn default_true() -> bool {
    true
}

fn default_sampling_interval() -> Duration {
    Duration::from_secs(10)
}

fn default_max_snapshots() -> usize {
    1000
}

fn default_allocation_threshold() -> usize {
    1024 // 1KB
}

fn default_stack_depth() -> usize {
    16
}

impl Default for ProfilerConfig {
    fn default() -> Self {
        Self {
            memory_enabled: true,
            leak_detection_enabled: true,
            sampling_interval: default_sampling_interval(),
            max_snapshots: default_max_snapshots(),
            allocation_threshold: default_allocation_threshold(),
            capture_stack_traces: false,
            max_stack_depth: default_stack_depth(),
        }
    }
}

/// Combined profiler that manages all profiling subsystems.
pub struct Profiler {
    config: ProfilerConfig,
    memory_profiler: Arc<RwLock<MemoryProfiler>>,
    leak_detector: Arc<RwLock<LeakDetector>>,
    started: Arc<std::sync::atomic::AtomicBool>,
}

impl Profiler {
    /// Create a new profiler with the given configuration.
    pub fn new(config: ProfilerConfig) -> Self {
        let memory_config = MemoryProfilerConfig {
            sampling_interval: config.sampling_interval,
            max_snapshots: config.max_snapshots,
            track_allocations: config.memory_enabled,
            allocation_threshold: config.allocation_threshold,
        };

        let leak_config = LeakDetectorConfig {
            enabled: config.leak_detection_enabled,
            check_interval: config.sampling_interval * 6, // Check every minute by default
            growth_threshold_percent: 10.0,
            min_samples_for_detection: 6,
        };

        Self {
            config,
            memory_profiler: Arc::new(RwLock::new(MemoryProfiler::new(memory_config))),
            leak_detector: Arc::new(RwLock::new(LeakDetector::new(leak_config))),
            started: Arc::new(std::sync::atomic::AtomicBool::new(false)),
        }
    }

    /// Create a profiler with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(ProfilerConfig::default())
    }

    /// Get the profiler configuration.
    pub fn config(&self) -> &ProfilerConfig {
        &self.config
    }

    /// Start profiling.
    pub fn start(&self) {
        self.started
            .store(true, std::sync::atomic::Ordering::SeqCst);
        self.memory_profiler.write().start();
        self.leak_detector.write().start();
    }

    /// Stop profiling.
    pub fn stop(&self) {
        self.started
            .store(false, std::sync::atomic::Ordering::SeqCst);
        self.memory_profiler.write().stop();
        self.leak_detector.write().stop();
    }

    /// Check if profiling is active.
    pub fn is_running(&self) -> bool {
        self.started.load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Record a memory allocation.
    pub fn record_allocation(&self, label: &str, size: usize) {
        if self.is_running() {
            self.memory_profiler.write().record_allocation(label, size);
            self.leak_detector.write().record_allocation(label, size);
        }
    }

    /// Record a memory deallocation.
    pub fn record_deallocation(&self, label: &str, size: usize) {
        if self.is_running() {
            self.memory_profiler
                .write()
                .record_deallocation(label, size);
            self.leak_detector.write().record_deallocation(label, size);
        }
    }

    /// Take a memory snapshot.
    pub fn snapshot(&self) -> MemorySnapshot {
        self.memory_profiler.read().snapshot()
    }

    /// Check for potential memory leaks.
    pub fn check_leaks(&self) -> Vec<LeakWarning> {
        self.leak_detector.read().check()
    }

    /// Generate a comprehensive profiling report.
    pub fn generate_report(&self) -> ProfileReport {
        let memory_report = self.memory_profiler.read().generate_report();
        let leak_warnings = self.check_leaks();

        ProfileReport {
            generated_at: chrono::Utc::now(),
            config: self.config.clone(),
            memory: memory_report,
            leak_warnings,
            is_running: self.is_running(),
        }
    }

    /// Get the memory profiler.
    pub fn memory_profiler(&self) -> Arc<RwLock<MemoryProfiler>> {
        Arc::clone(&self.memory_profiler)
    }

    /// Get the leak detector.
    pub fn leak_detector(&self) -> Arc<RwLock<LeakDetector>> {
        Arc::clone(&self.leak_detector)
    }

    /// Reset all profiling data.
    pub fn reset(&self) {
        self.memory_profiler.write().reset();
        self.leak_detector.write().reset();
    }
}

impl std::fmt::Debug for Profiler {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Profiler")
            .field("config", &self.config)
            .field("is_running", &self.is_running())
            .finish()
    }
}

/// Comprehensive profiling report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileReport {
    /// When the report was generated.
    pub generated_at: chrono::DateTime<chrono::Utc>,

    /// Profiler configuration used.
    pub config: ProfilerConfig,

    /// Memory profiling report.
    pub memory: MemoryReport,

    /// Detected leak warnings.
    pub leak_warnings: Vec<LeakWarning>,

    /// Whether profiling is currently running.
    pub is_running: bool,
}

impl ProfileReport {
    /// Check if there are any leak warnings.
    pub fn has_leak_warnings(&self) -> bool {
        !self.leak_warnings.is_empty()
    }

    /// Get the current memory usage in bytes.
    pub fn current_memory_bytes(&self) -> u64 {
        self.memory.current_bytes
    }

    /// Get the peak memory usage in bytes.
    pub fn peak_memory_bytes(&self) -> u64 {
        self.memory.peak_bytes
    }

    /// Export report as JSON.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Export report as a human-readable string.
    pub fn to_summary(&self) -> String {
        let mut summary = String::new();
        summary.push_str("=== TRAP Simulator Profiling Report ===\n\n");

        summary.push_str(&format!(
            "Generated: {}\n",
            self.generated_at.format("%Y-%m-%d %H:%M:%S UTC")
        ));
        summary.push_str(&format!(
            "Status: {}\n",
            if self.is_running {
                "Running"
            } else {
                "Stopped"
            }
        ));

        summary.push_str("\n--- Memory Usage ---\n");
        summary.push_str(&format!(
            "Current: {} MB\n",
            self.memory.current_bytes / 1024 / 1024
        ));
        summary.push_str(&format!(
            "Peak: {} MB\n",
            self.memory.peak_bytes / 1024 / 1024
        ));
        summary.push_str(&format!(
            "Total Allocated: {} MB\n",
            self.memory.total_allocated / 1024 / 1024
        ));
        summary.push_str(&format!(
            "Total Deallocated: {} MB\n",
            self.memory.total_deallocated / 1024 / 1024
        ));
        summary.push_str(&format!(
            "Allocation Count: {}\n",
            self.memory.allocation_count
        ));

        if !self.memory.regions.is_empty() {
            summary.push_str("\n--- Memory Regions ---\n");
            for (name, region) in &self.memory.regions {
                summary.push_str(&format!(
                    "  {}: {} KB (peak: {} KB, allocs: {})\n",
                    name,
                    region.current_bytes / 1024,
                    region.peak_bytes / 1024,
                    region.allocation_count
                ));
            }
        }

        if !self.leak_warnings.is_empty() {
            summary.push_str("\n--- Leak Warnings ---\n");
            for warning in &self.leak_warnings {
                summary.push_str(&format!(
                    "  [{}] {}: {} (growth: {:.1}%/min)\n",
                    warning.severity,
                    warning.region,
                    warning.message,
                    warning.growth_rate_per_minute
                ));
            }
        } else {
            summary.push_str("\n--- No leak warnings detected ---\n");
        }

        summary
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_profiler_config_default() {
        let config = ProfilerConfig::default();
        assert!(config.memory_enabled);
        assert!(config.leak_detection_enabled);
        assert_eq!(config.sampling_interval, Duration::from_secs(10));
    }

    #[test]
    fn test_profiler_lifecycle() {
        let profiler = Profiler::with_defaults();
        assert!(!profiler.is_running());

        profiler.start();
        assert!(profiler.is_running());

        profiler.stop();
        assert!(!profiler.is_running());
    }

    #[test]
    fn test_profiler_record_allocation() {
        let profiler = Profiler::with_defaults();
        profiler.start();

        profiler.record_allocation("test_region", 1024);
        profiler.record_allocation("test_region", 2048);

        let snapshot = profiler.snapshot();
        assert!(snapshot.current_bytes >= 3072);

        profiler.record_deallocation("test_region", 1024);

        let snapshot = profiler.snapshot();
        assert!(snapshot.current_bytes >= 2048);
    }

    #[test]
    fn test_profiler_report() {
        let profiler = Profiler::with_defaults();
        profiler.start();

        profiler.record_allocation("devices", 1024 * 1024);
        profiler.record_allocation("registers", 512 * 1024);

        let report = profiler.generate_report();
        assert!(report.is_running);
        assert!(!report.memory.regions.is_empty());

        let summary = report.to_summary();
        assert!(summary.contains("Memory Usage"));
    }

    #[test]
    fn test_profiler_reset() {
        let profiler = Profiler::with_defaults();
        profiler.start();

        profiler.record_allocation("test", 1024);
        profiler.reset();

        let snapshot = profiler.snapshot();
        assert_eq!(snapshot.current_bytes, 0);
    }
}