Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/base/src/sys/windows/system_info.rs
5394 views
1
// Copyright 2022 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 std::mem::MaybeUninit;
6
use std::sync::LazyLock;
7
8
use winapi::um::processthreadsapi::GetCurrentProcessId;
9
use winapi::um::sysinfoapi::GetNativeSystemInfo;
10
use winapi::um::sysinfoapi::SYSTEM_INFO;
11
12
use super::Pid;
13
use crate::Result;
14
15
struct SystemInfo {
16
pagesize: usize,
17
number_of_logical_cores: usize,
18
number_of_online_cores: usize,
19
allocation_granularity: u64,
20
}
21
22
static SYSTEM_INFO: LazyLock<SystemInfo> = LazyLock::new(|| {
23
// SAFETY:
24
// Safe because this is a universally available call on modern Windows systems.
25
let sysinfo = unsafe {
26
let mut sysinfo = MaybeUninit::<SYSTEM_INFO>::uninit();
27
GetNativeSystemInfo(sysinfo.as_mut_ptr());
28
sysinfo.assume_init()
29
};
30
31
SystemInfo {
32
pagesize: sysinfo.dwPageSize as usize,
33
number_of_logical_cores: sysinfo.dwNumberOfProcessors as usize,
34
// For Windows, the concept of "online" cores doesn't exist in the same way as in Linux.
35
// Cores can be considered "parked", invisible/inaccessible to the VM, disabled, or unused;
36
// but it's technically different from online vs offline. Assume all cores are online for
37
// simplicity.
38
number_of_online_cores: sysinfo.dwNumberOfProcessors as usize,
39
allocation_granularity: sysinfo.dwAllocationGranularity.into(),
40
}
41
});
42
43
/// Returns the system page size in bytes.
44
pub fn pagesize() -> usize {
45
SYSTEM_INFO.pagesize
46
}
47
48
/// Returns the number of logical cores on the system.
49
pub fn number_of_logical_cores() -> Result<usize> {
50
Ok(SYSTEM_INFO.number_of_logical_cores)
51
}
52
53
/// Returns the number of online cores on the system.
54
pub fn number_of_online_cores() -> Result<usize> {
55
Ok(SYSTEM_INFO.number_of_online_cores)
56
}
57
58
/// Returns the minimum memory allocation granularity in bytes.
59
pub fn allocation_granularity() -> u64 {
60
SYSTEM_INFO.allocation_granularity
61
}
62
63
/// Cross-platform wrapper around getting the current process id.
64
#[inline(always)]
65
pub fn getpid() -> Pid {
66
// SAFETY:
67
// Safe because we only use the return value.
68
unsafe { GetCurrentProcessId() }
69
}
70
71
/// Set the name of the thread.
72
pub fn set_thread_name(_name: &str) -> Result<()> {
73
todo!();
74
}
75
76
/// Returns a bool if the CPU is online, or an error if there was an issue reading the system
77
/// properties.
78
pub fn is_cpu_online(_cpu_id: usize) -> Result<bool> {
79
// See SYSTEM_INFO.number_of_online_cores for more context.
80
Ok(true)
81
}
82
83