rdmatop 0.1.32

A real-time TUI monitor for RDMA network interfaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
//! XGMI data layer built on top of AMD SMI (`libamd_smi`). The library is
//! dlopen'd at runtime; when absent or failing to init,
//! `read_all_xgmi_stats` returns an empty vector.

use std::ffi::CStr;
use std::io;
use std::ptr;
use std::sync::{Mutex, OnceLock};

/// Snapshot of a single XGMI link at one sample.
#[derive(Clone, Debug)]
pub struct XgmiLinkSnapshot {
    /// Peer GPU index (also indexes amdsmi's per-peer accumulators); not
    /// contiguous since the GPU's own index is skipped.
    pub link_id: u32,
    pub is_active: bool,
    /// Link max bandwidth in Gb/s (e.g. 512 on MI325X). Used for bar scaling.
    pub speed_gbps: Option<f64>,
    /// Per-lane bit rate in Gb/s (e.g. 32). Reserved for future column display.
    #[allow(dead_code)]
    pub bit_rate_gbps: Option<f64>,
    /// BDF of the peer GPU on the other end of the link.
    pub remote_pci_bdf: Option<String>,
    /// Accumulated bytes written to the link (amdsmi `write` KB * 1024).
    pub tx_bytes: Option<u64>,
    /// Accumulated bytes read from the link (amdsmi `read` KB * 1024).
    pub rx_bytes: Option<u64>,
}

/// Snapshot of every XGMI link attached to one GPU.
#[derive(Clone, Debug)]
pub struct XgmiSnapshot {
    pub gpu_index: u32,
    pub gpu_name: String,
    /// Number of topology-confirmed XGMI peer links.
    pub link_count: u32,
    /// Sum of active links' max bandwidth in Gb/s.
    pub link_gbps: Option<f64>,
    /// XGMI_WAFL block accumulated ECC counts (per-GPU, not per-link).
    pub correctable_errors: Option<u64>,
    pub uncorrectable_errors: Option<u64>,
    /// Live GPU health (util/vram/temp/power/clock) for the gauge strip.
    pub metrics: Option<crate::gpu::GpuMetrics>,
    pub links: Vec<XgmiLinkSnapshot>,
}

impl XgmiSnapshot {
    /// Count of links currently reported as up.
    pub fn active_links(&self) -> u32 {
        self.links.iter().filter(|l| l.is_active).count() as u32
    }
}

/// Render an `amdsmi_bdf_t` u64 as "dddd:bb:dd.f" (domain:bus:device.function).
/// Bit layout (LSB first): function 3 bits, device 5, bus 8, domain 48.
fn format_bdf(bdf: u64) -> String {
    let function = bdf & 0x7;
    let device = (bdf >> 3) & 0x1f;
    let bus = (bdf >> 8) & 0xff;
    let domain = (bdf >> 16) & 0xffff_ffff_ffff;
    format!("{:04x}:{:02x}:{:02x}.{:x}", domain, bus, device, function)
}

/// Minimal hand-rolled bindings for the amdsmi entry points rdmatop needs.
/// Layouts are pinned to the ROCm 6.x ABI and guarded by size/offset
/// assertions below; see amdsmi.h in ROCm for the reference definitions.
mod ffi {
    use std::os::raw::{c_char, c_void};

    pub type AmdsmiStatus = u32;
    pub type SocketHandle = *mut c_void;
    pub type ProcessorHandle = *mut c_void;

    pub const AMDSMI_STATUS_SUCCESS: AmdsmiStatus = 0;
    pub const AMDSMI_INIT_AMD_GPUS: u64 = 1 << 1;
    pub const AMDSMI_MAX_STRING_LENGTH: usize = 256;
    pub const AMDSMI_MAX_NUM_XGMI_PHYSICAL_LINK: usize = 64;
    pub const AMDSMI_IOLINK_TYPE_XGMI: u32 = 2;
    pub const AMDSMI_GPU_BLOCK_XGMI_WAFL: u64 = 0x80;
    pub const AMDSMI_TEMPERATURE_TYPE_EDGE: u32 = 0;
    pub const AMDSMI_TEMP_CURRENT: u32 = 0;
    pub const AMDSMI_CLK_TYPE_GFX: u32 = 0;

