hle: the title's question of the CPU's cores logged, VICTORY_REPORT_CORES to tell it fewer

GetLogicalProcessorInformation is the title's only question of the CPU's
size and went to the host unseen. It is now answered through us, logged once,
and VICTORY_REPORT_CORES=<n> keeps the first n cores. Told 2 of 6 cores, the
title still made 49 threads, as with all 6: it does not size its worker pools
by the cores it is told, so more cores cannot be given to its render-command
recording this way.
This commit is contained in:
DoubleGhosty
2026-12-01 12:11:33 +01:00
parent c205bd9198
commit f7b2e1cf83
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,118 @@
//! What the title is told of the CPU's cores.
//!
//! The title asks `GetLogicalProcessorInformation` (its only question about
//! the CPU's size) and is told the host's own answer. Each answer is logged
//! with its count of cores, and `VICTORY_REPORT_CORES=<n>` tells it of the
//! first `n` cores only, to find whether the title sizes its pools of worker
//! threads by what it is told (#194: five render-command workers on a CPU of
//! 24 cores).
#![allow(non_snake_case)]
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::ffi::c_void;
use super::files::host;
use crate::{HleModule, hle_export};
/// `SYSTEM_LOGICAL_PROCESSOR_INFORMATION` on x64: a processor mask, the
/// relationship (and padding), and a 16-byte union.
const ENTRY: usize = 32;
/// `RelationProcessorCore`.
const CORE: u32 = 0;
/// `VICTORY_REPORT_CORES`: how many cores the title is told of (unset: all).
fn report_cores() -> Option<usize> {
static N: std::sync::OnceLock<Option<usize>> = std::sync::OnceLock::new();
*N.get_or_init(|| {
std::env::var("VICTORY_REPORT_CORES").ok().and_then(|v| v.trim().parse().ok()).filter(|&n| n > 0)
})
}
/// Keeps the first `keep` core entries of `entries` (and every entry of
/// another relationship: caches, packages, NUMA nodes); returns the entries
/// kept and the cores there were.
fn keep_cores(entries: &mut [[u8; ENTRY]], keep: Option<usize>) -> (usize, usize) {
let relation = |e: &[u8; ENTRY]| u32::from_le_bytes([e[8], e[9], e[10], e[11]]);
let (mut out, mut cores) = (0, 0);
for i in 0..entries.len() {
if relation(&entries[i]) == CORE {
cores += 1;
if keep.is_some_and(|k| cores > k) {
continue;
}
}
entries[out] = entries[i];
out += 1;
}
(out, cores)
}
pub extern "system" fn GetLogicalProcessorInformation(buffer: *mut c_void, length: *mut u32) -> i32 {
let ok = host!("kernel32.dll", "GetLogicalProcessorInformation": extern "system" fn(*mut c_void, *mut u32) -> i32)(
buffer, length,
);
if ok == 0 || buffer.is_null() || length.is_null() {
return ok;
}
// SAFETY: the host filled `*length` bytes of `buffer` with whole entries.
let entries = unsafe {
std::slice::from_raw_parts_mut(buffer.cast::<[u8; ENTRY]>(), *length as usize / ENTRY)
};
let (kept, cores) = keep_cores(entries, report_cores());
// SAFETY: as above.
unsafe { *length = (kept * ENTRY) as u32 };
// Once, and as a warning so that release logs keep it.
static TOLD: std::sync::Once = std::sync::Once::new();
TOLD.call_once(|| tracing::warn!(
cores,
told = report_cores().map_or(cores, |k| k.min(cores)),
"GetLogicalProcessorInformation: the title asked how many cores (VICTORY_REPORT_CORES)"
));
ok
}
pub(crate) fn module() -> HleModule {
HleModule {
name: "cpus",
dlls: &["api-ms-win-core-sysinfo-*"],
exports: vec![hle_export!(GetLogicalProcessorInformation)],
}
}
#[cfg(test)]
mod tests {
use super::*;
fn entry(relation: u32, mask: u64) -> [u8; ENTRY] {
let mut e = [0; ENTRY];
e[..8].copy_from_slice(&mask.to_le_bytes());
e[8..12].copy_from_slice(&relation.to_le_bytes());
e
}
#[test]
fn only_the_first_cores_are_told_and_other_entries_stay() {
let mut entries = [entry(CORE, 1), entry(2, 3), entry(CORE, 2), entry(CORE, 4), entry(3, 7)];
let (kept, cores) = keep_cores(&mut entries, Some(2));
assert_eq!((kept, cores), (4, 3));
let masks: Vec<u8> = entries[..kept].iter().map(|e| e[0]).collect();
assert_eq!(masks, [1, 3, 2, 7]);
}
#[test]
fn unset_keeps_everything() {
let mut entries = [entry(CORE, 1), entry(CORE, 2)];
assert_eq!(keep_cores(&mut entries, None), (2, 2));
}
#[test]
fn the_host_answer_passes_through() {
let mut length = 0u32;
GetLogicalProcessorInformation(std::ptr::null_mut(), &mut length);
assert!(length as usize >= ENTRY);
let mut buffer = vec![0u8; length as usize];
assert_ne!(GetLogicalProcessorInformation(buffer.as_mut_ptr().cast(), &mut length), 0);
assert_eq!(length as usize % ENTRY, 0);
}
}

View File

@ -11,6 +11,8 @@
#[cfg(windows)]
pub mod apu;
pub mod audio;
#[cfg(windows)]
pub mod cpus;
pub mod d3d12x;
pub mod display;
pub mod dsp;
@ -64,6 +66,8 @@ pub(crate) fn all() -> Vec<HleModule> {
#[cfg(windows)]
all.push(dstorage::module());
#[cfg(windows)]
all.push(cpus::module());
#[cfg(windows)]
all.push(files::module());
#[cfg(windows)]
all.push(lifecycle::module());