ironmonitor 7.0.2

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
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
//! CPU microarchitecture detection and feature inference.
//!
//! Identifies CPU microarchitecture (Zen 4, Raptor Lake, Firestorm, etc.),
//! process node, pipeline features, ISA extensions, and infers performance
//! characteristics from model identification.
//!
//! ## Platform Support
//!
//! - **Linux**: `/proc/cpuinfo` (family/model/stepping), CPUID via flags
//! - **Windows**: `Win32_Processor` family/model, registry CPU info
//! - **macOS**: `sysctl machdep.cpu`, `hw.cpufamily`

use serde::{Deserialize, Serialize};

use crate::error::IronError;

/// What each platform's `read_cpu_info` returns:
/// `(model_name, family, model, stepping, flags, cores, threads)`.
type CpuInfoFields = (
    String,
    Option<u32>,
    Option<u32>,
    Option<u32>,
    Vec<String>,
    u32,
    u32,
);

/// CPU vendor/designer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CpuVendor {
    Intel,
    AMD,
    Apple,
    Qualcomm,
    Ampere,
    AWS,       // Graviton
    Fujitsu,   // A64FX
    HiSilicon, // Kunpeng
    Samsung,
    MediaTek,
    RiscV,
    Unknown,
}

impl std::fmt::Display for CpuVendor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Intel => write!(f, "Intel"),
            Self::AMD => write!(f, "AMD"),
            Self::Apple => write!(f, "Apple"),
            Self::Qualcomm => write!(f, "Qualcomm"),
            Self::Ampere => write!(f, "Ampere"),
            Self::AWS => write!(f, "AWS"),
            Self::Fujitsu => write!(f, "Fujitsu"),
            Self::HiSilicon => write!(f, "HiSilicon"),
            Self::Samsung => write!(f, "Samsung"),
            Self::MediaTek => write!(f, "MediaTek"),
            Self::RiscV => write!(f, "RISC-V"),
            Self::Unknown => write!(f, "Unknown"),
        }
    }
}

/// CPU microarchitecture identifier.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Microarchitecture {
    /// Microarchitecture name (e.g. "Zen 4", "Raptor Lake", "Firestorm").
    pub name: String,
    /// Code name (e.g. "Raphael", "Raptor Lake-S").
    pub codename: String,
    /// Manufacturing process node in nanometers (e.g. 5, 7, 10).
    pub process_nm: u32,
    /// Year of introduction.
    pub year: u32,
    /// CPU vendor.
    pub vendor: CpuVendor,
    /// Architecture family (e.g. "x86_64", "aarch64", "riscv64").
    pub arch: String,
    /// Whether this is a hybrid/big.LITTLE design.
    pub hybrid: bool,
    /// Performance core uarch name (for hybrid designs).
    pub p_core_uarch: Option<String>,
    /// Efficiency core uarch name (for hybrid designs).
    pub e_core_uarch: Option<String>,
}

/// ISA extension category.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IsaCategory {
    Simd,
    Crypto,
    Virtualization,
    Security,
    MachineLearning,
    Atomic,
    FloatingPoint,
    BitManip,
    Memory,
    Other,
}

/// An ISA extension or CPU feature.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IsaExtension {
    /// Extension name (e.g. "AVX-512", "AES-NI", "SVE").
    pub name: String,
    /// Category.
    pub category: IsaCategory,
    /// Whether this extension is present.
    pub supported: bool,
    /// Brief description.
    pub description: String,
}

/// Inferred performance characteristics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InferredPerformance {
    /// Estimated single-thread performance score (0-100).
    pub single_thread_score: u32,
    /// Estimated multi-thread scaling efficiency (0-100).
    pub mt_scaling_efficiency: u32,
    /// Estimated IPC (instructions per clock) relative to baseline.
    pub relative_ipc: f64,
    /// Estimated max boost clock in MHz (from model inference).
    pub estimated_boost_mhz: u32,
    /// Estimated TDP in watts.
    pub estimated_tdp_watts: u32,
    /// Target workload classification.
    pub target_workloads: Vec<String>,
    /// Known limitations or notes.
    pub notes: Vec<String>,
}

/// Full CPU microarchitecture report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CpuMicroarchReport {
    /// CPU model name string.
    pub model_name: String,
    /// CPUID family.
    /// CPUID family, where it is readable.
    ///
    /// `None` on Windows: WMI does not expose the CPUID triple, and the reader
    /// returned `(0, 0, 0)` under a comment saying so. Family 0 is not a value
    /// any modern x86 CPU reports — a Ryzen 9 9900X is family 0x1A — so the
    /// report published an impossible triple rather than an absent one. The
    /// ontology already withheld these on Windows; this is the same decision
    /// one layer down.
    pub family: Option<u32>,
    /// CPUID model.
    /// CPUID model, where it is readable. See [`Self::family`].
    pub model: Option<u32>,
    /// CPUID stepping.
    /// CPUID stepping, where it is readable. See [`Self::family`].
    pub stepping: Option<u32>,
    /// Detected microarchitecture.
    pub microarch: Microarchitecture,
    /// ISA extensions.
    pub extensions: Vec<IsaExtension>,
    /// Inferred performance.
    pub performance: InferredPerformance,
    /// Physical core count.
    pub physical_cores: u32,
    /// Logical core count (with SMT/HT).
    pub logical_cores: u32,
    /// Whether SMT/Hyper-Threading is enabled.
    pub smt_enabled: bool,
}

/// CPU microarchitecture monitor.
pub struct CpuMicroarchMonitor {
    report: CpuMicroarchReport,
}

/// Pull `(family, model, stepping)` out of a Windows processor description.
///
/// Both `Win32_Processor.Description` and the `PROCESSOR_IDENTIFIER`
/// environment variable use the same shape:
///
/// ```text
/// AMD64 Family 26 Model 68 Stepping 0
/// AMD64 Family 26 Model 68 Stepping 0, AuthenticAMD
/// x86 Family 6 Model 142 Stepping 10
/// ```
///
/// Parsed by keyword rather than by position, so a vendor suffix or an extra
/// leading token does not shift the fields. All three must be present: two
/// thirds of a CPUID triple is not one, and reporting a family without a model
/// would invite exactly the "close enough" matching this crate avoids.
#[cfg(target_os = "windows")]
fn parse_cpuid_triple(desc: &str) -> Option<(u32, u32, u32)> {
    let after = |key: &str| -> Option<u32> {
        let tokens: Vec<&str> = desc.split_whitespace().collect();
        let idx = tokens.iter().position(|t| t.eq_ignore_ascii_case(key))?;
        tokens
            .get(idx + 1)?
            .trim_end_matches(',')
            .parse::<u32>()
            .ok()
    };
    Some((after("Family")?, after("Model")?, after("Stepping")?))
}