    /// One entry of `amdsmi_link_metrics_t.links[]`.
    #[repr(C)]
    #[derive(Clone, Copy)]
    pub struct LinkMetricsEntry {
        pub bdf: u64,
        pub bit_rate: u32,      // current link speed in Gb/s
        pub max_bandwidth: u32, // max link bandwidth in Gb/s
        pub link_type: u32,     // amdsmi_link_type_t
        pub read_kb: u64,       // total data received per link (KB)
        pub write_kb: u64,      // total data transmitted per link (KB)
        pub reserved: [u64; 2],
    }

    #[repr(C)]
    pub struct LinkMetrics {
        pub num_links: u32,
        pub links: [LinkMetricsEntry; AMDSMI_MAX_NUM_XGMI_PHYSICAL_LINK],
        pub reserved: [u64; 7],
    }

    #[repr(C)]
    pub struct AsicInfo {
        pub market_name: [c_char; AMDSMI_MAX_STRING_LENGTH],
        pub vendor_id: u32,
        pub vendor_name: [c_char; AMDSMI_MAX_STRING_LENGTH],
        pub subvendor_id: u32,
        pub device_id: u64,
        pub rev_id: u32,
        pub asic_serial: [c_char; AMDSMI_MAX_STRING_LENGTH],
        pub oam_id: u32,
        pub num_of_compute_units: u32,
        pub target_graphics_version: u64,
        pub reserved: [u32; 22],
    }

    #[repr(C)]
    pub struct ErrorCount {
        pub correctable_count: u64,
        pub uncorrectable_count: u64,
        pub deferred_count: u64,
        pub reserved: [u64; 5],
    }

    /// Prefix of `amdsmi_pcie_info_t` (512 bytes total). Only the two leading
    /// `pcie_static` fields are read; the remainder is opaque padding.
    #[repr(C)]
    pub struct PcieInfo {
        pub max_pcie_width: u16,
        pub max_pcie_speed: u32, // MT/s at runtime (header comment says GT/s)
        pub _rest: [u64; 63],
    }

    /// GPU engine utilization percentages (`amdsmi_engine_usage_t`).
    #[repr(C)]
    pub struct EngineUsage {
        pub gfx_activity: u32, // %
        pub umc_activity: u32, // %
        pub mm_activity: u32,  // %
        pub reserved: [u32; 13],
    }

    /// VRAM usage (`amdsmi_vram_usage_t`); both values in MB, total first.
    #[repr(C)]
    pub struct VramUsage {
        pub vram_total: u32, // MB
        pub vram_used: u32,  // MB
        pub reserved: [u32; 2],
    }

    /// Socket power (`amdsmi_power_info_t`): `current_socket_power` is W on
    /// MI300+ only; `average_socket_power` is W on Navi/MI200 and earlier.
    #[repr(C)]
    pub struct PowerInfo {
        pub socket_power: u64, // uW, host (virtualization) only
        pub current_socket_power: u32,
        pub average_socket_power: u32,
        pub gfx_voltage: u32, // mV
        pub soc_voltage: u32, // mV
        pub mem_voltage: u32, // mV
        pub power_limit: u32, // W
        pub reserved: [u32; 2],
    }

    /// Clock info (`amdsmi_clk_info_t`); only the current `clk` MHz is read.
    #[repr(C)]
    pub struct ClkInfo {
        pub clk: u32,     // MHz
        pub min_clk: u32, // MHz
        pub max_clk: u32, // MHz
        pub clk_locked: u8,
        pub clk_deep_sleep: u8,
        pub reserved: [u32; 4],
    }

