mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(ext/node): stub cpu_info() for OpenBSD (#25807)
Add an implementation of cpu_info() for OpenBSD, that returns a correctly-sized array. Since Rust's libc bindings for OpenBSD do not contain all symbols necessary for a full implementation and it is not planned to add them, this solution at least avoids problems with code that relies on cpu_info() purely for the size of the returned array to derive the number of available CPUs. This addresses https://github.com/denoland/deno/issues/25621
This commit is contained in:
parent
ef3e4a8f74
commit
37cedefb4d
1 changed files with 48 additions and 0 deletions
|
@ -294,6 +294,54 @@ pub fn cpu_info() -> Option<Vec<CpuInfo>> {
|
||||||
Some(cpus)
|
Some(cpus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "openbsd")]
|
||||||
|
pub fn cpu_info() -> Option<Vec<CpuInfo>> {
|
||||||
|
// Stub implementation for OpenBSD that returns an array of the correct size
|
||||||
|
// but with dummy values.
|
||||||
|
// Rust's OpenBSD libc bindings don't contain all the symbols needed for a
|
||||||
|
// full implementation, and including them is not planned.
|
||||||
|
let mut mib = [libc::CTL_HW, libc::HW_NCPUONLINE];
|
||||||
|
|
||||||
|
// SAFETY: Assumes correct behavior of platform-specific
|
||||||
|
// sysctls and data structures. Relies on specific sysctl
|
||||||
|
// names and parameter existence.
|
||||||
|
unsafe {
|
||||||
|
let mut ncpu: libc::c_uint = 0;
|
||||||
|
let mut size = std::mem::size_of_val(&ncpu) as libc::size_t;
|
||||||
|
|
||||||
|
// Get number of CPUs online
|
||||||
|
let res = libc::sysctl(
|
||||||
|
mib.as_mut_ptr(),
|
||||||
|
mib.len() as _,
|
||||||
|
&mut ncpu as *mut _ as *mut _,
|
||||||
|
&mut size,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
// If res == 0, the sysctl call was succesful and
|
||||||
|
// ncpuonline contains the number of online CPUs.
|
||||||
|
if res != 0 {
|
||||||
|
return None;
|
||||||
|
} else {
|
||||||
|
let mut cpus = vec![CpuInfo::new(); ncpu as usize];
|
||||||
|
|
||||||
|
for (_, cpu) in cpus.iter_mut().enumerate() {
|
||||||
|
cpu.model = "Undisclosed CPU".to_string();
|
||||||
|
// Return 1 as a dummy value so the tests won't
|
||||||
|
// fail.
|
||||||
|
cpu.speed = 1;
|
||||||
|
cpu.times.user = 1;
|
||||||
|
cpu.times.nice = 1;
|
||||||
|
cpu.times.sys = 1;
|
||||||
|
cpu.times.idle = 1;
|
||||||
|
cpu.times.irq = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some(cpus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Reference in a new issue