scirs2-core 0.1.0-alpha.3

Core utilities and common functionality for SciRS2
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! # Profiling
//!
//! This module provides utilities for profiling computational performance in scientific applications.
//!
//! ## Features
//!
//! * Function-level timing instrumentation
//! * Memory allocation tracking
//! * Hierarchical profiling for nested operations
//! * Easy-to-use macros for profiling sections of code
//!
//! ## Usage
//!
//! ```rust,no_run
//! use scirs2_core::profiling::{Profiler, Timer, MemoryTracker};
//!
//! // Start the global profiler
//! Profiler::global().lock().unwrap().start();
//!
//! // Time a function call
//! let result = Timer::time_function("matrix_multiplication", || {
//!     // Perform matrix multiplication
//!     // ...
//!     42 // Return some result
//! });
//!
//! // Time a code block with more control
//! let timer = Timer::start("data_processing");
//! // Perform data processing
//! // ...
//! timer.stop();
//!
//! // Track memory allocations
//! let tracker = MemoryTracker::start("large_array_operation");
//! let large_array = vec![0; 1_000_000];
//! // ...
//! tracker.stop();
//!
//! // Print profiling report
//! Profiler::global().lock().unwrap().print_report();
//!
//! // Stop profiling
//! Profiler::global().lock().unwrap().stop();
//! ```

use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};

/// Timer for measuring code execution time
pub struct Timer {
    /// Name of the operation being timed
    name: String,
    /// Start time
    start_time: Instant,
    /// Whether the timer is currently running
    running: bool,
    /// Whether to automatically report the timing when dropped
    auto_report: bool,
    /// Parent timer name for hierarchical profiling
    parent: Option<String>,
}

impl Timer {
    /// Start a new timer with the given name
    pub fn start(name: &str) -> Self {
        let timer = Self {
            name: name.to_string(),
            start_time: Instant::now(),
            running: true,
            auto_report: true,
            parent: None,
        };
        if let Ok(mut profiler) = Profiler::global().lock() {
            profiler.register_timer_start(&timer);
        }
        timer
    }

    /// Start a new hierarchical timer with a parent
    pub fn start_with_parent(name: &str, parent: &str) -> Self {
        let timer = Self {
            name: name.to_string(),
            start_time: Instant::now(),
            running: true,
            auto_report: true,
            parent: Some(parent.to_string()),
        };
        if let Ok(mut profiler) = Profiler::global().lock() {
            profiler.register_timer_start(&timer);
        }
        timer
    }

    /// Time a function call and return its result
    pub fn time_function<F, R>(name: &str, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let timer = Self::start(name);
        let result = f();
        timer.stop();
        result
    }

    /// Time a function call with a parent timer and return its result
    pub fn time_function_with_parent<F, R>(name: &str, parent: &str, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let timer = Self::start_with_parent(name, parent);
        let result = f();
        timer.stop();
        result
    }

    /// Stop the timer and record the elapsed time
    pub fn stop(&self) {
        if !self.running {
            return;
        }

        let elapsed = self.start_time.elapsed();
        if let Ok(mut profiler) = Profiler::global().lock() {
            profiler.register_timer_stop(&self.name, elapsed, self.parent.as_deref());
        }
    }

    /// Get the elapsed time without stopping the timer
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Disable auto-reporting when dropped
    pub fn without_auto_report(mut self) -> Self {
        self.auto_report = false;
        self
    }
}

impl Drop for Timer {
    fn drop(&mut self) {
        if self.running && self.auto_report {
            let elapsed = self.start_time.elapsed();
            if let Ok(mut profiler) = Profiler::global().lock() {
                profiler.register_timer_stop(&self.name, elapsed, self.parent.as_deref());
            }
        }
    }
}

/// Memory allocation tracker
pub struct MemoryTracker {
    /// Name of the operation being tracked
    name: String,
    /// Start memory usage
    start_memory: usize,
    /// Whether the tracker is currently running
    running: bool,
    /// Whether to automatically report when dropped
    auto_report: bool,
}

impl MemoryTracker {
    /// Start a new memory tracker with the given name
    pub fn start(name: &str) -> Self {
        let current_memory = Self::current_memory_usage();
        let tracker = Self {
            name: name.to_string(),
            start_memory: current_memory,
            running: true,
            auto_report: true,
        };
        if let Ok(mut profiler) = Profiler::global().lock() {
            profiler.register_memory_tracker_start(&tracker);
        }
        tracker
    }

