torsh-profiler 0.1.2

Performance profiling and monitoring for ToRSh
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
//! Cross-platform Profiling Support
//!
//! This module provides platform-specific profiling implementations for:
//! - ARM64 (AArch64) - Apple Silicon, ARM servers
//! - RISC-V - Emerging RISC architecture
//! - WebAssembly (WASM) - Browser and edge computing
//!
//! # Features
//!
//! - Platform detection and capability discovery
//! - Architecture-specific performance counters
//! - Timer implementations with fallbacks
//! - Memory profiling adapters
//! - Portable profiling API

use serde::{Deserialize, Serialize};
use std::time::Instant;
use torsh_core::{Result as TorshResult, TorshError};

/// Platform architecture types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PlatformArch {
    X86_64,
    ARM64,
    RISCV64,
    WASM32,
    WASM64,
    Unknown,
}

impl PlatformArch {
    /// Detect the current platform architecture
    pub fn detect() -> Self {
        #[cfg(target_arch = "x86_64")]
        return Self::X86_64;

        #[cfg(target_arch = "aarch64")]
        return Self::ARM64;

        #[cfg(target_arch = "riscv64")]
        return Self::RISCV64;

        #[cfg(all(target_arch = "wasm32", not(target_pointer_width = "64")))]
        return Self::WASM32;

        #[cfg(all(target_arch = "wasm32", target_pointer_width = "64"))]
        return Self::WASM64;

        #[cfg(not(any(
            target_arch = "x86_64",
            target_arch = "aarch64",
            target_arch = "riscv64",
            target_arch = "wasm32"
        )))]
        return Self::Unknown;
    }

    /// Check if the platform is ARM-based
    pub fn is_arm(&self) -> bool {
        matches!(self, Self::ARM64)
    }

    /// Check if the platform is RISC-V
    pub fn is_riscv(&self) -> bool {
        matches!(self, Self::RISCV64)
    }

    /// Check if the platform is WebAssembly
    pub fn is_wasm(&self) -> bool {
        matches!(self, Self::WASM32 | Self::WASM64)
    }

    /// Check if the platform supports hardware performance counters
    pub fn supports_hardware_counters(&self) -> bool {
        matches!(self, Self::X86_64 | Self::ARM64 | Self::RISCV64)
    }
}

impl std::fmt::Display for PlatformArch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::X86_64 => write!(f, "x86_64"),
            Self::ARM64 => write!(f, "ARM64 (AArch64)"),
            Self::RISCV64 => write!(f, "RISC-V 64-bit"),
            Self::WASM32 => write!(f, "WebAssembly 32-bit"),
            Self::WASM64 => write!(f, "WebAssembly 64-bit"),
            Self::Unknown => write!(f, "Unknown"),
        }
    }
}

/// Platform capabilities for profiling
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlatformCapabilities {
    pub arch: PlatformArch,
    pub has_rdtsc: bool,
    pub has_pmu: bool,
    pub has_simd: bool,
    pub simd_width: usize,
    pub cache_line_size: usize,
    pub supports_atomics: bool,
    pub supports_threads: bool,
    pub timer_resolution_ns: u64,
}

