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
//! Core profiling metrics and analysis types

/// Overhead statistics for profiling operations
#[derive(Debug, Clone, Default)]
pub struct OverheadStats {
    pub add_event_time_ns: u64,
    pub add_event_count: u64,
    pub stack_trace_time_ns: u64,
    pub stack_trace_count: u64,
    pub export_time_ns: u64,
    pub export_count: u64,
    pub total_overhead_ns: u64,
}

/// Bottleneck detection results
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BottleneckAnalysis {
    pub slowest_operations: Vec<BottleneckEvent>,
    pub memory_hotspots: Vec<MemoryHotspot>,
    pub thread_contention: Vec<ThreadContentionEvent>,
    pub efficiency_issues: Vec<EfficiencyIssue>,
    pub recommendations: Vec<String>,
}

/// A performance bottleneck event
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BottleneckEvent {
    pub name: String,
    pub category: String,
    pub duration_us: u64,
    pub thread_id: usize,
    pub severity: BottleneckSeverity,
    pub impact_score: f64,
    pub recommendation: String,
}

/// Memory hotspot information
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MemoryHotspot {
    pub location: String,
    pub total_allocations: usize,
    pub total_bytes: usize,
    pub average_size: f64,
    pub peak_concurrent_allocations: usize,
    pub severity: BottleneckSeverity,
}

/// Thread contention event
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ThreadContentionEvent {
    pub thread_id: usize,
    pub operation: String,
    pub wait_time_us: u64,
    pub contention_count: usize,
}

/// Efficiency issue
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct EfficiencyIssue {
    pub issue_type: EfficiencyIssueType,
    pub description: String,
    pub affected_operations: Vec<String>,
    pub performance_impact: f64,
    pub recommendation: String,
}

/// Type of efficiency issue
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum EfficiencyIssueType {
    LowThroughput,
    HighLatency,
    MemoryWaste,
    CpuUnderutilization,
    FrequentAllocation,
    LargeAllocation,
}

/// Severity of a bottleneck
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
pub enum BottleneckSeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Efficiency metrics analysis
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct EfficiencyMetrics {
    pub cpu_efficiency: CpuEfficiency,
    pub memory_efficiency: MemoryEfficiency,
    pub cache_efficiency: CacheEfficiency,
    pub throughput_metrics: ThroughputMetrics,
    pub resource_utilization: ResourceUtilization,
    pub overall_score: f64,
    pub recommendations: Vec<String>,
}

/// CPU efficiency metrics
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CpuEfficiency {
    pub utilization_percentage: f64,
    pub instructions_per_cycle: f64,
    pub computational_intensity: f64,
    pub parallelism_efficiency: f64,
    pub idle_time_percentage: f64,
}

/// Memory efficiency metrics
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MemoryEfficiency {
    pub bandwidth_utilization: f64,
    pub cache_hit_ratio: f64,
    pub memory_access_pattern: MemoryAccessPattern,
    pub allocation_efficiency: f64,
    pub fragmentation_ratio: f64,
}

/// Cache efficiency metrics
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CacheEfficiency {
    pub l1_hit_rate: f64,
    pub l2_hit_rate: f64,
    pub l3_hit_rate: f64,
    pub memory_locality_score: f64,
    pub cache_miss_penalty: f64,
}

/// Throughput metrics
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ThroughputMetrics {
    pub operations_per_second: f64,
    pub flops_per_second: f64,
    pub bandwidth_gb_per_second: f64,
    pub latency_percentiles: LatencyPercentiles,
    pub throughput_efficiency: f64,
}

/// Resource utilization metrics
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ResourceUtilization {
    pub cpu_cores_used: usize,
    pub memory_usage_mb: f64,
    pub peak_memory_mb: f64,
    pub thread_efficiency: f64,
    pub load_balance_score: f64,
}

/// Latency percentiles
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LatencyPercentiles {
    pub p50: f64,
    pub p90: f64,
    pub p95: f64,
    pub p99: f64,
    pub max: f64,
}

/// Memory access pattern
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum MemoryAccessPattern {
    Sequential,
    Random,
    Strided,
    Mixed,
}

/// Correlation analysis results
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CorrelationAnalysis {
    pub operation_correlations: Vec<OperationCorrelation>,
    pub performance_correlations: Vec<PerformanceCorrelation>,
    pub memory_correlations: Vec<MemoryCorrelation>,
    pub temporal_correlations: Vec<TemporalCorrelation>,
    pub correlation_summary: CorrelationSummary,
}

