ironmonitor 7.1.0

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
//! Lock-free snapshot pipeline: decouples hardware collection from rendering.
//!
//! # Why this exists
//!
//! Before this module, every frontend (CLI, TUI, GUI) called blocking collectors
//! directly on its render thread. A slow WMI or NVML query would stall a frame,
//! and [`crate::backend::MonitoringBackend::update`] ran every collector serially,
//! so the slowest source set the floor for all of them.
//!
//! This module inverts that:
//!
//! - A dedicated **collector thread** owns all hardware handles and never shares them.
//! - Independent collectors run **concurrently** within each tick, so total tick cost
//!   is the slowest single collector rather than their sum.
//! - Results are published as an immutable [`Snapshot`] into an [`ArcSwap`] slot.
//!   Readers do a lock-free atomic load and never block, never wait on a mutex, and
//!   never touch a hardware API.
//!
//! # Thread-safety design
//!
//! [`crate::backend::MonitoringBackend`] cannot be moved across threads: it stores
//! `Vec<Box<dyn DiskDevice>>` and `Vec<Box<dyn MotherboardDevice>>`, trait objects
//! with no `Send` bound. Rather than retrofit `Send` onto those traits, the collector
//! thread **constructs its own sources** and only ever sends plain owned data
//! ([`Snapshot`]) across the boundary. Nothing that touches a driver ever crosses a
//! thread.
//!
//! On Windows this also matters for COM: `wmi` initializes COM per-thread, and
//! `disk::windows` contains a `COMLibrary::assume_initialized()` path that is only
//! sound on a thread where COM was actually initialized. Every thread this module
//! spawns therefore calls [`com_guard`] first and holds the guard for its lifetime.

use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use arc_swap::ArcSwap;
use serde::{Deserialize, Serialize};

use crate::connections::{ConnectionInfo, ConnectionMonitor};
use crate::core::cpu::CpuStats;
use crate::core::memory::MemoryStats;
use crate::gpu::{GpuCollection, GpuDynamicInfo, GpuStaticInfo};
use crate::network_monitor::NetworkMonitor;
use crate::process_monitor::{ProcessMonitor, ProcessMonitorInfo};
use crate::system_stats::SystemStats;

/// Default collector tick interval.
pub const DEFAULT_TICK: Duration = Duration::from_millis(1000);

/// Default number of samples retained per history series.
pub const DEFAULT_HISTORY: usize = 120;

// ============================================================================
// COM initialization guard
// ============================================================================

/// Per-thread COM guard.
///
/// On Windows this initializes COM for the calling thread and keeps it initialized
/// for as long as the returned value is held. Every thread that may reach a WMI
/// collector must hold one, otherwise `COMLibrary::assume_initialized()` in
/// `disk::windows` is unsound.
///
/// On non-Windows targets this is a zero-sized no-op.
#[cfg(target_os = "windows")]
pub fn com_guard() -> Option<wmi::COMLibrary> {
    wmi::COMLibrary::new().ok()
}

/// Per-thread COM guard (no-op on non-Windows targets).
///
/// Returns `Option<()>` rather than `()` so the five call sites can keep binding
/// the result to hold the guard for the scope, as they must on Windows. Returning
/// unit made every one of those a `let _x = ()`, which clippy rejects — and the
/// fix belongs here rather than in five cfg'd bindings at the call sites.
#[cfg(not(target_os = "windows"))]
pub fn com_guard() -> Option<()> {
    None
}

// ============================================================================
// Plain-data snapshot types
// ============================================================================

/// A disk row, flattened to plain data so it can cross a thread boundary.
///
/// `Box<dyn DiskDevice>` is not `Send`, so the collector flattens it here.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DiskSnapshot {
    /// Device model or drive letter.
    pub name: String,
    /// Mount point or drive root.
    pub mount_point: String,
    /// Total capacity in bytes, or `None` where it could not be read.
    ///
    /// These were `u64`, and the non-Windows path wrote `0` for both whenever
    /// `filesystem_info` returned nothing -- a disk of zero bytes, zero of them
    /// in use, which is not a disk. Consumers then guarded with `total > 0`,
    /// and that guard is why the fabrication was survivable and also why it was
    /// invisible: the sentinel and the real absence are the same value.
    pub total: Option<u64>,
    /// Used capacity in bytes, or `None` where it could not be read.
    pub used: Option<u64>,
    /// Filesystem label (NTFS, ext4, ...).
    pub filesystem: String,
    /// Read throughput in bytes/sec, or `None` where no rate was established.
    ///
    /// This was `f64` and it was **zero on every platform**. Windows takes the
    /// cheap `logical_drives` path, which reads capacity and no counters at
    /// all, and hardcoded `0.0`; the other platforms wrote
    /// `io.read_throughput.unwrap_or(0)`, and `read_throughput` is itself
    /// always `None` because a rate needs two samples to difference and
    /// `DiskIoStats` is built from one.
    ///
    /// So every consumer -- the TUI, the GUI, and
    /// `ironmon_disk_read_bytes_per_sec` -- has been drawing a flat line at zero
    /// and calling it disk throughput.
    pub read_rate: Option<f64>,
    /// Write throughput in bytes/sec. See [`Self::read_rate`].
    pub write_rate: Option<f64>,
}

/// Cumulative I/O counters for one physical device.
///
/// Kept separate from [`DiskSnapshot`] rather than folded into it, because the
/// two are keyed differently and joining them is its own problem.
/// `DiskSnapshot` is per filesystem — a drive letter or mount point — and these
/// counters are per physical drive; on Windows one physical drive can carry
/// several letters and one letter can span drives, so attributing a device
/// counter to a mount point needs a partition map nobody has written.
///
/// Publishing them under their own device label sidesteps that entirely, and is
/// what the metric means anyway: `ironmon_disk_read_bytes_total{device="..."}` is
/// a property of the hardware, not of a mount.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DiskIoSnapshot {
    /// Device name, e.g. `PhysicalDrive0` or `nvme0n1`.
    pub device: String,
    /// Bytes read from this device since boot, or `None` where the counter
    /// was not read. Not the same as a device that has read nothing.
    pub read_bytes: Option<u64>,
    /// Bytes written to this device since boot, or `None` where unread.
    pub write_bytes: Option<u64>,
}

