sysfs_rs/
cpu.rs

1use util::*;
2
3#[derive(Clone, Debug)]
4pub struct Cpu {
5    id: usize,
6    core_id: usize,
7    physical_package_id: usize,
8    core_siblings: Vec<bool>,
9    thread_siblings: Vec<bool>,
10    node_id: usize,
11}
12
13impl Cpu {
14    pub fn new(node: usize, id: usize) -> Result<Cpu, &'static str> {
15        let cpu = Cpu {
16            id: id,
17            core_id: 0,
18            physical_package_id: 0,
19            core_siblings: Vec::new(),
20            thread_siblings: Vec::new(),
21            node_id: node,
22        };
23        cpu.init()
24    }
25
26    fn init(mut self) -> Result<Self, &'static str> {
27        if let Ok(core_id) = get_core_id(self.node_id, self.id) {
28            self.core_id = core_id;
29        } else {
30            return Err("invalid core_id");
31        }
32        if let Ok(physical_package_id) = get_physical_package_id(self.id) {
33            self.physical_package_id = physical_package_id;
34        } else {
35            return Err("invalid physical_package_id");
36        }
37        if let Ok(core_siblings) = get_core_siblings(self.id) {
38            self.core_siblings = core_siblings;
39        } else {
40            return Err("invalid core_siblings");
41        }
42        if let Ok(thread_siblings) = get_thread_siblings(self.id) {
43            self.thread_siblings = thread_siblings;
44        } else {
45            return Err("invalid thread_siblings");
46        }
47        Ok(self)
48    }
49
50    pub fn id(&self) -> usize {
51        self.id
52    }
53
54    pub fn core_id(&self) -> usize {
55        self.core_id
56    }
57
58    pub fn set_node_id(&mut self, node_id: usize) {
59        self.node_id = node_id;
60    }
61
62    pub fn node_id(&self) -> usize {
63        self.node_id
64    }
65
66    pub fn physical_package_id(&self) -> usize {
67        self.physical_package_id
68    }
69
70    pub fn core_siblings(&self) -> Vec<bool> {
71        self.core_siblings.clone()
72    }
73
74    pub fn thread_siblings(&self) -> Vec<bool> {
75        self.thread_siblings.clone()
76    }
77
78    pub fn is_core_sibling(&self, id: usize) -> bool {
79        self.core_siblings[id]
80    }
81
82    pub fn is_thread_sibling(&self, id: usize) -> bool {
83        self.thread_siblings[id]
84    }
85}
86
87fn get_core_id(node: usize, id: usize) -> Result<usize, &'static str> {
88    let path = format!("/sys/devices/system/node/node{}/cpu{}/topology/core_id",
89                       node,
90                       id);
91    usize_from_file(path)
92}
93
94fn get_core_siblings(id: usize) -> Result<Vec<bool>, &'static str> {
95    let path = format!("/sys/devices/system/cpu/cpu{}/topology/core_siblings", id);
96    bitmask_from_hex_file(path)
97}
98
99fn get_thread_siblings(id: usize) -> Result<Vec<bool>, &'static str> {
100    let path = format!("/sys/devices/system/cpu/cpu{}/topology/thread_siblings", id);
101    bitmask_from_hex_file(path)
102}
103
104fn get_physical_package_id(id: usize) -> Result<usize, &'static str> {
105    let path = format!("/sys/devices/system/cpu/cpu{}/topology/physical_package_id",
106                       id);
107    usize_from_file(path)
108}