impl CpuMicroarchMonitor {
    /// Create a new monitor and detect microarchitecture.
    pub fn new() -> Result<Self, IronError> {
        let (model_name, family, model, stepping, flags, cores, threads) = Self::read_cpu_info()?;
        let microarch = Self::identify_microarch(&model_name, family, model, stepping);
        let extensions = Self::detect_extensions(&flags, &microarch);
        let performance = Self::infer_performance(&model_name, &microarch, cores);
        let smt_enabled = threads > cores && cores > 0;

        Ok(Self {
            report: CpuMicroarchReport {
                model_name,
                family,
                model,
                stepping,
                microarch,
                extensions,
                performance,
                physical_cores: cores,
                logical_cores: threads,
                smt_enabled,
            },
        })
    }

    /// Get the full report.
    pub fn report(&self) -> &CpuMicroarchReport {
        &self.report
    }

    /// Get only extensions that are supported.
    pub fn supported_extensions(&self) -> Vec<&IsaExtension> {
        self.report
            .extensions
            .iter()
            .filter(|e| e.supported)
            .collect()
    }

    /// Check if a specific extension is supported by name.
    pub fn has_extension(&self, name: &str) -> bool {
        let upper = name.to_uppercase();
        self.report
            .extensions
            .iter()
            .any(|e| e.supported && e.name.to_uppercase().contains(&upper))
    }

    /// Identify microarchitecture from CPUID family/model and model name.
    fn identify_microarch(
        model_name: &str,
        family: Option<u32>,
        model: Option<u32>,
        _stepping: Option<u32>,
    ) -> Microarchitecture {
        let upper = model_name.to_uppercase();

        // Try vendor detection first — AMD checked before Intel to avoid
        // "CORE" matching in "16-Core Processor" AMD strings
        let vendor = if upper.contains("AMD")
            || upper.contains("RYZEN")
            || upper.contains("EPYC")
            || upper.contains("THREADRIPPER")
            || upper.contains("ATHLON")
        {
            CpuVendor::AMD
        } else if upper.contains("INTEL")
            || upper.contains("CORE")
            || upper.contains("XEON")
            || upper.contains("CELERON")
            || upper.contains("PENTIUM")
            || upper.contains("ATOM")
        {
            CpuVendor::Intel
        } else if upper.contains("APPLE")
            || upper.contains("M1")
            || upper.contains("M2")
            || upper.contains("M3")
            || upper.contains("M4")
        {
            CpuVendor::Apple
        } else if upper.contains("GRAVITON") {
            CpuVendor::AWS
        } else if upper.contains("AMPERE") || upper.contains("ALTRA") {
            CpuVendor::Ampere
        } else if upper.contains("SNAPDRAGON") || upper.contains("QUALCOMM") {
            CpuVendor::Qualcomm
        } else {
            CpuVendor::Unknown
        };

        match vendor {
            CpuVendor::Intel => Self::identify_intel(&upper, family, model),
            CpuVendor::AMD => Self::identify_amd(&upper, family, model),
            CpuVendor::Apple => Self::identify_apple(&upper),
            CpuVendor::AWS => Microarchitecture {
                name: "Neoverse".into(),
                codename: if upper.contains("GRAVITON4") {
                    "Graviton4".into()
                } else if upper.contains("GRAVITON3") {
                    "Graviton3".into()
                } else {
                    "Graviton2".into()
                },
                process_nm: 5,
                year: 2023,
                vendor,
                arch: "aarch64".into(),
                hybrid: false,
                p_core_uarch: None,
                e_core_uarch: None,
            },
            _ => Microarchitecture {
                name: "Unknown".into(),
                codename: String::new(),
                process_nm: 0,
                year: 0,
                vendor,
                arch: if cfg!(target_arch = "x86_64") {
                    "x86_64".into()
                } else if cfg!(target_arch = "aarch64") {
                    "aarch64".into()
                } else {
                    "unknown".into()
                },
                hybrid: false,
                p_core_uarch: None,
                e_core_uarch: None,
            },
        }
    }

