Skip to main content

agentmux_srv_core/backend/gpu/
names.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The names Windows gives the instances of its GPU performance counters, parsed. They carry the
5//! process, the adapter and the engine, which is all the per-process and per-adapter figures are made from
6//! (SPEC_STATUS_BAR_GPU_PANEL_2026_11_15.md ยง3):
7//!
8//! ```text
9//! \GPU Engine(pid_17408_luid_0x00000000_0x0000F3A8_phys_0_eng_0_engtype_3D)\Utilization Percentage
10//! \GPU Adapter Memory(luid_0x00000000_0x0000F3A8_phys_0)\Dedicated Usage
11//! \GPU Process Memory(pid_17408_luid_0x00000000_0x0000F3A8_phys_0)\Dedicated Usage
12//! ```
13//!
14//! Pure text, so the tests run on every platform.
15
16/// One engine of one adapter, as one process uses it.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct EngineInstance {
19    pub pid: u32,
20    /// The adapter ([`Luid`]).
21    pub adapter: String,
22    /// The engine on that adapter, `<index>:<type>` (`0:3d`): one engine is what a figure of "how busy" is of.
23    pub engine: String,
24}
25
26/// An adapter's locally unique id in the one spelling both the counters and DXGI are put in, `0x00000000_0x0000F3A8`.
27pub fn luid(high: i32, low: u32) -> String {
28    format!("0x{:08X}_0x{:08X}", high as u32, low)
29}
30
31/// `0x0000f3a8` as a number; the counters write the parts in hex with a prefix.
32fn hex(token: &str) -> Option<u32> {
33    u32::from_str_radix(token.strip_prefix("0x")?, 16).ok()
34}
35
36/// The tokens of an instance name, lower case: a name is words and numbers joined by underscores.
37fn tokens(name: &str) -> Vec<String> {
38    name.to_ascii_lowercase().split('_').map(str::to_string).collect()
39}
40
41/// What follows `key` in the tokens, as a number.
42fn number_after(tokens: &[String], key: &str) -> Option<u32> {
43    let at = tokens.iter().position(|t| t == key)?;
44    tokens.get(at + 1)?.parse().ok()
45}
46
47/// The adapter named after `luid` in the tokens.
48fn adapter_in(tokens: &[String]) -> Option<String> {
49    let at = tokens.iter().position(|t| t == "luid")?;
50    let high = hex(tokens.get(at + 1)?)?;
51    let low = hex(tokens.get(at + 2)?)?;
52    Some(luid(high as i32, low))
53}
54
55/// `pid_<n>_luid_<high>_<low>_phys_<k>_eng_<i>_engtype_<type>`: a `GPU Engine` instance.
56pub fn parse_engine(name: &str) -> Option<EngineInstance> {
57    let t = tokens(name);
58    let pid = number_after(&t, "pid")?;
59    let adapter = adapter_in(&t)?;
60    let index = number_after(&t, "eng")?;
61    let at = t.iter().position(|x| x == "engtype")?;
62    // A type is a word ("3d", "copy", "videodecode"), but join what follows in case one ever has an underscore.
63    let kind = t.get(at + 1..)?.join("_");
64    if kind.is_empty() {
65        return None;
66    }
67    Some(EngineInstance { pid, adapter, engine: format!("{index}:{kind}") })
68}
69
70/// `luid_<high>_<low>_phys_<k>`: a `GPU Adapter Memory` instance.
71pub fn parse_adapter(name: &str) -> Option<String> {
72    adapter_in(&tokens(name))
73}
74
75/// `pid_<n>_luid_<high>_<low>_phys_<k>`: a `GPU Process Memory` instance, the process and the adapter its memory is on.
76pub fn parse_process_memory(name: &str) -> Option<(u32, String)> {
77    let t = tokens(name);
78    Some((number_after(&t, "pid")?, adapter_in(&t)?))
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    const ADAPTER: &str = "0x00000000_0x0000F3A8";
86
87    #[test]
88    fn an_engine_instance_names_process_adapter_and_engine() {
89        let e = parse_engine("pid_17408_luid_0x00000000_0x0000F3A8_phys_0_eng_3_engtype_3D").unwrap();
90        assert_eq!(e, EngineInstance { pid: 17408, adapter: ADAPTER.into(), engine: "3:3d".into() });
91    }
92
93    #[test]
94    fn the_case_and_the_engine_type_spelling_do_not_matter() {
95        let e = parse_engine("PID_4_LUID_0X00000000_0X0000f3a8_PHYS_0_ENG_12_ENGTYPE_VideoDecode").unwrap();
96        assert_eq!((e.pid, e.adapter.as_str(), e.engine.as_str()), (4, ADAPTER, "12:videodecode"));
97    }
98
99    #[test]
100    fn a_high_part_that_is_negative_in_dxgi_matches_its_hex_form() {
101        assert_eq!(luid(-1, 0x1A), "0xFFFFFFFF_0x0000001A");
102        let e = parse_engine("pid_1_luid_0xFFFFFFFF_0x0000001A_phys_0_eng_0_engtype_Copy").unwrap();
103        assert_eq!(e.adapter, luid(-1, 0x1A));
104    }
105
106    #[test]
107    fn an_adapter_memory_instance_names_the_adapter() {
108        assert_eq!(parse_adapter("luid_0x00000000_0x0000F3A8_phys_0").as_deref(), Some(ADAPTER));
109    }
110
111    #[test]
112    fn a_process_memory_instance_names_process_and_adapter() {
113        assert_eq!(
114            parse_process_memory("pid_980_luid_0x00000000_0x0000F3A8_phys_0"),
115            Some((980, ADAPTER.to_string()))
116        );
117    }
118
119    #[test]
120    fn a_name_that_is_not_one_of_these_is_not_parsed() {
121        assert_eq!(parse_engine("_Total"), None);
122        assert_eq!(parse_engine("pid_x_luid_0x0_0x1_phys_0_eng_0_engtype_3D"), None);
123        assert_eq!(parse_engine("pid_1_luid_0x00000000_0x0000F3A8_phys_0_eng_0"), None);
124        assert_eq!(parse_adapter("pid_1"), None);
125        assert_eq!(parse_process_memory("luid_0x00000000_0x0000F3A8_phys_0"), None);
126    }
127}