Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/base/src/sys/unix/system_info.rs
5394 views
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_NPROCESSORS_ONLN;
9
use libc::_SC_PAGESIZE;
10
11
use crate::errno_result;
12
use crate::Result;
13
14
// Comes from UIO_MAXIOV in "uio.h"
15
const IOV_MAX_DEFAULT: usize = 1024;
16
17
/// Safe wrapper for `sysconf(_SC_IOV_MAX)`.
18
#[inline(always)]
19
pub fn iov_max() -> usize {
20
// SAFETY:
21
// Trivially safe
22
let iov_max = unsafe { sysconf(_SC_IOV_MAX) };
23
if iov_max < 0 {
24
IOV_MAX_DEFAULT
25
} else {
26
iov_max as usize
27
}
28
}
29
30
/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
31
#[inline(always)]
32
pub fn pagesize() -> usize {
33
// SAFETY:
34
// Trivially safe
35
// man sysconf says return value "Must not be less than 1."
36
let pagesize = unsafe { sysconf(_SC_PAGESIZE) };
37
if pagesize <= 0 {
38
panic!("Error reading system page size, got invalid value.");
39
}
40
pagesize as usize
41
}
42
43
/// Returns the number of logical cores on the system.
44
#[inline(always)]
45
pub fn number_of_logical_cores() -> Result<usize> {
46
// SAFETY:
47
// Safe because we pass a flag for this call and the host supports this system call
48
let nprocs_conf = unsafe { sysconf(_SC_NPROCESSORS_CONF) };
49
if nprocs_conf < 0 {
50
errno_result()
51
} else {
52
Ok(nprocs_conf as usize)
53
}
54
}
55
56
/// Returns the number of online cores on the system.
57
#[inline(always)]
58
pub fn number_of_online_cores() -> Result<usize> {
59
// SAFETY:
60
// Safe because we pass a flag for this call and the host supports this system call
61
let nprocs_onln = unsafe { sysconf(_SC_NPROCESSORS_ONLN) };
62
if nprocs_onln < 0 {
63
errno_result()
64
} else {
65
Ok(nprocs_onln as usize)
66
}
67
}
68
69