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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
//! Thermal analysis system for performance profiling
//!
//! This module provides comprehensive thermal monitoring and analysis capabilities
//! including temperature tracking, thermal throttling detection, and thermal-aware
//! performance optimization recommendations.

use crate::ProfileEvent;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::time::{Duration, SystemTime};

/// Thermal sensors types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Hash, Eq)]
pub enum ThermalSensor {
    CpuCore(u32),
    CpuPackage,
    GpuCore,
    GpuMemory,
    Motherboard,
    Memory,
    Storage,
    Ambient,
    Custom(String),
}

impl std::fmt::Display for ThermalSensor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ThermalSensor::CpuCore(id) => write!(f, "CPU Core {id}"),
            ThermalSensor::CpuPackage => write!(f, "CPU Package"),
            ThermalSensor::GpuCore => write!(f, "GPU Core"),
            ThermalSensor::GpuMemory => write!(f, "GPU Memory"),
            ThermalSensor::Motherboard => write!(f, "Motherboard"),
            ThermalSensor::Memory => write!(f, "Memory"),
            ThermalSensor::Storage => write!(f, "Storage"),
            ThermalSensor::Ambient => write!(f, "Ambient"),
            ThermalSensor::Custom(name) => write!(f, "{name}"),
        }
    }
}

/// Temperature measurement units
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum TemperatureUnit {
    Celsius,
    Fahrenheit,
    Kelvin,
}

impl std::fmt::Display for TemperatureUnit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TemperatureUnit::Celsius => write!(f, "°C"),
            TemperatureUnit::Fahrenheit => write!(f, "°F"),
            TemperatureUnit::Kelvin => write!(f, "K"),
        }
    }
}

/// Temperature measurement sample
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemperatureSample {
    pub timestamp: SystemTime,
    pub sensor: ThermalSensor,
    pub temperature: f64,
    pub unit: TemperatureUnit,
    pub raw_value: Option<u64>,
    pub critical_temp: Option<f64>,
    pub max_temp: Option<f64>,
}

/// Thermal event types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ThermalEvent {
    ThrottlingStart {
        sensor: ThermalSensor,
        temperature: f64,
        threshold: f64,
    },
    ThrottlingEnd {
        sensor: ThermalSensor,
        temperature: f64,
    },
    CriticalTemperature {
        sensor: ThermalSensor,
        temperature: f64,
        critical_threshold: f64,
    },
    RapidTemperatureRise {
        sensor: ThermalSensor,
        rate_per_second: f64,
        threshold: f64,
    },
    ThermalShutdown {
        sensor: ThermalSensor,
        temperature: f64,
    },
}

/// Thermal throttling information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThrottlingInfo {
    pub sensor: ThermalSensor,
    pub start_time: SystemTime,
    pub end_time: Option<SystemTime>,
    pub max_temperature: f64,
    pub duration: Option<Duration>,
    pub performance_impact: f64, // Percentage
    pub frequency_reduction: Option<f64>,
}

/// Thermal statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThermalStats {
    pub sensor: ThermalSensor,
    pub sample_count: u64,
    pub min_temperature: f64,
    pub max_temperature: f64,
    pub average_temperature: f64,
    pub temperature_variance: f64,
    pub time_above_threshold: Duration,
    pub throttling_events: u64,
    pub critical_events: u64,
    pub temperature_slope: f64, // Rate of change per second
}

/// Thermal analysis configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThermalConfig {
    pub enabled_sensors: Vec<ThermalSensor>,
    pub sampling_rate_hz: f64,
    pub throttling_threshold: f64,
    pub critical_threshold: f64,
    pub rapid_rise_threshold: f64, // °C per second
    pub enable_cpu_sensors: bool,
    pub enable_gpu_sensors: bool,
    pub enable_system_sensors: bool,
    pub enable_hwmon: bool,     // Linux hardware monitoring
    pub enable_coretemp: bool,  // Intel Core temperature
    pub enable_k10temp: bool,   // AMD temperature
    pub enable_nvidia_ml: bool, // NVIDIA GPU temperature
    pub enable_amd_gpu: bool,   // AMD GPU temperature
    pub temperature_unit: TemperatureUnit,
}

