1use rigspark_core::{
2 catalog::require,
3 sizing::{CpuArch, Gpu, GpuVendor, Hardware, Platform, ValidationError},
4};
5use serde::Deserialize;
6use serde_json::{Value, json};
7use std::{process::Stdio, time::Duration};
8use tokio::{io::AsyncReadExt, process::Command};
9
10#[derive(Debug, Deserialize)]
11#[serde(rename_all = "camelCase")]
12struct Controller {
13 vendor: String,
14 vram: Option<f64>,
15 vram_dynamic: Option<bool>,
16 #[serde(default)]
17 shared_memory: Option<bool>,
18}
19#[derive(Debug, Deserialize)]
20struct Disk {
21 mount: String,
22 available: f64,
23}
24#[derive(Debug, Deserialize)]
25struct Snapshot {
26 arch: String,
27 platform: String,
28 total: f64,
29 available: Option<f64>,
30 free: f64,
31 #[serde(default)]
32 controllers: Vec<Controller>,
33 #[serde(default)]
34 disks: Vec<Disk>,
35 #[serde(default, rename = "unifiedMemory")]
36 unified_memory: Option<bool>,
37}
38
39fn vendor(value: &str) -> GpuVendor {
40 let value = value.to_lowercase();
41 let words: Vec<&str> = value.split(|ch: char| !ch.is_alphanumeric()).collect();
42 if value.contains("nvidia") {
43 GpuVendor::Nvidia
44 } else if value.contains("apple") {
45 GpuVendor::Apple
46 } else if value.contains("amd")
47 || value.contains("advanced micro")
48 || value.contains("radeon")
49 || words.contains(&"ati")
50 {
51 GpuVendor::Amd
52 } else if value.contains("intel") || words.contains(&"arc") {
53 GpuVendor::Intel
54 } else {
55 GpuVendor::None
56 }
57}
58pub fn validate_hardware(hardware: &Hardware) -> Result<(), ValidationError> {
59 let safe = |value: f64, min: f64| {
60 value.is_finite() && value >= min && value <= 9007199254740991.0 && value.fract() == 0.0
61 };
62 require(
63 safe(hardware.total_ram_bytes, 1.0)
64 && safe(hardware.free_ram_bytes, 1.0)
65 && safe(hardware.free_disk_bytes, 0.0)
66 && hardware.gpu.iter().all(|gpu| safe(gpu.vram_bytes, 0.0)),
67 "invalid hardware measurements",
68 )
69}
70pub fn map_snapshot(value: &Value) -> Result<Hardware, ValidationError> {
71 let raw: Snapshot = serde_json::from_value(value.clone())
72 .map_err(|error| ValidationError(error.to_string()))?;
73 let arch = match raw.arch.to_lowercase().as_str() {
74 "x64" | "x86_64" | "amd64" => CpuArch::X64,
75 "arm64" | "aarch64" => CpuArch::Arm64,
76 _ => return Err(ValidationError("unsupported CPU architecture".into())),
77 };
78 let platform = match raw.platform.as_str() {
79 "darwin" => Platform::Darwin,
80 "linux" => Platform::Linux,
81 "win32" => Platform::Win32,
82 _ => return Err(ValidationError("unsupported platform".into())),
83 };
84 let disks: Vec<_> = raw
85 .disks
86 .iter()
87 .filter(|disk| disk.available.is_finite() && disk.available >= 0.0)
88 .collect();
89 let root = disks.iter().find(|disk| {
90 disk.mount == "/"
91 || (disk.mount.len() >= 2
92 && disk.mount.len() <= 3
93 && disk.mount.as_bytes()[0].is_ascii_alphabetic()
94 && disk.mount.as_bytes()[1] == b':'
95 && (disk.mount.len() == 2 || disk.mount.ends_with('\\')))
96 });
97 let free_disk = root.map_or_else(
98 || {
99 disks
100 .iter()
101 .map(|disk| disk.available.round())
102 .fold(0.0_f64, f64::max)
103 },
104 |disk| disk.available.round(),
105 );
106 let gpu = raw
107 .controllers
108 .iter()
109 .map(|controller| {
110 let vendor = vendor(&controller.vendor);
111 let mib = controller
112 .vram
113 .filter(|value| value.is_finite() && *value > 0.0)
114 .unwrap_or(0.0);
115 let bytes = if vendor == GpuVendor::None
116 || (vendor == GpuVendor::Intel && controller.vram_dynamic != Some(false))
117 {
118 0.0
119 } else {
120 (mib * 1048576.0).round()
121 };
122 Gpu {
123 vendor,
124 vram_bytes: bytes,
125 }
126 })
127 .collect();
128 let shared_soc = arch == CpuArch::Arm64
130 && raw.controllers.iter().any(|controller| {
131 controller.shared_memory == Some(true)
132 && vendor(&controller.vendor) == GpuVendor::Nvidia
133 });
134 let hardware = Hardware {
135 arch,
136 platform,
137 total_ram_bytes: raw.total.round(),
138 free_ram_bytes: raw
139 .available
140 .filter(|value| value.is_finite() && *value > 0.0)
141 .unwrap_or(raw.free)
142 .round(),
143 gpu,
144 free_disk_bytes: free_disk,
145 unified_memory: (raw.unified_memory == Some(true) || shared_soc).then_some(true),
146 };
147 validate_hardware(&hardware)?;
148 Ok(hardware)
149}
150
151pub fn detect_with(
152 probe: impl FnOnce() -> Result<Value, String>,
153 fallback: &Hardware,
154) -> Result<(Hardware, Vec<String>), ValidationError> {
155 match probe()
156 .map_err(ValidationError)
157 .and_then(|value| map_snapshot(&value))
158 {
159 Ok(hardware) => Ok((hardware, vec![])),
160 Err(error) => {
161 let mut hardware = fallback.clone();
162 hardware.gpu.clear();
163 hardware.free_disk_bytes = 1099511627776.0;
164 validate_hardware(&hardware)?;
165 Ok((
166 hardware,
167 vec![format!(
168 "hardware detection fallback: {error}; disk availability uses the legacy sentinel, not a measurement"
169 )],
170 ))
171 }
172 }
173}
174
175pub fn parse_gpu_output(platform: &str, raw: &str) -> Result<Vec<Value>, ValidationError> {
176 if platform == "linux" {
177 return raw
178 .lines()
179 .filter(|line| !line.trim().is_empty())
180 .map(|line| {
181 let (name, memory) = line
182 .rsplit_once(',')
183 .ok_or_else(|| ValidationError("invalid NVIDIA memory reading".into()))?;
184 if ["[N/A]", "N/A", "[Not Supported]"].contains(&memory.trim()) {
185 return Ok(json!({"vendor":name.trim(),"vram":null,"vramDynamic":true,"sharedMemory":true}));
186 }
187 let mib = memory
188 .trim()
189 .parse::<f64>()
190 .map_err(|_| ValidationError("invalid NVIDIA VRAM".into()))?;
191 require(mib.is_finite() && mib > 0.0, "invalid NVIDIA VRAM")?;
192 Ok(json!({"vendor":name.trim(),"vram":mib,"vramDynamic":false}))
193 })
194 .collect();
195 }
196 let value: Value =
197 serde_json::from_str(raw).map_err(|error| ValidationError(error.to_string()))?;
198 let entries = if platform == "darwin" {
199 value["SPDisplaysDataType"]
200 .as_array()
201 .cloned()
202 .ok_or_else(|| ValidationError("missing display controllers".into()))?
203 } else if let Some(entries) = value.as_array() {
204 entries.clone()
205 } else if value.is_object() {
206 vec![value]
207 } else {
208 return Err(ValidationError("invalid GPU response".into()));
209 };
210 entries.iter().map(|entry| {
211 let name = entry[if platform == "darwin" { "sppci_model" } else { "Name" }].as_str().ok_or_else(|| ValidationError("missing GPU name".into()))?;
212 let mib = if platform == "darwin" {
213 let memory = entry["spdisplays_vram"].as_str().or(entry["spdisplays_vram_shared"].as_str());
214 memory.and_then(|text| { let mut parts = text.split_whitespace(); let number = parts.next()?.parse::<f64>().ok()?; let unit = parts.next()?; match unit { "GB" => Some(number * 1024.0), "MB" => Some(number), _ => None } }).unwrap_or(0.0)
215 } else { entry["AdapterRAM"].as_f64().unwrap_or(0.0) / 1048576.0 };
216 Ok(json!({"vendor":name,"vram":mib,"vramDynamic": if vendor(name) == GpuVendor::Intel { Value::Null } else { json!(false) }}))
217 }).collect()
218}
219
220pub async fn command_output(program: &str, args: &[&str]) -> Result<String, ValidationError> {
221 let mut child = Command::new(program)
222 .args(args)
223 .stdin(Stdio::null())
224 .stdout(Stdio::piped())
225 .stderr(Stdio::null())
226 .kill_on_drop(true)
227 .spawn()
228 .map_err(|error| ValidationError(error.to_string()))?;
229 let stdout = child
230 .stdout
231 .take()
232 .ok_or_else(|| ValidationError("missing probe output".into()))?;
233 let operation = async {
234 let mut bytes = Vec::new();
235 stdout
236 .take(1048577)
237 .read_to_end(&mut bytes)
238 .await
239 .map_err(|error| ValidationError(error.to_string()))?;
240 require(
241 bytes.len() <= 1048576,
242 "hardware probe output exceeds 1 MiB",
243 )?;
244 let status = child
245 .wait()
246 .await
247 .map_err(|error| ValidationError(error.to_string()))?;
248 require(status.success(), "hardware probe command failed")?;
249 String::from_utf8(bytes).map_err(|_| ValidationError("probe output is not UTF-8".into()))
250 };
251 tokio::time::timeout(Duration::from_secs(3), operation)
252 .await
253 .map_err(|_| ValidationError("hardware probe timed out".into()))?
254}
255
256pub async fn detect() -> Result<(Hardware, Vec<String>), ValidationError> {
257 let platform = match std::env::consts::OS {
258 "macos" => "darwin",
259 "windows" => "win32",
260 "linux" => "linux",
261 _ => return Err(ValidationError("unsupported platform".into())),
262 };
263 let mut warnings = Vec::new();
264 let gpu = match platform {
265 "darwin" => command_output("/usr/sbin/system_profiler", &["SPDisplaysDataType", "-json"]).await,
266 "win32" => command_output("powershell.exe", &["-NoProfile", "-NonInteractive", "-Command", "Get-CimInstance Win32_VideoController | Select-Object Name,AdapterRAM | ConvertTo-Json -Compress"]).await,
267 _ => command_output("nvidia-smi", &["--query-gpu=name,memory.total", "--format=csv,noheader,nounits"]).await,
268 };
269 let mut controllers = match gpu.and_then(|text| parse_gpu_output(platform, &text)) {
270 Ok(gpus) => gpus,
271 Err(error) => {
272 warnings.push(format!(
273 "GPU probe unavailable: {error}; dedicated VRAM unknown"
274 ));
275 vec![]
276 }
277 };
278 if platform == "win32" {
279 match command_output("nvidia-smi", &["--query-gpu=name,memory.total", "--format=csv,noheader,nounits"]).await.and_then(|raw| parse_gpu_output("linux", &raw)) {
280 Ok(precise) if !precise.is_empty() => controllers = merge_nvidia(controllers, precise),
281 _ => warnings.push("Windows GPU memory uses WMI fallback; large dedicated VRAM values may be truncated".into()),
282 }
283 }
284 if platform == "linux" {
285 match linux_drm().and_then(|entries| map_linux_drm(&entries)) {
286 Ok(entries) => controllers.extend(entries),
287 Err(error) => warnings.push(format!("DRM GPU probe unavailable: {error}")),
288 }
289 }
290 let measurements = tokio::task::spawn_blocking(|| {
291 let mut system = sysinfo::System::new();
292 system.refresh_memory();
293 #[cfg(unix)]
295 let disks = rustix::fs::statvfs("/")
296 .map(|stats| {
297 vec![json!({"mount":"/","available":stats.f_bavail.saturating_mul(stats.f_frsize)})]
298 })
299 .unwrap_or_default();
300 #[cfg(not(unix))]
301 let disks = sysinfo::Disks::new_with_refreshed_list()
302 .iter()
303 .map(|disk| json!({"mount":disk.mount_point().to_string_lossy(),"available":disk.available_space()}))
304 .collect::<Vec<_>>();
305 (
306 system.total_memory(),
307 system.available_memory(),
308 system.free_memory(),
309 disks,
310 )
311 });
312 let (total, available, free, disks) =
313 tokio::time::timeout(Duration::from_secs(3), measurements)
314 .await
315 .map_err(|_| ValidationError("RAM/disk probe timed out".into()))?
316 .map_err(|error| ValidationError(error.to_string()))?;
317 let snapshot = json!({"arch":std::env::consts::ARCH,"platform":platform,"total":total,"available":available,"free":free,"controllers":controllers,"disks":disks,"unifiedMemory":(platform == "linux" && kfd_apu(&linux_kfd_nodes())).then_some(true)});
318 let fallback = map_snapshot(
319 &json!({"arch":std::env::consts::ARCH,"platform":platform,"total":total,"free":free.max(1),"controllers":[],"disks":[]}),
320 )?;
321 let (hardware, fallback_warnings) = detect_with(|| Ok(snapshot), &fallback)?;
322 warnings.extend(fallback_warnings);
323 Ok((hardware, warnings))
324}
325
326pub fn merge_nvidia(mut controllers: Vec<Value>, precise: Vec<Value>) -> Vec<Value> {
327 controllers.retain(|entry| vendor(entry["vendor"].as_str().unwrap_or("")) != GpuVendor::Nvidia);
328 controllers.extend(precise);
329 controllers
330}
331
332pub fn kfd_apu(nodes: &[String]) -> bool {
334 nodes.iter().any(|properties| {
335 let count = |key: &str| {
336 properties.lines().find_map(|line| {
337 let (name, value) = line.split_once(' ')?;
338 (name == key)
339 .then(|| value.trim().parse::<u64>().ok())
340 .flatten()
341 })
342 };
343 count("cpu_cores_count").is_some_and(|cores| cores > 0)
344 && count("simd_count").is_some_and(|simds| simds > 0)
345 })
346}
347
348fn linux_kfd_nodes() -> Vec<String> {
349 use std::io::Read;
350 let Ok(nodes) = std::fs::read_dir("/sys/class/kfd/kfd/topology/nodes") else {
351 return vec![];
352 };
353 nodes
354 .take(64)
355 .filter_map(|node| {
356 let mut text = String::new();
357 std::fs::File::open(node.ok()?.path().join("properties"))
358 .ok()?
359 .take(65537)
360 .read_to_string(&mut text)
361 .ok()?;
362 (text.len() <= 65536).then_some(text)
363 })
364 .collect()
365}
366
367pub fn map_linux_drm(entries: &[Value]) -> Result<Vec<Value>, ValidationError> {
368 entries
369 .iter()
370 .map(|entry| {
371 let id = entry["vendor"]
372 .as_str()
373 .ok_or_else(|| ValidationError("missing DRM vendor".into()))?;
374 let vendor = match id.trim() {
375 "0x1002" => "AMD",
376 "0x8086" => "Intel",
377 "0x10de" => "NVIDIA",
378 _ => "unknown",
379 };
380 let bytes = match entry["bytes"].as_str() {
381 Some(raw) => raw
382 .trim()
383 .parse::<u64>()
384 .map_err(|_| ValidationError("invalid DRM memory bytes".into()))?,
385 None if entry["bytes"].is_null() => 0,
386 _ => return Err(ValidationError("invalid DRM memory measurement".into())),
387 };
388 require(bytes <= 9007199254740991, "DRM memory exceeds safe range")?;
389 Ok(json!({"vendor":vendor,"vram":bytes as f64 / 1048576.0,"vramDynamic":bytes == 0}))
390 })
391 .collect()
392}
393
394fn linux_drm() -> Result<Vec<Value>, ValidationError> {
395 use std::io::Read;
396 let read = |path: &std::path::Path| -> Option<String> {
397 let mut text = String::new();
398 std::fs::File::open(path)
399 .ok()?
400 .take(4097)
401 .read_to_string(&mut text)
402 .ok()?;
403 (text.len() <= 4096).then_some(text)
404 };
405 let directories =
406 std::fs::read_dir("/sys/class/drm").map_err(|error| ValidationError(error.to_string()))?;
407 let mut entries = Vec::new();
408 for entry in directories.take(256) {
409 let entry = entry.map_err(|error| ValidationError(error.to_string()))?;
410 let name = entry.file_name().to_string_lossy().into_owned();
411 if !name.strip_prefix("card").is_some_and(|suffix| {
412 !suffix.is_empty() && suffix.bytes().all(|byte| byte.is_ascii_digit())
413 }) {
414 continue;
415 }
416 let device = entry.path().join("device");
417 let Some(vendor) = read(&device.join("vendor")) else {
418 continue;
419 };
420 if vendor.trim() == "0x10de" {
421 continue;
422 }
423 let bytes = read(&device.join("mem_info_vram_total"))
424 .or_else(|| read(&device.join("lmem_total_bytes")));
425 entries.push(json!({"vendor":vendor,"bytes":bytes}));
426 }
427 Ok(entries)
428}