/// A network interface row with computed bandwidth rates.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct NetSnapshot {
    /// Interface name.
    pub name: String,
    /// Interface is up, or `None` where the state was not read.
    pub is_up: Option<bool>,
    /// Cumulative bytes received.
    pub rx_bytes: u64,
    /// Cumulative bytes transmitted.
    pub tx_bytes: u64,
    /// Receive rate in bytes/sec.
    /// Receive rate, or `None` until a second sample establishes one.
    ///
    /// A rate is a difference between two readings. This was a bare
    /// number and the first reading reported `0`, which says the link
    /// is idle -- and because the baseline was overwritten before the
    /// subtraction, every reading was the first reading.
    pub rx_rate: Option<f64>,
    /// Transmit rate in bytes/sec.
    /// Transmit rate. See [`Self::rx_rate`].
    pub tx_rate: Option<f64>,
    /// Link speed in Mbps, if known.
    pub speed_mbps: Option<u32>,
}

/// Per-collector wall-clock cost for the most recent tick, in microseconds.
///
/// Exposed so the TUI/GUI can surface which source is the bottleneck, and so
/// regressions in collection cost are observable rather than guessed at.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct StageTimings {
    /// CPU collection cost.
    pub cpu_us: u64,
    /// Memory collection cost.
    pub memory_us: u64,
    /// GPU collection cost.
    pub gpu_us: u64,
    /// Process enumeration cost.
    pub process_us: u64,
    /// Network collection cost.
    pub network_us: u64,
    /// Connection table cost.
    pub connection_us: u64,
    /// Disk enumeration cost.
    pub disk_us: u64,
    /// System stats cost.
    pub system_us: u64,
}

impl StageTimings {
    /// Cost of the slowest single collector — the theoretical floor for a tick
    /// once collectors run concurrently.
    pub fn critical_path_us(&self) -> u64 {
        [
            self.cpu_us,
            self.memory_us,
            self.gpu_us,
            self.process_us,
            self.network_us,
            self.connection_us,
            self.disk_us,
            self.system_us,
        ]
        .into_iter()
        .max()
        .unwrap_or(0)
    }

    /// Cost if every collector had run serially — what the old `update()` paid.
    pub fn serial_us(&self) -> u64 {
        self.cpu_us
            + self.memory_us
            + self.gpu_us
            + self.process_us
            + self.network_us
            + self.connection_us
            + self.disk_us
            + self.system_us
    }
}

/// Ring-buffer history series, stored as plain `Vec` so the snapshot stays trivially
/// cloneable and serializable.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Histories {
    /// CPU utilization percentage over time.
    pub cpu: Vec<f32>,
    /// Memory utilization percentage over time.
    pub memory: Vec<f32>,
    /// Per-GPU utilization percentage over time.
    pub gpu_util: Vec<Vec<f32>>,
    /// Per-GPU memory utilization percentage over time.
    pub gpu_memory: Vec<Vec<f32>>,
    /// Per-GPU temperature over time.
    pub gpu_temp: Vec<Vec<f32>>,
    /// Aggregate network receive rate over time.
    pub net_rx: Vec<f32>,
    /// Aggregate network transmit rate over time.
    pub net_tx: Vec<f32>,
}

/// An immutable, self-contained view of system state at one instant.
///
/// Cheap to share: readers hold an `Arc<Snapshot>` and never copy the contents.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Snapshot {
    /// Monotonically increasing tick counter. Readers compare this against a cached
    /// value to decide whether any recomputation (formatting, layout) is needed.
    pub generation: u64,
    /// Wall-clock capture time, seconds since the Unix epoch. `None` when the
    /// clock could not be read, and in a default snapshot: a snapshot with no
    /// time is never stored at the epoch.
    pub collected_at: Option<u64>,
    /// Total wall-clock cost of this tick.
    pub collect_us: u64,
    /// Per-collector breakdown of this tick.
    pub timings: StageTimings,
    /// Whether the process stage read the process table this tick. False on cached
    /// ticks, and also when no monitor exists or the enumeration failed; in those cases
    /// `processes` holds the last successful table, if any.
    #[serde(default)]
    pub process_sampled: bool,
    /// Whether the connection stage ran this tick rather than reusing its table.
    #[serde(default)]
    pub connections_sampled: bool,
    /// Per-protocol connection failures on a refresh tick; None on cached ticks.
    #[serde(default)]
    pub connection_errors: Option<Vec<String>>,

    /// CPU statistics, if the platform collector succeeded.
    pub cpu: Option<CpuStats>,
    /// Memory statistics, if the platform collector succeeded.
    pub memory: Option<MemoryStats>,
    /// Whether this is the warm-up pass, published before the slow sources exist.
    ///
    /// The collector publishes one snapshot from an empty [`Sources`] so that CPU
    /// and memory — available in under a millisecond — reach the UI without
    /// waiting on GPU enumeration. **That snapshot reports no GPUs, no processes
    /// and no connections, and nothing in it said so.** An empty `gpu_static` is
    /// what a machine with no GPU also produces, so a consumer reading the first
    /// generation could not tell "none here" from "not looked at yet" — the
    /// distinction this whole crate exists to preserve.
    ///
    /// A one-shot consumer should wait for `!warmup`; a continuously repainting
    /// one can ignore it, because the next generation supersedes it.
    pub warmup: bool,
    /// Static GPU descriptors, captured once at startup.
    pub gpu_static: Vec<GpuStaticInfo>,
    /// Per-tick GPU telemetry, index-aligned with [`Snapshot::gpu_static`].
    ///
    /// `None` marks a device whose query failed this tick. Failures keep their slot
    /// rather than being dropped, so a flaky device cannot silently renumber the
    /// GPUs beside it.
    pub gpu_dynamic: Vec<Option<GpuDynamicInfo>>,
    /// Process table with GPU attribution.
    pub processes: Vec<ProcessMonitorInfo>,
    /// Open network connections.
    pub connections: Vec<ConnectionInfo>,
    /// Mounted disks.
    pub disks: Vec<DiskSnapshot>,
    /// Cumulative I/O counters, per physical device. See [`DiskIoSnapshot`].
    pub disk_io: Vec<DiskIoSnapshot>,
    /// CPU temperature sensors, one entry per readable sensor.
    ///
    /// Empty where the CPU exposes none, which is a common case rather than a
    /// failure: this crate's own development machine reads zero sensors through
    /// every one of the four Windows paths `hwmon` tries. Empty means "nothing
    /// to report", never "0 degrees".
    pub cpu_temperatures: Vec<crate::hwmon::HwSensor>,
    /// Active network interfaces.
    pub network: Vec<NetSnapshot>,
    /// Load average / vmstat style system counters.
    pub system_stats: Option<SystemStats>,
    /// Retained time series.
    pub histories: Histories,
}