    fn identify_intel(name: &str, family: Option<u32>, model: Option<u32>) -> Microarchitecture {
        // Intel identification via model name patterns and CPUID family/model
        let (uarch, codename, process, year, hybrid, p_core, e_core) = if name.contains("ULTRA 9 2")
            || name.contains("ULTRA 7 2")
            || name.contains("ULTRA 5 2")
            || name.contains("ARROW LAKE")
        {
            (
                "Arrow Lake",
                "Arrow Lake",
                3,
                2024,
                true,
                Some("Lion Cove"),
                Some("Skymont"),
            )
        } else if name.contains("14TH GEN")
            || name.contains("14900")
            || name.contains("14700")
            || name.contains("14600")
            || name.contains("RAPTOR LAKE")
            || (family == Some(6) && matches!(model, Some(0xB7) | Some(0xBF)))
        {
            (
                "Raptor Lake",
                "Raptor Lake Refresh",
                7,
                2023,
                true,
                Some("Raptor Cove"),
                Some("Gracemont"),
            )
        } else if name.contains("13TH GEN")
            || name.contains("13900")
            || name.contains("13700")
            || name.contains("13600")
        {
            (
                "Raptor Lake",
                "Raptor Lake",
                7,
                2022,
                true,
                Some("Raptor Cove"),
                Some("Gracemont"),
            )
        } else if name.contains("12TH GEN")
            || name.contains("12900")
            || name.contains("12700")
            || name.contains("12600")
            || name.contains("ALDER LAKE")
            || (family == Some(6) && model == Some(0x97))
        {
            (
                "Alder Lake",
                "Alder Lake",
                7,
                2021,
                true,
                Some("Golden Cove"),
                Some("Gracemont"),
            )
        } else if name.contains("11TH GEN")
            || name.contains("11900")
            || name.contains("11700")
            || name.contains("ROCKET LAKE")
            || (family == Some(6) && model == Some(0xA7))
        {
            ("Cypress Cove", "Rocket Lake", 14, 2021, false, None, None)
        } else if name.contains("TIGER LAKE") || name.contains("1165") || name.contains("1185") {
            ("Willow Cove", "Tiger Lake", 10, 2020, false, None, None)
        } else if name.contains("10TH GEN")
            || name.contains("10900")
            || name.contains("10700")
            || name.contains("COMET LAKE")
        {
            ("Skylake", "Comet Lake", 14, 2020, false, None, None)
        } else if name.contains("ICE LAKE") || name.contains("1065") || name.contains("1035") {
            ("Sunny Cove", "Ice Lake", 10, 2019, false, None, None)
        } else if name.contains("9TH GEN") || name.contains("9900") || name.contains("COFFEE LAKE")
        {
            (
                "Skylake",
                "Coffee Lake Refresh",
                14,
                2018,
                false,
                None,
                None,
            )
        } else if name.contains("8TH GEN") || name.contains("8700") {
            ("Skylake", "Coffee Lake", 14, 2017, false, None, None)
        } else if name.contains("SAPPHIRE RAPIDS")
            || name.contains("W9-3")
            || name.contains("W7-3")
            || name.contains("W5-3")
        {
            ("Golden Cove", "Sapphire Rapids", 7, 2023, false, None, None)
        } else if name.contains("EMERALD RAPIDS") {
            ("Golden Cove", "Emerald Rapids", 7, 2023, false, None, None)
        } else if name.contains("GRANITE RAPIDS") {
            ("Redwood Cove", "Granite Rapids", 3, 2024, false, None, None)
        } else if name.contains("SIERRA FOREST") {
            ("Crestmont", "Sierra Forest", 3, 2024, false, None, None)
        } else if name.contains("LUNAR LAKE") {
            (
                "Lion Cove",
                "Lunar Lake",
                3,
                2024,
                true,
                Some("Lion Cove"),
                Some("Skymont"),
            )
        } else {
            ("Unknown Intel", "", 0, 0, false, None, None)
        };

        Microarchitecture {
            name: uarch.into(),
            codename: codename.into(),
            process_nm: process,
            year,
            vendor: CpuVendor::Intel,
            arch: "x86_64".into(),
            hybrid,
            p_core_uarch: p_core.map(String::from),
            e_core_uarch: e_core.map(String::from),
        }
    }

    fn identify_amd(name: &str, _family: Option<u32>, _model: Option<u32>) -> Microarchitecture {
        let (uarch, codename, process, year) = if name.contains("9950")
            || name.contains("9900")
            || name.contains("9700")
            || name.contains("9600")
            || name.contains("ZEN 5")
            || name.contains("GRANITE RIDGE")
        {
            ("Zen 5", "Granite Ridge", 4, 2024)
        } else if name.contains("7950")
            || name.contains("7900")
            || name.contains("7800")
            || name.contains("7700")
            || name.contains("7600")
            || name.contains("7500")
            || name.contains("ZEN 4")
        {
            ("Zen 4", "Raphael", 5, 2022)
        } else if name.contains("5950")
            || name.contains("5900")
            || name.contains("5800")
            || name.contains("5700")
            || name.contains("5600")
            || name.contains("5500")
            || name.contains("ZEN 3")
        {
            ("Zen 3", "Vermeer", 7, 2020)
        } else if name.contains("3950")
            || name.contains("3900")
            || name.contains("3800")
            || name.contains("3700")
            || name.contains("3600")
            || name.contains("ZEN 2")
        {
            ("Zen 2", "Matisse", 7, 2019)
        } else if name.contains("2700") || name.contains("2600") || name.contains("ZEN+") {
            ("Zen+", "Pinnacle Ridge", 12, 2018)
        } else if name.contains("1800") || name.contains("1700") || name.contains("ZEN 1") {
            ("Zen", "Summit Ridge", 14, 2017)
        } else if name.contains("EPYC 9") || name.contains("GENOA") {
            ("Zen 4", "Genoa", 5, 2022)
        } else if name.contains("EPYC 8") || name.contains("SIENA") {
            ("Zen 4c", "Siena", 5, 2023)
        } else if name.contains("EPYC 7")
            && (name.contains("73") || name.contains("75") || name.contains("79"))
        {
            ("Zen 3", "Milan", 7, 2021)
        } else if name.contains("TURIN") {
            ("Zen 5", "Turin", 4, 2024)
        } else if name.contains("THREADRIPPER 7") || name.contains("PRO 7") {
            ("Zen 4", "Storm Peak", 5, 2023)
        } else if name.contains("THREADRIPPER 5") || name.contains("PRO 5") {
            ("Zen 3", "Chagall", 7, 2022)
        } else {
            ("Unknown AMD", "", 0, 0)
        };

        Microarchitecture {
            name: uarch.into(),
            codename: codename.into(),
            process_nm: process,
            year,
            vendor: CpuVendor::AMD,
            arch: "x86_64".into(),
            hybrid: false,
            p_core_uarch: None,
            e_core_uarch: None,
        }
    }