impl Default for ThermalConfig {
    fn default() -> Self {
        Self {
            enabled_sensors: vec![
                ThermalSensor::CpuPackage,
                ThermalSensor::GpuCore,
                ThermalSensor::Ambient,
            ],
            sampling_rate_hz: 5.0,      // 5 Hz sampling
            throttling_threshold: 85.0, // °C
            critical_threshold: 95.0,   // °C
            rapid_rise_threshold: 5.0,  // °C/s
            enable_cpu_sensors: true,
            enable_gpu_sensors: true,
            enable_system_sensors: true,
            enable_hwmon: true,
            enable_coretemp: true,
            enable_k10temp: true,
            enable_nvidia_ml: true,
            enable_amd_gpu: true,
            temperature_unit: TemperatureUnit::Celsius,
        }
    }
}

/// Thermal profiler implementation
pub struct ThermalProfiler {
    config: ThermalConfig,
    samples: Vec<TemperatureSample>,
    events: Vec<ThermalEvent>,
    throttling_sessions: Vec<ThrottlingInfo>,
    hwmon_monitor: Option<HwmonMonitor>,
    coretemp_monitor: Option<CoretempMonitor>,
    k10temp_monitor: Option<K10tempMonitor>,
    nvidia_monitor: Option<NvidiaThermalMonitor>,
    amd_gpu_monitor: Option<AmdGpuThermalMonitor>,
    last_sample_time: Option<SystemTime>,
    active_throttling: HashMap<ThermalSensor, ThrottlingInfo>,
}

impl ThermalProfiler {
    pub fn new(config: ThermalConfig) -> Result<Self> {
        let hwmon_monitor = if config.enable_hwmon {
            Some(HwmonMonitor::new()?)
        } else {
            None
        };

        let coretemp_monitor = if config.enable_coretemp {
            Some(CoretempMonitor::new()?)
        } else {
            None
        };

        let k10temp_monitor = if config.enable_k10temp {
            Some(K10tempMonitor::new()?)
        } else {
            None
        };

        let nvidia_monitor = if config.enable_nvidia_ml {
            Some(NvidiaThermalMonitor::new()?)
        } else {
            None
        };

        let amd_gpu_monitor = if config.enable_amd_gpu {
            Some(AmdGpuThermalMonitor::new()?)
        } else {
            None
        };

        Ok(Self {
            config,
            samples: Vec::new(),
            events: Vec::new(),
            throttling_sessions: Vec::new(),
            hwmon_monitor,
            coretemp_monitor,
            k10temp_monitor,
            nvidia_monitor,
            amd_gpu_monitor,
            last_sample_time: None,
            active_throttling: HashMap::new(),
        })
    }

    /// Start thermal monitoring
    pub fn start_monitoring(&mut self) -> Result<()> {
        if let Some(hwmon) = &mut self.hwmon_monitor {
            hwmon.start()?;
        }
        if let Some(coretemp) = &mut self.coretemp_monitor {
            coretemp.start()?;
        }
        if let Some(k10temp) = &mut self.k10temp_monitor {
            k10temp.start()?;
        }
        if let Some(nvidia) = &mut self.nvidia_monitor {
            nvidia.start()?;
        }
        if let Some(amd_gpu) = &mut self.amd_gpu_monitor {
            amd_gpu.start()?;
        }

        self.last_sample_time = Some(SystemTime::now());
        Ok(())
    }

    /// Stop thermal monitoring
    pub fn stop_monitoring(&mut self) -> Result<()> {
        if let Some(hwmon) = &mut self.hwmon_monitor {
            hwmon.stop()?;
        }
        if let Some(coretemp) = &mut self.coretemp_monitor {
            coretemp.stop()?;
        }
        if let Some(k10temp) = &mut self.k10temp_monitor {
            k10temp.stop()?;
        }
        if let Some(nvidia) = &mut self.nvidia_monitor {
            nvidia.stop()?;
        }
        if let Some(amd_gpu) = &mut self.amd_gpu_monitor {
            amd_gpu.stop()?;
        }

        // End any active throttling sessions
        for (sensor, mut throttling) in self.active_throttling.drain() {
            throttling.end_time = Some(SystemTime::now());
            throttling.duration = throttling
                .end_time
                .and_then(|end| end.duration_since(throttling.start_time).ok());
            self.throttling_sessions.push(throttling);

            self.events.push(ThermalEvent::ThrottlingEnd {
                sensor,
                temperature: 0.0, // Would need current temperature
            });
        }

        Ok(())
    }

