system_info/unix/linux/
cpu.rs

1//! CPU information.
2
3use core::mem;
4
5fn count_with_affinity() -> usize {
6    let mut count = 0;
7    let mut set = mem::MaybeUninit::<libc::cpu_set_t>::uninit();
8    let result = unsafe {
9        libc::sched_getaffinity(0, mem::size_of_val(&set), set.as_mut_ptr())
10    };
11
12    if result == 0 {
13        let set = unsafe {
14            set.assume_init()
15        };
16
17        for idx in 0..libc::CPU_SETSIZE as usize {
18            if unsafe { libc::CPU_ISSET(idx, &set) } {
19                count += 1
20            }
21        }
22    }
23
24    count
25}
26
27///Returns number of CPU cores on system, as reported by OS.
28pub fn count() -> usize {
29    match count_with_affinity() {
30        0 => crate::unix::posix::cpu::count(),
31        count => count,
32    }
33}