trueno-gpu 0.4.17

Pure Rust PTX generation for NVIDIA CUDA - no LLVM, no nvcc
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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
//! Unified Compute Device Abstraction (TRUENO-SPEC-020)
//!
//! Hardware abstraction layer providing a unified interface for CPU, NVIDIA GPU,
//! and AMD GPU monitoring.
//!
//! # Design Principles (Toyota Way)
//!
//! | Principle | Application |
//! |-----------|-------------|
//! | **Genchi Genbutsu** | Direct hardware sampling via native APIs |
//! | **Poka-Yoke** | Type-safe metrics prevent unit confusion |
//!
//! # References
//!
//! - [Nickolls2008] CUDA programming model
//! - [Jia2018] GPU microarchitecture analysis

use std::fmt;

use crate::GpuError;

// ============================================================================
// Device Identification (TRUENO-SPEC-020 Section 2.1)
// ============================================================================

/// Unique identifier for a compute device
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DeviceId {
    /// Device type discriminant
    pub device_type: DeviceType,
    /// Device index within its type (e.g., GPU 0, GPU 1)
    pub index: u32,
}

impl DeviceId {
    /// Create a new device ID
    #[must_use]
    pub const fn new(device_type: DeviceType, index: u32) -> Self {
        Self { device_type, index }
    }

    /// Create CPU device ID
    #[must_use]
    pub const fn cpu() -> Self {
        Self::new(DeviceType::Cpu, 0)
    }

    /// Create NVIDIA GPU device ID
    #[must_use]
    pub const fn nvidia(index: u32) -> Self {
        Self::new(DeviceType::NvidiaGpu, index)
    }

    /// Create AMD GPU device ID
    #[must_use]
    pub const fn amd(index: u32) -> Self {
        Self::new(DeviceType::AmdGpu, index)
    }
}

impl fmt::Display for DeviceId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.device_type {
            DeviceType::Cpu => write!(f, "CPU"),
            DeviceType::NvidiaGpu => write!(f, "NVIDIA:{}", self.index),
            DeviceType::AmdGpu => write!(f, "AMD:{}", self.index),
            DeviceType::IntelGpu => write!(f, "Intel:{}", self.index),
            DeviceType::AppleSilicon => write!(f, "Apple:{}", self.index),
        }
    }
}

/// Type of compute device
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeviceType {
    /// CPU (x86, ARM, etc.)
    Cpu,
    /// NVIDIA GPU (CUDA)
    NvidiaGpu,
    /// AMD GPU (ROCm/HIP)
    AmdGpu,
    /// Intel GPU (oneAPI)
    IntelGpu,
    /// Apple Silicon (Metal)
    AppleSilicon,
}

impl fmt::Display for DeviceType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Cpu => write!(f, "CPU"),
            Self::NvidiaGpu => write!(f, "NVIDIA GPU"),
            Self::AmdGpu => write!(f, "AMD GPU"),
            Self::IntelGpu => write!(f, "Intel GPU"),
            Self::AppleSilicon => write!(f, "Apple Silicon"),
        }
    }
}

// ============================================================================
// Unified Device Trait (TRUENO-SPEC-020 Section 2.1)
// ============================================================================

/// Unified compute device abstraction
///
/// All compute devices (CPU, NVIDIA GPU, AMD GPU) implement this trait
/// for consistent monitoring across heterogeneous hardware.
///
/// # Example
///
/// ```rust,ignore
/// use trueno_gpu::monitor::{ComputeDevice, CpuDevice};
///
/// let cpu = CpuDevice::new();
/// println!("CPU: {} @ {:.1}%", cpu.device_name(), cpu.compute_utilization()?);
/// ```
pub trait ComputeDevice: Send + Sync {
    /// Get the unique device identifier
    fn device_id(&self) -> DeviceId;

    /// Get the device name (e.g., "NVIDIA GeForce RTX 4090")
    fn device_name(&self) -> &str;

    /// Get the device type
    fn device_type(&self) -> DeviceType;

    /// Get compute utilization (0.0-100.0%)
    fn compute_utilization(&self) -> Result<f64, GpuError>;

    /// Get compute clock speed in MHz
    fn compute_clock_mhz(&self) -> Result<u32, GpuError>;

    /// Get compute temperature in Celsius
    fn compute_temperature_c(&self) -> Result<f64, GpuError>;

    /// Get current power consumption in Watts
    fn compute_power_watts(&self) -> Result<f64, GpuError>;

    /// Get power limit in Watts
    fn compute_power_limit_watts(&self) -> Result<f64, GpuError>;

    /// Get used memory in bytes
    fn memory_used_bytes(&self) -> Result<u64, GpuError>;

    /// Get total memory in bytes
    fn memory_total_bytes(&self) -> Result<u64, GpuError>;

    /// Get memory bandwidth in GB/s (if available)
    fn memory_bandwidth_gbps(&self) -> Result<f64, GpuError>;

    /// Get number of compute units (SMs for NVIDIA, CUs for AMD, cores for CPU)
    fn compute_unit_count(&self) -> u32;

    /// Get number of active compute units
    fn active_compute_units(&self) -> Result<u32, GpuError>;

    /// Get PCIe TX bytes per second (GPU only)
    fn pcie_tx_bytes_per_sec(&self) -> Result<u64, GpuError>;

    /// Get PCIe RX bytes per second (GPU only)
    fn pcie_rx_bytes_per_sec(&self) -> Result<u64, GpuError>;

    /// Get PCIe generation (1, 2, 3, 4, 5)
    fn pcie_generation(&self) -> u8;

    /// Get PCIe width (x1, x4, x8, x16)
    fn pcie_width(&self) -> u8;

    /// Refresh metrics from hardware
    fn refresh(&mut self) -> Result<(), GpuError>;

    // =========================================================================
    // Default implementations for derived metrics
    // =========================================================================

    /// Get memory usage percentage (0.0-100.0)
    fn memory_usage_percent(&self) -> Result<f64, GpuError> {
        let used = self.memory_used_bytes()?;
        let total = self.memory_total_bytes()?;
        if total == 0 {
            return Ok(0.0);
        }
        Ok((used as f64 / total as f64) * 100.0)
    }

    /// Get available memory in bytes
    fn memory_available_bytes(&self) -> Result<u64, GpuError> {
        let used = self.memory_used_bytes()?;
        let total = self.memory_total_bytes()?;
        Ok(total.saturating_sub(used))
    }

    /// Get memory used in MB
    fn memory_used_mb(&self) -> Result<u64, GpuError> {
        Ok(self.memory_used_bytes()? / (1024 * 1024))
    }

    /// Get memory total in MB
    fn memory_total_mb(&self) -> Result<u64, GpuError> {
        Ok(self.memory_total_bytes()? / (1024 * 1024))
    }

    /// Get memory total in GB
    fn memory_total_gb(&self) -> Result<f64, GpuError> {
        Ok(self.memory_total_bytes()? as f64 / (1024.0 * 1024.0 * 1024.0))
    }

    /// Get power usage percentage (current/limit * 100)
    fn power_usage_percent(&self) -> Result<f64, GpuError> {
        let current = self.compute_power_watts()?;
        let limit = self.compute_power_limit_watts()?;
        if limit == 0.0 {
            return Ok(0.0);
        }
        Ok((current / limit) * 100.0)
    }

    /// Check if device is throttling due to temperature
    fn is_thermal_throttling(&self) -> Result<bool, GpuError> {
        let temp = self.compute_temperature_c()?;
        // Conservative threshold - most GPUs throttle around 83-85°C
        Ok(temp > 80.0)
    }

    /// Check if device is throttling due to power
    fn is_power_throttling(&self) -> Result<bool, GpuError> {
        let percent = self.power_usage_percent()?;
        Ok(percent > 95.0)
    }
}

// ============================================================================
// Throttle Reason (TRUENO-SPEC-020 Section 4.2)
// ============================================================================

/// Reason for compute throttling
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThrottleReason {
    /// No throttling
    None,
    /// Thermal throttling (temperature limit)
    Thermal,
    /// Power throttling (power limit)
    Power,
    /// Application-set clock limits
    ApplicationClocks,
    /// Software power cap
    SwPowerCap,
    /// Hardware slowdown (external factors)
    HwSlowdown,
    /// Sync boost throttling
    SyncBoost,
}

impl fmt::Display for ThrottleReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::None => write!(f, "None"),
            Self::Thermal => write!(f, "Thermal"),
            Self::Power => write!(f, "Power"),
            Self::ApplicationClocks => write!(f, "AppClocks"),
            Self::SwPowerCap => write!(f, "SwPowerCap"),
            Self::HwSlowdown => write!(f, "HwSlowdown"),
            Self::SyncBoost => write!(f, "SyncBoost"),
        }
    }
}

// ============================================================================
// Device Snapshot (for history tracking)
// ============================================================================

/// Point-in-time snapshot of device metrics
#[derive(Debug, Clone)]
pub struct DeviceSnapshot {
    /// Device ID
    pub device_id: DeviceId,
    /// Timestamp (milliseconds since epoch)
    pub timestamp_ms: u64,
    /// Compute utilization (0.0-100.0)
    pub compute_utilization: f64,
    /// Memory used bytes
    pub memory_used_bytes: u64,
    /// Memory total bytes
    pub memory_total_bytes: u64,
    /// Temperature in Celsius
    pub temperature_c: f64,
    /// Power in Watts
    pub power_watts: f64,
    /// Clock speed in MHz
    pub clock_mhz: u32,
}