    // ABI guards: sizes/offsets computed from amdsmi.h (ROCm 6.4.2). A silent
    // layout drift would corrupt every field after the divergence point.
    const _: () = assert!(std::mem::size_of::<LinkMetricsEntry>() == 56);
    const _: () = assert!(std::mem::offset_of!(LinkMetricsEntry, read_kb) == 24);
    const _: () = assert!(std::mem::size_of::<LinkMetrics>() == 3648);
    const _: () = assert!(std::mem::offset_of!(LinkMetrics, links) == 8);
    const _: () = assert!(std::mem::size_of::<AsicInfo>() == 896);
    const _: () = assert!(std::mem::offset_of!(AsicInfo, device_id) == 520);
    const _: () = assert!(std::mem::size_of::<ErrorCount>() == 64);
    const _: () = assert!(std::mem::size_of::<PcieInfo>() == 512);
    const _: () = assert!(std::mem::offset_of!(PcieInfo, max_pcie_speed) == 4);
    const _: () = assert!(std::mem::offset_of!(PcieInfo, _rest) == 8);
    const _: () = assert!(std::mem::size_of::<EngineUsage>() == 64);
    const _: () = assert!(std::mem::offset_of!(EngineUsage, gfx_activity) == 0);
    const _: () = assert!(std::mem::size_of::<VramUsage>() == 16);
    const _: () = assert!(std::mem::offset_of!(VramUsage, vram_total) == 0);
    const _: () = assert!(std::mem::offset_of!(VramUsage, vram_used) == 4);
    const _: () = assert!(std::mem::size_of::<PowerInfo>() == 40);
    const _: () = assert!(std::mem::offset_of!(PowerInfo, current_socket_power) == 8);
    const _: () = assert!(std::mem::offset_of!(PowerInfo, average_socket_power) == 12);
    const _: () = assert!(std::mem::size_of::<ClkInfo>() == 32);
    const _: () = assert!(std::mem::offset_of!(ClkInfo, clk) == 0);
}

/// Resolved amdsmi function pointers. `_lib` keeps the dlopen handle alive
/// for the lifetime of the pointers.
struct Amdsmi {
    _lib: libloading::Library,
    get_socket_handles: unsafe extern "C" fn(*mut u32, *mut ffi::SocketHandle) -> ffi::AmdsmiStatus,
    get_processor_handles: unsafe extern "C" fn(
        ffi::SocketHandle,
        *mut u32,
        *mut ffi::ProcessorHandle,
    ) -> ffi::AmdsmiStatus,
    get_gpu_asic_info:
        unsafe extern "C" fn(ffi::ProcessorHandle, *mut ffi::AsicInfo) -> ffi::AmdsmiStatus,
    get_link_metrics:
        unsafe extern "C" fn(ffi::ProcessorHandle, *mut ffi::LinkMetrics) -> ffi::AmdsmiStatus,
    get_gpu_device_bdf: unsafe extern "C" fn(ffi::ProcessorHandle, *mut u64) -> ffi::AmdsmiStatus,
    get_pcie_info:
        unsafe extern "C" fn(ffi::ProcessorHandle, *mut ffi::PcieInfo) -> ffi::AmdsmiStatus,
    topo_get_link_type: unsafe extern "C" fn(
        ffi::ProcessorHandle,
        ffi::ProcessorHandle,
        *mut u64,
        *mut u32,
    ) -> ffi::AmdsmiStatus,
    get_gpu_ecc_count:
        unsafe extern "C" fn(ffi::ProcessorHandle, u64, *mut ffi::ErrorCount) -> ffi::AmdsmiStatus,
    // Health-metric entry points, absent on older libamd_smi builds: each
    // None disables only its GpuMetrics field; link monitoring still works.
    get_gpu_activity: Option<
        unsafe extern "C" fn(ffi::ProcessorHandle, *mut ffi::EngineUsage) -> ffi::AmdsmiStatus,
    >,
    get_gpu_vram_usage: Option<
        unsafe extern "C" fn(ffi::ProcessorHandle, *mut ffi::VramUsage) -> ffi::AmdsmiStatus,
    >,
    get_temp_metric:
        Option<unsafe extern "C" fn(ffi::ProcessorHandle, u32, u32, *mut i64) -> ffi::AmdsmiStatus>,
    get_power_info: Option<
        unsafe extern "C" fn(ffi::ProcessorHandle, *mut ffi::PowerInfo) -> ffi::AmdsmiStatus,
    >,
    get_clock_info: Option<
        unsafe extern "C" fn(ffi::ProcessorHandle, u32, *mut ffi::ClkInfo) -> ffi::AmdsmiStatus,
    >,
}

/// Library names tried in order: bare sonames first (ld.so search path),
/// then the conventional ROCm install location.
const LIB_CANDIDATES: [&str; 3] = [
    "libamd_smi.so",
    "libamd_smi.so.25",
    "/opt/rocm/lib/libamd_smi.so",
];

impl Amdsmi {
    fn load() -> Option<Self> {
        for path in LIB_CANDIDATES {
            if let Ok(lib) = unsafe { libloading::Library::new(path) } {
                if let Some(api) = Self::from_lib(lib) {
                    return Some(api);
                }
            }
        }
        None
    }

