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
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
//! AMD Tools Integration
//!
//! This module provides integration with AMD profiling tools including:
//! - ROCm Profiler for GPU profiling
//! - CodeXL for comprehensive analysis
//! - AMD uProf for CPU profiling
//! - ROCTracer for detailed GPU tracing

use crate::{ProfileEvent, TorshError, TorshResult};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

/// AMD profiling integration manager
pub struct AMDProfiler {
    /// ROCm profiler for GPU operations
    rocm_profiler: Option<ROCmProfiler>,
    /// CodeXL profiler for comprehensive analysis
    codexl_profiler: Option<CodeXLProfiler>,
    /// uProf profiler for CPU analysis
    uprof_profiler: Option<UProfProfiler>,
    /// ROCTracer for detailed GPU tracing
    roctracer: Option<ROCTracer>,
    /// Whether AMD profiling is enabled
    enabled: bool,
    /// Configuration settings
    config: AMDProfilerConfig,
    /// Collected events
    events: Arc<Mutex<Vec<ProfileEvent>>>,
}

/// Configuration for AMD profiling tools
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AMDProfilerConfig {
    /// Enable ROCm GPU profiling
    pub enable_rocm: bool,
    /// Enable CodeXL profiling
    pub enable_codexl: bool,
    /// Enable uProf CPU profiling
    pub enable_uprof: bool,
    /// Enable ROCTracer
    pub enable_roctracer: bool,
    /// GPU device ID to profile
    pub gpu_device_id: u32,
    /// Sample frequency for CPU profiling (Hz)
    pub cpu_sample_frequency: u32,
    /// Enable kernel profiling
    pub enable_kernel_profiling: bool,
    /// Enable memory profiling
    pub enable_memory_profiling: bool,
    /// Enable power profiling
    pub enable_power_profiling: bool,
}

/// ROCm profiler for AMD GPU operations
pub struct ROCmProfiler {
    device_id: u32,
    enabled: bool,
    kernel_launches: Vec<ROCmKernelLaunch>,
    memory_operations: Vec<ROCmMemoryOperation>,
    hip_api_calls: Vec<HIPAPICall>,
}

/// ROCm kernel launch information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ROCmKernelLaunch {
    pub kernel_name: String,
    pub grid_size: (u32, u32, u32),
    pub block_size: (u32, u32, u32),
    pub shared_memory_bytes: u32,
    pub registers_per_thread: u32,
    pub start_time_us: u64,
    pub duration_us: u64,
    pub occupancy: f32,
    pub wave_count: u32,
}

/// ROCm memory operation information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ROCmMemoryOperation {
    pub operation_type: String, // "HtoD", "DtoH", "DtoD"
    pub size_bytes: usize,
    pub start_time_us: u64,
    pub duration_us: u64,
    pub bandwidth_gbps: f64,
    pub source_location: Option<String>,
}

/// HIP API call information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HIPAPICall {
    pub function_name: String,
    pub start_time_us: u64,
    pub duration_us: u64,
    pub return_code: i32,
    pub parameters: HashMap<String, String>,
}

/// CodeXL profiler for comprehensive analysis
pub struct CodeXLProfiler {
    enabled: bool,
    gpu_counters: Vec<GPUCounter>,
    cpu_samples: Vec<CPUSample>,
    power_samples: Vec<AMDPowerSample>,
}

/// GPU performance counter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GPUCounter {
    pub counter_name: String,
    pub value: u64,
    pub timestamp_us: u64,
    pub unit: String,
}

/// CPU profiling sample
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CPUSample {
    pub thread_id: u64,
    pub instruction_pointer: u64,
    pub timestamp_us: u64,
    pub cpu_id: u32,
    pub call_stack: Vec<String>,
}

/// AMD Power profiling sample
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AMDPowerSample {
    pub component: String, // "GPU", "CPU", "Memory", etc.
    pub power_watts: f64,
    pub temperature_celsius: f64,
    pub voltage_volts: f64,
    pub frequency_mhz: f64,
    pub timestamp_us: u64,
}

