#![doc = include_str!("README.md")]
use core_affinity;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CoreId {
pub id: usize,
internal: usize,
}
pub fn get_core_ids() -> Vec<CoreId> {
let internal_ids = core_affinity::get_core_ids().unwrap_or_default();
internal_ids
.into_iter()
.map(|c| CoreId {
id: c.id,
internal: c.id,
})
.collect()
}
pub fn pin_to_core(core_id: CoreId) -> bool {
let internal_core = core_affinity::CoreId {
id: core_id.internal,
};
core_affinity::set_for_current(internal_core)
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
#[test]
fn test_can_get_cores() {
let cores = get_core_ids();
assert!(!cores.is_empty(), "Should find atleast one core");
println!("Found {} cores", cores.len());
}
#[test]
fn test_pinning() {
let cores = get_core_ids();
if let Some(core) = cores.first() {
let core = *core;
let handle = thread::spawn(move || {
let success = pin_to_core(core);
if !success {
eprintln!("WARNING: Failed to pin thread (common in containers/CI)")
} else {
println!("Successfully pinned to core {}", core.id);
}
let mut x = 0;
for _ in 0..1000 {
x += 1;
}
x
});
assert_eq!(handle.join().unwrap(), 1000);
}
}
}