1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! Gets the number of cpus of the machine.
15 //!
16 //! Currently this crate supports two platform: `linux` and `windows`
17 
18 use std::os::raw::c_long;
19 
20 #[cfg(target_family = "unix")]
21 pub mod unix;
22 
23 #[cfg(target_os = "windows")]
24 pub mod windows;
25 
26 #[cfg(target_family = "unix")]
27 use crate::util::num_cpus::unix::get_cpu_num_online;
28 #[cfg(target_os = "windows")]
29 use crate::util::num_cpus::windows::get_cpu_num_online;
30 
31 /// The get_cpu_num function is the external interface, which will automatically
32 /// call the underlying functions for different operating systems. Linux, using
33 /// sysconf() function, which gets the number of cpu cores in the available
34 /// state by default. Windows, using GetSystemInfo() function, which gets the
35 /// number of cpu cores in the available state by default. # Example
36 ///
37 /// ```no run
38 /// use ylong_runtime::util::num_cpus;
39 ///
40 /// let cpus = num_cpus::get_cpu_num();
41 /// ```
get_cpu_num() -> c_long42 pub fn get_cpu_num() -> c_long {
43     get_cpu_num_online()
44 }
45 
46 #[cfg(test)]
47 mod test {
48     use super::*;
49 
50     /// UT test cases for num_cpus.
51     ///
52     /// # Brief
53     /// 1. call get_cpu_num and check it greater than zero
54     #[test]
ut_num_cpus_test()55     fn ut_num_cpus_test() {
56         let cpus = get_cpu_num();
57         assert!(cpus > 0);
58     }
59 }
60