    /// Collect temperature samples from all enabled monitors
    pub fn collect_samples(&mut self) -> Result<Vec<TemperatureSample>> {
        let mut new_samples = Vec::new();
        let timestamp = SystemTime::now();

        // Check if enough time has passed since last sample
        if let Some(last_time) = self.last_sample_time {
            let elapsed = timestamp
                .duration_since(last_time)
                .unwrap_or(Duration::ZERO);
            let sample_interval = Duration::from_secs_f64(1.0 / self.config.sampling_rate_hz);

            if elapsed < sample_interval {
                return Ok(new_samples);
            }
        }

        // Collect from all monitors
        if let Some(hwmon) = &self.hwmon_monitor {
            new_samples.extend(hwmon.get_samples(timestamp)?);
        }
        if let Some(coretemp) = &self.coretemp_monitor {
            new_samples.extend(coretemp.get_samples(timestamp)?);
        }
        if let Some(k10temp) = &self.k10temp_monitor {
            new_samples.extend(k10temp.get_samples(timestamp)?);
        }
        if let Some(nvidia) = &self.nvidia_monitor {
            new_samples.extend(nvidia.get_samples(timestamp)?);
        }
        if let Some(amd_gpu) = &self.amd_gpu_monitor {
            new_samples.extend(amd_gpu.get_samples(timestamp)?);
        }

        // Filter samples based on enabled sensors
        new_samples.retain(|sample| self.config.enabled_sensors.contains(&sample.sensor));

        // Process samples for thermal events
        for sample in &new_samples {
            self.process_thermal_sample(sample)?;
        }

        self.samples.extend(new_samples.clone());
        self.last_sample_time = Some(timestamp);

        Ok(new_samples)
    }

    fn process_thermal_sample(&mut self, sample: &TemperatureSample) -> Result<()> {
        // Check for throttling threshold
        if sample.temperature >= self.config.throttling_threshold {
            if !self.active_throttling.contains_key(&sample.sensor) {
                // Start new throttling session
                let throttling = ThrottlingInfo {
                    sensor: sample.sensor.clone(),
                    start_time: sample.timestamp,
                    end_time: None,
                    max_temperature: sample.temperature,
                    duration: None,
                    performance_impact: 0.0,
                    frequency_reduction: None,
                };

                self.active_throttling
                    .insert(sample.sensor.clone(), throttling);

                self.events.push(ThermalEvent::ThrottlingStart {
                    sensor: sample.sensor.clone(),
                    temperature: sample.temperature,
                    threshold: self.config.throttling_threshold,
                });
            } else {
                // Update existing throttling session
                if let Some(throttling) = self.active_throttling.get_mut(&sample.sensor) {
                    throttling.max_temperature = throttling.max_temperature.max(sample.temperature);
                }
            }
        } else {
            // Check if throttling should end
            if let Some(mut throttling) = self.active_throttling.remove(&sample.sensor) {
                throttling.end_time = Some(sample.timestamp);
                throttling.duration = throttling
                    .end_time
                    .and_then(|end| end.duration_since(throttling.start_time).ok());
                self.throttling_sessions.push(throttling);

                self.events.push(ThermalEvent::ThrottlingEnd {
                    sensor: sample.sensor.clone(),
                    temperature: sample.temperature,
                });
            }
        }

        // Check for critical temperature
        if sample.temperature >= self.config.critical_threshold {
            self.events.push(ThermalEvent::CriticalTemperature {
                sensor: sample.sensor.clone(),
                temperature: sample.temperature,
                critical_threshold: self.config.critical_threshold,
            });
        }

        // Check for rapid temperature rise
        if let Some(previous_sample) = self
            .samples
            .iter()
            .rev()
            .find(|s| s.sensor == sample.sensor)
        {
            let time_diff = sample
                .timestamp
                .duration_since(previous_sample.timestamp)
                .unwrap_or(Duration::from_secs(1))
                .as_secs_f64();

            if time_diff > 0.0 {
                let temp_rate = (sample.temperature - previous_sample.temperature) / time_diff;

                if temp_rate >= self.config.rapid_rise_threshold {
                    self.events.push(ThermalEvent::RapidTemperatureRise {
                        sensor: sample.sensor.clone(),
                        rate_per_second: temp_rate,
                        threshold: self.config.rapid_rise_threshold,
                    });
                }
            }
        }

        Ok(())
    }