impl Snapshot {
    /// CPU utilization percentage, or 0.0 when unavailable.
    pub fn cpu_utilization(&self) -> Option<f32> {
        self.cpu.as_ref().map(|c| 100.0 - c.total.idle)
    }

    /// Memory utilization percentage, or `None` when memory was not read.
    /// See [`Self::cpu_utilization`].
    pub fn memory_utilization(&self) -> Option<f32> {
        self.memory.as_ref().and_then(|m| m.ram_usage_percent())
    }

    /// Aggregate receive rate across all interfaces, in bytes/sec.
    ///
    /// `None` unless every interface has an established rate, and `None` when there
    /// are no interfaces. A sum over only the interfaces that happened to have one
    /// was published as the machine's total, so a link still establishing its
    /// rate, or one that never will, silently dropped out of the figure.
    pub fn total_rx_rate(&self) -> Option<f64> {
        self.sum_rates(|n| n.rx_rate)
    }

    /// Aggregate transmit rate across all interfaces, in bytes/sec. See
    /// [`Self::total_rx_rate`].
    pub fn total_tx_rate(&self) -> Option<f64> {
        self.sum_rates(|n| n.tx_rate)
    }

    /// Sum one rate over every interface, or `None` if any interface lacks it.
    fn sum_rates(&self, rate: impl Fn(&NetSnapshot) -> Option<f64>) -> Option<f64> {
        if self.network.is_empty() {
            return None;
        }
        self.network.iter().map(rate).sum::<Option<f64>>()
    }
}

// ============================================================================
// Reader handle
// ============================================================================

/// A cheap, cloneable reader handle onto the newest [`Snapshot`].
///
/// Cloning is refcount-only. [`SnapshotHandle::latest`] is a lock-free atomic load,
/// safe to call every frame from any number of threads.
#[derive(Clone)]
pub struct SnapshotHandle {
    slot: Arc<ArcSwap<Snapshot>>,
    stop: Arc<AtomicBool>,
    interval_ms: Arc<AtomicU64>,
}

impl SnapshotHandle {
    /// Load the newest snapshot. Lock-free; never blocks on the collector.
    pub fn latest(&self) -> Arc<Snapshot> {
        self.slot.load_full()
    }

    /// Generation of the newest snapshot, without cloning the `Arc`.
    ///
    /// Use this to skip work when nothing has changed since the last frame.
    pub fn generation(&self) -> u64 {
        self.slot.load().generation
    }

    /// Change the collector tick interval. Takes effect on the next tick.
    pub fn set_interval(&self, interval: Duration) {
        self.interval_ms
            .store(interval.as_millis() as u64, Ordering::Relaxed);
    }

    /// Current collector tick interval.
    pub fn interval(&self) -> Duration {
        Duration::from_millis(self.interval_ms.load(Ordering::Relaxed))
    }

    /// Ask the collector thread to shut down after its current tick.
    pub fn request_stop(&self) {
        self.stop.store(true, Ordering::Relaxed);
    }

    /// Whether a stop has been requested.
    pub fn is_stopping(&self) -> bool {
        self.stop.load(Ordering::Relaxed)
    }
}

// ============================================================================
// Collector
// ============================================================================

/// Called on the collector thread immediately after each snapshot is published.
///
/// Must not block: it runs inline on the collector between the store and the next
/// tick's sleep. Waking a UI is the intended use.
pub type PublishHook = Arc<dyn Fn() + Send + Sync>;

/// Collector configuration.
#[derive(Clone)]
pub struct CollectorConfig {
    /// Interval between ticks.
    pub interval: Duration,
    /// Samples retained per history series.
    pub history_size: usize,
    /// Collect the process table. Disable for lightweight headless use.
    pub collect_processes: bool,
    /// Collect the open-connection table (relatively expensive on Windows).
    pub collect_connections: bool,
    /// Re-enumerate disks every N ticks. Disk topology changes rarely, so
    /// re-enumerating every tick wastes a comparatively expensive WMI call.
    pub disk_every_n_ticks: u64,
    /// Re-read the process table every N ticks, reusing the previous table between.
    ///
    /// This is the most expensive stage by a wide margin — 970 ms for 484 processes
    /// on a Windows box, because each entry costs an `OpenProcess` plus a SID lookup.
    /// Front-ends display it far less often than they display CPU and GPU, so paying
    /// for it every tick pins a core for data nobody is looking at.
    pub process_every_n_ticks: u64,
    /// Re-read the open-connection table every N ticks.
    pub connection_every_n_ticks: u64,
    /// Largest share of wall-clock time collection may occupy, in `0.05..=1.0`.
    ///
    /// The collector sleeps until at least `tick_cost / max_duty_cycle` has passed,
    /// so a tick that costs more than the interval allows stretches the interval
    /// instead of pinning a core. At the 0.5 default a 500ms tick still publishes
    /// every second; a 1.5s tick publishes every three seconds rather than
    /// continuously.
    pub max_duty_cycle: f32,
    /// Invoked after every publish, on the collector thread.
    ///
    /// Without this a front-end has to poll: it wakes on a timer, checks the
    /// generation, and usually finds nothing. Polling at a fraction of the tick makes
    /// the observed update interval jitter by that fraction — the display advances
    /// twice in quick succession, then appears to stall — and the jitter is worst
    /// when tick cost varies, which is exactly when a machine is busy. A hook lets
    /// the collector wake the reader at the instant data lands, so the visible
    /// cadence is the collector's cadence.
    pub on_publish: Option<PublishHook>,
}

impl std::fmt::Debug for CollectorConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CollectorConfig")
            .field("interval", &self.interval)
            .field("history_size", &self.history_size)
            .field("collect_processes", &self.collect_processes)
            .field("collect_connections", &self.collect_connections)
            .field("disk_every_n_ticks", &self.disk_every_n_ticks)
            .field("process_every_n_ticks", &self.process_every_n_ticks)
            .field("connection_every_n_ticks", &self.connection_every_n_ticks)
            .field("max_duty_cycle", &self.max_duty_cycle)
            .field("on_publish", &self.on_publish.is_some())
            .finish()
    }
}

