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
//! EDID 1.x block decoder.
//!
//! EDID (Extended Display Identification Data) is the 128-byte block monitors
//! return over DDC. The header at byte 0 is the fixed magic
//! `00 FF FF FF FF FF FF 00`. Subsequent fields encode manufacturer (PNP
//! code), product code, manufacture date, EDID version, established and
//! standard timings, four detailed timing descriptors, and an extension
//! count.
//!
//! Sources:
//! - Linux: `/sys/class/drm/card*-*/edid` (binary blob, 128 or 256+ bytes
//!   depending on extension blocks).
//! - Windows: SetupDi / `MonitorManufacturer`/`EDID` registry value under
//!   `HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY\*\*\Device Parameters\EDID`.

use super::{ProfileGroup, Setting, SettingRisk, SettingValue, Subsystem};

const EDID_MAGIC: [u8; 8] = [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00];

/// Scan platform-specific sources and return one [`ProfileGroup`] per EDID
/// block found.
pub fn scan_edid_groups() -> Vec<ProfileGroup> {
    let mut groups = Vec::new();

    #[cfg(target_os = "linux")]
    linux_scan(&mut groups);

    #[cfg(windows)]
    windows_scan(&mut groups);

    groups
}

#[cfg(target_os = "linux")]
fn linux_scan(groups: &mut Vec<ProfileGroup>) {
    use std::fs;
    let Ok(rd) = fs::read_dir("/sys/class/drm") else {
        return;
    };
    for entry in rd.flatten() {
        let path = entry.path();
        let edid = path.join("edid");
        if !edid.exists() {
            continue;
        }
        let Ok(bytes) = fs::read(&edid) else { continue };
        if let Some(group) = decode_block(
            &bytes,
            edid.display().to_string(),
            entry.file_name().to_string_lossy().to_string(),
        ) {
            groups.push(group);
        }
    }
}

#[cfg(windows)]
fn windows_scan(groups: &mut Vec<ProfileGroup>) {
    use winreg::enums::*;
    use winreg::RegKey;
    let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
    let Ok(display) = hklm.open_subkey(r"SYSTEM\CurrentControlSet\Enum\DISPLAY") else {
        return;
    };
    for mfg in display.enum_keys().flatten() {
        let Ok(mfg_key) = display.open_subkey(&mfg) else {
            continue;
        };
        for inst in mfg_key.enum_keys().flatten() {
            let Ok(dev) = mfg_key.open_subkey(format!("{}\\Device Parameters", inst)) else {
                continue;
            };
            let edid: Result<Vec<u8>, _> = dev.get_raw_value("EDID").map(|v| v.bytes);
            if let Ok(bytes) = edid {
                let label = format!("{}/{}", mfg, inst);
                if let Some(group) = decode_block(
                    &bytes,
                    format!(r"HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY\{}", label),
                    label,
                ) {
                    groups.push(group);
                }
            }
        }
    }
}