    fn from_lib(lib: libloading::Library) -> Option<Self> {
        // Fn pointers are copied out of the Symbol wrappers; they stay valid
        // because `_lib` is stored alongside them.
        unsafe {
            let init: unsafe extern "C" fn(u64) -> ffi::AmdsmiStatus =
                *lib.get(b"amdsmi_init\0").ok()?;
            let api = Amdsmi {
                get_socket_handles: *lib.get(b"amdsmi_get_socket_handles\0").ok()?,
                get_processor_handles: *lib.get(b"amdsmi_get_processor_handles\0").ok()?,
                get_gpu_asic_info: *lib.get(b"amdsmi_get_gpu_asic_info\0").ok()?,
                get_link_metrics: *lib.get(b"amdsmi_get_link_metrics\0").ok()?,
                get_gpu_device_bdf: *lib.get(b"amdsmi_get_gpu_device_bdf\0").ok()?,
                get_pcie_info: *lib.get(b"amdsmi_get_pcie_info\0").ok()?,
                topo_get_link_type: *lib.get(b"amdsmi_topo_get_link_type\0").ok()?,
                get_gpu_ecc_count: *lib.get(b"amdsmi_get_gpu_ecc_count\0").ok()?,
                get_gpu_activity: lib.get(b"amdsmi_get_gpu_activity\0").ok().map(|s| *s),
                get_gpu_vram_usage: lib.get(b"amdsmi_get_gpu_vram_usage\0").ok().map(|s| *s),
                get_temp_metric: lib.get(b"amdsmi_get_temp_metric\0").ok().map(|s| *s),
                get_power_info: lib.get(b"amdsmi_get_power_info\0").ok().map(|s| *s),
                get_clock_info: lib.get(b"amdsmi_get_clock_info\0").ok().map(|s| *s),
                _lib: lib,
            };
            if init(ffi::AMDSMI_INIT_AMD_GPUS) != ffi::AMDSMI_STATUS_SUCCESS {
                return None;
            }
            Some(api)
        }
    }
}

/// dlopen + amdsmi_init exactly once per process; amdsmi_shut_down is never
/// called (process exit tears it down). A failed init is cached as `None`.
fn instance() -> Option<&'static Amdsmi> {
    static INSTANCE: OnceLock<Option<Amdsmi>> = OnceLock::new();
    INSTANCE.get_or_init(Amdsmi::load).as_ref()
}

/// C out-param wrapper with trailing headroom so a newer libamd_smi whose
/// struct outgrew our pinned ROCm 6.x layout cannot overrun the allocation.
#[repr(C)]
struct Padded<T> {
    value: T,
    _headroom: [u8; 4096],
}

impl<T> Padded<T> {
    /// All-integer repr(C) PODs only, mirroring the zeroed C-side init.
    fn zeroed() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

/// Read XGMI statistics for every AMD GPU in the system. Missing library or
/// zero GPUs yield an empty vector; per-GPU failures skip that GPU and
/// per-field failures degrade to `None`.
pub fn read_all_xgmi_stats() -> io::Result<Vec<XgmiSnapshot>> {
    let api = match instance() {
        Some(a) => a,
        None => return Ok(Vec::new()),
    };

    let handles = enumerate_gpus(api);
    if handles.is_empty() {
        return Ok(Vec::new());
    }
    let bdfs: Vec<Option<u64>> = handles.iter().map(|&h| read_bdf(api, h)).collect();
    let names = gpu_names(api, &handles);
    let speeds = link_speeds(api, &handles);
    let peers = xgmi_peers(api, &handles);

    let mut snapshots = Vec::with_capacity(handles.len());
    for idx in 0..handles.len() {
        if let Some(snap) =
            read_processor_snapshot(api, &handles, &bdfs, names, &speeds, &peers, idx)
        {
            snapshots.push(snap);
        }
    }
    Ok(snapshots)
}

/// GPU marketing names, read once and cached by GPU index:
/// `amdsmi_get_gpu_asic_info` leaks one fd per call on ROCm 6.4.x, so
/// calling it every refresh exhausts the fd table within minutes.
fn gpu_names(api: &Amdsmi, handles: &[ffi::ProcessorHandle]) -> &'static [String] {
    static NAMES: OnceLock<Vec<String>> = OnceLock::new();
    NAMES.get_or_init(|| {
        handles
            .iter()
            .map(|&h| read_gpu_name(api, h).unwrap_or_default())
            .collect()
    })
}