impl Default for CollectorConfig {
    fn default() -> Self {
        Self {
            interval: DEFAULT_TICK,
            history_size: DEFAULT_HISTORY,
            collect_processes: true,
            collect_connections: true,
            disk_every_n_ticks: 10,
            process_every_n_ticks: 1,
            connection_every_n_ticks: 1,
            max_duty_cycle: 0.5,
            on_publish: None,
        }
    }
}

/// Owns the collector thread. Dropping this stops and joins the thread.
pub struct Collector {
    handle: SnapshotHandle,
    join: Option<JoinHandle<()>>,
}

impl Collector {
    /// Spawn the collector thread and return immediately.
    ///
    /// The thread constructs its own hardware handles, so no driver state is ever
    /// shared. The first snapshot may briefly be [`Snapshot::default`] until the
    /// first tick lands; readers should treat generation 0 as "not ready yet".
    pub fn spawn(config: CollectorConfig) -> Self {
        let slot = Arc::new(ArcSwap::from_pointee(Snapshot::default()));
        let stop = Arc::new(AtomicBool::new(false));
        let interval_ms = Arc::new(AtomicU64::new(config.interval.as_millis() as u64));

        let handle = SnapshotHandle {
            slot: Arc::clone(&slot),
            stop: Arc::clone(&stop),
            interval_ms: Arc::clone(&interval_ms),
        };

        let join = thread::Builder::new()
            .name("ironmon-collector".into())
            .spawn(move || collector_loop(config, slot, stop, interval_ms))
            .ok();

        Self { handle, join }
    }

    /// A cloneable reader handle for the UI threads.
    pub fn handle(&self) -> SnapshotHandle {
        self.handle.clone()
    }
}

impl Drop for Collector {
    fn drop(&mut self) {
        self.handle.request_stop();
        if let Some(join) = self.join.take() {
            let _ = join.join();
        }
    }
}

/// Hardware handles owned exclusively by the collector thread.
struct Sources {
    gpu: Option<GpuCollection>,
    processes: Option<ProcessMonitor>,
    network: Option<NetworkMonitor>,
    connections: Option<ConnectionMonitor>,
}

/// Bounded push that keeps the newest `cap` samples.
fn push_capped(series: &mut Vec<f32>, value: f32, cap: usize) {
    if series.len() >= cap {
        let overflow = series.len() + 1 - cap;
        series.drain(..overflow);
    }
    series.push(value);
}

fn collector_loop(
    config: CollectorConfig,
    slot: Arc<ArcSwap<Snapshot>>,
    stop: Arc<AtomicBool>,
    interval_ms: Arc<AtomicU64>,
) {
    // COM must be initialized on this thread before any WMI collector runs, and
    // must stay initialized for the thread's lifetime.
    com_guard();

    let mut histories = Histories::default();
    let mut generation: u64 = 0;
    let mut cached_disks: Vec<DiskSnapshot> = Vec::new();
    let mut cached_disk_io: Vec<DiskIoSnapshot> = Vec::new();
    // Carried between ticks so a decimated stage keeps its last reading rather than
    // publishing an empty table on the ticks it does not run.
    let mut cached_processes: Vec<ProcessMonitorInfo> = Vec::new();
    let mut cached_connections: Vec<ConnectionInfo> = Vec::new();

    // === Warm-up publish ===
    //
    // Constructing the hardware sources is slow: `GpuCollection::auto_detect` spins
    // up vendor drivers (NVML, DXGI) and, on a multi-GPU host, measured 8-12s before
    // it returned. Publishing nothing until it finished left every UI blank for that
    // whole window, even though CPU and memory were available in under a millisecond.
    //
    // So publish those first. `collect_once` already treats every source as optional,
    // so passing an empty `Sources` yields exactly the collectors that need no setup
    // — CPU, memory, system stats and disks — and leaves the rest empty until the
    // real sources exist. Consumers key off `generation`, so this simply arrives as
    // the first update and is superseded a moment later.
    let mut warmup_sources = Sources {
        gpu: None,
        processes: None,
        network: None,
        connections: None,
    };
    generation += 1;
    let mut warmup = collect_once(
        &mut warmup_sources,
        &config,
        &mut histories,
        &mut cached_disks,
        &mut cached_disk_io,
        &mut cached_processes,
        &mut cached_connections,
        &[],
        generation,
    );
    // Say so in the snapshot itself. Every later generation leaves this false
    // through `Default`.
    warmup.warmup = true;
    slot.store(Arc::new(warmup));
    if let Some(ref hook) = config.on_publish {
        hook();
    }

    if stop.load(Ordering::Relaxed) {
        return;
    }

    let mut sources = Sources {
        gpu: GpuCollection::auto_detect().ok(),
        processes: if config.collect_processes {
            ProcessMonitor::new().ok()
        } else {
            None
        },
        network: NetworkMonitor::new().ok(),
        connections: if config.collect_connections {
            ConnectionMonitor::new().ok()
        } else {
            None
        },
    };

    // Static GPU descriptors are captured once; they do not change at runtime.
    //
    // Read from each device's static query alone, in collection order so they stay
    // index-aligned with `collect_gpu`. This went through `snapshot_all`, which also
    // takes a dynamic sample and fails if any device's sample fails. On Apple
    // silicon the sample comes from `powermetrics`, which needs root, so a
    // non-root GUI reported no accelerators on a machine whose GPU had been
    // enumerated.
    let gpu_static: Vec<GpuStaticInfo> = sources
        .gpu
        .as_ref()
        .and_then(|g| {
            g.gpus()
                .iter()
                .map(|gpu| gpu.static_info())
                .collect::<Result<Vec<_>, _>>()
                .ok()
        })
        .unwrap_or_default();

    // Size the per-GPU series now that the device count is known. The warm-up pass
    // ran with none, so these start empty.
    histories.gpu_util = vec![Vec::new(); gpu_static.len()];
    histories.gpu_memory = vec![Vec::new(); gpu_static.len()];
    histories.gpu_temp = vec![Vec::new(); gpu_static.len()];

    while !stop.load(Ordering::Relaxed) {
        let tick_start = Instant::now();
        generation += 1;

        let snapshot = collect_once(
            &mut sources,
            &config,
            &mut histories,
            &mut cached_disks,
            &mut cached_disk_io,
            &mut cached_processes,
            &mut cached_connections,
            &gpu_static,
            generation,
        );

        slot.store(Arc::new(snapshot));
        if let Some(ref hook) = config.on_publish {
            hook();
        }

        // Sleep the remainder of the interval, re-reading it each tick so the UI can
        // retune cadence live. Waking in short slices keeps shutdown responsive even
        // at long intervals.
        let interval = Duration::from_millis(interval_ms.load(Ordering::Relaxed).max(50));
        let elapsed = tick_start.elapsed();

        // Never let collection occupy more than `max_duty_cycle` of wall time.
        //
        // Collection cost is not a constant of the machine — it is a function of how
        // busy the machine is. NVML queries against a GPU running sustained compute
        // took 515ms here versus 1.6ms for an idle card of the same model, and the
        // WMI GPU counter classes slow down under load the same way. A fixed interval
        // therefore behaves worst exactly when the user is watching: the collector
        // takes a larger and larger share of a core, and because each tick overruns
        // by a different amount the publishes stop being evenly spaced.
        //
        // Backing off in proportion to what the last tick actually cost bounds the
        // load a monitor may impose and keeps the spacing predictable. When
        // collection is cheap — the common case — the configured interval is
        // unchanged and this does nothing.
        let min_period = elapsed.div_f32(config.max_duty_cycle.clamp(0.05, 1.0));
        let period = interval.max(min_period);
        let mut remaining = period.saturating_sub(elapsed);
        while remaining > Duration::ZERO && !stop.load(Ordering::Relaxed) {
            let slice = remaining.min(Duration::from_millis(50));
            thread::sleep(slice);
            remaining = remaining.saturating_sub(slice);
        }
    }
}