    /// Stop the tracker and record the memory usage
    pub fn stop(&self) {
        if !self.running {
            return;
        }

        let current_memory = Self::current_memory_usage();
        let memory_delta = current_memory.saturating_sub(self.start_memory);
        if let Ok(mut profiler) = Profiler::global().lock() {
            profiler.register_memory_tracker_stop(&self.name, memory_delta);
        }
    }

    /// Track memory usage for a function call and return its result
    pub fn track_function<F, R>(name: &str, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let tracker = Self::start(name);
        let result = f();
        tracker.stop();
        result
    }

    /// Get the current memory delta without stopping the tracker
    pub fn memory_delta(&self) -> isize {
        let current_memory = Self::current_memory_usage();
        current_memory as isize - self.start_memory as isize
    }

    /// Disable auto-reporting when dropped
    pub fn without_auto_report(mut self) -> Self {
        self.auto_report = false;
        self
    }

    /// Get the current memory usage (platform-dependent implementation)
    fn current_memory_usage() -> usize {
        // This is a simplified implementation that doesn't actually track real memory
        // A real implementation would use platform-specific APIs to get memory usage
        #[cfg(target_os = "linux")]
        {
            // On Linux, we would read /proc/self/statm
            0
        }

        #[cfg(target_os = "macos")]
        {
            // On macOS, we would use task_info
            0
        }

        #[cfg(target_os = "windows")]
        {
            // On Windows, we would use GetProcessMemoryInfo
            0
        }

        #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
        {
            // Fallback for other platforms
            0
        }
    }
}

impl Drop for MemoryTracker {
    fn drop(&mut self) {
        if self.running && self.auto_report {
            let current_memory = Self::current_memory_usage();
            let memory_delta = current_memory.saturating_sub(self.start_memory);
            if let Ok(mut profiler) = Profiler::global().lock() {
                profiler.register_memory_tracker_stop(&self.name, memory_delta);
            }
        }
    }
}

/// Timing entry for the profiler
#[derive(Debug, Clone)]
struct TimingEntry {
    /// Number of calls
    calls: usize,
    /// Total duration
    total_duration: Duration,
    /// Minimum duration
    min_duration: Duration,
    /// Maximum duration
    max_duration: Duration,
    /// Parent operation (used for hierarchical profiling structure)
    #[allow(dead_code)]
    parent: Option<String>,
    /// Child operations
    children: Vec<String>,
}

impl TimingEntry {
    /// Create a new timing entry
    fn new(duration: Duration, parent: Option<&str>) -> Self {
        Self {
            calls: 1,
            total_duration: duration,
            min_duration: duration,
            max_duration: duration,
            parent: parent.map(String::from),
            children: Vec::new(),
        }
    }

    /// Add a new timing measurement
    fn add_measurement(&mut self, duration: Duration) {
        self.calls += 1;
        self.total_duration += duration;
        self.min_duration = std::cmp::min(self.min_duration, duration);
        self.max_duration = std::cmp::max(self.max_duration, duration);
    }

    /// Add a child operation
    fn add_child(&mut self, child: &str) {
        if !self.children.contains(&child.to_string()) {
            self.children.push(child.to_string());
        }
    }

    /// Get the average duration
    fn average_duration(&self) -> Duration {
        if self.calls == 0 {
            Duration::from_secs(0)
        } else {
            self.total_duration / self.calls as u32
        }
    }
}

/// Memory tracking entry for the profiler
#[derive(Debug, Clone)]
struct MemoryEntry {
    /// Number of allocations
    allocations: usize,
    /// Total memory delta (can be negative for memory releases)
    total_delta: isize,
    /// Maximum memory delta in a single allocation
    max_delta: usize,
}

impl MemoryEntry {
    /// Create a new memory entry
    fn new(delta: usize) -> Self {
        Self {
            allocations: 1,
            total_delta: delta as isize,
            max_delta: delta,
        }
    }

    /// Add a new memory measurement
    fn add_measurement(&mut self, delta: usize) {
        self.allocations += 1;
        self.total_delta += delta as isize;
        self.max_delta = std::cmp::max(self.max_delta, delta);
    }

    /// Get the average memory delta
    #[allow(dead_code)]
    fn average_delta(&self) -> f64 {
        if self.allocations == 0 {
            0.0
        } else {
            self.total_delta as f64 / self.allocations as f64
        }
    }
}

/// Profiler for collecting performance metrics
pub struct Profiler {
    /// Timing measurements
    timings: HashMap<String, TimingEntry>,
    /// Memory measurements
    memory: HashMap<String, MemoryEntry>,
    /// Currently active timers
    active_timers: HashMap<String, Instant>,
    /// Whether the profiler is currently running
    running: bool,
}