impl DeviceSnapshot {
    /// Create snapshot from a compute device
    pub fn capture<D: ComputeDevice>(device: &D) -> Result<Self, GpuError> {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);

        Ok(Self {
            device_id: device.device_id(),
            timestamp_ms: now,
            compute_utilization: device.compute_utilization().unwrap_or(0.0),
            memory_used_bytes: device.memory_used_bytes().unwrap_or(0),
            memory_total_bytes: device.memory_total_bytes().unwrap_or(0),
            temperature_c: device.compute_temperature_c().unwrap_or(0.0),
            power_watts: device.compute_power_watts().unwrap_or(0.0),
            clock_mhz: device.compute_clock_mhz().unwrap_or(0),
        })
    }

    /// Get memory usage percentage
    #[must_use]
    pub fn memory_usage_percent(&self) -> f64 {
        if self.memory_total_bytes == 0 {
            return 0.0;
        }
        (self.memory_used_bytes as f64 / self.memory_total_bytes as f64) * 100.0
    }
}

// ============================================================================
// CPU Device Implementation
// ============================================================================

/// CPU compute device using sysinfo
#[derive(Debug)]
pub struct CpuDevice {
    name: String,
    core_count: u32,
    total_memory: u64,
    // Cached metrics (updated on refresh)
    cpu_usage: f64,
    memory_used: u64,
    temperature: Option<f64>,
}

impl CpuDevice {
    /// Create a new CPU device monitor
    #[must_use]
    pub fn new() -> Self {
        // Get CPU info from /proc/cpuinfo on Linux
        let name = Self::read_cpu_name().unwrap_or_else(|| "Unknown CPU".to_string());
        let core_count = Self::read_core_count();
        let total_memory = Self::read_total_memory();

        Self {
            name,
            core_count,
            total_memory,
            cpu_usage: 0.0,
            memory_used: 0,
            temperature: None,
        }
    }

    fn read_cpu_name() -> Option<String> {
        #[cfg(target_os = "linux")]
        {
            let content = std::fs::read_to_string("/proc/cpuinfo").ok()?;
            for line in content.lines() {
                if line.starts_with("model name") {
                    return line.split(':').nth(1).map(|s| s.trim().to_string());
                }
            }
        }
        None
    }

    fn read_core_count() -> u32 {
        #[cfg(target_os = "linux")]
        {
            if let Ok(content) = std::fs::read_to_string("/proc/cpuinfo") {
                return content
                    .lines()
                    .filter(|line| line.starts_with("processor"))
                    .count() as u32;
            }
        }
        // Fallback
        std::thread::available_parallelism()
            .map(|n| n.get() as u32)
            .unwrap_or(1)
    }

    fn read_total_memory() -> u64 {
        #[cfg(target_os = "linux")]
        {
            if let Ok(content) = std::fs::read_to_string("/proc/meminfo") {
                for line in content.lines() {
                    if line.starts_with("MemTotal:") {
                        // Parse "MemTotal:       32847868 kB"
                        let parts: Vec<&str> = line.split_whitespace().collect();
                        if parts.len() >= 2 {
                            if let Ok(kb) = parts[1].parse::<u64>() {
                                return kb * 1024; // Convert to bytes
                            }
                        }
                    }
                }
            }
        }
        0
    }

    fn read_memory_used() -> u64 {
        #[cfg(target_os = "linux")]
        {
            if let Ok(content) = std::fs::read_to_string("/proc/meminfo") {
                let mut total = 0u64;
                let mut available = 0u64;

                for line in content.lines() {
                    let parts: Vec<&str> = line.split_whitespace().collect();
                    if parts.len() >= 2 {
                        if line.starts_with("MemTotal:") {
                            total = parts[1].parse().unwrap_or(0) * 1024;
                        } else if line.starts_with("MemAvailable:") {
                            available = parts[1].parse().unwrap_or(0) * 1024;
                        }
                    }
                }
                return total.saturating_sub(available);
            }
        }
        0
    }

    fn read_cpu_usage() -> f64 {
        #[cfg(target_os = "linux")]
        {
            // Read /proc/stat for CPU usage
            // This is a simplified version - real implementation would track deltas
            if let Ok(content) = std::fs::read_to_string("/proc/stat") {
                for line in content.lines() {
                    if line.starts_with("cpu ") {
                        let parts: Vec<&str> = line.split_whitespace().collect();
                        if parts.len() >= 5 {
                            let user: u64 = parts[1].parse().unwrap_or(0);
                            let nice: u64 = parts[2].parse().unwrap_or(0);
                            let system: u64 = parts[3].parse().unwrap_or(0);
                            let idle: u64 = parts[4].parse().unwrap_or(0);

                            let total = user + nice + system + idle;
                            let busy = user + nice + system;
                            if total > 0 {
                                return (busy as f64 / total as f64) * 100.0;
                            }
                        }
                    }
                }
            }
        }
        0.0
    }

    fn read_temperature() -> Option<f64> {
        #[cfg(target_os = "linux")]
        {
            // Try hwmon thermal zones
            if let Ok(entries) = std::fs::read_dir("/sys/class/hwmon") {
                for entry in entries.flatten() {
                    let temp_path = entry.path().join("temp1_input");
                    if let Ok(content) = std::fs::read_to_string(&temp_path) {
                        if let Ok(millidegrees) = content.trim().parse::<i64>() {
                            return Some(millidegrees as f64 / 1000.0);
                        }
                    }
                }
            }
            // Fallback to thermal_zone
            if let Ok(content) = std::fs::read_to_string("/sys/class/thermal/thermal_zone0/temp") {
                if let Ok(millidegrees) = content.trim().parse::<i64>() {
                    return Some(millidegrees as f64 / 1000.0);
                }
            }
        }
        None
    }
}

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

impl ComputeDevice for CpuDevice {
    fn device_id(&self) -> DeviceId {
        DeviceId::cpu()
    }

    fn device_name(&self) -> &str {
        &self.name
    }

    fn device_type(&self) -> DeviceType {
        DeviceType::Cpu
    }

    fn compute_utilization(&self) -> Result<f64, GpuError> {
        Ok(self.cpu_usage)
    }

    fn compute_clock_mhz(&self) -> Result<u32, GpuError> {
        // Read current CPU frequency
        #[cfg(target_os = "linux")]
        {
            if let Ok(content) =
                std::fs::read_to_string("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")
            {
                if let Ok(khz) = content.trim().parse::<u64>() {
                    return Ok((khz / 1000) as u32);
                }
            }
        }
        Err(GpuError::NotSupported(
            "CPU frequency not available".to_string(),
        ))
    }

    fn compute_temperature_c(&self) -> Result<f64, GpuError> {
        self.temperature
            .ok_or_else(|| GpuError::NotSupported("CPU temperature not available".to_string()))
    }

    fn compute_power_watts(&self) -> Result<f64, GpuError> {
        // CPU power estimation based on TDP and utilization
        // This is a rough estimate - RAPL provides better data on supported CPUs
        Err(GpuError::NotSupported(
            "CPU power not available".to_string(),
        ))
    }

    fn compute_power_limit_watts(&self) -> Result<f64, GpuError> {
        Err(GpuError::NotSupported(
            "CPU power limit not available".to_string(),
        ))
    }

    fn memory_used_bytes(&self) -> Result<u64, GpuError> {
        Ok(self.memory_used)
    }

    fn memory_total_bytes(&self) -> Result<u64, GpuError> {
        Ok(self.total_memory)
    }

    fn memory_bandwidth_gbps(&self) -> Result<f64, GpuError> {
        // Would need memory controller stats - not easily available
        Err(GpuError::NotSupported(
            "Memory bandwidth not available".to_string(),
        ))
    }

    fn compute_unit_count(&self) -> u32 {
        self.core_count
    }

    fn active_compute_units(&self) -> Result<u32, GpuError> {
        // All cores are typically active
        Ok(self.core_count)
    }

    fn pcie_tx_bytes_per_sec(&self) -> Result<u64, GpuError> {
        Err(GpuError::NotSupported(
            "CPU has no PCIe metrics".to_string(),
        ))
    }

    fn pcie_rx_bytes_per_sec(&self) -> Result<u64, GpuError> {
        Err(GpuError::NotSupported(
            "CPU has no PCIe metrics".to_string(),
        ))
    }

    fn pcie_generation(&self) -> u8 {
        0 // N/A for CPU
    }

    fn pcie_width(&self) -> u8 {
        0 // N/A for CPU
    }

    fn refresh(&mut self) -> Result<(), GpuError> {
        self.cpu_usage = Self::read_cpu_usage();
        self.memory_used = Self::read_memory_used();
        self.temperature = Self::read_temperature();
        Ok(())
    }
}