/// The one lock every pass over the hardware readers takes, in this process.
///
/// The background collector and the HTTP sampler read the same static reader state,
/// so their passes must never overlap. Each pass holds this lock for its whole run.
/// Only [`collect_once`] and the schema resolver's pipeline take it, and neither calls
/// the other, so no pass nests inside another.
static READER_PASS: Mutex<()> = Mutex::new(());

/// Acquire the reader lock. A poisoned lock is recovered rather than propagated: a
/// panic in one pass must not stop every later pass.
pub(crate) fn reader_pass() -> MutexGuard<'static, ()> {
    READER_PASS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

/// Run one tick, fanning independent collectors out across scoped threads.
///
/// Each closure borrows a *disjoint* field of [`Sources`], which is what makes
/// concurrent `&mut` access sound here. Every scoped thread takes its own COM guard.
// Each argument is a distinct borrowed subsystem; bundling them into a struct would
// reintroduce the aliasing the disjoint-field borrows exist to avoid.
#[allow(clippy::too_many_arguments)]
fn collect_once(
    sources: &mut Sources,
    config: &CollectorConfig,
    histories: &mut Histories,
    cached_disks: &mut Vec<DiskSnapshot>,
    cached_disk_io: &mut Vec<DiskIoSnapshot>,
    cached_processes: &mut Vec<ProcessMonitorInfo>,
    cached_connections: &mut Vec<ConnectionInfo>,
    gpu_static: &[GpuStaticInfo],
    generation: u64,
) -> Snapshot {
    let _pass = reader_pass();
    let started = Instant::now();

    // Destructure so each scoped thread captures a distinct field.
    let Sources {
        gpu,
        processes,
        network,
        connections,
    } = sources;

    // The first tick always collects everything so the UI is populated immediately;
    // after that each decimated stage runs on its own cadence and the previous value
    // is reused in between.
    let due = |every: u64| generation == 1 || generation.is_multiple_of(every.max(1));
    let refresh_disks = due(config.disk_every_n_ticks);
    let refresh_processes = due(config.process_every_n_ticks);
    let refresh_connections = due(config.connection_every_n_ticks);

    #[allow(clippy::type_complexity)]
    let (cpu, memory, gpu_dynamic, process_list, net_list, conn_list, disk_list, sys_stats, temps) =
        thread::scope(|scope| {
            let cpu_h = scope.spawn(timed(collect_cpu));
            // ~2 ms per call after a one-time ~365 ms initialisation, so it sits
            // on the per-tick cadence with the CPU reading it belongs beside.
            let temp_h = scope.spawn(timed(crate::hwmon::read_cpu_temperatures));
            let mem_h = scope.spawn(timed(collect_memory));
            let sys_h = scope.spawn(timed(collect_system_stats));
            let gpu_h = scope.spawn(timed(move || collect_gpu(gpu.as_ref())));
            let proc_h = scope.spawn(timed(move || {
                refresh_processes
                    .then(|| collect_processes(processes.as_mut()))
                    .flatten()
            }));
            let net_h = scope.spawn(timed(move || collect_network(network.as_mut())));
            let conn_h = scope.spawn(timed(move || {
                refresh_connections.then(|| collect_connections(connections.as_mut()))
            }));
            let disk_h = scope.spawn(timed(move || {
                if refresh_disks {
                    Some((collect_disks(), collect_disk_io()))
                } else {
                    None
                }
            }));

            (
                join_or_default(cpu_h),
                join_or_default(mem_h),
                join_or_default(gpu_h),
                join_or_default(proc_h),
                join_or_default(net_h),
                join_or_default(conn_h),
                join_or_default(disk_h),
                join_or_default(sys_h),
                join_or_default(temp_h),
            )
        });

    let timings = StageTimings {
        cpu_us: cpu.1,
        memory_us: memory.1,
        gpu_us: gpu_dynamic.1,
        process_us: process_list.1,
        network_us: net_list.1,
        connection_us: conn_list.1,
        disk_us: disk_list.1,
        system_us: sys_stats.1,
    };

    let process_sampled = process_list.0.is_some();
    let connections_sampled = conn_list.0.is_some();

    let (cpu, memory) = (cpu.0, memory.0);
    let gpu_dynamic = gpu_dynamic.0;
    let network = net_list.0;
    let system_stats = sys_stats.0;

    if let Some((fresh_disks, fresh_io)) = disk_list.0 {
        *cached_disks = fresh_disks;
        *cached_disk_io = fresh_io;
    }
    if let Some(fresh) = process_list.0 {
        *cached_processes = fresh;
    }
    let mut connection_errors = None;
    if let Some((fresh, errors)) = conn_list.0 {
        *cached_connections = fresh;
        connection_errors = Some(errors);
    }
    let processes = cached_processes.clone();
    let connections = cached_connections.clone();

    // === histories ===
    let cap = config.history_size.max(1);
    if let Some(ref c) = cpu {
        push_capped(&mut histories.cpu, 100.0 - c.total.idle, cap);
    }
    if let Some(ref m) = memory {
        // No RAM total read means no sample: the history skips the tick rather
        // than plotting a zero.
        if let Some(pct) = m.ram_usage_percent() {
            push_capped(&mut histories.memory, pct, cap);
        }
    }
    for (i, info) in gpu_dynamic.iter().enumerate() {
        // A device that failed this tick contributes no sample; its series simply
        // does not advance, which reads as a flat segment rather than a false zero.
        let Some(info) = info.as_ref() else { continue };
        if i < histories.gpu_util.len() {
            if let Some(util) = info.utilization {
                push_capped(&mut histories.gpu_util[i], util as f32, cap);
            }
            // A device that reported no memory contributes no sample, exactly as
            // a device that failed its whole query contributes none above.
            if let Some(mem_util) = info.memory.utilization {
                push_capped(&mut histories.gpu_memory[i], mem_util as f32, cap);
            }
            if let Some(temp) = info.thermal.temperature {
                push_capped(&mut histories.gpu_temp[i], temp as f32, cap);
            }
        }
    }
    // Summed over the interfaces that have a rate. The history graph needs a
    // number per tick and the first tick after start-up has none -- pushing `0`
    // there draws a trough that never happened, so that sample is skipped and
    // the line starts one tick later.
    let rx_rates: Vec<f64> = network.iter().filter_map(|n| n.rx_rate).collect();
    let tx_rates: Vec<f64> = network.iter().filter_map(|n| n.tx_rate).collect();
    if !rx_rates.is_empty() {
        push_capped(
            &mut histories.net_rx,
            rx_rates.iter().sum::<f64>() as f32,
            cap,
        );
    }
    if !tx_rates.is_empty() {
        push_capped(
            &mut histories.net_tx,
            tx_rates.iter().sum::<f64>() as f32,
            cap,
        );
    }

    Snapshot {
        generation,
        // `collect_once` does not know whether its sources are the warm-up set;
        // the one caller that passes an empty `Sources` marks its own result.
        warmup: false,
        collected_at: SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .ok()
            .map(|d| d.as_secs()),
        collect_us: started.elapsed().as_micros() as u64,
        timings,
        process_sampled,
        connections_sampled,
        connection_errors,
        cpu,
        memory,
        gpu_static: gpu_static.to_vec(),
        gpu_dynamic,
        processes,
        connections,
        disks: cached_disks.clone(),
        disk_io: cached_disk_io.clone(),
        cpu_temperatures: temps.0,
        network,
        system_stats,
        histories: histories.clone(),
    }
}

