use std::sync::OnceLock;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Vendor {
Intel,
Amd,
Other,
}
#[derive(Copy, Clone, Debug)]
pub struct CpuInfo {
pub logical_threads: u32,
pub smt_threads_per_core: u8,
pub smt_threads_log2: u8,
pub physical_cores: u32,
pub vendor: Vendor,
pub family: u32,
pub model: u32,
pub stepping: u8,
}
impl CpuInfo {
fn detect() -> Self {
let logical = std::thread::available_parallelism()
.map(std::num::NonZeroUsize::get)
.unwrap_or(1) as u32;
let smt = detect_smt_factor();
let physical = (logical / smt as u32).max(1);
let smt_log2 = if smt >= 2 { 1 } else { 0 };
let (vendor, family, model, stepping) = detect_vendor_family_model();
Self {
logical_threads: logical,
smt_threads_per_core: smt,
smt_threads_log2: smt_log2,
physical_cores: physical,
vendor,
family,
model,
stepping,
}
}
}
#[cfg(target_arch = "x86_64")]
fn detect_vendor_family_model() -> (Vendor, u32, u32, u8) {
use std::arch::x86_64::__cpuid;
let leaf0 = __cpuid(0);
let mut vendor_bytes = [0u8; 12];
vendor_bytes[0..4].copy_from_slice(&leaf0.ebx.to_le_bytes());
vendor_bytes[4..8].copy_from_slice(&leaf0.edx.to_le_bytes());
vendor_bytes[8..12].copy_from_slice(&leaf0.ecx.to_le_bytes());
let vendor = match &vendor_bytes {
b"GenuineIntel" => Vendor::Intel,
b"AuthenticAMD" => Vendor::Amd,
_ => Vendor::Other,
};
if leaf0.eax < 1 {
return (vendor, 0, 0, 0);
}
let leaf1 = __cpuid(1);
let eax = leaf1.eax;
let base_family = (eax >> 8) & 0xF;
let ext_family = (eax >> 20) & 0xFF;
let base_model = (eax >> 4) & 0xF;
let ext_model = (eax >> 16) & 0xF;
let stepping = (eax & 0xF) as u8;
let family = if base_family == 0xF {
base_family + ext_family
} else {
base_family
};
let model = if base_family == 0xF || base_family == 0x6 {
(ext_model << 4) | base_model
} else {
base_model
};
(vendor, family, model, stepping)
}
#[cfg(not(target_arch = "x86_64"))]
fn detect_vendor_family_model() -> (Vendor, u32, u32, u8) {
(Vendor::Other, 0, 0, 0)
}
pub fn cpu_info() -> &'static CpuInfo {
static CACHE: OnceLock<CpuInfo> = OnceLock::new();
CACHE.get_or_init(CpuInfo::detect)
}
#[inline]
pub fn small_host_dispatch_factor() -> u64 {
dispatch_factor_for_cores(cpu_info().physical_cores)
}
#[inline]
pub(crate) fn dispatch_factor_for_cores(physical_cores: u32) -> u64 {
if physical_cores < 4 { 4 } else { 1 }
}
#[cfg(target_arch = "x86_64")]
fn detect_smt_factor() -> u8 {
use std::arch::x86_64::__cpuid;
let r = __cpuid(1);
let htt = (r.edx >> 28) & 1;
if htt == 1 { 2 } else { 1 }
}
#[cfg(not(target_arch = "x86_64"))]
fn detect_smt_factor() -> u8 {
1
}
#[cfg(target_arch = "x86_64")]
pub fn has_monitorx() -> bool {
*monitorx_available()
}
#[cfg(not(target_arch = "x86_64"))]
pub fn has_monitorx() -> bool {
false
}
#[cfg(target_arch = "x86_64")]
fn monitorx_available() -> &'static bool {
static CACHE: OnceLock<bool> = OnceLock::new();
CACHE.get_or_init(|| {
use std::arch::x86_64::__cpuid;
let max_ext = __cpuid(0x8000_0000).eax;
if max_ext < 0x8000_0001 {
return false;
}
(__cpuid(0x8000_0001).ecx >> 29) & 1 == 1
})
}
#[cfg(target_arch = "x86_64")]
pub fn has_waitpkg() -> bool {
*waitpkg_available()
}
#[cfg(not(target_arch = "x86_64"))]
pub fn has_waitpkg() -> bool {
false
}
#[cfg(target_arch = "x86_64")]
fn waitpkg_available() -> &'static bool {
static CACHE: OnceLock<bool> = OnceLock::new();
CACHE.get_or_init(|| {
use std::arch::x86_64::{__cpuid, __cpuid_count};
let max_leaf = __cpuid(0).eax;
if max_leaf < 7 {
return false;
}
let r = __cpuid_count(7, 0);
(r.ecx >> 5) & 1 == 1
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cpu_info_is_cached() {
let a = cpu_info() as *const _;
let b = cpu_info() as *const _;
assert_eq!(a, b);
}
#[test]
fn the_monitor_wait_detections_are_cached_and_agree_with_themselves() {
assert_eq!(has_monitorx(), has_monitorx());
assert_eq!(has_waitpkg(), has_waitpkg());
}
#[cfg(target_arch = "x86_64")]
#[test]
fn monitorx_is_never_claimed_without_the_extended_leaf_that_carries_it() {
use std::arch::x86_64::__cpuid;
let max_ext = __cpuid(0x8000_0000).eax;
if max_ext < 0x8000_0001 {
assert!(
!has_monitorx(),
"claimed MONITORX while the extended leaves stop at {max_ext:#x}"
);
}
}
#[cfg(target_arch = "x86_64")]
#[test]
fn monitorx_matches_a_second_independent_read_of_the_bit() {
use std::arch::x86_64::__cpuid;
let expected = __cpuid(0x8000_0000).eax >= 0x8000_0001
&& (__cpuid(0x8000_0001).ecx >> 29) & 1 == 1;
assert_eq!(has_monitorx(), expected);
}
#[test]
fn detected_values_are_sane() {
let info = cpu_info();
assert!(info.logical_threads >= 1);
assert!(info.physical_cores >= 1);
assert!(info.physical_cores <= info.logical_threads);
assert!(matches!(info.smt_threads_per_core, 1 | 2));
assert_eq!(
info.physical_cores * info.smt_threads_per_core as u32,
info.logical_threads,
"physical * smt should reconstruct logical"
);
assert_eq!(info.smt_threads_log2, if info.smt_threads_per_core >= 2 { 1 } else { 0 });
}
}