ironmonitor 7.0.2

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! GPU profile provider.
//!
//! Inspired by NVIDIA Profile Inspector, AMD Radeon Adrenalin's "Graphics" tab,
//! and Intel Graphics Command Center. The full NVPI experience requires
//! NVAPI-level access to the binary DRS profile database
//! (`%ProgramData%\NVIDIA Corporation\Drs\nvdrsdb*.bin`). That's a substantial
//! unsafe-FFI undertaking and is intentionally out of scope for the initial
//! read-only landing.
//!
//! What we *do* read in this pass:
//!
//! - **NVIDIA**: live NVML driver/version info, persistence mode, ECC mode,
//!   power management limit, default/min/max graphics clocks, power state caps,
//!   and the driver's published kernel-module parameters.
//! - **AMD** (Linux): `/sys/class/drm/cardN/device/` knobs (`power_dpm_state`,
//!   `power_dpm_force_performance_level`, `pp_od_clk_voltage`, fan policy).
//! - **Intel** (Linux): `/sys/class/drm/cardN/gt_*` GT-frequency policy values.
//! - **Windows**: enumerate the NVIDIA / AMD / Intel display-class device
//!   registry keys (`HKLM\SYSTEM\CurrentControlSet\Control\Class\{4d36e968-...}\NNNN`)
//!   so that callers can see the driver overrides already present on the system.

// `Setting`, `SettingValue` and `SettingRisk` are used only by the reader bodies
// below, every one of which sits behind a vendor feature. A build with none of
// them keeps the trait plumbing and drops the setting constructors, so the import
// is genuinely unused there. Annotated rather than split with a `cfg`, because
// the condition is the disjunction of every vendor gate in this file and getting
// it wrong breaks a platform this machine cannot compile for.
#[allow(unused_imports)]
use super::{ProfileGroup, ProfileProvider, Setting, SettingRisk, SettingValue, Subsystem};

pub struct GpuProfileProvider {
    _private: (),
}

/// NVML's performance state in the notation the domain uses.
///
/// Gated with its caller. This was written at module scope without the gate and
/// broke the build for every feature set without `nvidia`, because that is what
/// links `nvml_wrapper`. `--all-features` cannot catch that, and neither can the
/// two cross-target checks, which both name `nvidia` in their feature lists.
///
/// `nvml_wrapper` names the variants `Zero`..`Fifteen`, so `{:?}` printed
/// "Performance State = Eight" directly above a description reading "P0 =
/// highest performance, P15 = lowest". The value and its own documentation were
/// in different notations, and only one of them is what the driver, the vendor
/// tooling and every overclocking guide call it.
#[cfg(feature = "nvidia")]
fn pstate_name(p: nvml_wrapper::enum_wrappers::device::PerformanceState) -> Option<String> {
    use nvml_wrapper::enum_wrappers::device::PerformanceState as P;
    match p {
        P::Zero => Some("P0".into()),
        P::One => Some("P1".into()),
        P::Two => Some("P2".into()),
        P::Three => Some("P3".into()),
        P::Four => Some("P4".into()),
        P::Five => Some("P5".into()),
        P::Six => Some("P6".into()),
        P::Seven => Some("P7".into()),
        P::Eight => Some("P8".into()),
        P::Nine => Some("P9".into()),
        P::Ten => Some("P10".into()),
        P::Eleven => Some("P11".into()),
        P::Twelve => Some("P12".into()),
        P::Thirteen => Some("P13".into()),
        P::Fourteen => Some("P14".into()),
        P::Fifteen => Some("P15".into()),
        // NVML says it does not know. Publishing the string "unknown" as the
        // value would be the exact thing the absence-word guard exists to stop:
        // a caller cannot tell it from a state named that. The setting is
        // dropped instead, which is what every other reader here does when it
        // has nothing.
        P::Unknown => None,
    }
}

impl GpuProfileProvider {
    pub fn new() -> Self {
        Self { _private: () }
    }
}

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

impl ProfileProvider for GpuProfileProvider {
    fn subsystem(&self) -> Subsystem {
        Subsystem::Gpu
    }

