syswatch 0.6.1

Single-host, read-only system diagnostics TUI. Twelve tabs covering CPU, memory, disks, processes, GPU, power, services, network, plus a Timeline scrubber and an Insights anomaly engine. Sibling to netwatch.
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
//! Cross-platform GPU discovery + (where free) live util/temp/power.
//!
//! macOS: `system_profiler SPDisplaysDataType -json` (no sudo) for static
//! identity; `ioreg -r -d 1 -w 0 -c IOAccelerator` (also no sudo) for live
//! `Device Utilization %` and `In use system memory` from each accelerator's
//! `PerformanceStatistics` dict. Temperature + per-rail power still need
//! `powermetrics --samplers gpu_power` (sudo) or IOReport private FFI —
//! deferred to v0.2.
//!
//! Linux: scan `/sys/class/drm/card*/device/` for vendor/device PCI IDs and
//! read `gpu_busy_percent` per tick when the driver exposes it (AMDGPU,
//! recent i915). NVIDIA needs nvml-wrapper — feature-gated, future work.

use crate::collect::model::GpuTick;

// ── NVIDIA via nvml-wrapper (opt-in via `gpu-nvidia` feature) ────────────
//
// Lazy-initializes a single `Nvml` handle on first use. If init fails (no
// driver, library missing, container without device passthrough), every
// nvml call here returns None and the rest of the GPU pipeline falls back
// to the PCI-ID-only stub for NVIDIA cards.
#[cfg(all(target_os = "linux", feature = "gpu-nvidia"))]
mod nvidia {
    use super::GpuTick;
    use nvml_wrapper::enum_wrappers::device::TemperatureSensor;
    use nvml_wrapper::Nvml;
    use std::sync::OnceLock;

    // Option<Nvml>: None if init failed. We attempt init exactly once per
    // process; reattempts on transient failures aren't worth the complexity.
    static NVML: OnceLock<Option<Nvml>> = OnceLock::new();

    fn nvml() -> Option<&'static Nvml> {
        NVML.get_or_init(|| Nvml::init().ok()).as_ref()
    }

    pub fn discover() -> Vec<GpuTick> {
        let Some(nvml) = nvml() else {
            return Vec::new();
        };
        let count = nvml.device_count().unwrap_or(0);
        (0..count)
            .filter_map(|i| nvml.device_by_index(i).ok())
            .map(|d| {
                let mem = d.memory_info().ok();
                GpuTick {
                    name: d.name().unwrap_or_else(|_| "NVIDIA".into()),
                    vendor: "NVIDIA".into(),
                    driver: nvml.sys_driver_version().ok(),
                    vram_total_bytes: mem.as_ref().map(|m| m.total),
                    vram_used_bytes: mem.as_ref().map(|m| m.used),
                    util_pct: None,
                    renderer_util_pct: None,
                    tiler_util_pct: None,
                    temp_c: None,
                    power_w: None,
                    live_data_hint: None,
                    last_submitter_pid: None,
                }
            })
            .collect()
    }

    /// Refresh per-tick mutable fields for every NVIDIA device in `devs`.
    /// Maps NVIDIA-vendor entries to nvml indices in encounter order — same
    /// shape as the AMDGPU sysfs path.
    pub fn refresh(devs: &mut [GpuTick]) {
        let Some(nvml) = nvml() else {
            return;
        };
        let mut nv_idx: u32 = 0;
        for dev in devs.iter_mut() {
            if dev.vendor != "NVIDIA" {
                continue;
            }
            let Ok(d) = nvml.device_by_index(nv_idx) else {
                nv_idx += 1;
                continue;
            };
            if let Ok(util) = d.utilization_rates() {
                dev.util_pct = Some(util.gpu as f32);
            }
            if let Ok(mem) = d.memory_info() {
                dev.vram_total_bytes = Some(mem.total);
                dev.vram_used_bytes = Some(mem.used);
            }
            if let Ok(t) = d.temperature(TemperatureSensor::Gpu) {
                dev.temp_c = Some(t as f32);
            }
            if let Ok(mw) = d.power_usage() {
                dev.power_w = Some(mw as f32 / 1000.0);
            }
            dev.live_data_hint = None;
            nv_idx += 1;
        }
    }
}

#[cfg(target_os = "macos")]
const HINT_MACOS_NO_IOREPORT: &str =
    "IOReport unavailable — temperature + per-rail power can't be sampled";
