Merge agent/submit: submitting thread's work (time split in release, host lists reused, a submission's batch kept, spread work wakes only the threads it needs; on) and heap-record switches VICTORY_HEAP_SYNC_FIRST, VICTORY_SYNC_IN_PLACE, VICTORY_SPANS_QUICK, VICTORY_SYNC_FINE, VICTORY_HEAP_SYNC_AT_SUBMIT (off; the last three ported from Belobug/VIctory-Belobug-AMD 15ab94e, a284ca7, 673be0e+a609a2f)

The helper was stopped before its final report (user asked for all work in the main branch); its branch was
clean and on a906dcc. Built and tested at this merge; not yet measured in the game.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
readmet3xt-labandClaude Opus 5.5 committed 2026-11-27 01:28:17 +05:30
commit f1edd66379
9 files changed
+1161 -162

No files matched your search

+204 -26
View File
@@ -323,6 +323,79 @@ pub(crate) fn begin_sync_heaps() -> SyncedHeaps {
SyncedHeaps(Some(mine))
}
/// [`begin_sync_heaps`] for a submission that needs round `mark` (one that
/// began after what it reads was written, `crate::heap_marks`): asked for if
/// it has not been. 0: as [`begin_sync_heaps`]. Ported from
/// Belobug/VIctory-Belobug-AMD 673be0e and a609a2f.
#[cfg(windows)]
pub(crate) fn begin_sync_heaps_after(mark: u64) -> SyncedHeaps {
if mark == 0 || !sync_aside() {
return begin_sync_heaps();
}
// (The thread is started by the first `begin_sync_heaps`.)
let mut rounds = SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
if rounds.1 == 0 && rounds.0 == 0 {
drop(rounds);
return begin_sync_heaps();
}
let asked = rounds.0 < mark;
rounds.0 = rounds.0.max(mark);
drop(rounds);
use crate::heap_marks::{EARLY_ROUNDS, LATE_ROUNDS, STATS};
if asked {
SYNC_ROUNDS.1.notify_all();
STATS[LATE_ROUNDS].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
} else {
STATS[EARLY_ROUNDS].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
SyncedHeaps(Some(mark))
}
/// The number of a round of the heaps' records that begins after this call,
/// asked for if none is waiting to begin (at the title's submit: the round
/// runs while the submission waits in the line). 0 if the records are not
/// synced by a thread of their own (or it has not started yet).
#[cfg(windows)]
pub(crate) fn round_after_now() -> u64 {
if !sync_aside() {
return 0;
}
let mut rounds = SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
if rounds.1 == 0 && rounds.0 == 0 {
return 0;
}
let (round, ask) = crate::heap_marks::round_after(rounds.0, rounds.1);
if ask {
rounds.0 = round;
drop(rounds);
SYNC_ROUNDS.1.notify_all();
crate::heap_marks::STATS[crate::heap_marks::ROUNDS_ASKED].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
round
}
/// The number of the first round that begins after this call, not asked for
/// ([`begin_sync_heaps_after`] asks for it if nobody did).
#[cfg(windows)]
pub(crate) fn round_mark() -> u64 {
if !sync_aside() {
return 0;
}
let rounds = SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
if rounds.1 == 0 && rounds.0 == 0 {
return 0;
}
rounds.0 + 1
}
/// Whether `[at, at + len)` touches the title memory of a placed
/// shader-visible heap of descriptors.
#[cfg(windows)]
pub(crate) fn in_shader_heap(at: usize, len: usize) -> bool {
let (from, to) = (at as u64, at as u64 + len as u64);
SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner()).iter().any(|&(start, end, _)| from < end && start < to)
}
#[cfg(windows)]
impl Drop for SyncedHeaps {
fn drop(&mut self) {
@@ -360,8 +433,10 @@ pub(crate) fn sync_heaps() {
let mut spent = [0u64; 3];
let since = |at: std::time::Instant| at.elapsed().as_nanos() as u64;
let mut kinds = [0u64; 5];
let mut short = 0u64;
let mut wait: Vec<u32> = Vec::new();
let frame = crate::frame::current();
let (pages_a_job, records_a_job) = sync_jobs();
let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
for &(start, end, at) in heaps.iter() {
// SAFETY: registered heaps are live (removed when dropped).
@@ -379,8 +454,8 @@ pub(crate) fn sync_heaps() {
// since: a shader may pick them by index.
let before = host.copied_before(frame);
let of_tables = std::sync::atomic::AtomicU64::new(0);
crate::land::each_view(true, before.len().div_ceil(SYNC_AT_A_TIME), &|n| {
for &index in before.chunks(SYNC_AT_A_TIME).nth(n).unwrap_or_default() {
crate::land::each_view(true, before.len().div_ceil(records_a_job), &|n| {
for &index in before.chunks(records_a_job).nth(n).unwrap_or_default() {
if host.copied_tabled(index, frame) {
of_tables.fetch_add(1, Relaxed);
} else {
@@ -411,37 +486,62 @@ pub(crate) fn sync_heaps() {
let began = std::time::Instant::now();
let copied = host.copied_begin(step as usize);
let samplers = heap.samplers();
let found: Vec<std::sync::Mutex<SyncPart>> = parts.chunks(SYNC_PAGES).map(|_| Default::default()).collect();
let in_place = sync_in_place() && step == 32 && crate::safecopy::on();
let found: Vec<std::sync::Mutex<SyncPart>> = parts.chunks(pages_a_job).map(|_| Default::default()).collect();
crate::land::each_view(true, found.len(), &|n| {
let mut out = SyncPart::default();
let mut page_bytes: Vec<u8> = Vec::new();
let mut changed: Vec<u32> = Vec::new();
for &(from, to) in parts.chunks(SYNC_PAGES).nth(n).unwrap_or_default() {
// The page's records are read through the kernel, into a
// copy: a page the title unmaps meanwhile fails the read
// where 2,048 reads in place would fault.
page_bytes.resize(to - from, 0);
if !crate::host::read_into(from as *const u8, &mut page_bytes) {
continue;
}
out.pages += 1;
out.views += u64::from(crate::watch::guarded(from, to));
out.bytes += (to - from) as u64;
let mut bits: Vec<u64> = Vec::new();
for &(from, to) in parts.chunks(pages_a_job).nth(n).unwrap_or_default() {
let slots = changed_slots(from, to, start as usize, step, mirrored_count);
// A record that straddles the range's ends is left
// to the range that holds its first byte.
let first = ((from - start as usize) as u64).div_ceil(step).max(slots.start);
let Some(records) = page_bytes.get((start + first * step) as usize - from..) else {
continue;
let lead = (start + first * step) as usize;
let records: &[u8] = if in_place {
// Read where they are, each record once, and compared
// as it is read (`host::Copied::changes_in_place`).
let count = (to.saturating_sub(lead) as u64 / step).min(slots.end.saturating_sub(first));
changed.clear();
// SAFETY: as below; title memory is read by a copy that
// fails where a page is not there.
let whole =
unsafe { copied.changes_in_place(first as u32, lead as *const u8, count as usize, &mut bits, &mut changed) };
out.pages += 1;
out.views += u64::from(crate::watch::guarded(from, to));
out.bytes += (to - from) as u64;
out.looked += count;
out.short += u64::from(!whole);
// What was read of the changed records is in the records
// last seen now.
// SAFETY: this part's records alone, which nothing else
// writes meanwhile.
unsafe { copied.seen(first as u32, count as usize) }
} else {
// The page's records are read through the kernel, into a
// copy: a page the title unmaps meanwhile fails the read
// where 2,048 reads in place would fault.
page_bytes.resize(to - from, 0);
if !crate::host::read_into(from as *const u8, &mut page_bytes) {
continue;
}
out.pages += 1;
out.views += u64::from(crate::watch::guarded(from, to));
out.bytes += (to - from) as u64;
let Some(records) = page_bytes.get(lead.wrapping_sub(from)..) else {
continue;
};
let count = (records.len() as u64 / step).min(slots.end.saturating_sub(first));
out.looked += count;
// Only the records that are not the ones last seen
// there (`host::Copied::changes`).
changed.clear();
// SAFETY: the parts are ranges of the heap apart from one
// another, and only records that lie whole in one are its.
unsafe { copied.changes(first as u32, &records[..(count * step) as usize], &mut changed) };
records
};
let count = (records.len() as u64 / step).min(slots.end.saturating_sub(first));
out.looked += count;
// Only the records that are not the ones last seen
// there (`host::Copied::changes`).
changed.clear();
// SAFETY: the parts are ranges of the heap apart from one
// another, and only records that lie whole in one are its.
unsafe { copied.changes(first as u32, &records[..(count * step) as usize], &mut changed) };
for &index in &changed {
let at = (u64::from(index) - first) as usize * step as usize;
let word = |n: usize| {
@@ -494,6 +594,7 @@ pub(crate) fn sync_heaps() {
views += part.views;
bytes += part.bytes;
looked += part.looked;
short += part.short;
tabled += part.tabled;
kinds[0] += part.nothing;
later += part.wait.len() as u64;
@@ -506,8 +607,8 @@ pub(crate) fn sync_heaps() {
}
spent[1] += since(began);
let began = std::time::Instant::now();
crate::land::each_view(true, make.len().div_ceil(SYNC_AT_A_TIME), &|n| {
for &index in make.chunks(SYNC_AT_A_TIME).nth(n).unwrap_or_default() {
crate::land::each_view(true, make.len().div_ceil(records_a_job), &|n| {
for &index in make.chunks(records_a_job).nth(n).unwrap_or_default() {
host.mirror(index, start + u64::from(index) * step, false);
}
});
@@ -528,6 +629,34 @@ pub(crate) fn sync_heaps() {
for (count, n) in SYNCED_KINDS.iter().zip(kinds) {
count.fetch_add(n, Relaxed);
}
SYNC_SHORT.fetch_add(short, Relaxed);
}
/// Pages [`sync_heaps`] read in place of which a part could not be read
/// ([`sync_in_place`]), since the last frame line.
#[cfg(windows)]
static SYNC_SHORT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Whether [`sync_heaps`] reads a changed page's records where they are, each
/// once, and compares each with the one last seen as it reads it, 32 bytes at
/// a time (`VICTORY_SYNC_IN_PLACE=1`; off by default: into a copy of the page
/// first, through the kernel, then compared, as before).
///
/// Ported from Belobug/VIctory-Belobug-AMD 15ab94e (measured there: the copy
/// and the compare were each a pass over all of the page, 7.4 and 5.3 ms a
/// frame of the six threads for 35 MB, run heapsync-ab1; on by default there
/// since 8822ed4). Here the round's "reading and comparing" was 2.6-3.1 ms a
/// frame (release run 233354), and the submitting thread waits for the
/// round. A page the title unmaps meanwhile fails where the copy failed; the
/// records before the place are taken as they were read.
#[cfg(windows)]
fn sync_in_place() -> bool {
#[cfg(feature = "diagnostics")]
if let Some(on) = crate::ab::pick("VICTORY_SYNC_IN_PLACE") {
return on;
}
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_SYNC_IN_PLACE").is_some_and(|v| v != "0"))
}
/// Of those mirrored, since the last frame line: no view, a view of an object,
@@ -548,6 +677,8 @@ struct SyncPart {
make: Vec<u32>,
wait: Vec<u32>,
kinds: [u64; 5],
/// Pages read in place not all there ([`SYNC_SHORT`]).
short: u64,
}
/// Pages [`sync_heaps`] gives a thread at a time.
@@ -558,6 +689,35 @@ const SYNC_PAGES: usize = 8;
#[cfg(windows)]
const SYNC_AT_A_TIME: usize = 256;
/// With `VICTORY_SYNC_FINE=1`: pages and records [`sync_heaps`] gives a thread
/// at a time.
#[cfg(windows)]
const SYNC_PAGES_FINE: usize = 2;
#[cfg(windows)]
const SYNC_AT_A_TIME_FINE: usize = 64;
/// Pages and records [`sync_heaps`] gives a thread at a time
/// (`VICTORY_SYNC_FINE=1`: [`SYNC_PAGES_FINE`] and [`SYNC_AT_A_TIME_FINE`];
/// off by default: [`SYNC_PAGES`] and [`SYNC_AT_A_TIME`], as before).
///
/// Ported from Belobug/VIctory-Belobug-AMD a284ca7: a round for a submission
/// finds a dozen pages or so; by eight a job that was two jobs, on two of the
/// six threads, while the submission waited for the round (2 to 3.4 ms at a
/// time, their run city4).
#[cfg(windows)]
fn sync_jobs() -> (usize, usize) {
#[cfg(feature = "diagnostics")]
if let Some(fine) = crate::ab::pick("VICTORY_SYNC_FINE") {
return if fine { (SYNC_PAGES_FINE, SYNC_AT_A_TIME_FINE) } else { (SYNC_PAGES, SYNC_AT_A_TIME) };
}
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if *ON.get_or_init(|| std::env::var_os("VICTORY_SYNC_FINE").is_some_and(|v| v != "0")) {
(SYNC_PAGES_FINE, SYNC_AT_A_TIME_FINE)
} else {
(SYNC_PAGES, SYNC_AT_A_TIME)
}
}
/// Since the last frame line, of [`sync_heaps`]: changed pages of placed
/// shader-visible heaps looked at, slots on them, slots whose record was
/// not the one last seen and was mirrored, the microseconds it took; then
@@ -589,9 +749,27 @@ pub(crate) fn sync_line(frames: f64) {
let [pages, looked, mirrored, micros, tabled, later, waited] = [0, 1, 2, 3, 4, 5, 6].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed));
let split = [0, 1, 2, 3, 4].map(|i| SYNC_SPLIT[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64);
let kinds = [0, 1, 2, 3, 4].map(|i| SYNCED_KINDS[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames);
let short = SYNC_SHORT.swap(0, std::sync::atomic::Ordering::Relaxed);
let quick = [0, 1].map(|i| host::SPANS_QUICK[i].swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames);
if quick[0] > 0.0 || sync_jobs().0 == SYNC_PAGES_FINE {
tracing::info!(
"writes of view slots per frame (VICTORY_SPANS_QUICK=1): {:.0} leaving the placed views' spans alone, {:.0} under \
their lock; the heaps' records in jobs of {} pages (VICTORY_SYNC_FINE)",
quick[0],
quick[1],
sync_jobs().0
);
}
if pages == 0 {
return;
}
if sync_in_place() {
tracing::info!(
"the heaps' records read where they are (VICTORY_SYNC_IN_PLACE=1): {:.1} pages per frame, {short} of them not all \
there since the last line",
pages as f64 / frames
);
}
tracing::info!(
"descriptor records the title copied into its heap as bytes (VICTORY_HEAP_SYNC=0: not looked for): per frame \
{:.1} changed heap pages, {:.0} slots looked at, {:.1} whose record was new and was mirrored, {:.1} new ones of \