impl Profiler {
    /// Create a new profiler
    pub fn new() -> Self {
        Self {
            timings: HashMap::new(),
            memory: HashMap::new(),
            active_timers: HashMap::new(),
            running: false,
        }
    }

    /// Get the global profiler instance
    pub fn global() -> &'static Mutex<Profiler> {
        static GLOBAL_PROFILER: Lazy<Mutex<Profiler>> = Lazy::new(|| Mutex::new(Profiler::new()));
        &GLOBAL_PROFILER
    }

    /// Start the profiler
    pub fn start(&mut self) {
        self.running = true;
        self.timings.clear();
        self.memory.clear();
        self.active_timers.clear();
    }

    /// Stop the profiler
    pub fn stop(&mut self) {
        self.running = false;
    }

    /// Reset the profiler
    pub fn reset(&mut self) {
        self.timings.clear();
        self.memory.clear();
        self.active_timers.clear();
    }

    /// Register the start of a timer
    pub fn register_timer_start(&mut self, timer: &Timer) {
        if !self.running {
            return;
        }

        self.active_timers
            .insert(timer.name.clone(), timer.start_time);

        // Register the parent-child relationship
        if let Some(parent) = &timer.parent {
            if let Some(entry) = self.timings.get_mut(parent) {
                entry.add_child(&timer.name);
            }
        }
    }

    /// Register the stop of a timer
    pub fn register_timer_stop(&mut self, name: &str, duration: Duration, parent: Option<&str>) {
        if !self.running {
            return;
        }

        // Remove from active timers
        self.active_timers.remove(name);

        // Update the timing entry
        match self.timings.get_mut(name) {
            Some(entry) => {
                entry.add_measurement(duration);
            }
            None => {
                let entry = TimingEntry::new(duration, parent);
                self.timings.insert(name.to_string(), entry);
            }
        }

        // Register the parent-child relationship
        if let Some(parent) = parent {
            if let Some(entry) = self.timings.get_mut(parent) {
                entry.add_child(name);
            }
        }
    }

    /// Register the start of a memory tracker
    pub fn register_memory_tracker_start(&mut self, _tracker: &MemoryTracker) {
        if !self.running {
            // Nothing to do at start, just ensure the method exists for symmetry
        }
    }

    /// Register the stop of a memory tracker
    pub fn register_memory_tracker_stop(&mut self, name: &str, delta: usize) {
        if !self.running {
            return;
        }

        // Update the memory entry
        match self.memory.get_mut(name) {
            Some(entry) => {
                entry.add_measurement(delta);
            }
            None => {
                let entry = MemoryEntry::new(delta);
                self.memory.insert(name.to_string(), entry);
            }
        }
    }

    /// Print a report of the profiling results
    pub fn print_report(&self) {
        if self.timings.is_empty() && self.memory.is_empty() {
            println!("No profiling data collected.");
            return;
        }

        if !self.timings.is_empty() {
            println!("\n=== Timing Report ===");
            println!(
                "{:<30} {:<10} {:<15} {:<15} {:<15}",
                "Operation", "Calls", "Total (ms)", "Average (ms)", "Max (ms)"
            );
            println!("{}", "-".repeat(90));

            // Sort by total duration
            let mut entries: Vec<(&String, &TimingEntry)> = self.timings.iter().collect();
            entries.sort_by(|a, b| b.1.total_duration.cmp(&a.1.total_duration));

            for (name, entry) in entries {
                println!(
                    "{:<30} {:<10} {:<15.2} {:<15.2} {:<15.2}",
                    name,
                    entry.calls,
                    entry.total_duration.as_secs_f64() * 1000.0,
                    entry.average_duration().as_secs_f64() * 1000.0,
                    entry.max_duration.as_secs_f64() * 1000.0
                );
            }
        }

        if !self.memory.is_empty() {
            println!("\n=== Memory Report ===");
            println!(
                "{:<30} {:<10} {:<15} {:<15}",
                "Operation", "Counts", "Total (KB)", "Max (KB)"
            );
            println!("{}", "-".repeat(75));

            // Sort by total memory delta
            let mut entries: Vec<(&String, &MemoryEntry)> = self.memory.iter().collect();
            entries.sort_by(|a, b| b.1.total_delta.abs().cmp(&a.1.total_delta.abs()));

            for (name, entry) in entries {
                println!(
                    "{:<30} {:<10} {:<15.2} {:<15.2}",
                    name,
                    entry.allocations,
                    entry.total_delta as f64 / 1024.0,
                    entry.max_delta as f64 / 1024.0
                );
            }
        }
    }

    /// Get a report of the profiling results as a string
    pub fn get_report(&self) -> String {
        use std::fmt::Write;
        let mut report = String::new();

        if self.timings.is_empty() && self.memory.is_empty() {
            writeln!(report, "No profiling data collected.").unwrap();
            return report;
        }

        if !self.timings.is_empty() {
            writeln!(report, "\n=== Timing Report ===").unwrap();
            writeln!(
                report,
                "{:<30} {:<10} {:<15} {:<15} {:<15}",
                "Operation", "Calls", "Total (ms)", "Average (ms)", "Max (ms)"
            )
            .unwrap();
            writeln!(report, "{}", "-".repeat(90)).unwrap();

            // Sort by total duration
            let mut entries: Vec<(&String, &TimingEntry)> = self.timings.iter().collect();
            entries.sort_by(|a, b| b.1.total_duration.cmp(&a.1.total_duration));

            for (name, entry) in entries {
                writeln!(
                    report,
                    "{:<30} {:<10} {:<15.2} {:<15.2} {:<15.2}",
                    name,
                    entry.calls,
                    entry.total_duration.as_secs_f64() * 1000.0,
                    entry.average_duration().as_secs_f64() * 1000.0,
                    entry.max_duration.as_secs_f64() * 1000.0
                )
                .unwrap();
            }
        }

        if !self.memory.is_empty() {
            writeln!(report, "\n=== Memory Report ===").unwrap();
            writeln!(
                report,
                "{:<30} {:<10} {:<15} {:<15}",
                "Operation", "Counts", "Total (KB)", "Max (KB)"
            )
            .unwrap();
            writeln!(report, "{}", "-".repeat(75)).unwrap();

            // Sort by total memory delta
            let mut entries: Vec<(&String, &MemoryEntry)> = self.memory.iter().collect();
            entries.sort_by(|a, b| b.1.total_delta.abs().cmp(&a.1.total_delta.abs()));

            for (name, entry) in entries {
                writeln!(
                    report,
                    "{:<30} {:<10} {:<15.2} {:<15.2}",
                    name,
                    entry.allocations,
                    entry.total_delta as f64 / 1024.0,
                    entry.max_delta as f64 / 1024.0
                )
                .unwrap();
            }
        }

        report
    }

    /// Get timing statistics for a specific operation
    pub fn get_timing_stats(&self, name: &str) -> Option<(usize, Duration, Duration, Duration)> {
        self.timings.get(name).map(|entry| {
            (
                entry.calls,
                entry.total_duration,
                entry.average_duration(),
                entry.max_duration,
            )
        })
    }

    /// Get memory statistics for a specific operation
    pub fn get_memory_stats(&self, name: &str) -> Option<(usize, isize, usize)> {
        self.memory
            .get(name)
            .map(|entry| (entry.allocations, entry.total_delta, entry.max_delta))
    }
}