#[cfg(target_os = "linux")]
const HINT_LINUX_NO_AMDGPU: &str =
    "amdgpu driver not loaded — load it for util/VRAM/temp/power, or use the proprietary driver";
#[cfg(target_os = "linux")]
const HINT_LINUX_NVIDIA_NO_NVML: &str =
    "build with --features nvidia (linked against libnvidia-ml) for util/VRAM/temp/power";
#[cfg(target_os = "linux")]
const HINT_LINUX_INTEL: &str =
    "i915/xe live util needs `gpu_busy_percent` (recent kernels); per-rail power not exposed";

pub struct GpuDiscovery {
    /// Cached at startup (subprocess on macOS is too slow to poll).
    pub devices: Vec<GpuTick>,
}

impl GpuDiscovery {
    pub fn new() -> Self {
        Self {
            devices: discover(),
        }
    }

    /// Refresh per-tick mutable fields. On macOS the caller passes a
    /// pre-sampled `MacosTick` (constructed by the shared sampler in the
    /// Collector); on Linux we re-read sysfs directly. The macOS arg is
    /// ignored on other platforms.
    #[allow(unused_mut, unused_variables)]
    pub fn refresh(
        &mut self,
        #[cfg(target_os = "macos")] macos_tick: Option<&crate::collect::macos_sampler::MacosTick>,
    ) -> Vec<GpuTick> {
        let mut out = self.devices.clone();

        #[cfg(target_os = "macos")]
        {
            let stats = collect_macos_gpu_stats();
            // Zip in declaration order. system_profiler and ioreg both
            // enumerate accelerators in the same order on every Apple Silicon
            // box we've seen, so positional matching is reliable.
            for (dev, s) in out.iter_mut().zip(stats.iter()) {
                dev.util_pct = Some(s.device_util_pct);
                dev.renderer_util_pct = Some(s.renderer_util_pct);
                dev.tiler_util_pct = Some(s.tiler_util_pct);
                if s.in_use_system_memory > 0 {
                    dev.vram_used_bytes = Some(s.in_use_system_memory);
                }
                dev.last_submitter_pid = s.last_submission_pid;
            }

            // IOReport+SMC values from the shared sampler. None until the
            // sampler initializes successfully (or, for power, until the
            // second tick when a delta becomes available).
            let gpu_power_w = macos_tick.and_then(|t| t.gpu_power_w);
            let gpu_temp_c = macos_tick.and_then(|t| t.gpu_temp_c);
            for dev in out.iter_mut() {
                dev.power_w = gpu_power_w;
                dev.temp_c = gpu_temp_c;
                dev.live_data_hint = match (gpu_power_w, gpu_temp_c) {
                    (Some(_), Some(_)) => None,
                    _ if macos_tick.is_some() => None, // sampler ran, data primes next tick
                    _ => Some(HINT_MACOS_NO_IOREPORT.into()),
                };
            }
        }

        #[cfg(all(target_os = "linux", feature = "gpu-nvidia"))]
        nvidia::refresh(&mut out);

        #[cfg(target_os = "linux")]
        for (i, dev) in out.iter_mut().enumerate() {
            // gpu_busy_percent: AMDGPU + recent i915/xe both expose it.
            if let Some(util) = read_linux_busy_percent(i) {
                dev.util_pct = Some(util);
                dev.live_data_hint = None;
            }
            if dev.vendor == "AMD" {
                let device_path =
                    std::path::PathBuf::from(format!("/sys/class/drm/card{}/device", i));
                if let Some(used) = read_amdgpu_vram_bytes(&device_path.join("mem_info_vram_used"))
                {
                    dev.vram_used_bytes = Some(used);
                }
                // hwmon dir name varies (hwmon0, hwmon1, ...) — find the
                // first one nested under device/hwmon/.
                if let Some(hwmon) = find_amdgpu_hwmon_dir(&device_path) {
                    if let Some(t) = read_hwmon_temp_c(&hwmon.join("temp1_input")) {
                        dev.temp_c = Some(t);
                    }
                    if let Some(w) = read_hwmon_power_w(&hwmon.join("power1_average")) {
                        dev.power_w = Some(w);
                    }
                }
                // We've covered util/vram/temp/power — clear the hint so
                // the UI doesn't show a "needs nvml" message on AMD.
                if dev.util_pct.is_some()
                    && dev.vram_used_bytes.is_some()
                    && (dev.temp_c.is_some() || dev.power_w.is_some())
                {
                    dev.live_data_hint = None;
                }
            }
        }

        out
    }
}

