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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::{FileWrapper, ProcResult};
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, io::Read};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct CpuInfo {
pub fields: HashMap<String, String>,
pub cpus: Vec<HashMap<String, String>>,
}
impl CpuInfo {
pub fn from_reader<R: Read>(r: R) -> ProcResult<CpuInfo> {
use std::io::{BufRead, BufReader};
let reader = BufReader::new(r);
let mut list = Vec::new();
let mut map = Some(HashMap::new());
let mut found_first = false;
for line in reader.lines().flatten() {
if !line.is_empty() {
let mut s = line.split(':');
let key = expect!(s.next());
if !found_first && key.trim() == "processor" {
found_first = true;
}
if !found_first {
continue;
}
if let Some(value) = s.next() {
let key = key.trim().to_owned();
let value = value.trim().to_owned();
map.get_or_insert(HashMap::new()).insert(key, value);
}
} else if let Some(map) = map.take() {
list.push(map);
found_first = false;
}
}
if let Some(map) = map.take() {
list.push(map);
}
assert!(!list.is_empty());
let common_fields: Vec<String> = list[0]
.iter()
.filter_map(|(key, val)| {
if list.iter().all(|map| map.get(key).map_or(false, |v| v == val)) {
Some(key.clone())
} else {
None
}
})
.collect();
let mut common_map = HashMap::new();
for (k, v) in &list[0] {
if common_fields.contains(k) {
common_map.insert(k.clone(), v.clone());
}
}
for map in &mut list {
map.retain(|k, _| !common_fields.contains(k));
}
Ok(CpuInfo {
fields: common_map,
cpus: list,
})
}
pub fn new() -> ProcResult<CpuInfo> {
let file = FileWrapper::open("/proc/cpuinfo")?;
CpuInfo::from_reader(file)
}
pub fn num_cores(&self) -> usize {
self.cpus.len()
}
pub fn get_info(&self, cpu_num: usize) -> Option<HashMap<&str, &str>> {
self.cpus.get(cpu_num).map(|info| {
self.fields
.iter()
.chain(info.iter())
.map(|(k, v)| (k.as_ref(), v.as_ref()))
.collect()
})
}
pub fn get_field(&self, cpu_num: usize, field_name: &str) -> Option<&str> {
self.cpus.get(cpu_num).and_then(|cpu_fields| {
cpu_fields
.get(field_name)
.or_else(|| self.fields.get(field_name))
.map(|s| s.as_ref())
})
}
pub fn model_name(&self, cpu_num: usize) -> Option<&str> {
self.get_field(cpu_num, "model name")
}
pub fn vendor_id(&self, cpu_num: usize) -> Option<&str> {
self.get_field(cpu_num, "vendor_id")
}
pub fn physical_id(&self, cpu_num: usize) -> Option<u32> {
self.get_field(cpu_num, "physical id").and_then(|s| s.parse().ok())
}
pub fn flags(&self, cpu_num: usize) -> Option<Vec<&str>> {
self.get_field(cpu_num, "flags")
.map(|flags| flags.split_whitespace().collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cpuinfo() {
let info = CpuInfo::new().unwrap();
println!("{:#?}", info.flags(0));
for num in 0..info.num_cores() {
info.model_name(num).unwrap();
info.vendor_id(num).unwrap();
info.physical_id(num);
}
}
#[test]
fn test_cpuinfo_rpi() {
let data = r#"processor : 0
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 1
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 2
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 3
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
Hardware : BCM2835
Revision : a020d3
Serial : 0000000012345678
Model : Raspberry Pi 3 Model B Plus Rev 1.3
"#;
let r = std::io::Cursor::new(data.as_bytes());
let info = CpuInfo::from_reader(r).unwrap();
assert_eq!(info.num_cores(), 4);
let info = info.get_info(0).unwrap();
assert!(info.get("model name").is_some());
assert!(info.get("BogoMIPS").is_some());
assert!(info.get("Features").is_some());
assert!(info.get("CPU implementer").is_some());
assert!(info.get("CPU architecture").is_some());
assert!(info.get("CPU variant").is_some());
assert!(info.get("CPU part").is_some());
assert!(info.get("CPU revision").is_some());
}
}