diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index c115790..d77472d 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -9039,32 +9039,71 @@ impl Batch { // Those known to copy nothing are said so here, unless they are very // many: then beside the others. const FEW: usize = 256; - let mut spans = spans; - let many = spans.len() > FEW; - if !many { - let all = spans.len(); - spans.retain(|(shadow, from, to)| !shadow.nothing_new(*from, *to - *from)); - FLUSHES[2].fetch_add((all - spans.len()) as u64, Ordering::Relaxed); - } - let ahead = spans.len() > 1 && !crate::watch::kept_active(); + const CHUNK: usize = 64; + let spans = std::sync::Arc::new(spans); + let dirty: Vec = if spans.len() <= FEW { + let j = crate::watch::read_journal(); + let mut keep = Vec::with_capacity(spans.len()); + for (i, (shadow, from, to)) in spans.iter().enumerate() { + if !shadow.nothing_new_in(&j, *from, *to - *from) { + keep.push(i); + } + } + FLUSHES[2].fetch_add((spans.len() - keep.len()) as u64, Ordering::Relaxed); + keep + } else { + let flags: std::sync::Arc> = std::sync::Arc::new( + (0..spans.len()).map(|_| std::sync::atomic::AtomicBool::new(false)).collect(), + ); + let chunks = spans.len().div_ceil(CHUNK); + let spans_ref = spans.clone(); + let flags_ref = flags.clone(); + crate::shadow::wide_each(chunks, move |c| { + let start = c * CHUNK; + let end = (start + CHUNK).min(spans_ref.len()); + let j = crate::watch::read_journal(); + let mut skipped = 0u64; + for i in start..end { + let (shadow, from, to) = &spans_ref[i]; + if shadow.nothing_new_in(&j, *from, *to - *from) { + skipped += 1; + } else { + flags_ref[i].store(true, Ordering::Relaxed); + } + } + if skipped != 0 { + FLUSHES[2].fetch_add(skipped, Ordering::Relaxed); + } + }); + flags + .iter() + .enumerate() + .filter_map(|(i, f)| f.load(Ordering::Relaxed).then_some(i)) + .collect() + }; + let ahead = dirty.len() > 1 && !crate::watch::kept_active(); if ahead { - let ranges: Vec<(usize, usize)> = spans.iter().filter(|r| r.0.asked_ahead()).map(|r| (r.1, r.2)).collect(); + let ranges: Vec<(usize, usize)> = dirty + .iter() + .map(|&i| &spans[i]) + .filter(|r| r.0.asked_ahead()) + .map(|r| (r.1, r.2)) + .collect(); crate::watch::ask_ahead(&ranges); } FLUSHES[3].fetch_add(spans.len() as u64, Ordering::Relaxed); - crate::shadow::wide_each(spans.len() + due.len(), move |i| { - // The textures first: they are the longer pieces. - let Some(i) = i.checked_sub(due.len()) else { - due[i].refresh(); - return; - }; - let (shadow, from, to) = &spans[i]; - if many && shadow.nothing_new(*from, *to - *from) { - FLUSHES[2].fetch_add(1, Ordering::Relaxed); - return; - } - Self::update(shadow, *from, *to); - }); + let work = dirty.len() + due.len(); + if work > 0 { + crate::shadow::wide_each(work, move |i| { + // The textures first: they are the longer pieces. + let Some(i) = i.checked_sub(due.len()) else { + due[i].refresh(); + return; + }; + let (shadow, from, to) = &spans[dirty[i]]; + Self::update(shadow, *from, *to); + }); + } if ahead { crate::watch::ask_ahead_done(); } @@ -9185,13 +9224,19 @@ impl Batch { // and any thread woken. (Thousands of them, the heap's buffer views // once a frame: on the threads, below.) const FEW: usize = 256; + const CHUNK: usize = 64; let light = crate::watch::light(); if light && self.refreshes.len() <= FEW { let started = std::time::Instant::now(); let all = self.refreshes.len(); - self.refreshes.retain(|(shadow, from, to)| !shadow.nothing_new(*from, *to - *from)); + let j = crate::watch::read_journal(); + self.refreshes.retain(|(shadow, from, to)| !shadow.nothing_new_in(&j, *from, *to - *from)); + drop(j); PREPARING[0].fetch_add(started.elapsed().as_micros() as u64, Ordering::Relaxed); FLUSHES[2].fetch_add((all - self.refreshes.len()) as u64, Ordering::Relaxed); + if self.refreshes.is_empty() { + return; + } } // One write-watch asking for the copies that lie close together, // not one per copy. @@ -9215,16 +9260,49 @@ impl Batch { let before = PREPARING[0].load(Ordering::Relaxed); 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) { - FLUSHES[2].fetch_add(1, Ordering::Relaxed); - return; + if refreshes.len() > FEW { + let refreshes = std::sync::Arc::new(refreshes); + let flags: std::sync::Arc> = std::sync::Arc::new( + (0..refreshes.len()).map(|_| std::sync::atomic::AtomicBool::new(false)).collect(), + ); + let chunks = refreshes.len().div_ceil(CHUNK); + let r_ref = refreshes.clone(); + let f_ref = flags.clone(); + crate::shadow::wide_each(chunks, move |c| { + let start = c * CHUNK; + let end = (start + CHUNK).min(r_ref.len()); + let j = crate::watch::read_journal(); + let mut skipped = 0u64; + for i in start..end { + let (shadow, from, to) = &r_ref[i]; + if shadow.nothing_new_in(&j, *from, *to - *from) { + skipped += 1; + } else { + f_ref[i].store(true, Ordering::Relaxed); + } + } + if skipped != 0 { + FLUSHES[2].fetch_add(skipped, Ordering::Relaxed); + } + }); + let dirty: Vec = flags + .iter() + .enumerate() + .filter_map(|(i, f)| f.load(Ordering::Relaxed).then_some(i)) + .collect(); + if !dirty.is_empty() { + crate::shadow::wide_each(dirty.len(), move |i| { + let (shadow, from, to) = &refreshes[dirty[i]]; + Self::update(shadow, *from, *to); + }); } - Self::update(shadow, *from, *to); - }); + } else { + crate::shadow::wide_each(refreshes.len(), move |i| { + let (shadow, from, to) = &refreshes[i]; + Self::update(shadow, *from, *to); + }); + } } else { let jobs: Vec> = self .refreshes diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 5ccb5ae..7763dd0 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -3912,6 +3912,26 @@ impl Shadow { } } + /// [`Shadow::changed`] for multiple offset ranges under a single `kept` lock. + fn changed_runs(&self, runs: impl Iterator) { + if !keeps() { + return; + } + let mut runs = runs.filter(|r| r.0 < r.1).peekable(); + if runs.peek().is_none() { + return; + } + let mut kept = self.kept.lock().unwrap_or_else(|e| e.into_inner()); + if kept.0.is_empty() { + return; + } + for (from, to) in runs { + for piece in self.pieces(from, to) { + kept.0[piece] = false; + } + } + } + /// Whether shaders of a submission not yet copied back may be writing /// the copy. #[inline] @@ -4216,6 +4236,12 @@ impl Shadow { /// nothing, and knows it without asking anything /// (`crate::watch::nothing_new`). `false` if it would have to ask. pub(crate) fn nothing_new(&self, address: usize, len: usize) -> bool { + let j = crate::watch::read_journal(); + self.nothing_new_in(&j, address, len) + } + + /// [`Shadow::nothing_new`] using an already-held write-watch journal read guard. + pub(crate) fn nothing_new_in(&self, j: &crate::watch::JournalGuard, address: usize, len: usize) -> bool { if self.place.is_some() { return true; } @@ -4235,7 +4261,7 @@ impl Shadow { } let copied = self.copied.lock().unwrap_or_else(|e| e.into_inner()); let ranged = Self::ranged_in(&copied.1, from, to); - let same = crate::watch::nothing_new(from, to, start / PAGE, &copied.0, ranged); + let same = crate::watch::nothing_new_in(j, from, to, start / PAGE, &copied.0, ranged); #[cfg(feature = "diagnostics")] if same { use crate::copystat::{C, add}; @@ -4255,11 +4281,15 @@ impl Shadow { /// [`Shadow::update`] of `[from, to)` (inside the buffer) where no /// shader writes. fn update_free(&self, from: usize, to: usize) -> usize { - if self.watched.load(Ordering::Relaxed) != NOT_WATCHED { + let prev = self.watched.load(Ordering::Relaxed); + if prev != NOT_WATCHED { if let Some((now, written)) = crate::watch::writes(from, to, true) { // (A range whose blocks rest after a write has no guard now.) - let guarded = crate::watch::guarded(from, to) || self.watched.load(Ordering::Relaxed) == GUARDED; - self.watched.store(if guarded { GUARDED } else { 1 }, Ordering::Relaxed); + let guarded = prev == GUARDED || crate::watch::guarded(from, to); + let next = if guarded { GUARDED } else { 1 }; + if next != prev { + self.watched.store(next, Ordering::Relaxed); + } let copied = self.copy_stale(from, to, now, &written); self.refresh_check(from, to); #[cfg(feature = "diagnostics")] @@ -4393,8 +4423,14 @@ impl Shadow { if let Some((first, known)) = run { runs.push(((first * PAGE).max(from), ((last + 1) * PAGE).min(to), known)); } - let runs = by_parts(runs, &again_at, &crate::watch::parts_written(&again_at)); - let bytes = self.take_all(runs); + let bytes = if runs.is_empty() { + 0 + } else if again_at.is_empty() { + self.take_all(runs) + } else { + let runs = by_parts(runs, &again_at, &crate::watch::parts_written(&again_at)); + self.take_all(runs) + }; if crate::watch::light() { // All of the range is in the copy as of this asking. let ranged = &mut copied.1; @@ -4532,6 +4568,7 @@ impl Shadow { }; let whole = !known && card.before.get().is_none(); let start = self.start as usize; + let (mut send0, mut send1, mut send2, mut send3) = (0u64, 0u64, 0u64, 0u64); TITLE.with_borrow_mut(|title| { title.resize(PAGE, 0); let mut at = from; @@ -4546,24 +4583,37 @@ impl Shadow { // refresh lie apart. let held = unsafe { std::slice::from_raw_parts_mut(self.data.add(at - start), n) }; let was = crate::host::upcheck::on().then(|| held.to_vec()); - let runs = if whole { vec![(0, n)] } else { dword_runs(at, held, title, 0) }; + let runs = if whole { vec![(0, n)] } else { word_runs(at, held, title) }; + let mut page_bytes = 0u64; for &(a, len) in &runs { held[a..a + len].copy_from_slice(&title[a..a + len]); - self.changed(at - start + a, at - start + a + len); + page_bytes += len as u64; + } + if !runs.is_empty() { + self.changed_runs(runs.iter().map(|&(a, len)| (at - start + a, at - start + a + len))); + self.card_changed_runs(runs.iter().map(|&(a, len)| (at - start + a, at - start + a + len))); } - self.card_changed_runs(runs.iter().map(|&(a, len)| (at - start + a, at - start + a + len))); if let Some(was) = was { crate::host::upcheck::note(self.id, at - start, &was, held); } - SEND[0].fetch_add(n as u64, Ordering::Relaxed); + send0 += n as u64; if runs.is_empty() { - SEND[1].fetch_add(n as u64, Ordering::Relaxed); + send1 += n as u64; + } else { + send2 += page_bytes; + send3 += runs.len() as u64; } - SEND[2].fetch_add(runs.iter().map(|r| r.1 as u64).sum(), Ordering::Relaxed); - SEND[3].fetch_add(runs.len() as u64, Ordering::Relaxed); at += n; } }); + SEND[0].fetch_add(send0, Ordering::Relaxed); + if send1 != 0 { + SEND[1].fetch_add(send1, Ordering::Relaxed); + } + if send2 != 0 { + SEND[2].fetch_add(send2, Ordering::Relaxed); + SEND[3].fetch_add(send3, Ordering::Relaxed); + } to - from } diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 301b045..4dfb02b 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -110,6 +110,10 @@ struct Last { /// first checks, and after the hash found a change the guard had not /// seen ([`hot_textures`]). 0: not checked under the guard yet. hot_until: u64, + /// Cached negative result from write-watch (`writes(..., false)` returned + /// `None` or the range is already guarded view memory), avoiding a + /// guaranteed-failing `GetWriteWatch` syscall on every refresh. + not_watched: bool, } /// Frames a guarded texture is hashed at every check from its first check, @@ -1449,20 +1453,26 @@ impl Staging { if alone && !exact && last.hash.is_some() { return 0; } - if !alone && let Some((now, written)) = waited(1, || crate::watch::writes(start, start + size, false)) { - if !exact && last.asked != 0 && written.iter().all(|&(_, w)| w <= last.asked) { - return 0; + if !alone && !last.not_watched { + if crate::watch::guarded(start, start + size) { + last.not_watched = true; + } else if let Some((now, changed)) = waited(1, || crate::watch::writes_since(start, start + size, false, last.asked)) { + if !exact && !changed { + return 0; + } + last.asked = now; + // Read through the copy, which stands unmapped pages: memory of + // big pools is too slow to ask page by page. + with_read(size, |read| { + crate::host::copy_mapped(read.as_mut_ptr(), self.memory, size, false); + self.detile(read); + }); + #[cfg(feature = "diagnostics")] + crate::copystat::add(crate::copystat::C::TexWatched, size as u64); + return size; + } else { + last.not_watched = true; } - last.asked = now; - // Read through the copy, which stands unmapped pages: memory of - // big pools is too slow to ask page by page. - with_read(size, |read| { - crate::host::copy_mapped(read.as_mut_ptr(), self.memory, size, false); - self.detile(read); - }); - #[cfg(feature = "diagnostics")] - crate::copystat::add(crate::copystat::C::TexWatched, size as u64); - return size; } // View memory has no write-watch, and a texture there was hashed // whole at every check: 229 MB and 51 ms a frame in a gameplay view @@ -1479,10 +1489,10 @@ impl Staging { let mut blind = false; if !alone && *GUARD.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_GUARD").is_none_or(|v| v != "0")) - && let Some((now, written)) = waited(1, || crate::watch::writes(start, start + size, true)) + && let Some((now, changed)) = waited(1, || crate::watch::writes_since(start, start + size, true, last.asked)) { const HASH_EVERY: u64 = 8; - let unwritten = last.asked != 0 && written.iter().all(|&(_, w)| w <= last.asked); + let unwritten = !changed; last.asked = now; let frame = crate::frame::current(); let turn = frame diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 0a7d5ed..37fc6bd 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -164,6 +164,14 @@ fn journal_mut() -> std::sync::RwLockWriteGuard<'static, Journal> { waited(2, || JOURNAL.write().unwrap_or_else(|e| e.into_inner())) } +/// Shared read guard over the write-watch journal so a batch of `nothing_new` +/// checks can acquire the lock once instead of per buffer. +pub(crate) struct JournalGuard(std::sync::RwLockReadGuard<'static, Journal>); + +pub(crate) fn read_journal() -> JournalGuard { + JournalGuard(journal()) +} + /// Since the last line: waits and nanoseconds of waiting, of all threads, for /// the ranges held ([`ASKING`]), the journal to read it, the journal to note /// in it, and the guards (faults of the title's among them). @@ -367,17 +375,33 @@ pub(crate) fn ask_ahead(ranges: &[(usize, usize)]) { if kept_active() { return; } + let skip_guarded = light(); let mut pieces: Vec<(usize, usize)> = ranges .iter() - // Not view memory known by its bits: the kernel would refuse the - // asking (and, asked together with others, theirs). + // Not view memory known by its bits or guarded read-only: the kernel + // would refuse the asking (and, asked together with others, theirs). .filter(|r| r.0 < r.1 && !published(r.0, r.1)) .map(|&(start, end)| (start / PAGE, end.div_ceil(PAGE))) + .filter(|&(first, last)| !(skip_guarded && PROTECTED.any(first, last))) .collect(); if pieces.len() < 2 { return; } pieces.sort_unstable(); + // Collapse duplicate or fully-contained page spans before asking. + let mut write = 0usize; + for read in 1..pieces.len() { + let (a, b) = pieces[read]; + if a >= pieces[write].0 && b <= pieces[write].1 { + continue; + } + write += 1; + pieces[write] = (a, b); + } + pieces.truncate(write + 1); + if pieces.len() < 2 { + return; + } let _all = Asking::hold_all(); let (mut asked, mut written) = (Vec::new(), Vec::new()); let mut i = 0; @@ -392,7 +416,7 @@ pub(crate) fn ask_ahead(ranges: &[(usize, usize)]) { last = last.max(pieces[k].1); k += 1; } - ask_run(&pieces[i..k], &mut asked, &mut written); + ask_run(&pieces[i..k], &mut asked, &mut written, false); i = k; } sort_merge_ahead(&mut asked); @@ -578,63 +602,63 @@ pub(crate) fn once() -> bool { } /// With [`once`]: asks about `ranges` (everything a submission's lists -/// refresh) as [`ask_ahead`] does, and lets that asking stand until -/// [`ask_once_end`]. +/// refresh) in a single pass (merged runs and isolated ranges together) and +/// lets that asking stand until [`ask_once_end`]. pub(crate) fn ask_once(ranges: &[(usize, usize)]) { if !once() { return; } ask_once_end(); - ask_ahead(ranges); - ask_rest(ranges); -} - -/// [`ask_once`] after its [`ask_ahead`]: what that left to `writes`, and the -/// asking stands. -fn ask_rest(ranges: &[(usize, usize)]) { - let _all = Asking::hold_all(); - let mut ahead = { - let mut j = journal_mut(); - std::mem::take(&mut j.ahead) - }; - // `ask_ahead` leaves a range that lies alone to `writes`, which asks - // once per flush: with one asking for the submission that was every - // such range asked again at each of its 70 flushes (run 76: 2,300 - // askings a frame with the switch on, as without it). They are asked - // here, once; what is not write-watched stays with `writes`. - let mut written = Vec::new(); - let mut extra = Vec::new(); - for &(start, end) in ranges { - if start >= end || published(start, end) { + let skip_guarded = light(); + let mut pieces: Vec<(usize, usize)> = ranges + .iter() + .filter(|r| r.0 < r.1 && !published(r.0, r.1)) + .map(|&(start, end)| (start / PAGE, end.div_ceil(PAGE))) + .filter(|&(first, last)| !(skip_guarded && PROTECTED.any(first, last))) + .collect(); + if pieces.is_empty() { + return; + } + pieces.sort_unstable(); + // Collapse exact duplicates and fully-contained page spans in O(N). + let mut write = 0usize; + for read in 1..pieces.len() { + let (a, b) = pieces[read]; + if a >= pieces[write].0 && b <= pieces[write].1 { continue; } - let (first, last) = (start / PAGE, end.div_ceil(PAGE)); - if !ahead_contains(&ahead, first, last) - && !extra.iter().any(|&(a, b)| a <= first && last <= b) - && let Some(pages) = kernel_written(first, last) + write += 1; + pieces[write] = (a, b); + } + pieces.truncate(write + 1); + let _all = Asking::hold_all(); + let (mut asked, mut written) = (Vec::new(), Vec::new()); + let mut i = 0; + while i < pieces.len() { + let first = pieces[i].0; + let mut last = pieces[i].1; + let mut k = i + 1; + while k < pieces.len() + && pieces[k].0 <= last + AHEAD_GAP + && last.max(pieces[k].1) - first <= AHEAD_MAX { - written.extend(pages); - extra.push((first, last)); + last = last.max(pieces[k].1); + k += 1; } + ask_run(&pieces[i..k], &mut asked, &mut written, true); + i = k; } - if !extra.is_empty() { - ahead.extend(extra); - sort_merge_ahead(&mut ahead); - } + sort_merge_ahead(&mut asked); note_hot(&written); let mut j = journal_mut(); - // A number of its own, and the one this asking stands under: between - // the two parts another thread may have asked about a range of the - // second, copied it, and the title written it since. Under the first - // part's number that write was older than the copy, and never copied. let now = next_asking(); j.ahead_asked = now; for (page, parts) in written { j.note(page, parts, now); } - let kept = !ahead.is_empty(); + let kept = !asked.is_empty(); j.kept = kept; - j.ahead = ahead; + j.ahead = asked; AHEAD_KEPT.store(kept, std::sync::atomic::Ordering::Release); } @@ -686,10 +710,11 @@ fn elsewhere(at: usize, len: usize) -> Vec<(usize, Vec)> { /// One kernel asking for `pieces` (page ranges in order); by halves if it /// fails, as a run across two reservations or into memory that is not -/// write-watched does. A piece alone is left to [`writes`]. Adds what was -/// asked to `asked` and the pages found written to `written`. -fn ask_run(pieces: &[(usize, usize)], asked: &mut Vec<(usize, usize)>, written: &mut Vec<(usize, u16)>) { - if pieces.len() < 2 { +/// write-watched does. When `ask_single` is false, a piece alone is left to +/// [`writes`]. Adds what was asked to `asked` and the pages found written to +/// `written`. +fn ask_run(pieces: &[(usize, usize)], asked: &mut Vec<(usize, usize)>, written: &mut Vec<(usize, u16)>, ask_single: bool) { + if pieces.is_empty() || (!ask_single && pieces.len() < 2) { return; } let first = pieces[0].0; @@ -699,9 +724,12 @@ fn ask_run(pieces: &[(usize, usize)], asked: &mut Vec<(usize, usize)>, written: asked.push((first, last)); return; } + if pieces.len() < 2 { + return; + } let (a, b) = pieces.split_at(pieces.len() / 2); - ask_run(a, asked, written); - ask_run(b, asked, written); + ask_run(a, asked, written, ask_single); + ask_run(b, asked, written, ask_single); } /// What [`writes`] would say of `[start, end)` from the journal alone, if @@ -729,7 +757,13 @@ pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Ve materialize(start, end.saturating_sub(start)); } let (first, last) = (start / PAGE, end.div_ceil(PAGE)); - let of = |j: &Journal| -> Vec<(usize, u64)> { j.written.range(first..last).map(|(&p, &w)| (p, w)).collect() }; + let of = |j: &Journal| -> Vec<(usize, u64)> { + if first + 1 == last { + j.written.get(&first).map(|&w| vec![(first, w)]).unwrap_or_default() + } else { + j.written.range(first..last).map(|(&p, &w)| (p, w)).collect() + } + }; // Asked ahead with the submission's other ranges: the journal has it. let ahead = |j: &Journal| ahead_contains(&j.ahead, first, last).then(|| (j.ahead_asked, of(j))); if let Some(known) = ahead(&journal()) { @@ -800,29 +834,79 @@ pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Ve Some(answer) } +/// Like [`writes`], but returns `(asking, changed_since)` without allocating a +/// per-page vector on the hot fast paths (`ahead` or `published`). +pub(crate) fn writes_since(start: usize, end: usize, guarded: bool, since: u64) -> Option<(u64, bool)> { + if guarded && soft().is_none() { + materialize(start, end.saturating_sub(start)); + } + let (first, last) = (start / PAGE, end.div_ceil(PAGE)); + let newer = |j: &Journal| -> bool { + if since == 0 { + return true; + } + if first + 1 == last { + j.written.get(&first).is_some_and(|&w| w > since) + } else { + j.written.range(first..last).any(|(_, &w)| w > since) + } + }; + { + let j = journal(); + if ahead_contains(&j.ahead, first, last) { + return Some((j.ahead_asked, newer(&j))); + } + } + if guarded && published(start, end) { + #[cfg(feature = "diagnostics")] + { + tally(2, 1); + crate::faultbook::note(crate::faultbook::What::Known, first, last, 0); + } + let j = journal(); + return Some((next_asking(), newer(&j))); + } + let (now, written) = writes(start, end, guarded)?; + let changed = since == 0 || written.iter().any(|&(_, w)| w > since); + Some((now, changed)) +} + /// Whether nothing of `[start, end)` was written that a copy with the notes /// `copied` (the asking after which each page from page `base` on was taken) /// and `ranged` (the asking after which a range that holds this one was /// taken whole, or 0) does not hold, as [`writes`] would answer with nothing /// asked: the submission's asking covers the range, or its blocks are known /// by their bits. `false` if something would have to be asked. -pub(crate) fn nothing_new(start: usize, end: usize, base: usize, copied: &[u64], ranged: u64) -> bool { +pub(crate) fn nothing_new_in(j: &JournalGuard, start: usize, end: usize, base: usize, copied: &[u64], ranged: u64) -> bool { let (first, last) = (start / PAGE, end.div_ceil(PAGE)); - // The bits first, then the journal, as in `writes`. - let known = published(start, end); - #[cfg(feature = "diagnostics")] - if known { - crate::faultbook::note(crate::faultbook::What::Known, first, last, 1); - } - let j = journal(); - if !known && !ahead_contains(&j.ahead, first, last) { + let covered = { + #[cfg(feature = "diagnostics")] + { + let known = published(start, end); + if known { + crate::faultbook::note(crate::faultbook::What::Known, first, last, 1); + } + known || ahead_contains(&j.0.ahead, first, last) + } + #[cfg(not(feature = "diagnostics"))] + { + ahead_contains(&j.0.ahead, first, last) || published(start, end) + } + }; + if !covered { return false; } let Some(taken) = first.checked_sub(base).and_then(|from| copied.get(from..last - base)) else { return false; }; - (ranged != 0 || taken.iter().all(|&at| at != 0)) - && j.written.range(first..last).all(|(&page, &write)| write <= taken[page - first].max(ranged)) + if ranged == 0 && taken.iter().any(|&at| at == 0) { + return false; + } + if first + 1 == last { + j.0.written.get(&first).is_none_or(|&write| write <= taken[0].max(ranged)) + } else { + j.0.written.range(first..last).all(|(&page, &write)| write <= taken[page - first].max(ranged)) + } } impl Tracked { @@ -3750,7 +3834,7 @@ mod tests { let (copied, _) = writes(p, p + 4 * PAGE, false).expect("watched"); // SAFETY: committed pages of ours. unsafe { ((p + PAGE) as *mut u8).write_volatile(1) }; - ask_rest(&ranges); + ask_once(&ranges); let last = journal().written.get(&(p / PAGE + 1)).copied(); ask_once_end(); assert!(last.is_some_and(|last| last > copied), "{last:?} is not after {copied}");