use std::sync::OnceLock;
use crate::win_host::PreservedLastError;
use windows_sys::Win32::Foundation::{
ERROR_GEN_FAILURE, ERROR_INSUFFICIENT_BUFFER, ERROR_INVALID_PARAMETER, ERROR_NOACCESS,
};
use windows_sys::Win32::System::Power::{CallNtPowerInformation, ProcessorInformation};
use windows_sys::Win32::System::SystemInformation::{
GetLogicalProcessorInformationEx, LOGICAL_PROCESSOR_RELATIONSHIP, RelationAll, RelationCache,
RelationGroup, RelationNumaNode, RelationNumaNodeEx, RelationProcessorCore,
RelationProcessorDie, RelationProcessorModule, RelationProcessorPackage, SYSTEM_INFO,
};
use windows_sys::Win32::System::Threading::{ALL_PROCESSOR_GROUPS, GetActiveProcessorCount};
const LTP_PC_SMT: u8 = 1;
const LEGACY_RECORD: usize = 32;
const POWER_RECORD: usize = 24;
const STATUS_SUCCESS: u32 = 0;
const STATUS_INVALID_PARAMETER: u32 = 0xC000_000D;
const STATUS_BUFFER_TOO_SMALL: u32 = 0xC000_0023;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Cache {
descriptor: [u8; 12],
per_core: bool,
}
impl Cache {
fn level(&self) -> u8 {
self.descriptor[0]
}
}
#[derive(Clone, Copy)]
enum Record {
Processor {
relationship: i32,
mask: u64,
flags: u8,
},
Cache {
cache: Cache,
mask: u64,
},
Numa {
mask: u64,
},
Group,
}
impl Record {
fn relationship(&self) -> i32 {
match self {
Record::Processor { relationship, .. } => *relationship,
Record::Cache { .. } => RelationCache,
Record::Numa { .. } => RelationNumaNode,
Record::Group => RelationGroup,
}
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Topology {
pub(crate) cpus: u32,
pub(crate) threads_per_core: u32,
}
impl Topology {
pub(crate) fn mask(&self) -> u64 {
u64::MAX >> (64 - self.cpus)
}
fn cores(&self) -> impl Iterator<Item = u64> + use<> {
let (cpus, threads) = (self.cpus, self.threads_per_core);
(0..cpus).step_by(threads as usize).map(move |first| {
let run = threads.min(cpus - first);
(u64::MAX >> (64 - run)) << first
})
}
pub(crate) fn last_level_cache_index(&self, cpu: u32) -> u8 {
let caches = machine_caches();
let per_core = caches
.iter()
.max_by_key(|cache| cache.level())
.is_some_and(|cache| cache.per_core);
if per_core {
(cpu - cpu % self.threads_per_core) as u8
} else {
0
}
}
fn all(&self) -> Vec<Record> {
let caches = machine_caches();
let mut records = vec![Record::Processor {
relationship: RelationProcessorPackage,
mask: self.mask(),
flags: 0,
}];
for core in self.cores() {
records.push(Record::Processor {
relationship: RelationProcessorCore,
mask: core,
flags: if core.count_ones() > 1 { LTP_PC_SMT } else { 0 },
});
records.push(Record::Processor {
relationship: RelationProcessorModule,
mask: core,
flags: 0,
});
for cache in caches.iter().filter(|cache| cache.per_core) {
records.push(Record::Cache {
cache: *cache,
mask: core,
});
}
}
for cache in caches.iter().filter(|cache| !cache.per_core) {
records.push(Record::Cache {
cache: *cache,
mask: self.mask(),
});
}
records.push(Record::Numa { mask: self.mask() });
records.push(Record::Group);
records
}
fn records(&self, relationship: i32) -> Option<Vec<Record>> {
if relationship == RelationAll {
return Some(self.all());
}
if relationship == RelationProcessorDie {
return Some(vec![Record::Processor {
relationship: RelationProcessorDie,
mask: self.mask(),
flags: 0,
}]);
}
let wanted = if relationship == RelationNumaNodeEx {
RelationNumaNode
} else {
relationship
};
if !(RelationProcessorCore..=RelationGroup).contains(&wanted)
&& wanted != RelationProcessorModule
{
return None;
}
let mut records = self.all();
records.retain(|record| record.relationship() == wanted);
Some(records)
}
fn encode_ex(&self, record: &Record, out: &mut Vec<u8>) {
let start = out.len();
let size = match record {
Record::Processor { .. } | Record::Numa { .. } => 48,
Record::Cache { .. } => 56,
Record::Group => 80,
};
out.resize(start + size, 0);
let bytes = &mut out[start..];
bytes[0..4].copy_from_slice(&record.relationship().to_ne_bytes());
bytes[4..8].copy_from_slice(&(size as u32).to_ne_bytes());
let affinity = |bytes: &mut [u8], at: usize, mask: u64| {
bytes[at - 2..at].copy_from_slice(&1u16.to_ne_bytes());
bytes[at..at + 8].copy_from_slice(&mask.to_ne_bytes());
};
match *record {
Record::Processor { mask, flags, .. } => {
bytes[8] = flags;
affinity(bytes, 32, mask);
}
Record::Cache { cache, mask } => {
bytes[8..20].copy_from_slice(&cache.descriptor);
affinity(bytes, 40, mask);
}
Record::Numa { mask } => affinity(bytes, 32, mask),
Record::Group => {
bytes[8..10].copy_from_slice(&1u16.to_ne_bytes());
bytes[10..12].copy_from_slice(&1u16.to_ne_bytes());
bytes[32] = self.cpus as u8;
bytes[33] = self.cpus as u8;
bytes[72..80].copy_from_slice(&self.mask().to_ne_bytes());
}
}
}
fn encode_legacy(record: &Record) -> Option<[u8; LEGACY_RECORD]> {
let mut bytes = [0u8; LEGACY_RECORD];
let mask = match *record {
Record::Processor {
relationship,
mask,
flags,
} if relationship == RelationProcessorCore
|| relationship == RelationProcessorPackage =>
{
bytes[16] = flags;
mask
}
Record::Cache { cache, mask } => {
bytes[16..28].copy_from_slice(&cache.descriptor);
mask
}
Record::Numa { mask } => mask,
_ => return None,
};
bytes[0..8].copy_from_slice(&mask.to_ne_bytes());
bytes[8..12].copy_from_slice(&record.relationship().to_ne_bytes());
Some(bytes)
}
pub(crate) unsafe fn logical_processor_information(
&self,
relationship: Option<i32>,
info: *mut u8,
len: *mut u32,
) -> Result<(), u32> {
if len.is_null() {
return Err(ERROR_INVALID_PARAMETER);
}
let room = unsafe { len.read_unaligned() } as usize;
let bytes = match relationship {
None => {
let records: Vec<_> = self.all().iter().filter_map(Self::encode_legacy).collect();
records.concat()
}
Some(relationship) => {
let Some(records) = self.records(relationship) else {
unsafe { len.write_unaligned(0) };
return Err(ERROR_GEN_FAILURE);
};
let mut bytes = Vec::new();
for record in &records {
self.encode_ex(record, &mut bytes);
}
bytes
}
};
if info.is_null() && room != 0 {
return Err(ERROR_NOACCESS);
}
unsafe { len.write_unaligned(bytes.len() as u32) };
let (fits, result) = match (room < bytes.len(), relationship) {
(false, _) => (bytes.len(), Ok(())),
(true, None) => (
room / LEGACY_RECORD * LEGACY_RECORD,
Err(ERROR_INSUFFICIENT_BUFFER),
),
(true, Some(_)) => (0, Err(ERROR_INSUFFICIENT_BUFFER)),
};
if fits == 0 {
return result;
}
if relationship.is_some() {
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), info, fits) };
return result;
}
for (n, record) in bytes[..fits]
.as_chunks::<LEGACY_RECORD>()
.0
.iter()
.enumerate()
{
let at = n * LEGACY_RECORD;
unsafe {
std::ptr::copy_nonoverlapping(record.as_ptr(), info.add(at), 12);
std::ptr::copy_nonoverlapping(record[16..].as_ptr(), info.add(at + 16), 16);
}
}
result
}
pub(crate) unsafe fn write_system_info(&self, info: *mut u8) {
let info = info.cast::<SYSTEM_INFO>();
unsafe {
(&raw mut (*info).dwActiveProcessorMask).write_unaligned(self.mask() as usize);
(&raw mut (*info).dwNumberOfProcessors).write_unaligned(self.cpus);
}
}
pub(crate) unsafe fn power_information(
&self,
input: *const u8,
output: *mut u8,
output_len: u32,
) -> u32 {
if !input.is_null() || output.is_null() || output_len == 0 {
return STATUS_INVALID_PARAMETER;
}
let need = self.cpus as usize * POWER_RECORD;
if (output_len as usize) < need {
return STATUS_BUFFER_TOO_SMALL;
}
let real = machine_power();
for cpu in 0..self.cpus as usize {
let mut record = [0u8; POWER_RECORD];
if !real.is_empty() {
let from = cpu % real.len();
record.copy_from_slice(&real[from]);
}
record[0..4].copy_from_slice(&(cpu as u32).to_ne_bytes());
unsafe {
std::ptr::copy_nonoverlapping(
record.as_ptr(),
output.add(cpu * POWER_RECORD),
POWER_RECORD,
)
};
}
STATUS_SUCCESS
}
}
fn machine_records(relationship: LOGICAL_PROCESSOR_RELATIONSHIP) -> Vec<u8> {
let _error = PreservedLastError::new();
snare_interpose::real(|| {
let mut len = 0u32;
unsafe { GetLogicalProcessorInformationEx(relationship, std::ptr::null_mut(), &mut len) };
let mut buf = vec![0u8; len as usize];
let ok = unsafe {
GetLogicalProcessorInformationEx(relationship, buf.as_mut_ptr().cast(), &mut len)
};
buf.truncate(len as usize);
if ok == 0 { Vec::new() } else { buf }
})
}
fn split_records(buf: &[u8]) -> impl Iterator<Item = &[u8]> {
let mut rest = buf;
std::iter::from_fn(move || {
let size = u32::from_ne_bytes(rest.get(4..8)?.try_into().ok()?) as usize;
if size < 8 || size > rest.len() {
return None;
}
let (record, tail) = rest.split_at(size);
rest = tail;
Some(record)
})
}
fn group_zero_mask(record: &[u8], count_at: usize) -> Option<u64> {
let count = u16::from_ne_bytes(record.get(count_at..count_at + 2)?.try_into().ok()?).max(1);
(0..usize::from(count)).find_map(|n| {
let at = count_at + 2 + n * 16;
let affinity = record.get(at..at + 16)?;
let group = u16::from_ne_bytes(affinity[8..10].try_into().ok()?);
(group == 0).then(|| u64::from_ne_bytes(affinity[..8].try_into().unwrap()))
})
}
fn machine_caches() -> &'static [Cache] {
static CACHES: OnceLock<Vec<Cache>> = OnceLock::new();
CACHES.get_or_init(|| {
let cores = machine_records(RelationProcessorCore);
let Some(core) = split_records(&cores)
.filter_map(|record| group_zero_mask(record, 30))
.find(|mask| mask & 1 != 0)
else {
return Vec::new();
};
let caches = machine_records(RelationCache);
split_records(&caches)
.filter_map(|record| {
let mask = group_zero_mask(record, 38)?;
(mask & 1 != 0).then(|| Cache {
descriptor: record[8..20].try_into().unwrap(),
per_core: mask == core,
})
})
.collect()
})
}
fn machine_power() -> Vec<[u8; POWER_RECORD]> {
let _error = PreservedLastError::new();
snare_interpose::real(|| {
let count = unsafe { GetActiveProcessorCount(ALL_PROCESSOR_GROUPS) } as usize;
let mut buf = vec![0u8; count * POWER_RECORD];
let status = unsafe {
CallNtPowerInformation(
ProcessorInformation,
std::ptr::null(),
0,
buf.as_mut_ptr().cast(),
buf.len() as u32,
)
};
if status != 0 {
return Vec::new();
}
buf.as_chunks::<POWER_RECORD>().0.to_vec()
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cores_group_runs_of_threads() {
let topology = Topology {
cpus: 5,
threads_per_core: 2,
};
assert_eq!(
topology.cores().collect::<Vec<_>>(),
[0b11, 0b1100, 0b10000]
);
let full = Topology {
cpus: 64,
threads_per_core: 1,
};
assert_eq!(full.mask(), u64::MAX);
assert_eq!(full.cores().last(), Some(1 << 63));
}
}