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
//! Projecting a [`Snapshot`] into table rows.
//!
//! This is where the crate's `Option` discipline either survives into the store
//! or is quietly lost, so it is written to make losing it hard and tested to
//! make losing it loud.
//!
//! Three distinctions have to come through intact, and all three are invisible
//! if a reading arrives as a number:
//!
//! - **No accelerator** versus **an accelerator that did not answer.** A
//!   machine with no GPU produces one row with the GPU columns null. A machine
//!   whose card failed to read produces a row that *names the card* and leaves
//!   its metrics null — [`Snapshot::gpu_dynamic`] is index-aligned with
//!   `gpu_static` and keeps a failed device's slot, precisely so this stays
//!   expressible. The two look identical the moment either becomes a zero.
//! - **Idle** versus **unread.** `GpuDynamicInfo::utilization` is already
//!   `Option<u8>` because two backends used to return a literal `0` having read
//!   nothing; that history is in [`crate::gpu`]. Nothing here re-introduces it.
//! - **No interfaces up** versus **no rate established yet.**
//!   [`Snapshot::total_rx_rate`] returns `None` rather than `0.0` when no
//!   interface has a rate, on the stated grounds that "a total over nothing
//!   measured is not a measurement of nothing".
//!
//! # Units
//!
//! The table stores bytes; [`crate::core::memory::RamInfo`] stores kibibytes.
//! The conversion happens here, once, and is the only place it happens.

use crate::pipeline::Snapshot;

use super::HostId;

/// One row of the `host_metrics` table.
///
/// A plain struct rather than an Arrow builder, so the projection can be tested
/// without a columnar library in the way. Conversion to Arrow is a separate,
/// mechanical step.
#[derive(Debug, Clone, PartialEq)]
pub struct MetricRow {
    pub host_id: String,
    /// Unix microseconds, which is Iceberg's `timestamptz` resolution. The
    /// snapshot carries seconds, so this is a widening rather than a rounding.
    pub collected_at_us: i64,
    pub generation: i64,

    pub cpu_utilization: Option<f64>,
    pub cpu_cores: Option<i32>,

    pub memory_used_bytes: Option<i64>,
    pub memory_total_bytes: Option<i64>,
    pub swap_used_bytes: Option<i64>,
    pub swap_total_bytes: Option<i64>,

    pub gpu_index: Option<i32>,
    pub gpu_name: Option<String>,
    pub gpu_utilization: Option<f64>,
    pub gpu_memory_used_bytes: Option<i64>,
    pub gpu_memory_total_bytes: Option<i64>,
    pub gpu_temperature_c: Option<f64>,
    pub gpu_power_mw: Option<i64>,

    pub net_rx_bytes_per_sec: Option<f64>,
    pub net_tx_bytes_per_sec: Option<f64>,

    pub collect_us: Option<i64>,
}

/// Kibibytes to bytes. Zero is a value and stays `Some(0)`.
fn kib_to_bytes(kib: u64) -> Option<i64> {
    i64::try_from(kib.saturating_mul(1024)).ok()
}

/// RAM total in kibibytes to bytes, refusing to invent a total.
///
/// `RamInfo::total` is a bare `u64` rather than `Option`, so a failed read is not
/// expressible at the field level, only at the `Option<MemoryStats>` above it. A
/// total of zero is not a plausible reading from a machine that is running this
/// program, so it is treated as the absence of one rather than passed through as a
/// measurement. That is a judgement, and it is made here rather than left to every
/// consumer of the table. It applies to the total only: a zero swap figure is a
/// real reading (nothing paged out), and used bytes pass through as read.
fn ram_total_to_bytes(kib: u64) -> Option<i64> {
    if kib == 0 {
        return None;
    }
    kib_to_bytes(kib)
}

