Browse Source

util: add possible_cpus()

Alessandro Decina 4 years ago
parent
commit
f56c32b46b
1 changed files with 22 additions and 11 deletions
  1. 22 11
      aya/src/util.rs

+ 22 - 11
aya/src/util.rs

@@ -1,10 +1,11 @@
 use std::{fs, io, str::FromStr};
 
 const ONLINE_CPUS: &str = "/sys/devices/system/cpu/online";
+const POSSIBLE_CPUS: &str = "/sys/devices/system/cpu/possible";
 
 pub fn online_cpus() -> Result<Vec<u32>, io::Error> {
     let data = fs::read_to_string(ONLINE_CPUS)?;
-    parse_online_cpus(data.trim()).map_err(|_| {
+    parse_cpu_ranges(data.trim()).map_err(|_| {
         io::Error::new(
             io::ErrorKind::Other,
             format!("unexpected {} format", ONLINE_CPUS),
@@ -12,7 +13,17 @@ pub fn online_cpus() -> Result<Vec<u32>, io::Error> {
     })
 }
 
-fn parse_online_cpus(data: &str) -> Result<Vec<u32>, ()> {
+pub fn possible_cpus() -> Result<Vec<u32>, io::Error> {
+    let data = fs::read_to_string(POSSIBLE_CPUS)?;
+    parse_cpu_ranges(data.trim()).map_err(|_| {
+        io::Error::new(
+            io::ErrorKind::Other,
+            format!("unexpected {} format", POSSIBLE_CPUS),
+        )
+    })
+}
+
+fn parse_cpu_ranges(data: &str) -> Result<Vec<u32>, ()> {
     let mut cpus = Vec::new();
     for range in data.split(',') {
         cpus.extend({
@@ -41,14 +52,14 @@ mod tests {
 
     #[test]
     fn test_parse_online_cpus() {
-        assert_eq!(parse_online_cpus("0").unwrap(), vec![0]);
-        assert_eq!(parse_online_cpus("0,1").unwrap(), vec![0, 1]);
-        assert_eq!(parse_online_cpus("0,1,2").unwrap(), vec![0, 1, 2]);
-        assert_eq!(parse_online_cpus("0-7").unwrap(), Vec::from_iter(0..=7));
-        assert_eq!(parse_online_cpus("0-3,4-7").unwrap(), Vec::from_iter(0..=7));
-        assert_eq!(parse_online_cpus("0-5,6,7").unwrap(), Vec::from_iter(0..=7));
-        assert!(parse_online_cpus("").is_err());
-        assert!(parse_online_cpus("0-1,2-").is_err());
-        assert!(parse_online_cpus("foo").is_err());
+        assert_eq!(parse_cpu_ranges("0").unwrap(), vec![0]);
+        assert_eq!(parse_cpu_ranges("0,1").unwrap(), vec![0, 1]);
+        assert_eq!(parse_cpu_ranges("0,1,2").unwrap(), vec![0, 1, 2]);
+        assert_eq!(parse_cpu_ranges("0-7").unwrap(), Vec::from_iter(0..=7));
+        assert_eq!(parse_cpu_ranges("0-3,4-7").unwrap(), Vec::from_iter(0..=7));
+        assert_eq!(parse_cpu_ranges("0-5,6,7").unwrap(), Vec::from_iter(0..=7));
+        assert!(parse_cpu_ranges("").is_err());
+        assert!(parse_cpu_ranges("0-1,2-").is_err());
+        assert!(parse_cpu_ranges("foo").is_err());
     }
 }