-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rather than have a catch-all module, split it up. Added some more rustdocs, and changed the signature and tests for CPU range parsing to not do any io anymore. Test Plan ========= CI
- Loading branch information
1 parent
0f063de
commit a2280fe
Showing
3 changed files
with
82 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{fs::File, io::Read}; | ||
|
||
use anyhow::{Context, Error}; | ||
|
||
/// Parse a set of CPU ranges. They can be either a single number of a fully qualified range | ||
/// which is separated to one another with a comma (`,`) and use a dash (`-`) to indicate the | ||
/// start and (inclusive) end of the range. | ||
fn _read_cpu_range(ranges: &str) -> Result<Vec<u32>, Error> { | ||
let mut cpus = vec![]; | ||
|
||
for cpu_range in ranges.split(',') { | ||
let rangeop_result = cpu_range.find('-'); | ||
match rangeop_result { | ||
None => cpus.push( | ||
cpu_range | ||
.trim_end() | ||
.parse::<u32>() | ||
.with_context(|| "Failed to parse lone CPU".to_string())?, | ||
), | ||
Some(index) => { | ||
let start = cpu_range[..index] | ||
.trim_end() | ||
.parse::<u32>() | ||
.with_context(|| "Failed to parse starting CPU".to_string())?; | ||
let end = cpu_range[index + 1..] | ||
.trim_end() | ||
.parse::<u32>() | ||
.with_context(|| "Failed to parse ending CPU".to_string())?; | ||
cpus.extend(start..end + 1); | ||
} | ||
} | ||
} | ||
|
||
Ok(cpus) | ||
} | ||
|
||
/// Parses `/sys/devices/system/cpu/online` and returns the online CPUs in the system. | ||
pub fn get_online_cpus() -> Result<Vec<u32>, Error> { | ||
let mut file = File::open("/sys/devices/system/cpu/online")?; | ||
let mut ranges = String::new(); | ||
file.read_to_string(&mut ranges)?; | ||
|
||
_read_cpu_range(&ranges) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn cpu_ranges_to_list() { | ||
let cpus = _read_cpu_range("0").unwrap(); | ||
assert_eq!(cpus, vec![0]); | ||
|
||
let cpus = _read_cpu_range("0-7").unwrap(); | ||
assert_eq!(cpus, (0..=7).collect::<Vec<_>>()); | ||
|
||
let cpus = _read_cpu_range("0-7,16-23").unwrap(); | ||
let expected = (0..=7).chain(16..=23).collect::<Vec<_>>(); | ||
assert_eq!(cpus, expected); | ||
|
||
let cpus = _read_cpu_range("0-1,3,7-9,48,49").unwrap(); | ||
assert_eq!( | ||
cpus, | ||
(0..=1) | ||
.chain(3..=3) | ||
.chain(7..=9) | ||
.chain(48..=48) | ||
.chain(49..=49) | ||
.collect::<Vec<_>>() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod cpu; | ||
mod lpm; | ||
|
||
pub use cpu::get_online_cpus; | ||
pub use lpm::summarize_address_range; | ||
pub use lpm::AddressBlockRange; |