interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-cli / src / telemetry.rs

297 lines · 10132 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! GPU status read from kernel sysfs only. No NVIDIA userspace (nvidia-smi,
17//! NVML, libcuda) is executed or linked. Every read is a plain file read;
18//! nothing under /sys is written and the device power state is never touched.
19//!
20//! Field sources:
21//! - `available`: a PCI display-class device (class 0x03xxxx) with a bound
22//! kernel driver, from `/sys/bus/pci/devices/*/{class,driver}`
23//! (`aien_inference_abi::pci_gpu`).
24//! - `name`: `vendor`/`device` IDs of that device, mapped through the
25//! checked table in `pci_gpu`; unknown IDs are shown as `PCI vvvv:dddd`.
26//! - `temp_c`: the device's own hwmon (`<dev>/hwmon/hwmon*/temp1_input`) when
27//! the driver exports one, else the firmware ACPI GPU thermal zone
28//! (`/sys/class/thermal/thermal_zone*` whose `device/path` is `\_TZ_.TGPU`).
29//! On the DGX Spark the NVIDIA driver exports no hwmon, so the value is the
30//! ACPI zone, a different sensor from the one nvidia-smi reports.
31//! - `used_mb`, `total_mb`, `util_pct`: no kernel source exists without the
32//! NVIDIA resource manager, so they are always `None`. nvidia-smi already
33//! reported memory as `[N/A]` on the GB10 (unified memory).
34//!
35//! MIGRATION-PENDING: this Rust reader is a stop-gap. Its successor is a
36//! seeded-Omega (.om) sysfs reader of the same files with the same
37//! unavailable-field contract (PROPOSED, not yet written). Delete this module
38//! when that lands.
39
40use aien_inference_abi::pci_gpu::find_display_gpu;
41use std::fs;
42use std::path::{Path, PathBuf};
43
44const ACPI_GPU_THERMAL_ZONE: &str = "\\_TZ_.TGPU";
45
46#[derive(Debug, Clone, Default, PartialEq, Eq)]
47pub struct GpuTelemetry {
48 pub name: Option<String>,
49 pub temp_c: Option<u32>,
50 pub used_mb: Option<u64>,
51 pub total_mb: Option<u64>,
52 pub util_pct: Option<u32>,
53 pub available: bool,
54}
55
56impl GpuTelemetry {
57 pub fn display_name(&self) -> &str {
58 self.name
59 .as_deref()
60 .unwrap_or("Unavailable / Sensor Offline")
61 }
62
63 pub fn display_temp(&self) -> String {
64 self.temp_c
65 .map(|t| format!("{}°C", t))
66 .unwrap_or_else(|| "N/A".to_string())
67 }
68
69 pub fn display_vram(&self) -> String {
70 match (self.used_mb, self.total_mb) {
71 (Some(u), Some(t)) => format!("{} MB / {} MB", u, t),
72 _ => "N/A".to_string(),
73 }
74 }
75
76 pub fn display_util(&self) -> String {
77 self.util_pct
78 .map(|u| format!("{}%", u))
79 .unwrap_or_else(|| "N/A".to_string())
80 }
81}
82
83pub fn get_gpu_telemetry() -> GpuTelemetry {
84 read_gpu_telemetry(Path::new("/"))
85}
86
87/// Read telemetry from a filesystem rooted at `root` (`/` in production, a
88/// fixture tree in tests).
89fn read_gpu_telemetry(root: &Path) -> GpuTelemetry {
90 // Epistemic honesty: return explicit sensor unavailability, never fake synthetic data
91 let Some(gpu) = find_display_gpu(&root.join("sys/bus/pci/devices")) else {
92 return GpuTelemetry::default();
93 };
94 let temp_c = device_hwmon_temp(&gpu.path)
95 .or_else(|| acpi_gpu_zone_temp(&root.join("sys/class/thermal")))
96 .and_then(millicelsius_to_celsius);
97 GpuTelemetry {
98 name: Some(gpu.name()),
99 temp_c,
100 used_mb: None,
101 total_mb: None,
102 util_pct: None,
103 available: true,
104 }
105}
106
107fn device_hwmon_temp(dev: &Path) -> Option<i64> {
108 let mut hwmons: Vec<PathBuf> = fs::read_dir(dev.join("hwmon"))
109 .ok()?
110 .filter_map(|e| e.ok())
111 .map(|e| e.path())
112 .collect();
113 hwmons.sort();
114 hwmons.iter().find_map(|h| read_int(&h.join("temp1_input")))
115}
116
117fn acpi_gpu_zone_temp(thermal: &Path) -> Option<i64> {
118 let mut zones: Vec<PathBuf> = fs::read_dir(thermal)
119 .ok()?
120 .filter_map(|e| e.ok())
121 .map(|e| e.path())
122 .filter(|p| {
123 p.file_name()
124 .and_then(|n| n.to_str())
125 .is_some_and(|n| n.starts_with("thermal_zone"))
126 })
127 .collect();
128 zones.sort();
129 zones
130 .iter()
131 .find(|z| {
132 fs::read_to_string(z.join("device/path"))
133 .is_ok_and(|s| s.trim() == ACPI_GPU_THERMAL_ZONE)
134 })
135 .and_then(|z| read_int(&z.join("temp")))
136}
137
138/// sysfs temperatures are millidegrees Celsius; round to the nearest degree.
139/// A negative or out-of-range reading cannot be shown in `u32` and is
140/// reported as unavailable.
141fn millicelsius_to_celsius(m: i64) -> Option<u32> {
142 if m < 0 {
143 return None;
144 }
145 u32::try_from((m + 500) / 1000).ok()
146}
147
148fn read_int(p: &Path) -> Option<i64> {
149 fs::read_to_string(p).ok()?.trim().parse().ok()
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 fn put(root: &Path, rel: &str, body: &str) {
157 let p = root.join(rel);
158 fs::create_dir_all(p.parent().unwrap()).unwrap();
159 fs::write(p, body).unwrap();
160 }
161
162 /// The DGX Spark layout observed read-only on 2026-10-27: GB10 at
163 /// 000f:01:00.0 bound to `nvidia`, no device hwmon, seven ACPI zones with
164 /// `\_TZ_.TGPU` at zone 5.
165 fn spark_tree() -> tempfile::TempDir {
166 let t = tempfile::tempdir().unwrap();
167 let r = t.path();
168 let gpu = "sys/bus/pci/devices/000f:01:00.0";
169 put(r, &format!("{gpu}/class"), "0x030000\n");
170 put(r, &format!("{gpu}/vendor"), "0x10de\n");
171 put(r, &format!("{gpu}/device"), "0x2e12\n");
172 put(r, &format!("{gpu}/driver/bind"), "");
173 let nic = "sys/bus/pci/devices/0000:01:00.0";
174 put(r, &format!("{nic}/class"), "0x020000\n");
175 put(r, &format!("{nic}/vendor"), "0x15b3\n");
176 put(r, &format!("{nic}/device"), "0x1021\n");
177 put(r, &format!("{nic}/driver/bind"), "");
178 let zones = [
179 ("TSOC", 51800),
180 ("TS0E", 43200),
181 ("TS0P", 40900),
182 ("TS1E", 49400),
183 ("TS1P", 51800),
184 ("TGPU", 41700),
185 ("TUNC", 42700),
186 ];
187 for (i, (name, mc)) in zones.iter().enumerate() {
188 let z = format!("sys/class/thermal/thermal_zone{i}");
189 put(r, &format!("{z}/temp"), &format!("{mc}\n"));
190 put(r, &format!("{z}/device/path"), &format!("\\_TZ_.{name}\n"));
191 }
192 t
193 }
194
195 #[test]
196 fn spark_layout_reads_gb10_and_acpi_gpu_zone() {
197 let t = spark_tree();
198 let g = read_gpu_telemetry(t.path());
199 assert_eq!(
200 g,
201 GpuTelemetry {
202 name: Some("NVIDIA GB10".into()),
203 temp_c: Some(42),
204 used_mb: None,
205 total_mb: None,
206 util_pct: None,
207 available: true,
208 }
209 );
210 assert_eq!(g.display_vram(), "N/A");
211 assert_eq!(g.display_util(), "N/A");
212 }
213
214 #[test]
215 fn device_hwmon_wins_over_acpi_zone() {
216 let t = spark_tree();
217 put(
218 t.path(),
219 "sys/bus/pci/devices/000f:01:00.0/hwmon/hwmon9/temp1_input",
220 "38499\n",
221 );
222 assert_eq!(read_gpu_telemetry(t.path()).temp_c, Some(38));
223 }
224
225 #[test]
226 fn no_gpu_zone_means_no_temperature() {
227 let t = spark_tree();
228 put(
229 t.path(),
230 "sys/class/thermal/thermal_zone5/device/path",
231 "\\_TZ_.TXXX\n",
232 );
233 let g = read_gpu_telemetry(t.path());
234 assert!(g.available);
235 assert_eq!(g.temp_c, None);
236 assert_eq!(g.display_temp(), "N/A");
237 }
238
239 #[test]
240 fn unbound_gpu_is_unavailable() {
241 let t = spark_tree();
242 fs::remove_dir_all(t.path().join("sys/bus/pci/devices/000f:01:00.0/driver")).unwrap();
243 assert_eq!(read_gpu_telemetry(t.path()), GpuTelemetry::default());
244 }
245
246 #[test]
247 fn empty_root_is_unavailable() {
248 let t = tempfile::tempdir().unwrap();
249 let g = read_gpu_telemetry(t.path());
250 assert_eq!(g, GpuTelemetry::default());
251 assert_eq!(g.display_name(), "Unavailable / Sensor Offline");
252 }
253
254 #[test]
255 fn unknown_device_id_is_named_by_pci_ids_not_guessed() {
256 let t = spark_tree();
257 put(
258 t.path(),
259 "sys/bus/pci/devices/000f:01:00.0/device",
260 "0x2b85\n",
261 );
262 assert_eq!(
263 read_gpu_telemetry(t.path()).name.as_deref(),
264 Some("PCI 10de:2b85")
265 );
266 }
267
268 #[test]
269 fn nvidia_preferred_over_lower_addressed_display_device() {
270 let t = spark_tree();
271 let bmc = "sys/bus/pci/devices/0000:00:01.0";
272 put(t.path(), &format!("{bmc}/class"), "0x030000\n");
273 put(t.path(), &format!("{bmc}/vendor"), "0x1a03\n");
274 put(t.path(), &format!("{bmc}/device"), "0x2000\n");
275 put(t.path(), &format!("{bmc}/driver/bind"), "");
276 assert_eq!(
277 read_gpu_telemetry(t.path()).name.as_deref(),
278 Some("NVIDIA GB10")
279 );
280 }
281
282 #[test]
283 fn millicelsius_rounding_and_bounds() {
284 assert_eq!(millicelsius_to_celsius(0), Some(0));
285 assert_eq!(millicelsius_to_celsius(41499), Some(41));
286 assert_eq!(millicelsius_to_celsius(41500), Some(42));
287 assert_eq!(millicelsius_to_celsius(-1), None);
288 }
289
290 #[test]
291 fn module_never_spawns_a_process() {
292 // Pieces are joined at run time so this test's own text cannot match.
293 let src = include_str!("telemetry.rs");
294 assert!(!src.contains(&["Command", "::new("].concat()));
295 assert!(!src.contains(&["std::", "process"].concat()));
296 }
297}