pub fn detect_host_architecture_only() -> &'static str {
#[cfg(target_arch = "x86_64")]
return "x86_64";
#[cfg(target_arch = "aarch64")]
return "aarch64";
#[cfg(target_arch = "wasm32")]
return "wasm32";
#[cfg(target_arch = "wasm64")]
return "wasm64";
#[cfg(target_arch = "riscv32")]
return "riscv32";
#[cfg(target_arch = "riscv64")]
return "riscv64";
#[allow(unreachable_code)]
"x86_64"
}
pub fn detect_host_os() -> &'static str {
#[cfg(target_os = "linux")]
return "linux";
#[cfg(target_os = "macos")]
return "macos";
#[cfg(target_os = "windows")]
return "windows";
#[cfg(target_os = "freebsd")]
return "freebsd";
#[cfg(target_os = "openbsd")]
return "openbsd";
#[cfg(target_os = "netbsd")]
return "netbsd";
#[cfg(target_os = "dragonfly")]
return "dragonfly";
#[cfg(target_os = "redox")]
return "redox";
#[allow(unreachable_code)]
"unknown"
}
#[deprecated(since = "0.0.8", note = "Use `detect_host().to_str()` instead")]
pub fn detect_host_architecture() -> &'static str {
let arch = detect_host_architecture_only();
let os = detect_host_os();
match (arch, os) {
("x86_64", "linux") => "x86_64_linux",
("x86_64", "macos") => "x86_64_macos",
("x86_64", "windows") => "x86_64_windows",
("aarch64", "linux") => "aarch64_linux",
("aarch64", "macos") => "aarch64_macos",
("aarch64", "windows") => "aarch64_windows",
("wasm32", _) => "wasm32_unknown",
("wasm64", _) => "wasm64_unknown",
("riscv32", _) => "riscv32_unknown",
("riscv64", _) => "riscv64_unknown",
#[cfg(feature = "nightly")]
("riscv128", _) => "riscv128_unknown",
_ => {
match arch {
"x86_64" => "x86_64_unknown",
"aarch64" => "aarch64_unknown",
_ => "x86_64_unknown",
}
}
}
}
pub fn cpu_count() -> usize {
#[cfg(target_os = "linux")]
{
if let Ok(content) = std::fs::read_to_string("/proc/cpuinfo") {
let count = content
.lines()
.filter(|line| line.starts_with("processor"))
.count();
if count > 0 {
return count;
}
}
unsafe {
unsafe extern "C" {
fn sysconf(name: i32) -> i64;
}
const _SC_NPROCESSORS_ONLN: i32 = 84;
let count = sysconf(_SC_NPROCESSORS_ONLN);
if count > 0 {
return count as usize;
}
}
}
#[cfg(target_os = "macos")]
{
use std::ffi::CString;
unsafe {
unsafe extern "C" {
fn sysctlbyname(
name: *const i8,
oldp: *mut std::ffi::c_void,
oldlenp: *mut usize,
newp: *const std::ffi::c_void,
newlen: usize,
) -> i32;
}
#[allow(clippy::unwrap_used)]
let name = CString::new("hw.ncpu").unwrap();
let mut count: u32 = 0;
let mut size = std::mem::size_of::<u32>();
if sysctlbyname(
name.as_ptr(),
&mut count as *mut _ as *mut std::ffi::c_void,
&mut size,
std::ptr::null(),
0,
) == 0
{
return count as usize;
}
}
}
#[cfg(target_os = "windows")]
{
unsafe {
unsafe extern "system" {
fn GetSystemInfo(lpSystemInfo: *mut SystemInfo);
}
#[repr(C)]
struct SystemInfo {
processor_architecture: u16,
reserved: u16,
page_size: u32,
minimum_application_address: *mut std::ffi::c_void,
maximum_application_address: *mut std::ffi::c_void,
active_processor_mask: *mut u32,
number_of_processors: u32,
processor_type: u32,
allocation_granularity: u32,
processor_level: u16,
processor_revision: u16,
}
let mut info = std::mem::zeroed::<SystemInfo>();
GetSystemInfo(&mut info);
info.number_of_processors as usize
}
}
#[cfg(not(target_os = "windows"))]
{
1
}
}