Merge local/copies

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:36:14 -06:00
commit 0a32a773ea
9 files changed
+1712 -64

No files matched your search

+754 -21
View File
@@ -4572,6 +4572,8 @@ impl Host {
// Where each run's lists start in `lists`.
let mut firsts = Vec::with_capacity(runs.len());
let mut batch = Batch::default();
#[cfg(feature = "diagnostics")]
let split = Split::begin();
// The time a settle of flights takes inside this preparing: the
// wait for the card and the copy-back have their own places on the
// frame line (run 92 showed them twice, "preparing" 250 ms for 135).
@@ -4579,7 +4581,7 @@ impl Host {
// One write-watch asking for everything the submission refreshes
// (`VICTORY_ASK_ONCE`): per copy, the span of its refreshed ranges.
if crate::watch::once() {
let mut spans: std::collections::HashMap<usize, (usize, usize)> = std::collections::HashMap::new();
let mut spans: ByCopy<(usize, usize)> = ByCopy::default();
for list in runs {
for step in &list.deferred().copies {
if let Step::Refresh(shadow, address, len) = step
@@ -4594,6 +4596,8 @@ impl Host {
}
let ranges: Vec<(usize, usize)> = spans.into_values().collect();
crate::watch::ask_once(&ranges);
#[cfg(feature = "diagnostics")]
split.part(0);
// Behind flights: the title wrote memory that a flight's shaders
// may write too, and this submission refreshes it. The flights
// are settled, and the asking made again: the copy-back wrote
@@ -4609,6 +4613,8 @@ impl Host {
self.settle(&mut submitted, WHY_STALE);
settled += settling.elapsed();
}
#[cfg(feature = "diagnostics")]
split.part(1);
for list in runs {
list.run_deferred(&mut batch, |i| own.as_ref().is_some_and(|own| own.takes(list, i)));
firsts.push(lists.len());
@@ -4619,6 +4625,8 @@ impl Host {
}
batch.flush();
crate::watch::ask_once_end();
#[cfg(feature = "diagnostics")]
split.part(2);
// The snapshots of the ranges shaders may write, if they were left
// for now (`crate::shadow::wide_back`): the refreshes are done, the
// lists have not gone to the card.
@@ -4629,6 +4637,8 @@ impl Host {
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::SnapshotUs, snapshots.elapsed().as_micros() as u64);
}
#[cfg(feature = "diagnostics")]
split.part(3);
// Copies in the card's memory: what changed on their CPU side goes
// up ahead of the lists, and a compare of what the shaders may
// write follows them in the same submission.
@@ -4642,6 +4652,8 @@ impl Host {
if let Some(check) = card.as_ref().and_then(|c| c.check.clone()) {
lists.push(Some(check));
}
#[cfg(feature = "diagnostics")]
split.part(4);
let prepared = std::time::Instant::now();
if late {
*submitted += 1;
@@ -4654,6 +4666,8 @@ impl Host {
if let Err(e) = signalled {
fail(format_args!("a submission sent ahead could not be followed by a signal ({e})"));
}
#[cfg(feature = "diagnostics")]
split.part(5);
let back = declared_writes(runs);
let writes = runs.iter().flat_map(|l| l.deferred().writes.clone()).collect();
SUBMISSION_US[0].fetch_add((prepared - started).saturating_sub(settled).as_micros() as u64, Ordering::Relaxed);
@@ -5931,43 +5945,49 @@ impl HostList {
fn run_deferred(&self, batch: &mut Batch, taken: impl Fn(usize) -> bool) {
#[cfg(feature = "diagnostics")]
use crate::copystat::{C, add};
// The time and the bytes of the copies and fills, some 13,000 a
// frame between 20,000 refreshes: summed here and added once.
let (mut us, mut bytes) = (std::time::Duration::ZERO, 0u64);
#[cfg(feature = "diagnostics")]
let (mut copies, mut refreshes, mut refreshed) = (0u64, 0u64, 0u64);
for (i, step) in self.deferred().copies.iter().enumerate() {
if matches!(step, Step::Copy(..) | Step::Fill(..)) && taken(i) {
continue;
}
let started = std::time::Instant::now();
let copied = match step {
let (started, copied) = match step {
&Step::Copy(dst, src, len) => {
let started = std::time::Instant::now();
#[cfg(feature = "diagnostics")]
{
add(C::Copies, 1);
add(C::CopyBytes, len as u64);
copies += 1;
}
batch.title_written();
copy_mapped(dst as *mut u8, src as *const u8, len, true);
// With the submission's one asking: the journal learns it.
crate::watch::wrote(dst, len);
len
(started, len)
}
&Step::Fill(dst, src, len, _) => {
let started = std::time::Instant::now();
#[cfg(feature = "diagnostics")]
{
add(C::Copies, 1);
add(C::CopyBytes, len as u64);
copies += 1;
}
copy_mapped(dst as *mut u8, src as *const u8, len, false);
len
(started, len)
}
Step::Refresh(shadow, address, len) => {
#[cfg(feature = "diagnostics")]
{
add(C::Refreshes, 1);
add(C::RefreshBytes, *len as u64);
refreshes += 1;
refreshed += *len as u64;
}
batch.add(shadow, *address, *len);
continue;
}
Step::Run(work) | Step::Host(work, _) => {
#[cfg(feature = "diagnostics")]
let started = std::time::Instant::now();
if matches!(step, Step::Run(_)) {
batch.title_written();
}
@@ -6008,12 +6028,130 @@ impl HostList {
continue;
}
};
PREPARING[0].fetch_add(started.elapsed().as_micros() as u64, Ordering::Relaxed);
PREPARING[1].fetch_add(copied as u64, Ordering::Relaxed);
us += started.elapsed();
bytes += copied as u64;
}
PREPARING[0].fetch_add(us.as_micros() as u64, Ordering::Relaxed);
PREPARING[1].fetch_add(bytes, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
{
add(C::Copies, copies);
add(C::CopyBytes, bytes);
add(C::Refreshes, refreshes);
add(C::RefreshBytes, refreshed);
SPLIT[7].fetch_add(us.as_nanos() as u64, Ordering::Relaxed);
}
}
}
/// A table by a copy's address.
pub(crate) type ByCopy<T> = std::collections::HashMap<usize, T, std::hash::BuildHasherDefault<AddressHash>>;
/// A hash for addresses of ours: the bits that tell them apart, spread. The
/// tables it serves are asked 40,000 times a frame, and hold no one's keys
/// but our own.
#[derive(Default)]
pub(crate) struct AddressHash(u64);
impl std::hash::Hasher for AddressHash {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, bytes: &[u8]) {
for &b in bytes {
self.write_usize(usize::from(b));
}
}
fn write_usize(&mut self, n: usize) {
let h = (self.0.rotate_left(5) ^ n as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
self.0 = h ^ (h >> 32);
}
}
/// Since the last frame-rate line, nanoseconds of the submitting thread's
/// preparing: the write-watch asking for a submission; the title's bytes put
/// in under flights' writes; the lists' steps, the refreshes among them; the
/// snapshots; what goes to copies in the card's memory; the lists sent; and
/// of the steps: the refreshes, and the copies and fills (those with the
/// refreshes a copy of title memory flushes).
#[cfg(feature = "diagnostics")]
static SPLIT: [std::sync::atomic::AtomicU64; 8] = [const { std::sync::atomic::AtomicU64::new(0) }; 8];
/// Since the last frame-rate line: flushes of refreshes, their refreshes,
/// those known to copy nothing, and those done on several threads.
#[cfg(feature = "diagnostics")]
static FLUSHES: [std::sync::atomic::AtomicU64; 4] = [const { std::sync::atomic::AtomicU64::new(0) }; 4];
/// Counts into [`SPLIT`].
#[cfg(feature = "diagnostics")]
struct Split(std::cell::Cell<std::time::Instant>);
#[cfg(feature = "diagnostics")]
impl Split {
fn begin() -> Split {
Split(std::cell::Cell::new(std::time::Instant::now()))
}
/// What came since the last part was part `i`.
fn part(&self, i: usize) {
let now = std::time::Instant::now();
SPLIT[i].fetch_add((now - self.0.replace(now)).as_nanos() as u64, Ordering::Relaxed);
}
}
/// The split of the preparing, per frame, for the frame-rate line.
#[cfg(feature = "diagnostics")]
fn split_line(frames: f64) {
let ms: Vec<f64> = SPLIT.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / 1e6 / frames).collect();
let f: Vec<f64> = FLUSHES.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / frames).collect();
let a: Vec<f64> = crate::watch::asks().iter().map(|&n| n as f64 / frames).collect();
let h: Vec<f64> = crate::draw::HEAP_SPLIT.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / 1e6 / frames).collect();
let s: Vec<f64> = crate::shadow::send_stats().iter().map(|&n| n as f64 / (1 << 20) as f64 / frames).collect();
tracing::info!(
"preparing per frame, the submitting thread: {:.1} ms asking the write watch, {:.1} putting the title's bytes under flights' \
writes, {:.1} in the lists' steps (refreshes {:.1} in {:.0} flushes of {:.0}, {:.0} of them known to copy nothing, {:.0} on \
several threads; copies and fills {:.1}, the refreshes before copies among them), {:.1} snapshots, {:.1} for copies in the \
card's memory, {:.1} sending the lists (VICTORY_LIGHT_REFRESH is {})",
ms[0],
ms[1],
ms[2],
ms[6],
f[0],
f[1],
f[2],
f[3],
ms[7],
ms[3],
ms[4],
ms[5],
if crate::watch::light() { "on" } else { "off" },
);
tracing::info!(
"asking about title memory per frame, all threads: {:.0} kernel askings over {:.0} MB; view ranges: {:.0} answered by their \
bits, {:.0} by the guards whole, {:.0} block by block; {:.0} blocks made read-only, {:.0} opened by ourselves; {:.1} ms \
waiting for the guards, {:.1} ms changing protections; pages taken again for the card: {:.1} MB compared, {:.1} MB of them \
as they were, {:.1} MB to go up (VICTORY_SEND_CHANGED)",
a[0],
a[1] / 1024.0,
a[2],
a[3],
a[4],
a[5],
a[6],
a[7] / 1e6,
a[8] / 1e6,
s[0],
s[1],
s[2],
);
tracing::info!(
"the pass over the heap's views per frame: {:.1} ms looking at slots, {:.1} their copies' spans and refreshes, {:.1} textures",
h[0],
h[1],
h[2],
);
}
/// Whether a submission's refreshes of host copies run on several threads,
/// a copy to a thread (`VICTORY_WIDE_REFRESH=0` disables it; `=1` forces it).
///
@@ -6075,7 +6213,7 @@ struct Batch {
/// the heap's buffers ready adds some 4,100 copies, and searching them
/// for every refresh of a submission was tens of millions of
/// comparisons a frame.
index: std::collections::HashMap<usize, usize>,
index: ByCopy<usize>,
/// Staging buffers (by address) refreshed since the last title write.
textures: std::collections::HashSet<usize>,
}
@@ -6107,7 +6245,13 @@ impl Batch {
if let Some(moved) = self.refreshes.get(at) {
self.index.insert(std::sync::Arc::as_ptr(&moved.0) as usize, at);
}
Self::update(&shadow, from, to);
#[cfg(feature = "diagnostics")]
let started = std::time::Instant::now();
if !(crate::watch::light() && shadow.nothing_new(from, to - from)) {
Self::update(&shadow, from, to);
}
#[cfg(feature = "diagnostics")]
SPLIT[6].fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed);
}
/// Brings `[from, to)` of a copy up to date, timed.
@@ -6136,6 +6280,36 @@ impl Batch {
fn flush(&mut self) {
self.index.clear();
if self.refreshes.is_empty() {
return;
}
#[cfg(feature = "diagnostics")]
let (began, all) = (std::time::Instant::now(), self.refreshes.len());
self.flush_all();
#[cfg(feature = "diagnostics")]
{
SPLIT[6].fetch_add(began.elapsed().as_nanos() as u64, Ordering::Relaxed);
FLUSHES[0].fetch_add(1, Ordering::Relaxed);
FLUSHES[1].fetch_add(all as u64, Ordering::Relaxed);
}
}
fn flush_all(&mut self) {
// Most of a flush's copies are as they were: they are told so here,
// from what the submission's asking found, before anything is asked
// and any thread woken. (Thousands of them, the heap's buffer views
// once a frame: on the threads, below.)
const FEW: usize = 256;
let light = crate::watch::light();
if light && self.refreshes.len() <= FEW {
let started = std::time::Instant::now();
#[cfg(feature = "diagnostics")]
let all = self.refreshes.len();
self.refreshes.retain(|(shadow, from, to)| !shadow.nothing_new(*from, *to - *from));
PREPARING[0].fetch_add(started.elapsed().as_micros() as u64, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
FLUSHES[2].fetch_add((all - self.refreshes.len()) as u64, Ordering::Relaxed);
}
// One write-watch asking for the copies that lie close together,
// not one per copy.
let ahead = self.refreshes.len() > 1;
@@ -6156,12 +6330,28 @@ impl Batch {
// The time counted is the time it took, not the threads' sum.
let started = std::time::Instant::now();
let before = PREPARING[0].load(Ordering::Relaxed);
let jobs: Vec<Box<dyn FnOnce() + Send>> = self
.refreshes
.drain(..)
.map(|(shadow, from, to)| Box::new(move || Self::update(&shadow, from, to)) as Box<dyn FnOnce() + Send>)
.collect();
crate::shadow::wide_run(jobs);
#[cfg(feature = "diagnostics")]
FLUSHES[3].fetch_add(self.refreshes.len() as u64, Ordering::Relaxed);
if light {
// A number to a free thread, not a job to each copy.
let refreshes = std::mem::take(&mut self.refreshes);
crate::shadow::wide_each(refreshes.len(), move |i| {
let (shadow, from, to) = &refreshes[i];
if refreshes.len() > FEW && shadow.nothing_new(*from, *to - *from) {
#[cfg(feature = "diagnostics")]
FLUSHES[2].fetch_add(1, Ordering::Relaxed);
return;
}
Self::update(shadow, *from, *to);
});
} else {
let jobs: Vec<Box<dyn FnOnce() + Send>> = self
.refreshes
.drain(..)
.map(|(shadow, from, to)| Box::new(move || Self::update(&shadow, from, to)) as Box<dyn FnOnce() + Send>)
.collect();
crate::shadow::wide_run(jobs);
}
let summed = PREPARING[0].load(Ordering::Relaxed).saturating_sub(before);
PREPARING[0].fetch_sub(summed.saturating_sub(started.elapsed().as_micros() as u64), Ordering::Relaxed);
} else {
@@ -6579,6 +6769,8 @@ impl Meter {
late_line(frames);
card_clock_line(frames, ms);
crate::shadow::back_line(frames);
#[cfg(feature = "diagnostics")]
split_line(frames);
{
let [passes, pieces, compares] = [0, 1, 2].map(|i| CARD_PASSES[i].swap(0, Ordering::Relaxed) as f64 / frames);
if passes + compares > 0.0 {
@@ -7389,6 +7581,547 @@ mod spare {
}
}
#[cfg(test)]
mod refresh_tests {
use super::Batch;
use crate::shadow::Shadow;
use std::sync::Arc;
use windows_sys::Win32::System::Memory::{
CreateFileMappingW, FILE_MAP_WRITE, MEM_COMMIT, MEM_RESERVE, MapViewOfFile, PAGE_READWRITE,
VirtualAlloc,
};
const PAGE: usize = 64 << 10;
const MEM_WRITE_WATCH: u32 = 0x20_0000;
/// Title memory for the tests (never given back: copies of it stay in
/// the guards): write-watched, or a view of a section.
fn title(len: usize, view: bool) -> usize {
// SAFETY: plain allocations.
let at = unsafe {
if view {
let section =
CreateFileMappingW(-1isize as _, std::ptr::null(), PAGE_READWRITE, 0, len as u32, std::ptr::null());
let at = MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, len).Value as usize;
// As the runtime tells of it.
crate::title_memory_section(section as usize, len as u64, true);
crate::title_memory_remapped(at, len);
crate::title_memory_view(at, len, section as usize, 0);
at
} else {
VirtualAlloc(std::ptr::null(), len, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE) as usize
}
};
assert_ne!(at, 0);
at
}
fn memory(len: usize) -> *mut u8 {
// SAFETY: a plain allocation.
unsafe { VirtualAlloc(std::ptr::null(), len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE).cast() }
}
struct Random(u64);
impl Random {
fn below(&mut self, n: usize) -> usize {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
(self.0 >> 11) as usize % n
}
}
/// Prepares as a submission does: one asking for all the ranges, the
/// refreshes in several flushes, the asking's end.
fn submission(ranges: &[(Arc<Shadow>, usize, usize)], flushes: usize) {
// One at a time, as submissions are.
static ONE: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _one = ONE.lock().unwrap_or_else(|e| e.into_inner());
let asked: Vec<(usize, usize)> = ranges.iter().filter(|r| !r.0.unwatched()).map(|r| (r.1, r.1 + r.2)).collect();
crate::watch::ask_once(&asked);
let mut batch = Batch::default();
for part in ranges.chunks(ranges.len().div_ceil(flushes).max(1)) {
for (shadow, at, len) in part {
batch.add(shadow, *at, *len);
}
batch.flush();
}
crate::watch::ask_once_end();
}
/// Whatever is written where, by the title or by us, and whichever
/// copies (of both kinds of memory, over one another, none on a page
/// line) are refreshed in which ranges: a refreshed range holds what
/// title memory holds.
#[test]
fn refreshed_ranges_hold_title_memory() {
// SAFETY (all blocks): title memory and copies of the test's own,
// written by this thread and, in `Batch::flush`, by the threads that
// refresh, which are done when it returns.
let mut random = Random(0x1234_5678_9ABC_DEF1);
const LEN: usize = 24 * PAGE;
let regions = [title(LEN, false), title(LEN, true)];
let mut copies = Vec::new();
for &region in &regions {
for (from, to) in [(0, LEN), (100, LEN - 5000), (3 * PAGE + 777, 9 * PAGE + 10), (8 * PAGE, 8 * PAGE + 300), (PAGE, 20 * PAGE)] {
let data = memory(to - from);
copies.push((Shadow::unbacked((region + from) as u64, data, (to - from) as u64, false), data));
}
}
let byte = |at: usize| unsafe { (at as *const u8).read_volatile() };
for round in 0..600 {
for _ in 0..random.below(4) {
let at = regions[random.below(2)] + random.below(LEN - 64);
let n = 1 + random.below(60);
if random.below(3) == 0 {
// What shaders wrote.
let was: Vec<u8> = (0..n).map(|i| byte(at + i)).collect();
let now: Vec<u8> = was.iter().map(|b| if random.below(2) == 0 { *b } else { random.below(256) as u8 }).collect();
Shadow::put(at, &was, &now);
assert!(now.iter().enumerate().all(|(i, &b)| byte(at + i) == b), "not put");
continue;
}
for i in 0..n {
unsafe { ((at + i) as *mut u8).write_volatile(random.below(256) as u8) };
}
}
let mut ranges = Vec::new();
for (shadow, _) in &copies {
if random.below(3) == 0 {
continue;
}
let (start, len) = shadow.span();
let (start, len) = (start as usize, len as usize);
// Often the range of the time before, as a list's is.
let from = if random.below(2) == 0 { 0 } else { random.below(len) };
let n = if random.below(2) == 0 { len - from } else { 1 + random.below(len - from) };
ranges.push((shadow.clone(), start + from, n));
}
if ranges.is_empty() {
continue;
}
let flushes = 1 + random.below(3);
submission(&ranges, flushes);
for (shadow, at, n) in &ranges {
let data = copies.iter().find(|c| Arc::ptr_eq(&c.0, shadow)).unwrap().1;
let start = shadow.span().0 as usize;
let same = unsafe {
std::slice::from_raw_parts(*at as *const u8, *n) == std::slice::from_raw_parts(data.add(at - start), *n)
};
assert!(same, "round {round}: {at:#x}+{n:#x} of a copy at {start:#x}; first byte {}", byte(*at));
}
}
}
/// A copy in the card's memory, with the card's side and the sends to it
/// played by the test.
struct Carded {
shadow: Arc<Shadow>,
data: usize,
start: usize,
len: usize,
/// The last send, the last send every fetch to come is behind, and
/// the card's bytes: the queue's order is the order it is locked in.
card: std::sync::Mutex<(u64, u64, Vec<u8>)>,
/// Whether a flight that writes it has not landed.
flying: std::sync::atomic::AtomicBool,
/// The dword of each eight its shaders write.
class: usize,
}
impl Carded {
fn new(start: usize, len: usize, class: usize) -> Carded {
let data = memory(len);
Carded {
shadow: Shadow::unbacked(start as u64, data, len as u64, true),
data: data as usize,
start,
len,
card: std::sync::Mutex::new((0, 0, vec![0; len])),
flying: std::sync::atomic::AtomicBool::new(false),
class,
}
}
/// What changed on the CPU's side goes up.
fn send(&self) {
let mut card = self.card.lock().unwrap();
card.0 += 1;
let (number, merged) = (card.0, card.1);
for (a, b) in self.shadow.send_up(number, merged) {
// SAFETY: the copy's bytes; nothing writes those that are to
// go up.
unsafe { std::ptr::copy_nonoverlapping((self.data + a) as *const u8, card.2[a..b].as_mut_ptr(), b - a) };
}
}
}
/// Everything at once: the title writes its memory on two threads, the
/// submitting thread puts its bytes in under shader writes
/// (`Shadow::merge_under_writes`), refreshes copies that overlap (two in
/// the card's memory, one not), sends up what changed and declares what
/// the next flight's shaders write, and what flights wrote is landed
/// meanwhile (`crate::land`, beside the guards). When all of it stands
/// still and the copies are refreshed once more, each of them, and the
/// card's side of those that have one, hold what title memory holds, and
/// title memory what was written last.
///
/// (It fails in the first rounds if `landed_beside` notes nothing, or
/// only takes the block out of the guards' clean ones and leaves its bit,
/// or if a fault leaves the bit; in some thousand rounds,
/// `VICTORY_TEST_ROUNDS`, if what is landed is noted before it is in.)
#[test]
fn landings_title_writes_and_light_refreshes_at_once() {
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
assert!(crate::safecopy::on());
const LEN: usize = 24 * PAGE;
let rounds: usize = std::env::var("VICTORY_TEST_ROUNDS").ok().and_then(|v| v.parse().ok()).unwrap_or(400);
let region = title(LEN, true);
let dword = |at: usize| unsafe { &*((region + at) as *const AtomicU32) };
let carded = [Carded::new(region, LEN, 3), Carded::new(region + 4 * PAGE + 1000, LEN - 6 * PAGE - 4, 5)];
let plain = {
let (start, len) = (region + 700, LEN - 9000);
let data = memory(len);
(Shadow::unbacked(start as u64, data, len as u64, false), start, len, data as usize)
};
let stop = AtomicBool::new(false);
// Also when an assertion fails on one of the threads.
struct Stop<'a>(&'a AtomicBool);
impl Drop for Stop<'_> {
fn drop(&mut self) {
self.0.store(true, Ordering::Relaxed);
}
}
// Held shared by who writes title memory, alone while it is looked at.
let still = std::sync::RwLock::new(());
let (fly, flights) = std::sync::mpsc::channel::<(usize, Vec<(usize, usize)>, u32)>();
std::thread::scope(|s| {
for t in 0..2usize {
let (stop, still) = (&stop, &still);
s.spawn(move || {
let mut random = Random(0x7777_1234_0000_0001 + t as u64 * 0x1111);
let mut value = 0u32;
while !stop.load(Ordering::Relaxed) {
{
let _moving = still.read().unwrap();
for _ in 0..3 {
value += 1;
// Of each eight dwords, one a thread.
let at = random.below(LEN / 32) * 32 + 4 * (1 + t);
dword(at).store(value, Ordering::Relaxed);
}
}
std::thread::sleep(std::time::Duration::from_micros(20));
}
});
}
// Lands flights.
{
let (carded, stop) = (&carded, &stop);
s.spawn(move || {
let _stop = Stop(stop);
while let Ok((which, ranges, value)) = flights.recv() {
let c = &carded[which];
// Fetched: the card's bytes, behind every send so far.
let (after, now) = {
let card = c.card.lock().unwrap();
(card.0, card.2.clone())
};
// By piece: 64 KiB of title memory.
let mut pieces = Vec::new();
for &(from, to) in &ranges {
let mut at = from;
while at < to {
let next = ((c.start + at) / PAGE * PAGE + PAGE - c.start).min(to);
pieces.push((at, next, now.as_ptr() as usize + at));
at = next;
}
}
let parts: Vec<_> = pieces.chunks(3).collect();
crate::land::each(parts.len(), &|i| {
let mut notes = crate::land::Notes::default();
// SAFETY: the bytes of `now`; each piece once.
unsafe { c.shadow.land_card(parts[i], after, &mut notes) };
notes.flush();
});
for &(from, to) in &ranges {
for at in (from..to).filter(|at| (c.start - region + at) % 32 == 4 * c.class) {
// (The next fetch may have brought more.)
assert!(dword(c.start - region + at).load(Ordering::Relaxed) >= value, "not landed");
}
}
let done: Vec<_> = ranges.iter().map(|&(a, b)| (c.start + a, c.start + b)).collect();
c.shadow.copy_back_done(&done);
c.card.lock().unwrap().1 = after;
c.flying.store(false, Ordering::Release);
}
});
}
let _stop = Stop(&stop);
let mut random = Random(0xABCD_EF01_2345_6789);
let check = |round: usize, random: &mut Random, quiet: bool| {
for c in &carded {
if c.shadow.writes_pending() {
let mut runs = usize::MAX;
assert!(!c.shadow.merge_under_writes(c.start, c.len, &mut runs), "round {round}");
}
}
let mut ranges = Vec::new();
for (shadow, start, len) in
carded.iter().map(|c| (&c.shadow, c.start, c.len)).chain([(&plain.0, plain.1, plain.2)])
{
// All of it, or a part; often the same part.
let (from, n) = match random.below(3) {
_ if quiet => (0, len),
0 => (0, len),
1 => (len / 3 / 4 * 4, len / 2 / 4 * 4),
_ => {
let from = random.below(len / 4) * 4;
(from, (4 + random.below((len - from) / 4) * 4).min(len - from))
}
};
ranges.push((shadow.clone(), start + from, n));
}
submission(&ranges, 1 + random.below(3));
carded.iter().for_each(Carded::send);
};
let mut value = 1u32;
for round in 0..rounds {
check(round, &mut random, false);
// The flight of this submission: its shaders' writes.
for (which, c) in carded.iter().enumerate() {
if c.flying.swap(true, Ordering::Acquire) {
continue;
}
value += 1;
let mut ranges: Vec<(usize, usize)> = Vec::new();
let first = random.below(6) * PAGE / 2;
for k in 0..2 {
let from = first + k * 9 * PAGE + 4 * random.below(9000);
let to = (from + 4 * (1 + random.below(7 * PAGE / 4))).min(c.len);
if from < to {
ranges.push((from, to));
}
}
for &(from, to) in &ranges {
c.shadow.writing(c.start + from, to - from);
}
{
let mut card = c.card.lock().unwrap();
for &(from, to) in &ranges {
for at in (from..to).filter(|at| (c.start - region + at) % 32 == 4 * c.class) {
card.2[at..at + 4].copy_from_slice(&value.to_le_bytes());
}
}
}
fly.send((which, ranges, value)).unwrap();
}
if round % 20 != 19 {
continue;
}
// Still: no flight in the air, no title write.
let _still = still.write().unwrap();
while carded.iter().any(|c| c.flying.load(Ordering::Acquire)) {
assert!(!stop.load(Ordering::Relaxed), "the landing thread failed");
std::thread::yield_now();
}
check(round, &mut random, true);
// SAFETY: the test's own memory, written by nobody now.
let bytes = |at: usize, len: usize| unsafe { std::slice::from_raw_parts(at as *const u8, len) };
let first = |a: &[u8], b: &[u8]| a.iter().zip(b).position(|(x, y)| x != y);
for c in &carded {
let (held, there) = (bytes(c.data, c.len), bytes(c.start, c.len));
assert_eq!(first(held, there), None, "round {round}: a card copy's CPU side (class {})", c.class);
let card = c.card.lock().unwrap();
assert_eq!(first(&card.2, there), None, "round {round}: a card copy's card side (class {})", c.class);
}
assert_eq!(first(bytes(plain.3, plain.2), bytes(plain.1, plain.2)), None, "round {round}: the other copy");
}
drop(fly);
});
}
/// A range nothing wrote is known to be as it was, at its ends too, and
/// is so no more once something wrote it.
#[test]
fn unchanged_ranges_are_known() {
for view in [false, true] {
let region = title(8 * PAGE, view);
// (A page read for the first time counts as written.)
// SAFETY: the test's own memory.
unsafe { std::ptr::write_bytes(region as *mut u8, 1, 8 * PAGE) };
let data = memory(8 * PAGE);
let copy = Shadow::unbacked(region as u64, data, (8 * PAGE) as u64, false);
// Ends in the middle of pages.
let (at, len) = (region + 500, 5 * PAGE);
let look = |expected: bool, what: &str| {
// The submission's one asking, which takes two ranges. (Again
// if a submission of another test ended it.)
let mut known = false;
for _ in 0..100 {
crate::watch::ask_once(&[(at, at + len), (region + 7 * PAGE, region + 8 * PAGE)]);
known = copy.nothing_new(at, len);
if known || !expected {
break;
}
}
assert_eq!(known, expected, "view {view}: {what}");
let copied = copy.update(at, len);
crate::watch::ask_once_end();
copied
};
assert_eq!(look(false, "never taken"), len);
assert_eq!(look(true, "taken"), 0);
assert_eq!(look(true, "taken"), 0);
// SAFETY: the test's own memory.
unsafe { ((region + 600) as *mut u8).write_volatile(1) };
assert_eq!(look(false, "written at its end"), PAGE - 500);
assert_eq!(look(true, "taken again"), 0);
// Outside the range, on its last page.
// SAFETY: as above.
unsafe { ((region + 5 * PAGE + 600) as *mut u8).write_volatile(1) };
assert_eq!(look(false, "written on its last page"), 500);
// A longer range was never taken whole.
assert!(!copy.nothing_new(region, 6 * PAGE));
}
}
/// A part of a copy in the card's memory taken again goes up only if it
/// differs (with `VICTORY_SEND_CHANGED=1`), whole when never taken.
#[test]
fn only_what_differs_goes_up() {
let region = title(4 * PAGE, true);
let data = memory(4 * PAGE);
let copy = Shadow::unbacked(region as u64, data, (4 * PAGE) as u64, true);
copy.update(region, 4 * PAGE);
assert_eq!(copy.take_dirty(), [(0, 4 * PAGE)], "all of it, though zeros are no change");
// What the relay does: shader writes into title memory, and into the
// CPU's side.
crate::watch::landing(region + PAGE + 640, 64);
// SAFETY: the test's own memory.
unsafe {
std::ptr::write_bytes((region + PAGE + 640) as *mut u8, 7, 64);
std::ptr::write_bytes(data.add(PAGE + 640), 7, 64);
}
copy.update(region, 4 * PAGE);
let again = copy.take_dirty();
// SAFETY: as above.
unsafe { ((region + 2 * PAGE + 128) as *mut u8).write_volatile(9) };
copy.update(region, 4 * PAGE);
let written = copy.take_dirty();
if std::env::var_os("VICTORY_SEND_CHANGED").is_some_and(|v| v == "1") {
assert_eq!(again, []);
assert_eq!(written, [(2 * PAGE + 128, 2 * PAGE + 192)]);
} else {
assert_eq!(again, [(PAGE, 2 * PAGE)]);
assert_eq!(written, [(2 * PAGE, 3 * PAGE)]);
}
// SAFETY: as above.
assert_eq!(unsafe { data.add(2 * PAGE + 128).read() }, 9);
}
/// The refreshes of a frame as run record-1 had them: 30 submissions, the
/// first with a copy for each of 4,400 ranges, the others with 60 and a
/// big copy that 55 landed pieces and 9 title writes opened, while a
/// thread lands what shaders wrote. Run with `--ignored --nocapture`,
/// and with `VICTORY_LIGHT_REFRESH=0` for the way it was.
#[test]
#[ignore]
fn frame_of_refreshes() {
let mut random = Random(0x9876_5432_1234_5671);
const EACH: usize = 224 << 10;
const BIG: usize = 174 << 20;
let watched = title(3000 * EACH, false);
let views = title(1400 * EACH, true);
let big = title(BIG, true);
// SAFETY: the test's own memory.
unsafe {
std::ptr::write_bytes(watched as *mut u8, 1, 3000 * EACH);
std::ptr::write_bytes(views as *mut u8, 1, 1400 * EACH);
std::ptr::write_bytes(big as *mut u8, 1, BIG);
}
let mut small = Vec::new();
for i in 0..4400 {
let (start, at) = if i < 3000 { (watched, i * EACH) } else { (views, (i - 3000) * EACH) };
let data = memory(EACH);
// The range views read, not all of the copy.
small.push((Shadow::unbacked((start + at) as u64, data, EACH as u64, false), start + at + 4000, EACH - 9000));
}
let heap = (Shadow::unbacked(big as u64, memory(BIG), BIG as u64, true), big, BIG);
// The same memory, as another copy: what is landed is landed through
// it, and the refreshes are of the first.
let lands = Shadow::unbacked(big as u64, memory(BIG), BIG as u64, true);
let (mut best, mut asking) = ([f64::MAX; 2], [0f64; 2]);
let (mut sum, mut landing) = (0.0, 0.0);
const FRAMES: usize = 12;
for frame in 0..FRAMES {
let mut took = [0f64; 2];
for i in 0..30 {
// The title's writes, and the relay's beside the submission.
for _ in 0..9 {
let at = views + random.below(1400 * EACH / 8) * 8;
// SAFETY: the test's own memory.
unsafe { (at as *mut u64).write_volatile(frame as u64) };
}
for _ in 0..60 {
let at = watched + random.below(3000 * EACH / 8) * 8;
// SAFETY: the test's own memory.
unsafe { (at as *mut u64).write_volatile(frame as u64) };
}
let first = random.below(BIG / PAGE - 60);
let lands = lands.clone();
let land = std::thread::spawn(move || {
let now = vec![frame as u8 ^ i as u8 ^ 0x55; PAGE];
let started = std::time::Instant::now();
for pieces in (first..first + 55).collect::<Vec<_>>().chunks(4) {
let pieces: Vec<_> =
pieces.iter().map(|p| (p * PAGE + 8, (p + 1) * PAGE - 8, now.as_ptr() as usize)).collect();
let mut notes = crate::land::Notes::default();
// SAFETY: the bytes of `now`; nothing else lands here.
unsafe { lands.land_card(&pieces, 0, &mut notes) };
notes.flush();
}
started.elapsed().as_secs_f64() * 1e3
});
let ranges: Vec<(Arc<Shadow>, usize, usize)> = if i == 0 {
small.iter().cloned().chain([heap.clone()]).collect()
} else {
(0..60).map(|_| small[random.below(4400)].clone()).chain([heap.clone()]).collect()
};
let started = std::time::Instant::now();
submission(&ranges, 3);
took[usize::from(i != 0)] += started.elapsed().as_secs_f64() * 1e3;
let put = land.join().unwrap();
if frame >= 2 {
landing += put;
}
heap.0.take_dirty();
}
if frame >= 2 {
sum += took[0] + took[1];
for k in 0..2 {
best[k] = best[k].min(took[k]);
asking[k] += took[k];
}
}
}
println!(
"VICTORY_LIGHT_REFRESH {}: a frame's refreshes {:.2} ms in the mean; the submission of all copies {:.2} (best {:.2}), the 29 \
others {:.2} (best {:.2}); the landing thread's puts {:.2}",
crate::watch::light(),
sum / (FRAMES - 2) as f64,
asking[0] / (FRAMES - 2) as f64,
best[0],
asking[1] / (FRAMES - 2) as f64,
best[1],
landing / (FRAMES - 2) as f64,
);
#[cfg(feature = "diagnostics")]
println!("per frame, as `crate::watch::asks`: {:?}", crate::watch::asks().map(|n| n / FRAMES as u64));
}
}
#[cfg(test)]
mod batch { // Batch::texture_due lives here; `mod tests` is already taken at the top of this file
use super::Batch;