Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More human friendly temperature sensor naming #807

Merged
merged 4 commits into from
Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/app/data_harvester/temperature/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,57 @@ fn get_from_hwmon(
let temp_label = file_path.join(name.replace("input", "label"));
let temp_label = fs::read_to_string(temp_label).ok();

let name = match (&hwmon_name, &temp_label) {
// Do some messing around to get a more sensible name for sensors
//
// - For GPUs, this will use the kernel device name, ex `card0`
// - For nvme drives, this will also use the kernel name, ex `nvme0`.
// This is found differently than for GPUs
// - For whatever acpitz is, on my machine this is now `thermal_zone0`.
// - For k10temp, this will still be k10temp, but it has to be handled special.
let human_hwmon_name = {
let device = file_path.join("device");
// This will exist for GPUs but not others, this is how
// we find their kernel name
let drm = device.join("drm");
if drm.exists() {
// This should never actually be empty
let mut gpu = None;
for card in drm.read_dir()? {
let card = card?;
let name = card.file_name().to_str().unwrap_or_default().to_owned();
if name.starts_with("card") {
if let Some(hwmon_name) = hwmon_name.as_ref() {
gpu = Some(format!("{} ({})", name, hwmon_name.trim()));
} else {
gpu = Some(name)
}
break;
}
}
gpu
} else {
// This little mess is to account for stuff like k10temp
// This is needed because the `device` symlink
// points to `nvme*` for nvme drives, but to PCI buses for anything else
// If the first character is alphabetic,
// its an actual name like k10temp or nvme0, not a PCI bus
let link = fs::read_link(device)?
.file_name()
.map(|f| f.to_str().unwrap_or_default().to_owned())
.unwrap();
if link.as_bytes()[0].is_ascii_alphabetic() {
if let Some(hwmon_name) = hwmon_name.as_ref() {
Some(format!("{} ({})", link, hwmon_name.trim()))
} else {
Some(link)
}
} else {
hwmon_name.clone()
}
}
};

let name = match (&human_hwmon_name, &temp_label) {
(Some(name), Some(label)) => format!("{}: {}", name.trim(), label.trim()),
(None, Some(label)) => label.to_string(),
(Some(name), None) => name.to_string(),
Expand Down