impl Default for Profiler {
    fn default() -> Self {
        Self::new()
    }
}

/// Access a memory tracker from the profiling module to avoid name conflicts
pub fn profiling_memory_tracker() -> &'static MemoryTracker {
    // Create a dummy memory tracker for static access
    static MEMORY_TRACKER: once_cell::sync::Lazy<MemoryTracker> =
        once_cell::sync::Lazy::new(|| MemoryTracker {
            name: "global".to_string(),
            start_memory: 0,
            running: false,
            auto_report: false,
        });
    &MEMORY_TRACKER
}

/// Macro for timing a block of code
#[macro_export]
macro_rules! profile_time {
    ($name:expr, $body:block) => {{
        let timer = $crate::profiling::Timer::start($name);
        let result = $body;
        timer.stop();
        result
    }};
}

/// Macro for tracking memory usage in a block of code
#[macro_export]
macro_rules! profile_memory {
    ($name:expr, $body:block) => {{
        let tracker = $crate::profiling::MemoryTracker::start($name);
        let result = $body;
        tracker.stop();
        result
    }};
}

/// Macro for timing a block of code with a parent operation
#[macro_export]
macro_rules! profile_time_with_parent {
    ($name:expr, $parent:expr, $body:block) => {{
        let timer = $crate::profiling::Timer::start_with_parent($name, $parent);
        let result = $body;
        timer.stop();
        result
    }};
}