Merge local/next (e6f492d) into local/tests

Blocks the kernel writes, or the runtime is unmapping, are no known holes
and not published; foreign blocks are never held; freed memory forgets its
known holes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:47:04 -06:00
commit cd37859b39
20 files changed
+3521 -181

No files matched your search

+334 -51
View File
@@ -1531,7 +1531,7 @@ struct CardWork {
fetch: Prelude,
root: ID3D12RootSignature,
pso: ID3D12PipelineState,
/// One dword per piece compared, set where the piece differs: PC
/// One dword per piece compared, odd where the piece differs: PC
/// memory, mapped (buffer, mapping, bytes).
differs: (ID3D12Resource, usize, u64),
/// Where the pieces that differ are fetched to: PC memory, mapped.
@@ -1552,7 +1552,7 @@ static CARD_WORK: Mutex<Option<CardWork>> = Mutex::new(None);
/// The compare of a declared range of a copy in the card's memory with
/// its bytes from before, one group per 64 KiB piece (cut at multiples of
/// 64 KiB of title memory, as `Shadow::card_piece` cuts them): `differs`
/// gets a dword per piece, 1 where a byte differs. The offsets are widened
/// gets a dword per piece, odd where a byte differs. The offsets are widened
/// to 16 bytes; buffers are multiples of 64 KiB, and a piece flagged for a
/// byte just outside the range costs a fetch that finds nothing.
///
@@ -1602,7 +1602,9 @@ void main(uint3 id : SV_GroupID, uint thread : SV_GroupIndex) {
h = h.yzwx ^ (h >> 15);
}
if (compare != 0u) {
differs.Store(out_at + id.x * 4u, any(h != hashes.Load4(piece * 16u)) ? 1u : 0u);
// The flag, and above it 31 bits of the hash (`torn`).
uint folded = h.x ^ (h.y * 3u) ^ (h.z * 5u) ^ (h.w * 7u);
differs.Store(out_at + id.x * 4u, (folded << 1u) | (any(h != hashes.Load4(piece * 16u)) ? 1u : 0u));
// With the copy-back on its own thread: the next compare of the
// piece finds what is written from here on.
if (compare == 2u) hashes.Store4(piece * 16u, h);
@@ -1612,6 +1614,12 @@ void main(uint3 id : SV_GroupID, uint thread : SV_GroupIndex) {
}
"#;
/// The hash [`CARD_COMPARE`] gives of a piece with its flag.
#[cfg(test)]
pub(crate) fn piece_hash(bytes: &[u8]) -> u32 {
torn::hash(bytes)
}
/// One pass of [`CARD_COMPARE`] over a span's pieces, into `differs` from
/// dword `at` on (`mode` as the shader's `compare`): a group per piece, in
/// parts a dispatch can hold.
@@ -1678,6 +1686,22 @@ struct CardRange {
at: u32,
/// Whether the card compares it; else every piece is fetched.
compared: bool,
/// Looked at by `crate::picked`, and not declared: what is found changed
/// there was written through a view nobody declared. Not landed.
picked: bool,
}
impl CardPlan {
/// `crate::picked` is told where what it looked at changed, by the
/// compare's results.
fn picked_found(&self, differs: &[u32]) {
for r in self.ranges.iter().filter(|r| r.picked) {
let changed = (0..r.span.pieces).find(|p| differs.get((r.at + p) as usize).is_some_and(|&d| torn::flagged(d)));
if let Some(p) = changed {
crate::picked::found(&r.shadow, r.shadow.card_piece(&r.span, p).0);
}
}
}
}
/// What a submission does for copies in the card's memory.
@@ -1694,6 +1718,8 @@ struct CardPlan {
/// With [`relay`]: the number of the send to copies in the card's memory
/// that the submission ran behind ([`QUEUE_ORDER`]).
sent_as: u64,
/// With [`upcheck`]: the look at what it sent up.
looks: Option<upcheck::Looks>,
}
/// One piece of submission-time work, in recorded order.
@@ -2461,14 +2487,21 @@ fn stale_under_flights(runs: &[&HostList]) -> Vec<(std::sync::Arc<crate::shadow:
// Per copy, the span of what is refreshed (a flush brings that much
// up to date at most).
let mut spans: Vec<(Arc<crate::shadow::Shadow>, usize, usize)> = Vec::new();
// Where each copy is in it, if shaders may be writing it (none begins to
// while this runs): a frame has 20,000 refreshes of some 4,500 copies.
let mut places: ByCopy<Option<usize>> = ByCopy::default();
for list in runs {
for step in &list.deferred().copies {
if let Step::Refresh(shadow, address, len) = step
&& shadow.writes_pending()
{
match spans.iter_mut().find(|s| Arc::ptr_eq(&s.0, shadow)) {
Some(s) => (s.1, s.2) = (s.1.min(*address), s.2.max(*address + *len)),
None => spans.push((shadow.clone(), *address, *address + *len)),
if let Step::Refresh(shadow, address, len) = step {
let place = places.entry(Arc::as_ptr(shadow) as usize).or_insert_with(|| {
shadow.writes_pending().then(|| {
spans.push((shadow.clone(), *address, *address + *len));
spans.len() - 1
})
});
if let Some(i) = *place {
let s = &mut spans[i];
(s.1, s.2) = (s.1.min(*address), s.2.max(*address + *len));
}
}
}
@@ -2704,6 +2737,8 @@ struct RelayFetch {
sent: u64,
/// Buffer, mapping, bytes.
buffer: Option<(ID3D12Resource, usize, u64)>,
/// Pieces caught while a later flight was writing them.
suspects: torn::Suspects,
/// What earlier fetches brought of flights not landed yet.
had: crate::land::Had,
}
@@ -2938,9 +2973,19 @@ fn late_line(frames: f64) {
r[3] / 1000.0,
r[4] / 1000.0,
);
let t: Vec<f64> = torn::STATS.iter().map(|r| r.swap(0, Ordering::Relaxed) as f64 / frames).collect();
if t[0] > 0.0 {
tracing::info!(
"fetched pieces checked per frame (VICTORY_FETCH_CHECK): {:.1}, of which {:.2} were not what their flight ended with (caught while a later flight was at them, or title bytes went up meanwhile); {:.2} were found whole when fetched again",
t[0],
t[1],
t[2],
);
}
}
crate::land::log(frames);
card_steps::log(frames);
crate::picked::log(frames);
#[cfg(feature = "diagnostics")]
log_blame(frames);
if late_exact() {
@@ -2954,6 +2999,8 @@ fn late_line(frames: f64) {
}
mod card_steps;
mod torn;
pub(crate) mod upcheck;
#[cfg(test)]
mod late_tests {
@@ -3052,18 +3099,22 @@ fn continues(last: (usize, usize, usize), next: (usize, usize, usize)) -> bool {
/// One list's lock at a time: a submission may name a list twice.
fn declared_writes(runs: &[&HostList]) -> Vec<Declared> {
let mut back: Vec<Declared> = Vec::new();
// Where each copy is in it, and the ranges seen: dispatches declare the
// same ranges thousands of times a frame.
let mut places: ByCopy<usize> = ByCopy::default();
let mut seen = std::collections::HashSet::<(usize, usize, usize), std::hash::BuildHasherDefault<AddressHash>>::default();
for list in runs {
for step in &list.deferred().copies {
if let Step::GpuWrite(shadow, address, len) = step {
let range = (*address, *address + *len);
match back.iter_mut().find(|(s, _)| std::sync::Arc::ptr_eq(s, shadow)) {
Some((_, ranges)) => {
if !ranges.contains(&range) {
ranges.push(range);
}
}
None => back.push((shadow.clone(), vec![range])),
let key = std::sync::Arc::as_ptr(shadow) as usize;
if !seen.insert((key, *address, *len)) {
continue;
}
let i = *places.entry(key).or_insert_with(|| {
back.push((shadow.clone(), Vec::new()));
back.len() - 1
});
back[i].1.push((*address, *address + *len));
}
}
}
@@ -3456,7 +3507,9 @@ impl Host {
/// `flown`: the submission is sent ahead with [`relay`]. Its compare
/// then stores the hashes it makes (see [`flagged_pieces`] and
/// `relay`).
fn card_before(&self, runs: &[&HostList], flown: bool) -> Option<CardPlan> {
/// `picked`: ranges that may be written too, and ranges to look at
/// (`crate::picked`).
fn card_before(&self, runs: &[&HostList], picked: &crate::picked::Ranges, flown: bool) -> Option<CardPlan> {
use std::sync::Arc;
if !crate::shadow::cards() {
return None;
@@ -3480,6 +3533,10 @@ impl Host {
}
}
}
let declared = crate::picked::with(declared, &picked.written);
// (Of a copy with a declared range here, that says it.)
let looked: Vec<&crate::picked::Range> =
picked.looked.iter().filter(|l| !declared.iter().any(|(s, _)| Arc::ptr_eq(s, &l.0))).collect();
// With the relay: the order of the queue is held from here until
// what is taken is on the queue, and the taking is numbered.
let mut order = relay().then(|| QUEUE_ORDER.lock().unwrap_or_else(|e| e.into_inner()));
@@ -3491,7 +3548,7 @@ impl Host {
None => 0,
};
let mut dirty = crate::shadow::card_dirty(sent_as);
if declared.is_empty() && dirty.is_empty() {
if declared.is_empty() && dirty.is_empty() && looked.is_empty() {
return None;
}
let mut work = CARD_WORK.lock().unwrap_or_else(|e| e.into_inner());
@@ -3499,7 +3556,7 @@ impl Host {
// A copy whose shader writes are declared for the first time gets
// its bytes from before, whole.
let mut whole: Vec<Arc<crate::shadow::Shadow>> = Vec::new();
for (shadow, _) in &declared {
for shadow in declared.iter().map(|d| &d.0).chain(looked.iter().map(|l| &l.0)) {
if shadow.card_make_before(|size| {
let before = self.uav_buffer(crate::shadow::card_hash_bytes(size))?;
name(&before, || "title buffer on the card, hashes of its pieces".into());
@@ -3512,6 +3569,7 @@ impl Host {
}
}
let mut used: Vec<(bool, Prelude)> = Vec::new();
let mut looks = None;
if !dirty.is_empty() {
let Some(sync) = work.sync.pop().or_else(|| self.make_prelude()) else {
fail(format_args!("no host list to bring host copies in the card's memory up to date"));
@@ -3524,6 +3582,9 @@ impl Host {
(|| -> windows::core::Result<()> {
allocator.Reset()?;
list.Reset(allocator, None)?;
if upcheck::on() {
looks = upcheck::record(self, list, &dirty);
}
// Every copy first, then one barrier call, then every
// pass: a wait between each copy and its pass had the
// card do them one after the other.
@@ -3593,6 +3654,7 @@ impl Host {
_synced: dirty.into_iter().map(|(shadow, _)| shadow).collect(),
lists: used,
sent_as,
looks,
};
// Each byte once, as the copy-back joins the declared ranges. The
// results go behind those of plans still in the air.
@@ -3619,12 +3681,30 @@ impl Host {
span,
at,
compared,
picked: false,
});
if compared {
at += span.pieces;
}
}
}
for (shadow, from, len) in looked {
let twin = shadow.card_sides().is_some_and(|(_, before, _)| before.is_some());
if let Some(span) = shadow.card_span(*from, from + len)
&& twin
&& u64::from(at + span.pieces) * 4 <= work.differs.2
{
plan.ranges.push(CardRange {
shadow: shadow.clone(),
declared: (*from, from + len),
span,
at,
compared: true,
picked: true,
});
at += span.pieces;
}
}
work.taken = at;
if plan.ranges.iter().any(|r| r.compared) {
let Some(check) = work.check.pop().or_else(|| self.make_prelude()) else {
@@ -3657,7 +3737,9 @@ impl Host {
};
list.SetComputeRootShaderResourceView(1, r.shadow.buffer.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
card_pass(list, &r.span, size as u32, r.at, if flown { 2 } else { 1 });
// (What is only looked at keeps its hashes: what
// changed there is the next declared range's to land.)
card_pass(list, &r.span, size as u32, r.at, if flown && !r.picked { 2 } else { 1 });
CARD_PASSES[2].fetch_add(1, Ordering::Relaxed);
}
list.Close()?;
@@ -3690,6 +3772,9 @@ impl Host {
/// when nothing of ours is on the card, so the compare's results start
/// from the beginning again.
fn card_done(&self, plan: CardPlan) {
if let Some(looks) = plan.looks {
looks.read();
}
let mut work = CARD_WORK.lock().unwrap_or_else(|e| e.into_inner());
let Some(work) = work.as_mut() else {
return;
@@ -3741,6 +3826,7 @@ impl Host {
event,
sent: 0,
buffer: None,
suspects: torn::Suspects::default(),
had: crate::land::Had::default(),
})
}
@@ -3770,26 +3856,34 @@ impl Host {
if let (Some(fetch), Some(first)) = (fetch.as_mut(), plans.iter().map(|p| p.1).min()) {
fetch.had.landing(first);
}
if let Some((differs, dwords)) = differs {
// SAFETY: the mapping of the results. The compares of these
// plans have run, and their dwords are given to no other plan
// before these are done with (`card_done`).
let differs = unsafe { std::slice::from_raw_parts(differs as *const u32, dwords) };
// SAFETY: the mapping of the results. The compares of these plans
// have run, and their dwords are given to no other plan before these
// are done with (`card_done`).
let differs = differs.map(|(differs, dwords)| unsafe { std::slice::from_raw_parts(differs as *const u32, dwords) });
if let Some(differs) = differs {
// Pieces caught while a later flight was at them (`torn`).
let suspects = fetch.as_ref().map(|f| &f.suspects);
let mut seen = std::collections::HashSet::new();
for &(plan, behind) in plans {
plan.picked_found(differs);
let flags: Vec<(usize, u32, bool)> = plan
.ranges
.iter()
.filter(|r| !r.picked)
.flat_map(|r| {
(0..r.span.pieces).map(move |p| {
let flagged = !r.compared || differs.get((r.at + p) as usize).is_none_or(|&d| d != 0);
(std::sync::Arc::as_ptr(&r.shadow) as usize, r.span.first + p, flagged)
let flagged = !r.compared || differs.get((r.at + p) as usize).is_none_or(|&d| torn::flagged(d));
let wanted = suspects.map_or(flagged, |s| s.wanted((r.shadow.id(), r.span.first + p), flagged));
(std::sync::Arc::as_ptr(&r.shadow) as usize, r.span.first + p, wanted)
})
})
.collect();
let fetched = flagged_pieces(&flags);
for r in &plan.ranges {
for r in plan.ranges.iter().filter(|r| !r.picked) {
let copy = std::sync::Arc::as_ptr(&r.shadow) as usize;
if upcheck::on() {
upcheck::declared(r.shadow.id(), r.span.from as usize, r.span.to as usize);
}
for p in 0..r.span.pieces {
if fetched.binary_search(&(copy, r.span.first + p)).is_err() {
continue;
@@ -3804,6 +3898,9 @@ impl Host {
continue;
}
}
if upcheck::on() {
upcheck::fetched(r.shadow.id(), lo);
}
seen.insert((copy, lo, hi));
pieces.push((r.shadow.clone(), lo, hi, total));
total += (hi - lo) as u64;
@@ -3833,6 +3930,13 @@ impl Host {
if fetch.is_none() {
*fetch = self.make_relay_fetch();
}
// On the copy queue a later flight may be at a piece while it is
// fetched: the pieces come down whole, to be hashed.
let check = (torn::on() && fetch.as_ref().is_some_and(|f| f.queue.is_some()))
.then(|| torn::Check::new(plans.iter().map(|p| p.0), differs.unwrap_or_default(), &mut pieces));
if let Some(check) = &check {
total = check.total;
}
// The sends these flights ran behind, all a fetch on the copy queue
// is known to have run behind.
let behind = plans.iter().map(|p| p.0.sent_as).max().unwrap_or(0);
@@ -3873,7 +3977,7 @@ impl Host {
(|| -> windows::core::Result<u64> {
allocator.Reset()?;
list.Reset(&allocator, None)?;
for (shadow, lo, hi, at) in &pieces {
for (shadow, lo, hi, at) in check.as_ref().map_or(&pieces, |c| &c.copies) {
list.CopyBufferRegion(&buffer, *at, &shadow.buffer, *lo as u64, (*hi - *lo) as u64);
}
list.Close()?;
@@ -3922,6 +4026,12 @@ impl Host {
let put = std::sync::atomic::AtomicU64::new(0);
land::each(jobs.len(), &|j| {
let working = std::time::Instant::now();
if let Some(check) = &check {
for i in jobs[j].clone() {
// SAFETY: as below.
unsafe { check.look(i, data) };
}
}
let mut notes = land::Notes::default();
for run in pieces[jobs[j].clone()].chunk_by(|a, b| std::sync::Arc::ptr_eq(&a.0, &b.0)) {
let addressed: Vec<(usize, usize, usize)> =
@@ -3938,7 +4048,11 @@ impl Host {
written = put.into_inner();
} else {
let mut title = Vec::new();
for (shadow, lo, hi, at) in &pieces {
for (i, (shadow, lo, hi, at)) in pieces.iter().enumerate() {
if let Some(check) = &check {
// SAFETY: as below.
unsafe { check.look(i, data) };
}
// SAFETY: the mapping of the fetched bytes, `total` of them;
// the copy into it has run.
let gpu = unsafe { std::slice::from_raw_parts((data + *at as usize) as *const u8, hi - lo) };
@@ -3950,6 +4064,9 @@ impl Host {
land::spent(land::stat::CARD, merging);
COPY_BACK[1].fetch_add(written, Ordering::Relaxed);
if let Some(f) = fetch.as_mut() {
if let Some(check) = &check {
check.done(&mut f.suspects);
}
f.had.fetched(
ahead,
pieces.iter().map(|(shadow, lo, hi, _)| (shadow.id(), shadow.span().0 as usize + lo, hi - lo)),
@@ -4121,9 +4238,10 @@ impl Host {
// where the bytes come down.
let mut pieces: Vec<(usize, usize, usize, u64)> = Vec::new();
let mut total = 0u64;
for (i, r) in plan.ranges.iter().enumerate() {
plan.picked_found(differs);
for (i, r) in plan.ranges.iter().enumerate().filter(|(_, r)| !r.picked) {
for p in 0..r.span.pieces {
if r.compared && differs[(r.at + p) as usize] == 0 {
if r.compared && !torn::flagged(differs[(r.at + p) as usize]) {
continue;
}
let (lo, hi) = r.shadow.card_piece(&r.span, p);
@@ -4563,12 +4681,18 @@ impl Host {
// one at every present).
static SWEPT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
if !behind {
if upcheck::on() {
upcheck::at_rest(self);
}
sweep();
SWEPT.store(now_ms(), Ordering::Relaxed);
} else if late_free_present() && now_ms() > SWEPT.load(Ordering::Relaxed) + 1500 {
// With the present sent ahead nothing else brings a moment
// with nothing in the air: one is made for the sweep.
self.settle(&mut submitted, WHY_SYNC);
if upcheck::on() {
upcheck::at_rest(self);
}
sweep();
SWEPT.store(now_ms(), Ordering::Relaxed);
}
@@ -4601,7 +4725,7 @@ impl Host {
for list in runs {
for step in &list.deferred().copies {
if let Step::Refresh(shadow, address, len) = step
&& !shadow.unwatched()
&& shadow.asked_ahead()
{
let span = spans
.entry(std::sync::Arc::as_ptr(shadow) as usize)
@@ -4611,6 +4735,8 @@ impl Host {
}
}
let ranges: Vec<(usize, usize)> = spans.into_values().collect();
#[cfg(feature = "diagnostics")]
SPLIT[8].fetch_add(split.so_far(), Ordering::Relaxed);
crate::watch::ask_once(&ranges);
#[cfg(feature = "diagnostics")]
split.part(0);
@@ -4618,6 +4744,9 @@ impl Host {
// may write too, and this submission refreshes it. The flights
// are settled, and the asking made again: the copy-back wrote
// title memory, which other copies of it must see.
if behind && upcheck::on() {
upcheck::reads(runs);
}
let stale = if behind { stale_under_flights(runs) } else { Vec::new() };
if !stale.is_empty() {
let settling = std::time::Instant::now();
@@ -4664,7 +4793,13 @@ impl Host {
self.settle(&mut submitted, WHY_MOST);
settled += settling.elapsed();
}
let card = self.card_before(runs, flown);
// What shaders may write through views they pick: last, so that no
// refresh of this submission leaves it alone.
let picked = crate::picked::declare(runs);
for (shadow, at, len) in &picked.written {
shadow.writing(*at, *len);
}
let card = self.card_before(runs, &picked, flown);
if let Some(check) = card.as_ref().and_then(|c| c.check.clone()) {
lists.push(Some(check));
}
@@ -4684,7 +4819,7 @@ impl Host {
}
#[cfg(feature = "diagnostics")]
split.part(5);
let back = declared_writes(runs);
let back = crate::picked::with(declared_writes(runs), &picked.written);
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);
submitting(0, 0);
@@ -4804,7 +4939,7 @@ impl Host {
if let Some(card) = &card {
self.card_back(card, &mut submitted, &mut std::collections::HashSet::new());
}
let back = declared_writes(runs);
let back = crate::picked::with(declared_writes(runs), &picked.written);
hand_back(&back);
if let Some(card) = card {
self.card_done(card);
@@ -5929,6 +6064,12 @@ impl HostList {
self.deferred().textures.insert(key)
}
/// Whether `key` was asked about ([`HostList::first_use`]) since the
/// reset.
pub(crate) fn used(&self, key: usize) -> bool {
self.deferred().textures.contains(&key)
}
/// Runs `work` when the list is submitted, in order with its copies.
/// `work` may write title memory (refreshes after it copy again).
pub(crate) fn defer(&self, work: impl Fn() + Send + 'static) {
@@ -6039,7 +6180,10 @@ impl HostList {
}
Step::Texture(staging) => {
if batch.texture_due(staging) {
staging.refresh();
batch.due.push(staging.clone());
if !wide_textures() {
batch.refresh_textures();
}
}
continue;
}
@@ -6089,9 +6233,11 @@ impl std::hash::Hasher for AddressHash {
/// 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).
/// refreshes a copy of title memory flushes). Of the asking, which ranges it
/// is of. Of the refreshes, those before ranges are given to shaders, one
/// copy at a time: nanoseconds, and how many.
#[cfg(feature = "diagnostics")]
static SPLIT: [std::sync::atomic::AtomicU64; 8] = [const { std::sync::atomic::AtomicU64::new(0) }; 8];
static SPLIT: [std::sync::atomic::AtomicU64; 11] = [const { std::sync::atomic::AtomicU64::new(0) }; 11];
/// Since the last frame-rate line: flushes of refreshes, their refreshes,
/// those known to copy nothing, and those done on several threads.
@@ -6108,6 +6254,11 @@ impl Split {
Split(std::cell::Cell::new(std::time::Instant::now()))
}
/// Nanoseconds since the last part.
fn so_far(&self) -> u64 {
self.0.get().elapsed().as_nanos() as u64
}
/// What came since the last part was part `i`.
fn part(&self, i: usize) {
let now = std::time::Instant::now();
@@ -6124,11 +6275,12 @@ fn split_line(frames: f64) {
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' \
"preparing per frame, the submitting thread: {:.1} ms asking the write watch ({:.1} of them finding what to ask), {:.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 \
several threads, {:.1} one at a time in {:.0} before shader writes; 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[8],
ms[1],
ms[2],
ms[6],
@@ -6136,6 +6288,8 @@ fn split_line(frames: f64) {
f[1],
f[2],
f[3],
ms[9],
ms[10] * 1e6,
ms[7],
ms[3],
ms[4],
@@ -6143,12 +6297,17 @@ fn split_line(frames: f64) {
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 \
"asking about title memory per frame, all threads: {:.0} kernel askings over {:.0} MB ({:.1} ms, {:.0} pages of 4 KiB found \
written, {:.0} askings refused); {:.0} questions about holes; 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[11] / 1e6,
a[12],
a[10],
a[9],
a[2],
a[3],
a[4],
@@ -6230,8 +6389,29 @@ struct Batch {
/// for every refresh of a submission was tens of millions of
/// comparisons a frame.
index: ByCopy<usize>,
/// Staging buffers (by address) refreshed since the last title write.
/// Staging buffers (by address) refreshed since the last title write,
/// or due to be.
textures: std::collections::HashSet<usize>,
/// Those due ([`Batch::refresh_textures`]).
due: Vec<std::sync::Arc<crate::texture::Staging>>,
}
/// Whether a submission's textures are brought up to date on several
/// threads (`VICTORY_WIDE_TEXTURES=0`: one after the other, where the lists
/// name them, as before).
///
/// They are read, hashed and, if they changed, detiled into their staging
/// buffers: 5 ms a frame of the submitting thread, for some 16 MB. Each has
/// its own buffer, notes and lock, and is refreshed by the title's own
/// threads as it is. They are done before the next step that writes title
/// memory, as the copies' refreshes are, and before the lists go to the
/// card. What is new: the note of a list that its upload takes what the
/// buffer then holds (`texture.rs`, `dirty`) may come ahead of the refresh
/// an earlier list of the submission asked for, not behind it. The texture
/// is then uploaded by a later list too, once more than needed.
fn wide_textures() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_WIDE_TEXTURES").is_none_or(|v| v != "0") && wide_refresh())
}
impl Batch {
@@ -6267,7 +6447,12 @@ impl Batch {
Self::update(&shadow, from, to);
}
#[cfg(feature = "diagnostics")]
SPLIT[6].fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed);
{
let ns = started.elapsed().as_nanos() as u64;
SPLIT[6].fetch_add(ns, Ordering::Relaxed);
SPLIT[9].fetch_add(ns, Ordering::Relaxed);
SPLIT[10].fetch_add(1, Ordering::Relaxed);
}
}
/// Brings `[from, to)` of a copy up to date, timed.
@@ -6294,7 +6479,23 @@ impl Batch {
self.textures.clear();
}
/// The textures due are brought up to date.
fn refresh_textures(&mut self) {
if self.due.len() < 2 || !wide_textures() {
self.due.drain(..).for_each(|staging| staging.refresh());
return;
}
// The time counted is the time it took, not the threads' sum.
let started = std::time::Instant::now();
let before = PREPARING[2].load(Ordering::Relaxed);
let due = std::mem::take(&mut self.due);
crate::shadow::wide_each(due.len(), move |i| due[i].refresh());
let summed = PREPARING[2].load(Ordering::Relaxed).saturating_sub(before);
PREPARING[2].fetch_sub(summed.saturating_sub(started.elapsed().as_micros() as u64), Ordering::Relaxed);
}
fn flush(&mut self) {
self.refresh_textures();
self.index.clear();
if self.refreshes.is_empty() {
return;
@@ -6334,7 +6535,7 @@ impl Batch {
let ranges: Vec<(usize, usize)> = self
.refreshes
.iter()
.filter(|r| !r.0.unwatched())
.filter(|r| r.0.asked_ahead())
.map(|r| (r.1, r.2))
.collect();
crate::watch::ask_ahead(&ranges);
@@ -7661,6 +7862,50 @@ mod refresh_tests {
at
}
/// Title memory of `blocks` pages in a placeholder, one view to a page,
/// and every fourth page not there: as a title that streams has it.
fn with_holes(blocks: usize) -> usize {
use windows_sys::Win32::System::Memory::{
MEM_PRESERVE_PLACEHOLDER, MEM_RELEASE, MEM_REPLACE_PLACEHOLDER, MEM_RESERVE_PLACEHOLDER, MapViewOfFile3, PAGE_NOACCESS,
VirtualAlloc2, VirtualFree,
};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
// SAFETY: a placeholder and a section of our own.
unsafe {
let at = VirtualAlloc2(
GetCurrentProcess(),
std::ptr::null(),
blocks * PAGE,
MEM_RESERVE | MEM_RESERVE_PLACEHOLDER,
PAGE_NOACCESS,
std::ptr::null_mut(),
0,
) as usize;
assert_ne!(at, 0);
let len = blocks * PAGE;
let section =
CreateFileMappingW(-1isize as _, std::ptr::null(), PAGE_READWRITE, (len >> 32) as u32, len as u32, std::ptr::null());
for block in (0..blocks).filter(|b| b % 4 != 3) {
let to = at + block * PAGE;
VirtualFree(to as *mut _, PAGE, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER);
let view = MapViewOfFile3(
section,
GetCurrentProcess(),
to as *const _,
(block * PAGE) as u64,
PAGE,
MEM_REPLACE_PLACEHOLDER,
PAGE_READWRITE,
std::ptr::null_mut(),
0,
);
assert_eq!(view.Value as usize, to);
crate::title_memory_remapped(to, PAGE);
}
at
}
}
fn memory(len: usize) -> *mut u8 {
// SAFETY: a plain allocation.
unsafe { VirtualAlloc(std::ptr::null(), len, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE).cast() }
@@ -7821,7 +8066,7 @@ mod refresh_tests {
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 rounds: usize = std::env::var("VICTORY_TEST_ROUNDS").ok().and_then(|v| v.parse().ok()).unwrap_or(120);
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)];
@@ -7987,6 +8232,37 @@ mod refresh_tests {
});
}
/// The title's bytes put in under shader writes, for a copy of 174 MB
/// whose shader writes were declared as 40 ranges that overlap, with 12
/// pages written since. Run with `--ignored --nocapture`, and with
/// `VICTORY_LIGHT_REFRESH=0` for the way it was.
#[test]
#[ignore]
fn bytes_under_overlapping_ranges() {
const BIG: usize = 174 << 20;
let big = title(BIG, true);
// SAFETY (both): the test's own memory.
unsafe { std::ptr::write_bytes(big as *mut u8, 1, BIG) };
let copy = Shadow::unbacked(big as u64, memory(BIG), BIG as u64, true);
copy.update(big, BIG);
copy.take_dirty();
for i in 0..40 {
copy.writing(big + i * 5920, BIG - i * 5920);
}
let mut best = f64::MAX;
for round in 0..30 {
for i in 0..12 {
unsafe { ((big + (i * 211 + round) % 2700 * PAGE + 64) as *mut u8).write_volatile(round as u8) };
}
let started = std::time::Instant::now();
let mut runs = usize::MAX;
assert!(!copy.merge_under_writes(big, BIG, &mut runs));
best = best.min(started.elapsed().as_secs_f64() * 1e3);
copy.take_dirty();
}
println!("VICTORY_LIGHT_REFRESH {}: best {best:.3} ms", crate::watch::light());
}
/// 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]
@@ -8078,12 +8354,17 @@ mod refresh_tests {
const EACH: usize = 224 << 10;
const BIG: usize = 174 << 20;
let watched = title(3000 * EACH, false);
let views = title(1400 * EACH, true);
// `VICTORY_BENCH_HOLES=0`: views without holes.
let holes = std::env::var_os("VICTORY_BENCH_HOLES").is_none_or(|v| v != "0");
let views = if holes { with_holes(1400 * EACH / PAGE) } else { title(1400 * EACH, true) };
let there = |at: usize| !holes || (at - views) / PAGE % 4 != 3;
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);
for at in (views..views + 1400 * EACH).step_by(PAGE).filter(|&at| there(at)) {
std::ptr::write_bytes(at as *mut u8, 1, PAGE);
}
std::ptr::write_bytes(big as *mut u8, 1, BIG);
}
let mut small = Vec::new();
@@ -8106,8 +8387,10 @@ mod refresh_tests {
// 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) };
if there(at) {
// 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;