Lighter refreshes of host copies; landing past the guards; the heap pass spread
VICTORY_LIGHT_REFRESH (on): view memory nothing wrote since the last asking is known by a bit a block, copies nothing of which changed wake no thread, the ends of a range are not copied at every refresh, long copies are split. VICTORY_LAND_WINDOW=1: what shaders wrote goes into guarded title memory through a view of ours of the section, the guard stays. VICTORY_SEND_CHANGED=1: pages taken again go up to the card where they differ. VICTORY_HEAP_SPREAD (on): a sixteenth of the heap's slots at every pass. Three log lines split the preparing, the askings and the heap pass. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c897fa28ff
commit
2aa8134b59
9 files changed
+1549
-59
No files matched your search
@@ -35,6 +35,11 @@ pub(crate) struct Shadow {
|
||||
/// Per page of title memory from `start`'s on: the write-watch asking
|
||||
/// after which it was last copied (0: never).
|
||||
copied: Mutex<Vec<u64>>,
|
||||
/// Two ranges of title memory taken whole, the last one and the longest,
|
||||
/// and the asking after which each was ([`Shadow::copy_stale`]). A page
|
||||
/// a range holds in part cannot say so in `copied`. Locked inside
|
||||
/// `copied`'s lock.
|
||||
ranged: Mutex<[(usize, usize, u64); 2]>,
|
||||
/// Whether the memory is write-watched: unknown, yes, no.
|
||||
watched: AtomicU8,
|
||||
/// Ranges shaders of a submission not yet copied back may be writing:
|
||||
@@ -1028,6 +1033,11 @@ struct Workers {
|
||||
more: std::sync::Condvar,
|
||||
}
|
||||
|
||||
/// How many threads are kept for [`wide`].
|
||||
fn wide_threads() -> usize {
|
||||
std::thread::available_parallelism().map_or(4, |n| n.get() / 4).clamp(2, 6)
|
||||
}
|
||||
|
||||
fn workers() -> &'static Workers {
|
||||
static WORKERS: std::sync::OnceLock<Workers> = std::sync::OnceLock::new();
|
||||
static STARTED: std::sync::Once = std::sync::Once::new();
|
||||
@@ -1036,8 +1046,7 @@ fn workers() -> &'static Workers {
|
||||
more: std::sync::Condvar::new(),
|
||||
});
|
||||
STARTED.call_once(|| {
|
||||
let n = std::thread::available_parallelism().map_or(4, |n| n.get() / 4).clamp(2, 6);
|
||||
for i in 0..n {
|
||||
for i in 0..wide_threads() {
|
||||
let _ = std::thread::Builder::new().name(format!("victory-wide-{i}")).spawn(move || {
|
||||
let workers = WORKERS.get().expect("made above");
|
||||
loop {
|
||||
@@ -1103,6 +1112,30 @@ pub(crate) fn wide_run(jobs: Vec<Box<dyn FnOnce() + Send>>) {
|
||||
wide(jobs);
|
||||
}
|
||||
|
||||
/// Calls `work` with every number under `n`, on the kept threads and on
|
||||
/// this one, the next number to whichever is free: for thousands of small
|
||||
/// pieces of work, where a job each ([`wide`]) is mostly the jobs' queue.
|
||||
pub(crate) fn wide_each(n: usize, work: impl Fn(usize) + Send + Sync + 'static) {
|
||||
let next = Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
||||
let work = Arc::new(work);
|
||||
let threads = (wide_threads() + 1).min(n);
|
||||
let jobs = (0..threads)
|
||||
.map(|_| {
|
||||
let (next, work) = (next.clone(), work.clone());
|
||||
Box::new(move || {
|
||||
loop {
|
||||
let i = next.fetch_add(1, Ordering::Relaxed);
|
||||
if i >= n {
|
||||
break;
|
||||
}
|
||||
work(i);
|
||||
}
|
||||
}) as Job
|
||||
})
|
||||
.collect();
|
||||
wide(jobs);
|
||||
}
|
||||
|
||||
/// About this many bytes to a piece of work for [`wide`].
|
||||
const WIDE_JOB: usize = 1 << 20;
|
||||
|
||||
@@ -1390,7 +1423,11 @@ fn changed_runs(now: &[u8], was: &[u8]) -> Vec<(usize, usize)> {
|
||||
let mut runs = Vec::new();
|
||||
let mut i = 0;
|
||||
while i < n {
|
||||
// Equal bytes, eight at a time while they last.
|
||||
// Equal bytes, many at a time while they last (a page looked at
|
||||
// under a flight's writes hardly ever differs at all).
|
||||
while i + 512 <= n && now[i..i + 512] == was[i..i + 512] {
|
||||
i += 512;
|
||||
}
|
||||
while i + 8 <= n && now[i..i + 8] == was[i..i + 8] {
|
||||
i += 8;
|
||||
}
|
||||
@@ -1409,6 +1446,52 @@ fn changed_runs(now: &[u8], was: &[u8]) -> Vec<(usize, usize)> {
|
||||
runs
|
||||
}
|
||||
|
||||
/// Whether pages of a copy in the card's memory that a refresh takes again
|
||||
/// go up to the card only where they are not what the CPU's side holds
|
||||
/// already (`VICTORY_SEND_CHANGED=1`; off by default).
|
||||
///
|
||||
/// A refresh copies the 64 KiB pages that were written since it last took
|
||||
/// them, and all of each went up and was hashed there again. Most of them
|
||||
/// the title did not write: the relay put what shaders wrote into title
|
||||
/// memory, and the card and the CPU's side hold those bytes already
|
||||
/// ([`Shadow::card_merge_relay`]). Run record-1: 1,500 pieces (98 MB) a
|
||||
/// frame fetched from the card, 103 MB in 328 ranges sent up, 322 hash
|
||||
/// passes, while the title's own writes took some 260 guards off.
|
||||
///
|
||||
/// What it rests on: the card's side holds what the CPU's side holds, but
|
||||
/// for ranges shaders may be writing (no refresh touches them) and what is
|
||||
/// still to go up. A page's first taking always goes up whole, so does one
|
||||
/// that could not be read; after that every byte that changes on the CPU's
|
||||
/// side goes up (the bytes compared are the bytes stored).
|
||||
///
|
||||
/// Why it is not on by default: a shader that writes a copy through a view
|
||||
/// nobody declared (`crate::draw`, buffers picked by index) breaks that
|
||||
/// rule. Taken again, its page went up over what it wrote; with the switch
|
||||
/// its bytes stay, as they would on the console, until the title changes the
|
||||
/// same 64 bytes: clearing them to the zeros they held before is then no
|
||||
/// change.
|
||||
fn send_changed() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
if crate::copystat::mode() != 0 {
|
||||
return false;
|
||||
}
|
||||
std::env::var_os("VICTORY_SEND_CHANGED").is_some_and(|v| v == "1") && crate::safecopy::on()
|
||||
})
|
||||
}
|
||||
|
||||
/// Since the last frame-rate line, of pages taken again into copies in the
|
||||
/// card's memory ([`send_changed`]): bytes compared, those of pages that
|
||||
/// held the same, and bytes to go up.
|
||||
static SEND: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3];
|
||||
|
||||
/// [`SEND`], taken.
|
||||
#[cfg(feature = "diagnostics")]
|
||||
pub(crate) fn send_stats() -> [u64; 3] {
|
||||
[0, 1, 2].map(|i| SEND[i].swap(0, Ordering::Relaxed))
|
||||
}
|
||||
|
||||
/// Whether what shaders wrote goes into title memory run by run
|
||||
/// ([`put_changed`]; on by default, `VICTORY_EXACT_BACK=0` writes the whole
|
||||
/// stretch from the first changed byte to the last, as before).
|
||||
@@ -1452,6 +1535,16 @@ fn dword_runs(at: usize, title: &[u8], merged: &[u8], gap: usize) -> Vec<(usize,
|
||||
/// shortcut: a piece with very many runs has them joined over small gaps,
|
||||
/// where the old race is left.
|
||||
fn put_changed(at: usize, title: &[u8], merged: &[u8]) -> usize {
|
||||
// Past the guard, if it is one (`VICTORY_LAND_WINDOW`).
|
||||
if exact_back()
|
||||
&& crate::safecopy::on()
|
||||
// SAFETY: the same title memory through a view of ours.
|
||||
&& crate::watch::land_through(at, title.len(), |to| unsafe { crate::safecopy::put_changed(to, title, merged) })
|
||||
{
|
||||
PUT[0].fetch_add(1, Ordering::Relaxed);
|
||||
PUT[2].fetch_add(merged.len() as u64, Ordering::Relaxed);
|
||||
return merged.len();
|
||||
}
|
||||
crate::watch::landing(at, title.len());
|
||||
if !exact_back() {
|
||||
crate::host::write_mapped(at as *mut u8, merged.as_ptr(), merged.len());
|
||||
@@ -1520,6 +1613,34 @@ mod merge_tests {
|
||||
|
||||
/// The runs hold exactly the bytes that differ: putting them into the
|
||||
/// old bytes gives the new ones, and no run holds an equal byte.
|
||||
/// What looking at a page that did not change costs, against the look
|
||||
/// eight bytes at a time that it was (a frame has some 250 of them, on
|
||||
/// the submitting thread). Run with `--ignored --nocapture`.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn runs_of_equal_pages() {
|
||||
let (a, b) = (vec![7u8; 64 << 10], vec![7u8; 64 << 10]);
|
||||
let eights = || {
|
||||
let (now, was) = (std::hint::black_box(&a), std::hint::black_box(&b));
|
||||
let mut i = 0;
|
||||
while i + 8 <= now.len() && now[i..i + 8] == was[i..i + 8] {
|
||||
i += 8;
|
||||
}
|
||||
std::hint::black_box(i);
|
||||
};
|
||||
let best = |f: &dyn Fn()| {
|
||||
(0..500)
|
||||
.map(|_| {
|
||||
let t = std::time::Instant::now();
|
||||
f();
|
||||
t.elapsed().as_secs_f64() * 1e6
|
||||
})
|
||||
.fold(f64::MAX, f64::min)
|
||||
};
|
||||
let runs = || drop(std::hint::black_box(changed_runs(std::hint::black_box(&a), std::hint::black_box(&b))));
|
||||
println!("64 KiB that are the same: {:.1} us by eights, {:.1} us now", best(&eights), best(&runs));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changed_runs_are_exactly_the_bytes_that_differ() {
|
||||
let was: Vec<u8> = (0..300u32).map(|i| (i * 13 + 5) as u8).collect();
|
||||
@@ -1871,6 +1992,7 @@ impl Shadow {
|
||||
start,
|
||||
len,
|
||||
copied: Mutex::new(vec![0; pages as usize]),
|
||||
ranged: Mutex::default(),
|
||||
watched: AtomicU8::new(0),
|
||||
pending: Mutex::default(),
|
||||
flights: Mutex::default(),
|
||||
@@ -2547,6 +2669,43 @@ impl Shadow {
|
||||
self.update_free(from, to)
|
||||
}
|
||||
|
||||
/// Whether [`Shadow::update`] of `[address, address + len)` would copy
|
||||
/// nothing, and knows it without asking anything
|
||||
/// (`crate::watch::nothing_new`). `false` if it would have to ask.
|
||||
pub(crate) fn nothing_new(&self, address: usize, len: usize) -> bool {
|
||||
if self.place.is_some() {
|
||||
return true;
|
||||
}
|
||||
let (start, end) = (self.start as usize, (self.start + self.len) as usize);
|
||||
let (from, to) = (address.max(start), (address + len).min(end));
|
||||
if from >= to {
|
||||
return true;
|
||||
}
|
||||
if self.watched.load(Ordering::Relaxed) != 1 {
|
||||
return false;
|
||||
}
|
||||
// Around ranges shaders are writing the refresh asks gap by gap.
|
||||
if self.pending.lock().unwrap_or_else(|e| e.into_inner()).iter().any(|&(a, e)| a < to && e > from) {
|
||||
return false;
|
||||
}
|
||||
let copied = self.copied.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let same = crate::watch::nothing_new(from, to, start / PAGE, &copied, self.ranged(from, to));
|
||||
#[cfg(feature = "diagnostics")]
|
||||
if same {
|
||||
use crate::copystat::{C, add};
|
||||
add(C::Checked, (to - from) as u64);
|
||||
add(C::Unchanged, (to - from) as u64);
|
||||
}
|
||||
same
|
||||
}
|
||||
|
||||
/// The latest asking after which a range that holds `[from, to)` was
|
||||
/// taken whole (0: none is known). The caller holds `copied`'s lock.
|
||||
fn ranged(&self, from: usize, to: usize) -> u64 {
|
||||
let ranged = self.ranged.lock().unwrap_or_else(|e| e.into_inner());
|
||||
ranged.iter().filter(|r| r.0 <= from && to <= r.1).map(|r| r.2).max().unwrap_or(0)
|
||||
}
|
||||
|
||||
/// [`Shadow::update`] of `[from, to)` (inside the buffer) where no
|
||||
/// shader writes.
|
||||
fn update_free(&self, from: usize, to: usize) -> usize {
|
||||
@@ -2581,7 +2740,13 @@ impl Shadow {
|
||||
let base = self.start as usize / PAGE;
|
||||
let mut copied = self.copied.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut writes = written.iter().peekable();
|
||||
let (mut bytes, mut run) = (0, None);
|
||||
// Pages the range holds in part are never noted in `copied`, and
|
||||
// were copied again at every refresh of the range, written or not:
|
||||
// of the heap's buffer views alone two ends for each of 4,400
|
||||
// copies, every frame. A range that was taken whole, and not written
|
||||
// since, is as it was.
|
||||
let ranged = if crate::watch::light() { self.ranged(from, to) } else { 0 };
|
||||
let (mut runs, mut run) = (Vec::new(), None::<(usize, bool)>);
|
||||
let last = (to - 1) / PAGE;
|
||||
for page in from / PAGE..=last {
|
||||
let write = match writes.peek() {
|
||||
@@ -2592,25 +2757,111 @@ impl Shadow {
|
||||
_ => 0,
|
||||
};
|
||||
let at = &mut copied[page - base];
|
||||
if *at == 0 || write > *at {
|
||||
let taken = (*at).max(ranged);
|
||||
if taken == 0 || write > taken {
|
||||
// A page only partly in the range is copied in part: it
|
||||
// stays stale for the rest.
|
||||
let (start, end) = (self.start as usize, (self.start + self.len) as usize);
|
||||
if (page * PAGE).max(start) >= from && ((page + 1) * PAGE).min(end) <= to {
|
||||
*at = now;
|
||||
}
|
||||
run.get_or_insert(page);
|
||||
} else if let Some(first) = run.take() {
|
||||
bytes += self.copy((first * PAGE).max(from), (page * PAGE).min(to));
|
||||
// Cut where pages taken before meet pages never taken.
|
||||
let again = taken != 0;
|
||||
if let Some((first, known)) = run
|
||||
&& known != again
|
||||
{
|
||||
runs.push(((first * PAGE).max(from), (page * PAGE).min(to), known));
|
||||
run = None;
|
||||
}
|
||||
run.get_or_insert((page, again));
|
||||
} else if let Some((first, known)) = run.take() {
|
||||
runs.push(((first * PAGE).max(from), (page * PAGE).min(to), known));
|
||||
}
|
||||
}
|
||||
// Cut to the range: next to it shaders may be writing the copy.
|
||||
if let Some(first) = run {
|
||||
bytes += self.copy((first * PAGE).max(from), ((last + 1) * PAGE).min(to));
|
||||
if let Some((first, known)) = run {
|
||||
runs.push(((first * PAGE).max(from), ((last + 1) * PAGE).min(to), known));
|
||||
}
|
||||
let bytes = self.take_all(runs);
|
||||
if crate::watch::light() {
|
||||
// All of the range is in the copy as of this asking.
|
||||
let mut ranged = self.ranged.lock().unwrap_or_else(|e| e.into_inner());
|
||||
ranged[0] = (from, to, now);
|
||||
if to - from >= ranged[1].1 - ranged[1].0 {
|
||||
ranged[1] = (from, to, now);
|
||||
}
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
/// [`Shadow::take`] of `runs` (from, to, known), on several threads if
|
||||
/// they are long: the big heap's copy has some 4 MB of them in each of
|
||||
/// 14 refreshes a frame, on one thread while the others had nothing.
|
||||
fn take_all(&self, runs: Vec<(usize, usize, bool)>) -> usize {
|
||||
const LONG: usize = 1 << 20;
|
||||
const PART: usize = 256 << 10;
|
||||
let bytes = runs.iter().map(|r| r.1 - r.0).sum();
|
||||
if bytes < LONG || !crate::watch::light() {
|
||||
for (from, to, known) in runs {
|
||||
self.take(from, to, known);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
let mut parts = Vec::with_capacity(bytes / PART + runs.len());
|
||||
for (from, to, known) in runs {
|
||||
let mut at = from;
|
||||
while at < to {
|
||||
// Whole pages to a part.
|
||||
let next = ((at / PAGE) * PAGE + PART).min(to);
|
||||
parts.push((at, next, known));
|
||||
at = next;
|
||||
}
|
||||
}
|
||||
let this = std::ptr::from_ref(self) as usize;
|
||||
wide_each(parts.len(), move |i| {
|
||||
// SAFETY: `wide_each` returns when every part is done, before
|
||||
// the borrow of `self` ends. The parts lie apart.
|
||||
let this = unsafe { &*(this as *const Shadow) };
|
||||
let (from, to, known) = parts[i];
|
||||
this.take(from, to, known);
|
||||
});
|
||||
bytes
|
||||
}
|
||||
|
||||
/// Copies title memory `[from, to)` (inside the buffer) into the copy;
|
||||
/// `known`: the copy held it before ([`send_changed`]).
|
||||
fn take(&self, from: usize, to: usize, known: bool) -> usize {
|
||||
if !known || self.card.is_none() || !send_changed() {
|
||||
return self.copy(from, to);
|
||||
}
|
||||
let start = self.start as usize;
|
||||
let mut at = from;
|
||||
while at < to {
|
||||
let n = (PAGE - at % PAGE).min(to - at);
|
||||
// SAFETY: inside the mapping of the copy's CPU side, which
|
||||
// nothing else writes there: no shader's range holds it.
|
||||
let found = unsafe { crate::safecopy::update(self.data.add(at - start), at as *const u8, n) };
|
||||
SEND[0].fetch_add(n as u64, Ordering::Relaxed);
|
||||
match found {
|
||||
crate::safecopy::Updated::Same => {
|
||||
SEND[1].fetch_add(n as u64, Ordering::Relaxed);
|
||||
}
|
||||
crate::safecopy::Updated::Changed(a, b) => {
|
||||
SEND[2].fetch_add((b - a) as u64, Ordering::Relaxed);
|
||||
self.changed(at - start + a, at - start + b);
|
||||
self.card_changed(at - start + a, at - start + b);
|
||||
}
|
||||
// A page is not there: as before, around it, and all of it
|
||||
// goes up.
|
||||
crate::safecopy::Updated::Failed => {
|
||||
self.copy(at, at + n);
|
||||
}
|
||||
}
|
||||
at += n;
|
||||
}
|
||||
to - from
|
||||
}
|
||||
|
||||
/// Copies title memory `[from, to)`, cut to the buffer, into the copy.
|
||||
fn copy(&self, from: usize, to: usize) -> usize {
|
||||
let (start, end) = (self.start as usize, (self.start + self.len) as usize);
|
||||
@@ -2809,6 +3060,40 @@ impl Shadow {
|
||||
}
|
||||
}
|
||||
|
||||
/// For tests: copies with no host buffer behind them.
|
||||
#[cfg(test)]
|
||||
impl Shadow {
|
||||
/// A copy at `data` of title memory `[start, start + len)`, with a card
|
||||
/// side if `card`. It is never let go: there is no buffer to release.
|
||||
pub(crate) fn unbacked(start: u64, data: *mut u8, len: u64, card: bool) -> Arc<Shadow> {
|
||||
// SAFETY: an interface pointer nothing calls.
|
||||
let none = || unsafe { std::mem::transmute::<usize, ID3D12Resource>(8) };
|
||||
let mut shadow = Shadow::new(none(), data, 0, start, len);
|
||||
if card {
|
||||
shadow.card = Some(Card {
|
||||
staging: none(),
|
||||
before: Mutex::default(),
|
||||
dirty: Mutex::default(),
|
||||
sent: Mutex::default(),
|
||||
size: len.next_multiple_of(PIECE as u64),
|
||||
});
|
||||
}
|
||||
let shadow = Arc::new(shadow);
|
||||
std::mem::forget(shadow.clone());
|
||||
shadow
|
||||
}
|
||||
|
||||
/// What shaders wrote goes into title memory ([`put_changed`]).
|
||||
pub(crate) fn put(at: usize, title: &[u8], merged: &[u8]) {
|
||||
put_changed(at, title, merged);
|
||||
}
|
||||
|
||||
/// What is to go up to the card, taken.
|
||||
pub(crate) fn take_dirty(&self) -> Vec<(usize, usize)> {
|
||||
std::mem::take(&mut *self.card.as_ref().unwrap().dirty.lock().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
struct Entry {
|
||||
end: u64,
|
||||
shadow: Option<Arc<Shadow>>,
|
||||
|
||||
Reference in new issue
Block a user