    /// Calculate thermal statistics for a specific sensor
    pub fn calculate_thermal_stats(&self, sensor: &ThermalSensor) -> Option<ThermalStats> {
        let sensor_samples: Vec<_> = self
            .samples
            .iter()
            .filter(|s| s.sensor == *sensor)
            .collect();

        if sensor_samples.is_empty() {
            return None;
        }

        let temperatures: Vec<f64> = sensor_samples.iter().map(|s| s.temperature).collect();
        let min_temperature = temperatures.iter().copied().fold(f64::INFINITY, f64::min);
        let max_temperature = temperatures
            .iter()
            .copied()
            .fold(f64::NEG_INFINITY, f64::max);
        let average_temperature = temperatures.iter().sum::<f64>() / temperatures.len() as f64;

        // Calculate variance
        let temperature_variance = temperatures
            .iter()
            .map(|t| (t - average_temperature).powi(2))
            .sum::<f64>()
            / temperatures.len() as f64;

        // Calculate time above threshold
        let time_above_threshold = sensor_samples
            .iter()
            .filter(|s| s.temperature >= self.config.throttling_threshold)
            .count() as f64
            / self.config.sampling_rate_hz;

        // Count events
        let throttling_events = self
            .events
            .iter()
            .filter(|e| match e {
                ThermalEvent::ThrottlingStart { sensor: s, .. } => s == sensor,
                _ => false,
            })
            .count() as u64;

        let critical_events = self
            .events
            .iter()
            .filter(|e| match e {
                ThermalEvent::CriticalTemperature { sensor: s, .. } => s == sensor,
                _ => false,
            })
            .count() as u64;

        // Calculate temperature slope (rate of change)
        let temperature_slope = if sensor_samples.len() > 1 {
            let first = sensor_samples
                .first()
                .expect("sensor_samples should not be empty after length check");
            let last = sensor_samples
                .last()
                .expect("sensor_samples should not be empty after length check");
            let time_diff = last
                .timestamp
                .duration_since(first.timestamp)
                .unwrap_or(Duration::from_secs(1))
                .as_secs_f64();

            if time_diff > 0.0 {
                (last.temperature - first.temperature) / time_diff
            } else {
                0.0
            }
        } else {
            0.0
        };

        Some(ThermalStats {
            sensor: sensor.clone(),
            sample_count: sensor_samples.len() as u64,
            min_temperature,
            max_temperature,
            average_temperature,
            temperature_variance,
            time_above_threshold: Duration::from_secs_f64(time_above_threshold),
            throttling_events,
            critical_events,
            temperature_slope,
        })
    }