/// AMD uProf CPU profiler
pub struct UProfProfiler {
    enabled: bool,
    sample_frequency: u32,
    hotspots: Vec<CPUHotspot>,
    cache_analysis: CacheAnalysis,
    branch_analysis: BranchAnalysis,
}

/// CPU hotspot information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CPUHotspot {
    pub function_name: String,
    pub module_name: String,
    pub source_file: Option<String>,
    pub line_number: Option<u32>,
    pub exclusive_time_ms: f64,
    pub inclusive_time_ms: f64,
    pub sample_count: u64,
    pub cpu_cycles: u64,
    pub instructions: u64,
}

/// Cache performance analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheAnalysis {
    pub l1_data_cache_misses: u64,
    pub l1_instruction_cache_misses: u64,
    pub l2_cache_misses: u64,
    pub l3_cache_misses: u64,
    pub cache_miss_rate: f64,
    pub memory_stall_cycles: u64,
}

/// Branch prediction analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchAnalysis {
    pub total_branches: u64,
    pub branch_mispredictions: u64,
    pub branch_misprediction_rate: f64,
    pub indirect_branches: u64,
    pub conditional_branches: u64,
}

/// ROCTracer for detailed GPU tracing
pub struct ROCTracer {
    enabled: bool,
    hip_traces: Vec<HIPTrace>,
    hsa_traces: Vec<HSATrace>,
    kernel_traces: Vec<KernelTrace>,
}

/// HIP API trace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HIPTrace {
    pub api_name: String,
    pub start_time_us: u64,
    pub end_time_us: u64,
    pub thread_id: u64,
    pub correlation_id: u64,
    pub arguments: HashMap<String, String>,
}

/// HSA runtime trace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HSATrace {
    pub api_name: String,
    pub start_time_us: u64,
    pub end_time_us: u64,
    pub agent_handle: u64,
    pub queue_handle: u64,
}

/// Kernel execution trace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelTrace {
    pub kernel_name: String,
    pub start_time_us: u64,
    pub end_time_us: u64,
    pub grid_size: (u32, u32, u32),
    pub workgroup_size: (u32, u32, u32),
    pub correlation_id: u64,
    pub queue_index: u32,
}

impl Default for AMDProfilerConfig {
    fn default() -> Self {
        Self {
            enable_rocm: true,
            enable_codexl: false,
            enable_uprof: false,
            enable_roctracer: false,
            gpu_device_id: 0,
            cpu_sample_frequency: 1000,
            enable_kernel_profiling: true,
            enable_memory_profiling: true,
            enable_power_profiling: false,
        }
    }
}

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

