traffic.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //! Diagnostics: which host copies' bytes cross the bus, and which way: per
  2. //! copy (title address, bytes) what was sent up and what shaders wrote that
  3. //! was fetched, a frame; and how many different 64 KiB pieces of it. Every
  4. //! five seconds, the eight that move most. For placing resources: a copy whose
  5. //! pieces are fetched every frame and never sent up is written by shaders
  6. //! alone, and nobody but shaders may read it.
  7. use std::collections::{HashMap, HashSet};
  8. use std::sync::Mutex;
  9. /// Of a copy, each way (up, fetched): bytes, and the pieces (64 KiB).
  10. #[derive(Default)]
  11. struct Moved {
  12. bytes: [u64; 2],
  13. pieces: [HashSet<u32>; 2],
  14. }
  15. pub(super) const UP: usize = 0;
  16. pub(super) const FETCHED: usize = 1;
  17. static MOVED: Mutex<Option<HashMap<(u64, u64), Moved>>> = Mutex::new(None);
  18. /// Offsets `ranges` of the copy of `span` (title address, bytes) went `way`.
  19. pub(super) fn moved(way: usize, span: (u64, u64), ranges: impl Iterator<Item = (usize, usize)>) {
  20. let mut all = MOVED.lock().unwrap_or_else(|e| e.into_inner());
  21. let m = all.get_or_insert_with(Default::default).entry(span).or_default();
  22. for (from, to) in ranges.filter(|r| r.0 < r.1) {
  23. m.bytes[way] += (to - from) as u64;
  24. m.pieces[way].extend((from >> 16) as u32..=((to - 1) >> 16) as u32);
  25. }
  26. }
  27. /// For the log, over `frames` frames.
  28. pub(super) fn line(frames: f64) {
  29. let all = MOVED.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default();
  30. let mut all: Vec<_> = all.into_iter().collect();
  31. all.sort_by_key(|(_, m)| std::cmp::Reverse(m.bytes[0] + m.bytes[1]));
  32. let mb = |b: u64| b as f64 / 1e6 / frames.max(1.0);
  33. let sum = |way: usize| all.iter().map(|(_, m)| m.bytes[way]).sum::<u64>();
  34. // Copies that only move one way.
  35. let only = |way: usize| all.iter().filter(|(_, m)| m.bytes[1 - way] == 0).map(|(_, m)| m.bytes[way]).sum::<u64>();
  36. tracing::info!(
  37. "landings in title memory looked at by refreshes per frame (VICTORY_LAND_ECHO=0: sent up): {:.1} MB held already and left, \
  38. {:.1} MB the title's too",
  39. mb(crate::shadow::ECHO[0].swap(0, std::sync::atomic::Ordering::Relaxed)),
  40. mb(crate::shadow::ECHO[1].swap(0, std::sync::atomic::Ordering::Relaxed)),
  41. );
  42. tracing::info!(
  43. "bytes of host copies over the bus per frame: {:.1} MB up ({:.1} of copies nothing was fetched from), {:.1} MB fetched \
  44. ({:.1} of copies nothing was sent up to), {} copies; the most:",
  45. mb(sum(UP)),
  46. mb(only(UP)),
  47. mb(sum(FETCHED)),
  48. mb(only(FETCHED)),
  49. all.len()
  50. );
  51. for ((start, len), m) in all.into_iter().take(8) {
  52. let both = m.pieces[0].intersection(&m.pieces[1]).count();
  53. tracing::info!(
  54. " {start:#x}+{len:#x}: {:.2} MB up in {} different pieces, {:.2} MB fetched in {}, {both} of them both ways",
  55. mb(m.bytes[0]),
  56. m.pieces[0].len(),
  57. mb(m.bytes[1]),
  58. m.pieces[1].len()
  59. );
  60. }
  61. }