diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index cabe5a1..0972983 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -302,11 +302,9 @@ pub(crate) fn sync_heaps() { let started = std::time::Instant::now(); let (mut pages, mut looked, mut mirrored, mut tabled, mut later) = (0u64, 0u64, 0u64, 0u64, 0u64); let (mut views, mut bytes) = (0u64, 0u64); - let mut spent = [0u64; 4]; + let mut spent = [0u64; 3]; let since = |at: std::time::Instant| at.elapsed().as_nanos() as u64; - let mut kinds = [0u64; 6]; - let mut page_bytes: Vec = Vec::new(); - let mut changed: Vec = Vec::new(); + let mut kinds = [0u64; 5]; let mut wait: Vec = Vec::new(); let frame = crate::frame::current(); let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner()); @@ -344,87 +342,121 @@ pub(crate) fn sync_heaps() { let began = std::time::Instant::now(); let taken = written.take(start as usize, end as usize); spent[0] += since(began); + let mut parts: Vec<(usize, usize)> = Vec::new(); for (range_from, range_end) in taken { let mut page = range_from & !(crate::watch::PAGE - 1); while page < range_end { - let from = page.max(range_from); - let to = (page + crate::watch::PAGE).min(range_end); + parts.push((page.max(range_from), (page + crate::watch::PAGE).min(range_end))); + page += crate::watch::PAGE; + } + } + // A few pages a thread at a time: read, compared with what was last + // seen there and sorted out, beside one another (14 ms a frame of one + // thread at 160 pages, run faults-6). + let began = std::time::Instant::now(); + let copied = host.copied_begin(step as usize); + let samplers = heap.samplers(); + let found: Vec> = parts.chunks(SYNC_PAGES).map(|_| Default::default()).collect(); + crate::land::each_view(true, found.len(), &|n| { + let mut out = SyncPart::default(); + let mut page_bytes: Vec = Vec::new(); + let mut changed: Vec = Vec::new(); + for &(from, to) in parts.chunks(SYNC_PAGES).nth(n).unwrap_or_default() { // The page's records are read through the kernel, into a // copy: a page the title unmaps meanwhile fails the read // where 2,048 reads in place would fault. page_bytes.resize(to - from, 0); - let began = std::time::Instant::now(); - let there = crate::host::read_into(from as *const u8, &mut page_bytes); - spent[1] += since(began); - if there { - pages += 1; - views += u64::from(crate::watch::guarded(from, to)); - bytes += (to - from) as u64; - let slots = changed_slots(from, to, start as usize, step, mirrored_count); - // A record that straddles the range's ends is left - // to the range that holds its first byte. - let first = ((from - start as usize) as u64).div_ceil(step).max(slots.start); - let Some(records) = page_bytes.get((start + first * step) as usize - from..) else { - page += crate::watch::PAGE; - continue; - }; - let count = (records.len() as u64 / step).min(slots.end.saturating_sub(first)); - looked += count; - // Only the records that are not the ones last seen - // there (`HostHeap::copied_changes`). - changed.clear(); - let began = std::time::Instant::now(); - host.copied_changes(first as u32, &records[..(count * step) as usize], step as usize, &mut changed); - spent[2] += since(began); - for &index in &changed { - let at = (u64::from(index) - first) as usize * step as usize; - let word = |n: usize| { - records - .get(at + 4 * n..at + 4 * n + 4) - .and_then(|b| b.try_into().ok()) - .map_or(0, u32::from_le_bytes) - }; - if !heap.samplers() && word(0) == VIEW_TAG { - // A draw's own table: the draw makes what it - // reads of it (`draw.rs` `tables`). - if host.copied_tabled(index, frame) { - tabled += 1; - continue; - } - // Buffer views and placed textures get no host - // slot from this anyway, they wait for a draw - // or a pass: noted some frames on, unless a - // draw's table turns out to hold them. - if matches!(word(2) >> KIND_SHIFT, CONSTANT | PLACED) && host.copied_waits() { - wait.push(index); - later += 1; - continue; - } - } - make.push(index); - mirrored += 1; - kinds[if heap.samplers() { - 3 - } else if word(0) != VIEW_TAG { - 0 - } else if word(2) >> KIND_SHIFT == 0 { - 1 - } else { - 2 - }] += 1; - kinds[5] += u64::from(!heap.samplers() && host.near_tabled(index, frame)); - } + if !crate::host::read_into(from as *const u8, &mut page_bytes) { + continue; } - page += crate::watch::PAGE; + out.pages += 1; + out.views += u64::from(crate::watch::guarded(from, to)); + out.bytes += (to - from) as u64; + let slots = changed_slots(from, to, start as usize, step, mirrored_count); + // A record that straddles the range's ends is left + // to the range that holds its first byte. + let first = ((from - start as usize) as u64).div_ceil(step).max(slots.start); + let Some(records) = page_bytes.get((start + first * step) as usize - from..) else { + continue; + }; + let count = (records.len() as u64 / step).min(slots.end.saturating_sub(first)); + out.looked += count; + // Only the records that are not the ones last seen + // there (`host::Copied::changes`). + changed.clear(); + // SAFETY: the parts are ranges of the heap apart from one + // another, and only records that lie whole in one are its. + unsafe { copied.changes(first as u32, &records[..(count * step) as usize], &mut changed) }; + for &index in &changed { + let at = (u64::from(index) - first) as usize * step as usize; + let word = |n: usize| { + records + .get(at + 4 * n..at + 4 * n + 4) + .and_then(|b| b.try_into().ok()) + .map_or(0, u32::from_le_bytes) + }; + if !samplers && word(0) == VIEW_TAG { + // A draw's own table: the draw makes what it + // reads of it (`draw.rs` `tables`). + if host.copied_tabled(index, frame) { + out.tabled += 1; + continue; + } + // Buffer views and placed textures get no host + // slot from this anyway, they wait for a draw + // or a pass: noted some frames on, unless a + // draw's table turns out to hold them. + if matches!(word(2) >> KIND_SHIFT, CONSTANT | PLACED) && host.copied_waits() { + out.wait.push(index); + continue; + } + } + // No view, in a slot that shows none: nothing to make or + // unmake (32,000 of 41,000 a frame on the water, run + // b2-water). + if word(0) != VIEW_TAG && host.shows_none(index) { + out.nothing += 1; + continue; + } + out.make.push(index); + out.kinds[if samplers { + 3 + } else if word(0) != VIEW_TAG { + 0 + } else if word(2) >> KIND_SHIFT == 0 { + 1 + } else { + 2 + }] += 1; + } + } + *found[n].lock().unwrap_or_else(|e| e.into_inner()) = out; + }); + drop(copied); + for part in found { + let mut part = part.into_inner().unwrap_or_else(|e| e.into_inner()); + pages += part.pages; + views += part.views; + bytes += part.bytes; + looked += part.looked; + tabled += part.tabled; + kinds[0] += part.nothing; + later += part.wait.len() as u64; + mirrored += part.make.len() as u64; + wait.append(&mut part.wait); + make.append(&mut part.make); + for (kind, n) in kinds.iter_mut().zip(part.kinds) { + *kind += n; } } + spent[1] += since(began); let began = std::time::Instant::now(); crate::land::each_view(true, make.len().div_ceil(SYNC_AT_A_TIME), &|n| { for &index in make.chunks(SYNC_AT_A_TIME).nth(n).unwrap_or_default() { host.mirror(index, start + u64::from(index) * step, false); } }); - spent[3] += since(began); + spent[2] += since(began); if !wait.is_empty() { host.copied_wait(frame, std::mem::take(&mut wait)); } @@ -434,8 +466,8 @@ pub(crate) fn sync_heaps() { for (count, n) in SYNCED.iter().zip([pages, looked, mirrored, micros, tabled, later]) { count.fetch_add(n, Relaxed); } - let [taking, reading, comparing, making] = spent; - for (count, n) in SYNC_SPLIT.iter().zip([views, bytes, taking, reading, comparing, making]) { + let [taking, reading, making] = spent; + for (count, n) in SYNC_SPLIT.iter().zip([views, bytes, taking, reading, making]) { count.fetch_add(n, Relaxed); } for (count, n) in SYNCED_KINDS.iter().zip(kinds) { @@ -444,9 +476,28 @@ pub(crate) fn sync_heaps() { } /// Of those mirrored, since the last frame line: no view, a view of an object, -/// another, a sampler; waited and no draw came; next to a slot of a table's. +/// another, a sampler; waited and no draw came. #[cfg(windows)] -static SYNCED_KINDS: [std::sync::atomic::AtomicU64; 6] = [const { std::sync::atomic::AtomicU64::new(0) }; 6]; +static SYNCED_KINDS: [std::sync::atomic::AtomicU64; 5] = [const { std::sync::atomic::AtomicU64::new(0) }; 5]; + +/// What a thread found on its pages of a heap, for [`sync_heaps`]. +#[cfg(windows)] +#[derive(Default)] +struct SyncPart { + pages: u64, + views: u64, + bytes: u64, + looked: u64, + tabled: u64, + nothing: u64, + make: Vec, + wait: Vec, + kinds: [u64; 5], +} + +/// Pages [`sync_heaps`] gives a thread at a time. +#[cfg(windows)] +const SYNC_PAGES: usize = 8; /// Records [`sync_heaps`] gives a thread at a time. #[cfg(windows)] @@ -462,10 +513,10 @@ const SYNC_AT_A_TIME: usize = 256; pub(crate) static SYNCED: [std::sync::atomic::AtomicU64; 7] = [const { std::sync::atomic::AtomicU64::new(0) }; 7]; /// The same, of [`SYNCED`]'s pages: those in view memory that was guarded; -/// bytes read; nanoseconds asking what was written, reading it, comparing it -/// with what was last seen, making views. +/// bytes read; nanoseconds asking what was written, reading it and comparing +/// it with what was last seen, making views. #[cfg(windows)] -static SYNC_SPLIT: [std::sync::atomic::AtomicU64; 6] = [const { std::sync::atomic::AtomicU64::new(0) }; 6]; +static SYNC_SPLIT: [std::sync::atomic::AtomicU64; 5] = [const { std::sync::atomic::AtomicU64::new(0) }; 5]; /// With the frame line: what [`sync_heaps`] did per frame over `frames`. /// Nothing while no heap page changed. @@ -481,8 +532,8 @@ pub(crate) fn sync_line(frames: f64) { ); } let [pages, looked, mirrored, micros, tabled, later, waited] = [0, 1, 2, 3, 4, 5, 6].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed)); - let split = [0, 1, 2, 3, 4, 5].map(|i| SYNC_SPLIT[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64); - let kinds = [0, 1, 2, 3, 4, 5].map(|i| SYNCED_KINDS[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames); + let split = [0, 1, 2, 3, 4].map(|i| SYNC_SPLIT[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64); + let kinds = [0, 1, 2, 3, 4].map(|i| SYNCED_KINDS[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames); if pages == 0 { return; } @@ -490,8 +541,8 @@ pub(crate) fn sync_line(frames: f64) { "descriptor records the title copied into its heap as bytes (VICTORY_HEAP_SYNC=0: not looked for): per frame \ {:.1} changed heap pages, {:.0} slots looked at, {:.1} whose record was new and was mirrored, {:.1} new ones of \ draws' tables left to the draws, {:.1} that waited for a draw first, {:.2} ms ({:.2} ms waited for); of the mirrored \ - {:.0} no views, {:.0} of objects, {:.0} others, {:.0} samplers, {:.0} that waited and no draw came, {:.0} near a table's slot; \ - {:.1} of the pages guarded views, {:.1} MB read; {:.2} ms asking, {:.2} reading, {:.2} comparing, {:.2} making", + {:.0} no views (with those of slots that show none), {:.0} of objects, {:.0} others, {:.0} samplers, {:.0} that waited and no draw came; \ + {:.1} of the pages guarded views, {:.1} MB read; {:.2} ms asking, {:.2} reading and comparing, {:.2} making", pages as f64 / frames, looked as f64 / frames, mirrored as f64 / frames, @@ -504,13 +555,11 @@ pub(crate) fn sync_line(frames: f64) { kinds[2], kinds[3], kinds[4], - kinds[5], split[0] / frames, split[1] / (1 << 20) as f64 / frames, split[2] / 1e6 / frames, split[3] / 1e6 / frames, - split[4] / 1e6 / frames, - split[5] / 1e6 / frames + split[4] / 1e6 / frames ); } diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 1b9118e..1ffdc72 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -136,7 +136,7 @@ pub(crate) struct HostHeap { /// [`HostHeap::mirror`] asks first, without the lock. Changed under it. quick: Vec, /// The records of the title's heap as the pass over records copied in - /// as bytes last saw them ([`HostHeap::copied_changes`]). Made on first + /// as bytes last saw them ([`Copied::changes`]). Made on first /// use. copied: Mutex>, /// New ones of them that wait for a draw ([`HostHeap::copied_wait`]), @@ -2885,10 +2885,10 @@ impl HostHeap { } } - /// For a run of records of the title's heap from slot `first`, `step` - /// bytes each, on a page that changed with no call - /// (`super::sync_heaps`: the title copies records into its heap as - /// bytes): the slots whose record is not the one this last saw there. + /// The records last seen of the title's heap, `step` bytes each, held for + /// threads that compare what they read of its changed pages beside one + /// another ([`Copied::changes`]; `super::sync_heaps`: the title copies + /// records into its heap as bytes). /// /// The page says only that something on it was written, and `mirror` /// by itself would take every buffer or placed-texture record on it for @@ -2896,23 +2896,15 @@ impl HostHeap { /// or a pass uses it): the texture queue's entry for the slot would /// start again, and the buffer pass would look at 2,048 slots for one /// that changed. - /// `records`: their bytes as the caller read them (through the - /// kernel, which fails on a page the title unmapped meanwhile where a - /// read in place would fault: the title unmaps heap pages too). - pub(crate) fn copied_changes(&self, first: u32, records: &[u8], step: usize, out: &mut Vec) { + pub(crate) fn copied_begin(&self, step: usize) -> Copied<'_> { let mut copied = self.copied.lock().unwrap_or_else(|e| e.into_inner()); if copied.is_empty() { // Nothing there: a slot never looked at that holds nothing is // as it was. copied.resize(self.count as usize * step, 0); } - let at = first as usize * step; - let end = (at + records.len()).min(copied.len()); - let Some(last) = copied.get_mut(at..end) else { - return; - }; - let records = &records[..last.len()]; - changed_records(last, records, step, first, out); + let (at, len) = (copied.as_mut_ptr(), copied.len()); + Copied { at, len, step, _held: copied } } /// Whether new records of draws' tables are left to the draws @@ -2941,13 +2933,6 @@ impl HostHeap { stamp != 0 && now.wrapping_sub(stamp) & !TABLED_READ <= COPIED_FRAMES } - /// Whether a slot within 64 of `index` is a table's lately (counted for - /// the log: such a slot is likely one of a table not read yet). - pub(crate) fn near_tabled(&self, index: u32, frame: u64) -> bool { - let first = index & !63; - (first..first + 64).any(|i| i < self.count && self.copied_tabled(i, frame)) - } - /// New records the title copied into slots `slots` in `frame` wait for /// the draw that may read them through a table. pub(crate) fn copied_wait(&self, frame: u64, slots: Vec) { @@ -3091,6 +3076,12 @@ impl HostHeap { Some(view) } + /// Whether slot `index` was made from no record: what [`HostHeap::mirror`] + /// would find before it did nothing for one that holds no view. + pub(crate) fn shows_none(&self, index: u32) -> bool { + self.made.get(index).unwrap_or(0) == 0 + } + /// Slot `index` is made from record `key` and stays so (0: not). Under /// the lock of `made`. fn quick_is(&self, index: u32, key: u64) { @@ -4318,6 +4309,39 @@ fn quick_mirror() -> bool { *ON.get_or_init(|| std::env::var_os("VICTORY_QUICK_MIRROR").is_none_or(|v| v != "0")) } +/// The records last seen of a heap, held ([`HostHeap::copied_begin`]). +pub(crate) struct Copied<'a> { + at: *mut u8, + len: usize, + step: usize, + _held: std::sync::MutexGuard<'a, Vec>, +} + +// SAFETY: the bytes are reached through `changes` alone, whose callers take +// parts apart from one another; the guard stays with the thread that has it. +unsafe impl Sync for Copied<'_> {} + +impl Copied<'_> { + /// The slots of `records` (as the title has them now, from slot `first` + /// on) that are not the ones last seen, which they are from here on. + /// (`records`: as the caller read them, through the kernel, which fails on + /// a page the title unmapped meanwhile where a read in place would fault: + /// the title unmaps heap pages too.) + /// + /// # Safety + /// No other call beside this one has any of these slots. + pub(crate) unsafe fn changes(&self, first: u32, records: &[u8], out: &mut Vec) { + let at = first as usize * self.step; + let end = (at + records.len()).min(self.len); + if at >= end { + return; + } + // SAFETY: within the records held; the caller's slots alone. + let last = unsafe { std::slice::from_raw_parts_mut(self.at.add(at), end - at) }; + changed_records(last, &records[..end - at], self.step, first, out); + } +} + /// The records (`step` bytes each, the first is slot `first`) that differ /// between `last` and `records` (of one length) go into `out` and `last`. /// Most of a changed page is as it was: it is looked at sixteen records at a diff --git a/crates/victory-gpu/src/host/card_steps.rs b/crates/victory-gpu/src/host/card_steps.rs index 239f576..9ee1494 100644 --- a/crates/victory-gpu/src/host/card_steps.rs +++ b/crates/victory-gpu/src/host/card_steps.rs @@ -484,12 +484,19 @@ pub(super) fn cut_for(runs: &[&HostList]) -> Option { /// Whether every page of `[from, to)` can be read (through the kernel: /// asking Windows walks the page tables, milliseconds in the title's heap). -/// `room`: for what is read. -fn mapped(from: usize, to: usize, room: &mut Vec) -> bool { - if room.len() < to - from { - room.resize(to - from, 0); - } - read_into(from as *const u8, &mut room[..to - from]) +/// `known`: pages that could be, of the same submission: its fills' places +/// lie eight to a page, and each was read whole to find out. +fn mapped(from: usize, to: usize, known: &mut HashMap) -> bool { + const PAGE: usize = 4096; + (from / PAGE..to.div_ceil(PAGE)).all(|page| { + known.contains_key(&page) || { + let there = read_into((page * PAGE).max(from) as *const u8, &mut [0]); + if there { + known.insert(page, ()); + } + there + } + }) } /// A table by addresses and numbers of ours. @@ -697,7 +704,7 @@ impl Host { // Some 2,800 fills a frame, of a few hundred places: what holds each // is looked for once, as long as no copy is added to the writers. let mut held: HashMap<(usize, usize), Arc> = HashMap::default(); - let mut room = Vec::new(); + let mut room = HashMap::default(); let mut end = 0u64; for &(i, level) in &chosen { let Some(((src, _), (dst, _))) = reads_writes(&does[i]) else { diff --git a/crates/victory-gpu/src/land.rs b/crates/victory-gpu/src/land.rs index 33a0164..2bde585 100644 --- a/crates/victory-gpu/src/land.rs +++ b/crates/victory-gpu/src/land.rs @@ -340,7 +340,7 @@ pub(crate) fn each_under(count: usize, f: &(dyn Fn(usize) + Sync)) { } /// Calls `f` with every number below `count`, on this thread and on threads -/// kept for making descriptors (`VICTORY_VIEW_THREADS`, 4 with this one; 1: +/// kept for making descriptors (`VICTORY_VIEW_THREADS`, 4 with this one, 6 for records; 1: /// on this one alone). Those for `records`, the heaps' records copied as /// bytes, are others than those for the views picked by index: submissions /// wait for the first, lists for the second, and each kept the other waiting. @@ -348,7 +348,7 @@ pub(crate) fn each_view(records: bool, count: usize, f: &(dyn Fn(usize) + Sync)) static POOLS: [OnceLock<&'static Pool>; 2] = [const { OnceLock::new() }; 2]; POOLS[usize::from(records)] .get_or_init(|| { - let threads = std::env::var("VICTORY_VIEW_THREADS").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(4usize); + let threads = std::env::var("VICTORY_VIEW_THREADS").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(if records { 6usize } else { 4 }); Pool::new(if records { "victory-records" } else { "victory-views" }, threads.clamp(1, 16)) }) .each(count, f);