/// Wrap a collector so it takes its own COM guard and reports its own wall-clock cost.
fn timed<T, F>(f: F) -> impl FnOnce() -> (T, u64) + Send
where
    F: FnOnce() -> T + Send,
    T: Send,
{
    move || {
        com_guard();
        let start = Instant::now();
        let value = f();
        (value, start.elapsed().as_micros() as u64)
    }
}

/// Join a scoped collector, treating a panic as "no data" rather than taking the
/// whole collector thread down with it. One flaky vendor driver must not kill
/// monitoring for everything else.
fn join_or_default<T: Default>(handle: thread::ScopedJoinHandle<'_, (T, u64)>) -> (T, u64) {
    handle.join().unwrap_or_else(|_| (T::default(), 0))
}

// ============================================================================
// Individual collectors
// ============================================================================

fn collect_cpu() -> Option<CpuStats> {
    #[cfg(target_os = "windows")]
    {
        crate::platform::windows::read_cpu_stats().ok()
    }
    #[cfg(target_os = "linux")]
    {
        crate::platform::linux::read_cpu_stats().ok()
    }
    #[cfg(target_os = "macos")]
    {
        crate::platform::macos::read_cpu_stats().ok()
    }
    #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
    {
        None
    }
}

fn collect_memory() -> Option<MemoryStats> {
    #[cfg(target_os = "windows")]
    {
        crate::platform::windows::read_memory_stats().ok()
    }
    #[cfg(target_os = "linux")]
    {
        crate::platform::linux::read_memory_stats().ok()
    }
    #[cfg(target_os = "macos")]
    {
        crate::platform::macos::read_memory_stats().ok()
    }
    #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
    {
        None
    }
}

fn collect_system_stats() -> Option<SystemStats> {
    SystemStats::new().ok()
}

fn collect_gpu(gpu: Option<&GpuCollection>) -> Vec<Option<GpuDynamicInfo>> {
    let Some(collection) = gpu else {
        return Vec::new();
    };
    // Partial results keep index alignment with the static descriptors captured at
    // startup, and let one failing device degrade alone.
    collection
        .snapshot_all_partial()
        .into_iter()
        .map(|result| result.ok().map(|info| info.dynamic_info))
        .collect()
}

/// `None` when there is no monitor or the enumeration failed. Both used to produce an
/// empty table, which was then cached and reported as a sampled process list with
/// zero processes. Callers keep the previous table on `None`.
fn collect_processes(monitor: Option<&mut ProcessMonitor>) -> Option<Vec<ProcessMonitorInfo>> {
    monitor?.processes().ok()
}

fn collect_connections(
    monitor: Option<&mut ConnectionMonitor>,
) -> (Vec<ConnectionInfo>, Vec<String>) {
    monitor
        .map(|m| m.all_connections_checked())
        .unwrap_or_else(|| (Vec::new(), vec!["Connection collector unavailable".into()]))
}

fn collect_network(monitor: Option<&mut NetworkMonitor>) -> Vec<NetSnapshot> {
    let Some(monitor) = monitor else {
        return Vec::new();
    };
    let Ok(interfaces) = monitor.interfaces() else {
        return Vec::new();
    };

    let is_virtual = |name: &str| {
        name.starts_with("lo")
            || name.contains("Loopback")
            || name.starts_with("vEthernet")
            || name.starts_with("Local Area Connection*")
            || name.starts_with("VMware")
            || name.starts_with("VirtualBox")
    };

    let mut out: Vec<NetSnapshot> = interfaces
        .iter()
        .filter(|i| !is_virtual(&i.name) && i.is_active())
        .map(|i| {
            let rate = monitor.bandwidth_rate(&i.name, i);
            NetSnapshot {
                name: i.name.clone(),
                is_up: i.is_up,
                rx_bytes: i.rx_bytes,
                tx_bytes: i.tx_bytes,
                rx_rate: rate.map(|(rx, _)| rx),
                tx_rate: rate.map(|(_, tx)| tx),
                speed_mbps: i.speed_mbps,
            }
        })
        .collect();

    // Fall back to any active non-loopback interface rather than showing nothing.
    if out.is_empty() {
        out = interfaces
            .iter()
            .filter(|i| !i.name.starts_with("lo") && !i.name.contains("Loopback") && i.is_active())
            .map(|i| {
                let rate = monitor.bandwidth_rate(&i.name, i);
                NetSnapshot {
                    name: i.name.clone(),
                    is_up: i.is_up,
                    rx_bytes: i.rx_bytes,
                    tx_bytes: i.tx_bytes,
                    rx_rate: rate.map(|(rx, _)| rx),
                    tx_rate: rate.map(|(_, tx)| tx),
                    speed_mbps: i.speed_mbps,
                }
            })
            .collect();
    }

    out
}

