summaryrefslogtreecommitdiffstats
path: root/vendor/sysinfo/src/freebsd/component.rs
blob: 6529be73cdb99155023a137167e469abe12f1cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Take a look at the license at the top of the repository in the LICENSE file.

use super::utils::get_sys_value_by_name;
use crate::ComponentExt;

#[doc = include_str!("../../md_doc/component.md")]
pub struct Component {
    id: Vec<u8>,
    label: String,
    temperature: f32,
    max: f32,
}

impl ComponentExt for Component {
    fn temperature(&self) -> f32 {
        self.temperature
    }

    fn max(&self) -> f32 {
        self.max
    }

    fn critical(&self) -> Option<f32> {
        None
    }

    fn label(&self) -> &str {
        &self.label
    }

    fn refresh(&mut self) {
        unsafe {
            if let Some(temperature) = refresh_component(&self.id) {
                self.temperature = temperature;
                if self.temperature > self.max {
                    self.max = self.temperature;
                }
            }
        }
    }
}

unsafe fn refresh_component(id: &[u8]) -> Option<f32> {
    let mut temperature: libc::c_int = 0;
    if !get_sys_value_by_name(id, &mut temperature) {
        None
    } else {
        // convert from Kelvin (x 10 -> 273.2 x 10) to Celsius
        Some((temperature - 2732) as f32 / 10.)
    }
}

pub unsafe fn get_components(nb_cpus: usize) -> Vec<Component> {
    // For now, we only have temperature for CPUs...
    let mut components = Vec::with_capacity(nb_cpus);

    for core in 0..nb_cpus {
        let id = format!("dev.cpu.{}.temperature\0", core)
            .as_bytes()
            .to_vec();
        if let Some(temperature) = refresh_component(&id) {
            components.push(Component {
                id,
                label: format!("CPU {}", core + 1),
                temperature,
                max: temperature,
            });
        }
    }
    components
}