/// Correlation between two operations
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct OperationCorrelation {
    pub operation_a: String,
    pub operation_b: String,
    pub correlation_coefficient: f64,
    pub co_occurrence_frequency: f64,
    pub temporal_proximity: f64,
    pub correlation_strength: CorrelationStrength,
    pub correlation_type: CorrelationType,
    pub insights: Vec<String>,
}

/// Performance metric correlation
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PerformanceCorrelation {
    pub metric_a: String,
    pub metric_b: String,
    pub correlation_coefficient: f64,
    pub significance_level: f64,
    pub sample_size: usize,
    pub correlation_strength: CorrelationStrength,
}

/// Memory usage correlation
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MemoryCorrelation {
    pub operation: String,
    pub memory_metric: String,
    pub duration_correlation: f64,
    pub bytes_correlation: f64,
    pub allocation_pattern: String,
    pub correlation_strength: CorrelationStrength,
}

/// Temporal relationship between operations
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TemporalCorrelation {
    pub operation_sequence: Vec<String>,
    pub sequence_frequency: f64,
    pub average_interval: f64,
    pub sequence_efficiency: f64,
    pub optimization_potential: f64,
}

/// Correlation strength classification
#[derive(Debug, Clone, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub enum CorrelationStrength {
    VeryWeak,   // |r| < 0.2
    Weak,       // 0.2 <= |r| < 0.4
    Moderate,   // 0.4 <= |r| < 0.6
    Strong,     // 0.6 <= |r| < 0.8
    VeryStrong, // |r| >= 0.8
}

impl std::fmt::Display for CorrelationStrength {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CorrelationStrength::VeryWeak => write!(f, "Very Weak"),
            CorrelationStrength::Weak => write!(f, "Weak"),
            CorrelationStrength::Moderate => write!(f, "Moderate"),
            CorrelationStrength::Strong => write!(f, "Strong"),
            CorrelationStrength::VeryStrong => write!(f, "Very Strong"),
        }
    }
}

/// Type of correlation
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum CorrelationType {
    Causal,        // One operation directly affects another
    Complementary, // Operations work together
    Competitive,   // Operations compete for resources
    Sequential,    // Operations follow in sequence
    Independent,   // No significant relationship
}

/// Summary of correlation analysis
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CorrelationSummary {
    pub total_correlations_analyzed: usize,
    pub strong_correlations_found: usize,
    pub causal_relationships: usize,
    pub bottleneck_correlations: usize,
    pub optimization_opportunities: Vec<String>,
    pub key_insights: Vec<String>,
}

/// Pattern detection analysis results
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PatternAnalysis {
    pub performance_patterns: Vec<PerformancePattern>,
    pub bottleneck_patterns: Vec<BottleneckPattern>,
    pub resource_patterns: Vec<ResourcePattern>,
    pub temporal_patterns: Vec<TemporalPattern>,
    pub optimization_patterns: Vec<OptimizationPattern>,
    pub pattern_summary: PatternSummary,
}

/// Recurring performance pattern
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PerformancePattern {
    pub pattern_type: PerformancePatternType,
    pub operations: Vec<String>,
    pub frequency: f64,
    pub average_duration: f64,
    pub variance: f64,
    pub confidence_score: f64,
    pub impact_level: PatternImpact,
    pub description: String,
}

/// Type of performance pattern
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum PerformancePatternType {
    RegularCycle,       // Recurring operations with regular intervals
    BurstActivity,      // Periods of high activity followed by low activity
    GradualDegradation, // Performance gradually getting worse
    SpikesAndDips,      // Irregular spikes and dips in performance
    ConstantLoad,       // Steady, consistent performance
    Oscillation,        // Performance oscillates between states
}

impl std::fmt::Display for PerformancePatternType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PerformancePatternType::RegularCycle => write!(f, "Regular Cycle"),
            PerformancePatternType::BurstActivity => write!(f, "Burst Activity"),
            PerformancePatternType::GradualDegradation => write!(f, "Gradual Degradation"),
            PerformancePatternType::SpikesAndDips => write!(f, "Spikes and Dips"),
            PerformancePatternType::ConstantLoad => write!(f, "Constant Load"),
            PerformancePatternType::Oscillation => write!(f, "Oscillation"),
        }
    }
}