    fn identify_apple(name: &str) -> Microarchitecture {
        let (uarch, codename, process, year, hybrid) = if name.contains("M4 MAX")
            || name.contains("M4 PRO")
            || name.contains("M4 ULTRA")
        {
            ("Everest", "M4 Pro/Max/Ultra", 3, 2024, true)
        } else if name.contains("M4") {
            ("Everest", "M4", 3, 2024, true)
        } else if name.contains("M3 MAX") || name.contains("M3 PRO") || name.contains("M3 ULTRA") {
            ("Ibiza", "M3 Pro/Max/Ultra", 3, 2023, true)
        } else if name.contains("M3") {
            ("Ibiza", "M3", 3, 2023, true)
        } else if name.contains("M2 MAX") || name.contains("M2 PRO") || name.contains("M2 ULTRA") {
            ("Avalanche/Blizzard", "M2 Pro/Max/Ultra", 5, 2023, true)
        } else if name.contains("M2") {
            ("Avalanche/Blizzard", "M2", 5, 2022, true)
        } else if name.contains("M1 MAX") || name.contains("M1 PRO") || name.contains("M1 ULTRA") {
            ("Firestorm/Icestorm", "M1 Pro/Max/Ultra", 5, 2021, true)
        } else if name.contains("M1") {
            ("Firestorm/Icestorm", "M1", 5, 2020, true)
        } else {
            ("Unknown Apple", "", 0, 0, true)
        };

        Microarchitecture {
            name: uarch.into(),
            codename: codename.into(),
            process_nm: process,
            year,
            vendor: CpuVendor::Apple,
            arch: "aarch64".into(),
            hybrid,
            p_core_uarch: Some(uarch.split('/').next().unwrap_or("P-core").into()),
            e_core_uarch: Some(uarch.split('/').nth(1).unwrap_or("E-core").into()),
        }
    }

    /// Detect ISA extensions from CPU flags.
    fn detect_extensions(flags: &[String], microarch: &Microarchitecture) -> Vec<IsaExtension> {
        let flag_set: std::collections::HashSet<String> =
            flags.iter().map(|f| f.to_lowercase()).collect();

        let mut extensions = Vec::new();

        // x86_64 extensions
        if microarch.arch == "x86_64" {
            let x86_exts = vec![
                ("sse", IsaCategory::Simd, "SSE", "Streaming SIMD Extensions"),
                (
                    "sse2",
                    IsaCategory::Simd,
                    "SSE2",
                    "Streaming SIMD Extensions 2",
                ),
                (
                    "sse3",
                    IsaCategory::Simd,
                    "SSE3",
                    "Streaming SIMD Extensions 3",
                ),
                ("ssse3", IsaCategory::Simd, "SSSE3", "Supplemental SSE3"),
                (
                    "sse4_1",
                    IsaCategory::Simd,
                    "SSE4.1",
                    "Streaming SIMD Extensions 4.1",
                ),
                (
                    "sse4_2",
                    IsaCategory::Simd,
                    "SSE4.2",
                    "Streaming SIMD Extensions 4.2",
                ),
                (
                    "avx",
                    IsaCategory::Simd,
                    "AVX",
                    "Advanced Vector Extensions",
                ),
                (
                    "avx2",
                    IsaCategory::Simd,
                    "AVX2",
                    "Advanced Vector Extensions 2",
                ),
                (
                    "avx512f",
                    IsaCategory::Simd,
                    "AVX-512F",
                    "AVX-512 Foundation",
                ),
                (
                    "avx512bw",
                    IsaCategory::Simd,
                    "AVX-512BW",
                    "AVX-512 Byte/Word",
                ),
                (
                    "avx512vl",
                    IsaCategory::Simd,
                    "AVX-512VL",
                    "AVX-512 Vector Length",
                ),
                (
                    "avx512cd",
                    IsaCategory::Simd,
                    "AVX-512CD",
                    "AVX-512 Conflict Detection",
                ),
                (
                    "avx512vnni",
                    IsaCategory::Simd,
                    "AVX-512 VNNI",
                    "Vector Neural Network Instructions",
                ),
                (
                    "avx512_bf16",
                    IsaCategory::MachineLearning,
                    "AVX-512 BF16",
                    "BFloat16 instructions",
                ),
                (
                    "avx512_fp16",
                    IsaCategory::FloatingPoint,
                    "AVX-512 FP16",
                    "Half-precision floating point",
                ),
                (
                    "amx_tile",
                    IsaCategory::MachineLearning,
                    "AMX-TILE",
                    "Advanced Matrix Extensions - Tile",
                ),
                (
                    "amx_bf16",
                    IsaCategory::MachineLearning,
                    "AMX-BF16",
                    "Advanced Matrix Extensions - BF16",
                ),
                (
                    "amx_int8",
                    IsaCategory::MachineLearning,
                    "AMX-INT8",
                    "Advanced Matrix Extensions - INT8",
                ),
                ("aes", IsaCategory::Crypto, "AES-NI", "AES New Instructions"),
                ("vaes", IsaCategory::Crypto, "VAES", "Vectorized AES"),
                (
                    "sha_ni",
                    IsaCategory::Crypto,
                    "SHA-NI",
                    "SHA New Instructions",
                ),
                (
                    "pclmulqdq",
                    IsaCategory::Crypto,
                    "PCLMULQDQ",
                    "Carry-Less Multiplication",
                ),
                (
                    "rdrand",
                    IsaCategory::Crypto,
                    "RDRAND",
                    "Hardware Random Number Generator",
                ),
                (
                    "rdseed",
                    IsaCategory::Crypto,
                    "RDSEED",
                    "Hardware Random Seed Generator",
                ),
                (
                    "vmx",
                    IsaCategory::Virtualization,
                    "VMX",
                    "Virtual Machine Extensions (VT-x)",
                ),
                (
                    "svm",
                    IsaCategory::Virtualization,
                    "SVM",
                    "Secure Virtual Machine (AMD-V)",
                ),
                (
                    "sgx",
                    IsaCategory::Security,
                    "SGX",
                    "Software Guard Extensions",
                ),
                (
                    "sev",
                    IsaCategory::Security,
                    "SEV",
                    "Secure Encrypted Virtualization",
                ),
                (
                    "sme",
                    IsaCategory::Security,
                    "SME",
                    "Secure Memory Encryption",
                ),
                (
                    "fma",
                    IsaCategory::FloatingPoint,
                    "FMA3",
                    "Fused Multiply-Add 3-operand",
                ),
                (
                    "f16c",
                    IsaCategory::FloatingPoint,
                    "F16C",
                    "Half-precision float conversion",
                ),
                (
                    "bmi1",
                    IsaCategory::BitManip,
                    "BMI1",
                    "Bit Manipulation Instructions 1",
                ),
                (
                    "bmi2",
                    IsaCategory::BitManip,
                    "BMI2",
                    "Bit Manipulation Instructions 2",
                ),
                (
                    "popcnt",
                    IsaCategory::BitManip,
                    "POPCNT",
                    "Population Count",
                ),
                (
                    "lzcnt",
                    IsaCategory::BitManip,
                    "LZCNT",
                    "Leading Zero Count",
                ),
                (
                    "cx16",
                    IsaCategory::Atomic,
                    "CMPXCHG16B",
                    "Compare and Exchange 16 bytes",
                ),
                (
                    "tsx",
                    IsaCategory::Memory,
                    "TSX",
                    "Transactional Synchronization Extensions",
                ),
                (
                    "mpx",
                    IsaCategory::Memory,
                    "MPX",
                    "Memory Protection Extensions",
                ),
                (
                    "clflushopt",
                    IsaCategory::Memory,
                    "CLFLUSHOPT",
                    "Optimized Cache Line Flush",
                ),
                ("clwb", IsaCategory::Memory, "CLWB", "Cache Line Write Back"),
            ];

            for (flag, cat, name, desc) in x86_exts {
                extensions.push(IsaExtension {
                    name: name.into(),
                    category: cat,
                    supported: flag_set.contains(flag),
                    description: desc.into(),
                });
            }
        }

        // AArch64 extensions (inferred from microarch when not available via flags)
        if microarch.arch == "aarch64" {
            let aarch_exts = vec![
                ("asimd", IsaCategory::Simd, "NEON/ASIMD", "Advanced SIMD"),
                ("sve", IsaCategory::Simd, "SVE", "Scalable Vector Extension"),
                (
                    "sve2",
                    IsaCategory::Simd,
                    "SVE2",
                    "Scalable Vector Extension 2",
                ),
                (
                    "aes",
                    IsaCategory::Crypto,
                    "AES",
                    "AES cryptographic instructions",
                ),
                (
                    "sha1",
                    IsaCategory::Crypto,
                    "SHA-1",
                    "SHA-1 cryptographic instructions",
                ),
                (
                    "sha2",
                    IsaCategory::Crypto,
                    "SHA-2",
                    "SHA-256 cryptographic instructions",
                ),
                (
                    "sha3",
                    IsaCategory::Crypto,
                    "SHA-3",
                    "SHA-3 cryptographic instructions",
                ),
                (
                    "pmull",
                    IsaCategory::Crypto,
                    "PMULL",
                    "Polynomial multiply long",
                ),
                (
                    "atomics",
                    IsaCategory::Atomic,
                    "LSE Atomics",
                    "Large System Extensions atomics",
                ),
                (
                    "fphp",
                    IsaCategory::FloatingPoint,
                    "FP16",
                    "Half-precision floating point",
                ),
                (
                    "bf16",
                    IsaCategory::MachineLearning,
                    "BF16",
                    "BFloat16 instructions",
                ),
                (
                    "i8mm",
                    IsaCategory::MachineLearning,
                    "I8MM",
                    "Int8 matrix multiply",
                ),
                (
                    "dotprod",
                    IsaCategory::MachineLearning,
                    "DotProd",
                    "Dot product instructions",
                ),
            ];

            for (flag, cat, name, desc) in aarch_exts {
                extensions.push(IsaExtension {
                    name: name.into(),
                    category: cat,
                    supported: flag_set.contains(flag),
                    description: desc.into(),
                });
            }
        }

        extensions
    }

