use super::{HwSensor, HwSensorType, HwType};
#[allow(dead_code)]
const IA32_THERM_STATUS: u32 = 0x19C;
#[allow(dead_code)]
const IA32_TEMPERATURE_TARGET: u32 = 0x1A2;
#[allow(dead_code)]
const IA32_PACKAGE_THERM_STATUS: u32 = 0x1B1;
#[allow(dead_code)]
const AMD_MSR_HARDWARE_THERMAL_CONTROL: u32 = 0xC001_0059;
#[allow(dead_code)]
const AMD_MSR_REPORTED_TEMP_CONTROL: u32 = 0xC001_0063;
pub fn read_cpu_temperatures() -> Vec<HwSensor> {
let mut sensors = Vec::new();
#[cfg(target_os = "windows")]
{
if let Some(temps) = read_intel_dts() {
sensors.extend(temps);
}
if sensors.is_empty() {
if let Some(temps) = read_amd_temps() {
sensors.extend(temps);
}
}
if sensors.is_empty() {
if let Some(temps) = read_from_performance_counters() {
sensors.extend(temps);
}
}
if sensors.is_empty() {
if let Some(temps) = read_from_registry() {
sensors.extend(temps);
}
}
}
#[cfg(target_os = "linux")]
{
sensors.extend(read_linux_cpu_temps());
}
sensors
}
#[cfg(target_os = "windows")]
fn read_intel_dts() -> Option<Vec<HwSensor>> {
None
}
#[cfg(target_os = "windows")]
fn read_amd_temps() -> Option<Vec<HwSensor>> {
None
}
#[cfg(target_os = "windows")]
fn read_from_performance_counters() -> Option<Vec<HwSensor>> {
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use windows::core::PCWSTR;
use windows::Win32::System::Performance::{
PdhAddEnglishCounterW, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQueryW,
PDH_FMT_COUNTERVALUE, PDH_FMT_DOUBLE,
};
unsafe {
let mut query = std::mem::zeroed();
let result = PdhOpenQueryW(PCWSTR::null(), 0, &mut query);
if result != 0 {
return None;
}
let counter_path: Vec<u16> = OsStr::new("\\Thermal Zone Information(*)\\Temperature")
.encode_wide()
.chain(std::iter::once(0))
.collect();
let mut counter = std::mem::zeroed();
let result = PdhAddEnglishCounterW(
query,
PCWSTR::from_raw(counter_path.as_ptr()),
0,
&mut counter,
);
if result != 0 {
return None;
}
let result = PdhCollectQueryData(query);
if result != 0 {
return None;
}
let mut value: PDH_FMT_COUNTERVALUE = std::mem::zeroed();
let result = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, None, &mut value);
if result == 0 {
let kelvin = value.Anonymous.doubleValue / 10.0;
let celsius = kelvin - 273.15;
if celsius > 0.0 && celsius < 150.0 {
return Some(vec![HwSensor {
name: "CPU Package".to_string(),
value: celsius as f32,
min: None,
max: None,
sensor_type: HwSensorType::Temperature,
hardware_type: HwType::Cpu,
}]);
}
}
}
None
}
#[cfg(target_os = "windows")]
fn read_from_registry() -> Option<Vec<HwSensor>> {
use winreg::enums::*;
use winreg::RegKey;
let locations = [
(HKEY_LOCAL_MACHINE, r"HARDWARE\ACPI\THERMAL_ZONE"),
(
HKEY_LOCAL_MACHINE,
r"SYSTEM\CurrentControlSet\Control\Class\{4d36e97d-e325-11ce-bfc1-08002be10318}",
),
];
for (hive, path) in &locations {
if let Ok(key) = RegKey::predef(*hive).open_subkey(path) {
for name in key.enum_keys().filter_map(|k| k.ok()) {
if let Ok(subkey) = key.open_subkey(&name) {
if let Ok(temp) = subkey.get_value::<u32, _>("CurrentTemperature") {
let kelvin = temp as f64 / 10.0;
let celsius = kelvin - 273.15;
if celsius > 0.0 && celsius < 150.0 {
return Some(vec![HwSensor {
name: format!("Thermal Zone {}", name),
value: celsius as f32,
min: None,
max: None,
sensor_type: HwSensorType::Temperature,
hardware_type: HwType::Cpu,
}]);
}
}
}
}
}
}
None
}
#[cfg(target_os = "linux")]
fn read_linux_cpu_temps() -> Vec<HwSensor> {
use std::fs;
use std::path::Path;
let mut sensors = Vec::new();
let hwmon_path = Path::new("/sys/class/hwmon");
if let Ok(entries) = fs::read_dir(hwmon_path) {
for entry in entries.filter_map(|e| e.ok()) {
let path = entry.path();
let name_path = path.join("name");
let name = fs::read_to_string(&name_path)
.map(|s| s.trim().to_string())
.unwrap_or_default();
if name.contains("coretemp") || name.contains("k10temp") || name.contains("cpu") {
for i in 1..=16 {
let input_path = path.join(format!("temp{}_input", i));
let label_path = path.join(format!("temp{}_label", i));
if let Ok(temp_str) = fs::read_to_string(&input_path) {
if let Ok(temp_millicelsius) = temp_str.trim().parse::<i32>() {
let temp_celsius = temp_millicelsius as f32 / 1000.0;
let label = fs::read_to_string(&label_path)
.map(|s| s.trim().to_string())
.unwrap_or_else(|_| format!("{} temp{}", name, i));
sensors.push(HwSensor {
name: label,
value: temp_celsius,
min: None,
max: None,
sensor_type: HwSensorType::Temperature,
hardware_type: HwType::Cpu,
});
}
}
}
}
}
}
sensors
}