Merge pull request #676 from shurizzle/master

Add support for nvidia GPUs
This commit is contained in:
Clement Tsang 2022-02-22 03:19:44 -08:00 committed by GitHub
commit 39597975a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 178 additions and 11 deletions

107
Cargo.lock generated
View File

@ -251,6 +251,7 @@ dependencies = [
"itertools",
"libc",
"log",
"nvml-wrapper",
"once_cell",
"predicates 1.0.8",
"procfs",
@ -324,7 +325,7 @@ dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"strsim 0.8.0",
"textwrap",
"unicode-width",
"vec_map",
@ -459,6 +460,41 @@ dependencies = [
"winapi",
]
[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim 0.9.3",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "difference"
version = "2.0.0"
@ -548,6 +584,12 @@ dependencies = [
"num-traits",
]
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "futures"
version = "0.3.17"
@ -839,6 +881,12 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "indexmap"
version = "1.7.0"
@ -891,6 +939,16 @@ version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
name = "libloading"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883"
dependencies = [
"cfg-if",
"winapi",
]
[[package]]
name = "lock_api"
version = "0.4.5"
@ -1051,6 +1109,29 @@ dependencies = [
"libc",
]
[[package]]
name = "nvml-wrapper"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f6fb95ab904553d1b8914d340cadd0b34bee1cc984668eaa096a018f04ed8b8"
dependencies = [
"bitflags",
"libloading",
"nvml-wrapper-sys",
"static_assertions",
"thiserror",
"wrapcenum-derive",
]
[[package]]
name = "nvml-wrapper-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be195fa681ad2a9c903a866bc3f97f174333f04fb7b9e7c1f2413452f698484"
dependencies = [
"libloading",
]
[[package]]
name = "object"
version = "0.26.2"
@ -1375,12 +1456,24 @@ dependencies = [
"winapi",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "syn"
version = "1.0.80"
@ -1590,3 +1683,15 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "wrapcenum-derive"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bcc065c85ad2c3bd12aa4118bf164835712e25080c392557801a13292c60aec"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]

View File

@ -32,7 +32,9 @@ opt-level = 3
codegen-units = 1
[features]
default = ["fern", "log", "battery"]
default = ["fern", "log", "battery", "gpu"]
gpu = ["nvidia"]
nvidia = ["nvml-wrapper"]
[dependencies]
anyhow = "1.0.40"
@ -64,6 +66,7 @@ unicode-width = "0.1"
fern = { version = "0.6.0", optional = true }
log = { version = "0.4.14", optional = true }
battery = { version = "0.7.8", optional = true }
nvml-wrapper = { version = "0.7.0", optional = true }
[target.'cfg(unix)'.dependencies]
libc = "0.2.86"

View File

@ -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))
}

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 {

View File

@ -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(())
}

View File

@ -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))
}