#[cfg(target_os = "macos")]
fn discover() -> Vec<GpuTick> {
    use std::process::Command;
    let output = Command::new("system_profiler")
        .args(["SPDisplaysDataType", "-json"])
        .output();
    let Ok(out) = output else { return Vec::new() };
    let text = String::from_utf8_lossy(&out.stdout);
    let Ok(parsed): Result<serde_json::Value, _> = serde_json::from_str(&text) else {
        return Vec::new();
    };
    let Some(arr) = parsed.get("SPDisplaysDataType").and_then(|v| v.as_array()) else {
        return Vec::new();
    };
    arr.iter()
        .map(|d| {
            let name = d
                .get("sppci_model")
                .or_else(|| d.get("_name"))
                .and_then(|v| v.as_str())
                .unwrap_or("Unknown GPU")
                .to_string();
            // SPDisplays returns localization keys like "sppci_vendor_Apple";
            // strip the prefix so the UI shows a real vendor name.
            let vendor = d
                .get("spdisplays_vendor")
                .and_then(|v| v.as_str())
                .map(strip_macos_vendor_key)
                .unwrap_or_else(|| "Apple".into());
            let vram = d
                .get("spdisplays_vram_shared")
                .or_else(|| d.get("spdisplays_vram"))
                .and_then(|v| v.as_str())
                .and_then(parse_vram_string);
            let driver = d
                .get("spdisplays_metalfamily")
                .or_else(|| d.get("spdisplays_mtlgpufamilysupport"))
                .and_then(|v| v.as_str())
                .map(String::from);
            GpuTick {
                name,
                vendor,
                driver,
                vram_total_bytes: vram,
                vram_used_bytes: None,
                util_pct: None,
                renderer_util_pct: None,
                tiler_util_pct: None,
                temp_c: None,
                power_w: None,
                // refresh() recomputes this on the first tick based on
                // whether the IOReport+SMC handles initialized.
                live_data_hint: None,
                last_submitter_pid: None,
            }
        })
        .collect()
}

/// One row per IOAccelerator entry from `ioreg`.
#[cfg(target_os = "macos")]
#[derive(Debug, Clone, Default, PartialEq)]
struct MacGpuStats {
    device_util_pct: f32,
    renderer_util_pct: f32,
    tiler_util_pct: f32,
    in_use_system_memory: u64,
    alloc_system_memory: u64,
    /// From ioreg's `AGCInfo.fLastSubmissionPID` — the most recent
    /// process to submit GPU work. Rotating hint, not a usage metric.
    last_submission_pid: Option<u32>,
}

#[cfg(target_os = "macos")]
fn collect_macos_gpu_stats() -> Vec<MacGpuStats> {
    use std::process::Command;
    let Ok(out) = Command::new("ioreg")
        .args(["-r", "-d", "1", "-w", "0", "-c", "IOAccelerator"])
        .output()
    else {
        return Vec::new();
    };
    parse_ioreg_perf_stats(&String::from_utf8_lossy(&out.stdout))
}

/// Parse one `MacGpuStats` per IOAccelerator block from ioreg output.
///
/// Walks the text linearly, tracking accelerator-block boundaries
/// (`+-o ` lines from `ioreg -r -c IOAccelerator`). Within a block,
/// pulls `PerformanceStatistics` (util / VRAM) and `AGCInfo`
/// (`fLastSubmissionPID`) into the same struct so the per-device
/// indices stay aligned even when a block is missing one of the keys.
#[cfg(target_os = "macos")]
fn parse_ioreg_perf_stats(text: &str) -> Vec<MacGpuStats> {
    const PERF_PREFIX: &str = "\"PerformanceStatistics\" = {";
    const AGC_PREFIX: &str = "\"AGCInfo\" = {";
    let mut out: Vec<MacGpuStats> = Vec::new();
    let mut current: Option<MacGpuStats> = None;

    for line in text.lines() {
        // Block boundary: `+-o IOAccelerator...` starts a new device.
        if line.trim_start().starts_with("+-o ") {
            if let Some(prev) = current.take() {
                out.push(prev);
            }
            current = Some(MacGpuStats::default());
            continue;
        }

        // Auto-open a block on the first content line — defensive
        // against ioreg variants that omit the `+-o` boundary, plus
        // simpler test inputs.
        if current.is_none()
            && (extract_dict_body(line, PERF_PREFIX).is_some()
                || extract_dict_body(line, AGC_PREFIX).is_some())
        {
            current = Some(MacGpuStats::default());
        }

        let Some(stats) = current.as_mut() else {
            continue;
        };

        if let Some(body) = extract_dict_body(line, PERF_PREFIX) {
            apply_perf_stats(stats, body);
        } else if let Some(body) = extract_dict_body(line, AGC_PREFIX) {
            apply_agc_info(stats, body);
        }
    }
    if let Some(last) = current.take() {
        out.push(last);
    }
    out
}

