use std::process::Command; use super::{Gpu, GpuMachine}; #[derive(Default)] pub struct Sampler; impl Sampler { pub fn sample(&mut self) -> Option { let output = Command::new("ioreg") .args(["-r", "-d", "1", "-w", "0", "-c", "IOAccelerator"]) .output() .ok()?; let text = String::from_utf8_lossy(&output.stdout); let number = |key: &str| -> Option { let at = text.find(&format!("\"{key}\"="))? + key.len() + 3; let digits: String = text[at..] .chars() .take_while(char::is_ascii_digit) .collect(); digits.parse().ok() }; let model = text .find("\"model\" = \"") .map(|at| at + 11) .and_then(|at| text[at..].split('"').next()) .unwrap_or_default() .to_string(); let mb = |bytes: u64| (bytes / (1024 * 1024)) as u32; Some(Gpu { model, machine: GpuMachine { gfx: number("Device Utilization %")? as f32, video: 0., vram_used_mb: number("In use system memory").map_or(0, mb), vram_total_mb: number("Alloc system memory").map_or(0, mb), }, ..Gpu::default() }) } }