/// Cumulative per-device I/O counters.
///
/// One batched read rather than a call per disk. On Windows each `io_stats`
/// opens its own WMI connection, which measured 8.0 s across four drives
/// against 9.9 ms for the batch -- see `crate::disk::all_io_counters`. That cost
/// is why the Windows `collect_disks` takes the capacity-only `logical_drives`
/// path and why these counters were missing from the snapshot at all, which left
/// `ironmon_disk_read_bytes_total` unpublishable on the served endpoint.
///
/// Runs on the disk cadence (every 10 ticks by default), in the same stage.
fn collect_disk_io() -> Vec<DiskIoSnapshot> {
    let mut out: Vec<DiskIoSnapshot> = crate::disk::all_io_counters()
        .into_iter()
        .map(|(device, io)| DiskIoSnapshot {
            device,
            // Both carry the absence through; see `DiskIoSnapshot`.
            read_bytes: io.read_bytes,
            write_bytes: io.write_bytes,
        })
        .collect();
    // A map iterates in whatever order it likes; a snapshot should not shuffle
    // its rows between ticks.
    out.sort_by(|a, b| a.device.cmp(&b.device));
    out
}

#[cfg(target_os = "windows")]
fn collect_disks() -> Vec<DiskSnapshot> {
    // Windows: GetLogicalDriveStrings + GetDiskFreeSpaceExW is far cheaper and more
    // reliable than the WMI disk enumeration path.
    crate::platform::windows::logical_drives()
        .map(|drives| {
            drives
                .into_iter()
                .map(|d| DiskSnapshot {
                    name: d.name.clone(),
                    mount_point: d.name,
                    // `GetDiskFreeSpaceExW` succeeded or `logical_drives`
                    // would not have yielded this drive, so both are readings.
                    total: Some(d.total),
                    used: Some(d.used),
                    filesystem: d.filesystem,
                    // `logical_drives` reads capacity and no I/O counters, so
                    // there is no rate here to report. It said `0.0`.
                    read_rate: None,
                    write_rate: None,
                })
                .collect()
        })
        .unwrap_or_default()
}

