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
use std::fs::read;
use std::io::Error;
use std::str::FromStr;
const SYS_CPU_ONLINE: &str = "/sys/devices/system/cpu/online";
pub type CpuId = i32;
pub fn get_online() -> Result<Vec<CpuId>, Error> {
let cpus = unsafe { String::from_utf8_unchecked(read(SYS_CPU_ONLINE)?) };
Ok(list_from_string(&cpus.trim()))
}
fn list_from_string(cpus: &str) -> Vec<CpuId> {
let cpu_list = cpus.split(',').flat_map(|group| {
let mut split = group.split('-');
let start = split.next();
let end = split.next();
if let (Some(start), None) = (start, end) {
let cpuid = CpuId::from_str(start).unwrap();
return cpuid..=cpuid;
}
let start = CpuId::from_str(start.unwrap()).unwrap();
let end = CpuId::from_str(end.unwrap()).unwrap();
(start..=end)
});
cpu_list.collect()
}
mod test {
#[test]
fn test() {
use crate::cpus::list_from_string;
assert_eq!(list_from_string("0"), vec![0]);
assert_eq!(list_from_string("0-4"), vec![0, 1, 2, 3, 4]);
assert_eq!(list_from_string("0-2,5-6"), vec![0, 1, 2, 5, 6]);
}
}