    fn snapshot(&mut self) -> Vec<ProfileGroup> {
        let mut groups = Vec::new();

        #[cfg(feature = "nvidia")]
        groups.extend(nvidia_groups());

        #[cfg(all(target_os = "linux", feature = "amd"))]
        groups.extend(amd_linux_groups());

        #[cfg(all(target_os = "linux", feature = "intel"))]
        groups.extend(intel_linux_groups());

        #[cfg(windows)]
        groups.extend(windows_display_class_groups());

        #[cfg(windows)]
        groups.extend(super::nvidia_drs::scan_drs_groups());

        if groups.is_empty() {
            let mut g = ProfileGroup::new(
                Subsystem::Gpu,
                "(no GPU driver readable)",
                "Default",
                "auto-detect",
            );
            g.note("No supported GPU driver surfaces were readable on this platform.");
            g.note("Compile with --features nvidia,amd,intel and run with adequate permissions for full data.");
            groups.push(g);
        }
        groups
    }
}

#[cfg(feature = "nvidia")]
fn nvidia_groups() -> Vec<ProfileGroup> {
    use nvml_wrapper::enum_wrappers::device::{Clock, ClockId};
    use nvml_wrapper::Nvml;

    let Ok(nvml) = Nvml::init() else {
        return Vec::new();
    };
    let count = nvml.device_count().unwrap_or(0);
    let mut groups = Vec::new();

    let driver_version = nvml.sys_driver_version().unwrap_or_else(|_| "?".into());
    let nvml_version = nvml.sys_nvml_version().unwrap_or_else(|_| "?".into());

    for i in 0..count {
        let Ok(dev) = nvml.device_by_index(i) else {
            continue;
        };
        let name = dev.name().unwrap_or_else(|_| format!("NVIDIA GPU {}", i));
        let mut g = ProfileGroup::new(
            Subsystem::Gpu,
            &name,
            "Global driver profile",
            format!("NVML index {}", i),
        );

        g.push(
            Setting::info(
                "driver_version",
                "Driver Version",
                SettingValue::Text(driver_version.clone()),
            )
            .with_source("NVML"),
        );
        g.push(
            Setting::info(
                "nvml_version",
                "NVML Version",
                SettingValue::Text(nvml_version.clone()),
            )
            .with_source("NVML"),
        );

        if let Ok(mode) = dev.is_ecc_enabled() {
            g.push(
                Setting::info(
                    "ecc_mode",
                    "ECC Memory",
                    SettingValue::Bool(mode.currently_enabled),
                )
                .with_description(
                    "Whether ECC error correction is currently active on this GPU's VRAM.",
                )
                .with_risk(SettingRisk::Moderate)
                .with_source("nvmlDeviceIsEccEnabled"),
            );
        }
        #[cfg(target_os = "linux")]
        if let Ok(persistent) = dev.is_in_persistent_mode() {
            g.push(
                Setting::info(
                    "persistence_mode",
                    "Persistence Mode",
                    SettingValue::Bool(persistent),
                )
                .with_description(
                    "When enabled, the NVIDIA driver remains loaded with no active client. Reduces first-launch latency. Writable on Linux with root (NVML).",
                )
                .with_risk(SettingRisk::Safe)
                .with_source("nvmlDeviceGetPersistenceMode")
                .with_writable(true),
            );
        }
        if let Ok(limit_mw) = dev.enforced_power_limit() {
            let mut s = Setting::info(
                "power_limit_mw",
                "Power Limit",
                SettingValue::Uint(limit_mw as u64),
            )
            .with_unit("mW")
            .with_risk(SettingRisk::Moderate)
            .with_source("nvmlDeviceGetEnforcedPowerLimit");
            if let Ok(constraints) = dev.power_management_limit_constraints() {
                s.range = Some((
                    SettingValue::Uint(constraints.min_limit as u64),
                    SettingValue::Uint(constraints.max_limit as u64),
                ));
            }
            if let Ok(default) = dev.power_management_limit_default() {
                s.default = Some(SettingValue::Uint(default as u64));
            }
            g.push(s);
        }
        for (clock, label, id) in [
            (Clock::Graphics, "Max Graphics Clock", "max_gfx_clock_mhz"),
            (Clock::SM, "Max SM Clock", "max_sm_clock_mhz"),
            (Clock::Memory, "Max Memory Clock", "max_mem_clock_mhz"),
        ] {
            if let Ok(v) = dev.max_clock_info(clock) {
                g.push(
                    Setting::info(id, label, SettingValue::Uint(v as u64))
                        .with_unit("MHz")
                        .with_source("nvmlDeviceGetMaxClockInfo"),
                );
            }
        }
        if let Ok(v) = dev.clock(Clock::Graphics, ClockId::Current) {
            g.push(
                Setting::info(
                    "current_gfx_clock_mhz",
                    "Current Graphics Clock",
                    SettingValue::Uint(v as u64),
                )
                .with_unit("MHz")
                .with_source("nvmlDeviceGetClock"),
            );
        }
        if let Some(pstate) = dev.performance_state().ok().and_then(pstate_name) {
            g.push(
                Setting::info(
                    "performance_state",
                    "Performance State",
                    SettingValue::Text(pstate),
                )
                .with_description(
                    "P0 = highest performance, P15 = lowest. The driver picks this automatically.",
                )
                .with_source("nvmlDeviceGetPerformanceState"),
            );
        }
        if let Ok(brand) = dev.brand() {
            g.push(
                Setting::info("brand", "Brand", SettingValue::Text(format!("{:?}", brand)))
                    .with_source("NVML"),
            );
        }
        if let Ok(vbios) = dev.vbios_version() {
            g.push(
                Setting::info("vbios_version", "VBIOS Version", SettingValue::Text(vbios))
                    .with_source("nvmlDeviceGetVbiosVersion"),
            );
        }

        g.note(
            "Per-application profiles (NVPI-style DRS database) require NVAPI access \
            and are not yet implemented. The values shown here are NVML-readable \
            global driver state.",
        );
        groups.push(g);
    }

    if let Some(extra) = nvidia_kmod_params_group() {
        groups.push(extra);
    }
    groups
}

