yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use std::{process, sync::Arc}; |
| 2 | |
| 3 | use nvml_wrapper::{Nvml, enums::device::UsedGpuMemory}; |
| 4 | |
| 5 | use super::{Gpu, GpuMachine}; |
| 6 | |
| 7 | const MIB: u64 = 1024 * 1024; |
| 8 | |
| 9 | #[derive(Default)] |
| 10 | enum State { |
| 11 | #[default] |
| 12 | Unset, |
| 13 | Ready(Box<Nvml>), |
| 14 | Failed, |
| 15 | } |
| 16 | |
| 17 | #[derive(Default)] |
| 18 | pub struct Sampler { |
| 19 | state: State, |
| 20 | last_seen: Option<u64>, |
| 21 | model: Option<Arc<str>>, |
| 22 | } |
| 23 | |
| 24 | impl Sampler { |
| 25 | pub fn sample(&mut self) -> Option<Gpu> { |
| 26 | if let State::Unset = self.state { |
| 27 | self.state = match Nvml::init() { |
| 28 | Ok(nvml) => State::Ready(Box::new(nvml)), |
| 29 | Err(_) => State::Failed, |
| 30 | }; |
| 31 | } |
| 32 | let State::Ready(nvml) = &self.state else { |
| 33 | return None; |
| 34 | }; |
| 35 | let pid = process::id(); |
| 36 | for index in 0..nvml.device_count().ok()? { |
| 37 | let Ok(device) = nvml.device_by_index(index) else { |
| 38 | continue; |
| 39 | }; |
| 40 | let Some(process) = device |
| 41 | .running_graphics_processes() |
| 42 | .unwrap_or_default() |
| 43 | .into_iter() |
| 44 | .chain(device.running_compute_processes().unwrap_or_default()) |
| 45 | .find(|process| process.pid == pid) |
| 46 | else { |
| 47 | continue; |
| 48 | }; |
| 49 | let samples = device |
| 50 | .process_utilization_stats(self.last_seen) |
| 51 | .unwrap_or_default(); |
| 52 | self.last_seen = samples |
| 53 | .iter() |
| 54 | .map(|sample| sample.timestamp) |
| 55 | .max() |
| 56 | .max(self.last_seen); |
| 57 | let (gfx, video) = samples.iter().filter(|sample| sample.pid == pid).fold( |
| 58 | (0, 0), |
| 59 | |(gfx, video), sample| { |
| 60 | ( |
| 61 | gfx.max(sample.sm_util), |
| 62 | video.max(sample.enc_util.max(sample.dec_util)), |
| 63 | ) |
| 64 | }, |
| 65 | ); |
| 66 | let vram_mb = match process.used_gpu_memory { |
| 67 | UsedGpuMemory::Used(bytes) => (bytes / MIB) as u32, |
| 68 | UsedGpuMemory::Unavailable => 0, |
| 69 | }; |
| 70 | let machine_gfx = device.utilization_rates().map_or(0, |rates| rates.gpu); |
| 71 | let machine_video = device |
| 72 | .encoder_utilization() |
| 73 | .map_or(0, |info| info.utilization) |
| 74 | .max( |
| 75 | device |
| 76 | .decoder_utilization() |
| 77 | .map_or(0, |info| info.utilization), |
| 78 | ); |
| 79 | let (vram_used_mb, vram_total_mb) = device.memory_info().map_or((0, 0), |memory| { |
| 80 | ((memory.used / MIB) as u32, (memory.total / MIB) as u32) |
| 81 | }); |
| 82 | let model = self |
| 83 | .model |
| 84 | .get_or_insert_with(|| { |
| 85 | let name = device.name().unwrap_or_default(); |
| 86 | if name.contains("NVIDIA") { |
| 87 | name.into() |
| 88 | } else { |
| 89 | format!("NVIDIA {name}").into() |
| 90 | } |
| 91 | }) |
| 92 | .clone(); |
| 93 | return Some(Gpu { |
| 94 | model: model.to_string(), |
| 95 | gfx: gfx as f32, |
| 96 | video: video as f32, |
| 97 | vram_mb, |
| 98 | machine: GpuMachine { |
| 99 | gfx: machine_gfx as f32, |
| 100 | video: machine_video as f32, |
| 101 | vram_used_mb, |
| 102 | vram_total_mb, |
| 103 | }, |
| 104 | }); |
| 105 | } |
| 106 | None |
| 107 | } |
| 108 | } |