    /// Analyze thermal impact on performance
    pub fn analyze_thermal_performance_impact(
        &self,
        events: &[ProfileEvent],
    ) -> ThermalPerformanceAnalysis {
        let mut analysis = ThermalPerformanceAnalysis {
            total_throttling_time: Duration::ZERO,
            performance_degradation: 0.0,
            affected_operations: Vec::new(),
            thermal_correlation: HashMap::new(),
            recommendations: Vec::new(),
        };

        // Calculate total throttling time
        analysis.total_throttling_time = self
            .throttling_sessions
            .iter()
            .filter_map(|t| t.duration)
            .sum();

        // Find operations affected by thermal throttling
        for event in events {
            for throttling in &self.throttling_sessions {
                let event_time = SystemTime::UNIX_EPOCH + Duration::from_micros(event.start_us);
                if event_time >= throttling.start_time
                    && throttling.end_time.map_or(true, |end| event_time <= end)
                {
                    analysis.affected_operations.push(event.name.clone());
                }
            }
        }

        // Calculate performance degradation
        let total_events = events.len() as f64;
        let affected_events = analysis.affected_operations.len() as f64;
        analysis.performance_degradation = if total_events > 0.0 {
            (affected_events / total_events) * 100.0
        } else {
            0.0
        };

        // Generate recommendations
        if analysis.total_throttling_time.as_secs() > 0 {
            analysis
                .recommendations
                .push("Consider improving cooling system".to_string());
            analysis
                .recommendations
                .push("Reduce workload intensity during high temperature periods".to_string());
        }

        if analysis.performance_degradation > 10.0 {
            analysis
                .recommendations
                .push("Implement thermal-aware scheduling".to_string());
            analysis
                .recommendations
                .push("Consider hardware upgrades for better thermal management".to_string());
        }

        analysis
    }

    /// Export thermal data to CSV
    pub fn export_csv(&self, path: &str) -> Result<()> {
        let mut csv = String::new();
        csv.push_str("timestamp,sensor,temperature_c,critical_temp,max_temp\n");

        for sample in &self.samples {
            let temp_c = match sample.unit {
                TemperatureUnit::Celsius => sample.temperature,
                TemperatureUnit::Fahrenheit => (sample.temperature - 32.0) * 5.0 / 9.0,
                TemperatureUnit::Kelvin => sample.temperature - 273.15,
            };

            csv.push_str(&format!(
                "{},{},{},{},{}\n",
                sample
                    .timestamp
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .expect("sample timestamp should be after UNIX_EPOCH")
                    .as_secs(),
                sample.sensor,
                temp_c,
                sample.critical_temp.unwrap_or(0.0),
                sample.max_temp.unwrap_or(0.0),
            ));
        }

        fs::write(path, csv)?;
        Ok(())
    }

    /// Get thermal events in a time range
    pub fn get_events_in_range(&self, start: SystemTime, end: SystemTime) -> Vec<ThermalEvent> {
        // Note: This would require timestamps in ThermalEvent for proper filtering
        self.events.clone()
    }

    /// Clear collected data
    pub fn clear_data(&mut self) {
        self.samples.clear();
        self.events.clear();
        self.throttling_sessions.clear();
        self.active_throttling.clear();
    }

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

    /// Get event count
    pub fn event_count(&self) -> usize {
        self.events.len()
    }
}

/// Thermal performance analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThermalPerformanceAnalysis {
    pub total_throttling_time: Duration,
    pub performance_degradation: f64, // Percentage
    pub affected_operations: Vec<String>,
    pub thermal_correlation: HashMap<String, f64>,
    pub recommendations: Vec<String>,
}

/// Hardware monitoring (hwmon) interface for Linux
pub struct HwmonMonitor {
    enabled: bool,
    sensor_paths: HashMap<ThermalSensor, String>,
}

impl HwmonMonitor {
    pub fn new() -> Result<Self> {
        let mut sensor_paths = HashMap::new();

        // Scan for hwmon sensors
        if let Ok(entries) = fs::read_dir("/sys/class/hwmon") {
            for entry in entries.flatten() {
                let hwmon_path = entry.path();
                if let Ok(name_content) = fs::read_to_string(hwmon_path.join("name")) {
                    let name = name_content.trim();

                    // Map known sensor names to thermal sensors
                    match name {
                        "coretemp" => {
                            // Find core temperature inputs
                            if let Ok(temp_entries) = fs::read_dir(&hwmon_path) {
                                for temp_entry in temp_entries.flatten() {
                                    let filename =
                                        temp_entry.file_name().to_string_lossy().to_string();
                                    if filename.starts_with("temp") && filename.ends_with("_input")
                                    {
                                        if let Ok(core_id) =
                                            filename[4..filename.len() - 6].parse::<u32>()
                                        {
                                            sensor_paths.insert(
                                                ThermalSensor::CpuCore(core_id),
                                                temp_entry.path().to_string_lossy().to_string(),
                                            );
                                        }
                                    }
                                }
                            }
                        }
                        "k10temp" => {
                            sensor_paths.insert(
                                ThermalSensor::CpuPackage,
                                hwmon_path.join("temp1_input").to_string_lossy().to_string(),
                            );
                        }
                        _ => {
                            // Generic sensor
                            if hwmon_path.join("temp1_input").exists() {
                                sensor_paths.insert(
                                    ThermalSensor::Custom(name.to_string()),
                                    hwmon_path.join("temp1_input").to_string_lossy().to_string(),
                                );
                            }
                        }
                    }
                }
            }
        }

        Ok(Self {
            enabled: !sensor_paths.is_empty(),
            sensor_paths,
        })
    }