#[cfg(all(feature = "nvidia", target_os = "linux"))]
fn nvidia_kmod_params_group() -> Option<ProfileGroup> {
    let path = std::path::Path::new("/sys/module/nvidia/parameters");
    if !path.exists() {
        return None;
    }
    let mut g = ProfileGroup::new(
        Subsystem::Gpu,
        "NVIDIA kernel module",
        "nvidia.ko parameters",
        path.display().to_string(),
    );
    if let Ok(rd) = std::fs::read_dir(path) {
        for entry in rd.flatten() {
            let name = entry.file_name().to_string_lossy().to_string();
            let val = std::fs::read_to_string(entry.path())
                .map(|s| s.trim().to_string())
                .unwrap_or_else(|e| format!("<err: {}>", e));
            g.push(
                Setting::info(&name, &name, SettingValue::Text(val))
                    .with_source("/sys/module/nvidia/parameters"),
            );
        }
    }
    Some(g)
}

#[cfg(all(feature = "nvidia", not(target_os = "linux")))]
fn nvidia_kmod_params_group() -> Option<ProfileGroup> {
    None
}

#[cfg(all(target_os = "linux", feature = "amd"))]
fn amd_linux_groups() -> Vec<ProfileGroup> {
    use std::fs;
    let mut groups = Vec::new();
    let drm = match fs::read_dir("/sys/class/drm") {
        Ok(rd) => rd,
        Err(_) => return groups,
    };
    for entry in drm.flatten() {
        let name = entry.file_name().to_string_lossy().to_string();
        if !name.starts_with("card") || name.contains('-') {
            continue;
        }
        let dev_dir = entry.path().join("device");
        let vendor = fs::read_to_string(dev_dir.join("vendor")).unwrap_or_default();
        if vendor.trim() != "0x1002" {
            continue;
        }
        let model = fs::read_to_string(dev_dir.join("device"))
            .unwrap_or_default()
            .trim()
            .to_string();
        let mut g = ProfileGroup::new(
            Subsystem::Gpu,
            format!("AMD {} ({})", name, model),
            "amdgpu profile",
            dev_dir.display().to_string(),
        );
        for (file, id, label, risk) in [
            (
                "power_dpm_state",
                "power_dpm_state",
                "Power DPM State",
                SettingRisk::Moderate,
            ),
            (
                "power_dpm_force_performance_level",
                "perf_level",
                "Performance Level",
                SettingRisk::Moderate,
            ), // marked writable below via post-processing
            (
                "pp_power_profile_mode",
                "power_profile_mode",
                "Power Profile Mode",
                SettingRisk::Moderate,
            ),
            (
                "pp_od_clk_voltage",
                "od_clk_voltage",
                "Overdrive Clock/Voltage Table",
                SettingRisk::Dangerous,
            ),
            (
                "pp_dpm_sclk",
                "dpm_sclk",
                "SCLK DPM Table",
                SettingRisk::Informational,
            ),
            (
                "pp_dpm_mclk",
                "dpm_mclk",
                "MCLK DPM Table",
                SettingRisk::Informational,
            ),
            (
                "pp_dpm_pcie",
                "dpm_pcie",
                "PCIe DPM Table",
                SettingRisk::Informational,
            ),
        ] {
            let p = dev_dir.join(file);
            if let Ok(v) = fs::read_to_string(&p) {
                let value = if v.contains('\n') {
                    SettingValue::Text(v.trim().to_string())
                } else if let Ok(u) = v.trim().parse::<u64>() {
                    SettingValue::Uint(u)
                } else {
                    SettingValue::Text(v.trim().to_string())
                };
                let writable = id == "perf_level";
                g.push(
                    Setting::info(id, label, value)
                        .with_risk(risk)
                        .with_source(p.display().to_string())
                        .with_writable(writable),
                );
            }
        }
        // hwmon power cap
        if let Ok(hwmon) = fs::read_dir(dev_dir.join("hwmon")) {
            for hw in hwmon.flatten() {
                let cap = hw.path().join("power1_cap");
                if let Ok(v) = fs::read_to_string(&cap) {
                    if let Ok(uw) = v.trim().parse::<u64>() {
                        g.push(
                            Setting::info("power_cap_uw", "Power Cap", SettingValue::Uint(uw))
                                .with_unit("μW")
                                .with_risk(SettingRisk::Moderate)
                                .with_source(cap.display().to_string()),
                        );
                    }
                }
            }
        }
        groups.push(g);
    }
    groups
}

