1
0
Fork 0
forked from wry/wry
wry/src/utils/num_cpus.rs
2024-09-06 11:08:22 +02:00

21 lines
608 B
Rust

use {
crate::utils::oserror::OsError,
smallvec::{smallvec_inline, SmallVec},
uapi::{c, Errno},
};
#[cfg_attr(not(feature = "it"), expect(dead_code))]
pub fn num_cpus() -> Result<u32, OsError> {
let mut buf: SmallVec<[usize; 32]> = smallvec_inline![0; 32];
loop {
match uapi::sched_getaffinity(0, &mut buf) {
Ok(_) => return Ok(count(&buf)),
Err(Errno(c::EINVAL)) => buf.extend_from_slice(&[0; 32][..]),
Err(e) => return Err(e.into()),
}
}
}
fn count(buf: &[usize]) -> u32 {
buf.iter().copied().map(|n| n.count_ones()).sum()
}