impl AMDProfiler {
    /// Create a new AMD profiler instance
    pub fn new() -> Self {
        Self {
            rocm_profiler: None,
            codexl_profiler: None,
            uprof_profiler: None,
            roctracer: None,
            enabled: false,
            config: AMDProfilerConfig::default(),
            events: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Create a new AMD profiler with custom configuration
    pub fn with_config(config: AMDProfilerConfig) -> Self {
        let mut profiler = Self::new();
        profiler.config = config;
        profiler
    }

    /// Enable AMD profiling with the current configuration
    pub fn enable(&mut self) -> TorshResult<()> {
        if self.config.enable_rocm {
            self.rocm_profiler = Some(ROCmProfiler::new(self.config.gpu_device_id)?);
        }

        if self.config.enable_codexl {
            self.codexl_profiler = Some(CodeXLProfiler::new()?);
        }

        if self.config.enable_uprof {
            self.uprof_profiler = Some(UProfProfiler::new(self.config.cpu_sample_frequency)?);
        }

        if self.config.enable_roctracer {
            self.roctracer = Some(ROCTracer::new()?);
        }

        self.enabled = true;
        Ok(())
    }

    /// Disable AMD profiling
    pub fn disable(&mut self) {
        self.enabled = false;
        self.rocm_profiler = None;
        self.codexl_profiler = None;
        self.uprof_profiler = None;
        self.roctracer = None;
    }

    /// Record a HIP kernel launch
    pub fn record_hip_kernel_launch(
        &mut self,
        kernel_name: &str,
        grid_size: (u32, u32, u32),
        block_size: (u32, u32, u32),
        shared_memory: u32,
        duration: Duration,
    ) -> TorshResult<()> {
        if !self.enabled {
            return Ok(());
        }

        if let Some(ref mut rocm) = self.rocm_profiler {
            rocm.record_kernel_launch(kernel_name, grid_size, block_size, shared_memory, duration)?;
        }

        // Add to global events
        let mut events = self.events.lock();
        events.push(ProfileEvent {
            name: format!("HIP::{kernel_name}"),
            category: "hip_kernel".to_string(),
            start_us: 0, // Would be set from actual timing
            duration_us: duration.as_micros() as u64,
            thread_id: 0,
            operation_count: Some(1),
            flops: Some(0),             // Would be calculated based on kernel analysis
            bytes_transferred: Some(0), // Would be calculated based on memory access
            stack_trace: None,
        });

        Ok(())
    }

    /// Record a HIP memory operation
    pub fn record_hip_memory_operation(
        &mut self,
        operation_type: &str,
        size_bytes: usize,
        duration: Duration,
    ) -> TorshResult<()> {
        if !self.enabled {
            return Ok(());
        }

        if let Some(ref mut rocm) = self.rocm_profiler {
            rocm.record_memory_operation(operation_type, size_bytes, duration)?;
        }

        // Calculate bandwidth
        let _bandwidth_gbps =
            (size_bytes as f64) / (1024.0 * 1024.0 * 1024.0) / duration.as_secs_f64();

        // Add to global events
        let mut events = self.events.lock();
        events.push(ProfileEvent {
            name: format!("HIP::Memory::{operation_type}"),
            category: "hip_memory".to_string(),
            start_us: 0,
            duration_us: duration.as_micros() as u64,
            thread_id: 0,
            operation_count: Some(1),
            flops: Some(0),
            bytes_transferred: Some(size_bytes as u64),
            stack_trace: None,
        });

        Ok(())
    }

    /// Start CPU profiling with uProf
    pub fn start_cpu_profiling(&mut self) -> TorshResult<()> {
        if let Some(ref mut uprof) = self.uprof_profiler {
            uprof.start_profiling()?;
        }
        Ok(())
    }

    /// Stop CPU profiling and collect results
    pub fn stop_cpu_profiling(&mut self) -> TorshResult<Vec<CPUHotspot>> {
        if let Some(ref mut uprof) = self.uprof_profiler {
            return uprof.stop_profiling();
        }
        Ok(Vec::new())
    }

    /// Get ROCm profiling statistics
    pub fn get_rocm_stats(&self) -> Option<ROCmStats> {
        self.rocm_profiler.as_ref().map(|rocm| rocm.get_stats())
    }

    /// Get all collected events
    pub fn get_events(&self) -> Vec<ProfileEvent> {
        self.events.lock().clone()
    }

    /// Export AMD profiling data to JSON
    pub fn export_data(&self, filename: &str) -> TorshResult<()> {
        let data = AMDProfilingData {
            rocm_kernels: self
                .rocm_profiler
                .as_ref()
                .map(|r| r.kernel_launches.clone())
                .unwrap_or_default(),
            rocm_memory_ops: self
                .rocm_profiler
                .as_ref()
                .map(|r| r.memory_operations.clone())
                .unwrap_or_default(),
            cpu_hotspots: self
                .uprof_profiler
                .as_ref()
                .map(|u| u.hotspots.clone())
                .unwrap_or_default(),
            gpu_counters: self
                .codexl_profiler
                .as_ref()
                .map(|c| c.gpu_counters.clone())
                .unwrap_or_default(),
            power_samples: self
                .codexl_profiler
                .as_ref()
                .map(|c| c.power_samples.clone())
                .unwrap_or_default(),
        };

        let json = serde_json::to_string_pretty(&data)
            .map_err(|e| TorshError::SerializationError(e.to_string()))?;

        std::fs::write(filename, json).map_err(|e| TorshError::IoError(e.to_string()))?;

        Ok(())
    }
}

/// ROCm profiling statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ROCmStats {
    pub total_kernel_launches: usize,
    pub total_kernel_time_us: u64,
    pub average_kernel_time_us: f64,
    pub total_memory_operations: usize,
    pub total_bytes_transferred: u64,
    pub average_bandwidth_gbps: f64,
    pub peak_memory_bandwidth_gbps: f64,
    pub gpu_utilization_percent: f64,
}

/// Complete AMD profiling data for export
#[derive(Debug, Serialize, Deserialize)]
pub struct AMDProfilingData {
    pub rocm_kernels: Vec<ROCmKernelLaunch>,
    pub rocm_memory_ops: Vec<ROCmMemoryOperation>,
    pub cpu_hotspots: Vec<CPUHotspot>,
    pub gpu_counters: Vec<GPUCounter>,
    pub power_samples: Vec<AMDPowerSample>,
}

impl ROCmProfiler {
    fn new(device_id: u32) -> TorshResult<Self> {
        Ok(Self {
            device_id,
            enabled: true,
            kernel_launches: Vec::new(),
            memory_operations: Vec::new(),
            hip_api_calls: Vec::new(),
        })
    }