    /// Infer performance characteristics from microarchitecture.
    fn infer_performance(
        model_name: &str,
        microarch: &Microarchitecture,
        cores: u32,
    ) -> InferredPerformance {
        let upper = model_name.to_uppercase();

        // Base IPC by microarchitecture
        let (base_ipc, st_score, tdp, boost) = match microarch.name.as_str() {
            // Intel
            "Arrow Lake" | "Lion Cove" => (2.1, 92, 125, 5800),
            "Raptor Lake" | "Raptor Cove" => (2.0, 90, 125, 5800),
            "Alder Lake" | "Golden Cove" => (1.9, 85, 125, 5200),
            "Cypress Cove" => (1.7, 78, 125, 5300),
            "Willow Cove" => (1.8, 80, 28, 4800),
            "Sunny Cove" => (1.6, 72, 28, 3900),
            "Skylake" => (1.5, 68, 95, 5000),
            "Redwood Cove" => (2.0, 88, 350, 5300),
            "Crestmont" => (1.4, 55, 270, 3200),
            // AMD
            "Zen 5" => (2.2, 95, 170, 5700),
            "Zen 4" => (2.0, 90, 170, 5700),
            "Zen 3" => (1.9, 85, 105, 4900),
            "Zen 2" => (1.7, 77, 105, 4700),
            "Zen+" => (1.5, 68, 105, 4350),
            "Zen" => (1.4, 64, 95, 4100),
            "Zen 4c" => (1.8, 75, 225, 3700),
            // Apple
            "Everest" => (2.3, 98, 30, 4400),
            "Ibiza" => (2.2, 95, 22, 4100),
            "Avalanche/Blizzard" => (2.1, 92, 20, 3500),
            "Firestorm/Icestorm" => (2.0, 88, 15, 3200),
            // ARM server
            "Neoverse" => (1.6, 65, 250, 3400),
            _ => (1.0, 50, 65, 3000),
        };

        // Adjust for specific SKU
        let tdp_adjusted = if upper.contains("XEON") || upper.contains("EPYC") {
            tdp.max(200)
        } else if upper.contains("LAPTOP") || upper.contains("U)") || upper.contains("P)") {
            tdp.min(28)
        } else if upper.contains("H)") || upper.contains("HX)") {
            tdp.clamp(45, 55)
        } else {
            tdp
        };

        // MT scaling
        let mt_efficiency = if cores <= 4 {
            90
        } else if cores <= 8 {
            85
        } else if cores <= 16 {
            78
        } else if cores <= 32 {
            72
        } else {
            65
        };

        // Target workloads
        let mut workloads = Vec::new();
        if st_score >= 85 {
            workloads.push("Gaming".into());
            workloads.push("Desktop productivity".into());
        }
        if cores >= 8 {
            workloads.push("Content creation".into());
            workloads.push("Compilation".into());
        }
        if cores >= 16 {
            workloads.push("Video encoding".into());
        }
        if cores >= 32 || upper.contains("XEON") || upper.contains("EPYC") {
            workloads.push("Server workloads".into());
            workloads.push("Virtualization".into());
        }
        if microarch.vendor == CpuVendor::Apple {
            workloads.push("Efficient mobile computing".into());
        }

        let mut notes = Vec::new();
        if microarch.hybrid {
            notes.push("Hybrid architecture: scheduler-aware threading recommended".into());
        }
        if microarch.process_nm > 0 && microarch.process_nm <= 5 {
            notes.push(format!("Leading-edge {}nm process", microarch.process_nm));
        }

        InferredPerformance {
            single_thread_score: st_score,
            mt_scaling_efficiency: mt_efficiency,
            relative_ipc: base_ipc,
            estimated_boost_mhz: boost,
            estimated_tdp_watts: tdp_adjusted,
            target_workloads: workloads,
            notes,
        }
    }