/// Project one snapshot into the rows it contributes.
///
/// **Always at least one row**, even on a machine with no accelerators: the
/// host-level metrics are the reason the row exists, and a fleet whose GPU-less
/// members simply vanish from the table cannot answer questions about the fleet.
///
/// On a machine with accelerators there is one row per card, carrying the
/// host-level columns repeated. See [`super::schema`] for why the layout is flat
/// and what that repetition costs a careless `SUM`.
///
/// Empty when the snapshot has no collection time (`collected_at` is `None`):
/// a row needs a time, and one stamped at the epoch would be a fabricated
/// reading. Nothing is stored for such a snapshot.
pub fn rows_from_snapshot(snapshot: &Snapshot, host: &HostId) -> Vec<MetricRow> {
    let Some(collected_at) = snapshot.collected_at else {
        return Vec::new();
    };
    let base = MetricRow {
        host_id: host.as_str().to_owned(),
        collected_at_us: (collected_at as i64).saturating_mul(1_000_000),
        generation: snapshot.generation as i64,

        cpu_utilization: snapshot.cpu_utilization().map(f64::from),
        cpu_cores: snapshot
            .cpu
            .as_ref()
            .and_then(|c| i32::try_from(c.cores.len()).ok()),

        memory_used_bytes: snapshot
            .memory
            .as_ref()
            .and_then(|m| kib_to_bytes(m.ram.used)),
        memory_total_bytes: snapshot
            .memory
            .as_ref()
            .and_then(|m| ram_total_to_bytes(m.ram.total)),
        swap_used_bytes: snapshot
            .memory
            .as_ref()
            .and_then(|m| m.swap.used)
            .and_then(kib_to_bytes),
        swap_total_bytes: snapshot
            .memory
            .as_ref()
            .and_then(|m| m.swap.total)
            .and_then(kib_to_bytes),

        gpu_index: None,
        gpu_name: None,
        gpu_utilization: None,
        gpu_memory_used_bytes: None,
        gpu_memory_total_bytes: None,
        gpu_temperature_c: None,
        gpu_power_mw: None,

        net_rx_bytes_per_sec: snapshot.total_rx_rate(),
        net_tx_bytes_per_sec: snapshot.total_tx_rate(),

        collect_us: i64::try_from(snapshot.collect_us).ok(),
    };

    if snapshot.gpu_static.is_empty() {
        return vec![base];
    }

    snapshot
        .gpu_static
        .iter()
        .enumerate()
        .map(|(slot, static_info)| {
            let mut row = base.clone();
            // The card is present, so it is named even when nothing about its
            // state could be read. `name` describes the hardware; `uuid` would
            // identify the unit and is deliberately not carried.
            row.gpu_index = i32::try_from(static_info.index).ok();
            row.gpu_name = Some(static_info.name.clone());

            // Index-aligned by contract, but a shorter vector is treated as
            // "not read" rather than panicking: a partial snapshot is a thing
            // that can reach here, and losing the whole tick over it would be
            // the wrong trade.
            if let Some(Some(dynamic)) = snapshot.gpu_dynamic.get(slot) {
                row.gpu_utilization = dynamic.utilization.map(f64::from);
                row.gpu_memory_used_bytes = dynamic.memory.used.and_then(|b| i64::try_from(b).ok());
                row.gpu_memory_total_bytes =
                    dynamic.memory.total.and_then(|b| i64::try_from(b).ok());
                row.gpu_temperature_c = dynamic.thermal.temperature.map(f64::from);
                row.gpu_power_mw = dynamic.power.draw.map(i64::from);
            }
            row
        })
        .collect()
}

/// GPU fixtures shared with [`super::store`]'s round-trip tests.
///
/// Shared rather than duplicated because the round trip asserts on the same
/// distinction this module's tests do — an idle card against an unread one —
/// and two copies of a fixture drift into testing two different things.
#[cfg(test)]
pub(crate) mod fixtures {
    use crate::gpu::{
        GpuClocks, GpuDynamicInfo, GpuEngines, GpuMemory, GpuPower, GpuStaticInfo, GpuThermal,
        GpuVendor, PcieLinkInfo,
    };

