Skip to content
Closed
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
37 changes: 36 additions & 1 deletion crates/victory-gpu/src/copystat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,26 @@ pub(crate) enum C {
TwinToDepthBytes,
TwinToTwin,
TwinToTwinBytes,
/// The heap sync (`crate::descriptor::sync_heaps`): pages of title heaps
/// read, and of their slots those skipped as made from the record they
/// hold, those holding no view, those checked under the locks (and of
/// them those in blocks not verified since views were last forgotten),
/// and those made again; the time spent reading pages, waiting for the
/// heaps' lock, checking and making; the title's releases that forgot
/// views (each makes every block check its slots again).
SyncPages,
SyncFast,
SyncNoView,
SyncChecked,
SyncUnverified,
SyncMade,
SyncReadNs,
SyncLockNs,
SyncCheckNs,
SyncMakeNs,
ViewsForgot,
}
const COUNTERS: usize = C::TwinToTwinBytes as usize + 1;
const COUNTERS: usize = C::ViewsForgot as usize + 1;
static COUNT: [AtomicU64; COUNTERS] = [const { AtomicU64::new(0) }; COUNTERS];

pub(crate) fn add(c: C, n: u64) {
Expand Down Expand Up @@ -450,6 +468,23 @@ pub(crate) fn log(frames: f64) {
c[C::TwinToTwin as usize],
mb(c[C::TwinToTwinBytes as usize]),
);
tracing::info!(
" heap sync per frame: {:.0} pages, {:.0} slots: {:.0} made from their record already, {:.0} no view, \
{:.0} checked ({:.0} in blocks not verified) in {:.1} ms, {:.0} made again in {:.1} ms; pages read in \
{:.1} ms, {:.1} ms waiting for the heaps' lock; {:.1} releases forgot views",
c[C::SyncPages as usize],
c[C::SyncSlots as usize],
c[C::SyncFast as usize],
c[C::SyncNoView as usize],
c[C::SyncChecked as usize],
c[C::SyncUnverified as usize],
c[C::SyncCheckNs as usize] / 1e6,
c[C::SyncMade as usize],
c[C::SyncMakeNs as usize] / 1e6,
c[C::SyncReadNs as usize] / 1e6,
c[C::SyncLockNs as usize] / 1e6,
c[C::ViewsForgot as usize],
);
let mut snapshots: Vec<((u64, u64), u64)> =
SNAPSHOTS.lock().unwrap_or_else(|e| e.into_inner()).take().into_iter().flatten().collect();
snapshots.sort_by_key(|&(_, bytes)| std::cmp::Reverse(bytes));
Expand Down
35 changes: 34 additions & 1 deletion crates/victory-gpu/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,14 @@ pub(crate) fn sync_heaps() {
for (start, at, ranges) in work {
for (from, to) in ranges {
for page in (from & !(page_size - 1)..to).step_by(page_size) {
#[cfg(feature = "diagnostics")]
let waited = crate::watch::TimedNs(
std::time::Instant::now(),
crate::copystat::C::SyncLockNs,
);
let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
#[cfg(feature = "diagnostics")]
drop(waited);
if !heaps.iter().any(|&(s, _, h)| (s, h) == (start, at)) {
break;
}
Expand All @@ -304,7 +311,17 @@ pub(crate) fn sync_heaps() {
// By page: read the whole mapped range once; if part of a 64 KiB
// page is unmapped, fall back to 4 KiB OS pages so mapped slots
// are never skipped.
if crate::host::read_into(page as *const u8, &mut page_buf[..len]) {
#[cfg(feature = "diagnostics")]
let reading = crate::watch::TimedNs(
std::time::Instant::now(),
crate::copystat::C::SyncReadNs,
);
let mapped = crate::host::read_into(page as *const u8, &mut page_buf[..len]);
#[cfg(feature = "diagnostics")]
drop(reading);
if mapped {
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::SyncPages, 1);
sync_range(host, heap.desc.count, start, step, page, end, &page_buf[..len]);
} else {
for sub in (page & !(SUB_PAGE - 1)..end).step_by(SUB_PAGE) {
Expand Down Expand Up @@ -847,6 +864,8 @@ pub(crate) fn forget(resource: usize) {
}
if removed {
VIEWS_FORGOT.fetch_add(1, std::sync::atomic::Ordering::Release);
#[cfg(all(windows, feature = "diagnostics"))]
crate::copystat::add(crate::copystat::C::ViewsForgot, 1);
}
}

Expand Down Expand Up @@ -1570,6 +1589,18 @@ pub(crate) mod tests {

/// A record the title copies into its shader-visible heap as bytes (no
/// call) is made on the host at the next sync, and only then.
/// Waits (a few seconds at most) for `made` to hold. The ranges a title
/// wrote are taken by whichever thread calls `sync_heaps` first, so a
/// test's heap may be made by another test's call that is still running
/// when this test's own call finds nothing left to do.
#[cfg(windows)]
fn made_by_the_sync(made: impl Fn() -> bool) {
let until = std::time::Instant::now() + std::time::Duration::from_secs(5);
while !made() && std::time::Instant::now() < until {
std::thread::yield_now();
}
}

#[cfg(windows)]
#[test]
fn records_copied_as_bytes_are_made_at_the_next_sync() {
Expand Down Expand Up @@ -1597,6 +1628,7 @@ pub(crate) mod tests {
unsafe { std::ptr::copy_nonoverlapping(at as *const u8, slot as *mut u8, 16) };
assert!(!host.made(5));
sync_heaps();
made_by_the_sync(|| host.made(5));
assert!(host.made(5), "made at the sync");
}
slot::<extern "system" fn(This) -> u32>(h, 0x10)(h);
Expand Down Expand Up @@ -1644,6 +1676,7 @@ pub(crate) mod tests {
unsafe { std::ptr::copy_nonoverlapping(at as *const u8, slot1 as *mut u8, 16) };
assert!(!host.made(1));
sync_heaps();
made_by_the_sync(|| host.made(1));
assert!(host.made(1), "partial 64 KiB page / small heap must still sync");
}
slot::<extern "system" fn(This) -> u32>(h, 0x10)(h);
Expand Down
Loading
Loading