gpu: fuse ask_once, batch journal nothing_new, use AVX2 word_runs in Shadow::take, and cache non-watched view textures

This commit is contained in:
theme613 committed 2026-11-28 19:53:17 +08:00
1 parent 3a4ce453cd
commit 3f3e525d55
4 files changed
+342 -120

No files matched your search

+145 -61
View File
@@ -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<usize>)> {
/// 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}");