    #[cfg(target_os = "linux")]
    fn read_cpu_info() -> Result<CpuInfoFields, IronError> {
        let cpuinfo = std::fs::read_to_string("/proc/cpuinfo").map_err(IronError::Io)?;

        let mut model_name = String::new();
        // `None` until /proc/cpuinfo supplies them. Zero was both the
        // "not seen yet" marker and a publishable value.
        let mut family: Option<u32> = None;
        let mut model: Option<u32> = None;
        let mut stepping: Option<u32> = None;
        let mut flags = Vec::new();
        let mut siblings = 0u32;
        let mut cores = 0u32;

        for line in cpuinfo.lines() {
            if let Some((key, val)) = line.split_once(':') {
                let key = key.trim();
                let val = val.trim();
                match key {
                    "model name" if model_name.is_empty() => model_name = val.into(),
                    "cpu family" if family.is_none() => {
                        family = val.parse().ok();
                    }
                    "model" if model.is_none() => {
                        model = val.parse().ok();
                    }
                    "stepping" if stepping.is_none() => {
                        stepping = val.parse().ok();
                    }
                    "flags" if flags.is_empty() => {
                        flags = val.split_whitespace().map(String::from).collect();
                    }
                    "siblings" if siblings == 0 => {
                        siblings = val.parse().unwrap_or(0);
                    }
                    "cpu cores" if cores == 0 => {
                        cores = val.parse().unwrap_or(0);
                    }
                    _ => {}
                }
            }
        }

        if siblings == 0 {
            siblings = cores;
        }

        Ok((model_name, family, model, stepping, flags, cores, siblings))
    }

    #[cfg(target_os = "windows")]
    fn read_cpu_info() -> Result<CpuInfoFields, IronError> {
        let output = std::process::Command::new("powershell")
            .args([
                "-NoProfile",
                "-Command",
                "Get-CimInstance Win32_Processor | Select-Object Name,Description,Family,NumberOfCores,NumberOfLogicalProcessors | ConvertTo-Json",
            ])
            .output()
            .map_err(IronError::Io)?;

        let text = String::from_utf8_lossy(&output.stdout);
        let val: serde_json::Value = serde_json::from_str(text.trim()).unwrap_or_default();

        // `Win32_Processor.Name` is space-padded to a fixed width; the same
        // padding reached hardware_ai before it was trimmed there.
        let model_name = val["Name"]
            .as_str()
            .unwrap_or("Unknown CPU")
            .trim()
            .to_string();
        // `0`, not `1`. The resolver decides whether a core count was read by
        // testing `physical_cores > 0` and reports "the platform reported no
        // physical core count" when it is zero -- so a fallback of 1 does not
        // merely guess low, it **defeats the guard**: `cpu.cores.physical`,
        // `cpu.microarch.physical_cores` and `cpu.microarch.logical_cores` all
        // publish 1 as a measurement, and `smt_enabled` publishes false.
        //
        // Same shape as the cache line size that sat at a sentinel of 64 and
        // slipped past the guard written to catch it. The Linux arm has always
        // used 0 here, and so does `Default`.
        let cores = val["NumberOfCores"].as_u64().unwrap_or(0) as u32;
        let threads = val["NumberOfLogicalProcessors"]
            .as_u64()
            .unwrap_or(cores as u64) as u32;

        // WMI does expose the CPUID triple, and the comment that used to sit
        // here said it does not. `Win32_Processor.Description` is the string
        // "AMD64 Family 26 Model 68 Stepping 0", and `PROCESSOR_IDENTIFIER`
        // carries the same text -- which this file was already reading, two
        // functions down, for something else.
        //
        // Description first because it comes from the query already running;
        // the environment variable is the fallback, and is what a process
        // launched under an unusual environment might be missing.
        let (family, model, stepping) = val["Description"]
            .as_str()
            .and_then(parse_cpuid_triple)
            .or_else(|| {
                std::env::var("PROCESSOR_IDENTIFIER")
                    .ok()
                    .as_deref()
                    .and_then(parse_cpuid_triple)
            })
            .map_or((None, None, None), |(f, m, s)| (Some(f), Some(m), Some(s)));

        let flags = Self::read_windows_cpu_features();

        Ok((model_name, family, model, stepping, flags, cores, threads))
    }

    #[cfg(target_os = "windows")]
    fn read_windows_cpu_features() -> Vec<String> {
        // NOTE: the flags below are asserted from the architecture rather than
        // read from CPUID, which is why the ontology publishes them with
        // `specification` provenance rather than `measured`. Every x86_64 part
        // has them; that is a fact about the instruction set, not about this
        // chip.
        // Detect features from environment or registry
        // This is a simplified version - real implementation would use CPUID
        let mut flags = Vec::new();

        // Try PROCESSOR_IDENTIFIER env var
        if let Ok(id) = std::env::var("PROCESSOR_IDENTIFIER") {
            let upper = id.to_uppercase();
            if upper.contains("INTEL") || upper.contains("AMD") {
                // Modern x86_64 CPUs all have these
                flags.extend(
                    [
                        "sse", "sse2", "sse3", "ssse3", "sse4_1", "sse4_2", "popcnt", "cx16",
                    ]
                    .iter()
                    .map(|s| s.to_string()),
                );
            }
        }

        flags
    }