    pub fn card(index: usize, name: &str) -> GpuStaticInfo {
        GpuStaticInfo {
            index,
            vendor: GpuVendor::Nvidia,
            name: name.to_owned(),
            pci_bus_id: None,
            uuid: Some("GPU-deadbeef-0000-0000-0000-000000000000".to_owned()),
            vbios_version: None,
            driver_version: None,
            compute_capability: None,
            shader_cores: None,
            num_engines: None,
            integrated: Some(false),
            l2_cache: None,
        }
    }

    pub fn reading(utilization: Option<u8>, temperature: Option<i32>) -> GpuDynamicInfo {
        GpuDynamicInfo {
            utilization,
            memory: GpuMemory {
                total: Some(8 * 1024 * 1024 * 1024),
                used: Some(1024 * 1024 * 1024),
                free: None,
                utilization: None,
            },
            clocks: GpuClocks {
                graphics: None,
                graphics_max: None,
                memory: None,
                memory_max: None,
                sm: None,
                video: None,
            },
            power: GpuPower {
                draw: Some(210_000),
                limit: None,
                default_limit: None,
                usage_percent: None,
            },
            thermal: GpuThermal {
                temperature,
                max_temperature: None,
                critical_temperature: None,
                fan_speed: None,
                fan_rpm: None,
            },
            pcie: PcieLinkInfo {
                current_gen: None,
                max_gen: None,
                current_width: None,
                max_width: None,
                current_speed: None,
                max_speed: None,
                tx_throughput: None,
                rx_throughput: None,
            },
            engines: GpuEngines {
                graphics: None,
                compute: None,
                encoder: None,
                decoder: None,
                copy: None,
                vendor_specific: Vec::new(),
            },
            processes: Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::fixtures::{card, reading};
    use super::*;

    fn host() -> HostId {
        HostId::rotate(Some("test-host"))
    }

    /// A machine with no accelerators must still appear in the table.
    #[test]
    fn a_gpu_less_host_still_produces_a_row() {
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            generation: 7,
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(rows.len(), 1, "a fleet member with no GPU must not vanish");
        assert_eq!(rows[0].generation, 7);
        assert!(rows[0].gpu_index.is_none());
        assert!(rows[0].gpu_utilization.is_none());
    }

    /// One row per card, so a per-card question has a per-card row to answer it.
    #[test]
    fn each_accelerator_gets_its_own_row() {
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            generation: 1,
            gpu_static: vec![
                card(0, "NVIDIA A"),
                card(1, "NVIDIA B"),
                card(2, "NVIDIA C"),
            ],
            gpu_dynamic: vec![
                Some(reading(Some(23), Some(49))),
                Some(reading(Some(0), Some(45))),
                Some(reading(None, None)),
            ],
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(rows.len(), 3);
        assert_eq!(rows[0].gpu_name.as_deref(), Some("NVIDIA A"));
        assert_eq!(rows[0].gpu_utilization, Some(23.0));
        assert_eq!(rows[0].gpu_temperature_c, Some(49.0));
    }

    /// **The distinction this module exists for.** An idle card reports zero; an
    /// unread card reports nothing. They must not arrive as the same value.
    #[test]
    fn an_idle_card_and_an_unread_card_are_different_rows() {
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            gpu_static: vec![card(0, "idle"), card(1, "unread")],
            gpu_dynamic: vec![Some(reading(Some(0), Some(40))), Some(reading(None, None))],
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(
            rows[0].gpu_utilization,
            Some(0.0),
            "a card that reported 0% is a measurement and must be stored as one"
        );
        assert_eq!(
            rows[1].gpu_utilization, None,
            "a card that reported nothing must be null, or it averages like an idle one"
        );
    }

    /// A card that failed to answer keeps its slot and its name.
    #[test]
    fn a_card_that_did_not_answer_is_still_named() {
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            gpu_static: vec![card(0, "NVIDIA A"), card(1, "NVIDIA B")],
            // The second device failed entirely; the slot is kept by contract.
            gpu_dynamic: vec![Some(reading(Some(50), Some(60))), None],
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(
            rows.len(),
            2,
            "a failed device must not drop out of the tick"
        );
        assert_eq!(rows[1].gpu_name.as_deref(), Some("NVIDIA B"));
        assert_eq!(rows[1].gpu_index, Some(1));
        assert_eq!(
            rows[1].gpu_utilization, None,
            "present but unread is null, not zero"
        );
        assert_eq!(rows[1].gpu_memory_used_bytes, None);
    }

    /// The GPU's UUID identifies a unit and must never reach the table.
    #[test]
    fn no_hardware_identifier_reaches_a_row() {
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            gpu_static: vec![card(0, "NVIDIA A")],
            gpu_dynamic: vec![Some(reading(Some(10), Some(40)))],
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        let rendered = format!("{:?}", rows[0]);
        assert!(
            !rendered.contains("deadbeef"),
            "the card's UUID identifies one unit and is not published; row was {rendered}"
        );
    }

    /// A truncated snapshot must not panic and must not invent readings.
    #[test]
    fn a_shorter_dynamic_vector_reads_as_unmeasured() {
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            gpu_static: vec![card(0, "a"), card(1, "b")],
            // Deliberately violating the index-alignment contract.
            gpu_dynamic: vec![Some(reading(Some(10), Some(40)))],
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[1].gpu_utilization, None);
    }

    /// Every row carries the host, or the table cannot be grouped.
    #[test]
    fn every_row_carries_the_host_id() {
        let id = host();
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            gpu_static: vec![card(0, "a"), card(1, "b")],
            gpu_dynamic: vec![None, None],
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &id);
        assert!(rows.iter().all(|r| r.host_id == id.as_str()));
    }

