xref: /aosp_15_r20/external/crosvm/base/src/sys/unix/system_info.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use libc::sysconf;
6 use libc::_SC_IOV_MAX;
7 use libc::_SC_NPROCESSORS_CONF;
8 use libc::_SC_PAGESIZE;
9 
10 use crate::Result;
11 
12 /// Safe wrapper for `sysconf(_SC_IOV_MAX)`.
13 #[inline(always)]
iov_max() -> usize14 pub fn iov_max() -> usize {
15     // SAFETY:
16     // Trivially safe
17     unsafe { sysconf(_SC_IOV_MAX) as usize }
18 }
19 
20 /// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
21 #[inline(always)]
pagesize() -> usize22 pub fn pagesize() -> usize {
23     // SAFETY:
24     // Trivially safe
25     unsafe { sysconf(_SC_PAGESIZE) as usize }
26 }
27 
28 /// Returns the number of online logical cores on the system.
29 #[inline(always)]
number_of_logical_cores() -> Result<usize>30 pub fn number_of_logical_cores() -> Result<usize> {
31     // SAFETY:
32     // Safe because we pass a flag for this call and the host supports this system call
33     Ok(unsafe { sysconf(_SC_NPROCESSORS_CONF) } as usize)
34 }
35