Add support for nvidia GPUs

This commit is contained in:
shurizzle
2022-02-13 00:07:44 +01:00
parent 9ef7f5d4b7
commit c0feff3c01
6 changed files with 178 additions and 11 deletions
@@ -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))
}
+15
View File
@@ -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 {
@@ -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(())
}
@@ -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))
}