// ============================================================================
// Tests (Extreme TDD - TRUENO-SPEC-020)
// ============================================================================

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

    // =========================================================================
    // H001: Device ID Tests
    // =========================================================================

    #[test]
    fn h001_device_id_cpu() {
        let id = DeviceId::cpu();
        assert_eq!(id.device_type, DeviceType::Cpu);
        assert_eq!(id.index, 0);
        assert_eq!(format!("{}", id), "CPU");
    }

    #[test]
    fn h001_device_id_nvidia() {
        let id = DeviceId::nvidia(0);
        assert_eq!(id.device_type, DeviceType::NvidiaGpu);
        assert_eq!(id.index, 0);
        assert_eq!(format!("{}", id), "NVIDIA:0");

        let id2 = DeviceId::nvidia(1);
        assert_eq!(format!("{}", id2), "NVIDIA:1");
    }

    #[test]
    fn h001_device_id_amd() {
        let id = DeviceId::amd(0);
        assert_eq!(id.device_type, DeviceType::AmdGpu);
        assert_eq!(id.index, 0);
        assert_eq!(format!("{}", id), "AMD:0");
    }

    #[test]
    fn h001_device_id_equality() {
        let id1 = DeviceId::nvidia(0);
        let id2 = DeviceId::nvidia(0);
        let id3 = DeviceId::nvidia(1);
        let id4 = DeviceId::amd(0);

        assert_eq!(id1, id2);
        assert_ne!(id1, id3);
        assert_ne!(id1, id4);
    }

    // =========================================================================
    // H002: Device Type Tests
    // =========================================================================

    #[test]
    fn h002_device_type_display() {
        assert_eq!(format!("{}", DeviceType::Cpu), "CPU");
        assert_eq!(format!("{}", DeviceType::NvidiaGpu), "NVIDIA GPU");
        assert_eq!(format!("{}", DeviceType::AmdGpu), "AMD GPU");
        assert_eq!(format!("{}", DeviceType::IntelGpu), "Intel GPU");
        assert_eq!(format!("{}", DeviceType::AppleSilicon), "Apple Silicon");
    }

    // =========================================================================
    // H003: CPU Device Tests
    // =========================================================================

    #[test]
    fn h003_cpu_device_creation() {
        let cpu = CpuDevice::new();
        assert_eq!(cpu.device_type(), DeviceType::Cpu);
        assert_eq!(cpu.device_id(), DeviceId::cpu());
        assert!(cpu.core_count > 0);
    }

    #[test]
    fn h003_cpu_device_default() {
        let cpu = CpuDevice::default();
        assert!(cpu.compute_unit_count() > 0);
    }

    #[test]
    fn h003_cpu_device_name() {
        let cpu = CpuDevice::new();
        // Name should be non-empty
        assert!(!cpu.device_name().is_empty());
    }

    #[test]
    fn h003_cpu_device_memory_total() {
        let cpu = CpuDevice::new();
        // Should have some memory (at least 1GB in practice)
        let total = cpu.memory_total_bytes().unwrap_or(0);
        assert!(total > 0, "CPU should report total memory");
    }

    #[test]
    fn h003_cpu_device_refresh() {
        let mut cpu = CpuDevice::new();
        assert!(cpu.refresh().is_ok());
        // After refresh, metrics should be populated
    }

    // =========================================================================
    // H004: Device Snapshot Tests
    // =========================================================================

    #[test]
    fn h004_device_snapshot_capture() {
        let cpu = CpuDevice::new();
        let snapshot = DeviceSnapshot::capture(&cpu);
        assert!(snapshot.is_ok());

        let snap = snapshot.unwrap();
        assert_eq!(snap.device_id, DeviceId::cpu());
        assert!(snap.timestamp_ms > 0);
    }

    #[test]
    fn h004_device_snapshot_memory_percent() {
        let snap = DeviceSnapshot {
            device_id: DeviceId::cpu(),
            timestamp_ms: 0,
            compute_utilization: 50.0,
            memory_used_bytes: 50 * 1024 * 1024 * 1024, // 50 GB
            memory_total_bytes: 100 * 1024 * 1024 * 1024, // 100 GB
            temperature_c: 45.0,
            power_watts: 100.0,
            clock_mhz: 3000,
        };

        assert!((snap.memory_usage_percent() - 50.0).abs() < 0.01);
    }

    #[test]
    fn h004_device_snapshot_memory_percent_zero_total() {
        let snap = DeviceSnapshot {
            device_id: DeviceId::cpu(),
            timestamp_ms: 0,
            compute_utilization: 0.0,
            memory_used_bytes: 0,
            memory_total_bytes: 0, // Division by zero case
            temperature_c: 0.0,
            power_watts: 0.0,
            clock_mhz: 0,
        };

        assert!((snap.memory_usage_percent() - 0.0).abs() < 0.01);
    }

    // =========================================================================
    // H005: Throttle Reason Tests
    // =========================================================================

    #[test]
    fn h005_throttle_reason_display() {
        assert_eq!(format!("{}", ThrottleReason::None), "None");
        assert_eq!(format!("{}", ThrottleReason::Thermal), "Thermal");
        assert_eq!(format!("{}", ThrottleReason::Power), "Power");
    }

    // =========================================================================
    // H006: Derived Metrics Tests (ComputeDevice trait defaults)
    // =========================================================================

    #[test]
    fn h006_memory_usage_percent() {
        let cpu = CpuDevice::new();
        let percent = cpu.memory_usage_percent();
        // On Linux, this should always succeed
        assert!(percent.is_ok());
        let p = percent.unwrap();
        assert!(p >= 0.0 && p <= 100.0);
    }

    #[test]
    fn h006_memory_available_bytes() {
        let cpu = CpuDevice::new();
        // On Linux, these should always succeed
        let avail = cpu.memory_available_bytes().unwrap();
        let total = cpu.memory_total_bytes().unwrap();
        assert!(avail <= total);
    }

    #[test]
    fn h006_memory_mb_helpers() {
        let cpu = CpuDevice::new();
        // On Linux, these should always succeed
        let used_mb = cpu.memory_used_mb().unwrap();
        let total_mb = cpu.memory_total_mb().unwrap();
        assert!(used_mb <= total_mb);
    }

    #[test]
    fn h006_memory_gb_helper() {
        let cpu = CpuDevice::new();
        // On Linux, this should always succeed
        let total_gb = cpu.memory_total_gb().unwrap();
        // Should be positive (most systems have > 1GB)
        assert!(total_gb > 0.0);
    }

    // =========================================================================
    // H007: Thermal Throttling Detection
    // =========================================================================

    #[test]
    fn h007_thermal_throttling_detection() {
        let cpu = CpuDevice::new();
        // Just verify it doesn't panic
        let _ = cpu.is_thermal_throttling();
    }

    // =========================================================================
    // H008: Power Throttling Detection
    // =========================================================================

    #[test]
    fn h008_power_throttling_detection() {
        let cpu = CpuDevice::new();
        // CPU doesn't support power metrics, but shouldn't panic
        let _ = cpu.is_power_throttling();
    }

    // =========================================================================
    // H009: Edge Cases
    // =========================================================================

    #[test]
    fn h009_cpu_unsupported_metrics() {
        let cpu = CpuDevice::new();

        // PCIe metrics should return NotSupported
        assert!(cpu.pcie_tx_bytes_per_sec().is_err());
        assert!(cpu.pcie_rx_bytes_per_sec().is_err());
        assert_eq!(cpu.pcie_generation(), 0);
        assert_eq!(cpu.pcie_width(), 0);
    }

    #[test]
    fn h009_device_id_hash() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(DeviceId::cpu());
        set.insert(DeviceId::nvidia(0));
        set.insert(DeviceId::nvidia(1));
        set.insert(DeviceId::amd(0));

        assert_eq!(set.len(), 4);

        // Duplicate should not increase size
        set.insert(DeviceId::cpu());
        assert_eq!(set.len(), 4);
    }

    // =========================================================================
    // H010: Additional Display Coverage
    // =========================================================================

    #[test]
    fn h010_device_id_intel_display() {
        let id = DeviceId::new(DeviceType::IntelGpu, 0);
        assert_eq!(format!("{}", id), "Intel:0");
        assert_eq!(id.device_type, DeviceType::IntelGpu);

        let id2 = DeviceId::new(DeviceType::IntelGpu, 2);
        assert_eq!(format!("{}", id2), "Intel:2");
    }

    #[test]
    fn h010_device_id_apple_display() {
        let id = DeviceId::new(DeviceType::AppleSilicon, 0);
        assert_eq!(format!("{}", id), "Apple:0");
        assert_eq!(id.device_type, DeviceType::AppleSilicon);
    }

    #[test]
    fn h010_throttle_reason_all_variants() {
        // Cover all ThrottleReason Display variants
        assert_eq!(format!("{}", ThrottleReason::None), "None");
        assert_eq!(format!("{}", ThrottleReason::Thermal), "Thermal");
        assert_eq!(format!("{}", ThrottleReason::Power), "Power");
        assert_eq!(
            format!("{}", ThrottleReason::ApplicationClocks),
            "AppClocks"
        );
        assert_eq!(format!("{}", ThrottleReason::SwPowerCap), "SwPowerCap");
        assert_eq!(format!("{}", ThrottleReason::HwSlowdown), "HwSlowdown");
        assert_eq!(format!("{}", ThrottleReason::SyncBoost), "SyncBoost");
    }

    // =========================================================================
    // H011: Default Trait Impl Edge Cases
    // =========================================================================

    /// Mock device to test default trait implementations with controlled values
    struct MockDevice {
        mem_used: u64,
        mem_total: u64,
        power_current: f64,
        power_limit: f64,
        temperature: f64,
    }

    impl MockDevice {
        fn new(
            mem_used: u64,
            mem_total: u64,
            power_current: f64,
            power_limit: f64,
            temperature: f64,
        ) -> Self {
            Self {
                mem_used,
                mem_total,
                power_current,
                power_limit,
                temperature,
            }
        }
    }

    impl ComputeDevice for MockDevice {
        fn device_id(&self) -> DeviceId {
            DeviceId::cpu()
        }
        fn device_name(&self) -> &str {
            "Mock"
        }
        fn device_type(&self) -> DeviceType {
            DeviceType::Cpu
        }
        fn compute_utilization(&self) -> Result<f64, GpuError> {
            Ok(50.0)
        }
        fn compute_clock_mhz(&self) -> Result<u32, GpuError> {
            Ok(3000)
        }
        fn compute_temperature_c(&self) -> Result<f64, GpuError> {
            Ok(self.temperature)
        }
        fn compute_power_watts(&self) -> Result<f64, GpuError> {
            Ok(self.power_current)
        }
        fn compute_power_limit_watts(&self) -> Result<f64, GpuError> {
            Ok(self.power_limit)
        }
        fn memory_used_bytes(&self) -> Result<u64, GpuError> {
            Ok(self.mem_used)
        }
        fn memory_total_bytes(&self) -> Result<u64, GpuError> {
            Ok(self.mem_total)
        }
        fn memory_bandwidth_gbps(&self) -> Result<f64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn compute_unit_count(&self) -> u32 {
            8
        }
        fn active_compute_units(&self) -> Result<u32, GpuError> {
            Ok(8)
        }
        fn pcie_tx_bytes_per_sec(&self) -> Result<u64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn pcie_rx_bytes_per_sec(&self) -> Result<u64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn pcie_generation(&self) -> u8 {
            0
        }
        fn pcie_width(&self) -> u8 {
            0
        }
        fn refresh(&mut self) -> Result<(), GpuError> {
            Ok(())
        }
    }

    #[test]
    fn h011_memory_usage_percent_zero_total() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
        // Zero total should return 0.0, not divide by zero
        assert!((mock.memory_usage_percent().unwrap() - 0.0).abs() < 0.01);
    }

    #[test]
    fn h011_memory_usage_percent_normal() {
        let mock = MockDevice::new(
            50 * 1024 * 1024 * 1024,
            100 * 1024 * 1024 * 1024,
            0.0,
            0.0,
            0.0,
        );
        // 50% usage
        assert!((mock.memory_usage_percent().unwrap() - 50.0).abs() < 0.01);
    }

    #[test]
    fn h011_memory_available_bytes() {
        let mock = MockDevice::new(
            30 * 1024 * 1024 * 1024,
            100 * 1024 * 1024 * 1024,
            0.0,
            0.0,
            0.0,
        );
        // 70GB available
        let available = mock.memory_available_bytes().unwrap();
        assert_eq!(available, 70 * 1024 * 1024 * 1024);
    }

    #[test]
    fn h011_memory_mb_gb_conversions() {
        let mock = MockDevice::new(1024 * 1024 * 1024, 16 * 1024 * 1024 * 1024, 0.0, 0.0, 0.0);
        // 1GB used = 1024MB
        assert_eq!(mock.memory_used_mb().unwrap(), 1024);
        // 16GB total = 16384MB
        assert_eq!(mock.memory_total_mb().unwrap(), 16384);
        // 16GB as f64
        assert!((mock.memory_total_gb().unwrap() - 16.0).abs() < 0.01);
    }

    #[test]
    fn h011_power_usage_percent_zero_limit() {
        let mock = MockDevice::new(0, 0, 100.0, 0.0, 0.0);
        // Zero limit should return 0.0, not divide by zero
        assert!((mock.power_usage_percent().unwrap() - 0.0).abs() < 0.01);
    }

    #[test]
    fn h011_power_usage_percent_normal() {
        let mock = MockDevice::new(0, 0, 150.0, 300.0, 0.0);
        // 50% power usage
        assert!((mock.power_usage_percent().unwrap() - 50.0).abs() < 0.01);
    }

    #[test]
    fn h011_thermal_throttling_below_threshold() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 75.0);
        // Below 80°C - no throttling
        assert!(!mock.is_thermal_throttling().unwrap());
    }

    #[test]
    fn h011_thermal_throttling_above_threshold() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 85.0);
        // Above 80°C - throttling
        assert!(mock.is_thermal_throttling().unwrap());
    }

    #[test]
    fn h011_power_throttling_below_threshold() {
        let mock = MockDevice::new(0, 0, 90.0, 100.0, 0.0);
        // 90% - below 95% threshold
        assert!(!mock.is_power_throttling().unwrap());
    }

    #[test]
    fn h011_power_throttling_above_threshold() {
        let mock = MockDevice::new(0, 0, 98.0, 100.0, 0.0);
        // 98% - above 95% threshold
        assert!(mock.is_power_throttling().unwrap());
    }

    // =========================================================================
    // H012: DeviceSnapshot Edge Cases
    // =========================================================================

    #[test]
    fn h012_device_snapshot_from_mock() {
        let mock = MockDevice::new(
            8 * 1024 * 1024 * 1024,
            16 * 1024 * 1024 * 1024,
            150.0,
            300.0,
            65.0,
        );
        let snapshot = DeviceSnapshot::capture(&mock).unwrap();

        assert_eq!(snapshot.device_id, DeviceId::cpu());
        assert!((snapshot.compute_utilization - 50.0).abs() < 0.01);
        assert_eq!(snapshot.memory_used_bytes, 8 * 1024 * 1024 * 1024);
        assert_eq!(snapshot.memory_total_bytes, 16 * 1024 * 1024 * 1024);
        assert!((snapshot.temperature_c - 65.0).abs() < 0.01);
        assert!((snapshot.power_watts - 150.0).abs() < 0.01);
        assert_eq!(snapshot.clock_mhz, 3000);
    }

    // =========================================================================
    // H013: CpuDevice Additional Coverage
    // =========================================================================

    #[test]
    fn h013_cpu_device_refresh() {
        let mut cpu = CpuDevice::new();
        // Refresh should not panic
        let _ = cpu.refresh();
        // After refresh, metrics should still work
        let _ = cpu.compute_utilization();
        let _ = cpu.memory_used_bytes();
    }

    #[test]
    fn h013_cpu_device_multiple_refreshes() {
        let mut cpu = CpuDevice::new();
        // Multiple refreshes should be idempotent
        for _ in 0..3 {
            let _ = cpu.refresh();
        }
    }

    #[test]
    fn h013_cpu_device_name_not_empty() {
        let cpu = CpuDevice::new();
        assert!(!cpu.device_name().is_empty());
    }

    #[test]
    fn h013_cpu_device_core_count_positive() {
        let cpu = CpuDevice::new();
        assert!(cpu.compute_unit_count() > 0);
    }

    #[test]
    fn h013_cpu_clock_speed() {
        let cpu = CpuDevice::new();
        // Clock speed should be positive (or NotSupported error on some systems)
        // Just verify we can call it without panic
        let _ = cpu.compute_clock_mhz();
    }

    #[test]
    fn h013_cpu_temperature() {
        let cpu = CpuDevice::new();
        // Temperature may not be available on all systems
        // Just verify we can call it without panic
        let _ = cpu.compute_temperature_c();
    }

    // =========================================================================
    // H014: MockDevice Extended Coverage
    // =========================================================================

    #[test]
    fn h014_mock_device_all_methods() {
        let mock = MockDevice::new(1024, 2048, 10.0, 100.0, 30.0);

        // Test all trait method implementations
        assert_eq!(mock.device_id(), DeviceId::cpu());
        assert_eq!(mock.device_name(), "Mock");
        assert!(matches!(mock.device_type(), DeviceType::Cpu));
        assert_eq!(mock.compute_unit_count(), 8);
        assert_eq!(mock.memory_used_bytes().unwrap(), 1024);
        assert_eq!(mock.memory_total_bytes().unwrap(), 2048);
        assert!((mock.compute_utilization().unwrap() - 50.0).abs() < 0.01); // MockDevice always returns 50.0
        assert!((mock.compute_temperature_c().unwrap() - 30.0).abs() < 0.01);
        assert!((mock.compute_power_watts().unwrap() - 10.0).abs() < 0.01);
        assert_eq!(mock.compute_clock_mhz().unwrap(), 3000);
    }

    #[test]
    fn h014_mock_device_derived_metrics() {
        let mock = MockDevice::new(1024, 2048, 10.0, 100.0, 30.0);

        // Derived metrics
        let usage_percent = mock.memory_usage_percent().unwrap();
        assert!((usage_percent - 50.0).abs() < 0.01); // 1024/2048 = 50%

        let available = mock.memory_available_bytes().unwrap();
        assert_eq!(available, 1024); // 2048 - 1024
    }

    #[test]
    fn h014_mock_device_mb_gb_helpers() {
        let mock = MockDevice::new(
            1024 * 1024 * 1024,
            2 * 1024 * 1024 * 1024,
            10.0,
            100.0,
            30.0,
        );

        let used_mb = mock.memory_used_mb().unwrap();
        assert_eq!(used_mb, 1024); // 1 GB = 1024 MB

        let total_gb = mock.memory_total_gb().unwrap();
        assert!((total_gb - 2.0).abs() < 0.1); // 2 GB
    }

    // =========================================================================
    // H015: DeviceId Additional Coverage
    // =========================================================================

    #[test]
    fn h015_device_id_display() {
        assert_eq!(format!("{}", DeviceId::cpu()), "CPU");
        assert_eq!(format!("{}", DeviceId::nvidia(0)), "NVIDIA:0");
        assert_eq!(format!("{}", DeviceId::nvidia(1)), "NVIDIA:1");
        assert_eq!(format!("{}", DeviceId::amd(0)), "AMD:0");
    }

    #[test]
    fn h015_device_id_debug() {
        let cpu_id = DeviceId::cpu();
        let debug_str = format!("{:?}", cpu_id);
        assert!(debug_str.contains("Cpu"));
    }

    #[test]
    fn h015_device_id_clone() {
        let id1 = DeviceId::nvidia(0);
        let id2 = id1.clone();
        assert_eq!(id1, id2);
    }

    // =========================================================================
    // H016: DeviceSnapshot Additional Coverage
    // =========================================================================

    #[test]
    fn h016_snapshot_debug() {
        let mock = MockDevice::new(1024, 2048, 10.0, 100.0, 30.0);
        let snapshot = DeviceSnapshot::capture(&mock).unwrap();
        let debug_str = format!("{:?}", snapshot);
        assert!(debug_str.contains("DeviceSnapshot"));
    }

    #[test]
    fn h016_snapshot_clone() {
        let mock = MockDevice::new(1024, 2048, 10.0, 100.0, 30.0);
        let snapshot1 = DeviceSnapshot::capture(&mock).unwrap();
        let snapshot2 = snapshot1.clone();
        assert_eq!(snapshot1.device_id, snapshot2.device_id);
        assert_eq!(snapshot1.memory_used_bytes, snapshot2.memory_used_bytes);
    }

    // =========================================================================
    // H017: DeviceType Coverage
    // =========================================================================

    #[test]
    fn h017_device_type_display() {
        assert_eq!(format!("{}", DeviceType::Cpu), "CPU");
        assert_eq!(format!("{}", DeviceType::NvidiaGpu), "NVIDIA GPU");
        assert_eq!(format!("{}", DeviceType::AmdGpu), "AMD GPU");
        assert_eq!(format!("{}", DeviceType::IntelGpu), "Intel GPU");
    }

    #[test]
    fn h017_device_type_debug() {
        let gpu = DeviceType::NvidiaGpu;
        let debug_str = format!("{:?}", gpu);
        assert!(debug_str.contains("NvidiaGpu"));
    }

    // =========================================================================
    // H018: Error Path Coverage (Best Effort)
    // =========================================================================

    #[test]
    fn h018_cpu_unsupported_pcie_metrics() {
        let cpu = CpuDevice::new();

        // PCIe metrics return NotSupported
        let tx = cpu.pcie_tx_bytes_per_sec();
        assert!(matches!(tx, Err(GpuError::NotSupported(_))));

        let rx = cpu.pcie_rx_bytes_per_sec();
        assert!(matches!(rx, Err(GpuError::NotSupported(_))));
    }

    #[test]
    fn h018_cpu_power_metrics() {
        let cpu = CpuDevice::new();

        // Power metrics return NotSupported for CPU
        let power = cpu.compute_power_watts();
        assert!(matches!(power, Err(GpuError::NotSupported(_))));

        let power_limit = cpu.compute_power_limit_watts();
        assert!(matches!(power_limit, Err(GpuError::NotSupported(_))));

        let bw = cpu.memory_bandwidth_gbps();
        assert!(matches!(bw, Err(GpuError::NotSupported(_))));
    }

    // =========================================================================
    // H019: CpuDevice active_compute_units Test
    // =========================================================================

    #[test]
    fn h019_cpu_active_compute_units() {
        let cpu = CpuDevice::new();

        // active_compute_units should return the core count
        let active = cpu.active_compute_units();
        assert!(active.is_ok());
        let count = active.unwrap();
        assert!(count > 0, "Should have at least one active compute unit");
        assert_eq!(
            count,
            cpu.compute_unit_count(),
            "Active should equal total cores"
        );
    }

    // =========================================================================
    // H020: MockDevice Full Coverage Tests
    // =========================================================================

    #[test]
    fn h020_mock_device_pcie_metrics() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);

        // PCIe metrics should return NotSupported
        assert!(matches!(
            mock.pcie_tx_bytes_per_sec(),
            Err(GpuError::NotSupported(_))
        ));
        assert!(matches!(
            mock.pcie_rx_bytes_per_sec(),
            Err(GpuError::NotSupported(_))
        ));
        assert_eq!(mock.pcie_generation(), 0);
        assert_eq!(mock.pcie_width(), 0);
    }

    #[test]
    fn h020_mock_device_active_compute_units() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);

        let active = mock.active_compute_units();
        assert!(active.is_ok());
        assert_eq!(active.unwrap(), 8); // MockDevice returns 8 compute units
    }

    #[test]
    fn h020_mock_device_memory_bandwidth() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);

        assert!(matches!(
            mock.memory_bandwidth_gbps(),
            Err(GpuError::NotSupported(_))
        ));
    }

    #[test]
    fn h020_mock_device_refresh() {
        let mut mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);

        // refresh should succeed
        assert!(mock.refresh().is_ok());
    }

    // =========================================================================
    // H021: CpuDevice Default Derived Metrics
    // =========================================================================

    #[test]
    fn h021_cpu_device_memory_mb_conversion() {
        let cpu = CpuDevice::new();

        // Test memory_used_mb conversion
        if let Ok(used_bytes) = cpu.memory_used_bytes() {
            let used_mb = cpu.memory_used_mb();
            assert!(used_mb.is_ok());
            assert_eq!(used_mb.unwrap(), used_bytes / (1024 * 1024));
        }
    }

    #[test]
    fn h021_cpu_device_memory_total_mb() {
        let cpu = CpuDevice::new();

        if let Ok(total_bytes) = cpu.memory_total_bytes() {
            let total_mb = cpu.memory_total_mb();
            assert!(total_mb.is_ok());
            assert_eq!(total_mb.unwrap(), total_bytes / (1024 * 1024));
        }
    }

    #[test]
    fn h021_cpu_device_memory_total_gb() {
        let cpu = CpuDevice::new();

        if let Ok(total_bytes) = cpu.memory_total_bytes() {
            let total_gb = cpu.memory_total_gb();
            assert!(total_gb.is_ok());
            let expected_gb = total_bytes as f64 / (1024.0 * 1024.0 * 1024.0);
            assert!((total_gb.unwrap() - expected_gb).abs() < 0.001);
        }
    }

    #[test]
    fn h021_cpu_device_memory_available() {
        let cpu = CpuDevice::new();

        if let (Ok(used), Ok(total)) = (cpu.memory_used_bytes(), cpu.memory_total_bytes()) {
            let available = cpu.memory_available_bytes();
            assert!(available.is_ok());
            assert_eq!(available.unwrap(), total.saturating_sub(used));
        }
    }

    // =========================================================================
    // H022: Edge Case Tests for Default Trait Implementations
    // =========================================================================

    #[test]
    fn h022_mock_device_thermal_throttling_at_threshold() {
        // Test exactly at the 80 degree threshold
        let mock_at_80 = MockDevice::new(0, 0, 0.0, 0.0, 80.0);
        // 80.0 is not > 80.0, so no throttling
        assert!(!mock_at_80.is_thermal_throttling().unwrap());

        // Just above threshold
        let mock_at_80_1 = MockDevice::new(0, 0, 0.0, 0.0, 80.1);
        assert!(mock_at_80_1.is_thermal_throttling().unwrap());
    }

    #[test]
    fn h022_mock_device_power_throttling_at_threshold() {
        // Test exactly at the 95% threshold
        let mock_at_95 = MockDevice::new(0, 0, 95.0, 100.0, 0.0);
        // 95.0 is not > 95.0, so no throttling
        assert!(!mock_at_95.is_power_throttling().unwrap());

        // Just above threshold
        let mock_at_95_1 = MockDevice::new(0, 0, 95.1, 100.0, 0.0);
        assert!(mock_at_95_1.is_power_throttling().unwrap());
    }

    #[test]
    fn h022_mock_device_memory_usage_full() {
        // Test 100% memory usage
        let mock_full = MockDevice::new(100, 100, 0.0, 0.0, 0.0);
        assert!((mock_full.memory_usage_percent().unwrap() - 100.0).abs() < 0.01);
        assert_eq!(mock_full.memory_available_bytes().unwrap(), 0);
    }

    // =========================================================================
    // H023: DeviceSnapshot Additional Field Coverage
    // =========================================================================

    #[test]
    fn h023_device_snapshot_field_access() {
        let mock = MockDevice::new(
            8 * 1024 * 1024 * 1024,
            32 * 1024 * 1024 * 1024,
            250.0,
            350.0,
            72.0,
        );
        let snapshot = DeviceSnapshot::capture(&mock).unwrap();

        // Verify all fields are accessible and have expected values
        assert_eq!(snapshot.memory_used_bytes, 8 * 1024 * 1024 * 1024);
        assert_eq!(snapshot.memory_total_bytes, 32 * 1024 * 1024 * 1024);
        assert!((snapshot.temperature_c - 72.0).abs() < 0.01);
        assert!((snapshot.power_watts - 250.0).abs() < 0.01);
        assert_eq!(snapshot.clock_mhz, 3000);

        // Test memory_usage_percent calculation
        // 8GB / 32GB = 25%
        assert!((snapshot.memory_usage_percent() - 25.0).abs() < 0.01);
    }

    // =========================================================================
    // H024: CpuDevice Internal Method Coverage via Refresh
    // =========================================================================

    #[test]
    fn h024_cpu_device_refresh_populates_fields() {
        let mut cpu = CpuDevice::new();

        // First refresh
        let result = cpu.refresh();
        assert!(result.is_ok());

        // After refresh, utilization should be populated (may be 0 if just started)
        let util = cpu.compute_utilization();
        assert!(util.is_ok());
        let util_val = util.unwrap();
        assert!(util_val >= 0.0 && util_val <= 100.0);

        // Memory used should be reasonable
        let mem = cpu.memory_used_bytes();
        assert!(mem.is_ok());
    }

    #[test]
    fn h024_cpu_device_refresh_multiple_times() {
        let mut cpu = CpuDevice::new();

        // Refresh multiple times should always succeed
        for _ in 0..5 {
            assert!(cpu.refresh().is_ok());
        }

        // Values should still be accessible
        assert!(cpu.compute_utilization().is_ok());
        assert!(cpu.memory_used_bytes().is_ok());
    }

    // =========================================================================
    // H025: CpuDevice Direct Read Functions Coverage
    // =========================================================================

    #[test]
    fn h025_cpu_device_read_core_count() {
        // read_core_count is called in CpuDevice::new()
        // On Linux it reads from /proc/cpuinfo
        // Verify the result is a valid positive number
        let cpu = CpuDevice::new();
        let count = cpu.compute_unit_count();
        assert!(count >= 1, "Should have at least 1 core");
        assert!(
            count <= 1024,
            "Sanity check: should have fewer than 1024 cores"
        );
    }

    #[test]
    fn h025_cpu_device_read_total_memory() {
        // read_total_memory is called in CpuDevice::new()
        let cpu = CpuDevice::new();
        let total = cpu.memory_total_bytes().unwrap();
        // System should have at least 1GB and less than 1TB typically
        assert!(total >= 1024 * 1024 * 1024, "Should have at least 1GB");
        assert!(total < 100 * 1024 * 1024 * 1024 * 1024, "Sanity: < 100TB");
    }

    #[test]
    fn h025_cpu_device_read_cpu_name() {
        // read_cpu_name is called in CpuDevice::new()
        let cpu = CpuDevice::new();
        let name = cpu.device_name();
        assert!(!name.is_empty(), "CPU name should not be empty");
        // Name could be "Unknown CPU" if /proc/cpuinfo doesn't have model name
    }

    // =========================================================================
    // H026: CpuDevice Compute Clock Coverage
    // =========================================================================

    #[test]
    fn h026_cpu_device_compute_clock_value() {
        let cpu = CpuDevice::new();
        // On systems with frequency scaling, this should return Ok
        // On systems without, it returns NotSupported
        match cpu.compute_clock_mhz() {
            Ok(mhz) => {
                // Valid frequency range: 100 MHz to 10 GHz
                assert!(mhz >= 100, "Clock should be at least 100 MHz");
                assert!(mhz <= 10000, "Clock should be at most 10 GHz");
            }
            Err(GpuError::NotSupported(_)) => {
                // Expected on systems without frequency info
            }
            Err(e) => panic!("Unexpected error: {:?}", e),
        }
    }

    // =========================================================================
    // H027: CpuDevice Temperature Coverage
    // =========================================================================

    #[test]
    fn h027_cpu_device_temperature_value() {
        let mut cpu = CpuDevice::new();
        cpu.refresh().unwrap();

        // Temperature may or may not be available depending on hardware/permissions
        match cpu.compute_temperature_c() {
            Ok(temp) => {
                // Valid temperature range: 0 to 150 Celsius
                assert!(temp >= 0.0, "Temperature should be non-negative");
                assert!(temp <= 150.0, "Temperature should be at most 150C");
            }
            Err(GpuError::NotSupported(_)) => {
                // Expected on systems without temperature sensors
            }
            Err(e) => panic!("Unexpected error: {:?}", e),
        }
    }

    // =========================================================================
    // H028: CpuDevice CPU Usage Coverage
    // =========================================================================

    #[test]
    fn h028_cpu_device_cpu_usage_after_refresh() {
        let mut cpu = CpuDevice::new();
        cpu.refresh().unwrap();

        let usage = cpu.compute_utilization().unwrap();
        // CPU usage should be between 0 and 100
        assert!(usage >= 0.0, "CPU usage should be non-negative");
        assert!(usage <= 100.0, "CPU usage should be at most 100%");
    }

    // =========================================================================
    // H029: CpuDevice Memory Used Coverage
    // =========================================================================

    #[test]
    fn h029_cpu_device_memory_used_after_refresh() {
        let mut cpu = CpuDevice::new();
        cpu.refresh().unwrap();

        let used = cpu.memory_used_bytes().unwrap();
        let total = cpu.memory_total_bytes().unwrap();

        // Used should be <= total
        assert!(used <= total, "Used memory should not exceed total");
        // At least some memory should be used (kernel, etc.)
        assert!(used > 0, "Some memory should be in use");
    }

    // =========================================================================
    // H030: MockDevice Additional Coverage
    // =========================================================================

    #[test]
    fn h030_mock_device_device_name() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
        assert_eq!(mock.device_name(), "Mock");
    }

    #[test]
    fn h030_mock_device_device_type() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
        assert!(matches!(mock.device_type(), DeviceType::Cpu));
    }

    #[test]
    fn h030_mock_device_device_id() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
        assert_eq!(mock.device_id(), DeviceId::cpu());
    }

    #[test]
    fn h030_mock_device_compute_utilization() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
        assert!((mock.compute_utilization().unwrap() - 50.0).abs() < 0.01);
    }

    #[test]
    fn h030_mock_device_compute_clock() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
        assert_eq!(mock.compute_clock_mhz().unwrap(), 3000);
    }

    #[test]
    fn h030_mock_device_compute_temperature() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 45.0);
        assert!((mock.compute_temperature_c().unwrap() - 45.0).abs() < 0.01);
    }

    #[test]
    fn h030_mock_device_compute_power() {
        let mock = MockDevice::new(0, 0, 200.0, 300.0, 0.0);
        assert!((mock.compute_power_watts().unwrap() - 200.0).abs() < 0.01);
        assert!((mock.compute_power_limit_watts().unwrap() - 300.0).abs() < 0.01);
    }

    #[test]
    fn h030_mock_device_compute_unit_count() {
        let mock = MockDevice::new(0, 0, 0.0, 0.0, 0.0);
        assert_eq!(mock.compute_unit_count(), 8);
    }

    #[test]
    fn h030_mock_device_memory_bytes() {
        let mock = MockDevice::new(1000, 2000, 0.0, 0.0, 0.0);
        assert_eq!(mock.memory_used_bytes().unwrap(), 1000);
        assert_eq!(mock.memory_total_bytes().unwrap(), 2000);
    }

    // =========================================================================
    // H031: Error-Propagating Mock Device
    // =========================================================================

    /// Mock device that returns errors for testing error propagation in default trait methods
    struct ErrorMockDevice {
        return_error: bool,
    }

    impl ErrorMockDevice {
        fn new(return_error: bool) -> Self {
            Self { return_error }
        }
    }

    impl ComputeDevice for ErrorMockDevice {
        fn device_id(&self) -> DeviceId {
            DeviceId::cpu()
        }
        fn device_name(&self) -> &str {
            "ErrorMock"
        }
        fn device_type(&self) -> DeviceType {
            DeviceType::Cpu
        }
        fn compute_utilization(&self) -> Result<f64, GpuError> {
            if self.return_error {
                Err(GpuError::NotSupported("test".into()))
            } else {
                Ok(50.0)
            }
        }
        fn compute_clock_mhz(&self) -> Result<u32, GpuError> {
            if self.return_error {
                Err(GpuError::NotSupported("test".into()))
            } else {
                Ok(3000)
            }
        }
        fn compute_temperature_c(&self) -> Result<f64, GpuError> {
            if self.return_error {
                Err(GpuError::NotSupported("test".into()))
            } else {
                Ok(50.0)
            }
        }
        fn compute_power_watts(&self) -> Result<f64, GpuError> {
            if self.return_error {
                Err(GpuError::NotSupported("test".into()))
            } else {
                Ok(100.0)
            }
        }
        fn compute_power_limit_watts(&self) -> Result<f64, GpuError> {
            if self.return_error {
                Err(GpuError::NotSupported("test".into()))
            } else {
                Ok(200.0)
            }
        }
        fn memory_used_bytes(&self) -> Result<u64, GpuError> {
            if self.return_error {
                Err(GpuError::NotSupported("test".into()))
            } else {
                Ok(1024)
            }
        }
        fn memory_total_bytes(&self) -> Result<u64, GpuError> {
            if self.return_error {
                Err(GpuError::NotSupported("test".into()))
            } else {
                Ok(2048)
            }
        }
        fn memory_bandwidth_gbps(&self) -> Result<f64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn compute_unit_count(&self) -> u32 {
            8
        }
        fn active_compute_units(&self) -> Result<u32, GpuError> {
            Ok(8)
        }
        fn pcie_tx_bytes_per_sec(&self) -> Result<u64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn pcie_rx_bytes_per_sec(&self) -> Result<u64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn pcie_generation(&self) -> u8 {
            0
        }
        fn pcie_width(&self) -> u8 {
            0
        }
        fn refresh(&mut self) -> Result<(), GpuError> {
            Ok(())
        }
    }

    #[test]
    fn h031_error_mock_memory_usage_percent_error() {
        let mock = ErrorMockDevice::new(true);
        // Should propagate the error from memory_used_bytes
        let result = mock.memory_usage_percent();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_memory_available_bytes_error() {
        let mock = ErrorMockDevice::new(true);
        // Should propagate the error from memory_used_bytes
        let result = mock.memory_available_bytes();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_memory_used_mb_error() {
        let mock = ErrorMockDevice::new(true);
        let result = mock.memory_used_mb();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_memory_total_mb_error() {
        let mock = ErrorMockDevice::new(true);
        let result = mock.memory_total_mb();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_memory_total_gb_error() {
        let mock = ErrorMockDevice::new(true);
        let result = mock.memory_total_gb();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_power_usage_percent_error() {
        let mock = ErrorMockDevice::new(true);
        // Should propagate the error from compute_power_watts
        let result = mock.power_usage_percent();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_is_thermal_throttling_error() {
        let mock = ErrorMockDevice::new(true);
        // Should propagate the error from compute_temperature_c
        let result = mock.is_thermal_throttling();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_is_power_throttling_error() {
        let mock = ErrorMockDevice::new(true);
        // Should propagate the error from power_usage_percent -> compute_power_watts
        let result = mock.is_power_throttling();
        assert!(result.is_err());
    }

    #[test]
    fn h031_error_mock_working_correctly() {
        let mock = ErrorMockDevice::new(false);
        // When not returning errors, should work
        assert!(mock.memory_usage_percent().is_ok());
        assert!(mock.memory_available_bytes().is_ok());
        assert!(mock.memory_used_mb().is_ok());
        assert!(mock.memory_total_mb().is_ok());
        assert!(mock.memory_total_gb().is_ok());
        assert!(mock.power_usage_percent().is_ok());
        assert!(mock.is_thermal_throttling().is_ok());
        assert!(mock.is_power_throttling().is_ok());
    }

    // =========================================================================
    // H032: DeviceSnapshot with Errors
    // =========================================================================

    #[test]
    fn h032_device_snapshot_with_errors() {
        let mock = ErrorMockDevice::new(true);
        // DeviceSnapshot::capture uses unwrap_or defaults
        let snapshot = DeviceSnapshot::capture(&mock);
        assert!(snapshot.is_ok());

        let snap = snapshot.unwrap();
        // Should use defaults when metrics fail
        assert_eq!(snap.compute_utilization, 0.0);
        assert_eq!(snap.memory_used_bytes, 0);
        assert_eq!(snap.memory_total_bytes, 0);
        assert_eq!(snap.temperature_c, 0.0);
        assert_eq!(snap.power_watts, 0.0);
        assert_eq!(snap.clock_mhz, 0);
    }

    // =========================================================================
    // H033: ThrottleReason Complete Coverage
    // =========================================================================

    #[test]
    fn h033_throttle_reason_clone() {
        let reason = ThrottleReason::Power;
        let cloned = reason.clone();
        assert_eq!(reason, cloned);
    }

    #[test]
    fn h033_throttle_reason_copy() {
        let reason = ThrottleReason::Thermal;
        let copied: ThrottleReason = reason; // Copy
        assert_eq!(reason, copied);
    }

    #[test]
    fn h033_throttle_reason_equality() {
        assert_eq!(ThrottleReason::None, ThrottleReason::None);
        assert_eq!(ThrottleReason::Thermal, ThrottleReason::Thermal);
        assert_ne!(ThrottleReason::None, ThrottleReason::Thermal);
        assert_ne!(ThrottleReason::Power, ThrottleReason::Thermal);
    }

    #[test]
    fn h033_throttle_reason_debug() {
        let reason = ThrottleReason::HwSlowdown;
        let debug_str = format!("{:?}", reason);
        assert!(debug_str.contains("HwSlowdown"));
    }

    // =========================================================================
    // H034: DeviceType Complete Coverage
    // =========================================================================

    #[test]
    fn h034_device_type_clone() {
        let dt = DeviceType::NvidiaGpu;
        let cloned = dt.clone();
        assert_eq!(dt, cloned);
    }

    #[test]
    fn h034_device_type_copy() {
        let dt = DeviceType::AmdGpu;
        let copied: DeviceType = dt; // Copy
        assert_eq!(dt, copied);
    }

    #[test]
    fn h034_device_type_equality() {
        assert_eq!(DeviceType::Cpu, DeviceType::Cpu);
        assert_ne!(DeviceType::Cpu, DeviceType::NvidiaGpu);
        assert_ne!(DeviceType::NvidiaGpu, DeviceType::AmdGpu);
        assert_ne!(DeviceType::AmdGpu, DeviceType::IntelGpu);
        assert_ne!(DeviceType::IntelGpu, DeviceType::AppleSilicon);
    }

    #[test]
    fn h034_device_type_hash() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(DeviceType::Cpu);
        set.insert(DeviceType::NvidiaGpu);
        set.insert(DeviceType::AmdGpu);
        set.insert(DeviceType::IntelGpu);
        set.insert(DeviceType::AppleSilicon);

        assert_eq!(set.len(), 5);

        // Duplicate should not increase size
        set.insert(DeviceType::Cpu);
        assert_eq!(set.len(), 5);
    }

    // =========================================================================
    // H035: DeviceId Complete Coverage
    // =========================================================================

    #[test]
    fn h035_device_id_copy() {
        let id = DeviceId::nvidia(0);
        let copied: DeviceId = id; // Copy
        assert_eq!(id, copied);
    }

    #[test]
    fn h035_device_id_new_with_all_types() {
        // Test DeviceId::new with all DeviceTypes
        let cpu = DeviceId::new(DeviceType::Cpu, 0);
        let nvidia = DeviceId::new(DeviceType::NvidiaGpu, 1);
        let amd = DeviceId::new(DeviceType::AmdGpu, 2);
        let intel = DeviceId::new(DeviceType::IntelGpu, 3);
        let apple = DeviceId::new(DeviceType::AppleSilicon, 4);

        assert_eq!(cpu.device_type, DeviceType::Cpu);
        assert_eq!(cpu.index, 0);
        assert_eq!(nvidia.device_type, DeviceType::NvidiaGpu);
        assert_eq!(nvidia.index, 1);
        assert_eq!(amd.device_type, DeviceType::AmdGpu);
        assert_eq!(amd.index, 2);
        assert_eq!(intel.device_type, DeviceType::IntelGpu);
        assert_eq!(intel.index, 3);
        assert_eq!(apple.device_type, DeviceType::AppleSilicon);
        assert_eq!(apple.index, 4);
    }

    // =========================================================================
    // H036: CpuDevice Partial Coverage via Edge Cases
    // =========================================================================

    #[test]
    fn h036_cpu_device_memory_used_realistic() {
        let mut cpu = CpuDevice::new();
        cpu.refresh().unwrap();

        // memory_used should be between 0 and total
        let used = cpu.memory_used_bytes().unwrap();
        let total = cpu.memory_total_bytes().unwrap();
        assert!(used <= total);
    }

    // =========================================================================
    // H037: Boundary Tests for Default Implementations
    // =========================================================================

    #[test]
    fn h037_memory_usage_percent_boundary_values() {
        // Test 0% usage
        let mock_empty = MockDevice::new(0, 1000, 0.0, 0.0, 0.0);
        assert!((mock_empty.memory_usage_percent().unwrap() - 0.0).abs() < 0.01);

        // Test 100% usage
        let mock_full = MockDevice::new(1000, 1000, 0.0, 0.0, 0.0);
        assert!((mock_full.memory_usage_percent().unwrap() - 100.0).abs() < 0.01);
    }

    #[test]
    fn h037_memory_available_boundary() {
        // All memory available
        let mock_empty = MockDevice::new(0, 1000, 0.0, 0.0, 0.0);
        assert_eq!(mock_empty.memory_available_bytes().unwrap(), 1000);

        // No memory available
        let mock_full = MockDevice::new(1000, 1000, 0.0, 0.0, 0.0);
        assert_eq!(mock_full.memory_available_bytes().unwrap(), 0);
    }

    #[test]
    fn h037_power_usage_percent_boundary() {
        // 0% power
        let mock_idle = MockDevice::new(0, 0, 0.0, 100.0, 0.0);
        assert!((mock_idle.power_usage_percent().unwrap() - 0.0).abs() < 0.01);

        // 100% power
        let mock_max = MockDevice::new(0, 0, 100.0, 100.0, 0.0);
        assert!((mock_max.power_usage_percent().unwrap() - 100.0).abs() < 0.01);
    }

    // =========================================================================
    // H038: DeviceSnapshot Memory Percent Edge Cases
    // =========================================================================

    #[test]
    fn h038_snapshot_memory_percent_edge_cases() {
        // Test with various memory ratios
        let snap_50 = DeviceSnapshot {
            device_id: DeviceId::cpu(),
            timestamp_ms: 12345,
            compute_utilization: 25.0,
            memory_used_bytes: 500,
            memory_total_bytes: 1000,
            temperature_c: 60.0,
            power_watts: 75.0,
            clock_mhz: 2500,
        };
        assert!((snap_50.memory_usage_percent() - 50.0).abs() < 0.01);

        // Test 0%
        let snap_0 = DeviceSnapshot {
            device_id: DeviceId::cpu(),
            timestamp_ms: 0,
            compute_utilization: 0.0,
            memory_used_bytes: 0,
            memory_total_bytes: 1000,
            temperature_c: 0.0,
            power_watts: 0.0,
            clock_mhz: 0,
        };
        assert!((snap_0.memory_usage_percent() - 0.0).abs() < 0.01);

        // Test 100%
        let snap_100 = DeviceSnapshot {
            device_id: DeviceId::cpu(),
            timestamp_ms: 0,
            compute_utilization: 0.0,
            memory_used_bytes: 1000,
            memory_total_bytes: 1000,
            temperature_c: 0.0,
            power_watts: 0.0,
            clock_mhz: 0,
        };
        assert!((snap_100.memory_usage_percent() - 100.0).abs() < 0.01);
    }

    // =========================================================================
    // H039: CpuDevice Method Coverage via Direct Tests
    // =========================================================================

    #[test]
    fn h039_cpu_device_device_id_and_type() {
        let cpu = CpuDevice::new();
        assert_eq!(cpu.device_id(), DeviceId::cpu());
        assert_eq!(cpu.device_type(), DeviceType::Cpu);
    }

    #[test]
    fn h039_cpu_utilization_initial() {
        let cpu = CpuDevice::new();
        // Initially cpu_usage is 0.0 before refresh
        let util = cpu.compute_utilization().unwrap();
        assert!(util >= 0.0 && util <= 100.0);
    }

    #[test]
    fn h039_cpu_memory_used_initial() {
        let cpu = CpuDevice::new();
        // Initially memory_used is 0 before refresh
        let used = cpu.memory_used_bytes().unwrap();
        assert!(used >= 0);
    }

    #[test]
    fn h039_cpu_active_units_equals_total() {
        let cpu = CpuDevice::new();
        let total = cpu.compute_unit_count();
        let active = cpu.active_compute_units().unwrap();
        assert_eq!(total, active);
    }

    // =========================================================================
    // H040: Additional MockDevice Trait Methods
    // =========================================================================

    #[test]
    fn h040_mock_device_power_limit_access() {
        let mock = MockDevice::new(0, 0, 50.0, 100.0, 0.0);
        let limit = mock.compute_power_limit_watts().unwrap();
        assert!((limit - 100.0).abs() < 0.01);
    }

    #[test]
    fn h040_mock_device_memory_total_access() {
        let mock = MockDevice::new(500, 1000, 0.0, 0.0, 0.0);
        let total = mock.memory_total_bytes().unwrap();
        assert_eq!(total, 1000);
    }

    // =========================================================================
    // H041: ErrorMockDevice Additional Coverage
    // =========================================================================

    #[test]
    fn h041_error_mock_device_name() {
        let mock = ErrorMockDevice::new(false);
        assert_eq!(mock.device_name(), "ErrorMock");
    }

    #[test]
    fn h041_error_mock_device_type() {
        let mock = ErrorMockDevice::new(false);
        assert_eq!(mock.device_type(), DeviceType::Cpu);
    }

    #[test]
    fn h041_error_mock_compute_units() {
        let mock = ErrorMockDevice::new(false);
        assert_eq!(mock.compute_unit_count(), 8);
        assert_eq!(mock.active_compute_units().unwrap(), 8);
    }

    #[test]
    fn h041_error_mock_pcie_metrics() {
        let mock = ErrorMockDevice::new(false);
        assert_eq!(mock.pcie_generation(), 0);
        assert_eq!(mock.pcie_width(), 0);
        assert!(mock.pcie_tx_bytes_per_sec().is_err());
        assert!(mock.pcie_rx_bytes_per_sec().is_err());
    }

    #[test]
    fn h041_error_mock_refresh() {
        let mut mock = ErrorMockDevice::new(false);
        assert!(mock.refresh().is_ok());
    }

    #[test]
    fn h041_error_mock_memory_bandwidth() {
        let mock = ErrorMockDevice::new(false);
        assert!(mock.memory_bandwidth_gbps().is_err());
    }

    // =========================================================================
    // H042: Partial Error Mock for Second-Call Error Propagation
    // =========================================================================

    /// Mock device that returns errors only for total memory (not used)
    /// to test error propagation in memory_usage_percent when second call fails
    struct PartialErrorMockDevice {
        error_on_total: bool,
        error_on_limit: bool,
    }

    impl PartialErrorMockDevice {
        fn with_total_error() -> Self {
            Self {
                error_on_total: true,
                error_on_limit: false,
            }
        }

        fn with_limit_error() -> Self {
            Self {
                error_on_total: false,
                error_on_limit: true,
            }
        }
    }

    impl ComputeDevice for PartialErrorMockDevice {
        fn device_id(&self) -> DeviceId {
            DeviceId::cpu()
        }
        fn device_name(&self) -> &str {
            "PartialErrorMock"
        }
        fn device_type(&self) -> DeviceType {
            DeviceType::Cpu
        }
        fn compute_utilization(&self) -> Result<f64, GpuError> {
            Ok(50.0)
        }
        fn compute_clock_mhz(&self) -> Result<u32, GpuError> {
            Ok(3000)
        }
        fn compute_temperature_c(&self) -> Result<f64, GpuError> {
            Ok(50.0)
        }
        fn compute_power_watts(&self) -> Result<f64, GpuError> {
            Ok(100.0)
        }
        fn compute_power_limit_watts(&self) -> Result<f64, GpuError> {
            if self.error_on_limit {
                Err(GpuError::NotSupported("limit error".into()))
            } else {
                Ok(200.0)
            }
        }
        fn memory_used_bytes(&self) -> Result<u64, GpuError> {
            Ok(1024)
        }
        fn memory_total_bytes(&self) -> Result<u64, GpuError> {
            if self.error_on_total {
                Err(GpuError::NotSupported("total error".into()))
            } else {
                Ok(2048)
            }
        }
        fn memory_bandwidth_gbps(&self) -> Result<f64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn compute_unit_count(&self) -> u32 {
            8
        }
        fn active_compute_units(&self) -> Result<u32, GpuError> {
            Ok(8)
        }
        fn pcie_tx_bytes_per_sec(&self) -> Result<u64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn pcie_rx_bytes_per_sec(&self) -> Result<u64, GpuError> {
            Err(GpuError::NotSupported("mock".into()))
        }
        fn pcie_generation(&self) -> u8 {
            0
        }
        fn pcie_width(&self) -> u8 {
            0
        }
        fn refresh(&mut self) -> Result<(), GpuError> {
            Ok(())
        }
    }

    #[test]
    fn h042_partial_error_memory_usage_percent_total_error() {
        let mock = PartialErrorMockDevice::with_total_error();
        // memory_used_bytes succeeds, but memory_total_bytes fails
        // Should propagate the error from the second call
        let result = mock.memory_usage_percent();
        assert!(result.is_err());
    }

    #[test]
    fn h042_partial_error_memory_available_bytes_total_error() {
        let mock = PartialErrorMockDevice::with_total_error();
        // memory_used_bytes succeeds, but memory_total_bytes fails
        let result = mock.memory_available_bytes();
        assert!(result.is_err());
    }

    #[test]
    fn h042_partial_error_power_usage_percent_limit_error() {
        let mock = PartialErrorMockDevice::with_limit_error();
        // compute_power_watts succeeds, but compute_power_limit_watts fails
        let result = mock.power_usage_percent();
        assert!(result.is_err());
    }

    #[test]
    fn h042_partial_error_device_name() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert_eq!(mock.device_name(), "PartialErrorMock");
    }

    #[test]
    fn h042_partial_error_device_type() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert_eq!(mock.device_type(), DeviceType::Cpu);
    }

    #[test]
    fn h042_partial_error_device_id() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert_eq!(mock.device_id(), DeviceId::cpu());
    }

    #[test]
    fn h042_partial_error_compute_utilization() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert!((mock.compute_utilization().unwrap() - 50.0).abs() < 0.01);
    }

    #[test]
    fn h042_partial_error_compute_clock() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert_eq!(mock.compute_clock_mhz().unwrap(), 3000);
    }

    #[test]
    fn h042_partial_error_compute_temperature() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert!((mock.compute_temperature_c().unwrap() - 50.0).abs() < 0.01);
    }

    #[test]
    fn h042_partial_error_compute_power() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert!((mock.compute_power_watts().unwrap() - 100.0).abs() < 0.01);
    }

    #[test]
    fn h042_partial_error_memory_used() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert_eq!(mock.memory_used_bytes().unwrap(), 1024);
    }

    #[test]
    fn h042_partial_error_compute_units() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert_eq!(mock.compute_unit_count(), 8);
        assert_eq!(mock.active_compute_units().unwrap(), 8);
    }

    #[test]
    fn h042_partial_error_pcie_metrics() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert_eq!(mock.pcie_generation(), 0);
        assert_eq!(mock.pcie_width(), 0);
        assert!(mock.pcie_tx_bytes_per_sec().is_err());
        assert!(mock.pcie_rx_bytes_per_sec().is_err());
    }

    #[test]
    fn h042_partial_error_memory_bandwidth() {
        let mock = PartialErrorMockDevice::with_total_error();
        assert!(mock.memory_bandwidth_gbps().is_err());
    }

    #[test]
    fn h042_partial_error_refresh() {
        let mut mock = PartialErrorMockDevice::with_total_error();
        assert!(mock.refresh().is_ok());
    }

    // =========================================================================
    // H043: DeviceSnapshot Timestamp Coverage
    // =========================================================================

    #[test]
    fn h043_device_snapshot_timestamp_non_zero() {
        let mock = MockDevice::new(1024, 2048, 100.0, 200.0, 50.0);
        let snapshot = DeviceSnapshot::capture(&mock).unwrap();

        // Timestamp should be non-zero (based on system time)
        assert!(snapshot.timestamp_ms > 0);
    }

    #[test]
    fn h043_device_snapshot_all_fields_populated() {
        let mock = MockDevice::new(1024, 2048, 100.0, 200.0, 50.0);
        let snapshot = DeviceSnapshot::capture(&mock).unwrap();

        // Verify all fields are populated from the mock
        assert_eq!(snapshot.device_id, DeviceId::cpu());
        assert!((snapshot.compute_utilization - 50.0).abs() < 0.01);
        assert_eq!(snapshot.memory_used_bytes, 1024);
        assert_eq!(snapshot.memory_total_bytes, 2048);
        assert!((snapshot.temperature_c - 50.0).abs() < 0.01);
        assert!((snapshot.power_watts - 100.0).abs() < 0.01);
        assert_eq!(snapshot.clock_mhz, 3000);
    }

    // =========================================================================
    // H044: CpuDevice Debug Trait Coverage
    // =========================================================================

    #[test]
    fn h044_cpu_device_debug() {
        let cpu = CpuDevice::new();
        let debug_str = format!("{:?}", cpu);
        assert!(debug_str.contains("CpuDevice"));
    }
}