diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index bd9f371..e0b190e 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -30,11 +30,44 @@ pub(crate) static LOCK_WAITS: [std::sync::atomic::AtomicU64; 4] = [const { std:: /// A wait for lock `which` (0: the texture queue's, 1: the views table's) /// that began at `started` is over: counted in [`LOCK_WAITS`]. -pub(crate) fn waited(which: usize, started: std::time::Instant) { +pub(crate) fn waited(which: usize, started: std::time::Instant) -> u64 { let other = usize::from(!IN_TEXTURE_PASS.get()); - LOCK_WAITS[2 * which + other].fetch_add(started.elapsed().as_nanos() as u64, std::sync::atomic::Ordering::Relaxed); + let ns = started.elapsed().as_nanos() as u64; + LOCK_WAITS[2 * which + other].fetch_add(ns, std::sync::atomic::Ordering::Relaxed); + ns } +/// The places that take the lock of the queue of textures picked by index +/// (`host::TextureQueue`), counted apart in [`QUEUE_LOCK`]. +pub(crate) mod queue_site { + /// The pass asks what is known of a slot, or tells what came of it. + pub(crate) const TELL: usize = 0; + /// The pass takes the slots due and the queue's counts. + pub(crate) const DUE: usize = 1; + /// Slots made due by memory mapped, unmapped or written, by room on + /// the card again, by levels waiting for it. + pub(crate) const MADE_DUE: usize = 2; + /// A slot's record made, copied in or made by a draw or the pass: the + /// slot queued, kept, or taken out. + pub(crate) const WRITE: usize = 3; + /// A draw read the slot through a table: taken out. + pub(crate) const TABLE: usize = 4; + /// Slots made stale by a newer view of other memory: taken out. + pub(crate) const STALE: usize = 5; + /// Anything else (counts for the lines, the bakes' news). + pub(crate) const OTHER: usize = 6; + pub(crate) const COUNT: usize = 7; +} + +/// Since the last 5-second line, by [`queue_site`]: the times the texture +/// queue's lock was taken; the nanoseconds threads waited for it; the +/// nanoseconds it was held by the places that hold it for many slots at a +/// time (the slots due, slots made due, stale views); and the times it was +/// not taken because what was asked would have changed nothing +/// (`host::QueueMarks`). +pub(crate) static QUEUE_LOCK: [[std::sync::atomic::AtomicU64; queue_site::COUNT]; 4] = + [const { [const { std::sync::atomic::AtomicU64::new(0) }; queue_site::COUNT] }; 4]; + const HEAP_IIDS: [Iid; 4] = [ guid( 0x8efb471d, diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 456860a..fbb5e22 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -1804,20 +1804,56 @@ fn null_sampler(at: D3D12_CPU_DESCRIPTOR_HANDLE) { unsafe { host.device.CreateSampler(&desc, at) }; } +/// The queue of textures picked by index, locked by place `site` +/// (`super::queue_site`): for the places that hold it for many slots at a +/// time, how long it was held is counted when it is let go. +struct QueueGuard<'a> { + queue: std::sync::MutexGuard<'a, TextureQueue>, + held: Option<(usize, std::time::Instant)>, +} + +impl std::ops::Deref for QueueGuard<'_> { + type Target = TextureQueue; + fn deref(&self) -> &TextureQueue { + &self.queue + } +} + +impl std::ops::DerefMut for QueueGuard<'_> { + fn deref_mut(&mut self) -> &mut TextureQueue { + &mut self.queue + } +} + +impl Drop for QueueGuard<'_> { + fn drop(&mut self) { + if let Some((site, since)) = self.held { + super::QUEUE_LOCK[2][site].fetch_add(since.elapsed().as_nanos() as u64, std::sync::atomic::Ordering::Relaxed); + } + } +} + impl HostHeap { - /// The queue of textures picked by index, locked; a wait for its lock - /// is counted (`super::LOCK_WAITS`). - fn queue(&self) -> std::sync::MutexGuard<'_, TextureQueue> { - match self.textures.try_lock() { + /// The queue of textures picked by index, locked by place `site` + /// (`super::queue_site`); the lock taken, a wait for it, and how long + /// the places that hold it for many slots held it are counted + /// (`super::LOCK_WAITS`, `super::QUEUE_LOCK`). + fn queue(&self, site: usize) -> QueueGuard<'_> { + use super::queue_site::{DUE, MADE_DUE, STALE}; + use std::sync::atomic::Ordering::Relaxed; + let queue = match self.textures.try_lock() { Ok(queue) => queue, Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(), Err(std::sync::TryLockError::WouldBlock) => { let started = std::time::Instant::now(); let queue = self.textures.lock().unwrap_or_else(|e| e.into_inner()); - super::waited(0, started); + super::QUEUE_LOCK[1][site].fetch_add(super::waited(0, started), Relaxed); queue } - } + }; + super::QUEUE_LOCK[0][site].fetch_add(1, Relaxed); + let held = matches!(site, DUE | MADE_DUE | STALE).then(|| (site, std::time::Instant::now())); + QueueGuard { queue, held } } /// `n` consecutive free dynamic slots (view heaps only), taken until @@ -1900,14 +1936,14 @@ impl HostHeap { /// ([`TextureQueue::due`]), and the queue's counts /// ([`TextureQueue::census`]). pub(crate) fn texture_slots_due(&self, frame: u64, max: usize) -> (Vec<(u32, u64)>, [u64; 6]) { - let (due, census) = self.queue().due_and_census(frame); + let (due, census) = self.queue(super::queue_site::DUE).due_and_census(frame); (if max == 0 { Vec::new() } else { due.order(max) }, census) } /// A number that changes when a slot is written or made due anew, and /// whether memory was noted since a pass last took the notes. pub(crate) fn texture_news(&self) -> (u64, bool) { - let writes = self.queue().writes; + let writes = self.queue(super::queue_site::OTHER).writes; let noted = |ranges: &Mutex>| !ranges.lock().unwrap_or_else(|e| e.into_inner()).is_empty(); (writes, noted(&REMAPPED) || noted(&PAGE_WRITTEN) || noted(&BAKE_PAGE_WRITTEN)) } @@ -1917,7 +1953,7 @@ impl HostHeap { /// look): for the card accounting line /// (`crate::resource::card_accounting`). pub(crate) fn texture_slots_all_due(&self, frame: u64) -> Vec<(u32, bool)> { - let textures = self.queue(); + let textures = self.queue(super::queue_site::OTHER); textures .slots .iter() @@ -1931,7 +1967,7 @@ impl HostHeap { /// that many bytes of its memory there, not all), or not until the /// slot is written again. pub(crate) fn texture_slot_done(&self, index: u32, write: u64, again: Option<(u64, u64)>) { - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::TELL); textures.note_served(index, write, crate::frame::current()); match again { Some((frame, mapped)) => textures.partly(index, write, frame, mapped), @@ -1951,38 +1987,38 @@ impl HostHeap { looks: u8, uploaded: u64, ) { - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::TELL); textures.note_served(index, write, crate::frame::current()); textures.recheck(index, write, due, at, print, looks, uploaded); } /// See [`TextureQueue::checked`]. pub(crate) fn texture_slot_checked(&self, index: u32) -> Option<(u64, u64, u64, u8, u64)> { - self.queue().checked(index) + self.queue(super::queue_site::TELL).checked(index) } /// Nothing to upload at title slot `index` now (its memory is not /// mapped, or no more of it than before): looked at again after 1, 2, /// 4, ... frames, at most `most`. pub(crate) fn texture_slot_later(&self, index: u32, write: u64, frame: u64, most: u64) { - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::TELL); textures.later(index, write, frame, most); } /// See [`TextureQueue::hold`]. pub(crate) fn texture_slot_held(&self, index: u32, write: u64, due: u64) { - self.queue().hold(index, write, due); + self.queue(super::queue_site::TELL).hold(index, write, due); } /// See [`TextureQueue::room`]. pub(crate) fn texture_slots_room(&self, frame: u64) -> usize { - self.queue().room(frame) + self.queue(super::queue_site::MADE_DUE).room(frame) } /// Bytes of title slot `index`'s memory that were mapped when it was /// last uploaded in part (0: not uploaded, or whole). pub(crate) fn texture_slot_mapped(&self, index: u32) -> u64 { - self.queue().mapped(index) + self.queue(super::queue_site::TELL).mapped(index) } /// Takes the ranges of title memory the runtime mapped, unmapped or @@ -2010,7 +2046,7 @@ impl HostHeap { slots.sort_unstable(); slots.dedup(); slots.retain(|&slot| !self.tabled_lately(slot, frame)); - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::MADE_DUE); for &slot in &slots { textures.changed(slot, frame, frame + 1); } @@ -2035,7 +2071,7 @@ impl HostHeap { slots.sort_unstable(); slots.dedup(); slots.retain(|&slot| !self.tabled_lately(slot, frame)); - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::MADE_DUE); for &slot in &slots { textures.changed(slot, frame, frame + 1); } @@ -2095,7 +2131,7 @@ impl HostHeap { slots.sort_unstable(); slots.dedup(); slots.retain(|&slot| !self.tabled_lately(slot, frame)); - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::MADE_DUE); slots.retain(|slot| textures.slots.get(slot).is_none_or(|w| w.mapped > 0 || w.checked.2 != 0)); for &slot in &slots { textures.changed(slot, frame, frame); @@ -2116,7 +2152,7 @@ impl HostHeap { slots.dedup(); slots.retain(|&slot| !self.tabled_lately(slot, frame)); let held = slots.len(); - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::MADE_DUE); // Uploaded before: out of the queue, or in it after an upload (in // part, or whole and being looked at again). slots.retain(|slot| textures.slots.get(slot).is_none_or(|w| w.mapped > 0 || w.checked.2 != 0)); @@ -2206,7 +2242,7 @@ impl HostHeap { slots.sort_unstable(); slots.dedup(); slots.retain(|&slot| !self.tabled_lately(slot, frame)); - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::MADE_DUE); for &slot in &slots { textures.changed(slot, frame, frame + 1); } @@ -2701,7 +2737,7 @@ impl HostHeap { if old != now | TABLED_READ { stamp.store(now | TABLED_READ, Relaxed); if tabled_out() { - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::TABLE); if textures.slots.remove(&index).is_some() { TABLED_WRITES[1].fetch_add(1, Relaxed); } @@ -2737,7 +2773,7 @@ impl HostHeap { // Made now by a draw of this frame that read it through a table: // up to date. let read_now = seen && stamp == now | TABLED_READ; - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::WRITE); if texture && !read_now { let due = if lately && !seen { TABLED_WRITES[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); @@ -2846,7 +2882,7 @@ impl HostHeap { }; if !stale.is_empty() { STALE[0].fetch_add(stale.len() as u64, std::sync::atomic::Ordering::Relaxed); - let mut textures = self.queue(); + let mut textures = self.queue(super::queue_site::STALE); for &(slot, _) in &stale { textures.remove(slot); } @@ -3474,7 +3510,7 @@ impl HostHeap { /// `index` at `frame`, in a few words (diagnostics). #[cfg(feature = "diagnostics")] pub(crate) fn texture_slot_state(&self, index: u32, frame: u64) -> String { - let textures = self.queue(); + let textures = self.queue(super::queue_site::OTHER); match textures.slots.get(&index) { None => "not queued".into(), Some(w) => format!( diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index e4c456c..821c6a7 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -968,6 +968,52 @@ fn outcome(which: usize) { HEAP_TEXTURE_SLOTS[which].fetch_add(1, Ordering::Relaxed); } +/// Of [`HEAP_TEXTURE_REST`]' place 4 (slots shown after their upload, +/// flipped or made at once), since the last 5-second line: nanoseconds and +/// calls of slots made at once (`HostHeap::mirror` by the pass), of slots +/// shown after their upload (`texture_slot_show_after`), and of slots +/// flipped (`texture_slot_flip`, or made at once as they show an earlier +/// view of the same texture): [ns made, ns after, ns flipped, made, after, +/// flipped]. +pub(crate) static HEAP_TEXTURE_SHOWN: [AtomicU64; 6] = [const { AtomicU64::new(0) }; 6]; + +/// `f`'s result, its time added to [`HEAP_TEXTURE_REST`]' place 4 and to +/// [`HEAP_TEXTURE_SHOWN`]' place `which`. +fn shown(which: usize, f: impl FnOnce() -> T) -> T { + let started = std::time::Instant::now(); + let out = f(); + let ns = started.elapsed().as_nanos() as u64; + HEAP_TEXTURE_REST[4].fetch_add(ns, Ordering::Relaxed); + HEAP_TEXTURE_SHOWN[which].fetch_add(ns, Ordering::Relaxed); + HEAP_TEXTURE_SHOWN[3 + which].fetch_add(1, Ordering::Relaxed); + out +} + +/// Of the slots the pass found nothing to bring up to date for +/// ([`slot_outcome::NOTHING`]), since the last 5-second line: views of a +/// texture object the host renders to or writes (the queue held them for +/// nothing: a host texture is never uploaded); slots with no texture view +/// or no texture the host can make; slots whose host texture is the host's +/// own (a target, depth or unordered access texture placed in title +/// memory); and of all these, the ones the pass made at once. +pub(crate) static HEAP_TEXTURE_NOTHING: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4]; + +/// Of [`HEAP_TEXTURE_REST`]' place 7 (the passes' work before their loops), +/// since the last 5-second line, in nanoseconds: slots made due (room on the +/// card again, memory mapped, unmapped or written, levels waiting); views +/// made again and slots flipped after earlier uploads; the slots due taken +/// from the queue and ordered. (The rest of it: budgets, counts, the card +/// accounting line.) +pub(crate) static HEAP_TEXTURE_BEFORE: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3]; + +/// `f`'s result, its time added to [`HEAP_TEXTURE_BEFORE`]' place `slot`. +fn before(slot: usize, f: impl FnOnce() -> T) -> T { + let started = std::time::Instant::now(); + let out = f(); + HEAP_TEXTURE_BEFORE[slot].fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed); + out +} + /// Passes since the last 5-second line by kind (the frame's, from /// [`State::heap_buffers`]; the early one at a frame's first draw; before a /// bake draw), their loops' nanoseconds by kind, and the passes that stopped @@ -2983,40 +3029,46 @@ impl State { once!("textures picked by index: the card has no room for more; they wait"); } let mut refused = 0; - // Room again: the slots held back while the card was full are due - // now (`full_card_pass`). - if room { - host.texture_slots_room(frame); - } - // Slots whose texture's memory the runtime mapped, unmapped or - // wrote since the last pass are due again. - host.texture_slots_remapped(frame); let baking = crate::texture::BAKING.get(); - if baking { - host.texture_slots_bake_written(frame); - } - host.texture_slots_short(frame); - // The slots of textures whose first level with memory (levels on - // demand), or whose host copy for one with more levels, changed since - // the last pass are made again from it; those whose new view's upload - // has run are made from that. - host.texture_views_remade(); - host.texture_slots_flip(); + before(0, || { + // Room again: the slots held back while the card was full are + // due now (`full_card_pass`). + if room { + host.texture_slots_room(frame); + } + // Slots whose texture's memory the runtime mapped, unmapped or + // wrote since the last pass are due again. + host.texture_slots_remapped(frame); + if baking { + host.texture_slots_bake_written(frame); + } + host.texture_slots_short(frame); + }); + before(1, || { + // The slots of textures whose first level with memory (levels + // on demand), or whose host copy for one with more levels, + // changed since the last pass are made again from it; those + // whose new view's upload has run are made from that. + host.texture_views_remade(); + host.texture_slots_flip(); + }); // And with room, the textures whose upload left a held level // without the card's memory are uploaded again. if room { - host.texture_slots_waiting(frame); + before(0, || host.texture_slots_waiting(frame)); } let required = PASS_BUDGET.get().is_some_and(|(_, _, bake)| bake); - let (mut due, census) = host.texture_slots_due(frame, if baking { 1 << 16 } else { 4096 }); + let (mut due, census) = before(2, || host.texture_slots_due(frame, if baking { 1 << 16 } else { 4096 })); // (Those that are due later after all that are due.) let on_time = due.len(); if baking { - let known: std::collections::HashSet = due.iter().map(|&(index, _)| index).collect(); - static AHEAD: AtomicU64 = AtomicU64::new(u64::MAX); - let again = AHEAD.swap(frame, Ordering::Relaxed) == frame; - let (ahead, _) = host.texture_slots_due(frame + BAKE_AHEAD[usize::from(again)], 1 << 16); - due.extend(ahead.into_iter().filter(|(index, _)| !known.contains(index))); + before(2, || { + let known: std::collections::HashSet = due.iter().map(|&(index, _)| index).collect(); + static AHEAD: AtomicU64 = AtomicU64::new(u64::MAX); + let again = AHEAD.swap(frame, Ordering::Relaxed) == frame; + let (ahead, _) = host.texture_slots_due(frame + BAKE_AHEAD[usize::from(again)], 1 << 16); + due.extend(ahead.into_iter().filter(|(index, _)| !known.contains(index))); + }); } HEAP_TEXTURES_WAITING.store(census[0], Ordering::Relaxed); for (count, now) in HEAP_TEXTURES_QUEUE.iter().zip(census) { @@ -3211,6 +3263,7 @@ impl State { && !crate::resource::title_fills(object as This) { outcome(slot_outcome::NOTHING); + HEAP_TEXTURE_NOTHING[0].fetch_add(1, Ordering::Relaxed); rest(0, || host.texture_slot_done(index, write, None)); continue; } @@ -3290,9 +3343,11 @@ impl State { (view, None) } }); - let at_once = || { + // (`nothing`: the slot has nothing to bring up to date.) + let at_once = |nothing: bool| { if (late || bake_key.is_some()) && let Some(handle) = heap.handle(index) { - rest(4, || { + HEAP_TEXTURE_NOTHING[3].fetch_add(u64::from(nothing), Ordering::Relaxed); + shown(0, || { let _ = host.mirror(index, handle, true); }); } @@ -3305,8 +3360,9 @@ impl State { let resource = match placed.as_ref().or(view.as_ref()) { Some(&View::Shader(resource, _)) => resource as This, _ => { - at_once(); + at_once(true); outcome(slot_outcome::NOTHING); + HEAP_TEXTURE_NOTHING[1].fetch_add(1, Ordering::Relaxed); rest(0, || host.texture_slot_done(index, write, None)); continue; } @@ -3318,9 +3374,10 @@ impl State { } let title_fills = rest(6, || texture.is_some_and(crate::texture::title_written)); if !title_fills { - at_once(); + at_once(true); // a render target, a depth or unordered access texture: the host's own outcome(slot_outcome::NOTHING); + HEAP_TEXTURE_NOTHING[2].fetch_add(1, Ordering::Relaxed); rest(0, || host.texture_slot_done(index, write, None)); continue; } @@ -3344,17 +3401,17 @@ impl State { sync.publish.push((index, handle, key)); } else { if shows_other && let Some(handle) = heap.handle(index) { - rest(4, || { + shown(2, || { let _ = host.mirror(index, handle, true); }); } // Its bytes are in, or on the way there in a list before. if late && texture.is_some_and(crate::pool::fresh) && !crate::texture::ahead::there(resource) { if let Some(handle) = heap.handle(index) { - rest(4, || host.texture_slot_show_after(list, index, handle)); + shown(1, || host.texture_slot_show_after(list, index, handle)); } } else { - at_once(); + at_once(false); } } let again = partly.map(|mapped| (frame + 1, mapped)); @@ -3422,10 +3479,10 @@ impl State { sync.publish.push((index, handle, key)); } if shows_other && let Some(handle) = heap.handle(index) { - rest(4, || host.texture_slot_flip(list, index, handle)); + shown(2, || host.texture_slot_flip(list, index, handle)); } if late && let Some(handle) = heap.handle(index) { - rest(4, || host.texture_slot_show_after(list, index, handle)); + shown(1, || host.texture_slot_show_after(list, index, handle)); } #[cfg(feature = "diagnostics")] if crate::book::on() diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 1140960..4e4acef 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -9170,6 +9170,64 @@ impl Meter { slots[9], ); } + let shown = crate::draw::HEAP_TEXTURE_SHOWN.each_ref().map(|n| n.swap(0, Ordering::Relaxed)); + let nothing = crate::draw::HEAP_TEXTURE_NOTHING.each_ref().map(|n| n.swap(0, Ordering::Relaxed)); + let ahead_of = crate::draw::HEAP_TEXTURE_BEFORE.each_ref().map(|n| n.swap(0, Ordering::Relaxed) / 1_000_000); + if all > 0 || parts[7] > 0 { + let [made_ns, after_ns, flipped_ns, made, after, flipped] = shown; + let [due_by_memory, remade, due_list] = ahead_of; + tracing::info!( + "the pass over textures picked by index, its parts in the last 5 seconds: slots shown: {} ms made at \ + once ({made}), {} ms shown after their upload ({after}), {} ms flipped ({flipped}); nothing to bring \ + up to date: {} views of textures the host writes, {} without a texture, {} the host's own texture, {} of \ + them made at once; before the loops: {due_by_memory} ms making slots due, {remade} ms views made again \ + and slots flipped, {due_list} ms taking the slots due, {} ms the rest", + made_ns / 1_000_000, + after_ns / 1_000_000, + flipped_ns / 1_000_000, + nothing[0], + nothing[1], + nothing[2], + nothing[3], + parts[7].saturating_sub(due_by_memory + remade + due_list), + ); + } + // The queue's lock by place (`crate::descriptor::queue_site`). + let lock = crate::descriptor::QUEUE_LOCK.each_ref().map(|row| row.each_ref().map(|n| n.swap(0, Ordering::Relaxed))); + let [taken, waited, held, skipped] = lock; + if taken.iter().sum::() + skipped.iter().sum::() > 0 { + let ms = |ns: u64| ns / 1_000_000; + tracing::info!( + "the texture queue's lock in the last 5 seconds: taken {} times ({} by the pass asking of or telling of a \ + slot, {} taking the slots due, {} making slots due, {} noting a slot's record, {} by table reads, {} for \ + stale views, {} else); waited for {} ms ({}, {}, {}, {}, {}, {}, {} ms by the same places); held {} ms \ + taking the slots due, {} ms making slots due, {} ms for stale views; not taken {} times, as nothing would \ + have changed ({} noting a slot's record, {} by table reads, {} for stale views)", + taken.iter().sum::(), + taken[0], + taken[1], + taken[2], + taken[3], + taken[4], + taken[5], + taken[6], + ms(waited.iter().sum::()), + ms(waited[0]), + ms(waited[1]), + ms(waited[2]), + ms(waited[3]), + ms(waited[4]), + ms(waited[5]), + ms(waited[6]), + ms(held[1]), + ms(held[2]), + ms(held[5]), + skipped.iter().sum::(), + skipped[3], + skipped[4], + skipped[5], + ); + } } if crate::resource::held_only() { let [copies, bytes, grown, whole] = [0, 1, 2, 3].map(|i| crate::resource::HELD[i].load(Ordering::Relaxed));