    #[cfg(target_os = "macos")]
    fn read_cpu_info() -> Result<CpuInfoFields, IronError> {
        let brand = std::process::Command::new("sysctl")
            .args(["-n", "machdep.cpu.brand_string"])
            .output()
            .ok()
            .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
            .unwrap_or_default();

        let family = std::process::Command::new("sysctl")
            .args(["-n", "machdep.cpu.family"])
            .output()
            .ok()
            .and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok());

        let model = std::process::Command::new("sysctl")
            .args(["-n", "machdep.cpu.model"])
            .output()
            .ok()
            .and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok());

        let stepping = std::process::Command::new("sysctl")
            .args(["-n", "machdep.cpu.stepping"])
            .output()
            .ok()
            .and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok());

        let features_str = std::process::Command::new("sysctl")
            .args(["-n", "machdep.cpu.features"])
            .output()
            .ok()
            .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
            .unwrap_or_default();

        let flags: Vec<String> = features_str
            .split_whitespace()
            .map(|s| s.to_lowercase())
            .collect();

        // `0` for the same reason as the Windows arm above: the resolver reads
        // zero as "not reported" and says so, and a fallback of 1 publishes a
        // single-core machine instead.
        let cores = std::process::Command::new("sysctl")
            .args(["-n", "hw.physicalcpu"])
            .output()
            .ok()
            .and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok())
            .unwrap_or(0);

        let threads = std::process::Command::new("sysctl")
            .args(["-n", "hw.logicalcpu"])
            .output()
            .ok()
            .and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok())
            .unwrap_or(cores);

        Ok((brand, family, model, stepping, flags, cores, threads))
    }

    /// A platform with no reader here reports nothing, including no core count.
    ///
    /// This returned `1, 1` -- one physical core and one logical -- which the
    /// resolver publishes as a measurement, because its "was this read" test is
    /// `> 0`. Every other field in the tuple was already empty or zero for
    /// exactly that reason; the two counts were the exception.
    #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
    fn read_cpu_info() -> Result<CpuInfoFields, IronError> {
        Ok((String::new(), 0, 0, 0, Vec::new(), 0, 0))
    }
}

/// The CPU's identity -- its model name and physical core count -- probed
/// once per process.
///
/// **One cache for every surface that needs these two facts.** Before this,
/// the TUI kept its own `OnceLock` fed from a background thread, instance 25
/// added a second, blocking one to `backend.rs`, and `agent/state.rs` read
/// neither: it set `cores` to the logical count and, on macOS, named the CPU
/// "Apple Silicon / Intel". Four other modules each read
/// `machdep.cpu.brand_string` themselves. Every surface that disagrees with
/// another about how many cores a machine has is a surface that read the count
/// its own way, so there is now one way.
///
/// Both fields are `None` where the platform did not report them. A physical
/// count of `0` from the reader means "not reported", and the logical count is
/// never substituted for it -- that substitution is the defect this exists to
/// end.
#[derive(Debug, Clone, Default)]
pub struct CpuIdentity {
    /// Marketing name, e.g. "AMD Ryzen 9 9900X 12-Core Processor".
    pub model_name: Option<String>,
    /// Physical cores, distinct from hardware threads.
    pub physical_cores: Option<usize>,
}

static CPU_IDENTITY: std::sync::OnceLock<CpuIdentity> = std::sync::OnceLock::new();

/// The CPU's identity, probing on first use and blocking until it has it.
///
/// Probing can take a noticeable time on Windows, where it goes through WMI.
/// Call this only off any thread that draws: the CLI, the agent's `ask()` path
/// (already off the UI thread, and already waiting on a model), or a
/// background thread. A UI thread uses [`cpu_identity_if_known`].
pub fn cpu_identity() -> &'static CpuIdentity {
    CPU_IDENTITY.get_or_init(|| {
        let Ok(monitor) = CpuMicroarchMonitor::new() else {
            return CpuIdentity::default();
        };
        let report = monitor.report();
        CpuIdentity {
            model_name: Some(report.model_name.trim().to_string()).filter(|n| !n.is_empty()),
            physical_cores: Some(report.physical_cores as usize).filter(|&p| p > 0),
        }
    })
}

/// The CPU's identity if it has already been probed, without blocking.
///
/// `None` both before the probe has run and after it has run and found
/// nothing; a display treats the two the same, by showing what it does know.
pub fn cpu_identity_if_known() -> Option<&'static CpuIdentity> {
    CPU_IDENTITY.get()
}

/// Physical core count; see [`cpu_identity`] for when it may block.
pub fn physical_cores() -> Option<usize> {
    cpu_identity().physical_cores
}

/// Physical core count if already probed; never blocks.
pub fn physical_cores_if_known() -> Option<usize> {
    cpu_identity_if_known().and_then(|id| id.physical_cores)
}

impl Default for CpuMicroarchMonitor {
    fn default() -> Self {
        Self::new().unwrap_or(Self {
            report: CpuMicroarchReport {
                model_name: String::new(),
                family: None,
                model: None,
                stepping: None,
                microarch: Microarchitecture {
                    name: "Unknown".into(),
                    codename: String::new(),
                    process_nm: 0,
                    year: 0,
                    vendor: CpuVendor::Unknown,
                    arch: String::new(),
                    hybrid: false,
                    p_core_uarch: None,
                    e_core_uarch: None,
                },
                extensions: Vec::new(),
                performance: InferredPerformance {
                    single_thread_score: 0,
                    mt_scaling_efficiency: 0,
                    relative_ipc: 0.0,
                    estimated_boost_mhz: 0,
                    estimated_tdp_watts: 0,
                    target_workloads: Vec::new(),
                    notes: Vec::new(),
                },
                physical_cores: 0,
                logical_cores: 0,
                smt_enabled: false,
            },
        })
    }
}

#[cfg(all(test, target_os = "windows"))]
mod cpuid_triple_tests {
    use super::parse_cpuid_triple;

    #[test]
    fn a_windows_processor_description_yields_its_cpuid_triple() {
        // Exactly what `Win32_Processor.Description` returns on the machine
        // this was written against, and what `PROCESSOR_IDENTIFIER` returns
        // there with the vendor appended.
        assert_eq!(
            parse_cpuid_triple("AMD64 Family 26 Model 68 Stepping 0"),
            Some((26, 68, 0))
        );
        assert_eq!(
            parse_cpuid_triple("AMD64 Family 26 Model 68 Stepping 0, AuthenticAMD"),
            Some((26, 68, 0))
        );
        // An Intel form, with a different leading token.
        assert_eq!(
            parse_cpuid_triple("x86 Family 6 Model 142 Stepping 10"),
            Some((6, 142, 10))
        );
    }

