Merge night/cpu: processor work, wait instructions patched, submit thread (off)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 06:21:22 -06:00
commit 7d51f42418
14 files changed
+2287 -240

No files matched your search

+104 -14
View File
@@ -1249,20 +1249,40 @@ fn pooled(n: usize) -> Vec<u8> {
v
}
/// Returns a snapshot buffer to the pool: at most 8 kept, the largest, and
/// of those the largest one and the next ones up to `KEPT` bytes in all.
/// How many snapshot buffers are kept (`VICTORY_SNAPSHOT_POOL`; `8`: as
/// before).
///
/// A frame gives some 330 ranges of copies in the PC's memory to shaders, and
/// most are of copies of their own; with two or three frames in the air eight
/// buffers were the eight largest, and every other snapshot was memory fresh
/// from the kernel and given back to it: 11 % of the submitting thread in the
/// opening (profile cpu-1: `NtAllocateVirtualMemory` under
/// [`Shadow::writing`]), and the page faults of filling it.
fn snapshots_kept() -> usize {
static MOST: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
*MOST.get_or_init(|| std::env::var("VICTORY_SNAPSHOT_POOL").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(1024))
}
/// Returns a snapshot buffer to the pool: at most [`snapshots_kept`] kept,
/// the largest, and of those the largest one and the next ones up to `KEPT`
/// bytes in all.
fn give_back(v: Vec<u8>) {
const KEPT: usize = 512 << 20;
let mut pool = POOL.lock().unwrap_or_else(|e| e.into_inner());
pool.push(v);
pool.sort_by_key(|v| std::cmp::Reverse(v.capacity()));
pool.truncate(8);
let (mut seen, mut total) = (0, 0usize);
pool.retain(|v| {
seen += 1;
let (mut kept, mut total) = (0, 0usize);
for v in pool.iter().take(snapshots_kept()) {
total = total.saturating_add(v.capacity());
seen == 1 || total <= KEPT
});
if kept > 0 && total > KEPT {
break;
}
kept += 1;
}
// (Given back to the kernel without the lock: milliseconds.)
let gone = pool.split_off(kept);
drop(pool);
drop(gone);
}
const NOT_WATCHED: u8 = 2;
@@ -2423,6 +2443,66 @@ fn stale_pages_in(base: usize, copied: &[u64], a: usize, e: usize, written: &[(u
.collect()
}
/// `runs` (from, to, whether the copy held it before), cut in those of pages
/// held before (`pages`: page number and asking, in order) to the parts of
/// each page that were written since (`parts`: a bit for each 4 KiB).
fn by_parts(runs: Vec<(usize, usize, bool)>, pages: &[(usize, u64)], parts: &[u16]) -> Vec<(usize, usize, bool)> {
const PART: usize = PAGE / 16;
if parts.iter().all(|&p| p == u16::MAX) {
return runs;
}
let mut out: Vec<(usize, usize, bool)> = Vec::with_capacity(runs.len() + pages.len());
let mut pages = pages.iter().zip(parts).peekable();
for (from, to, known) in runs {
if !known {
out.push((from, to, known));
continue;
}
while let Some((&(page, _), &parts)) = pages.next_if(|((page, _), _)| page * PAGE < to) {
for k in (0..16).filter(|k| parts >> k & 1 != 0) {
let (a, b) = ((page * PAGE + k * PART).max(from), (page * PAGE + (k + 1) * PART).min(to));
if a >= b {
continue;
}
match out.last_mut() {
Some((_, end, true)) if *end == a => *end = b,
_ => out.push((a, b, true)),
}
}
}
}
out
}
#[cfg(test)]
mod parts_tests {
use super::{PAGE, by_parts};
#[test]
fn runs_are_cut_to_the_parts_written() {
const P: usize = PAGE / 16;
let at = |page: usize, part: usize| page * PAGE + part * P;
// Pages 10 and 11 again (from the middle of 10's part 1 on), 12
// never taken, 14 again.
let runs = vec![
(at(10, 1) + 100, at(12, 0), true),
(at(12, 0), at(13, 0), false),
(at(14, 0), at(14, 9) + 7, true),
];
let pages = [(10, 5), (11, 5), (14, 6)];
assert_eq!(by_parts(runs.clone(), &pages, &[u16::MAX; 3]), runs);
assert_eq!(
by_parts(runs, &pages, &[0b1000_0000_0000_0011, 0b11, 0b10_0000_0000]),
[
(at(10, 1) + 100, at(10, 2), true),
(at(10, 15), at(11, 2), true),
(at(12, 0), at(13, 0), false),
(at(14, 9), at(14, 9) + 7, true),
]
);
}
}
#[cfg(test)]
mod stale_tests {
use super::*;
@@ -3255,19 +3335,23 @@ impl Shadow {
// SAFETY: inside the mapping of the copy's CPU side,
// which only the submitting thread writes.
let held = unsafe { std::slice::from_raw_parts_mut(self.data.add(at), n) };
let runs = changed_runs(title, held);
if runs.len() > *runs_left {
// Whole words go up as pairs, all of them in one
// dispatch: only what is a copy of its own counts.
let runs = if crate::host::words_under() { dword_runs(lo, title, held, 0) } else { changed_runs(title, held) };
let own = |&&(r, m): &&(usize, usize)| !crate::host::words_under() || !crate::host::goes_as_words(at + r, m);
let copies = runs.iter().filter(own).count();
if copies > *runs_left {
note_stale(2);
return true;
}
*runs_left -= runs.len();
*runs_left -= copies;
let was = (!runs.is_empty() && crate::host::upcheck::on()).then(|| held.to_vec());
for (r, m) in runs {
for &(r, m) in &runs {
held[r..r + m].copy_from_slice(&title[r..r + m]);
self.card_changed(at + r, at + r + m);
MERGED[1].fetch_add(1, Ordering::Relaxed);
MERGED[2].fetch_add(m as u64, Ordering::Relaxed);
}
MERGED[1].fetch_add(runs.len() as u64, Ordering::Relaxed);
self.card_changed_runs(runs.into_iter().map(|(r, m)| (at + r, at + r + m)));
if let Some(was) = was {
crate::host::upcheck::note(self.id, at, &was, held);
}
@@ -3484,6 +3568,8 @@ impl Shadow {
// since, is as it was.
let ranged = if crate::watch::light() { self.ranged(from, to) } else { 0 };
let (mut runs, mut run) = (Vec::new(), None::<(usize, bool)>);
// Pages taken before and written since: in the order of their runs.
let mut again_at: Vec<(usize, u64)> = Vec::new();
let last = (to - 1) / PAGE;
for page in from / PAGE..=last {
let write = match writes.peek() {
@@ -3504,6 +3590,9 @@ impl Shadow {
}
// Cut where pages taken before meet pages never taken.
let again = taken != 0;
if again {
again_at.push((page, taken));
}
if let Some((first, known)) = run
&& known != again
{
@@ -3519,6 +3608,7 @@ 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);
if crate::watch::light() {
// All of the range is in the copy as of this asking.