What goes up to card copies: a check, and fetches caught in between put right

VICTORY_FETCH_CHECK=1 (off by default): with VICTORY_RELAY_COPY a piece is
fetched beside later submissions, and one of those that clears it and fills
it again with the same is not flagged by its compare: title memory and the
CPU's side kept what was in between, and the next refresh of the 64 KiB
sent it up over the right bytes. The compare now gives the piece's hash
with its flag; a fetch that is not that is made again by the next flight
that may write the piece. (The test shows 11-12 of 12 wrong without it.)

VICTORY_UPCOPY_CHECK=1 (diagnostics): at each send up, the words that the
title did not change and the card holds another value of; every few seconds
at rest, the words in which a copy's card side is not its CPU side, and
whether they were declared as written and fetched.

victory replay --upcopies: the same question of a captured frame, and which
pieces were written undeclared (replay1 and replay2: compute
82a2ad6d07417d36 through views picked by index, 73-75 pieces a frame).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:17:50 -06:00
1 parent da89c8a5a9
commit f86aae992a
10 files changed
+1688 -54

No files matched your search

+342
View File
@@ -0,0 +1,342 @@
//! Pieces of card copies that came down while a later flight was writing
//! them (`VICTORY_FETCH_CHECK`, with `VICTORY_RELAY_COPY`).
//!
//! On the copy queue a fetch runs beside later submissions (`relay_copy`),
//! so what comes down of a piece may be what it held in the middle of one:
//! a counter cleared and not counted up again, a list half filled. Such
//! bytes went into title memory and the copy's CPU side, to be put right by
//! that submission's own landing, which fetches what its compare flagged.
//! But the compare flags a piece only if it is not, at the submission's end,
//! what it was at the end of the one before. A piece cleared and filled
//! again with the same is not flagged, is not fetched, and the CPU's side
//! keeps the bytes from in between: until they go up with a refresh of
//! their 64 KiB, over the right ones.
//!
//! So the compare gives each piece's hash with its flag, and what came down
//! is hashed here too. The same hash: it is what the flight ended with. Not
//! the same: the piece is suspect (another flight was at it, or the title's
//! bytes went up to it meanwhile), and the next flight that may write it
//! fetches it whatever its compare says, until a fetch is found whole.
//!
//! What it costs: pieces are fetched whole where a flight declared a part
//! of one, and 64 KiB of what came down are hashed (some 4 us) just before
//! they are merged, which reads them again.
use std::collections::{HashMap, HashSet};
use super::*;
/// `VICTORY_FETCH_CHECK=1`; off by default until a run has shown how often a
/// piece is caught (the frame-rate line says so) and what the hashes cost
/// (3-4 ms of the landing thread's time for 96 MB, beside the merge).
pub(super) fn on() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_FETCH_CHECK").is_some_and(|v| v == "1"))
}
/// A piece: its copy's number, and the piece.
pub(super) type Piece = (u64, u32);
/// Since the last frame-rate line: pieces fetched and hashed, those found
/// suspect, and those fetched because an earlier fetch was.
pub(super) static STATS: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3];
/// The pieces whose last fetch was not what a flight ended with.
#[derive(Default)]
pub(super) struct Suspects(HashSet<Piece>);
impl Suspects {
/// Whether a flight that may write `piece` fetches it, its compare
/// having flagged it or not.
pub(super) fn wanted(&self, piece: Piece, flagged: bool) -> bool {
flagged || self.0.contains(&piece)
}
/// `piece` was fetched; `same`: it was what the last of the flights
/// landed ended with.
pub(super) fn fetched(&mut self, piece: Piece, same: bool) {
STATS[0].fetch_add(1, Ordering::Relaxed);
if same {
if self.0.remove(&piece) {
STATS[2].fetch_add(1, Ordering::Relaxed);
}
} else {
self.0.insert(piece);
STATS[1].fetch_add(1, Ordering::Relaxed);
}
}
}
/// A piece to fetch.
pub(super) struct Fetch {
pub(super) shadow: std::sync::Arc<crate::shadow::Shadow>,
/// The offsets fetched, and where they come down.
pub(super) from: usize,
pub(super) to: usize,
pub(super) at: u64,
/// The offsets merged: what flights declared of it.
pub(super) merged: Vec<(usize, usize)>,
/// With the check: the piece, and the hash the last flight that may
/// have written it ended with.
pub(super) check: Option<(u32, Option<u32>)>,
}
/// What is fetched for `plans`, whose compares' results are in `differs`.
/// With `suspects`, pieces are fetched whole, with the hash to compare.
pub(super) fn fetches(plans: &[&CardPlan], differs: &[u32], suspects: Option<&Suspects>) -> Vec<Fetch> {
let mut out: Vec<Fetch> = Vec::new();
let mut total = 0u64;
let mut seen = HashSet::new();
// With `suspects`: where each piece is in `out`.
let mut index: HashMap<Piece, usize> = HashMap::new();
for plan in plans {
let flags: Vec<(usize, u32, bool)> = plan
.ranges
.iter()
.flat_map(|r| {
(0..r.span.pieces).map(move |p| {
let piece = r.span.first + p;
let flagged = !r.compared || differs.get((r.at + p) as usize).is_none_or(|&d| flagged(d));
let wanted = suspects.map_or(flagged, |s| s.wanted((r.shadow.id(), piece), flagged));
(std::sync::Arc::as_ptr(&r.shadow) as usize, piece, wanted)
})
})
.collect();
let fetched = flagged_pieces(&flags);
for r in &plan.ranges {
let copy = std::sync::Arc::as_ptr(&r.shadow) as usize;
if upcheck::on() {
upcheck::declared(r.shadow.id(), r.span.from as usize, r.span.to as usize);
}
for p in 0..r.span.pieces {
let piece = r.span.first + p;
if !fetched.contains(&(copy, piece)) {
continue;
}
let (lo, hi) = r.shadow.card_piece(&r.span, p);
if lo >= hi || !seen.insert((copy, lo, hi)) {
continue;
}
if upcheck::on() {
upcheck::fetched(r.shadow.id(), lo);
}
if suspects.is_none() {
out.push(Fetch {
shadow: r.shadow.clone(),
from: lo,
to: hi,
at: total,
merged: vec![(lo, hi)],
check: None,
});
total += (hi - lo) as u64;
continue;
}
let i = *index.entry((r.shadow.id(), piece)).or_insert_with(|| {
let (from, to) = r.shadow.card_hashed(piece);
out.push(Fetch {
shadow: r.shadow.clone(),
from,
to,
at: total,
merged: Vec::new(),
check: Some((piece, None)),
});
total += (to - from) as u64;
out.len() - 1
});
out[i].merged.push((lo, hi));
}
}
}
if suspects.is_some() {
// The hash of the last flight that may have written the piece.
for plan in plans {
for r in plan.ranges.iter().filter(|r| r.compared) {
for p in 0..r.span.pieces {
if let Some(&i) = index.get(&(r.shadow.id(), r.span.first + p))
&& let Some(&d) = differs.get((r.at + p) as usize)
&& let Some((_, hash)) = &mut out[i].check
{
*hash = Some(folded(d));
}
}
}
}
}
out
}
/// Of a result of the compare: whether the piece differs.
pub(super) fn flagged(result: u32) -> bool {
result & 1 != 0
}
/// Of a result of the compare: the piece's hash, 31 bits of it.
pub(super) fn folded(result: u32) -> u32 {
result >> 1
}
/// The hash the compare on the card ([`CARD_COMPARE`]) makes of a piece
/// that holds `bytes` (a multiple of 16 of them), as [`folded`].
pub(super) fn hash(bytes: &[u8]) -> u32 {
#[cfg(target_arch = "x86_64")]
if std::is_x86_feature_detected!("avx2") {
// SAFETY: the feature is there.
return unsafe { hash_avx2(bytes) };
}
hash_plain(bytes)
}
const MUL: [u32; 4] = [0x0100_0193, 0x9E37_79B1, 0x85EB_CA77, 0xC2B2_AE3D];
const SEED: [u32; 4] = [0x9E37_79B9, 0x85EB_CA6B, 0xC2B2_AE35, 0x27D4_EB2F];
fn step(h: &mut [u32; 4], with: [u32; 4]) {
let x: [u32; 4] = std::array::from_fn(|i| (h[i] ^ with[i]).wrapping_mul(MUL[i]));
*h = std::array::from_fn(|i| x[(i + 1) % 4] ^ (x[i] >> 15));
}
/// The 64 threads' hashes as one, folded to 31 bits.
fn finish(parts: &[[u32; 4]; 64]) -> u32 {
let mut h = parts[0];
for part in &parts[1..] {
step(&mut h, *part);
}
let f = h[0] ^ h[1].wrapping_mul(3) ^ h[2].wrapping_mul(5) ^ h[3].wrapping_mul(7);
f & 0x7FFF_FFFF
}
fn seeds() -> [[u32; 4]; 64] {
std::array::from_fn(|thread| SEED.map(|s| s.wrapping_add(thread as u32)))
}
fn tail(parts: &mut [[u32; 4]; 64], bytes: &[u8]) {
for (thread, chunk) in bytes.chunks_exact(16).enumerate() {
let with = std::array::from_fn(|i| u32::from_le_bytes([chunk[i * 4], chunk[i * 4 + 1], chunk[i * 4 + 2], chunk[i * 4 + 3]]));
step(&mut parts[thread], with);
}
}
fn hash_plain(bytes: &[u8]) -> u32 {
let mut parts = seeds();
for row in bytes.chunks(1024) {
tail(&mut parts, row);
}
finish(&parts)
}
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
fn hash_avx2(bytes: &[u8]) -> u32 {
use std::arch::x86_64::*;
let mut parts = seeds();
let rows = bytes.chunks_exact(1024);
let rest = rows.remainder();
let mul = _mm256_setr_epi32(
MUL[0] as i32, MUL[1] as i32, MUL[2] as i32, MUL[3] as i32, MUL[0] as i32, MUL[1] as i32, MUL[2] as i32, MUL[3] as i32,
);
let state = parts.as_mut_ptr().cast::<__m256i>();
for row in rows {
let from = row.as_ptr().cast::<__m256i>();
for k in 0..32 {
// SAFETY: two threads' hashes of 16 bytes each, and their 32 of
// the row's 1,024 bytes.
unsafe {
let h = _mm256_xor_si256(_mm256_loadu_si256(state.add(k)), _mm256_loadu_si256(from.add(k)));
let x = _mm256_mullo_epi32(h, mul);
// y, z, w, x of each.
let turned = _mm256_shuffle_epi32::<0b00_11_10_01>(x);
_mm256_storeu_si256(state.add(k), _mm256_xor_si256(turned, _mm256_srli_epi32::<15>(x)));
}
}
}
tail(&mut parts, rest);
finish(&parts)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_wide_hash_is_the_plain_one() {
let bytes: Vec<u8> = (0..65_536u32).map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8).collect();
for n in [0usize, 16, 32, 1008, 1024, 1040, 5000 & !15, 65_536] {
assert_eq!(hash(&bytes[..n]), hash_plain(&bytes[..n]), "{n} bytes");
}
let mut other = bytes.clone();
other[40_000] ^= 1;
assert_ne!(hash(&bytes), hash(&other));
}
/// A model of one piece. The card runs flights in order; each writes the
/// piece several times and its compare flags it if it ends otherwise
/// than the flight before. Each landing fetches what is wanted, on a
/// queue beside the drawing one: the fetch sees the piece as it is while
/// a later flight is at some step. Returns what the CPU's side holds in
/// the end, and the fetches.
fn landed(flights: &[&[u32]], seen: &[(usize, usize)], check: bool) -> (u32, u32) {
let piece: Piece = (1, 0);
let mut suspects = Suspects::default();
let (mut held, mut ended, mut fetches) = (0, 0, 0);
for (f, steps) in flights.iter().enumerate() {
let before = ended;
ended = *steps.last().expect("a step");
let flagged = ended != before;
if if check { !suspects.wanted(piece, flagged) } else { !flagged } {
continue;
}
fetches += 1;
// While flight `at` is after its step `step`.
let (at, step) = seen[f];
assert!(at >= f);
held = flights[at][step];
suspects.fetched(piece, held == ended);
}
(held, fetches)
}
/// A counter that each flight clears and counts up to the same: the
/// first flight's fetch sees it cleared by the second, whose compare
/// flags nothing.
#[test]
fn a_piece_caught_in_between_is_fetched_again() {
let count: &[u32] = &[0, 3, 5];
// The first is fetched while the second has cleared it.
assert_eq!(landed(&[count, count], &[(1, 0), (1, 2)], false), (0, 1), "the CPU's side keeps what was in between");
assert_eq!(landed(&[count, count], &[(1, 0), (1, 2)], true), (5, 2));
// And again, while the third was at it; the fourth finds it whole.
let four = [count; 4];
assert_eq!(landed(&four, &[(1, 0), (2, 1), (3, 0), (3, 2)], true), (5, 4));
// Fetched whole: no more fetches than without the check.
assert_eq!(landed(&four, &[(0, 2), (1, 2), (2, 2), (3, 2)], true), (5, 1));
assert_eq!(landed(&four, &[(0, 2), (1, 2), (2, 2), (3, 2)], false), (5, 1));
}
/// What the hash costs (ignored: a measurement): pieces that just came
/// down, hashed alone, compared with what the CPU's side holds (as the
/// merge begins), and both.
#[test]
#[ignore]
fn hash_costs() {
let (bytes, held) = (vec![0x5Au8; 96 << 20], vec![0x5Au8; 96 << 20]);
let pieces = || bytes.chunks(64 << 10).zip(held.chunks(64 << 10));
let compare = |a: &[u8], b: &[u8]| a.chunks(4096).zip(b.chunks(4096)).filter(|(a, b)| a != b).count() as u32;
for _ in 0..5 {
let mut all = 0u32;
let mut time = |f: &dyn Fn(&[u8], &[u8]) -> u32| {
let t = std::time::Instant::now();
for (a, b) in pieces() {
all ^= f(a, b);
}
t.elapsed().as_secs_f64() * 1e3
};
let (hashed, compared, both) = (
time(&|a, _| hash(a)),
time(&compare),
time(&|a, b| hash(a) ^ compare(a, b)),
);
println!("96 MB: hashed {hashed:.2} ms, compared {compared:.2} ms, both {both:.2} ms ({all:#x})");
}
}
}