    #[test]
    fn two_thirds_of_a_triple_is_not_a_triple() {
        // A partial reading is an absent one. Publishing a family with no
        // model invites matching a CPU against the wrong microarchitecture,
        // which is the whole reason these three travel together.
        assert_eq!(parse_cpuid_triple("AMD64 Family 26 Model 68"), None);
        assert_eq!(parse_cpuid_triple("AMD64 Family 26"), None);
        assert_eq!(parse_cpuid_triple(""), None);
        assert_eq!(parse_cpuid_triple("Intel64 Family Model Stepping"), None);
        // A vendor string that merely mentions the words.
        assert_eq!(parse_cpuid_triple("Family Values Model Agency"), None);
    }
}

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

    #[test]
    fn test_intel_raptor_lake() {
        let uarch = CpuMicroarchMonitor::identify_microarch(
            "13th Gen Intel(R) Core(TM) i9-13900K",
            Some(6),
            Some(0xB7),
            Some(1),
        );
        assert_eq!(uarch.name, "Raptor Lake");
        assert_eq!(uarch.vendor, CpuVendor::Intel);
        assert!(uarch.hybrid);
        assert_eq!(uarch.process_nm, 7);
    }

    #[test]
    fn test_amd_zen4() {
        let uarch = CpuMicroarchMonitor::identify_microarch(
            "AMD Ryzen 9 7950X 16-Core Processor",
            Some(25),
            Some(97),
            Some(2),
        );
        assert_eq!(uarch.name, "Zen 4");
        assert_eq!(uarch.vendor, CpuVendor::AMD);
        assert!(!uarch.hybrid);
        assert_eq!(uarch.process_nm, 5);
    }

    /// The published CPUID triple is absent where it was not read, not zero.
    ///
    /// `read_cpu_info` on Windows returned `(0, 0, 0)` under a comment saying
    /// WMI does not expose the triple. Family 0 is not a value any modern x86
    /// CPU reports — a Ryzen 9 9900X is family 0x1A — so the report carried an
    /// impossible triple rather than an absent one, and `ironmon`'s own ontology
    /// had to test `family > 0` to work out whether to publish it.
    #[test]
    fn an_unread_cpuid_triple_is_absent_not_zero() {
        let report = CpuMicroarchMonitor::new()
            .expect("a report")
            .report()
            .clone();

        // Whatever the platform, the three go together: all read or none.
        let present = [
            report.family.is_some(),
            report.model.is_some(),
            report.stepping.is_some(),
        ];
        assert!(
            present.iter().all(|p| *p) || present.iter().all(|p| !*p),
            concat!(
                "family/model/stepping are decoded from one CPUID leaf ",
                "and must not be published separately: {:?}"
            ),
            present
        );

        assert_ne!(
            report.family,
            Some(0),
            "family 0 is not a value any modern x86 CPU reports"
        );
    }

    /// `Win32_Processor.Name` is space-padded to a fixed width.
    #[test]
    fn the_model_name_carries_no_padding() {
        let report = CpuMicroarchMonitor::new()
            .expect("a report")
            .report()
            .clone();
        assert_eq!(
            report.model_name.trim(),
            report.model_name,
            "the model name kept its WMI padding: {:?}",
            report.model_name
        );
    }

    #[test]
    fn test_apple_m3() {
        // Apple reports no x86 CPUID triple; identification is by name.
        let uarch = CpuMicroarchMonitor::identify_microarch("Apple M3 Pro", None, None, None);
        assert_eq!(uarch.name, "Ibiza");
        assert_eq!(uarch.vendor, CpuVendor::Apple);
        assert!(uarch.hybrid);
        assert_eq!(uarch.process_nm, 3);
    }

    #[test]
    fn test_performance_inference() {
        let uarch = Microarchitecture {
            name: "Zen 4".into(),
            codename: "Raphael".into(),
            process_nm: 5,
            year: 2022,
            vendor: CpuVendor::AMD,
            arch: "x86_64".into(),
            hybrid: false,
            p_core_uarch: None,
            e_core_uarch: None,
        };
        let perf = CpuMicroarchMonitor::infer_performance("AMD Ryzen 9 7950X", &uarch, 16);
        assert!(perf.single_thread_score >= 85);
        assert!(perf.relative_ipc >= 1.8);
        assert!(perf.estimated_tdp_watts > 100);
    }

    #[test]
    fn test_extension_detection() {
        let flags: Vec<String> = vec!["avx2", "aes", "fma", "bmi2", "popcnt", "avx512f"]
            .into_iter()
            .map(String::from)
            .collect();
        let uarch = Microarchitecture {
            name: "Zen 4".into(),
            codename: "".into(),
            process_nm: 5,
            year: 2022,
            vendor: CpuVendor::AMD,
            arch: "x86_64".into(),
            hybrid: false,
            p_core_uarch: None,
            e_core_uarch: None,
        };
        let exts = CpuMicroarchMonitor::detect_extensions(&flags, &uarch);
        let avx2 = exts.iter().find(|e| e.name == "AVX2").unwrap();
        assert!(avx2.supported);
        let avx512 = exts.iter().find(|e| e.name == "AVX-512F").unwrap();
        assert!(avx512.supported);
    }

    #[test]
    fn test_monitor_default() {
        let monitor = CpuMicroarchMonitor::default();
        let _report = monitor.report();
    }

    #[test]
    fn test_serialization() {
        let uarch = Microarchitecture {
            name: "Zen 4".into(),
            codename: "Raphael".into(),
            process_nm: 5,
            year: 2022,
            vendor: CpuVendor::AMD,
            arch: "x86_64".into(),
            hybrid: false,
            p_core_uarch: None,
            e_core_uarch: None,
        };
        let json = serde_json::to_string(&uarch).unwrap();
        assert!(json.contains("Zen 4"));
        let _: Microarchitecture = serde_json::from_str(&json).unwrap();
    }
}