    fn record_kernel_launch(
        &mut self,
        kernel_name: &str,
        grid_size: (u32, u32, u32),
        block_size: (u32, u32, u32),
        shared_memory: u32,
        duration: Duration,
    ) -> TorshResult<()> {
        let launch = ROCmKernelLaunch {
            kernel_name: kernel_name.to_string(),
            grid_size,
            block_size,
            shared_memory_bytes: shared_memory,
            registers_per_thread: 64, // Would be obtained from kernel analysis
            start_time_us: 0,         // Would be set from actual timing
            duration_us: duration.as_micros() as u64,
            occupancy: self.calculate_occupancy(grid_size, block_size),
            wave_count: self.calculate_wave_count(grid_size, block_size),
        };

        self.kernel_launches.push(launch);
        Ok(())
    }

    fn record_memory_operation(
        &mut self,
        operation_type: &str,
        size_bytes: usize,
        duration: Duration,
    ) -> TorshResult<()> {
        let bandwidth_gbps =
            (size_bytes as f64) / (1024.0 * 1024.0 * 1024.0) / duration.as_secs_f64();

        let operation = ROCmMemoryOperation {
            operation_type: operation_type.to_string(),
            size_bytes,
            start_time_us: 0, // Would be set from actual timing
            duration_us: duration.as_micros() as u64,
            bandwidth_gbps,
            source_location: None, // Would be filled by debug info
        };

        self.memory_operations.push(operation);
        Ok(())
    }

    fn calculate_occupancy(&self, _grid_size: (u32, u32, u32), block_size: (u32, u32, u32)) -> f32 {
        // Simplified occupancy calculation
        let threads_per_block = block_size.0 * block_size.1 * block_size.2;
        let max_threads_per_cu = 2560; // Typical for RDNA/GCN
        (threads_per_block as f32 / max_threads_per_cu as f32).min(1.0)
    }

    fn calculate_wave_count(&self, grid_size: (u32, u32, u32), block_size: (u32, u32, u32)) -> u32 {
        let total_threads =
            grid_size.0 * grid_size.1 * grid_size.2 * block_size.0 * block_size.1 * block_size.2;
        total_threads.div_ceil(64) // 64 threads per wavefront on AMD
    }

