Skip to content

cpu_temp in Linux #128

@darkforcesjedi

Description

@darkforcesjedi

In linux.rs, the cpu_temp function (

fn cpu_temp(&self) -> io::Result<f32> {
read_file("/sys/class/thermal/thermal_zone0/temp")
.or(read_file("/sys/class/hwmon/hwmon0/temp1_input"))
.and_then(|data| match data.trim().parse::<f32>() {
Ok(x) => Ok(x),
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
"Could not parse float",
)),
})
.map(|num| num / 1000.0)
}
) assumes that the CPU temperature is at /sys/class/thermal/thermal_zone0/temp or /sys/class/hwmon/hwmon0/temp1_input. The order of devices in /sys/class/hwmon is determined by how modules are loaded at boot time and can change from one boot to the next.

A better approach is to scan through the hwmon hierarchy to find a temperature provided by a kernel module associated with a CPU. This is what CPU-X does:
https://github.com/TheTumultuousUnicornOfDarkness/CPU-X/blob/84f2da456e57898e5f6655794b2ff0712f41b8c9/src/util.cpp#L507-574

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions