ipfrs-network 0.2.0

Peer-to-peer networking layer with libp2p and QUIC for IPFRS
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
//! ARM Performance Profiling for Network Operations
//!
//! This module provides performance profiling utilities specifically designed
//! for ARM devices including Raspberry Pi, Jetson, and other embedded platforms.
//!
//! ## Features
//!
//! - CPU usage tracking
//! - Memory usage monitoring
//! - Network throughput measurement
//! - Latency profiling
//! - Battery/power consumption estimation
//! - Thermal monitoring (on supported devices)
//!
//! ## Use Cases
//!
//! - Performance optimization for ARM devices
//! - Identifying bottlenecks on resource-constrained devices
//! - Regression testing across ARM platforms
//! - Power consumption analysis

use parking_lot::RwLock;
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;

/// Errors that can occur during profiling
#[derive(Debug, Error)]
pub enum ProfilerError {
    #[error("Profiler not started")]
    NotStarted,

    #[error("System information unavailable")]
    SystemInfoUnavailable,

    #[error("Insufficient samples for analysis")]
    InsufficientSamples,
}

/// ARM device profile
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ArmDevice {
    /// Raspberry Pi (various models)
    RaspberryPi,
    /// NVIDIA Jetson (Nano, TX2, Xavier, etc.)
    Jetson,
    /// Generic ARM device
    Generic,
    /// Unknown device
    Unknown,
}

impl ArmDevice {
    /// Detect ARM device type from system information
    pub fn detect() -> Self {
        // In a real implementation, read /proc/cpuinfo or device tree
        #[cfg(target_arch = "aarch64")]
        {
            Self::Generic
        }
        #[cfg(target_arch = "arm")]
        {
            Self::RaspberryPi
        }
        #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))]
        {
            Self::Unknown
        }
    }

    /// Get recommended configuration for this device
    pub fn recommended_config(&self) -> ProfilerConfig {
        match self {
            ArmDevice::RaspberryPi => ProfilerConfig::raspberry_pi(),
            ArmDevice::Jetson => ProfilerConfig::jetson(),
            ArmDevice::Generic => ProfilerConfig::default(),
            ArmDevice::Unknown => ProfilerConfig::default(),
        }
    }
}

/// Performance profiling configuration
#[derive(Debug, Clone)]
pub struct ProfilerConfig {
    /// Enable CPU usage tracking
    pub track_cpu: bool,

    /// Enable memory usage tracking
    pub track_memory: bool,

    /// Enable network throughput tracking
    pub track_throughput: bool,

    /// Enable latency profiling
    pub track_latency: bool,

    /// Sample interval for metrics
    pub sample_interval: Duration,

    /// Maximum number of samples to keep
    pub max_samples: usize,

    /// Enable thermal monitoring (if supported)
    pub track_thermal: bool,
}

impl Default for ProfilerConfig {
    fn default() -> Self {
        Self {
            track_cpu: true,
            track_memory: true,
            track_throughput: true,
            track_latency: true,
            sample_interval: Duration::from_secs(1),
            max_samples: 1000,
            track_thermal: false,
        }
    }
}

impl ProfilerConfig {
    /// Configuration optimized for Raspberry Pi
    pub fn raspberry_pi() -> Self {
        Self {
            track_cpu: true,
            track_memory: true,
            track_throughput: true,
            track_latency: true,
            sample_interval: Duration::from_secs(2),
            max_samples: 500,
            track_thermal: true, // RPi has temp sensor
        }
    }

    /// Configuration optimized for NVIDIA Jetson
    pub fn jetson() -> Self {
        Self {
            track_cpu: true,
            track_memory: true,
            track_throughput: true,
            track_latency: true,
            sample_interval: Duration::from_millis(500),
            max_samples: 2000, // Jetson has more resources
            track_thermal: true,
        }
    }
}

/// Performance sample
#[derive(Debug, Clone)]
pub struct PerformanceSample {
    /// Timestamp of the sample
    pub timestamp: Instant,
    /// CPU usage percentage (0.0-100.0)
    pub cpu_usage: Option<f64>,
    /// Memory usage in bytes
    pub memory_usage: Option<u64>,
    /// Network throughput (bytes/sec)
    pub throughput: Option<u64>,
    /// Average latency in microseconds
    pub latency_us: Option<u64>,
    /// Temperature in Celsius (if available)
    pub temperature: Option<f32>,
}

/// Performance statistics
#[derive(Debug, Clone)]
pub struct PerformanceStats {
    /// Average CPU usage
    pub avg_cpu: f64,
    /// Peak CPU usage
    pub peak_cpu: f64,
    /// Average memory usage (bytes)
    pub avg_memory: u64,
    /// Peak memory usage (bytes)
    pub peak_memory: u64,
    /// Average throughput (bytes/sec)
    pub avg_throughput: u64,
    /// Peak throughput (bytes/sec)
    pub peak_throughput: u64,
    /// Average latency (microseconds)
    pub avg_latency: u64,
    /// 95th percentile latency (microseconds)
    pub p95_latency: u64,
    /// 99th percentile latency (microseconds)
    pub p99_latency: u64,
    /// Average temperature (Celsius)
    pub avg_temperature: Option<f32>,
    /// Peak temperature (Celsius)
    pub peak_temperature: Option<f32>,
    /// Number of samples
    pub sample_count: usize,
    /// Profiling duration
    pub duration: Duration,
}

