diff options
author | shurizzle <me@shurizzle.dev> | 2022-02-12 22:40:33 +0100 |
---|---|---|
committer | shurizzle <me@shurizzle.dev> | 2022-02-13 00:07:44 +0100 |
commit | c0feff3c01d8901ac3a0685c1d0c879652b35810 (patch) | |
tree | 026bdd278ae92954150c572a3b62b0fb6f24c11a /src | |
parent | 9ef7f5d4b787b97a35c39407ce9748e11c0e2fcd (diff) |
Add support for nvidia GPUs
Diffstat (limited to 'src')
-rw-r--r-- | src/app/data_harvester/temperature/heim.rs | 5 | ||||
-rw-r--r-- | src/app/data_harvester/temperature/mod.rs | 15 | ||||
-rw-r--r-- | src/app/data_harvester/temperature/nvidia.rs | 39 | ||||
-rw-r--r-- | src/app/data_harvester/temperature/sysinfo.rs | 18 |
4 files changed, 68 insertions, 9 deletions
diff --git a/src/app/data_harvester/temperature/heim.rs b/src/app/data_harvester/temperature/heim.rs index 570f490e..9cd4d348 100644 --- a/src/app/data_harvester/temperature/heim.rs +++ b/src/app/data_harvester/temperature/heim.rs @@ -49,6 +49,11 @@ pub async fn get_temperature_data( } } + #[cfg(feature = "nvidia")] + { + super::nvidia::add_nvidia_data(&mut temperature_vec, temp_type, filter)?; + } + temp_vec_sort(&mut temperature_vec); Ok(Some(temperature_vec)) } diff --git a/src/app/data_harvester/temperature/mod.rs b/src/app/data_harvester/temperature/mod.rs index 8f3b776e..cbd92c04 100644 --- a/src/app/data_harvester/temperature/mod.rs +++ b/src/app/data_harvester/temperature/mod.rs @@ -13,6 +13,9 @@ cfg_if::cfg_if! { } } +#[cfg(feature = "nvidia")] +pub mod nvidia; + use std::cmp::Ordering; use crate::app::Filter; @@ -36,6 +39,18 @@ impl Default for TemperatureType { } } +cfg_if::cfg_if! { + if #[cfg(any(feature = "nvidia", target_os = "macos", target_os = "windows"))] { + fn convert_celsius_to_kelvin(celsius: f32) -> f32 { + celsius + 273.15 + } + + fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 { + (celsius * (9.0 / 5.0)) + 32.0 + } + } +} + fn is_temp_filtered(filter: &Option<Filter>, text: &str) -> bool { if let Some(filter) = filter { if filter.is_list_ignored { diff --git a/src/app/data_harvester/temperature/nvidia.rs b/src/app/data_harvester/temperature/nvidia.rs new file mode 100644 index 00000000..c30345ab --- /dev/null +++ b/src/app/data_harvester/temperature/nvidia.rs @@ -0,0 +1,39 @@ +use crate::app::Filter; + +use super::{ + convert_celsius_to_fahrenheit, convert_celsius_to_kelvin, is_temp_filtered, TempHarvest, + TemperatureType, +}; + +use nvml_wrapper::{enum_wrappers::device::TemperatureSensor, NVML}; + +pub fn add_nvidia_data( + temperature_vec: &mut Vec<TempHarvest>, temp_type: &TemperatureType, filter: &Option<Filter>, +) -> crate::utils::error::Result<()> { + if let Ok(nvml) = NVML::init() { + if let Ok(ngpu) = nvml.device_count() { + for i in 0..ngpu { + if let Ok(device) = nvml.device_by_index(i) { + if let (Ok(name), Ok(temperature)) = + (device.name(), device.temperature(TemperatureSensor::Gpu)) + { + if is_temp_filtered(filter, &name) { + let temperature = temperature as f32; + let temperature = match temp_type { + TemperatureType::Celsius => temperature, + TemperatureType::Kelvin => convert_celsius_to_kelvin(temperature), + TemperatureType::Fahrenheit => { + convert_celsius_to_fahrenheit(temperature) + } + }; + + temperature_vec.push(TempHarvest { name, temperature }); + } + } + } + } + } + } + + Ok(()) +} diff --git a/src/app/data_harvester/temperature/sysinfo.rs b/src/app/data_harvester/temperature/sysinfo.rs index bc246636..f4f0061b 100644 --- a/src/app/data_harvester/temperature/sysinfo.rs +++ b/src/app/data_harvester/temperature/sysinfo.rs @@ -1,6 +1,9 @@ //! Gets temperature data via sysinfo. -use super::{is_temp_filtered, temp_vec_sort, TempHarvest, TemperatureType}; +use super::{ + convert_celsius_to_fahrenheit, convert_celsius_to_kelvin, is_temp_filtered, temp_vec_sort, + TempHarvest, TemperatureType, +}; use crate::app::Filter; pub async fn get_temperature_data( @@ -12,14 +15,6 @@ pub async fn get_temperature_data( return Ok(None); } - fn convert_celsius_to_kelvin(celsius: f32) -> f32 { - celsius + 273.15 - } - - fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 { - (celsius * (9.0 / 5.0)) + 32.0 - } - let mut temperature_vec: Vec<TempHarvest> = Vec::new(); let sensor_data = sys.components(); @@ -40,6 +35,11 @@ pub async fn get_temperature_data( } } + #[cfg(feature = "nvidia")] + { + super::nvidia::add_nvidia_data(&mut temperature_vec, temp_type, filter)?; + } + temp_vec_sort(&mut temperature_vec); Ok(Some(temperature_vec)) } |