    pub fn start(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn stop(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn get_samples(&self, timestamp: SystemTime) -> Result<Vec<TemperatureSample>> {
        let mut samples = Vec::new();

        if self.enabled {
            for (sensor, path) in &self.sensor_paths {
                if let Ok(temp_str) = fs::read_to_string(path) {
                    if let Ok(temp_millic) = temp_str.trim().parse::<f64>() {
                        let temp_celsius = temp_millic / 1000.0;

                        samples.push(TemperatureSample {
                            timestamp,
                            sensor: sensor.clone(),
                            temperature: temp_celsius,
                            unit: TemperatureUnit::Celsius,
                            raw_value: Some(temp_millic as u64),
                            critical_temp: None,
                            max_temp: None,
                        });
                    }
                }
            }
        }

        Ok(samples)
    }
}

/// Intel Core temperature monitor (coretemp)
pub struct CoretempMonitor {
    enabled: bool,
    core_count: usize,
}

impl CoretempMonitor {
    pub fn new() -> Result<Self> {
        let core_count = num_cpus::get();
        let enabled = Path::new("/sys/devices/platform/coretemp.0").exists();

        Ok(Self {
            enabled,
            core_count,
        })
    }

    pub fn start(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn stop(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn get_samples(&self, timestamp: SystemTime) -> Result<Vec<TemperatureSample>> {
        let mut samples = Vec::new();

        if self.enabled {
            // This is a simplified implementation
            // Real implementation would read from proper coretemp sysfs paths
            for core in 0..self.core_count {
                samples.push(TemperatureSample {
                    timestamp,
                    sensor: ThermalSensor::CpuCore(core as u32),
                    temperature: 45.0 + (core as f64 * 2.0), // Placeholder
                    unit: TemperatureUnit::Celsius,
                    raw_value: None,
                    critical_temp: Some(100.0),
                    max_temp: Some(90.0),
                });
            }
        }

        Ok(samples)
    }
}

/// AMD K10 temperature monitor
pub struct K10tempMonitor {
    enabled: bool,
}

impl K10tempMonitor {
    pub fn new() -> Result<Self> {
        let enabled = Path::new("/sys/devices/pci0000:00").exists(); // Simplified check
        Ok(Self { enabled })
    }

    pub fn start(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn stop(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn get_samples(&self, timestamp: SystemTime) -> Result<Vec<TemperatureSample>> {
        let mut samples = Vec::new();

        if self.enabled {
            samples.push(TemperatureSample {
                timestamp,
                sensor: ThermalSensor::CpuPackage,
                temperature: 50.0, // Placeholder
                unit: TemperatureUnit::Celsius,
                raw_value: None,
                critical_temp: Some(90.0),
                max_temp: Some(80.0),
            });
        }

        Ok(samples)
    }
}

/// NVIDIA GPU thermal monitor
pub struct NvidiaThermalMonitor {
    enabled: bool,
}

impl NvidiaThermalMonitor {
    pub fn new() -> Result<Self> {
        // In real implementation, would check for NVIDIA GPU and NVML
        Ok(Self { enabled: false })
    }

    pub fn start(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn stop(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn get_samples(&self, timestamp: SystemTime) -> Result<Vec<TemperatureSample>> {
        let mut samples = Vec::new();

        if self.enabled {
            samples.push(TemperatureSample {
                timestamp,
                sensor: ThermalSensor::GpuCore,
                temperature: 65.0, // Placeholder
                unit: TemperatureUnit::Celsius,
                raw_value: None,
                critical_temp: Some(95.0),
                max_temp: Some(85.0),
            });
        }

        Ok(samples)
    }
}

/// AMD GPU thermal monitor
pub struct AmdGpuThermalMonitor {
    enabled: bool,
}

impl AmdGpuThermalMonitor {
    pub fn new() -> Result<Self> {
        // In real implementation, would check for AMD GPU and appropriate drivers
        Ok(Self { enabled: false })
    }

    pub fn start(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn stop(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn get_samples(&self, timestamp: SystemTime) -> Result<Vec<TemperatureSample>> {
        let mut samples = Vec::new();

        if self.enabled {
            samples.push(TemperatureSample {
                timestamp,
                sensor: ThermalSensor::GpuCore,
                temperature: 70.0, // Placeholder
                unit: TemperatureUnit::Celsius,
                raw_value: None,
                critical_temp: Some(100.0),
                max_temp: Some(90.0),
            });
        }

        Ok(samples)
    }
}

/// Public API functions
/// Create a thermal profiler with default configuration
pub fn create_thermal_profiler() -> Result<ThermalProfiler> {
    ThermalProfiler::new(ThermalConfig::default())
}

/// Create a thermal profiler with custom configuration
pub fn create_thermal_profiler_with_config(config: ThermalConfig) -> Result<ThermalProfiler> {
    ThermalProfiler::new(config)
}

/// Convert temperature between units
pub fn convert_temperature(temp: f64, from: TemperatureUnit, to: TemperatureUnit) -> f64 {
    if from == to {
        return temp;
    }

    // Convert to Celsius first
    let celsius = match from {
        TemperatureUnit::Celsius => temp,
        TemperatureUnit::Fahrenheit => (temp - 32.0) * 5.0 / 9.0,
        TemperatureUnit::Kelvin => temp - 273.15,
    };

    // Convert from Celsius to target unit
    match to {
        TemperatureUnit::Celsius => celsius,
        TemperatureUnit::Fahrenheit => celsius * 9.0 / 5.0 + 32.0,
        TemperatureUnit::Kelvin => celsius + 273.15,
    }
}

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

    #[test]
    fn test_thermal_config_creation() {
        let config = ThermalConfig::default();
        assert!(config.enabled_sensors.contains(&ThermalSensor::CpuPackage));
        assert!(config.enabled_sensors.contains(&ThermalSensor::GpuCore));
        assert_eq!(config.sampling_rate_hz, 5.0);
        assert_eq!(config.throttling_threshold, 85.0);
    }

    #[test]
    fn test_temperature_sample_creation() {
        let sample = TemperatureSample {
            timestamp: SystemTime::now(),
            sensor: ThermalSensor::CpuCore(0),
            temperature: 65.5,
            unit: TemperatureUnit::Celsius,
            raw_value: Some(65500),
            critical_temp: Some(100.0),
            max_temp: Some(90.0),
        };

        assert_eq!(sample.temperature, 65.5);
        assert_eq!(sample.unit, TemperatureUnit::Celsius);
        assert_eq!(sample.sensor, ThermalSensor::CpuCore(0));
    }

    #[test]
    fn test_thermal_profiler_creation() {
        let config = ThermalConfig::default();
        let profiler = ThermalProfiler::new(config);
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_temperature_conversion() {
        // Celsius to Fahrenheit
        assert_eq!(
            convert_temperature(0.0, TemperatureUnit::Celsius, TemperatureUnit::Fahrenheit),
            32.0
        );
        assert_eq!(
            convert_temperature(100.0, TemperatureUnit::Celsius, TemperatureUnit::Fahrenheit),
            212.0
        );

        // Fahrenheit to Celsius
        assert_eq!(
            convert_temperature(32.0, TemperatureUnit::Fahrenheit, TemperatureUnit::Celsius),
            0.0
        );
        assert_eq!(
            convert_temperature(212.0, TemperatureUnit::Fahrenheit, TemperatureUnit::Celsius),
            100.0
        );

        // Celsius to Kelvin
        assert_eq!(
            convert_temperature(0.0, TemperatureUnit::Celsius, TemperatureUnit::Kelvin),
            273.15
        );
        assert_eq!(
            convert_temperature(100.0, TemperatureUnit::Celsius, TemperatureUnit::Kelvin),
            373.15
        );

        // Same unit conversion
        assert_eq!(
            convert_temperature(25.0, TemperatureUnit::Celsius, TemperatureUnit::Celsius),
            25.0
        );
    }

    #[test]
    fn test_thermal_sensor_display() {
        assert_eq!(ThermalSensor::CpuCore(0).to_string(), "CPU Core 0");
        assert_eq!(ThermalSensor::CpuPackage.to_string(), "CPU Package");
        assert_eq!(ThermalSensor::GpuCore.to_string(), "GPU Core");
        assert_eq!(
            ThermalSensor::Custom("Custom".to_string()).to_string(),
            "Custom"
        );
    }

    #[test]
    fn test_temperature_unit_display() {
        assert_eq!(TemperatureUnit::Celsius.to_string(), "°C");
        assert_eq!(TemperatureUnit::Fahrenheit.to_string(), "°F");
        assert_eq!(TemperatureUnit::Kelvin.to_string(), "K");
    }

    #[test]
    fn test_thermal_stats_calculation() {
        let mut profiler = ThermalProfiler::new(ThermalConfig::default()).unwrap();

        // Add some sample data
        profiler.samples = vec![
            TemperatureSample {
                timestamp: SystemTime::now(),
                sensor: ThermalSensor::CpuCore(0),
                temperature: 50.0,
                unit: TemperatureUnit::Celsius,
                raw_value: None,
                critical_temp: None,
                max_temp: None,
            },
            TemperatureSample {
                timestamp: SystemTime::now(),
                sensor: ThermalSensor::CpuCore(0),
                temperature: 60.0,
                unit: TemperatureUnit::Celsius,
                raw_value: None,
                critical_temp: None,
                max_temp: None,
            },
            TemperatureSample {
                timestamp: SystemTime::now(),
                sensor: ThermalSensor::CpuCore(0),
                temperature: 70.0,
                unit: TemperatureUnit::Celsius,
                raw_value: None,
                critical_temp: None,
                max_temp: None,
            },
        ];

        let stats = profiler
            .calculate_thermal_stats(&ThermalSensor::CpuCore(0))
            .unwrap();
        assert_eq!(stats.sample_count, 3);
        assert_eq!(stats.min_temperature, 50.0);
        assert_eq!(stats.max_temperature, 70.0);
        assert_eq!(stats.average_temperature, 60.0);
    }

    #[test]
    fn test_hwmon_monitor_creation() {
        let monitor = HwmonMonitor::new();
        assert!(monitor.is_ok());
    }

    #[test]
    fn test_coretemp_monitor_creation() {
        let monitor = CoretempMonitor::new();
        assert!(monitor.is_ok());
    }

    #[test]
    fn test_thermal_event_matching() {
        let event = ThermalEvent::ThrottlingStart {
            sensor: ThermalSensor::CpuCore(0),
            temperature: 90.0,
            threshold: 85.0,
        };

        match event {
            ThermalEvent::ThrottlingStart {
                sensor,
                temperature,
                threshold,
            } => {
                assert_eq!(sensor, ThermalSensor::CpuCore(0));
                assert_eq!(temperature, 90.0);
                assert_eq!(threshold, 85.0);
            }
            _ => panic!("Wrong event type"),
        }
    }

    #[test]
    fn test_thermal_performance_analysis() {
        let profiler = ThermalProfiler::new(ThermalConfig::default()).unwrap();
        let events = vec![ProfileEvent {
            name: "test_op".to_string(),
            category: "test".to_string(),
            start_us: 0,
            duration_us: 1000,
            thread_id: 0,
            operation_count: Some(1),
            flops: Some(100),
            bytes_transferred: Some(1024),
            stack_trace: Some("test trace".to_string()),
        }];

        let analysis = profiler.analyze_thermal_performance_impact(&events);
        assert_eq!(analysis.total_throttling_time, Duration::ZERO);
        assert_eq!(analysis.performance_degradation, 0.0);
        assert!(analysis.affected_operations.is_empty());
    }
}