cpu_utils/
cpusets.rs

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
use std::str::FromStr;

use ccp_shared::types::CPUIdType;
use ccp_shared::types::LogicalCoreId;

pub const LINUX_CPUSET_CPUS_PATH: &str = "/sys/fs/cgroup/cpuset.cpus";

#[derive(Debug, thiserror::Error, PartialEq)]
pub enum CpuListParserError {
    #[error("invalid CPU list {list:?}: {error}")]
    InvalidNumber {
        list: String,
        error: <CPUIdType as FromStr>::Err,
    },
}

pub fn read_cpu_list_string() -> std::io::Result<String> {
    std::fs::read_to_string(LINUX_CPUSET_CPUS_PATH)
}

pub fn parse_cpu_list(content: &str) -> Result<Vec<LogicalCoreId>, CpuListParserError> {
    if content.is_empty() {
        return Ok(vec![]);
    }
    let ranges = content
        .split(',')
        .map(|group| match group.split_once('-') {
            Some((a, b)) => a
                .trim()
                .parse::<CPUIdType>()
                .and_then(|a| b.trim().parse().map(move |b| (a..=b))),
            None => group.trim().parse().map(|a| (a..=a)),
        })
        .collect::<Result<Vec<_>, _>>()
        .map_err(|error| CpuListParserError::InvalidNumber {
            list: content.to_owned(),
            error,
        })?;

    let mut cpu_ids: Vec<_> = ranges
        .into_iter()
        .flatten()
        .map(LogicalCoreId::new)
        .collect();

    // Make output consistent and remove possible duplicates.
    cpu_ids.sort_unstable();
    cpu_ids.dedup();

    Ok(cpu_ids)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parser() {
        let list = parse_cpu_list("4,0-2,6-8,1").unwrap();
        assert_eq!(
            list,
            vec![
                LogicalCoreId::new(0),
                LogicalCoreId::new(1),
                LogicalCoreId::new(2),
                LogicalCoreId::new(4),
                LogicalCoreId::new(6),
                LogicalCoreId::new(7),
                LogicalCoreId::new(8),
            ]
        );
    }

    #[test]
    fn test_parser_spaces() {
        let list = parse_cpu_list(" 4 , 0 - 2 , 6-8, 1  ").unwrap();
        assert_eq!(
            list,
            vec![
                LogicalCoreId::new(0),
                LogicalCoreId::new(1),
                LogicalCoreId::new(2),
                LogicalCoreId::new(4),
                LogicalCoreId::new(6),
                LogicalCoreId::new(7),
                LogicalCoreId::new(8),
            ]
        );
    }

    #[test]
    fn test_empty() {
        let list = parse_cpu_list("").unwrap();
        assert!(list.is_empty());
    }

    #[test]
    fn test_invalid_1() {
        let content = "1-a";
        let err = parse_cpu_list(content).unwrap_err();
        let expected_err = "a".parse::<CPUIdType>().unwrap_err();
        assert_eq!(
            err,
            CpuListParserError::InvalidNumber {
                list: content.to_owned(),
                error: expected_err
            }
        );
    }

    #[test]
    fn test_invalid_empty_range_1() {
        let content = "3,";
        let err = parse_cpu_list(content).unwrap_err();
        let expected_err = "".parse::<CPUIdType>().unwrap_err();
        assert_eq!(
            err,
            CpuListParserError::InvalidNumber {
                list: content.to_owned(),
                error: expected_err
            }
        );
    }

    #[test]
    fn test_invalid_empty_range_2() {
        let content = ",3";
        let err = parse_cpu_list(content).unwrap_err();
        let expected_err = "".parse::<CPUIdType>().unwrap_err();
        assert_eq!(
            err,
            CpuListParserError::InvalidNumber {
                list: content.to_owned(),
                error: expected_err
            }
        );
    }
}