/// Returns the inside-the-braces body of `"<key>" = {...}` if `prefix`
/// matches, otherwise None.
#[cfg(target_os = "macos")]
fn extract_dict_body<'a>(line: &'a str, prefix: &str) -> Option<&'a str> {
    let idx = line.find(prefix)?;
    let body_start = idx + prefix.len();
    let rel_end = line[body_start..].find('}')?;
    Some(&line[body_start..body_start + rel_end])
}

#[cfg(target_os = "macos")]
fn apply_agc_info(stats: &mut MacGpuStats, body: &str) {
    for pair in body.split(',') {
        let Some(eq) = pair.find('=') else { continue };
        let key = pair[..eq].trim().trim_matches('"');
        let val = pair[eq + 1..].trim();
        if key == "fLastSubmissionPID" {
            if let Ok(p) = val.parse::<u32>() {
                stats.last_submission_pid = Some(p);
            }
        }
    }
}

#[cfg(target_os = "macos")]
fn apply_perf_stats(stats: &mut MacGpuStats, body: &str) {
    for pair in body.split(',') {
        // Each pair is `"Key"=value`. Numbers only as values in the
        // PerformanceStatistics dict, no nested commas to worry about.
        let Some(eq) = pair.find('=') else { continue };
        let key = pair[..eq].trim().trim_matches('"');
        let val = pair[eq + 1..].trim();
        match key {
            "Device Utilization %" => {
                stats.device_util_pct = val.parse::<f32>().unwrap_or(0.0);
            }
            "Renderer Utilization %" => {
                stats.renderer_util_pct = val.parse::<f32>().unwrap_or(0.0);
            }
            "Tiler Utilization %" => {
                stats.tiler_util_pct = val.parse::<f32>().unwrap_or(0.0);
            }
            "In use system memory" => {
                stats.in_use_system_memory = val.parse::<u64>().unwrap_or(0);
            }
            "Alloc system memory" => {
                stats.alloc_system_memory = val.parse::<u64>().unwrap_or(0);
            }
            _ => {}
        }
    }
}