#[cfg(all(target_os = "linux", feature = "intel"))]
fn intel_linux_groups() -> Vec<ProfileGroup> {
    use std::fs;
    let mut groups = Vec::new();
    let drm = match fs::read_dir("/sys/class/drm") {
        Ok(rd) => rd,
        Err(_) => return groups,
    };
    for entry in drm.flatten() {
        let name = entry.file_name().to_string_lossy().to_string();
        if !name.starts_with("card") || name.contains('-') {
            continue;
        }
        let dev_dir = entry.path().join("device");
        let vendor = fs::read_to_string(dev_dir.join("vendor")).unwrap_or_default();
        if vendor.trim() != "0x8086" {
            continue;
        }
        let model = fs::read_to_string(dev_dir.join("device"))
            .unwrap_or_default()
            .trim()
            .to_string();
        let card_dir = entry.path();
        let mut g = ProfileGroup::new(
            Subsystem::Gpu,
            format!("Intel {} ({})", name, model),
            "i915/xe profile",
            card_dir.display().to_string(),
        );
        for (file, id, label) in [
            ("gt_min_freq_mhz", "gt_min_freq_mhz", "GT Min Frequency"),
            ("gt_max_freq_mhz", "gt_max_freq_mhz", "GT Max Frequency"),
            (
                "gt_boost_freq_mhz",
                "gt_boost_freq_mhz",
                "GT Boost Frequency",
            ),
            ("gt_cur_freq_mhz", "gt_cur_freq_mhz", "GT Current Frequency"),
            ("rc6_enable", "rc6_enable", "RC6 Power Saving"),
        ] {
            let p = card_dir.join(file);
            if let Ok(v) = fs::read_to_string(&p) {
                let value = v
                    .trim()
                    .parse::<u64>()
                    .map(SettingValue::Uint)
                    .unwrap_or_else(|_| SettingValue::Text(v.trim().to_string()));
                let writable = id == "gt_max_freq_mhz";
                g.push(
                    Setting::info(id, label, value)
                        .with_unit(if id.ends_with("_mhz") { "MHz" } else { "" })
                        .with_risk(SettingRisk::Moderate)
                        .with_source(p.display().to_string())
                        .with_writable(writable),
                );
            }
        }
        groups.push(g);
    }
    groups
}

