diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2023-12-20 01:36:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-20 01:36:08 -0500 |
commit | a67da93c5f48b9c85fed0fae5e4c310d6a9361e1 (patch) | |
tree | 90bcbd4f688aecb339e5c450a1d6bac11ac671f5 /src/app | |
parent | 004c83728d13ecbd2eb892a7db718a5fc2413a52 (diff) |
other: if in a non-D0 state, short-circuit further logic (#1355)
* other: if in a non-D0 state, short-circuit further logic
* cleanup
* add back an empty name and value
* fix for macos/windows
* some testing things
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/data_harvester/nvidia.rs | 2 | ||||
-rw-r--r-- | src/app/data_harvester/temperature.rs | 2 | ||||
-rw-r--r-- | src/app/data_harvester/temperature/linux.rs | 77 | ||||
-rw-r--r-- | src/app/data_harvester/temperature/sysinfo.rs | 6 |
4 files changed, 47 insertions, 40 deletions
diff --git a/src/app/data_harvester/nvidia.rs b/src/app/data_harvester/nvidia.rs index 848a18f7..964ccc5a 100644 --- a/src/app/data_harvester/nvidia.rs +++ b/src/app/data_harvester/nvidia.rs @@ -54,7 +54,7 @@ pub fn get_nvidia_vecs( temp_vec.push(TempHarvest { name: name.clone(), - temperature, + temperature: Some(temperature), }); } } diff --git a/src/app/data_harvester/temperature.rs b/src/app/data_harvester/temperature.rs index a15eb26a..220190e6 100644 --- a/src/app/data_harvester/temperature.rs +++ b/src/app/data_harvester/temperature.rs @@ -18,7 +18,7 @@ use crate::app::Filter; #[derive(Default, Debug, Clone)] pub struct TempHarvest { pub name: String, - pub temperature: f32, + pub temperature: Option<f32>, } #[derive(Clone, Debug, Copy, PartialEq, Eq, Default)] diff --git a/src/app/data_harvester/temperature/linux.rs b/src/app/data_harvester/temperature/linux.rs index a62c52b1..a25dc352 100644 --- a/src/app/data_harvester/temperature/linux.rs +++ b/src/app/data_harvester/temperature/linux.rs @@ -9,7 +9,7 @@ use anyhow::Result; use hashbrown::{HashMap, HashSet}; use super::{is_temp_filtered, TempHarvest, TemperatureType}; -use crate::app::Filter; +use crate::{app::Filter, utils::error::BottomError}; const EMPTY_NAME: &str = "Unknown"; @@ -24,7 +24,7 @@ fn read_temp(path: &Path) -> Result<f32> { Ok(fs::read_to_string(path)? .trim_end() .parse::<f32>() - .map_err(|e| crate::utils::error::BottomError::ConversionError(e.to_string()))? + .map_err(|e| BottomError::ConversionError(e.to_string()))? / 1_000.0) } @@ -131,8 +131,8 @@ fn finalize_name( None => label, }, (Some(name), None) => name, - (None, None) => match &fallback_sensor_name { - Some(sensor_name) => sensor_name.clone(), + (None, None) => match fallback_sensor_name { + Some(sensor_name) => sensor_name.to_owned(), None => EMPTY_NAME.to_string(), }, }; @@ -140,6 +140,31 @@ fn finalize_name( counted_name(seen_names, candidate_name) } +/// Whether the temperature should *actually* be read during enumeration. +/// Will return false if the state is not D0/unknown, or if it does not support `device/power_state`. +#[inline] +fn is_device_awake(path: &Path) -> bool { + // Whether the temperature should *actually* be read during enumeration. + // Set to false if the device is in ACPI D3cold. + // Documented at https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-power_state + let device = path.join("device"); + let power_state = device.join("power_state"); + if power_state.exists() { + if let Ok(state) = fs::read_to_string(power_state) { + let state = state.trim(); + // The zenpower3 kernel module (incorrectly?) reports "unknown", causing this check + // to fail and temperatures to appear as zero instead of having the file not exist. + // + // Their self-hosted git instance has disabled sign up, so this bug cant be reported either. + state == "D0" || state == "unknown" + } else { + true + } + } else { + true + } +} + /// Get temperature sensors from the linux sysfs interface `/sys/class/hwmon` and /// `/sys/devices/platform/coretemp.*`. It returns all found temperature sensors, and the number /// of checked hwmon directories (not coretemp directories). @@ -178,29 +203,15 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H for file_path in dirs { let sensor_name = read_to_string_lossy(file_path.join("name")); - // Whether the temperature should *actually* be read during enumeration. - // Set to false if the device is in ACPI D3cold. - // - // If it is false, then the temperature will be set to 0.0 later down the line. - let should_read_temp = { - // Documented at https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-power_state - let device = file_path.join("device"); - let power_state = device.join("power_state"); - if power_state.exists() { - if let Ok(state) = fs::read_to_string(power_state) { - let state = state.trim(); - // The zenpower3 kernel module (incorrectly?) reports "unknown", causing this check - // to fail and temperatures to appear as zero instead of having the file not exist. - // - // Their self-hosted git instance has disabled sign up, so this bug cant be reported either. - state == "D0" || state == "unknown" - } else { - true - } - } else { - true + if !is_device_awake(&file_path) { + if let Some(sensor_name) = sensor_name { + temperatures.push(TempHarvest { + name: sensor_name, + temperature: None, + }); } - }; + continue; + } if let Ok(dir_entries) = file_path.read_dir() { // Enumerate the devices temperature sensors @@ -272,19 +283,15 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H let name = finalize_name(hwmon_name, sensor_label, &sensor_name, &mut seen_names); if is_temp_filtered(filter, &name) { - let temp_celsius = if should_read_temp { - if let Ok(temp) = read_temp(&temp_path) { - temp - } else { - continue; - } + let temp_celsius = if let Ok(temp) = read_temp(&temp_path) { + temp } else { - 0.0 + continue; }; temperatures.push(TempHarvest { name, - temperature: temp_type.convert_temp_unit(temp_celsius), + temperature: Some(temp_type.convert_temp_unit(temp_celsius)), }); } } @@ -329,7 +336,7 @@ fn add_thermal_zone_temperatures( temperatures.push(TempHarvest { name, - temperature: temp_type.convert_temp_unit(temp_celsius), + temperature: Some(temp_type.convert_temp_unit(temp_celsius)), }); } } diff --git a/src/app/data_harvester/temperature/sysinfo.rs b/src/app/data_harvester/temperature/sysinfo.rs index cc415164..4e28efa7 100644 --- a/src/app/data_harvester/temperature/sysinfo.rs +++ b/src/app/data_harvester/temperature/sysinfo.rs @@ -19,7 +19,7 @@ pub fn get_temperature_data( if is_temp_filtered(filter, &name) { temperature_vec.push(TempHarvest { name, - temperature: temp_type.convert_temp_unit(component.temperature()), + temperature: Some(temp_type.convert_temp_unit(component.temperature())), }); } } @@ -36,11 +36,11 @@ pub fn get_temperature_data( if let Some(temp) = temp.as_temperature() { temperature_vec.push(TempHarvest { name, - temperature: match temp_type { + temperature: Some(match temp_type { TemperatureType::Celsius => temp.celsius(), TemperatureType::Kelvin => temp.kelvin(), TemperatureType::Fahrenheit => temp.fahrenheit(), - }, + }), }); } } |