1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright (c) 2020 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.

pub fn get_num_cpu() -> Result<u16, nc::Errno> {
    let mut set = [0; 128];
    set[0] = 1;

    unsafe {
        nc::sched_getaffinity(0, set.len(), &mut set)?;
    }

    let mut count = 0;
    for item in set.iter() {
        let mut item = *item;
        while item > 0 {
            item &= item - 1;
            count += 1;
        }
    }

    Ok(count)
}

fn main() {
    println!("num of cpu: {}", get_num_cpu().unwrap());
}