use eyre::eyre;
use hwlocality::{
Topology,
cpu::binding::CpuBindingFlags,
object::{TopologyObject, types::ObjectType},
};
fn main() -> eyre::Result<()> {
let topology = Topology::new()?;
let Some(support) = topology.feature_support().cpu_binding() else {
println!("This example requires CPU binding support");
return Ok(());
};
if !(support.get_process() && support.set_process()) {
println!("This example needs support for querying and setting process CPU bindings");
return Ok(());
}
if cfg!(target_os = "windows") {
println!(
"This example currently fails on Windows for unknown reasons, and has been disabled"
);
return Ok(());
}
let pid = std::process::id();
println!("Binding Process with PID {pid:?}");
let cpuset = last_pu(&topology)?
.cpuset()
.ok_or_else(|| eyre!("PU objects should have a CpuSet"))?;
let print_binding_location = |situation: &str| -> eyre::Result<()> {
println!(
"Cpu Binding {situation}: {:?}",
topology.process_cpu_binding(pid, CpuBindingFlags::empty())?
);
if support.get_process_last_cpu_location() {
println!(
"Cpu Location {situation}: {:?}",
topology.last_process_cpu_location(pid, CpuBindingFlags::empty())?
)
}
Ok(())
};
print_binding_location("before binding")?;
topology.bind_process_cpu(pid, cpuset, CpuBindingFlags::empty())?;
print_binding_location("after binding")?;
Ok(())
}
fn last_pu(topology: &Topology) -> eyre::Result<&TopologyObject> {
topology
.objects_with_type(ObjectType::PU)
.next_back()
.ok_or_else(|| eyre!("system should have at least one PU"))
}