cores.rs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //! Which processors our own threads run on, where a processor has two kinds
  2. //! of cores. `cpu_sets` and `keep_on_fast_cores` are fxceless/victory-fork's
  3. //! (`framelog.rs`, commit 0b0074e0, GPL-3.0-or-later, as this).
  4. /// The system's processor sets of group 0: each one's id and its efficiency
  5. /// class (higher is faster).
  6. fn cpu_sets() -> Vec<(u32, u8)> {
  7. use windows_sys::Win32::System::SystemInformation::{GetSystemCpuSetInformation, SYSTEM_CPU_SET_INFORMATION};
  8. use windows_sys::Win32::System::Threading::GetCurrentProcess;
  9. let mut bytes = 0u32;
  10. // SAFETY: a size query, then the call into a buffer of that size.
  11. unsafe { GetSystemCpuSetInformation(std::ptr::null_mut(), 0, &mut bytes, GetCurrentProcess(), 0) };
  12. let mut buffer = vec![0u64; (bytes as usize).div_ceil(8).max(1)];
  13. // SAFETY: as above; the buffer holds `bytes` bytes, aligned for the records.
  14. let ok = unsafe { GetSystemCpuSetInformation(buffer.as_mut_ptr().cast(), bytes, &mut bytes, GetCurrentProcess(), 0) };
  15. let mut sets = Vec::new();
  16. let mut at = 0usize;
  17. while ok != 0 && at + size_of::<SYSTEM_CPU_SET_INFORMATION>() <= bytes as usize {
  18. // SAFETY: a record inside the buffer; each says its own size.
  19. let info = unsafe { &*(buffer.as_ptr().cast::<u8>().add(at).cast::<SYSTEM_CPU_SET_INFORMATION>()) };
  20. if info.Size == 0 {
  21. break;
  22. }
  23. // SAFETY: the record's type is CpuSet (the only one there is).
  24. let set = unsafe { &info.Anonymous.CpuSet };
  25. if set.Group == 0 {
  26. sets.push((set.Id, set.EfficiencyClass));
  27. }
  28. at += info.Size as usize;
  29. }
  30. sets
  31. }
  32. /// The sets of the fastest class, if there are slower ones too.
  33. fn fast_of(sets: &[(u32, u8)]) -> Vec<u32> {
  34. let best = sets.iter().map(|c| c.1).max().unwrap_or(0);
  35. let fast: Vec<u32> = sets.iter().filter(|c| c.1 == best).map(|c| c.0).collect();
  36. if fast.len() == sets.len() { Vec::new() } else { fast }
  37. }
  38. /// `VICTORY_FAST_CORES`: 0: no thread asks; 1: the threads a frame waits for
  39. /// one by one; 2 (the default): the threads they share their work with too.
  40. fn stage() -> u32 {
  41. static STAGE: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
  42. *STAGE.get_or_init(|| std::env::var("VICTORY_FAST_CORES").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(2))
  43. }
  44. /// Asks the system to run the calling thread, which a frame waits for, on
  45. /// the fast cores only, where the processor has two kinds. That fork's submit
  46. /// thread was found on a slow one in 1,529 looks of 24,021. (A selection of
  47. /// processors is a preference the scheduler keeps unless nothing of it is
  48. /// free.) The title's threads are not asked for.
  49. pub(crate) fn keep_on_fast_cores(what: &str) {
  50. ask(what, 1);
  51. }
  52. /// [`keep_on_fast_cores`] for one of the threads a submission's preparing or
  53. /// a landing shares its work with.
  54. pub(crate) fn keep_helper_on_fast_cores(what: &str) {
  55. ask(what, 2);
  56. }
  57. fn ask(what: &str, from: u32) {
  58. use windows_sys::Win32::System::Threading::{GetCurrentThread, SetThreadSelectedCpuSets};
  59. if stage() < from {
  60. return;
  61. }
  62. static FAST: std::sync::OnceLock<(Vec<u32>, usize)> = std::sync::OnceLock::new();
  63. let (fast, of) = FAST.get_or_init(|| {
  64. let sets = cpu_sets();
  65. (fast_of(&sets), sets.len())
  66. });
  67. // All alike (or nothing known): nothing to ask for.
  68. if fast.is_empty() {
  69. return;
  70. }
  71. // SAFETY: our own thread and a list of the system's own set ids.
  72. let ok = unsafe { SetThreadSelectedCpuSets(GetCurrentThread(), fast.as_ptr(), fast.len() as u32) };
  73. if ok == 0 {
  74. tracing::warn!(what, error = %std::io::Error::last_os_error(), "the thread could not be kept on the fast cores");
  75. } else {
  76. tracing::info!(what, fast = fast.len(), of, "the thread is kept on the fast cores (VICTORY_FAST_CORES=0: not)");
  77. }
  78. }
  79. #[cfg(test)]
  80. mod tests {
  81. use super::*;
  82. #[test]
  83. fn the_fast_cores_are_those_of_the_best_class_if_there_is_a_worse_one() {
  84. assert_eq!(fast_of(&[(256, 1), (257, 1), (258, 0)]), [256, 257]);
  85. assert!(fast_of(&[(256, 0), (257, 0)]).is_empty());
  86. assert!(fast_of(&[]).is_empty());
  87. assert!(!cpu_sets().is_empty());
  88. std::thread::spawn(|| keep_on_fast_cores("test")).join().unwrap();
  89. }
  90. }