/// XGMI peer adjacency, row-major [src][dst]. Topology is static while the
/// driver is loaded, so the O(N^2) topo sweep is cached — but only once every
/// call answered and only while the GPU count matches (retry/rebuild otherwise).
fn xgmi_peers(api: &Amdsmi, handles: &[ffi::ProcessorHandle]) -> Vec<Vec<bool>> {
    static PEERS: Mutex<Option<Vec<Vec<bool>>>> = Mutex::new(None);
    let mut cached = PEERS.lock().unwrap_or_else(|e| e.into_inner());
    if let Some(m) = cached.as_ref() {
        if m.len() == handles.len() {
            return m.clone();
        }
    }
    let n = handles.len();
    let mut matrix = vec![vec![false; n]; n];
    let mut complete = true;
    for src in 0..n {
        for dst in 0..n {
            if src == dst {
                continue;
            }
            match is_xgmi_peer(api, handles[src], handles[dst]) {
                Some(peer) => matrix[src][dst] = peer,
                None => complete = false,
            }
        }
    }
    if complete && n > 0 {
        *cached = Some(matrix.clone());
    }
    matrix
}

/// Per-link (bit_rate, bandwidth) in Gb/s from PCIe static info.
type LinkSpeed = (Option<f64>, Option<f64>);

/// Per-GPU link speeds. Link speed is static and `amdsmi_get_pcie_info`
/// costs ~0.5 ms (two SMU round-trips), so successful reads are cached by
/// GPU index; failed reads retry on the next poll rather than degrading
/// speed reporting for the process lifetime.
fn link_speeds(api: &Amdsmi, handles: &[ffi::ProcessorHandle]) -> Vec<LinkSpeed> {
    static CACHE: Mutex<Vec<Option<LinkSpeed>>> = Mutex::new(Vec::new());
    let mut cache = CACHE.lock().unwrap_or_else(|e| e.into_inner());
    if cache.len() < handles.len() {
        cache.resize(handles.len(), None);
    }
    handles
        .iter()
        .zip(cache.iter_mut())
        .map(|(&handle, slot)| match slot {
            Some(speed) => *speed,
            None => {
                let speed = read_link_speed(api, handle);
                if speed != (None, None) {
                    *slot = Some(speed);
                }
                speed
            }
        })
        .collect()
}

/// Flatten socket/processor enumeration into one ordered GPU handle list.
/// This order defines `gpu_index` and matches amdsmi's accumulator indexing.
fn enumerate_gpus(api: &Amdsmi) -> Vec<ffi::ProcessorHandle> {
    let mut handles = Vec::new();
    let mut socket_count: u32 = 0;
    let rc = unsafe { (api.get_socket_handles)(&mut socket_count, ptr::null_mut()) };
    if rc != ffi::AMDSMI_STATUS_SUCCESS || socket_count == 0 {
        return handles;
    }
    let mut sockets: Vec<ffi::SocketHandle> = vec![ptr::null_mut(); socket_count as usize];
    let rc = unsafe { (api.get_socket_handles)(&mut socket_count, sockets.as_mut_ptr()) };
    if rc != ffi::AMDSMI_STATUS_SUCCESS {
        return handles;
    }
    for &socket in sockets.iter().take(socket_count as usize) {
        let mut proc_count: u32 = 0;
        let rc = unsafe { (api.get_processor_handles)(socket, &mut proc_count, ptr::null_mut()) };
        if rc != ffi::AMDSMI_STATUS_SUCCESS || proc_count == 0 {
            continue;
        }
        let mut procs: Vec<ffi::ProcessorHandle> = vec![ptr::null_mut(); proc_count as usize];
        let rc =
            unsafe { (api.get_processor_handles)(socket, &mut proc_count, procs.as_mut_ptr()) };
        if rc != ffi::AMDSMI_STATUS_SUCCESS {
            continue;
        }
        handles.extend(procs.iter().take(proc_count as usize).copied());
    }
    handles
}

fn read_bdf(api: &Amdsmi, handle: ffi::ProcessorHandle) -> Option<u64> {
    let mut bdf: u64 = 0;
    let rc = unsafe { (api.get_gpu_device_bdf)(handle, &mut bdf) };
    (rc == ffi::AMDSMI_STATUS_SUCCESS).then_some(bdf)
}

