From 827c53a4c49fb722690b752f020a8788ab2eef9a Mon Sep 17 00:00:00 2001 From: ClementTsang <34804052+ClementTsang@users.noreply.github.com> Date: Sun, 10 Dec 2023 20:55:57 -0500 Subject: [PATCH] cleanup --- src/app/data_harvester/temperature/linux.rs | 51 +++++++++++---------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/src/app/data_harvester/temperature/linux.rs b/src/app/data_harvester/temperature/linux.rs index fea02711d..2070c9a78 100644 --- a/src/app/data_harvester/temperature/linux.rs +++ b/src/app/data_harvester/temperature/linux.rs @@ -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,31 +203,7 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option) -> 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 !should_read_temp { + if !is_device_awake(&file_path) { continue; }