    fn get_stats(&self) -> ROCmStats {
        let total_kernel_time: u64 = self.kernel_launches.iter().map(|k| k.duration_us).sum();

        let total_bytes: u64 = self
            .memory_operations
            .iter()
            .map(|m| m.size_bytes as u64)
            .sum();

        let total_memory_time: f64 = self
            .memory_operations
            .iter()
            .map(|m| m.duration_us as f64 / 1_000_000.0)
            .sum();

        ROCmStats {
            total_kernel_launches: self.kernel_launches.len(),
            total_kernel_time_us: total_kernel_time,
            average_kernel_time_us: if !self.kernel_launches.is_empty() {
                total_kernel_time as f64 / self.kernel_launches.len() as f64
            } else {
                0.0
            },
            total_memory_operations: self.memory_operations.len(),
            total_bytes_transferred: total_bytes,
            average_bandwidth_gbps: if total_memory_time > 0.0 {
                (total_bytes as f64) / (1024.0 * 1024.0 * 1024.0) / total_memory_time
            } else {
                0.0
            },
            peak_memory_bandwidth_gbps: self
                .memory_operations
                .iter()
                .map(|m| m.bandwidth_gbps)
                .fold(0.0, f64::max),
            gpu_utilization_percent: 85.0, // Would be calculated from actual measurements
        }
    }
}

impl CodeXLProfiler {
    fn new() -> TorshResult<Self> {
        Ok(Self {
            enabled: true,
            gpu_counters: Vec::new(),
            cpu_samples: Vec::new(),
            power_samples: Vec::new(),
        })
    }
}

impl UProfProfiler {
    fn new(sample_frequency: u32) -> TorshResult<Self> {
        Ok(Self {
            enabled: true,
            sample_frequency,
            hotspots: Vec::new(),
            cache_analysis: CacheAnalysis {
                l1_data_cache_misses: 0,
                l1_instruction_cache_misses: 0,
                l2_cache_misses: 0,
                l3_cache_misses: 0,
                cache_miss_rate: 0.0,
                memory_stall_cycles: 0,
            },
            branch_analysis: BranchAnalysis {
                total_branches: 0,
                branch_mispredictions: 0,
                branch_misprediction_rate: 0.0,
                indirect_branches: 0,
                conditional_branches: 0,
            },
        })
    }

    fn start_profiling(&mut self) -> TorshResult<()> {
        // In a real implementation, this would start uProf collection
        Ok(())
    }

    fn stop_profiling(&mut self) -> TorshResult<Vec<CPUHotspot>> {
        // In a real implementation, this would stop uProf and collect results
        Ok(self.hotspots.clone())
    }
}

impl ROCTracer {
    fn new() -> TorshResult<Self> {
        Ok(Self {
            enabled: true,
            hip_traces: Vec::new(),
            hsa_traces: Vec::new(),
            kernel_traces: Vec::new(),
        })
    }
}

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

    #[test]
    fn test_amd_profiler_creation() {
        let profiler = AMDProfiler::new();
        assert!(!profiler.enabled);
    }

    #[test]
    fn test_amd_profiler_with_config() {
        let config = AMDProfilerConfig {
            enable_rocm: true,
            enable_uprof: true,
            gpu_device_id: 1,
            cpu_sample_frequency: 2000,
            ..Default::default()
        };

        let profiler = AMDProfiler::with_config(config.clone());
        assert_eq!(profiler.config.gpu_device_id, 1);
        assert_eq!(profiler.config.cpu_sample_frequency, 2000);
    }

    #[test]
    fn test_rocm_occupancy_calculation() -> TorshResult<()> {
        let rocm = ROCmProfiler::new(0)?;
        let occupancy = rocm.calculate_occupancy((32, 32, 1), (16, 16, 1));
        assert!(occupancy > 0.0 && occupancy <= 1.0);
        Ok(())
    }

    #[test]
    fn test_wave_count_calculation() -> TorshResult<()> {
        let rocm = ROCmProfiler::new(0)?;
        let wave_count = rocm.calculate_wave_count((1, 1, 1), (64, 1, 1));
        assert_eq!(wave_count, 1); // Exactly one wavefront

        let wave_count = rocm.calculate_wave_count((1, 1, 1), (128, 1, 1));
        assert_eq!(wave_count, 2); // Two wavefronts
        Ok(())
    }
}