/// ARM performance profiler
pub struct ArmProfiler {
    /// Configuration
    config: ProfilerConfig,
    /// Device type
    device: ArmDevice,
    /// Performance samples
    samples: Arc<RwLock<VecDeque<PerformanceSample>>>,
    /// Start time
    start_time: Option<Instant>,
    /// Last sample time
    last_sample: Arc<RwLock<Option<Instant>>>,
}

impl ArmProfiler {
    /// Create a new ARM profiler
    pub fn new(config: ProfilerConfig) -> Self {
        let device = ArmDevice::detect();
        Self {
            config,
            device,
            samples: Arc::new(RwLock::new(VecDeque::new())),
            start_time: None,
            last_sample: Arc::new(RwLock::new(None)),
        }
    }

    /// Create with auto-detected device configuration
    pub fn auto_detect() -> Self {
        let device = ArmDevice::detect();
        let config = device.recommended_config();
        Self::new(config)
    }

    /// Start profiling
    pub fn start(&mut self) {
        self.start_time = Some(Instant::now());
        *self.last_sample.write() = Some(Instant::now());
    }

    /// Stop profiling
    pub fn stop(&mut self) {
        self.start_time = None;
    }

    /// Record a performance sample
    pub fn record_sample(&self, sample: PerformanceSample) {
        let mut samples = self.samples.write();

        // Add new sample
        samples.push_back(sample);

        // Limit sample count
        while samples.len() > self.config.max_samples {
            samples.pop_front();
        }

        // Update last sample time
        *self.last_sample.write() = Some(Instant::now());
    }

    /// Record CPU usage
    pub fn record_cpu(&self, cpu_usage: f64) {
        if !self.config.track_cpu {
            return;
        }

        let sample = PerformanceSample {
            timestamp: Instant::now(),
            cpu_usage: Some(cpu_usage),
            memory_usage: None,
            throughput: None,
            latency_us: None,
            temperature: None,
        };

        self.record_sample(sample);
    }

    /// Record memory usage
    pub fn record_memory(&self, memory_bytes: u64) {
        if !self.config.track_memory {
            return;
        }

        let sample = PerformanceSample {
            timestamp: Instant::now(),
            cpu_usage: None,
            memory_usage: Some(memory_bytes),
            throughput: None,
            latency_us: None,
            temperature: None,
        };

        self.record_sample(sample);
    }

    /// Record network throughput
    pub fn record_throughput(&self, bytes_per_sec: u64) {
        if !self.config.track_throughput {
            return;
        }

        let sample = PerformanceSample {
            timestamp: Instant::now(),
            cpu_usage: None,
            memory_usage: None,
            throughput: Some(bytes_per_sec),
            latency_us: None,
            temperature: None,
        };

        self.record_sample(sample);
    }

    /// Record latency
    pub fn record_latency(&self, latency: Duration) {
        if !self.config.track_latency {
            return;
        }

        let sample = PerformanceSample {
            timestamp: Instant::now(),
            cpu_usage: None,
            memory_usage: None,
            throughput: None,
            latency_us: Some(latency.as_micros() as u64),
            temperature: None,
        };

        self.record_sample(sample);
    }

    /// Get performance statistics
    pub fn stats(&self) -> Result<PerformanceStats, ProfilerError> {
        let samples = self.samples.read();

        if samples.is_empty() {
            return Err(ProfilerError::InsufficientSamples);
        }

        let duration = self
            .start_time
            .map(|start| start.elapsed())
            .unwrap_or_default();

        // Calculate CPU stats
        let cpu_values: Vec<f64> = samples.iter().filter_map(|s| s.cpu_usage).collect();

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

        let peak_cpu = cpu_values.iter().cloned().fold(0.0f64, |a, b| a.max(b));

        // Calculate memory stats
        let memory_values: Vec<u64> = samples.iter().filter_map(|s| s.memory_usage).collect();

        let avg_memory = if !memory_values.is_empty() {
            memory_values.iter().sum::<u64>() / memory_values.len() as u64
        } else {
            0
        };

        let peak_memory = memory_values.iter().cloned().max().unwrap_or(0);

        // Calculate throughput stats
        let throughput_values: Vec<u64> = samples.iter().filter_map(|s| s.throughput).collect();

        let avg_throughput = if !throughput_values.is_empty() {
            throughput_values.iter().sum::<u64>() / throughput_values.len() as u64
        } else {
            0
        };

        let peak_throughput = throughput_values.iter().cloned().max().unwrap_or(0);

        // Calculate latency stats
        let mut latency_values: Vec<u64> = samples.iter().filter_map(|s| s.latency_us).collect();

        latency_values.sort_unstable();

        let avg_latency = if !latency_values.is_empty() {
            latency_values.iter().sum::<u64>() / latency_values.len() as u64
        } else {
            0
        };

        let p95_latency = if !latency_values.is_empty() {
            let idx = (latency_values.len() as f64 * 0.95) as usize;
            latency_values.get(idx).cloned().unwrap_or(0)
        } else {
            0
        };

        let p99_latency = if !latency_values.is_empty() {
            let idx = (latency_values.len() as f64 * 0.99) as usize;
            latency_values.get(idx).cloned().unwrap_or(0)
        } else {
            0
        };

        // Calculate temperature stats
        let temp_values: Vec<f32> = samples.iter().filter_map(|s| s.temperature).collect();

        let avg_temperature = if !temp_values.is_empty() {
            Some(temp_values.iter().sum::<f32>() / temp_values.len() as f32)
        } else {
            None
        };

        let peak_temperature = if !temp_values.is_empty() {
            Some(temp_values.iter().cloned().fold(0.0f32, |a, b| a.max(b)))
        } else {
            None
        };

        Ok(PerformanceStats {
            avg_cpu,
            peak_cpu,
            avg_memory,
            peak_memory,
            avg_throughput,
            peak_throughput,
            avg_latency,
            p95_latency,
            p99_latency,
            avg_temperature,
            peak_temperature,
            sample_count: samples.len(),
            duration,
        })
    }