/// Build one GPU's snapshot. A "link" is an XGMI peer connection: topology
/// says which destination GPUs are XGMI-reachable, link_metrics supplies the
/// per-peer accumulators (indexed by destination GPU), pcie_info the speed.
fn read_processor_snapshot(
    api: &Amdsmi,
    handles: &[ffi::ProcessorHandle],
    bdfs: &[Option<u64>],
    names: &[String],
    speeds: &[LinkSpeed],
    peers: &[Vec<bool>],
    idx: usize,
) -> Option<XgmiSnapshot> {
    let handle = handles[idx];
    let mut metrics: Padded<ffi::LinkMetrics> = Padded::zeroed();
    let rc = unsafe { (api.get_link_metrics)(handle, &mut metrics.value) };
    if rc != ffi::AMDSMI_STATUS_SUCCESS {
        return None;
    }
    let metrics = &metrics.value;

    let gpu_name = names.get(idx).cloned().unwrap_or_default();
    let (bit_rate_gbps, speed_gbps) = speeds.get(idx).copied().unwrap_or((None, None));
    let (correctable_errors, uncorrectable_errors) = read_wafl_errors(api, handle);

    let n = (metrics.num_links as usize).min(ffi::AMDSMI_MAX_NUM_XGMI_PHYSICAL_LINK);
    let mut links = Vec::new();
    let mut total_gbps = 0.0;
    for (peer_idx, peer_bdf) in bdfs.iter().enumerate() {
        if peer_idx == idx
            || !peers
                .get(idx)
                .and_then(|row| row.get(peer_idx))
                .copied()
                .unwrap_or(false)
        {
            continue;
        }
        // Accumulators are indexed by destination GPU; peers beyond the
        // reported entry count carry no counters but still show the link.
        // The positional mapping is trusted only while the entry's own bdf
        // is unset (always zero on ROCm 6.4) or agrees with the peer's.
        let (tx_bytes, rx_bytes) = match metrics.links.get(peer_idx) {
            Some(entry) if peer_idx < n && (entry.bdf == 0 || Some(entry.bdf) == *peer_bdf) => (
                Some(entry.write_kb.wrapping_mul(1024)),
                Some(entry.read_kb.wrapping_mul(1024)),
            ),
            _ => (None, None),
        };
        total_gbps += speed_gbps.unwrap_or(0.0);
        links.push(XgmiLinkSnapshot {
            link_id: peer_idx as u32,
            // amdsmi_get_gpu_xgmi_link_status reports DOWN/DISABLE on
            // working MI300 links, so topo-confirmed peers show as active.
            is_active: true,
            speed_gbps,
            bit_rate_gbps,
            remote_pci_bdf: peer_bdf.map(format_bdf),
            tx_bytes,
            rx_bytes,
        });
    }

    // A GPU with no XGMI peers (e.g. consumer card) contributes no row.
    if links.is_empty() {
        return None;
    }

    Some(XgmiSnapshot {
        gpu_index: idx as u32,
        gpu_name,
        link_count: links.len() as u32,
        link_gbps: (total_gbps > 0.0).then_some(total_gbps),
        correctable_errors,
        uncorrectable_errors,
        metrics: Some(read_gpu_metrics(api, handle)).filter(|m| !m.is_empty()),
        links,
    })
}

