From d2e24b3bc3d3904a7ecf7708045c8514eefedd89 Mon Sep 17 00:00:00 2001 From: readmet3xt-lab <238408149+readmet3xt-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:25:55 +0530 Subject: [PATCH] VICTORY_HEAP_SYNC_AT_SUBMIT=1: the heaps' records asked for at the title's submit Ported from Belobug/VIctory-Belobug-AMD 673be0e and a609a2f (renderer port 7, its heaps' records part; the journal in 64 parts, which shares the switch VICTORY_PORT_JOURNAL there, is not taken). There the lane waited 4.5-5.2 ms a frame for the heaps' records and port 7 took 2.4 ms off it. Here the submitting thread waited 2.7-5.8 ms a frame for them (release run 233354). A round of the heap-sync thread is asked for when the title submits (Lane::push, round_after), so it runs while the submission waits in the line (12-15 are in it when one is added), and the submitting thread waits for that round (begin_sync_heaps_after), or for a later one if a wait of its queue that was met in memory went since (the queue's floor in the line), or a step of ours wrote, or may have written, a heap's title memory while preparing an earlier submission (heap_marks::prep_wrote: buffer copies into a heap, copies at their place, our own steps). Added here: a submission that waited for flights to land begins a round of its own after the wait, as without the switch. With the switch off nothing is marked, asked or counted. VICTORY_HEAP_SYNC_FIRST applies only to a submission without a mark. Not behaviour-neutral in timing: a record the title writes for a submission after submitting it (or after a wait behind which the submission stands was met) is left to a later round, as the console's card could miss it too. Off by default. The frame line "the heaps' records asked for at the title's submit" gives the wait per frame, the rounds asked at a submit, the preparings that found theirs asked earlier or had to ask, and those that wrote a heap's memory. Tests: the round given begins after the call (heap_marks), and the line's marks with waits and parts taken along (queue::behind). Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/descriptor.rs | 73 ++++++++++ crates/victory-gpu/src/heap_marks.rs | 190 +++++++++++++++++++++++++ crates/victory-gpu/src/host.rs | 34 ++++- crates/victory-gpu/src/lib.rs | 2 + crates/victory-gpu/src/queue/behind.rs | 172 ++++++++++++++++++++-- 5 files changed, 458 insertions(+), 13 deletions(-) create mode 100644 crates/victory-gpu/src/heap_marks.rs diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index f1f265e..087f2b3 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -290,6 +290,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) { diff --git a/crates/victory-gpu/src/heap_marks.rs b/crates/victory-gpu/src/heap_marks.rs new file mode 100644 index 0000000..0bd6e62 --- /dev/null +++ b/crates/victory-gpu/src/heap_marks.rs @@ -0,0 +1,190 @@ +//! The heaps' records asked for at the title's submit +//! (`VICTORY_HEAP_SYNC_AT_SUBMIT=1`, off by default). +//! +//! Ported from Belobug/VIctory-Belobug-AMD 673be0e and a609a2f (renderer +//! port 7, its heaps' records part; there under `VICTORY_PORT_JOURNAL` with +//! the journal in parts, which is not taken here). There the submitting lane +//! waited 4.5-5.2 ms a frame for the heaps' records, and port 7 took 2.4 ms +//! off the lane. Here it waited 2.7-5.8 ms a frame (release run 233354). +//! +//! A round of the heap-sync thread (`crate::descriptor::sync_heaps`) is asked +//! for when the title submits ([`round_after`]), so it runs while the +//! submission waits in the line (12-15 submissions a frame are in it when one +//! is added), and the submitting thread waits for that round, or for a later +//! one if (a) a wait of its queue that was met in memory went since (the +//! queue's floor, `queue/behind.rs` `State::floors`) or (b) a step of ours +//! wrote, or may have written, a heap's title memory while preparing an +//! earlier submission ([`prep_wrote`]). A submission that waited for flights +//! to land begins a round of its own after the wait (`crate::host`). +//! +//! Why a submission's records are what they were: a round reads the title's +//! records as they are when it runs. The records a submission's lists use are +//! written by the title before its submit call, or, for a part behind a wait +//! for a memory value or a fence, before that wait is met. A round asked for +//! at the submit call (or, behind a wait, the first round to begin after the +//! line saw the wait met) begins after those writes: it sees all of them, as +//! the round begun at the preparing did. What changes: a record the title +//! writes for a submission after submitting it is left to a later round (the +//! console's card could miss it as well). + +use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; + +/// Whether the switch is on. +pub(crate) fn on() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| crate::switch::on("VICTORY_HEAP_SYNC_AT_SUBMIT")) +} + +/// With rounds `asked` asked for and `done` done (one runs at a time, in +/// order; round `k` begins only once `k - 1` is done and `k` has been asked +/// for): the number of a round that begins after now, and whether it has to +/// be asked for (it is `asked + 1` then). A round asked for and not begun +/// yet (`asked >= done + 2`: the one before it is not done) begins after +/// now; with `asked == done + 1` round `asked` may be running already. +pub(crate) fn round_after(asked: u64, done: u64) -> (u64, bool) { + if asked >= done + 2 { (asked, false) } else { (asked + 1, true) } +} + +thread_local! { + /// On the submitting thread: the round of the heaps' records the + /// submission being prepared needs (0: none known, a round is asked for + /// at the preparing, as without the switch). + static MARK: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// The line is about to do a submission that needs round `mark`. +pub(crate) fn set_mark(mark: u64) { + MARK.set(mark); +} + +/// The round every submission prepared from now on needs at least: one that +/// began after a step of a submission being prepared wrote, or may have +/// written, title memory of a heap of descriptors ([`prep_wrote`]). +static PREP_FLOOR: AtomicU64 = AtomicU64::new(0); + +/// A step of the submission being prepared wrote, or may have written, title +/// memory of a heap of descriptors: every submission prepared after it waits +/// for a round begun after now. +pub(crate) fn prep_wrote() { + PREP_FLOOR.fetch_max(crate::descriptor::round_mark(), Relaxed); + STATS[PREP_WRITES].fetch_add(1, Relaxed); +} + +/// The round the submission about to be prepared needs (its mark, at least +/// [`PREP_FLOOR`]); 0 if none is known (or the switch is off). +pub(crate) fn needed() -> u64 { + if !on() { + return 0; + } + match MARK.get() { + 0 => { + STATS[NO_MARK].fetch_add(1, Relaxed); + 0 + } + m => m.max(PREP_FLOOR.load(Relaxed)), + } +} + +/// Since the last frame line: rounds asked for at a submit; submissions +/// that found their round asked earlier; preparings that wrote a heap's +/// memory (or may have); nanoseconds the submitting thread waited for the +/// heaps' records, and how many times; preparings with no mark; those whose +/// round had to be asked for at the preparing. +pub(crate) static STATS: [AtomicU64; 7] = [const { AtomicU64::new(0) }; 7]; +pub(crate) const ROUNDS_ASKED: usize = 0; +pub(crate) const EARLY_ROUNDS: usize = 1; +const PREP_WRITES: usize = 2; +const HEAPS_NS: usize = 3; +const HEAPS: usize = 4; +const NO_MARK: usize = 5; +pub(crate) const LATE_ROUNDS: usize = 6; + +/// The submitting thread waited `ns` for the heaps' records. +pub(crate) fn heaps_waited(ns: u64) { + STATS[HEAPS_NS].fetch_add(ns, Relaxed); + STATS[HEAPS].fetch_add(1, Relaxed); +} + +/// The frame line's, per frame. +pub(crate) fn line(frames: f64) { + let s: [f64; 7] = std::array::from_fn(|i| STATS[i].swap(0, Relaxed) as f64); + if !on() { + return; + } + let f = frames.max(1.0); + tracing::info!( + "the heaps' records asked for at the title's submit, per frame (VICTORY_HEAP_SYNC_AT_SUBMIT=1): the submitting thread \ + waited {:.2} ms in {:.1} preparings; {:.1} rounds asked for at a submit, {:.1} preparings found theirs asked earlier, \ + {:.1} asked it at the preparing, {:.1} had no mark; {:.1} preparings wrote a heap's memory", + s[HEAPS_NS] / 1e6 / f, + s[HEAPS] / f, + s[ROUNDS_ASKED] / f, + s[EARLY_ROUNDS] / f, + s[LATE_ROUNDS] / f, + s[NO_MARK] / f, + s[PREP_WRITES] / f, + ); +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A round asked for and not begun is one that begins after now; one + /// that may be running is not. + #[test] + fn a_round_is_one_that_begins_after_now() { + // Nothing asked: ask for round 1. + assert_eq!(round_after(0, 0), (1, true)); + // Round 3 asked for, 2 done: 3 may be running, ask for 4. + assert_eq!(round_after(3, 2), (4, true)); + // Rounds up to 5 asked for, 2 done: 3 runs or waits, 5 cannot have + // begun. + assert_eq!(round_after(5, 2), (5, false)); + assert_eq!(round_after(4, 2), (4, false)); + // Everything done: ask anew. + assert_eq!(round_after(7, 7), (8, true)); + } + + /// A simulation of the rounds' thread: whatever the times of asking, + /// the round a caller is given begins after its call. + #[test] + fn the_round_given_begins_after_the_call() { + let (mut asked, mut done) = (0u64, 0u64); + // When each round began (in calls), and each caller's (call, round). + let mut began: std::collections::HashMap = Default::default(); + let mut given: Vec<(u64, u64)> = Vec::new(); + let mut running: Option = None; + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + for call in 1..5000u64 { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + match seed % 3 { + 0 => { + let (round, ask) = round_after(asked, done); + if ask { + asked += 1; + } + given.push((call, round)); + } + 1 => { + if let Some(r) = running.take() { + done = r; + } + } + _ => { + if running.is_none() && asked > done { + running = Some(done + 1); + began.insert(done + 1, call); + } + } + } + } + for (call, round) in given { + if let Some(&at) = began.get(&round) { + assert!(at > call, "round {round} began at {at}, before the call at {call}"); + } + } + } +} diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 3571ca6..824c04a 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -5646,9 +5646,12 @@ impl Host { } let was_working = working(true); submitting(1, runs.len()); - // With [`heap_sync_first`]: the heaps' records begun now, beside the - // planning of steps done on the card and the look at flights. - let first_heaps = (late && heap_sync_first()).then(crate::descriptor::begin_sync_heaps); + // The round of the heaps' records asked for when the title submitted + // these lists, if that is known (`crate::heap_marks`, 0: not). + let heaps_mark = if late { crate::heap_marks::needed() } else { 0 }; + // Else with [`heap_sync_first`]: the heaps' records begun now, beside + // the planning of steps done on the card and the look at flights. + let first_heaps = (late && heaps_mark == 0 && heap_sync_first()).then(crate::descriptor::begin_sync_heaps); let ahead = Split::begin(); SPLIT[11].fetch_add(called.elapsed().as_nanos() as u64, Ordering::Relaxed); stage.next("submit: steps given to the card"); @@ -5681,6 +5684,8 @@ impl Host { // command lists that index those slots are submitted. (Begun first: // again after a wait for flights, whose landings wrote title memory, // as the round began here saw them; the first is not waited for.) + // With a mark (`crate::heap_marks`): that round, unless the + // submission waited for flights, whose landings came after it. let heaps_synced = match first_heaps { Some(first) if !waited_for_flights => { HEAP_SYNC_FIRST[0].fetch_add(1, Ordering::Relaxed); @@ -5691,7 +5696,11 @@ impl Host { HEAP_SYNC_FIRST[1].fetch_add(1, Ordering::Relaxed); std::mem::forget(first); } - crate::descriptor::begin_sync_heaps() + if heaps_mark != 0 && !waited_for_flights { + crate::descriptor::begin_sync_heaps_after(heaps_mark) + } else { + crate::descriptor::begin_sync_heaps() + } } }; // That list runs first, and is one of the submission's from here on. @@ -5850,7 +5859,11 @@ impl Host { } split.part(4); stage.next("submit: waiting for the heaps' records"); + let heaps_began = std::time::Instant::now(); drop(heaps_synced); + if crate::heap_marks::on() { + crate::heap_marks::heaps_waited(heaps_began.elapsed().as_nanos() as u64); + } // The slots that waited for a texture's host copy, before the // lists that may read them run. crate::descriptor::make_waiting(); @@ -7614,6 +7627,11 @@ impl HostList { } batch.fill(); batch.title_written(); + // Into a heap's records: the rounds asked for earlier + // did not see it (`crate::heap_marks`). + if crate::heap_marks::on() && crate::descriptor::in_shader_heap(dst, len) { + crate::heap_marks::prep_wrote(); + } 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); @@ -7640,6 +7658,9 @@ impl HostList { } Step::Placed(p) => { let started = std::time::Instant::now(); + if crate::heap_marks::on() && crate::descriptor::in_shader_heap(p.dst, p.len) { + crate::heap_marks::prep_wrote(); + } let free = !touches(air, p.src, p.src + p.len) && !touches(air, p.dst, p.dst + p.len); batch.fill(); if free { @@ -7665,6 +7686,10 @@ impl HostList { let started = std::time::Instant::now(); if matches!(step, Step::Run(_)) { batch.title_written(); + // (It may write anywhere, a heap's records too.) + if crate::heap_marks::on() { + crate::heap_marks::prep_wrote(); + } } let _step = crate::timeline::span("a step of ours"); work(); @@ -9197,6 +9222,7 @@ impl Meter { crate::shadow::back_line(frames); submit_split_line(frames); fresh_open_line(frames); + crate::heap_marks::line(frames); #[cfg(feature = "diagnostics")] split_line(frames); { diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index 9708b93..d0ec362 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -40,6 +40,8 @@ pub mod frame; #[cfg(all(windows, feature = "diagnostics"))] mod book; #[cfg(windows)] +mod heap_marks; +#[cfg(windows)] mod host; #[cfg(windows)] mod indirect; diff --git a/crates/victory-gpu/src/queue/behind.rs b/crates/victory-gpu/src/queue/behind.rs index 700d739..0d43ef1 100644 --- a/crates/victory-gpu/src/queue/behind.rs +++ b/crates/victory-gpu/src/queue/behind.rs @@ -84,12 +84,36 @@ pub(super) trait Work: Send { } /// Does it. `forced`: its wait was not met. fn go(self, forced: bool); + /// A mark taken when an entry that runs is put in the line (by the thread + /// that puts it, before the line's lock): the round of the heaps' records + /// begun after that (`crate::heap_marks`). 0: none. + fn mark_now() -> u64 { + 0 + } + /// A mark taken when a wait goes or is taken along (on our thread): a + /// round that begins after. + fn mark_later() -> u64 { + 0 + } + /// Whether what its queue has behind this wait may have been written + /// only just before the wait was met (by the CPU, which writes, then + /// meets the wait): it was met in memory. A wait met by a bottom-of-pipe + /// write of a flight or of the submission it is taken along with is the + /// card's order, not the CPU's. + fn raises(&self) -> bool { + true + } + /// Told just before [`Work::go`] of a submission: the highest mark of + /// what it is made of and of the waits its queue went past. + fn marked(&mut self, _mark: u64) {} } struct Entry { number: u64, queue: usize, what: W, + /// [`Work::mark_now`] when it was put in the line. + mark: u64, } struct State { @@ -107,6 +131,23 @@ struct State { /// Presents and submissions in the line or being done. presents: usize, runs: usize, + /// Per queue, the highest [`Work::mark_later`] of the waits it went past: + /// what is behind a wait may have been written only before the wait was + /// met, not before it was put in the line. + floors: Vec<(usize, u64)>, +} + +impl State { + fn floor(&self, queue: usize) -> u64 { + self.floors.iter().find(|f| f.0 == queue).map_or(0, |f| f.1) + } + + fn raise(&mut self, queue: usize, mark: u64) { + match self.floors.iter_mut().find(|f| f.0 == queue) { + Some(f) => f.1 = f.1.max(mark), + None => self.floors.push((queue, mark)), + } + } } /// See the module's text. @@ -129,6 +170,7 @@ impl Lane { served: 0, presents: 0, runs: 0, + floors: Vec::new(), }), work: Condvar::new(), moved: Condvar::new(), @@ -139,19 +181,21 @@ impl Lane { self.state.lock().unwrap_or_else(|e| e.into_inner()) } - fn put(state: &mut State, queue: usize, what: W) { + fn put(state: &mut State, queue: usize, what: W, mark: u64) { state.presents += usize::from(what.shows()); state.runs += usize::from(what.runs()); let number = state.next; state.next += 1; - state.items.push_back(Entry { number, queue, what }); + state.items.push_back(Entry { number, queue, what, mark }); } /// Adds to the end of the line, and has it looked at. pub(super) fn push(&self, queue: usize, what: impl IntoIterator) { + let what: Vec = what.into_iter().collect(); + let mark = if what.iter().any(W::runs) { W::mark_now() } else { 0 }; let mut state = self.lock(); for what in what { - Self::put(&mut state, queue, what); + Self::put(&mut state, queue, what, mark); } state.asked += 1; drop(state); @@ -164,7 +208,7 @@ impl Lane { pub(super) fn push_behind(&self, queue: usize, always: bool, what: W, now: impl FnOnce()) { let mut state = self.lock(); if always || state.running == Some(queue) || state.items.iter().any(|e| e.queue == queue) { - Self::put(&mut state, queue, what); + Self::put(&mut state, queue, what, 0); } else { now(); } @@ -256,13 +300,13 @@ impl Lane { /// The parts of `entry`, which stands for them, in place `at`. fn unfold(state: &mut State, at: usize, entry: Entry) { let began = Instant::now(); - let (number, queue, runs) = (entry.number, entry.queue, usize::from(entry.what.runs())); + let (number, queue, runs, mark) = (entry.number, entry.queue, usize::from(entry.what.runs()), entry.mark); let parts = entry.what.unfold(); STATS[UNFOLDING].fetch_add(began.elapsed().as_micros() as u64, Relaxed); state.runs -= runs; state.runs += parts.iter().filter(|p| p.runs()).count(); for (i, what) in parts.into_iter().enumerate() { - state.items.insert(at + i, Entry { number, queue, what }); + state.items.insert(at + i, Entry { number, queue, what, mark }); } } @@ -319,6 +363,14 @@ impl Lane { entry }; let (shows, mut runs) = (entry.what.shows(), usize::from(entry.what.runs())); + // The highest mark of what it is made of (`crate::heap_marks`). + let mut mark = entry.mark; + if entry.what.ask().is_some() && !entry.what.folded() && entry.what.raises() { + // A wait that goes: what its queue has behind it was written + // before it was met, which is now at the latest. + let later = W::mark_later(); + self.lock().raise(entry.queue, later); + } if entry.what.folded() { // Its parts take its place: behind everything before it, before // everything after it (they share its number; the first of them @@ -353,15 +405,27 @@ impl Lane { state.items.insert(at, next); break; } - let Entry { number, queue, what } = next; + let Entry { number, queue, what, mark: theirs } = next; + let waits = what.ask().is_some() && what.raises(); match entry.what.take_along(what) { - Ok(run) => runs += usize::from(run), + Ok(run) => { + runs += usize::from(run); + mark = mark.max(theirs); + if waits { + let later = W::mark_later(); + self.lock().raise(queue, later); + } + } Err(what) => { - self.lock().items.insert(at, Entry { number, queue, what }); + self.lock().items.insert(at, Entry { number, queue, what, mark: theirs }); break; } } } + if runs > 0 { + let floor = self.lock().floor(entry.queue); + entry.what.marked(mark.max(floor)); + } entry.what.go(forced); { let mut state = self.lock(); @@ -449,6 +513,31 @@ impl Work for Job { } } + fn mark_now() -> u64 { + if crate::heap_marks::on() { crate::descriptor::round_after_now() } else { 0 } + } + + fn mark_later() -> u64 { + // (Asked for now: the round runs while the line does what is before + // the part behind the wait.) + if crate::heap_marks::on() { crate::descriptor::round_after_now() } else { 0 } + } + + fn raises(&self) -> bool { + // (Without the switch no mark is taken: the memory is not read.) + if !crate::heap_marks::on() { + return true; + } + match self { + Job::Value(_, wait) => wait.met(), + _ => true, + } + } + + fn marked(&mut self, mark: u64) { + crate::heap_marks::set_mark(mark); + } + fn met(ask: &Ask) -> bool { match ask { &Ask::Wait(fence, value) => crate::fence::completed(fence as This) >= value, @@ -599,6 +688,8 @@ impl Work for Job { QUEUED.fetch_sub(1, Relaxed); } } + // (The mark was this submission's alone.) + crate::heap_marks::set_mark(0); STATS[BUSY].fetch_add(began.elapsed().as_micros() as u64, Relaxed); } } @@ -956,10 +1047,73 @@ mod tests { } } } + fn mark_now() -> u64 { + NOW.get() + } + fn mark_later() -> u64 { + LATER.get() + } + fn marked(&mut self, mark: u64) { + let name = match self { + T::Run(name, _) | T::Slow(name, ..) => name.to_string(), + T::Group(names, ..) => names.join("+"), + _ => "?".into(), + }; + MARKS.with(|m| m.borrow_mut().push((name, mark))); + } } thread_local! { static FORCED_HERE: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// What [`Work::mark_now`] and [`Work::mark_later`] give in a test. + static NOW: std::cell::Cell = const { std::cell::Cell::new(0) }; + static LATER: std::cell::Cell = const { std::cell::Cell::new(0) }; + /// The marks submissions were done with, by name. + static MARKS: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + } + + /// (Ported from Belobug/VIctory-Belobug-AMD 673be0e.) A submission is + /// done with the mark taken when it was put in the line, or, behind a + /// wait of its queue, at least the one taken when that wait went (what + /// is behind a wait may be written only before it is met); one taken + /// along with others, the highest of all. + #[test] + fn a_submission_needs_what_was_written_before_it_and_its_queues_waits() { + let lane = Lane::new(); + let log: Log = Default::default(); + let f = fence(); + let run = |name| T::Run(name, log.clone()); + NOW.set(5); + lane.push(1, [run("a")]); + NOW.set(6); + lane.push(1, [T::Wait(f, 1), run("b")]); + look(&lane); + assert_eq!(MARKS.take(), [("a".to_string(), 5)], "b stands at its wait"); + // The wait is met later; what b reads may have been written just + // before that. + LATER.set(9); + crate::fence::set(f as This, 1); + look(&lane); + assert_eq!(MARKS.take(), [("b".to_string(), 9)]); + // Another queue went past no wait; this one did, for good. + NOW.set(7); + lane.push(2, [run("c")]); + NOW.set(3); + lane.push(1, [run("d")]); + look(&lane); + assert_eq!(MARKS.take(), [("c".to_string(), 7), ("d".to_string(), 9)]); + // Taken along: a met wait between two parts raises them both. + let part = |name| T::Group(vec![name], log.clone(), Vec::new()); + NOW.set(10); + LATER.set(12); + lane.push(3, [part("x"), T::Wait(f, 1), part("y")]); + look(&lane); + assert_eq!(MARKS.take(), [("x+y".to_string(), 12)]); + // A present is no submission: it is told no mark. + lane.push(3, [T::Present("present", log.clone())]); + look(&lane); + assert!(MARKS.take().is_empty()); + assert_eq!(*log.lock().unwrap(), ["a", "b", "c", "d", "x+y", "present"]); } fn fence() -> usize {