all-smi 0.26.2

Command-line utility for monitoring GPU hardware. It provides a real-time view of GPU utilization, memory usage, temperature, power consumption, and other metrics.
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
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;
use crate::device::types::{GpuInfo, MAX_GPU_FAN_RPM};
use std::collections::HashMap;

fn make_baseline_gpu_info() -> GpuInfo {
    GpuInfo {
        uuid: "Intel-GPU-0000:03:00.0".to_string(),
        time: "2026-01-01 00:00:00".to_string(),
        name: "Intel Arc B580".to_string(),
        device_type: "GPU".to_string(),
        host_id: "test-host".to_string(),
        hostname: "test-host".to_string(),
        instance: "test-host".to_string(),
        utilization: 0.0,
        ane_utilization: 0.0,
        dla_utilization: None,
        tensorcore_utilization: None,
        temperature: 0,
        used_memory: 0,
        total_memory: 12 * 1024 * 1024 * 1024,
        frequency: 0,
        power_consumption: 0.0,
        gpu_core_count: None,
        temperature_threshold_slowdown: None,
        temperature_threshold_shutdown: None,
        temperature_threshold_max_operating: None,
        temperature_threshold_acoustic: None,
        performance_state: None,
        fan_speed_rpm: None,
        numa_node_id: None,
        gsp_firmware_mode: None,
        gsp_firmware_version: None,
        nvlink_remote_devices: Vec::new(),
        gpm_metrics: None,
        detail: HashMap::new(),
    }
}