pub fn decode_block(bytes: &[u8], source: String, locator: String) -> Option<ProfileGroup> {
    if bytes.len() < 128 {
        return None;
    }
    if bytes[..8] != EDID_MAGIC {
        return None;
    }

    let mfg = decode_pnp(&bytes[8..10]);
    let product_code = u16::from_le_bytes([bytes[10], bytes[11]]);
    let serial_id = u32::from_le_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]);
    let week = bytes[16];
    let year = bytes[17] as u16 + 1990;
    let edid_major = bytes[18];
    let edid_minor = bytes[19];
    let video_input = bytes[20];
    let digital = (video_input & 0x80) != 0;
    let max_h_cm = bytes[21];
    let max_v_cm = bytes[22];
    let gamma_raw = bytes[23];
    let gamma = (gamma_raw as f64 + 100.0) / 100.0;
    let features = bytes[24];
    let dpms_standby = (features & 0x80) != 0;
    let dpms_suspend = (features & 0x40) != 0;
    let dpms_active_off = (features & 0x20) != 0;
    let display_type = (features >> 3) & 0x3;
    let display_type_str = if digital {
        match display_type {
            0 => "RGB 4:4:4",
            1 => "RGB 4:4:4 + YCrCb 4:4:4",
            2 => "RGB 4:4:4 + YCrCb 4:2:2",
            _ => "RGB 4:4:4 + YCrCb 4:4:4 + YCrCb 4:2:2",
        }
    } else {
        match display_type {
            0 => "Monochrome",
            1 => "RGB color",
            2 => "Non-RGB color",
            _ => "Undefined",
        }
    };
    let monitor_name = read_monitor_name_descriptor(bytes);
    let preferred = read_preferred_timing(bytes);

    let label = monitor_name
        .clone()
        .unwrap_or_else(|| format!("{} {:04X}", mfg, product_code));
    let mut group = ProfileGroup::new(
        Subsystem::Display,
        format!("EDID: {} — {}", locator, label),
        format!("EDID {}.{}", edid_major, edid_minor),
        source,
    );

    group.push(Setting::info(
        "edid.manufacturer",
        "Manufacturer (PNP)",
        SettingValue::Text(mfg),
    ));
    group.push(Setting::info(
        "edid.product_code",
        "Product Code",
        SettingValue::Uint(product_code as u64),
    ));
    if serial_id != 0 {
        group.push(Setting::info(
            "edid.serial_id",
            "Serial ID",
            SettingValue::Uint(serial_id as u64),
        ));
    }
    if let Some(name) = monitor_name {
        group.push(Setting::info(
            "edid.monitor_name",
            "Monitor Name",
            SettingValue::Text(name),
        ));
    }
    if week > 0 || year > 1990 {
        group.push(Setting::info(
            "edid.manufactured",
            "Manufactured",
            SettingValue::Text(format!("week {} of {}", week, year)),
        ));
    }
    group.push(Setting::info(
        "edid.signal",
        "Video Signal",
        SettingValue::Text(if digital {
            "Digital".into()
        } else {
            "Analog".into()
        }),
    ));
    group.push(Setting::info(
        "edid.display_type",
        "Display Type",
        SettingValue::Text(display_type_str.into()),
    ));
    if max_h_cm > 0 && max_v_cm > 0 {
        group.push(Setting::info(
            "edid.max_image_size",
            "Max Image Size",
            SettingValue::Text(format!("{} × {} cm", max_h_cm, max_v_cm)),
        ));
    }
    if gamma_raw != 0xFF {
        group.push(
            Setting::info("edid.gamma", "Gamma", SettingValue::Float(gamma))
                .with_description("Display transfer characteristic. 2.2 is sRGB."),
        );
    }
    group.push(Setting::info(
        "edid.dpms_standby",
        "DPMS Standby Supported",
        SettingValue::Bool(dpms_standby),
    ));
    group.push(Setting::info(
        "edid.dpms_suspend",
        "DPMS Suspend Supported",
        SettingValue::Bool(dpms_suspend),
    ));
    group.push(Setting::info(
        "edid.dpms_active_off",
        "DPMS Active-Off Supported",
        SettingValue::Bool(dpms_active_off),
    ));
    if let Some((h, v, refresh_hz, interlaced)) = preferred {
        group.push(
            Setting::info(
                "edid.preferred_timing",
                "Preferred Native Timing",
                SettingValue::Text(format!(
                    "{}×{}{} @ {:.2} Hz",
                    h,
                    v,
                    if interlaced { "i" } else { "" },
                    refresh_hz
                )),
            )
            .with_description(concat!(
                "First Detailed Timing Descriptor — manufacturer's ",
                "recommended mode. An `i` marks an interlaced mode, whose ",
                "line count the descriptor stores per field and which is ",
                "doubled here to whole frames."
            ))
            .with_risk(SettingRisk::Safe),
        );
    }
    let ext_count = bytes[126];
    group.push(Setting::info(
        "edid.extension_blocks",
        "Extension Blocks",
        SettingValue::Uint(ext_count as u64),
    ));
    Some(group)
}

/// Decode the 3-letter PNP manufacturer code from two big-endian bytes.
/// Each letter is a 5-bit value with 'A' = 1.
fn decode_pnp(b: &[u8]) -> String {
    if b.len() < 2 {
        return "???".into();
    }
    let word = ((b[0] as u16) << 8) | b[1] as u16;
    let a = ((word >> 10) & 0x1F) as u8;
    let b2 = ((word >> 5) & 0x1F) as u8;
    let c = (word & 0x1F) as u8;
    fn ch(v: u8) -> char {
        if v == 0 {
            '?'
        } else {
            (b'A' + v - 1) as char
        }
    }
    format!("{}{}{}", ch(a), ch(b2), ch(c))
}

/// Walk the four 18-byte detailed-timing/monitor-descriptor blocks at
/// offsets 54..72..90..108 and return the monitor name (descriptor type
/// `0xFC`) if present.
fn read_monitor_name_descriptor(bytes: &[u8]) -> Option<String> {
    for off in [54usize, 72, 90, 108] {
        if off + 18 > bytes.len() {
            continue;
        }
        let block = &bytes[off..off + 18];
        // A descriptor (not a detailed timing) has first two bytes = 00 00.
        if block[0] == 0 && block[1] == 0 && block[3] == 0xFC {
            let raw = &block[5..18];
            let mut s = String::new();
            for &b in raw {
                if b == 0x0A {
                    break;
                }
                if (0x20..0x7F).contains(&b) {
                    s.push(b as char);
                }
            }
            let trimmed = s.trim().to_string();
            if !trimmed.is_empty() {
                return Some(trimmed);
            }
        }
    }
    None
}