#[cfg(windows)]
fn windows_display_class_groups() -> Vec<ProfileGroup> {
    use winreg::enums::*;
    use winreg::RegKey;

    let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
    let class_path =
        r"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}";
    let Ok(class_key) = hklm.open_subkey(class_path) else {
        return Vec::new();
    };

    let mut groups = Vec::new();
    let subkeys: Vec<String> = class_key.enum_keys().filter_map(|k| k.ok()).collect();
    for sub in subkeys {
        // Only 4-digit instance subkeys.
        if sub.len() != 4 || !sub.chars().all(|c| c.is_ascii_digit()) {
            continue;
        }
        let Ok(inst) = class_key.open_subkey(&sub) else {
            continue;
        };
        let desc: String = inst.get_value("DriverDesc").unwrap_or_default();
        if desc.is_empty() {
            continue;
        }
        let provider: String = inst.get_value("ProviderName").unwrap_or_default();
        let driver_ver: String = inst.get_value("DriverVersion").unwrap_or_default();
        let driver_date: String = inst.get_value("DriverDate").unwrap_or_default();

        let mut g = ProfileGroup::new(
            Subsystem::Gpu,
            &desc,
            "Driver class registry",
            format!(r"HKLM\{}\{}", class_path, sub),
        );
        g.push(Setting::info(
            "provider",
            "Provider",
            SettingValue::Text(provider),
        ));
        g.push(Setting::info(
            "driver_version",
            "Driver Version",
            SettingValue::Text(driver_ver),
        ));
        g.push(Setting::info(
            "driver_date",
            "Driver Date",
            SettingValue::Text(driver_date),
        ));

        // Enumerate string/dword values under the instance key — these are
        // the vendor-specific UMD/KMD overrides that vendor control panels
        // and tools like NVPI / AMD Adrenalin write.
        for name in inst.enum_values().filter_map(|v| v.ok()).map(|(n, _)| n) {
            if matches!(
                name.as_str(),
                "DriverDesc"
                    | "ProviderName"
                    | "DriverVersion"
                    | "DriverDate"
                    | "DriverDateData"
                    | "InfPath"
                    | "InfSection"
                    | "MatchingDeviceId"
            ) {
                continue;
            }
            // Try DWORD first, fall back to string.
            if let Ok(dw) = inst.get_value::<u32, _>(&name) {
                g.push(
                    Setting::info(&name, &name, SettingValue::Uint(dw as u64))
                        .with_risk(SettingRisk::Moderate)
                        .with_source("registry"),
                );
            } else if let Ok(s) = inst.get_value::<String, _>(&name) {
                if s.len() < 256 {
                    g.push(
                        Setting::info(&name, &name, SettingValue::Text(s))
                            .with_risk(SettingRisk::Moderate)
                            .with_source("registry"),
                    );
                }
            }
        }
        g.note(
            "Vendor control panels (NVIDIA Control Panel, AMD Adrenalin, Intel Arc \
            Control) and tweakers like NVIDIA Profile Inspector write per-driver \
            overrides into this registry instance. Per-application profiles \
            require NVAPI/ADL access (not yet implemented).",
        );
        groups.push(g);
    }
    groups
}

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

    #[test]
    fn provider_smoke() {
        let mut p = GpuProfileProvider::new();
        let _ = p.snapshot();
    }
}