#[test]
fn linux_fresh_sysman_overwrites_fields() {
    let mut gpu = make_baseline_gpu_info();
    gpu.utilization = 42.0;
    gpu.temperature = 60;
    gpu.frequency = 1900;
    gpu.power_consumption = 80.0;
    gpu.detail.insert(
        "Metrics Source".to_string(),
        "sysfs (engine counters)".to_string(),
    );
    // Mirror what the Linux sysfs baseline actually produces: the typed
    // field and the detail string are written together from one hwmon read.
    gpu.fan_speed_rpm = Some(1400);
    gpu.detail
        .insert("Fan Speed".to_string(), "1400 RPM".to_string());

    let readout = LevelZeroReadout {
        engines: vec![("compute (XMX)", 80.0), ("render", 30.0)],
        primary_engine_utilization: Some(FreshValue::level_zero(80.0)),
        power_watts: Some(FreshValue::level_zero(120.5)),
        temperature_celsius: Some(FreshValue::level_zero(72)),
        memory: Some(LevelZeroMemoryReadout {
            used_bytes: 4 * 1024 * 1024 * 1024,
            total_bytes: 12 * 1024 * 1024 * 1024,
            kind: LevelZeroMemoryKind::DedicatedLocal,
            source: "Level Zero Sysman",
        }),
        frequency_mhz: Some(FreshValue::level_zero(2300)),
        fan: Some(LevelZeroFanReadout {
            rpm: Some(1800),
            percent: None,
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Linux);

    assert_eq!(gpu.utilization, 80.0);
    assert_eq!(gpu.temperature, 72);
    assert_eq!(gpu.frequency, 2300);
    assert_eq!(gpu.used_memory, 4 * 1024 * 1024 * 1024);
    assert_eq!(gpu.total_memory, 12 * 1024 * 1024 * 1024);
    assert_eq!(
        gpu.detail.get("Source: Memory Used").map(String::as_str),
        Some("Level Zero Sysman")
    );
    assert_eq!(
        gpu.detail.get("Power (L0)").map(String::as_str),
        Some("120.50 W")
    );
    // The sysfs qualifier survives. Level Zero used to assign this string
    // rather than append to it, so "(engine counters)" was lost the moment
    // Sysman produced a reading.
    assert_eq!(
        gpu.detail.get("Metrics Source").map(String::as_str),
        Some("sysfs (engine counters) + Level Zero Sysman")
    );
    assert_eq!(
        gpu.detail.get("Source: Utilization").map(String::as_str),
        Some("Level Zero Sysman")
    );
    assert_eq!(
        gpu.detail.get("Fan Speed").map(String::as_str),
        Some("1400 RPM"),
        "Linux hwmon fan must keep priority over L0 fan"
    );
    assert_eq!(
        gpu.fan_speed_rpm,
        Some(1400),
        "the typed field must follow the same priority as the detail string"
    );
}

#[test]
fn missing_sysman_fields_keep_linux_baseline() {
    let mut gpu = make_baseline_gpu_info();
    gpu.utilization = 42.0;
    gpu.temperature = 68;
    gpu.frequency = 1950;
    gpu.power_consumption = 150.0;
    let readout = LevelZeroReadout {
        engines: vec![("copy", 90.0)],
        power_watts: Some(FreshValue::level_zero(95.0)),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Linux);

    assert_eq!(gpu.utilization, 42.0, "no fresh L0 primary engine");
    assert_eq!(gpu.temperature, 68, "no L0 temperature");
    assert_eq!(gpu.frequency, 1950, "no L0 frequency");
    assert_eq!(gpu.power_consumption, 95.0, "fresh L0 power wins");
}

#[test]
fn shared_memory_does_not_fabricate_vram_budget() {
    let mut gpu = make_baseline_gpu_info();
    gpu.used_memory = 0;
    gpu.total_memory = 0;
    let readout = LevelZeroReadout {
        memory: Some(LevelZeroMemoryReadout {
            used_bytes: 0,
            total_bytes: 0,
            kind: LevelZeroMemoryKind::SharedSystem,
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Linux);

    assert_eq!(gpu.used_memory, 0);
    assert_eq!(gpu.total_memory, 0);
    assert_eq!(
        gpu.detail.get("Memory (L0)").map(String::as_str),
        Some("Shared/system memory; dedicated VRAM budget unavailable")
    );
}

#[test]
fn windows_overwrites_wmi_gaps() {
    let mut gpu = make_baseline_gpu_info();
    gpu.detail
        .insert("Metrics Source".to_string(), "WMI".to_string());
    let readout = LevelZeroReadout {
        engines: vec![("compute (XMX)", 65.0), ("render", 20.0)],
        primary_engine_utilization: Some(FreshValue::level_zero(65.0)),
        power_watts: Some(FreshValue::level_zero(95.0)),
        temperature_celsius: Some(FreshValue::level_zero(71)),
        frequency_mhz: Some(FreshValue::level_zero(2200)),
        fan: Some(LevelZeroFanReadout {
            rpm: Some(1600),
            percent: Some(40),
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);

    assert!((gpu.utilization - 65.0).abs() < 1e-9);
    assert!((gpu.power_consumption - 95.0).abs() < 1e-9);
    assert_eq!(gpu.temperature, 71);
    assert_eq!(gpu.frequency, 2200);
    assert_eq!(
        gpu.detail.get("Fan Speed").map(String::as_str),
        Some("1600 RPM (40%)")
    );
    // The duty cycle only ever rides in the detail string; the typed field
    // carries the tachometer reading on its own.
    assert_eq!(gpu.fan_speed_rpm, Some(1600));
    assert_eq!(
        gpu.detail.get("Metrics Source").map(String::as_str),
        Some("WMI + Level Zero Sysman")
    );
}

#[test]
fn duty_cycle_only_fan_leaves_the_typed_field_unset() {
    // Some drivers report a fan percentage with no tachometer. A
    // percentage stored in a field named `_rpm` would be exported as a
    // wildly wrong RPM, so the field stays `None` while the percentage
    // still reaches snapshots through the detail string.
    let mut gpu = make_baseline_gpu_info();
    let readout = LevelZeroReadout {
        fan: Some(LevelZeroFanReadout {
            rpm: None,
            percent: Some(40),
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);

    assert_eq!(gpu.detail.get("Fan Speed").map(String::as_str), Some("40%"));
    assert!(gpu.fan_speed_rpm.is_none());
}

#[test]
fn windows_duty_cycle_only_fan_clears_a_stale_tachometer_reading() {
    // `overwrite_existing` replaces the detail string unconditionally, so
    // the typed field has to follow it. Keeping an RPM from an earlier
    // sample would leave the exporter publishing a number the detail
    // string no longer agrees with.
    let mut gpu = make_baseline_gpu_info();
    gpu.fan_speed_rpm = Some(1450);
    gpu.detail
        .insert("Fan Speed".to_string(), "1450 RPM".to_string());
    let readout = LevelZeroReadout {
        fan: Some(LevelZeroFanReadout {
            rpm: None,
            percent: Some(40),
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);

    assert_eq!(gpu.detail.get("Fan Speed").map(String::as_str), Some("40%"));
    assert!(gpu.fan_speed_rpm.is_none());
}

#[test]
fn linux_l0_fan_fills_a_gap_the_hwmon_baseline_left() {
    // No hwmon tachometer means no `Fan Speed` detail key, so the
    // overwrite guard does not fire and Level Zero supplies both
    // representations.
    let mut gpu = make_baseline_gpu_info();
    assert!(gpu.fan_speed_rpm.is_none());
    let readout = LevelZeroReadout {
        fan: Some(LevelZeroFanReadout {
            rpm: Some(1800),
            percent: None,
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Linux);

    assert_eq!(gpu.fan_speed_rpm, Some(1800));
    assert_eq!(
        gpu.detail.get("Fan Speed").map(String::as_str),
        Some("1800 RPM")
    );
}

#[test]
fn a_garbled_l0_fan_reading_is_clamped_before_either_write() {
    // A corrupted Sysman sample must never reach `GpuInfo::fan_speed_rpm`
    // or the `Fan Speed` detail string unclamped, and the two must keep
    // agreeing with each other after the clamp the same way they do for a
    // normal reading.
    let mut gpu = make_baseline_gpu_info();
    let readout = LevelZeroReadout {
        fan: Some(LevelZeroFanReadout {
            rpm: Some(u32::MAX),
            percent: None,
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Linux);

    assert_eq!(gpu.fan_speed_rpm, Some(MAX_GPU_FAN_RPM));
    assert_eq!(
        gpu.detail.get("Fan Speed").map(String::as_str),
        Some(format!("{MAX_GPU_FAN_RPM} RPM").as_str())
    );
}

#[test]
fn no_data_keeps_baseline() {
    let mut gpu = make_baseline_gpu_info();
    gpu.utilization = 42.0;
    gpu.detail
        .insert("Metrics Source".to_string(), "WMI".to_string());

    apply_to_gpu_info(
        &mut gpu,
        &LevelZeroReadout::default(),
        ApplyPlatform::Windows,
    );

    assert_eq!(gpu.utilization, 42.0);
    assert_eq!(
        gpu.detail.get("Metrics Source").map(String::as_str),
        Some("WMI")
    );
    assert!(!gpu.detail.contains_key("Power (L0)"));
}

// ---------------------------------------------------------------------
// Integrated Windows parts: the DXGI handoff (issue #364)
// ---------------------------------------------------------------------

/// Shape of an Intel iGPU after the vendor-neutral Windows layer has run:
/// DXGI resolved the capacity to the shared aperture and said so, and PDH
/// contributed a utilization figure.
fn integrated_after_dxgi() -> GpuInfo {
    let mut gpu = make_baseline_gpu_info();
    gpu.name = "Intel(R) Arc(TM) B390 GPU".to_string();
    gpu.used_memory = 6 * 1024 * 1024 * 1024;
    gpu.total_memory = 19_202_415_943;
    gpu.detail
        .insert("Metrics Source".to_string(), "WMI".to_string());
    gpu.detail
        .insert("Source: Memory".to_string(), "DXGI (shared)".to_string());
    gpu.detail.insert(
        "Source: Memory Used".to_string(),
        "PDH (shared)".to_string(),
    );
    crate::device::readers::detail_keys::note_metrics_source(&mut gpu.detail, "DXGI");
    crate::device::readers::detail_keys::note_metrics_source(&mut gpu.detail, "PDH");
    gpu
}

/// A 128 MiB Sysman readout on an integrated part is the stolen-memory
/// carve-out, not the capacity. Letting it through is how the B390 was
/// reported with 128 MiB of VRAM against a real 17.88 GiB aperture.
#[test]
fn a_dedicated_carve_out_never_replaces_a_shared_aperture() {
    let mut gpu = integrated_after_dxgi();
    let readout = LevelZeroReadout {
        memory: Some(LevelZeroMemoryReadout {
            used_bytes: 64 * 1024 * 1024,
            total_bytes: 128 * 1024 * 1024,
            kind: LevelZeroMemoryKind::DedicatedLocal,
            source: "Level Zero Sysman",
        }),
        temperature_celsius: Some(FreshValue::level_zero(48)),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);

    assert_eq!(gpu.total_memory, 19_202_415_943);
    assert_eq!(gpu.used_memory, 6 * 1024 * 1024 * 1024);
    assert_eq!(
        gpu.detail.get("Source: Memory").map(String::as_str),
        Some("DXGI (shared)")
    );
    assert_eq!(
        gpu.detail.get("Source: Memory Used").map(String::as_str),
        Some("PDH (shared)")
    );
    // Not discarded: the carve-out is real, it is just not the capacity.
    assert_eq!(
        gpu.detail.get("VRAM Dedicated (L0)").map(String::as_str),
        Some("134217728 bytes")
    );
    assert!(!gpu.detail.contains_key("VRAM Total"));
    // The rest of the readout still lands.
    assert_eq!(gpu.temperature, 48);
}

/// The guard must not cost discrete cards their Sysman VRAM figure, which
/// is the only 64-bit-correct source when DXGI is unavailable.
#[test]
fn a_discrete_card_still_takes_its_sysman_total() {
    let mut gpu = make_baseline_gpu_info();
    gpu.detail
        .insert("Source: Memory".to_string(), "WMI".to_string());
    gpu.detail
        .insert("Source: Memory Used".to_string(), "PDH".to_string());
    let readout = LevelZeroReadout {
        memory: Some(LevelZeroMemoryReadout {
            used_bytes: 2 * 1024 * 1024 * 1024,
            total_bytes: 12 * 1024 * 1024 * 1024,
            kind: LevelZeroMemoryKind::DedicatedLocal,
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);

    assert_eq!(gpu.total_memory, 12 * 1024 * 1024 * 1024);
    assert_eq!(gpu.used_memory, 2 * 1024 * 1024 * 1024);
    assert_eq!(
        gpu.detail.get("Source: Memory").map(String::as_str),
        Some("Level Zero Sysman")
    );
    assert_eq!(
        gpu.detail.get("Source: Memory Used").map(String::as_str),
        Some("Level Zero Sysman")
    );
}

/// Linux has no DXGI layer, so the marker cannot appear there and the
/// sysfs-derived total must keep yielding to Sysman as it always has.
#[test]
fn the_shared_aperture_guard_is_windows_only() {
    let mut gpu = make_baseline_gpu_info();
    gpu.detail
        .insert("Source: Memory".to_string(), "DXGI (shared)".to_string());
    let readout = LevelZeroReadout {
        memory: Some(LevelZeroMemoryReadout {
            used_bytes: 1024,
            total_bytes: 8 * 1024 * 1024 * 1024,
            kind: LevelZeroMemoryKind::DedicatedLocal,
            source: "Level Zero Sysman",
        }),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Linux);

    assert_eq!(gpu.total_memory, 8 * 1024 * 1024 * 1024);
}

/// A driver that reports a dedicated pool of zero must not zero a capacity
/// another layer already established.
#[test]
fn an_empty_dedicated_readout_leaves_the_total_alone() {
    let mut gpu = make_baseline_gpu_info();
    let readout = LevelZeroReadout {
        memory: Some(LevelZeroMemoryReadout {
            used_bytes: 0,
            total_bytes: 0,
            kind: LevelZeroMemoryKind::DedicatedLocal,
            source: "Level Zero Sysman",
        }),
        temperature_celsius: Some(FreshValue::level_zero(40)),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);

    assert_eq!(gpu.total_memory, 12 * 1024 * 1024 * 1024);
    assert!(!gpu.detail.contains_key("VRAM Dedicated (L0)"));
}

/// Every layer that ran must still be named. Assigning here is what made a
/// fully-instrumented host report only "WMI + Level Zero Sysman".
#[test]
fn the_full_windows_stack_is_recorded_in_order() {
    let mut gpu = integrated_after_dxgi();
    let readout = LevelZeroReadout {
        temperature_celsius: Some(FreshValue::level_zero(48)),
        ..Default::default()
    };
    apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);

    assert_eq!(
        gpu.detail.get("Metrics Source").map(String::as_str),
        Some("WMI + DXGI + PDH + Level Zero Sysman")
    );
}

/// Polling repeatedly must not grow the string.
#[test]
fn repeated_polls_do_not_grow_the_metrics_source() {
    let mut gpu = integrated_after_dxgi();
    let readout = LevelZeroReadout {
        temperature_celsius: Some(FreshValue::level_zero(48)),
        ..Default::default()
    };
    for _ in 0..5 {
        apply_to_gpu_info(&mut gpu, &readout, ApplyPlatform::Windows);
    }
    assert_eq!(
        gpu.detail.get("Metrics Source").map(String::as_str),
        Some("WMI + DXGI + PDH + Level Zero Sysman")
    );
}