/// Decode the first Detailed Timing Descriptor at offset 54 (the
/// manufacturer-preferred / native mode). Returns (h_active, v_active, refresh_hz).
/// The first Detailed Timing Descriptor: active pixels, refresh, and whether the
/// mode is interlaced.
///
/// The interlace flag is not decoration. For an interlaced timing the spec stores
/// **vertical active as lines per field**, so a 1080i mode carries 540 in the
/// DTD. Ignoring the flag reported an AV receiver on this machine as
/// `1920×540 @ 60.05 Hz` — a resolution no equipment has — where the real mode
/// is 1920×1080 interlaced at a 60 Hz field rate. The frame lines are what a
/// reader means by a resolution, so they are what is returned, with the flag
/// alongside so the caller can say which it is.
fn read_preferred_timing(bytes: &[u8]) -> Option<(u32, u32, f64, bool)> {
    if bytes.len() < 72 {
        return None;
    }
    let dtd = &bytes[54..72];
    // Bytes 0-1: pixel clock in 10 kHz units (little-endian). 0 => monitor descriptor.
    let pixel_clock_khz = (u16::from_le_bytes([dtd[0], dtd[1]]) as u32) * 10;
    if pixel_clock_khz == 0 {
        return None;
    }
    let h_active_lo = dtd[2] as u32;
    let h_blank_lo = dtd[3] as u32;
    let h_active_hi = ((dtd[4] >> 4) & 0xF) as u32;
    let h_blank_hi = (dtd[4] & 0xF) as u32;
    let h_active = (h_active_hi << 8) | h_active_lo;
    let h_blank = (h_blank_hi << 8) | h_blank_lo;

    let v_active_lo = dtd[5] as u32;
    let v_blank_lo = dtd[6] as u32;
    let v_active_hi = ((dtd[7] >> 4) & 0xF) as u32;
    let v_blank_hi = (dtd[7] & 0xF) as u32;
    let v_active = (v_active_hi << 8) | v_active_lo;
    let v_blank = (v_blank_hi << 8) | v_blank_lo;

    let h_total = h_active + h_blank;
    let v_total = v_active + v_blank;
    if h_total == 0 || v_total == 0 {
        return None;
    }
    let refresh = (pixel_clock_khz as f64 * 1000.0) / (h_total as f64 * v_total as f64);

    // Byte 17 bit 7. The refresh computed above is the field rate for an
    // interlaced mode, which is the number equipment is labelled with — 1080i60
    // means sixty fields — so it is left as it is and only the line count is
    // doubled back to whole frames.
    let interlaced = (dtd[17] & 0x80) != 0;
    let v_frame = if interlaced { v_active * 2 } else { v_active };

    Some((h_active, v_frame, refresh, interlaced))
}

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

    fn synth_edid(name: &str, h: u32, v: u32, pclk_khz: u32) -> Vec<u8> {
        let mut b = vec![0u8; 128];
        b[..8].copy_from_slice(&EDID_MAGIC);
        // Manufacturer = "ABC": A=1,B=2,C=3 → bits 1<<10 | 2<<5 | 3
        let mfg_word: u16 = (1 << 10) | (2 << 5) | 3;
        b[8] = (mfg_word >> 8) as u8;
        b[9] = (mfg_word & 0xFF) as u8;
        b[10] = 0x42;
        b[11] = 0x00;
        b[16] = 10; // week
        b[17] = 34; // year offset → 2024
        b[18] = 1; // major
        b[19] = 4; // minor
        b[20] = 0x80; // digital input
        b[21] = 60; // 60 cm wide
        b[22] = 34; // 34 cm tall
        b[23] = (2.2 * 100.0 - 100.0) as u8; // gamma 2.2
        b[24] = 0xE0; // all DPMS bits

        // DTD at offset 54
        let pclk_units = (pclk_khz / 10) as u16;
        b[54] = (pclk_units & 0xFF) as u8;
        b[55] = (pclk_units >> 8) as u8;
        b[56] = (h & 0xFF) as u8;
        b[57] = 0x00; // h blank lo (= 0 for simplicity)
        b[58] = (((h >> 8) & 0xF) << 4) as u8;
        b[59] = (v & 0xFF) as u8;
        b[60] = 0x00;
        b[61] = (((v >> 8) & 0xF) << 4) as u8;

        // Monitor name descriptor at offset 72
        b[72] = 0x00;
        b[73] = 0x00;
        b[74] = 0x00;
        b[75] = 0xFC;
        b[76] = 0x00;
        let n = name.as_bytes();
        for (i, &c) in n.iter().take(13).enumerate() {
            b[77 + i] = c;
        }
        if n.len() < 13 {
            b[77 + n.len()] = 0x0A;
        }
        b
    }

    /// An interlaced mode reports whole frames, not fields.
    ///
    /// The bytes below are the real first DTD from a Yamaha RX-A740 read out of
    /// this machine's registry, not a synthesised one — a fixture built by the
    /// same hand as the parser is what let four NVMe SMART fields ship sixteen
    /// bytes out of place. Decoded against the EDID specification it is
    /// 1920 active pixels, 540 lines *per field*, pixel clock 74.25 MHz, flags
    /// byte 0x9E with bit 7 set for interlace: 1080i60.
    ///
    /// Before the flag was read this reported `1920×540`, a resolution no
    /// equipment has.
    #[test]
    fn an_interlaced_mode_reports_frame_lines_not_field_lines() {
        const RX_A740_DTD: [u8; 18] = [
            0x01, 0x1D, 0x80, 0x18, 0x71, 0x1C, 0x16, 0x20, 0x58, 0x2C, 0x25, 0x00, 0xC4, 0x8E,
            0x21, 0x00, 0x00, 0x9E,
        ];

        let mut b = synth_edid("RX-A740", 1920, 1080, 74_250);
        b[54..72].copy_from_slice(&RX_A740_DTD);

        let (h, v, refresh, interlaced) = read_preferred_timing(&b).expect("a timing");
        assert_eq!(h, 1920);
        assert_eq!(
            v, 1080,
            "540 field lines must be reported as 1080 frame lines"
        );
        assert!(interlaced, "flags byte 0x9E has bit 7 set");
        // 74.25 MHz over 2200 × 562 is the field rate, which is what 1080i60 names.
        assert!(
            (refresh - 60.05).abs() < 0.05,
            "field rate was {refresh}, expected ~60.05"
        );

        let g = decode_block(&b, "test".into(), "hdmi".into()).expect("decodes");
        let shown = g
            .settings
            .iter()
            .find(|s| s.id == "edid.preferred_timing")
            .map(|s| s.value.to_string())
            .expect("preferred timing is published");
        assert!(
            shown.contains("1920×1080i"),
            "an interlaced mode must be marked as one, got {shown:?}"
        );
    }

    /// A progressive mode is left alone by the same code path.
    #[test]
    fn a_progressive_mode_is_not_doubled() {
        let b = synth_edid("Test Display", 2560, 1440, 241_500);
        let (h, v, _, interlaced) = read_preferred_timing(&b).expect("a timing");
        assert_eq!((h, v), (2560, 1440));
        assert!(!interlaced);
    }

    #[test]
    fn detects_magic_and_pnp() {
        let b = synth_edid("Test Display", 2560, 1440, 241_500);
        let g = decode_block(&b, "test".into(), "card0".into()).unwrap();
        let mfg = g
            .settings
            .iter()
            .find(|s| s.id == "edid.manufacturer")
            .unwrap();
        assert!(matches!(&mfg.value, SettingValue::Text(t) if t == "ABC"));
    }

    #[test]
    fn extracts_monitor_name() {
        let b = synth_edid("DELL U2723QE", 3840, 2160, 533_250);
        let g = decode_block(&b, "test".into(), "card0".into()).unwrap();
        let name = g
            .settings
            .iter()
            .find(|s| s.id == "edid.monitor_name")
            .unwrap();
        assert!(matches!(&name.value, SettingValue::Text(t) if t.contains("DELL")));
    }

    #[test]
    fn decodes_preferred_timing() {
        // 2560x1440 with pclk such that refresh ≈ 60 Hz when h_total≈h_active, v_total≈v_active
        let b = synth_edid("Mon", 2560, 1440, 220_000);
        let g = decode_block(&b, "test".into(), "card0".into()).unwrap();
        let t = g
            .settings
            .iter()
            .find(|s| s.id == "edid.preferred_timing")
            .unwrap();
        if let SettingValue::Text(s) = &t.value {
            assert!(s.contains("2560×1440"));
        } else {
            panic!("expected text");
        }
    }

    #[test]
    fn rejects_bad_magic() {
        let b = vec![0u8; 128];
        assert!(decode_block(&b, "test".into(), "card0".into()).is_none());
    }
}