| 1 | use std::{num::NonZeroU64, sync::OnceLock}; |
| 2 | |
| 3 | use nvml_wrapper::{ |
| 4 | Nvml, enum_wrappers::device::TemperatureSensor, enums::device::UsedGpuMemory, error::NvmlError, |
| 5 | }; |
| 6 | |
| 7 | use crate::{ |
| 8 | app::{filter::Filter, layout_manager::UsedWidgets}, |
| 9 | collection::{memory::MemData, processes::Pid, temperature::TempSensorData}, |
| 10 | utils::int_hash::IntHashMap, |
| 11 | }; |
| 12 | |
| 13 | pub static NVML_DATA: OnceLock<Result<Nvml, NvmlError>> = OnceLock::new(); |
| 14 | |
| 15 | pub struct GpusData { |
| 16 | pub memory: Option<Vec<(String, MemData)>>, |
| 17 | pub temperature: Option<Vec<TempSensorData>>, |
| 18 | pub procs: Option<(u64, Vec<IntHashMap<Pid, (u64, u32)>>)>, |
| 19 | } |
| 20 | |
| 21 | /// Wrapper around Nvml::init |
| 22 | /// |
| 23 | /// On Linux, if `Nvml::init()` fails, this function attempts to explicitly load |
| 24 | /// the library from `libnvidia-ml.so.1`. On other platforms, it simply calls |
| 25 | /// `Nvml::init`. |
| 26 | /// |
| 27 | /// This is a workaround until https://github.com/Cldfire/nvml-wrapper/pull/63 is accepted. |
| 28 | /// Then, we can go back to calling `Nvml::init` directly on all platforms. |
| 29 | fn init_nvml() -> Result<Nvml, NvmlError> { |
| 30 | #[cfg(not(target_os = "linux"))] |
| 31 | { |
| 32 | Nvml::init() |
| 33 | } |
| 34 | #[cfg(target_os = "linux")] |
| 35 | { |
| 36 | match Nvml::init() { |
| 37 | Ok(nvml) => Ok(nvml), |
| 38 | Err(_) => Nvml::builder() |
| 39 | .lib_path(std::ffi::OsStr::new("libnvidia-ml.so.1")) |
| 40 | .init(), |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Returns the GPU data from NVIDIA cards. |
| 46 | #[inline] |
| 47 | pub fn get_nvidia_vecs( |
| 48 | filter: &Option<Filter>, graph_filter: &Option<Filter>, widgets_to_harvest: &UsedWidgets, |
| 49 | ) -> Option<GpusData> { |
| 50 | if let Ok(nvml) = NVML_DATA.get_or_init(init_nvml) { |
| 51 | if let Ok(num_gpu) = nvml.device_count() { |
| 52 | let mut temp_vec = Vec::with_capacity(num_gpu as usize); |
| 53 | let mut mem_vec = Vec::with_capacity(num_gpu as usize); |
| 54 | let mut proc_vec = Vec::with_capacity(num_gpu as usize); |
| 55 | let mut total_mem = 0; |
| 56 | |
| 57 | for i in 0..num_gpu { |
| 58 | if let Ok(device) = nvml.device_by_index(i) { |
| 59 | if let Ok(name) = device.name() { |
| 60 | if widgets_to_harvest.use_mem |
| 61 | && let Ok(mem) = device.memory_info() |
| 62 | && let Some(total_bytes) = NonZeroU64::new(mem.total) |
| 63 | { |
| 64 | mem_vec.push(( |
| 65 | name.clone(), |
| 66 | MemData { |
| 67 | total_bytes, |
| 68 | used_bytes: mem.used, |
| 69 | }, |
| 70 | )); |
| 71 | } |
| 72 | |
| 73 | if (widgets_to_harvest.use_temp || widgets_to_harvest.use_temp_graph) |
| 74 | && (Filter::optional_should_keep(filter, &name) |
| 75 | || Filter::optional_should_keep(graph_filter, &name)) |
| 76 | { |
| 77 | if let Ok(temperature) = device.temperature(TemperatureSensor::Gpu) { |
| 78 | temp_vec.push(TempSensorData { |
| 79 | name, |
| 80 | temperature: Some(temperature as f32), |
| 81 | }); |
| 82 | } else { |
| 83 | temp_vec.push(TempSensorData { |
| 84 | name, |
| 85 | temperature: None, |
| 86 | }); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if widgets_to_harvest.use_proc { |
| 92 | let mut procs = IntHashMap::default(); |
| 93 | |
| 94 | if let Ok(gpu_procs) = device.process_utilization_stats(None) { |
| 95 | for proc in gpu_procs { |
| 96 | let pid = proc.pid as Pid; |
| 97 | let gpu_util = proc.sm_util + proc.enc_util + proc.dec_util; |
| 98 | procs.insert(pid, (0, gpu_util)); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if let Ok(compute_procs) = device.running_compute_processes() { |
| 103 | for proc in compute_procs { |
| 104 | let pid = proc.pid as Pid; |
| 105 | let gpu_mem = match proc.used_gpu_memory { |
| 106 | UsedGpuMemory::Used(val) => val, |
| 107 | UsedGpuMemory::Unavailable => 0, |
| 108 | }; |
| 109 | if let Some(prev) = procs.get(&pid) { |
| 110 | procs.insert(pid, (gpu_mem, prev.1)); |
| 111 | } else { |
| 112 | procs.insert(pid, (gpu_mem, 0)); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Use the legacy API too but prefer newer API results |
| 118 | if let Ok(graphics_procs) = device.running_graphics_processes_v2() { |
| 119 | for proc in graphics_procs { |
| 120 | let pid = proc.pid as Pid; |
| 121 | let gpu_mem = match proc.used_gpu_memory { |
| 122 | UsedGpuMemory::Used(val) => val, |
| 123 | UsedGpuMemory::Unavailable => 0, |
| 124 | }; |
| 125 | if let Some(prev) = procs.get(&pid) { |
| 126 | procs.insert(pid, (gpu_mem, prev.1)); |
| 127 | } else { |
| 128 | procs.insert(pid, (gpu_mem, 0)); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if let Ok(graphics_procs) = device.running_graphics_processes() { |
| 134 | for proc in graphics_procs { |
| 135 | let pid = proc.pid as Pid; |
| 136 | let gpu_mem = match proc.used_gpu_memory { |
| 137 | UsedGpuMemory::Used(val) => val, |
| 138 | UsedGpuMemory::Unavailable => 0, |
| 139 | }; |
| 140 | if let Some(prev) = procs.get(&pid) { |
| 141 | procs.insert(pid, (gpu_mem, prev.1)); |
| 142 | } else { |
| 143 | procs.insert(pid, (gpu_mem, 0)); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if !procs.is_empty() { |
| 149 | proc_vec.push(procs); |
| 150 | } |
| 151 | |
| 152 | // running total for proc % |
| 153 | if let Ok(mem) = device.memory_info() { |
| 154 | total_mem += mem.total; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | Some(GpusData { |
| 161 | memory: if !mem_vec.is_empty() { |
| 162 | Some(mem_vec) |
| 163 | } else { |
| 164 | None |
| 165 | }, |
| 166 | temperature: if !temp_vec.is_empty() { |
| 167 | Some(temp_vec) |
| 168 | } else { |
| 169 | None |
| 170 | }, |
| 171 | procs: if !proc_vec.is_empty() { |
| 172 | Some((total_mem, proc_vec)) |
| 173 | } else { |
| 174 | None |
| 175 | }, |
| 176 | }) |
| 177 | } else { |
| 178 | None |
| 179 | } |
| 180 | } else { |
| 181 | None |
| 182 | } |
| 183 | } |