    /// Get the detected device type
    pub fn device(&self) -> &ArmDevice {
        &self.device
    }

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

    /// Clear all samples
    pub fn clear(&self) {
        self.samples.write().clear();
    }

    /// Get sample count
    pub fn sample_count(&self) -> usize {
        self.samples.read().len()
    }
}

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

    #[test]
    fn test_profiler_creation() {
        let config = ProfilerConfig::default();
        let profiler = ArmProfiler::new(config);

        assert!(profiler.sample_count() == 0);
    }

    #[test]
    fn test_auto_detect() {
        let profiler = ArmProfiler::auto_detect();
        assert!(profiler.sample_count() == 0);
    }

    #[test]
    fn test_record_cpu() {
        let profiler = ArmProfiler::auto_detect();
        profiler.record_cpu(50.0);

        assert_eq!(profiler.sample_count(), 1);
    }

    #[test]
    fn test_record_memory() {
        let profiler = ArmProfiler::auto_detect();
        profiler.record_memory(1024 * 1024);

        assert_eq!(profiler.sample_count(), 1);
    }

    #[test]
    fn test_record_throughput() {
        let profiler = ArmProfiler::auto_detect();
        profiler.record_throughput(1000000);

        assert_eq!(profiler.sample_count(), 1);
    }

    #[test]
    fn test_record_latency() {
        let profiler = ArmProfiler::auto_detect();
        profiler.record_latency(Duration::from_millis(10));

        assert_eq!(profiler.sample_count(), 1);
    }

    #[test]
    fn test_stats_calculation() {
        let profiler = ArmProfiler::auto_detect();

        // Record some samples
        profiler.record_cpu(30.0);
        profiler.record_cpu(50.0);
        profiler.record_cpu(70.0);

        profiler.record_memory(1024);
        profiler.record_memory(2048);
        profiler.record_memory(3072);

        let stats = profiler
            .stats()
            .expect("test: stats should succeed with recorded samples");

        assert_eq!(stats.avg_cpu, 50.0);
        assert_eq!(stats.peak_cpu, 70.0);
        assert_eq!(stats.avg_memory, 2048);
        assert_eq!(stats.peak_memory, 3072);
    }

    #[test]
    fn test_latency_percentiles() {
        let profiler = ArmProfiler::auto_detect();

        // Record latencies
        for i in 1..=100 {
            profiler.record_latency(Duration::from_micros(i * 10));
        }

        let stats = profiler
            .stats()
            .expect("test: stats should succeed with 100 latency samples");

        assert!(stats.avg_latency > 0);
        assert!(stats.p95_latency > stats.avg_latency);
        assert!(stats.p99_latency > stats.p95_latency);
    }

    #[test]
    fn test_max_samples_limit() {
        let config = ProfilerConfig {
            max_samples: 10,
            ..Default::default()
        };

        let profiler = ArmProfiler::new(config);

        // Record more samples than the limit
        for i in 0..20 {
            profiler.record_cpu(i as f64);
        }

        // Should only keep the last 10 samples
        assert_eq!(profiler.sample_count(), 10);
    }

    #[test]
    fn test_clear_samples() {
        let profiler = ArmProfiler::auto_detect();

        profiler.record_cpu(50.0);
        profiler.record_cpu(60.0);
        assert_eq!(profiler.sample_count(), 2);

        profiler.clear();
        assert_eq!(profiler.sample_count(), 0);
    }

    #[test]
    fn test_device_configs() {
        let rpi_config = ProfilerConfig::raspberry_pi();
        let jetson_config = ProfilerConfig::jetson();

        assert!(rpi_config.sample_interval > jetson_config.sample_interval);
        assert!(rpi_config.max_samples < jetson_config.max_samples);
    }

    #[test]
    fn test_insufficient_samples_error() {
        let profiler = ArmProfiler::auto_detect();
        let result = profiler.stats();

        assert!(matches!(result, Err(ProfilerError::InsufficientSamples)));
    }
}