impl PlatformCapabilities {
    /// Detect platform capabilities
    pub fn detect() -> Self {
        let arch = PlatformArch::detect();

        let (has_rdtsc, has_pmu, has_simd, simd_width) = match arch {
            PlatformArch::X86_64 => (true, true, true, 256), // AVX2
            PlatformArch::ARM64 => (false, true, true, 128), // NEON
            PlatformArch::RISCV64 => (false, true, true, 128), // RVV
            PlatformArch::WASM32 | PlatformArch::WASM64 => (false, false, true, 128), // SIMD128
            PlatformArch::Unknown => (false, false, false, 0),
        };

        let cache_line_size = match arch {
            PlatformArch::X86_64 => 64,
            PlatformArch::ARM64 => 64,
            PlatformArch::RISCV64 => 64,
            PlatformArch::WASM32 | PlatformArch::WASM64 => 0, // No cache in WASM
            PlatformArch::Unknown => 64,
        };

        let supports_atomics = !arch.is_wasm(); // WASM atomics require SharedArrayBuffer
        let supports_threads = !arch.is_wasm(); // WASM threads require special features

        // Estimate timer resolution
        let timer_resolution_ns = match arch {
            PlatformArch::X86_64 => 1,   // RDTSC nanosecond precision
            PlatformArch::ARM64 => 10,   // ARM generic timer ~10ns
            PlatformArch::RISCV64 => 10, // RISC-V timer ~10ns
            PlatformArch::WASM32 | PlatformArch::WASM64 => 1000, // performance.now() ~1μs
            PlatformArch::Unknown => 1000,
        };

        Self {
            arch,
            has_rdtsc,
            has_pmu,
            has_simd,
            simd_width,
            cache_line_size,
            supports_atomics,
            supports_threads,
            timer_resolution_ns,
        }
    }
}

/// Cross-platform high-resolution timer
pub struct CrossPlatformTimer {
    start: Instant,
    capabilities: PlatformCapabilities,
}

impl CrossPlatformTimer {
    /// Create a new timer
    pub fn new() -> Self {
        Self {
            start: Instant::now(),
            capabilities: PlatformCapabilities::detect(),
        }
    }

    /// Start timing
    pub fn start(&mut self) {
        self.start = Instant::now();
    }

    /// Get elapsed time in microseconds
    pub fn elapsed_us(&self) -> u64 {
        self.start.elapsed().as_micros() as u64
    }

    /// Get elapsed time in nanoseconds
    pub fn elapsed_ns(&self) -> u64 {
        self.start.elapsed().as_nanos() as u64
    }

    /// Get platform-specific timestamp counter (if available)
    pub fn get_cycle_count(&self) -> Option<u64> {
        #[cfg(target_arch = "x86_64")]
        {
            // Use RDTSC on x86_64
            Some(unsafe { std::arch::x86_64::_rdtsc() })
        }

        #[cfg(target_arch = "aarch64")]
        {
            // ARM64 - use PMCCNTR_EL0 (requires privileged access)
            // Fallback to Instant-based timing
            None
        }

        #[cfg(target_arch = "riscv64")]
        {
            // RISC-V - use RDCYCLE
            // Note: Requires M-mode or counter delegation
            None
        }

        #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
        {
            // WASM has no cycle counter
            None
        }

        #[cfg(not(any(
            target_arch = "x86_64",
            target_arch = "aarch64",
            target_arch = "riscv64",
            target_arch = "wasm32",
            target_arch = "wasm64"
        )))]
        {
            None
        }
    }

    /// Get platform capabilities
    pub fn capabilities(&self) -> &PlatformCapabilities {
        &self.capabilities
    }
}

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

/// ARM64-specific profiling features
#[cfg(target_arch = "aarch64")]
pub mod arm64 {
    use super::*;

    /// ARM64 performance counter types
    #[derive(Debug, Clone, Copy)]
    pub enum ARM64Counter {
        CycleCount,
        InstructionCount,
        CacheMisses,
        BranchMisses,
        L1DCacheAccess,
        L1DCacheMiss,
        L2CacheAccess,
        L2CacheMiss,
    }

    /// ARM64 NEON SIMD information
    pub struct NeonInfo {
        pub available: bool,
        pub register_width: usize,
        pub num_registers: usize,
    }

    impl NeonInfo {
        pub fn detect() -> Self {
            Self {
                available: true, // NEON is mandatory in ARMv8-A
                register_width: 128,
                num_registers: 32,
            }
        }
    }

    /// Apple Silicon specific features
    #[cfg(target_os = "macos")]
    pub mod apple_silicon {
        /// Detect if running on Apple Silicon
        pub fn is_apple_silicon() -> bool {
            cfg!(all(target_arch = "aarch64", target_os = "macos"))
        }

        /// Get performance core count (P-cores)
        pub fn performance_core_count() -> usize {
            // This is a simplified detection
            // Real implementation would use sysctl
            num_cpus::get() / 2
        }

