Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4301,6 +4301,19 @@ impl Meter {
crate::texture::ALIAS_COPIES.swap(0, Ordering::Relaxed) as f64 / report_frames,
crate::twin::COPIES.swap(0, Ordering::Relaxed) as f64 / report_frames,
);
// How long the threads that ran held work were held by it: a
// title thread waits there instead of going on with its frame.
let ms = |us: u64| us as f64 / 1e3 / report_frames;
let held: Vec<String> = crate::queue::WHY_NAMES
.iter()
.zip(crate::queue::take_blocked())
.map(|(why, [lock, run])| format!("{why} {:.1} + {:.1}", ms(lock), ms(run)))
.collect();
tracing::info!(
"threads held by host GPU work per frame, ms waiting for the queues' lock + \
running it: {}",
held.join(", ")
);
#[cfg(feature = "diagnostics")]
{
if let Some([(local, budget), (system, system_budget)]) = video_memory() {
Expand Down
94 changes: 90 additions & 4 deletions crates/victory-gpu/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ extern "system" fn execute(this: This, count: u32, lists: *const This) {
if crate::host::host().is_some() {
// SAFETY: the title passes `count` lists.
let lists = unsafe { std::slice::from_raw_parts(lists, count as usize) };
let mut held = held();
let mut held = held_for(Why::Submission);
for &l in lists {
for segment in crate::list::segments(l) {
let waits = segment.waits();
Expand Down Expand Up @@ -164,10 +164,61 @@ fn held() -> std::sync::MutexGuard<'static, Holding> {
HELD.lock().unwrap_or_else(|e| e.into_inner())
}

/// Microseconds the threads that ran held work spent on it, by what ran it
/// ([`Why`], in its order): `[0]` waiting for the queues' lock, `[1]` in
/// the pump, the host submissions included. The thread that called is held
/// for both: a title thread for all but [`Why::Timer`], which is ours. The
/// frame report divides them by its frames ([`take_blocked`]).
#[cfg(windows)]
static BLOCKED_US: [[AtomicU64; 2]; 6] = [const { [const { AtomicU64::new(0) }; 2] }; 6];

/// [`held`] for a caller that runs held work for `why`, its wait for the
/// lock counted in [`BLOCKED_US`].
#[cfg(windows)]
fn held_for(why: Why) -> std::sync::MutexGuard<'static, Holding> {
lock_timed(&HELD, &BLOCKED_US[why as usize][0])
}

/// Locks `lock`, adding the microseconds it waited to `waited`.
#[cfg(any(windows, test))]
fn lock_timed<'a, T>(
lock: &'a std::sync::Mutex<T>,
waited: &AtomicU64,
) -> std::sync::MutexGuard<'a, T> {
let asked = std::time::Instant::now();
let guard = lock.lock().unwrap_or_else(|e| e.into_inner());
waited.fetch_add(asked.elapsed().as_micros() as u64, Ordering::Relaxed);
guard
}

/// [`Why`]'s variants in their order, as the frame report names them: the
/// title's submissions, its queues' Signals and Waits, its CPU fence calls,
/// its presents, and our timer thread.
#[cfg(windows)]
pub(crate) const WHY_NAMES: [&str; 6] = [
"title submissions",
"queue signals",
"queue waits",
"fence calls",
"presents",
"our timer",
];
#[cfg(windows)]
const _: () = assert!(Why::Timer as usize + 1 == WHY_NAMES.len());

/// [`BLOCKED_US`] since the last call, cleared: for each [`Why`], the
/// microseconds waiting for the queues' lock and in the pump.
#[cfg(windows)]
pub(crate) fn take_blocked() -> [[u64; 2]; 6] {
BLOCKED_US
.each_ref()
.map(|w| w.each_ref().map(|a| a.swap(0, Ordering::Relaxed)))
}

/// Runs held work that may run now (`why` for the frame line).
pub(crate) fn pump_held(why: Why) {
#[cfg(windows)]
pump(&mut held(), why);
pump(&mut held_for(why), why);
#[cfg(not(windows))]
let _ = why;
}
Expand All @@ -190,8 +241,17 @@ pub(crate) fn pump_held_if_free(why: Why) {
#[cfg(windows)]
const VALVE: std::time::Duration = std::time::Duration::from_secs(10);

/// Runs what held work may run now (`why`), its time counted in
/// [`BLOCKED_US`].
#[cfg(windows)]
fn pump(held: &mut Holding, why: Why) {
let entered = std::time::Instant::now();
pump_work(held, why);
BLOCKED_US[why as usize][1].fetch_add(entered.elapsed().as_micros() as u64, Ordering::Relaxed);
}

#[cfg(windows)]
fn pump_work(held: &mut Holding, why: Why) {
if held.is_empty() {
return;
}
Expand Down Expand Up @@ -382,7 +442,7 @@ extern "system" fn signal(this: This, fence: This, value: u64) -> HRESULT {
}
#[cfg(windows)]
{
let mut held = held();
let mut held = held_for(Why::Signal);
if held.holds_work(this as usize) {
crate::fence::keep(fence);
held.push(this as usize, Item::Signal(fence as usize, value));
Expand All @@ -409,7 +469,7 @@ extern "system" fn wait(this: This, fence: This, value: u64) -> HRESULT {
}
#[cfg(windows)]
if crate::host::host().is_some() && crate::fence::value(fence) < value {
let mut held = held();
let mut held = held_for(Why::Wait);
held.push(this as usize, Item::FenceWait(fence as usize, value));
pump(&mut held, Why::Wait);
}
Expand Down Expand Up @@ -881,6 +941,32 @@ mod tests {
use super::*;
use crate::object::slot;

/// A lock another thread holds is waited for, and the wait is counted
/// in microseconds; a free lock adds next to nothing.
#[test]
fn a_timed_lock_counts_its_wait() {
let lock = std::sync::Mutex::new(());
let waited = AtomicU64::new(0);
drop(lock_timed(&lock, &waited));
assert!(waited.load(Ordering::Relaxed) < 5_000, "a free lock");
let (taken, take) = std::sync::mpsc::channel();
std::thread::scope(|s| {
s.spawn(|| {
let _held = lock.lock().unwrap();
taken.send(()).unwrap();
std::thread::sleep(std::time::Duration::from_millis(30));
});
take.recv().unwrap();
let before = waited.load(Ordering::Relaxed);
drop(lock_timed(&lock, &waited));
let counted = waited.load(Ordering::Relaxed) - before;
assert!(
counted >= 20_000,
"waited {counted} us for a lock held 30 ms"
);
});
}

/// Which plane gets shown: the one `VICTORY_PRESENT_PLANE` names when
/// it names one, else the plane whose first resource has the most
/// pixels. It only weighs the areas it is handed, so it runs on every
Expand Down