/// Bottleneck pattern
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BottleneckPattern {
    pub pattern_id: String,
    pub bottleneck_operations: Vec<String>,
    pub occurrence_frequency: f64,
    pub average_severity: f64,
    pub blocking_relationships: Vec<BlockingRelationship>,
    pub root_cause_analysis: RootCauseAnalysis,
    pub mitigation_strategies: Vec<String>,
}

/// Resource usage pattern
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ResourcePattern {
    pub resource_type: ResourceType,
    pub usage_pattern: UsagePatternType,
    pub peak_usage_times: Vec<u64>,
    pub utilization_efficiency: f64,
    pub waste_indicators: Vec<WasteIndicator>,
    pub optimization_potential: f64,
}

/// Temporal execution pattern
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TemporalPattern {
    pub pattern_name: String,
    pub operation_sequence: Vec<String>,
    pub sequence_probability: f64,
    pub execution_consistency: f64,
    pub timing_variance: f64,
    pub parallelization_opportunities: Vec<ParallelizationOpportunity>,
}

/// Optimization opportunity pattern
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct OptimizationPattern {
    pub optimization_type: OptimizationType,
    pub affected_operations: Vec<String>,
    pub potential_improvement: f64,
    pub implementation_complexity: ComplexityLevel,
    pub confidence_level: f64,
    pub prerequisites: Vec<String>,
    pub estimated_effort: EffortLevel,
}

/// Type of resource
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum ResourceType {
    Cpu,
    Memory,
    Bandwidth,
    Storage,
    Network,
    Compute,
}

/// Usage pattern type
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum UsagePatternType {
    Steady,
    Bursty,
    Cyclical,
    Growing,
    Declining,
    Random,
}

/// Waste indicator
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct WasteIndicator {
    pub indicator_type: WasteType,
    pub severity: f64,
    pub description: String,
    pub recommendations: Vec<String>,
}

/// Type of waste
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum WasteType {
    IdleTime,
    MemoryLeaks,
    RedundantOperations,
    InefficientAlgorithm,
    PoorCacheUsage,
    ExcessiveAllocation,
}

/// Blocking relationship between operations
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BlockingRelationship {
    pub blocker: String,
    pub blocked: String,
    pub blocking_duration: f64,
    pub frequency: f64,
    pub impact_score: f64,
}

/// Root cause analysis
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RootCauseAnalysis {
    pub primary_cause: String,
    pub contributing_factors: Vec<String>,
    pub evidence_strength: f64,
    pub analysis_confidence: f64,
}

/// Parallelization opportunity
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ParallelizationOpportunity {
    pub operations: Vec<String>,
    pub parallel_potential: f64,
    pub data_dependencies: Vec<String>,
    pub expected_speedup: f64,
}

/// Type of optimization
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum OptimizationType {
    Parallelization,
    Vectorization,
    CacheOptimization,
    MemoryPooling,
    AlgorithmChange,
    DataStructureOptimization,
    ConcurrencyImprovement,
}

impl std::fmt::Display for OptimizationType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OptimizationType::Parallelization => write!(f, "Parallelization"),
            OptimizationType::Vectorization => write!(f, "Vectorization"),
            OptimizationType::CacheOptimization => write!(f, "Cache Optimization"),
            OptimizationType::MemoryPooling => write!(f, "Memory Pooling"),
            OptimizationType::AlgorithmChange => write!(f, "Algorithm Change"),
            OptimizationType::DataStructureOptimization => write!(f, "Data Structure Optimization"),
            OptimizationType::ConcurrencyImprovement => write!(f, "Concurrency Improvement"),
        }
    }
}

/// Pattern impact level
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
pub enum PatternImpact {
    Negligible,
    Low,
    Medium,
    High,
    Critical,
}

/// Implementation complexity
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
pub enum ComplexityLevel {
    Trivial,
    Low,
    Medium,
    High,
    VeryHigh,
}

/// Implementation effort level
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
pub enum EffortLevel {
    Minimal,
    Low,
    Medium,
    High,
    Significant,
}

/// Summary of pattern detection
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PatternSummary {
    pub total_patterns_detected: usize,
    pub critical_patterns: usize,
    pub optimization_opportunities: usize,
    pub bottleneck_patterns: usize,
    pub key_recommendations: Vec<String>,
    pub pattern_confidence: f64,
    pub analysis_completeness: f64,
}