/// Live GPU health; each field None when its call is missing or fails.
fn read_gpu_metrics(api: &Amdsmi, handle: ffi::ProcessorHandle) -> crate::gpu::GpuMetrics {
    let util_pct = api.get_gpu_activity.and_then(|f| {
        let mut u: Padded<ffi::EngineUsage> = Padded::zeroed();
        (unsafe { f(handle, &mut u.value) } == ffi::AMDSMI_STATUS_SUCCESS)
            .then_some(u.value.gfx_activity)
    });
    let vram = api.get_gpu_vram_usage.and_then(|f| {
        let mut v: Padded<ffi::VramUsage> = Padded::zeroed();
        (unsafe { f(handle, &mut v.value) } == ffi::AMDSMI_STATUS_SUCCESS)
            .then_some((v.value.vram_used as u64, v.value.vram_total as u64))
    });
    let temp_c = api.get_temp_metric.and_then(|f| {
        let mut t: i64 = 0;
        (unsafe {
            f(
                handle,
                ffi::AMDSMI_TEMPERATURE_TYPE_EDGE,
                ffi::AMDSMI_TEMP_CURRENT,
                &mut t,
            )
        } == ffi::AMDSMI_STATUS_SUCCESS)
            // amdsmi returns whole degrees C (the lib divides its millidegree
            // source by 1000; header doc is misleading).
            .then_some(t)
    });
    let power_w = api.get_power_info.and_then(|f| {
        let mut p: Padded<ffi::PowerInfo> = Padded::zeroed();
        if unsafe { f(handle, &mut p.value) } != ffi::AMDSMI_STATUS_SUCCESS {
            return None;
        }
        // current_socket_power is MI300+-only; average_socket_power covers older parts;
        // 0/0 = unsupported.
        let w = if p.value.current_socket_power > 0 {
            p.value.current_socket_power
        } else {
            p.value.average_socket_power
        };
        (w > 0).then_some(w as f64)
    });
    let clock_mhz = api.get_clock_info.and_then(|f| {
        let mut c: Padded<ffi::ClkInfo> = Padded::zeroed();
        (unsafe { f(handle, ffi::AMDSMI_CLK_TYPE_GFX, &mut c.value) } == ffi::AMDSMI_STATUS_SUCCESS)
            .then_some(c.value.clk)
    });
    crate::gpu::GpuMetrics {
        util_pct,
        vram_used_mb: vram.map(|(u, _)| u),
        vram_total_mb: vram.map(|(_, t)| t),
        temp_c,
        power_w,
        clock_mhz,
    }
}

/// Whether topology reports an XGMI io-link between the two GPUs.
/// `None` when the topo query itself failed (transient during GPU init),
/// so callers can retry instead of caching a false negative.
fn is_xgmi_peer(
    api: &Amdsmi,
    src: ffi::ProcessorHandle,
    dst: ffi::ProcessorHandle,
) -> Option<bool> {
    let mut hops: u64 = 0;
    let mut link_type: u32 = 0;
    let rc = unsafe { (api.topo_get_link_type)(src, dst, &mut hops, &mut link_type) };
    if rc != ffi::AMDSMI_STATUS_SUCCESS {
        return None;
    }
    Some(link_type == ffi::AMDSMI_IOLINK_TYPE_XGMI)
}

/// Per-link (bit_rate, bandwidth) in Gb/s from PCIe static info, mirroring
/// amd-smi: bit_rate = max_pcie_speed/1000, bandwidth = bit_rate * width.
fn read_link_speed(api: &Amdsmi, handle: ffi::ProcessorHandle) -> LinkSpeed {
    let mut info: Padded<ffi::PcieInfo> = Padded::zeroed();
    let rc = unsafe { (api.get_pcie_info)(handle, &mut info.value) };
    if rc != ffi::AMDSMI_STATUS_SUCCESS || info.value.max_pcie_speed == 0 {
        return (None, None);
    }
    let info = &info.value;
    // Runtime value is MT/s (e.g. 32000); tolerate GT/s (e.g. 32) as-is.
    let bit_rate = if info.max_pcie_speed >= 1000 {
        info.max_pcie_speed as f64 / 1000.0
    } else {
        info.max_pcie_speed as f64
    };
    let bandwidth = (info.max_pcie_width > 0).then_some(bit_rate * info.max_pcie_width as f64);
    (Some(bit_rate), bandwidth)
}

fn read_gpu_name(api: &Amdsmi, handle: ffi::ProcessorHandle) -> Option<String> {
    let mut info: Padded<ffi::AsicInfo> = Padded::zeroed();
    let rc = unsafe { (api.get_gpu_asic_info)(handle, &mut info.value) };
    if rc != ffi::AMDSMI_STATUS_SUCCESS {
        return None;
    }
    let info = &mut info.value;
    // Ensure NUL-termination even if amdsmi fills the buffer completely.
    info.market_name[ffi::AMDSMI_MAX_STRING_LENGTH - 1] = 0;
    let name = unsafe { CStr::from_ptr(info.market_name.as_ptr() as *const std::os::raw::c_char) };
    let name = name.to_string_lossy().trim().to_string();
    (!name.is_empty()).then_some(name)
}