#[cfg(not(target_os = "windows"))]
fn collect_disks() -> Vec<DiskSnapshot> {
    let Ok(disks) = crate::disk::enumerate_disks() else {
        return Vec::new();
    };

    disks
        .iter()
        .filter_map(|disk| {
            let info = disk.info().ok()?;

            let (mount_point, filesystem, used, total) = disk
                .filesystem_info()
                .ok()
                .and_then(|fs_infos| fs_infos.first().cloned())
                .map(|fs| {
                    (
                        fs.mount_point.to_string_lossy().to_string(),
                        fs.fs_type.clone(),
                        fs.used_size,
                        fs.total_size,
                    )
                })
                // No filesystem was mounted on this device, or the mount could
                // not be read. The device's own capacity still stands where
                // sysfs gave one; how much of it is in use is not known, and
                // said so rather than reported as none of it.
                .unwrap_or_else(|| ("N/A".to_string(), "N/A".to_string(), None, info.capacity));

            let (read_rate, write_rate) = disk
                .io_stats()
                .ok()
                .map(|io| {
                    (
                        io.read_throughput.map(|v| v as f64),
                        io.write_throughput.map(|v| v as f64),
                    )
                })
                // A disk whose `io_stats` call failed reports no rate, rather
                // than a rate of zero.
                .unwrap_or((None, None));

            Some(DiskSnapshot {
                // The model when the device reported one, else the device name.
                // Not the string "Unknown".
                name: info.model.unwrap_or_else(|| disk.name().to_string()),
                mount_point,
                total,
                used,
                filesystem,
                read_rate,
                write_rate,
            })
        })
        .collect()
}

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

    /// Without a monitor there is no table, so the stage must report no reading, not an
    /// empty one. An empty `Some` would be cached and flagged as sampled.
    #[test]
    fn collect_processes_without_a_monitor_reports_no_table() {
        assert!(collect_processes(None).is_none());
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn macos_gui_pipeline_reads_real_cpu_and_memory() {
        let cpu = collect_cpu().expect("macOS GUI pipeline CPU reader");
        assert!(!cpu.cores.is_empty());
        let memory = collect_memory().expect("macOS GUI pipeline memory reader");
        assert!(memory.ram.total > 0);
    }

    /// The sleep must bound collection's share of wall time, and must not shorten
    /// the configured interval when collection is cheap.
    ///
    /// This is the arithmetic from the collector loop. Collection cost rises with how
    /// busy the machine is — NVML against a loaded GPU measured 515ms versus 1.6ms
    /// for an idle card — so without a bound the collector takes its largest share of
    /// the machine exactly when the machine is already struggling.
    #[test]
    fn tick_period_bounds_collection_duty_cycle() {
        // Mirrors `collector_loop`: sleep until at least `elapsed / duty` has passed,
        // never publishing faster than the configured interval.
        let period = |elapsed_ms: u64, interval_ms: u64, duty: f32| -> Duration {
            let elapsed = Duration::from_millis(elapsed_ms);
            let interval = Duration::from_millis(interval_ms);
            interval.max(elapsed.div_f32(duty.clamp(0.05, 1.0)))
        };
        let ms = |d: Duration| d.as_millis() as u64;

        // Cheap tick: the configured interval is untouched.
        assert_eq!(ms(period(20, 1000, 0.5)), 1000);
        // Exactly at the bound: still the configured interval.
        assert_eq!(ms(period(500, 1000, 0.5)), 1000);
        // Expensive tick: the period stretches so collection stays at half the wall
        // clock rather than running back to back.
        assert_eq!(ms(period(1500, 1000, 0.5)), 3000);
        // A stricter budget stretches further.
        assert_eq!(ms(period(1000, 1000, 0.25)), 4000);

        // Whatever the inputs, the resulting duty cycle never exceeds the budget.
        // Compared in full Duration precision: the collector sleeps on Durations, so
        // rounding to whole milliseconds here would flag its own truncation.
        for elapsed_ms in [1u64, 50, 500, 1500, 5000] {
            for duty in [0.1f32, 0.25, 0.5, 1.0] {
                let p = period(elapsed_ms, 1000, duty).as_secs_f64();
                let ratio = Duration::from_millis(elapsed_ms).as_secs_f64() / p;
                assert!(
                    ratio <= duty as f64 + 1e-6,
                    "elapsed {elapsed_ms}ms over period {p}s gives duty {ratio}, over budget {duty}"
                );
            }
        }
    }

    #[test]
    fn push_capped_keeps_newest_samples() {
        let mut series = Vec::new();
        for i in 0..10 {
            push_capped(&mut series, i as f32, 4);
        }
        assert_eq!(series.len(), 4, "series must be bounded by cap");
        assert_eq!(series, vec![6.0, 7.0, 8.0, 9.0], "oldest samples evicted");
    }

    #[test]
    fn push_capped_handles_cap_of_one() {
        let mut series = Vec::new();
        push_capped(&mut series, 1.0, 1);
        push_capped(&mut series, 2.0, 1);
        assert_eq!(series, vec![2.0]);
    }

    #[test]
    fn critical_path_is_max_not_sum() {
        let t = StageTimings {
            cpu_us: 100,
            memory_us: 50,
            gpu_us: 900,
            process_us: 400,
            ..Default::default()
        };
        assert_eq!(
            t.critical_path_us(),
            900,
            "critical path is the slowest stage"
        );
        assert_eq!(t.serial_us(), 1450, "serial cost is the sum");
        assert!(
            t.critical_path_us() < t.serial_us(),
            "concurrent collection must beat serial"
        );
    }

    #[test]
    fn default_snapshot_is_generation_zero() {
        let snap = Snapshot::default();
        assert_eq!(snap.generation, 0, "readers treat gen 0 as not-ready");
        // Not `Some(0.0)`. A default snapshot has read nothing, and 0% CPU
        // is a claim that the machine is idle.
        assert_eq!(snap.cpu_utilization(), None);
        assert_eq!(snap.memory_utilization(), None);
    }

    #[test]
    fn snapshot_aggregates_network_rates() {
        let snap = Snapshot {
            network: vec![
                NetSnapshot {
                    rx_rate: Some(1.5),
                    tx_rate: Some(0.5),
                    ..Default::default()
                },
                NetSnapshot {
                    rx_rate: Some(2.5),
                    tx_rate: Some(1.5),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        assert_eq!(snap.total_rx_rate(), Some(4.0));
        assert_eq!(snap.total_tx_rate(), Some(2.0));
    }

    /// A total is a measurement of the machine only when every interface has
    /// contributed one. A partial sum, and a snapshot where none has a rate, are
    /// both absences rather than figures.
    #[test]
    fn snapshot_totals_need_every_interface_to_have_a_rate() {
        let partial = Snapshot {
            network: vec![
                NetSnapshot {
                    rx_rate: Some(3.0),
                    tx_rate: None,
                    ..Default::default()
                },
                NetSnapshot {
                    rx_rate: None,
                    tx_rate: None,
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        assert_eq!(
            partial.total_rx_rate(),
            None,
            "one interface has no rate yet"
        );
        assert_eq!(partial.total_tx_rate(), None);

        let none_yet = Snapshot {
            network: vec![NetSnapshot::default()],
            ..Default::default()
        };
        assert_eq!(none_yet.total_rx_rate(), None);

        let no_interfaces = Snapshot::default();
        assert_eq!(no_interfaces.total_rx_rate(), None);
        assert_eq!(no_interfaces.total_tx_rate(), None);
    }

    #[test]
    fn handle_is_lock_free_readable_and_stoppable() {
        let slot = Arc::new(ArcSwap::from_pointee(Snapshot::default()));
        let handle = SnapshotHandle {
            slot: Arc::clone(&slot),
            stop: Arc::new(AtomicBool::new(false)),
            interval_ms: Arc::new(AtomicU64::new(1000)),
        };

        assert_eq!(handle.generation(), 0);
        slot.store(Arc::new(Snapshot {
            generation: 7,
            ..Default::default()
        }));
        assert_eq!(handle.generation(), 7, "readers see the newest publish");
        assert_eq!(handle.latest().generation, 7);

        handle.set_interval(Duration::from_millis(250));
        assert_eq!(handle.interval(), Duration::from_millis(250));

        assert!(!handle.is_stopping());
        handle.request_stop();
        assert!(handle.is_stopping());
    }

    /// The first published snapshot must already carry useful data.
    ///
    /// Constructing the GPU collection initializes vendor drivers and can take
    /// seconds. Before the warm-up publish, nothing at all was published until that
    /// finished, so every UI sat blank. This asserts the invariant that makes the
    /// difference — first data does not wait on driver enumeration — rather than an
    /// absolute millisecond bound, which would be flaky on a loaded machine.
    #[test]
    fn first_publish_does_not_wait_for_driver_enumeration() {
        let collector = Collector::spawn(CollectorConfig {
            interval: Duration::from_millis(200),
            ..Default::default()
        });
        let handle = collector.handle();

        let start = Instant::now();
        while handle.generation() == 0 && start.elapsed() < Duration::from_secs(60) {
            thread::sleep(Duration::from_millis(1));
        }

        let first = handle.latest();
        assert!(
            first.generation > 0,
            "collector published nothing within 60s"
        );
        assert!(
            first.cpu.is_some() || first.memory.is_some(),
            "the first snapshot carried no CPU or memory sample, so the warm-up \
             publish is not doing its job — consumers would render an empty UI \
             until driver enumeration completed"
        );
    }

    #[test]
    fn snapshot_is_send_and_sync() {
        // The whole design rests on Snapshot crossing threads; assert it statically
        // so a future field addition (e.g. an Rc or a trait object) fails the build
        // here rather than at the call site.
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Snapshot>();
        assert_send_sync::<SnapshotHandle>();
    }
}