From f56c32b46bdd4c634b1ce6136ecab3c88d202040 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Sun, 14 Feb 2021 08:57:05 +0000 Subject: [PATCH] util: add possible_cpus() --- aya/src/util.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/aya/src/util.rs b/aya/src/util.rs index 0d004dd0..ca651395 100644 --- a/aya/src/util.rs +++ b/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, 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, io::Error> { }) } -fn parse_online_cpus(data: &str) -> Result, ()> { +pub fn possible_cpus() -> Result, 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, ()> { 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()); } }