| 1 | use std::process::Command; |
| 2 | |
| 3 | use super::{Gpu, GpuMachine}; |
| 4 | |
| 5 | #[derive(Default)] |
| 6 | pub struct Sampler; |
| 7 | |
| 8 | impl Sampler { |
| 9 | pub fn sample(&mut self) -> Option<Gpu> { |
| 10 | let output = Command::new("ioreg") |
| 11 | .args(["-r", "-d", "1", "-w", "0", "-c", "IOAccelerator"]) |
| 12 | .output() |
| 13 | .ok()?; |
| 14 | let text = String::from_utf8_lossy(&output.stdout); |
| 15 | let number = |key: &str| -> Option<u64> { |
| 16 | let at = text.find(&format!("\"{key}\"="))? + key.len() + 3; |
| 17 | let digits: String = text[at..] |
| 18 | .chars() |
| 19 | .take_while(char::is_ascii_digit) |
| 20 | .collect(); |
| 21 | digits.parse().ok() |
| 22 | }; |
| 23 | let model = text |
| 24 | .find("\"model\" = \"") |
| 25 | .map(|at| at + 11) |
| 26 | .and_then(|at| text[at..].split('"').next()) |
| 27 | .unwrap_or_default() |
| 28 | .to_string(); |
| 29 | let mb = |bytes: u64| (bytes / (1024 * 1024)) as u32; |
| 30 | Some(Gpu { |
| 31 | model, |
| 32 | machine: GpuMachine { |
| 33 | gfx: number("Device Utilization %")? as f32, |
| 34 | video: 0., |
| 35 | vram_used_mb: number("In use system memory").map_or(0, mb), |
| 36 | vram_total_mb: number("Alloc system memory").map_or(0, mb), |
| 37 | }, |
| 38 | ..Gpu::default() |
| 39 | }) |
| 40 | } |
| 41 | } |