        /// Get efficiency core count (E-cores)
        pub fn efficiency_core_count() -> usize {
            num_cpus::get() / 2
        }
    }
}

/// RISC-V specific profiling features
#[cfg(target_arch = "riscv64")]
pub mod riscv {
    use super::*;

    /// RISC-V performance counter types
    #[derive(Debug, Clone, Copy)]
    pub enum RISCVCounter {
        CycleCount,
        InstructionCount,
        Time,
    }

    /// RISC-V Vector (RVV) extension information
    pub struct RVVInfo {
        pub available: bool,
        pub vlen: usize, // Vector register length in bits
    }

    impl RVVInfo {
        pub fn detect() -> Self {
            Self {
                available: false, // Detect via CSR or runtime check
                vlen: 0,
            }
        }
    }
}

/// WebAssembly specific profiling features
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
pub mod wasm {
    use super::*;

    /// WASM runtime environment
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum WasmRuntime {
        Browser,
        Node,
        Wasmtime,
        Wasmer,
        Unknown,
    }

    impl WasmRuntime {
        pub fn detect() -> Self {
            // Detection would require JS interop
            Self::Unknown
        }
    }

    /// WASM SIMD support
    pub struct WasmSimdInfo {
        pub available: bool,
        pub simd128: bool,
    }

    impl WasmSimdInfo {
        pub fn detect() -> Self {
            Self {
                available: true,
                simd128: true, // Most modern WASM runtimes support SIMD128
            }
        }
    }

    /// WASM memory profiling (different from native)
    pub struct WasmMemoryProfiler {
        initial_pages: usize,
        max_pages: Option<usize>,
    }

    impl WasmMemoryProfiler {
        pub fn new() -> Self {
            Self {
                initial_pages: 256,     // Default 16MB (64KB per page)
                max_pages: Some(65536), // Maximum 4GB
            }
        }

        pub fn current_memory_pages(&self) -> usize {
            // Would use wasm memory.size instruction
            self.initial_pages
        }

        pub fn memory_bytes(&self) -> usize {
            self.current_memory_pages() * 65536 // 64KB per page
        }
    }

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

/// Cross-platform profiler adapter
pub struct CrossPlatformProfiler {
    capabilities: PlatformCapabilities,
    timer: CrossPlatformTimer,
}

impl CrossPlatformProfiler {
    /// Create a new cross-platform profiler
    pub fn new() -> Self {
        Self {
            capabilities: PlatformCapabilities::detect(),
            timer: CrossPlatformTimer::new(),
        }
    }

    /// Get platform information
    pub fn platform_info(&self) -> String {
        format!(
            "Platform: {}\n\
             Hardware Counters: {}\n\
             SIMD: {} (width: {} bits)\n\
             Cache Line: {} bytes\n\
             Atomics: {}\n\
             Threads: {}\n\
             Timer Resolution: {} ns",
            self.capabilities.arch,
            if self.capabilities.has_pmu {
                "Yes"
            } else {
                "No"
            },
            if self.capabilities.has_simd {
                "Yes"
            } else {
                "No"
            },
            self.capabilities.simd_width,
            self.capabilities.cache_line_size,
            if self.capabilities.supports_atomics {
                "Yes"
            } else {
                "No"
            },
            if self.capabilities.supports_threads {
                "Yes"
            } else {
                "No"
            },
            self.capabilities.timer_resolution_ns
        )
    }

    /// Start profiling
    pub fn start(&mut self) {
        self.timer.start();
    }

    /// Stop profiling and get elapsed time
    pub fn stop(&self) -> u64 {
        self.timer.elapsed_us()
    }

    /// Get capabilities
    pub fn capabilities(&self) -> &PlatformCapabilities {
        &self.capabilities
    }

    /// Check if running on a specific architecture
    pub fn is_architecture(&self, arch: PlatformArch) -> bool {
        self.capabilities.arch == arch
    }

