copystat.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //! Diagnostics: where the submission copies of title memory go (which
  2. //! ranges, how often, of what memory) and what limits their rate, logged
  3. //! with the frame-rate line. `VICTORY_COPY_MODE=plain` copies without the
  4. //! mapped-page guard, `none` skips the copies (both for timing only).
  5. use std::collections::HashMap;
  6. use std::sync::Mutex;
  7. use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
  8. /// Counters since the last line.
  9. pub(crate) enum C {
  10. Submissions,
  11. Copies,
  12. CopyBytes,
  13. Refreshes,
  14. RefreshBytes,
  15. Skipped,
  16. SkippedBytes,
  17. Runs,
  18. RunUs,
  19. Queries,
  20. QueryUs,
  21. MoveUs,
  22. Unmapped,
  23. /// Write-watch askings and their time.
  24. Watches,
  25. WatchUs,
  26. /// Bytes of watched ranges checked, and of those found unchanged.
  27. Checked,
  28. Unchanged,
  29. /// Copies that met an unmapped page.
  30. Fallbacks,
  31. /// Writes taken by the guard on view memory.
  32. GuardFaults,
  33. /// Texture bytes detiled after a watched write; hashed; detiled after a
  34. /// hash change.
  35. TexWatched,
  36. TexHashed,
  37. TexChanged,
  38. }
  39. const COUNTERS: usize = 22;
  40. static COUNT: [AtomicU64; COUNTERS] = [const { AtomicU64::new(0) }; COUNTERS];
  41. pub(crate) fn add(c: C, n: u64) {
  42. COUNT[c as usize].fetch_add(n, Relaxed);
  43. }
  44. /// How copies run: 0 guarded (as always), 1 plain, 2 not at all.
  45. pub(crate) fn mode() -> u8 {
  46. static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
  47. *MODE.get_or_init(|| match std::env::var("VICTORY_COPY_MODE").as_deref() {
  48. Ok("plain") => 1,
  49. Ok("none") => 2,
  50. _ => 0,
  51. })
  52. }
  53. /// Per source range (start, length): copies, bytes, microseconds, memory type.
  54. type Ranges = HashMap<(usize, usize), (u64, u64, u64, u32)>;
  55. static RANGES: Mutex<Option<Ranges>> = Mutex::new(None);
  56. /// A copy of `len` bytes from `src` (memory type `kind`) took `us`.
  57. pub(crate) fn range(src: usize, len: usize, us: u64, kind: u32) {
  58. let mut map = RANGES.lock().unwrap_or_else(|e| e.into_inner());
  59. let e = map
  60. .get_or_insert_with(HashMap::new)
  61. .entry((src, len))
  62. .or_insert((0, 0, 0, kind));
  63. e.0 += 1;
  64. e.1 += len as u64;
  65. e.2 += us;
  66. }
  67. fn page_faults() -> u64 {
  68. use windows_sys::Win32::System::ProcessStatus::{
  69. K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS,
  70. };
  71. use windows_sys::Win32::System::Threading::GetCurrentProcess;
  72. let mut c = PROCESS_MEMORY_COUNTERS {
  73. cb: size_of::<PROCESS_MEMORY_COUNTERS>() as u32,
  74. ..Default::default()
  75. };
  76. // SAFETY: our own process and a sized structure.
  77. unsafe { K32GetProcessMemoryInfo(GetCurrentProcess(), &mut c, c.cb) };
  78. u64::from(c.PageFaultCount)
  79. }
  80. /// Logs the counters per frame over `frames` frames, and the ranges that
  81. /// took most bytes.
  82. pub(crate) fn log(frames: f64) {
  83. static FAULTS: AtomicU64 = AtomicU64::new(0);
  84. let faults = page_faults();
  85. let faults = faults - FAULTS.swap(faults, Relaxed).min(faults);
  86. let c: Vec<f64> = COUNT
  87. .iter()
  88. .map(|a| a.swap(0, Relaxed) as f64 / frames)
  89. .collect();
  90. let mb = |v: f64| v / 1e6;
  91. let ms = |v: f64| v / 1e3;
  92. tracing::info!(
  93. "copies per frame: {:.1} submissions; {:.0} title copies {:.1} MB; {:.0} refreshes {:.1} MB, \
  94. {:.0} skipped {:.1} MB; {:.0} runs {:.1} ms; {:.0} queries {:.2} ms, moves {:.2} ms, \
  95. {:.2} MB unmapped; {:.0} page faults; {:.0} watches {:.2} ms, {:.1} MB checked, {:.1} MB \
  96. unchanged; {:.1} fallbacks; {:.0} guard faults; textures {:.1} MB watched and \
  97. changed, {:.1} MB hashed, {:.1} MB of them changed",
  98. c[C::Submissions as usize],
  99. c[C::Copies as usize],
  100. mb(c[C::CopyBytes as usize]),
  101. c[C::Refreshes as usize],
  102. mb(c[C::RefreshBytes as usize]),
  103. c[C::Skipped as usize],
  104. mb(c[C::SkippedBytes as usize]),
  105. c[C::Runs as usize],
  106. ms(c[C::RunUs as usize]),
  107. c[C::Queries as usize],
  108. ms(c[C::QueryUs as usize]),
  109. ms(c[C::MoveUs as usize]),
  110. mb(c[C::Unmapped as usize]),
  111. faults as f64 / frames,
  112. c[C::Watches as usize],
  113. ms(c[C::WatchUs as usize]),
  114. mb(c[C::Checked as usize]),
  115. mb(c[C::Unchanged as usize]),
  116. c[C::Fallbacks as usize],
  117. c[C::GuardFaults as usize],
  118. mb(c[C::TexWatched as usize]),
  119. mb(c[C::TexHashed as usize]),
  120. mb(c[C::TexChanged as usize]),
  121. );
  122. let ranges = RANGES.lock().unwrap_or_else(|e| e.into_inner()).take();
  123. let mut ranges: Vec<_> = ranges.into_iter().flatten().collect();
  124. ranges.sort_by_key(|&(_, (_, bytes, _, _))| std::cmp::Reverse(bytes));
  125. for ((src, len), (n, bytes, us, _)) in ranges.into_iter().take(10) {
  126. use windows_sys::Win32::System::Memory::{MEMORY_BASIC_INFORMATION, VirtualQuery};
  127. // SAFETY: plain data filled in by the query.
  128. let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() };
  129. // SAFETY: a valid out pointer of the right size.
  130. unsafe { VirtualQuery(src as *const _, &mut info, size_of_val(&info)) };
  131. let kind = info.Type;
  132. tracing::info!(
  133. " copied from {src:#x}+{len:#x} (memory {kind:#x}): {:.1}x/frame, {:.2} MB/frame, {:.2} ms/frame",
  134. n as f64 / frames,
  135. bytes as f64 / 1e6 / frames,
  136. us as f64 / 1e3 / frames
  137. );
  138. }
  139. }