residency.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //! Host-memory admission for speculative texture work and reusable upload storage.
  2. use std::sync::OnceLock;
  3. use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
  4. use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX};
  5. const GIB: u64 = 1 << 30;
  6. static PRESSURED: AtomicBool = AtomicBool::new(false);
  7. static HEADROOM: AtomicU64 = AtomicU64::new(u64::MAX);
  8. fn memory_status() -> Option<MEMORYSTATUSEX> {
  9. // SAFETY: the API initializes the fields of a correctly sized structure.
  10. let mut status: MEMORYSTATUSEX = unsafe { std::mem::zeroed() };
  11. status.dwLength = std::mem::size_of::<MEMORYSTATUSEX>() as u32;
  12. (unsafe { GlobalMemoryStatusEx(&mut status) } != 0).then_some(status)
  13. }
  14. pub(crate) fn limited_host_ram() -> bool {
  15. static LIMITED: OnceLock<bool> = OnceLock::new();
  16. *LIMITED.get_or_init(|| memory_status().is_some_and(|s| s.ullTotalPhys <= 32 * GIB))
  17. }
  18. fn next_pressure(was: bool, physical: u64, commit: u64) -> bool {
  19. if was {
  20. physical < 6 * GIB || commit < 8 * GIB
  21. } else {
  22. physical <= 4 * GIB || commit <= 6 * GIB
  23. }
  24. }
  25. /// Called by the once-per-second submission sweep. A failed sample keeps the last good state.
  26. pub(crate) fn refresh() {
  27. let Some(status) = memory_status() else { return };
  28. let (physical, commit) = (status.ullAvailPhys, status.ullAvailPageFile);
  29. HEADROOM.store(physical.min(commit), Ordering::Relaxed);
  30. let was = pressured();
  31. let pressure = next_pressure(was, physical, commit);
  32. if pressure != was {
  33. PRESSURED.store(pressure, Ordering::Relaxed);
  34. if pressure {
  35. tracing::warn!(
  36. pid = std::process::id(),
  37. frame = crate::frame::current(),
  38. physical_free_mib = physical >> 20,
  39. available_commit_mib = commit >> 20,
  40. buffer_mirror_span_mib = crate::shadow::live_span_bytes() >> 20,
  41. texture_staging_mib = crate::texture::live_staging_bytes() >> 20,
  42. spare_scratch_mib = crate::host::ALIVE[4].load(Ordering::Relaxed) >> 20,
  43. game_filled_gpu_texture_mib = crate::resource::KINDS[1].load(Ordering::Relaxed) >> 20,
  44. "host memory pressure: limiting texture prefetch and idle upload storage"
  45. );
  46. } else {
  47. tracing::info!(physical_free_mib = physical >> 20, available_commit_mib = commit >> 20, "host memory pressure cleared");
  48. }
  49. }
  50. }
  51. pub(crate) fn headroom() -> Option<u64> {
  52. let bytes = HEADROOM.load(Ordering::Relaxed);
  53. (bytes != u64::MAX).then_some(bytes)
  54. }
  55. pub(crate) fn pressured() -> bool {
  56. PRESSURED.load(Ordering::Relaxed)
  57. }
  58. /// A soft per-frame admission limit. A single texture can cross it. The
  59. /// queue retains unserved slots so they can be retried on later frames.
  60. pub(crate) fn prefetch_limit(bytes: u64, pressure: bool) -> u64 {
  61. if pressure { bytes.min(16 << 20) } else { bytes }
  62. }
  63. #[cfg(test)]
  64. mod tests {
  65. use super::*;
  66. #[test]
  67. fn either_constraint_enters_pressure_and_both_must_recover() {
  68. assert!(next_pressure(false, 12 * GIB, 6 * GIB));
  69. assert!(next_pressure(false, 4 * GIB, 16 * GIB));
  70. assert!(next_pressure(true, 5 * GIB, 9 * GIB));
  71. assert!(next_pressure(true, 7 * GIB, 7 * GIB));
  72. assert!(!next_pressure(true, 6 * GIB, 8 * GIB));
  73. assert!(!next_pressure(false, 5 * GIB, 7 * GIB));
  74. }
  75. #[test]
  76. fn prefetch_respects_existing_budgets_and_resumes_after_recovery() {
  77. assert_eq!(prefetch_limit(0, true), 0);
  78. assert_eq!(prefetch_limit(8 << 20, true), 8 << 20);
  79. assert_eq!(prefetch_limit(512 << 20, true), 16 << 20);
  80. assert_eq!(prefetch_limit(192 << 20, false), 192 << 20);
  81. }
  82. }