#[cfg(target_os = "linux")]
fn discover() -> Vec<GpuTick> {
    use std::fs;
    let mut out = Vec::new();
    let Ok(entries) = fs::read_dir("/sys/class/drm") else {
        return out;
    };
    // Sort so the per-card index used by refresh() matches discovery order.
    let mut entries: Vec<_> = entries.flatten().collect();
    entries.sort_by_key(|e| e.file_name());
    for entry in entries {
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        // Match cardN (no suffix — skip cardN-HDMI-A-1 connector entries).
        if !name_str.starts_with("card") || name_str.contains('-') {
            continue;
        }
        let card_path = entry.path();
        let device_path = card_path.join("device");
        let vendor_id = fs::read_to_string(device_path.join("vendor"))
            .ok()
            .map(|s| s.trim().to_string());
        let device_id = fs::read_to_string(device_path.join("device"))
            .ok()
            .map(|s| s.trim().to_string());

        let vendor = match vendor_id.as_deref() {
            Some("0x10de") => "NVIDIA",
            Some("0x1002") => "AMD",
            Some("0x8086") => "Intel",
            _ => "Unknown",
        }
        .to_string();
        let name = format!(
            "{} {}",
            vendor,
            device_id.unwrap_or_else(|| "Unknown".into())
        );

        // Per-vendor sysfs probes. AMD: amdgpu exposes mem_info_vram_total
        // even before any client allocates. NVIDIA: PCI ID only without nvml.
        // Intel: name from device ID; util via gpu_busy_percent in refresh.
        let vram_total_bytes = if vendor == "AMD" {
            read_amdgpu_vram_bytes(&device_path.join("mem_info_vram_total"))
        } else {
            None
        };
        let live_data_hint = match vendor.as_str() {
            "AMD" => None, // refresh() will fill util/vram_used/temp/power
            "NVIDIA" => Some(HINT_LINUX_NVIDIA_NO_NVML.into()),
            "Intel" => Some(HINT_LINUX_INTEL.into()),
            _ => Some(HINT_LINUX_NO_AMDGPU.into()),
        };

        out.push(GpuTick {
            name,
            vendor,
            driver: None,
            vram_total_bytes,
            vram_used_bytes: None,
            util_pct: None,
            renderer_util_pct: None,
            tiler_util_pct: None,
            temp_c: None,
            power_w: None,
            live_data_hint,
            last_submitter_pid: None,
        });
    }
    // Replace the PCI-ID-only NVIDIA stubs with rich nvml-derived entries
    // when the feature is on and nvml init succeeds. nvml output supersedes
    // sysfs entirely for NVIDIA — no stable mapping between sysfs cardN and
    // nvml index is exposed.
    #[cfg(feature = "gpu-nvidia")]
    {
        let nv = nvidia::discover();
        if !nv.is_empty() {
            out.retain(|g| g.vendor != "NVIDIA");
            out.extend(nv);
        }
    }
    out
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn discover() -> Vec<GpuTick> {
    Vec::new()
}

#[cfg(target_os = "linux")]
fn read_linux_busy_percent(card_idx: usize) -> Option<f32> {
    let path = format!("/sys/class/drm/card{}/device/gpu_busy_percent", card_idx);
    let s = std::fs::read_to_string(path).ok()?;
    s.trim().parse::<f32>().ok()
}

// ── AMDGPU sysfs helpers (linux | test for fixture-driven tests) ────────

/// Read a single u64 from `path` (whitespace-trimmed). Pure file IO so
/// tests can drive it from a tempdir.
#[cfg(any(target_os = "linux", test))]
fn read_amdgpu_vram_bytes(path: &std::path::Path) -> Option<u64> {
    std::fs::read_to_string(path)
        .ok()?
        .trim()
        .parse::<u64>()
        .ok()
}

/// AMDGPU exposes hwmon as `device/hwmon/hwmonN/`. Picks the first
/// `hwmon*` subdir; that's the AMDGPU-managed one (other hwmon
/// instances live under different parents).
#[cfg(any(target_os = "linux", test))]
fn find_amdgpu_hwmon_dir(device_path: &std::path::Path) -> Option<std::path::PathBuf> {
    let hwmon_root = device_path.join("hwmon");
    let entries = std::fs::read_dir(&hwmon_root).ok()?;
    for entry in entries.flatten() {
        let name = entry.file_name();
        if name.to_string_lossy().starts_with("hwmon") {
            return Some(entry.path());
        }
    }
    None
}

/// Read `temp1_input` (millicelsius) and convert to °C. amdgpu units are
/// stable across all kernels we care about.
#[cfg(any(target_os = "linux", test))]
fn read_hwmon_temp_c(path: &std::path::Path) -> Option<f32> {
    let raw: i64 = std::fs::read_to_string(path).ok()?.trim().parse().ok()?;
    Some(raw as f32 / 1000.0)
}

/// Read `power1_average` (microwatts) and convert to W. May read 0 when
/// the GPU is asleep — propagate as Some(0.0), the UI handles it.
#[cfg(any(target_os = "linux", test))]
fn read_hwmon_power_w(path: &std::path::Path) -> Option<f32> {
    let raw: u64 = std::fs::read_to_string(path).ok()?.trim().parse().ok()?;
    Some(raw as f32 / 1_000_000.0)
}

#[cfg(target_os = "macos")]
fn parse_vram_string(s: &str) -> Option<u64> {
    // "16 GB" / "8192 MB" / "1024"
    let parts: Vec<&str> = s.split_whitespace().collect();
    let n: f64 = parts.first()?.parse().ok()?;
    let mult: u64 = match parts.get(1).map(|s| s.to_ascii_uppercase()).as_deref() {
        Some("GB") => 1024 * 1024 * 1024,
        Some("MB") => 1024 * 1024,
        Some("KB") => 1024,
        _ => 1,
    };
    Some((n * mult as f64) as u64)
}

/// Strip macOS SPDisplays localization-key prefixes ("sppci_vendor_Apple" →
/// "Apple", "0x10de" → "10de"). Pulled out so it's testable without spawning
/// `system_profiler`.
#[cfg(target_os = "macos")]
fn strip_macos_vendor_key(s: &str) -> String {
    s.strip_prefix("sppci_vendor_")
        .unwrap_or(s)
        .trim_start_matches("0x")
        .to_string()
}

#[cfg(test)]
mod tests {
    #[cfg(target_os = "macos")]
    use super::*;

    #[cfg(target_os = "macos")]
    #[test]
    fn vram_string_handles_units() {
        assert_eq!(parse_vram_string("16 GB"), Some(16 * 1024 * 1024 * 1024));
        assert_eq!(parse_vram_string("8192 MB"), Some(8192 * 1024 * 1024));
        assert_eq!(parse_vram_string("512 KB"), Some(512 * 1024));
        // Lowercase units are folded.
        assert_eq!(parse_vram_string("4 gb"), Some(4 * 1024 * 1024 * 1024));
        // Decimals.
        assert_eq!(
            parse_vram_string("1.5 GB"),
            Some((1.5 * 1024.0 * 1024.0 * 1024.0) as u64)
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn vram_string_no_unit_treated_as_bytes() {
        assert_eq!(parse_vram_string("1024"), Some(1024));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn vram_string_garbage_returns_none() {
        assert_eq!(parse_vram_string(""), None);
        assert_eq!(parse_vram_string("not a number"), None);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn strips_sppci_vendor_prefix() {
        assert_eq!(strip_macos_vendor_key("sppci_vendor_Apple"), "Apple");
        assert_eq!(strip_macos_vendor_key("sppci_vendor_AMD"), "AMD");
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn strips_hex_vendor_id() {
        assert_eq!(strip_macos_vendor_key("0x10de"), "10de");
        assert_eq!(strip_macos_vendor_key("0x1002"), "1002");
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn passes_through_unknown_format() {
        assert_eq!(strip_macos_vendor_key("Apple"), "Apple");
        assert_eq!(strip_macos_vendor_key("NVIDIA Corp"), "NVIDIA Corp");
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn parses_real_perf_stats_line() {
        // Captured verbatim from `ioreg -r -d 1 -w 0 -c IOAccelerator` on M3 Pro.
        let sample = r#"
+-o AGXAcceleratorG15X  <class AGXAcceleratorG15X, id 0x100000481, ...>
    {
      "model" = "Apple M3 Pro"
      "PerformanceStatistics" = {"In use system memory (driver)"=0,"Alloc system memory"=16749051904,"Tiler Utilization %"=7,"recoveryCount"=0,"lastRecoveryTime"=0,"Renderer Utilization %"=11,"TiledSceneBytes"=1441792,"Device Utilization %"=16,"SplitSceneCount"=0,"Allocated PB Size"=89915392,"In use system memory"=568164352}
    }
        "#;
        let stats = parse_ioreg_perf_stats(sample);
        assert_eq!(stats.len(), 1);
        let s = &stats[0];
        assert_eq!(s.device_util_pct as i32, 16);
        assert_eq!(s.renderer_util_pct as i32, 11);
        assert_eq!(s.tiler_util_pct as i32, 7);
        assert_eq!(s.in_use_system_memory, 568_164_352);
        assert_eq!(s.alloc_system_memory, 16_749_051_904);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn handles_multiple_accelerators() {
        let sample = r#"
+-o A
    "PerformanceStatistics" = {"Device Utilization %"=10,"In use system memory"=100}
+-o B
    "PerformanceStatistics" = {"Device Utilization %"=90,"In use system memory"=200}
        "#;
        let stats = parse_ioreg_perf_stats(sample);
        assert_eq!(stats.len(), 2);
        assert_eq!(stats[0].device_util_pct as i32, 10);
        assert_eq!(stats[1].device_util_pct as i32, 90);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn pulls_last_submission_pid_from_agc_info() {
        let sample = r#"
+-o AGXAcceleratorG15X
    "AGCInfo" = {"fLastSubmissionPID"=373,"fSubmissionsSinceLastCheck"=0,"fBusyCount"=0}
    "PerformanceStatistics" = {"Device Utilization %"=42,"In use system memory"=100}
        "#;
        let stats = parse_ioreg_perf_stats(sample);
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].last_submission_pid, Some(373));
        assert_eq!(stats[0].device_util_pct as i32, 42);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn missing_agc_info_leaves_pid_none() {
        // Block has PerfStats but no AGCInfo line.
        let sample = r#"
+-o AGX
    "PerformanceStatistics" = {"Device Utilization %"=10}
        "#;
        let stats = parse_ioreg_perf_stats(sample);
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].last_submission_pid, None);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn agc_info_attribution_aligns_per_block() {
        // Only the second block has AGCInfo; ensure it lands on the
        // right device, not the first.
        let sample = r#"
+-o A
    "PerformanceStatistics" = {"Device Utilization %"=10}
+-o B
    "AGCInfo" = {"fLastSubmissionPID"=999}
    "PerformanceStatistics" = {"Device Utilization %"=90}
        "#;
        let stats = parse_ioreg_perf_stats(sample);
        assert_eq!(stats.len(), 2);
        assert_eq!(stats[0].last_submission_pid, None);
        assert_eq!(stats[1].last_submission_pid, Some(999));
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn no_perf_stats_yields_empty_vec() {
        assert!(parse_ioreg_perf_stats("nothing useful here").is_empty());
        assert!(parse_ioreg_perf_stats("").is_empty());
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn missing_fields_default_to_zero() {
        let sample = r#""PerformanceStatistics" = {"Device Utilization %"=42}"#;
        let stats = parse_ioreg_perf_stats(sample);
        assert_eq!(stats.len(), 1);
        assert_eq!(stats[0].device_util_pct as i32, 42);
        assert_eq!(stats[0].renderer_util_pct, 0.0);
        assert_eq!(stats[0].in_use_system_memory, 0);
    }

    // ── AMDGPU sysfs helpers ── exercised on any host via tempfile ────────

    use super::{
        find_amdgpu_hwmon_dir, read_amdgpu_vram_bytes, read_hwmon_power_w, read_hwmon_temp_c,
    };
    use std::fs;

    #[test]
    fn amdgpu_vram_parses_bytes() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("mem_info_vram_total");
        fs::write(&path, "17163091968\n").unwrap();
        assert_eq!(read_amdgpu_vram_bytes(&path), Some(17_163_091_968));
    }

    #[test]
    fn amdgpu_vram_missing_file_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(read_amdgpu_vram_bytes(&dir.path().join("missing")), None);
    }

    #[test]
    fn amdgpu_vram_garbage_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("mem_info_vram_total");
        fs::write(&path, "not a number").unwrap();
        assert_eq!(read_amdgpu_vram_bytes(&path), None);
    }

    #[test]
    fn finds_first_hwmon_subdir() {
        let dir = tempfile::tempdir().unwrap();
        let hwmon0 = dir.path().join("hwmon").join("hwmon3");
        fs::create_dir_all(&hwmon0).unwrap();
        let found = find_amdgpu_hwmon_dir(dir.path()).unwrap();
        assert_eq!(found, hwmon0);
    }

    #[test]
    fn skips_non_hwmon_entries_in_hwmon_dir() {
        let dir = tempfile::tempdir().unwrap();
        // Some kernels stash a `subsystem` symlink in here too.
        fs::create_dir_all(dir.path().join("hwmon").join("hwmon2")).unwrap();
        let found = find_amdgpu_hwmon_dir(dir.path()).unwrap();
        assert!(found
            .file_name()
            .unwrap()
            .to_string_lossy()
            .starts_with("hwmon"));
    }

    #[test]
    fn no_hwmon_dir_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(find_amdgpu_hwmon_dir(dir.path()), None);
    }

    #[test]
    fn hwmon_temp_converts_millicelsius() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("temp1_input");
        fs::write(&path, "65500\n").unwrap();
        assert_eq!(read_hwmon_temp_c(&path), Some(65.5));
    }

    #[test]
    fn hwmon_power_converts_microwatts() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("power1_average");
        fs::write(&path, "85000000\n").unwrap();
        assert_eq!(read_hwmon_power_w(&path), Some(85.0));
    }

    #[test]
    fn hwmon_power_zero_passes_through() {
        // GPU asleep — driver returns 0 µW, not "missing".
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("power1_average");
        fs::write(&path, "0\n").unwrap();
        assert_eq!(read_hwmon_power_w(&path), Some(0.0));
    }
}