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:
localandClaude Opus 5.5 committed 2026-11-24 22:03:45 -06:00
1 parent eb50022824
commit b8bf6d518c
9 files changed
+1549 -59

No files matched your search

+524 -21
View File
@@ -4176,6 +4176,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).
@@ -4183,7 +4185,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
@@ -4198,6 +4200,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
@@ -4213,6 +4217,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);
firsts.push(lists.len());
@@ -4223,6 +4229,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.
@@ -4233,6 +4241,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.
@@ -4246,6 +4256,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;
@@ -4258,6 +4270,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);
@@ -5471,40 +5485,46 @@ impl HostList {
fn run_deferred(&self, batch: &mut Batch) {
#[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 step in &self.deferred().copies {
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();
}
@@ -5545,12 +5565,132 @@ 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 ({:.0} pieces \
put past the guards, VICTORY_LAND_WINDOW); {:.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[9],
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).
///
@@ -5612,7 +5752,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>,
}
@@ -5644,7 +5784,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.
@@ -5673,6 +5819,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;
@@ -5693,12 +5869,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 {
@@ -6116,6 +6308,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 {
@@ -6853,6 +7047,315 @@ 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 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);
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 land = std::thread::spawn(move || {
let mut took = 0.0;
for piece in first..first + 55 {
let mut was = vec![0; PAGE];
assert!(super::read_into((big + piece * PAGE) as *const u8, &mut was));
let mut now = was.clone();
now[8..PAGE - 8].fill(frame as u8 ^ i as u8);
let started = std::time::Instant::now();
Shadow::put(big + piece * PAGE, &was, &now);
took += started.elapsed().as_secs_f64() * 1e3;
}
took
});
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;