    /// Get recommended profiling strategy for the platform
    pub fn recommended_strategy(&self) -> ProfilingStrategy {
        match self.capabilities.arch {
            PlatformArch::X86_64 => ProfilingStrategy::HardwareCounters,
            PlatformArch::ARM64 => ProfilingStrategy::Hybrid,
            PlatformArch::RISCV64 => ProfilingStrategy::Sampling,
            PlatformArch::WASM32 | PlatformArch::WASM64 => ProfilingStrategy::Lightweight,
            PlatformArch::Unknown => ProfilingStrategy::Basic,
        }
    }
}

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

/// Recommended profiling strategy based on platform
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProfilingStrategy {
    /// Use hardware performance counters (x86_64)
    HardwareCounters,
    /// Hybrid approach (ARM64)
    Hybrid,
    /// Sampling-based profiling (RISC-V)
    Sampling,
    /// Lightweight instrumentation (WASM)
    Lightweight,
    /// Basic timing only
    Basic,
}

impl std::fmt::Display for ProfilingStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::HardwareCounters => write!(f, "Hardware Counters"),
            Self::Hybrid => write!(f, "Hybrid"),
            Self::Sampling => write!(f, "Sampling"),
            Self::Lightweight => write!(f, "Lightweight"),
            Self::Basic => write!(f, "Basic"),
        }
    }
}

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

    #[test]
    fn test_platform_detection() {
        let arch = PlatformArch::detect();
        println!("Detected architecture: {}", arch);
        assert_ne!(arch, PlatformArch::Unknown);
    }

    #[test]
    fn test_capabilities_detection() {
        let caps = PlatformCapabilities::detect();
        println!("Platform capabilities:");
        println!("  Architecture: {}", caps.arch);
        println!("  RDTSC: {}", caps.has_rdtsc);
        println!("  PMU: {}", caps.has_pmu);
        println!("  SIMD: {} (width: {})", caps.has_simd, caps.simd_width);
        println!("  Cache line: {} bytes", caps.cache_line_size);
        println!("  Atomics: {}", caps.supports_atomics);
        println!("  Threads: {}", caps.supports_threads);
        println!("  Timer resolution: {} ns", caps.timer_resolution_ns);

        assert!(caps.cache_line_size > 0 || caps.arch.is_wasm());
    }

    #[test]
    fn test_cross_platform_timer() {
        let mut timer = CrossPlatformTimer::new();
        timer.start();

        // Simulate some work
        std::thread::sleep(std::time::Duration::from_micros(100));

        let elapsed = timer.elapsed_us();
        println!("Elapsed time: {} μs", elapsed);
        assert!(elapsed >= 100);
    }

    #[test]
    fn test_cross_platform_profiler() {
        let mut profiler = CrossPlatformProfiler::new();
        println!("{}", profiler.platform_info());

        profiler.start();
        std::thread::sleep(std::time::Duration::from_micros(100));
        let elapsed = profiler.stop();

        println!("Profiled time: {} μs", elapsed);
        assert!(elapsed >= 100);

        let strategy = profiler.recommended_strategy();
        println!("Recommended strategy: {}", strategy);
    }

    #[test]
    fn test_architecture_checks() {
        let profiler = CrossPlatformProfiler::new();
        let arch = profiler.capabilities().arch;

        assert_eq!(profiler.is_architecture(arch), true);
    }

    #[cfg(target_arch = "aarch64")]
    #[test]
    fn test_arm64_neon() {
        let neon = arm64::NeonInfo::detect();
        assert!(neon.available);
        assert_eq!(neon.register_width, 128);
        println!(
            "NEON: {} registers of {} bits",
            neon.num_registers, neon.register_width
        );
    }

    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    #[test]
    fn test_apple_silicon() {
        if arm64::apple_silicon::is_apple_silicon() {
            println!("Running on Apple Silicon");
            println!(
                "P-cores: {}",
                arm64::apple_silicon::performance_core_count()
            );
            println!("E-cores: {}", arm64::apple_silicon::efficiency_core_count());
        }
    }
}