//! Which processors our own threads run on, where a processor has two kinds //! of cores. `cpu_sets` and `keep_on_fast_cores` are fxceless/victory-fork's //! (`framelog.rs`, commit 0b0074e0, GPL-3.0-or-later, as this). /// The system's processor sets of group 0: each one's id and its efficiency /// class (higher is faster). fn cpu_sets() -> Vec<(u32, u8)> { use windows_sys::Win32::System::SystemInformation::{GetSystemCpuSetInformation, SYSTEM_CPU_SET_INFORMATION}; use windows_sys::Win32::System::Threading::GetCurrentProcess; let mut bytes = 0u32; // SAFETY: a size query, then the call into a buffer of that size. unsafe { GetSystemCpuSetInformation(std::ptr::null_mut(), 0, &mut bytes, GetCurrentProcess(), 0) }; let mut buffer = vec![0u64; (bytes as usize).div_ceil(8).max(1)]; // SAFETY: as above; the buffer holds `bytes` bytes, aligned for the records. let ok = unsafe { GetSystemCpuSetInformation(buffer.as_mut_ptr().cast(), bytes, &mut bytes, GetCurrentProcess(), 0) }; let mut sets = Vec::new(); let mut at = 0usize; while ok != 0 && at + size_of::() <= bytes as usize { // SAFETY: a record inside the buffer; each says its own size. let info = unsafe { &*(buffer.as_ptr().cast::().add(at).cast::()) }; if info.Size == 0 { break; } // SAFETY: the record's type is CpuSet (the only one there is). let set = unsafe { &info.Anonymous.CpuSet }; if set.Group == 0 { sets.push((set.Id, set.EfficiencyClass)); } at += info.Size as usize; } sets } /// The sets of the fastest class, if there are slower ones too. fn fast_of(sets: &[(u32, u8)]) -> Vec { let best = sets.iter().map(|c| c.1).max().unwrap_or(0); let fast: Vec = sets.iter().filter(|c| c.1 == best).map(|c| c.0).collect(); if fast.len() == sets.len() { Vec::new() } else { fast } } /// `VICTORY_FAST_CORES`: 0: no thread asks; 1: the threads a frame waits for /// one by one; 2 (the default): the threads they share their work with too; /// 3: those that work beside the rest as well (lists translated, descriptors /// made): more threads by then than the fast cores run at a time, while the /// slow ones stood idle. fn stage() -> u32 { static STAGE: std::sync::OnceLock = std::sync::OnceLock::new(); *STAGE.get_or_init(|| std::env::var("VICTORY_FAST_CORES").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(2)) } /// Asks the system to run the calling thread, which a frame waits for, on /// the fast cores only, where the processor has two kinds. That fork's submit /// thread was found on a slow one in 1,529 looks of 24,021. (A selection of /// processors is a preference the scheduler keeps unless nothing of it is /// free.) The title's threads are not asked for. pub(crate) fn keep_on_fast_cores(what: &str) { ask(what, 1); } /// [`keep_on_fast_cores`] for one of the threads a submission's preparing or /// a landing shares its work with. pub(crate) fn keep_helper_on_fast_cores(what: &str) { ask(what, 2); } /// [`keep_on_fast_cores`] for one of many threads whose work is ahead of what /// a frame waits for. pub(crate) fn keep_many_on_fast_cores(what: &str) { ask(what, 3); } /// The priority our own threads that a frame waits for, and those they share /// their work with, run at (`VICTORY_HELPER_PRIORITY=`, a thread priority /// from -2 to 2; not given: as made, normal, the submitting thread above /// normal). A frame's timeline in the first gameplay area (run timeline, /// 2026-11-26) had such threads woken and not running yet for 4-11 ms of its /// chain: the listing threads up to 5.4 ms at a time, while the title's own /// workers held every processor. fn helper_priority() -> Option { static PRIORITY: std::sync::OnceLock> = std::sync::OnceLock::new(); *PRIORITY.get_or_init(|| { std::env::var("VICTORY_HELPER_PRIORITY").ok().and_then(|v| v.trim().parse::().ok()).map(|p| p.clamp(-2, 2)) }) } /// Effective priority for critical submission/helper threads (`from <= 2`) vs /// background list-translation workers (`from == 3`): when `VICTORY_HELPER_PRIORITY` /// is positive (e.g. `1`), critical submit/wide threads run one tier higher (`+2`, /// `HIGHEST`) than list-translation workers (`+1`, `ABOVE_NORMAL`), which in turn /// stay ahead of the title's busy-wait threads (`0`, `NORMAL`). fn effective_priority(from: u32, requested: i32) -> i32 { if from <= 2 && requested > 0 { (requested + 1).min(2) } else { requested } } fn ask(what: &str, from: u32) { use windows_sys::Win32::System::Threading::{GetCurrentThread, SetThreadPriority, SetThreadSelectedCpuSets}; if let Some(requested) = helper_priority() { let priority = effective_priority(from, requested); // SAFETY: this thread's own priority. let set = unsafe { SetThreadPriority(GetCurrentThread(), priority) } != 0; tracing::debug!(what, from, priority, set, "the thread's priority (VICTORY_HELPER_PRIORITY)"); } if stage() < from { return; } static FAST: std::sync::OnceLock<(Vec, usize)> = std::sync::OnceLock::new(); let (fast, of) = FAST.get_or_init(|| { let sets = cpu_sets(); (fast_of(&sets), sets.len()) }); // All alike (or nothing known): nothing to ask for. if fast.is_empty() { return; } // SAFETY: our own thread and a list of the system's own set ids. let ok = unsafe { SetThreadSelectedCpuSets(GetCurrentThread(), fast.as_ptr(), fast.len() as u32) }; if ok == 0 { tracing::warn!(what, error = %std::io::Error::last_os_error(), "the thread could not be kept on the fast cores"); } else { tracing::info!(what, fast = fast.len(), of, "the thread is kept on the fast cores (VICTORY_FAST_CORES=0: not)"); } } /// Whether the threads the submitting thread waits for run at its priority /// (`VICTORY_HELPER_PRIORITY`, on unless `0`; `VICTORY_AB` can flip it while /// the title runs): above normal, ahead of the title's threads and of the /// threads that translate lists. They are the threads that spread its work /// (`shadow::wide`, the pools of `land`), the one that makes the heaps' /// records and the one that passes over the heap's views beside the lists. /// /// On a processor with six cores (i5-12400F, 12 threads) the title's threads /// and eight translating lists kept every core busy, and the submitting /// thread (above normal since `queue::behind`) stood waiting for helpers at /// normal priority that were not running. Measured with `VICTORY_AB` in one /// run (run10, Series S at 540p, medians of 5-second windows in the world, /// off / on): 8.6 / 9.6 fps, refreshes 23.1 / 17.6 ms a frame, waiting for /// the heaps' records 6.2 / 2.5 ms, the card idle behind the submitting /// thread 24.7 / 13.2 ms. Lowering the translating threads below normal as /// well made each list call about 5 % slower, with no clear gain of its own. fn helpers_raised() -> bool { crate::toggled!("VICTORY_HELPER_PRIORITY") } /// A helper's priority ([`helpers_raised`]). [`Self::keep`] is called before /// each piece of work; it asks the system only when the switch has changed. pub(crate) struct Priority { applied: Option, } impl Priority { pub(crate) fn new() -> Self { let mut priority = Self { applied: None }; priority.keep(); priority } pub(crate) fn keep(&mut self) { use windows_sys::Win32::System::Threading::{ GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_NORMAL, }; let on = helpers_raised(); if self.applied == Some(on) { return; } let priority = if on { helper_priority().map_or(THREAD_PRIORITY_ABOVE_NORMAL, |p| effective_priority(2, p)) } else { THREAD_PRIORITY_NORMAL }; // SAFETY: our own thread. unsafe { SetThreadPriority(GetCurrentThread(), priority) }; self.applied = Some(on); } } #[cfg(test)] mod tests { use super::*; #[test] fn the_fast_cores_are_those_of_the_best_class_if_there_is_a_worse_one() { assert_eq!(fast_of(&[(256, 1), (257, 1), (258, 0)]), [256, 257]); assert!(fast_of(&[(256, 0), (257, 0)]).is_empty()); assert!(fast_of(&[]).is_empty()); assert!(!cpu_sets().is_empty()); std::thread::spawn(|| keep_on_fast_cores("test")).join().unwrap(); } }