Skip to content

Commit

Permalink
Add CPU temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
valpackett committed Nov 12, 2017
1 parent 7e90257 commit c27e070
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ A Rust library for getting system information/statistics:
- battery life
- filesystem mounts (and disk usage)
- network interfaces
- CPU temperature

Unlike [sys-info-rs](https://github.com/FillZpp/sys-info-rs), this one is written purely in Rust.

Expand Down
5 changes: 5 additions & 0 deletions examples/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ fn main() {
let cpu = cpu.done().unwrap();
println!("CPU load: {}% user, {}% nice, {}% system, {}% intr, {}% idle ",
cpu.user * 100.0, cpu.nice * 100.0, cpu.system * 100.0, cpu.interrupt * 100.0, cpu.idle * 100.0);

match sys.cpu_temp() {
Ok(cpu_temp) => println!("\nCPU temp: {}", cpu_temp),
Err(x) => println!("\nCPU temp: {}", x)
}
}
5 changes: 5 additions & 0 deletions src/platform/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ pub trait Platform {
/// interface, and we're doing deduplication and packing everything into one object per
/// interface. You can use the .values() iterator if you need to iterate over all of them.
fn networks(&self) -> io::Result<BTreeMap<String, Network>>;

/// Returns the current CPU temperature in degrees Celsius.
///
/// Depending on the platform, this might be core 0, package, etc.
fn cpu_temp(&self) -> io::Result<f32>;
}
8 changes: 8 additions & 0 deletions src/platform/freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ lazy_static! {
static ref BATTERY_LIFE: [c_int; 4] = sysctl_mib!(4, "hw.acpi.battery.life");
static ref BATTERY_TIME: [c_int; 4] = sysctl_mib!(4, "hw.acpi.battery.time");
static ref ACLINE: [c_int; 3] = sysctl_mib!(3, "hw.acpi.acline");
static ref CPU0TEMP: [c_int; 4] = sysctl_mib!(4, "dev.cpu.0.temperature");

static ref CP_TIMES_SIZE: usize = {
let mut size: usize = 0;
Expand Down Expand Up @@ -143,6 +144,13 @@ impl Platform for PlatformImpl {
fn networks(&self) -> io::Result<BTreeMap<String, Network>> {
unix::networks()
}

fn cpu_temp(&self) -> io::Result<f32> {
let mut temp: i32 = 0; sysctl!(CPU0TEMP, &mut temp, mem::size_of::<i32>());
// The sysctl interface supports more units, but both amdtemp and coretemp always
// use IK (deciKelvin)
Ok((temp as f32 - 2731.5) / 10.0)
}
}


Expand Down
6 changes: 6 additions & 0 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ impl Platform for PlatformImpl {
fn networks(&self) -> io::Result<BTreeMap<String, Network>> {
unix::networks()
}

fn cpu_temp(&self) -> io::Result<f32> {
read_file("/sys/class/thermal/thermal_zone0/temp")
.and_then(|data| data.parse::<f32>())
.map(|num| num / 1000.0)
}
}

#[repr(C)]
Expand Down
4 changes: 4 additions & 0 deletions src/platform/openbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ impl Platform for PlatformImpl {
fn networks(&self) -> io::Result<BTreeMap<String, Network>> {
unix::networks()
}

fn cpu_temp(&self) -> io::Result<f32> {
Err(io::Error::new(io::ErrorKind::Other, "Not supported"))
}
}

fn measure_cpu() -> io::Result<Vec<CpuTime>> {
Expand Down
6 changes: 5 additions & 1 deletion src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ impl Platform for PlatformImpl {
fn networks(&self) -> io::Result<BTreeMap<String, Network>> {
Err(io::Error::new(io::ErrorKind::Other, "Not supported"))
}

fn cpu_temp(&self) -> io::Result<f32> {
Err(io::Error::new(io::ErrorKind::Other, "Not supported"))
}
}

fn power_status() -> winbase::SYSTEM_POWER_STATUS {
Expand All @@ -99,4 +103,4 @@ fn power_status() -> winbase::SYSTEM_POWER_STATUS {
};
unsafe { kernel32::GetSystemPowerStatus(&mut status); }
status
}
}

0 comments on commit c27e070

Please sign in to comment.