Skip to content

Commit

Permalink
scx_utils: expose possible and online cpus from Topology
Browse files Browse the repository at this point in the history
This exposes possible and online CPUs to schedulers.

Related to #721.
  • Loading branch information
evanrittenhouse committed Dec 27, 2024
1 parent b8e467b commit 13f31a4
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions rust/scx_utils/src/topology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ pub struct Topology {
pub all_llcs: BTreeMap<usize, Arc<Llc>>,
pub all_cores: BTreeMap<usize, Arc<Core>>,
pub all_cpus: BTreeMap<usize, Arc<Cpu>>,

pub online_cpus: Cpumask,
pub possible_cpus: Cpumask,
}

impl Topology {
Expand Down Expand Up @@ -233,12 +236,17 @@ impl Topology {
node.all_cpus = node_cpus;
}

let online_cpus = cpus_online()?;
let possible_cpus = cpus_possible()?;

Ok(Topology {
nodes,
span,
all_llcs: topo_llcs,
all_cores: topo_cores,
all_cpus: topo_cpus,
online_cpus,
possible_cpus,
})
}

Expand Down Expand Up @@ -339,17 +347,24 @@ impl TopoCtx {
}

fn cpus_online() -> Result<Cpumask> {
let path = "/sys/devices/system/cpu/online";
let online = std::fs::read_to_string(&path)?;
let online_groups: Vec<&str> = online.split(',').collect();
parse_cpus("/sys/devices/system/cpu/online")
}

fn cpus_possible() -> Result<Cpumask> {
parse_cpus("/sys/devices/system/cpu/possible")
}

fn parse_cpus(path: &str) -> Result<Cpumask> {
let cpus = std::fs::read_to_string(&path)?;
let cpus: Vec<&str> = cpus.split(',').collect();
let mut mask = Cpumask::new();
for group in online_groups.iter() {
for group in cpus.iter() {
let (min, max) = match sscanf!(group.trim(), "{usize}-{usize}") {
Ok((x, y)) => (x, y),
Err(_) => match sscanf!(group.trim(), "{usize}") {
Ok(x) => (x, x),
Err(_) => {
bail!("Failed to parse online cpus {}", group.trim());
bail!("Failed to parse cpus from {}: {}", path, group.trim());
}
},
};
Expand Down

0 comments on commit 13f31a4

Please sign in to comment.