fn read_wafl_errors(api: &Amdsmi, handle: ffi::ProcessorHandle) -> (Option<u64>, Option<u64>) {
    let mut ec: Padded<ffi::ErrorCount> = Padded::zeroed();
    let rc =
        unsafe { (api.get_gpu_ecc_count)(handle, ffi::AMDSMI_GPU_BLOCK_XGMI_WAFL, &mut ec.value) };
    if rc != ffi::AMDSMI_STATUS_SUCCESS {
        return (None, None);
    }
    (
        Some(ec.value.correctable_count),
        Some(ec.value.uncorrectable_count),
    )
}

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

    fn link(id: u32, active: bool) -> XgmiLinkSnapshot {
        XgmiLinkSnapshot {
            link_id: id,
            is_active: active,
            speed_gbps: None,
            bit_rate_gbps: None,
            remote_pci_bdf: None,
            tx_bytes: None,
            rx_bytes: None,
        }
    }

    #[test]
    fn active_links_counts_active_only() {
        let snap = XgmiSnapshot {
            gpu_index: 0,
            gpu_name: "test".to_string(),
            link_count: 3,
            link_gbps: None,
            correctable_errors: None,
            uncorrectable_errors: None,
            metrics: None,
            links: vec![link(0, true), link(1, false), link(2, true)],
        };
        assert_eq!(snap.active_links(), 2);
    }

    #[test]
    fn active_links_no_links() {
        let snap = XgmiSnapshot {
            gpu_index: 0,
            gpu_name: "test".to_string(),
            link_count: 0,
            link_gbps: None,
            correctable_errors: None,
            uncorrectable_errors: None,
            metrics: None,
            links: Vec::new(),
        };
        assert_eq!(snap.active_links(), 0);
    }

    #[test]
    fn format_bdf_matches_lspci_form() {
        // 0000:05:00.0 -> domain 0, bus 5, device 0, function 0
        assert_eq!(format_bdf(0x0000_0000_0000_0500), "0000:05:00.0");
        // 0000:f5:00.0 (amd1 GPU7)
        assert_eq!(format_bdf(0x0000_0000_0000_f500), "0000:f5:00.0");
        // function/device bits: bus 0x65, device 0x1f, function 7
        // bdf = domain<<16 | bus<<8 | device<<3 | function
        assert_eq!(format_bdf((0x65 << 8) | (0x1f << 3) | 0x7), "0000:65:1f.7");
    }

    /// Hardware smoke test: run with `cargo test -- --ignored`
    /// on a machine with AMD GPUs.
    #[test]
    #[ignore]
    fn smoke_read_all_xgmi_stats() {
        let snaps = read_all_xgmi_stats().expect("io error");
        assert!(!snaps.is_empty(), "expected XGMI-capable GPUs on this host");
        assert!(
            snaps.iter().any(|s| s.link_count == 7),
            "expected at least one GPU with 7 XGMI links on amd1"
        );
        for s in &snaps {
            assert!(s.link_count > 0);
            assert!(
                s.active_links() > 0,
                "gpu{} has no active links",
                s.gpu_index
            );
            for l in &s.links {
                let bdf = l.remote_pci_bdf.as_deref().unwrap_or("<none>");
                assert!(
                    l.remote_pci_bdf.is_some() && bdf != "0000:00:00.0",
                    "gpu{} link{} has null BDF",
                    s.gpu_index,
                    l.link_id
                );
                assert!(
                    l.speed_gbps.unwrap_or(0.0) > 0.0,
                    "gpu{} link{} has zero speed",
                    s.gpu_index,
                    l.link_id
                );
                // Guards the positional accumulator mapping: every peer on
                // this host must carry counters (catches off-by-one in `n`
                // and bdf-mismatch degradation).
                assert!(
                    l.tx_bytes.is_some() && l.rx_bytes.is_some(),
                    "gpu{} link{} has no accumulator data",
                    s.gpu_index,
                    l.link_id
                );
            }
        }
        // Print compact dump of gpu0's snapshot for eyeballing.
        if let Some(s) = snaps.first() {
            let fl = s.links.first();
            eprintln!(
                "gpu0 name={:?} link_gbps={:?} first_link={:?}",
                s.gpu_name, s.link_gbps, fl
            );
        }

        // Regression check: repeated polling must not leak fds (the TUI
        // calls this every refresh; amdsmi_get_gpu_asic_info leaks on
        // ROCm 6.4.x when called per refresh, so names are cached).
        let count_fds = || std::fs::read_dir("/proc/self/fd").unwrap().count();
        let before = count_fds();
        for _ in 0..100 {
            read_all_xgmi_stats().expect("io error");
        }
        let after = count_fds();
        assert!(
            after <= before + 2,
            "fd leak: {} -> {} after 100 polls",
            before,
            after
        );
    }
}