    /// Kibibytes become bytes exactly once.
    #[test]
    fn memory_is_converted_from_kib_to_bytes() {
        use crate::core::memory::{MemoryStats, RamInfo, SwapInfo};

        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            memory: Some(MemoryStats {
                ram: RamInfo {
                    total: 1024,
                    used: 512,
                    free: Some(512),
                    buffers: None,
                    cached: None,
                    shared: None,
                    lfb: None,
                },
                swap: SwapInfo {
                    total: Some(2048),
                    used: Some(0),
                    cached: None,
                },
                emc: None,
                iram: None,
            }),
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(rows[0].memory_total_bytes, Some(1024 * 1024));
        assert_eq!(rows[0].memory_used_bytes, Some(512 * 1024));
        assert_eq!(rows[0].swap_total_bytes, Some(2048 * 1024));
        // Zero swap in use is a reading (nothing paged out), not an absence.
        assert_eq!(rows[0].swap_used_bytes, Some(0));
    }

    /// Only the RAM total refuses zero. Swap and used memory report what was read.
    #[test]
    fn zero_is_a_reading_except_for_ram_total() {
        use crate::core::memory::{MemoryStats, RamInfo, SwapInfo};

        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            memory: Some(MemoryStats {
                ram: RamInfo {
                    total: 0,
                    used: 0,
                    free: None,
                    buffers: None,
                    cached: None,
                    shared: None,
                    lfb: None,
                },
                swap: SwapInfo {
                    total: Some(0),
                    used: Some(0),
                    cached: None,
                },
                emc: None,
                iram: None,
            }),
            ..Default::default()
        };

        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(rows[0].memory_total_bytes, None);
        assert_eq!(rows[0].memory_used_bytes, Some(0));
        assert_eq!(rows[0].swap_total_bytes, Some(0));
        assert_eq!(rows[0].swap_used_bytes, Some(0));
    }

    /// Memory that was never read must not become zero bytes of RAM.
    #[test]
    fn unread_memory_stays_null() {
        let snapshot = Snapshot {
            collected_at: Some(1_700_000_000),
            memory: None,
            ..Default::default()
        };
        let rows = rows_from_snapshot(&snapshot, &host());
        assert_eq!(rows[0].memory_total_bytes, None);
        assert_eq!(rows[0].memory_used_bytes, None);
    }
}