Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Dec 18, 2023
1 parent 376af24 commit 72ef30e
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/app/data_harvester/temperature/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -178,31 +203,7 @@ 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 !should_read_temp {
if !is_device_awake(&file_path) {
continue;
}

Expand Down

0 comments on commit 72ef30e

Please sign in to comment.