Sends: what stays under pressure within a line of bytes scaled by free RAM, and made in 64 MiB steps past 64 MiB

The bench on the first commit: the send-trim cost is gone and the send
buffers made and let go fell to a handful in 5 minutes of a walk, but
one sweep made 640 MiB at once in two buffers (by doubles, 512 and 128),
and the spares then sat at ~656 MiB on a 32 GB PC that pages heavily: a
burst's buffers were in use in the window, so they stayed for all of it.

- What was in use is counted in the bytes the requests needed at once
  too, each at the size made for it (not the size of a bigger spare a
  small request took). A trim keeps within a line: all of what the last
  sweep needed (the working set, made again within a frame if it goes,
  so letting it go saves nothing; with two frames in the air, the busiest
  frame twice over), and of the window's busiest moment beyond that only
  kept() scaled by the share of 8 GiB of RAM that is free (the copy
  sources' keeps_under_pressure line); at least the hot floor. Past the
  line the biggest go first, in use or not.
- Send memory past 64 MiB is made in 64 MiB steps, not by doubles: at
  most 64 MiB more than asked for (512 MiB for a request just past 256
  before). A submission's sends stay in one buffer (shortcut: record,
  its scatter pass and the copy queue each address one).
- The sweep's line also says the bytes needed at once.

host::send: 15 passed, 1 ignored; among them a walk with two frames in
the air and a burst of 300 + 100 MB: 320 + 128 MiB made, kept one sweep,
then 184 MiB with 2 GiB free (504 with 16), 56 once the window passed,
nothing made again. Not run in game.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lyrnc
2026-11-30 11:49:23 +03:00
parent 697590ad26
commit 1ecaa98a9f
2 changed files with 328 additions and 142 deletions

View File

@ -155,8 +155,9 @@ pub(super) struct Memory {
buffer: ID3D12Resource,
data: usize,
size: u64,
/// A submission has it, not the spares ([`IN_USE`]).
lent: bool,
/// While a submission has it, not the spares: the bytes its request
/// needed, at the size made for it ([`IN_USE`]).
lent: Option<u64>,
}
// SAFETY: a D3D12 object (free-threaded) and its mapping, with one owner.
@ -166,8 +167,8 @@ impl Drop for Memory {
fn drop(&mut self) {
// A submission's that is let go without coming back (a plan given
// up) is not in use any more either.
if self.lent {
IN_USE.back(self.size);
if let Some(need) = self.lent {
IN_USE.back(self.size, need);
}
}
}
@ -175,60 +176,87 @@ impl Drop for Memory {
/// What submissions that have run gave back.
static SPARE: Mutex<Vec<Memory>> = Mutex::new(Vec::new());
/// The least send memory made ([`made_for`]); the others are its doubles.
/// The least send memory made ([`made_for`]).
const LEAST: u64 = 8 << 20;
/// Send memory is made by doubles from [`LEAST`] up to this, and past it in
/// steps of it ([`made_for`]).
const STEP: u64 = 64 << 20;
/// Size classes of send memory, by doubles from [`LEAST`]: 8 MiB up to
/// 2 GiB, and the last for every one larger.
const CLASSES: usize = 10;
/// The class of `size` bytes of send memory: one not a double (made at its
/// own size) is of the double below it.
/// The class of `size` bytes of send memory: one not a double (made in
/// [`STEP`]s) is of the double below it.
fn class(size: u64) -> usize {
((size.max(LEAST).ilog2() - LEAST.ilog2()) as usize).min(CLASSES - 1)
}
/// Of each class of send memory: how many submissions have now, and the most
/// they had at once since the sweep before ([`InUse::roll`]).
/// What submissions have of send memory now, and the most they had at once
/// since the sweep before ([`InUse::roll`]): of each class, how many; and
/// the bytes their requests needed, at the sizes made for them.
struct InUse {
now: [AtomicU32; CLASSES],
most: [AtomicU32; CLASSES],
need_now: std::sync::atomic::AtomicU64,
need_most: std::sync::atomic::AtomicU64,
}
impl InUse {
const fn new() -> Self {
Self { now: [const { AtomicU32::new(0) }; CLASSES], most: [const { AtomicU32::new(0) }; CLASSES] }
Self {
now: [const { AtomicU32::new(0) }; CLASSES],
most: [const { AtomicU32::new(0) }; CLASSES],
need_now: std::sync::atomic::AtomicU64::new(0),
need_most: std::sync::atomic::AtomicU64::new(0),
}
}
/// A submission took memory of `size` bytes.
fn lend(&self, size: u64) {
/// A submission took memory of `size` bytes for a request that needed
/// `need` ([`made_for`]; less than `size` when a bigger spare was free).
fn lend(&self, size: u64, need: u64) {
let class = class(size);
let now = self.now[class].fetch_add(1, Ordering::Relaxed) + 1;
self.most[class].fetch_max(now, Ordering::Relaxed);
let need = self.need_now.fetch_add(need, Ordering::Relaxed) + need;
self.need_most.fetch_max(need, Ordering::Relaxed);
}
/// Memory of `size` bytes came back (or was let go).
fn back(&self, size: u64) {
/// Memory [`InUse::lend`] counted came back (or was let go).
fn back(&self, size: u64, need: u64) {
let _ = self.now[class(size)].fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_sub(1));
let _ = self.need_now.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |n| Some(n.saturating_sub(need)));
}
/// The most in use at once of each class since the last call; the next
/// count starts from those in use now.
fn roll(&self) -> [u32; CLASSES] {
std::array::from_fn(|class| self.most[class].swap(self.now[class].load(Ordering::Relaxed), Ordering::Relaxed))
/// The most in use at once since the last call; the next count starts
/// from what is in use now.
fn roll(&self) -> Sweep {
Sweep {
most: std::array::from_fn(|class| self.most[class].swap(self.now[class].load(Ordering::Relaxed), Ordering::Relaxed)),
need: self.need_most.swap(self.need_now.load(Ordering::Relaxed), Ordering::Relaxed),
}
}
}
static IN_USE: InUse = InUse::new();
/// Of each class, the most submissions had in use at once in each of the
/// last sweeps ([`tick`]), the newest last.
static SEEN: Mutex<std::collections::VecDeque<[u32; CLASSES]>> = Mutex::new(std::collections::VecDeque::new());
/// One sweep's [`InUse::roll`]: of each class the most in use at once, and
/// the most bytes needed at once.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Sweep {
most: [u32; CLASSES],
need: u64,
}
/// The last sweeps ([`tick`]), the newest last.
static SEEN: Mutex<std::collections::VecDeque<Sweep>> = Mutex::new(std::collections::VecDeque::new());
/// For how many sweeps (once a second) send memory in use is remembered
/// (`VICTORY_SEND_KEEP_SECONDS`, 10 by default): under pressure the spares
/// keep, of each class, as many as were in use at once in that time
/// ([`trim`]). `0`: none is, and they go down to the hot floor, as before.
/// keep, of each class, as many as were in use at once in that time, within
/// [`trim_line`]'s bytes. `0`: none is, and they go down to the hot floor,
/// as before.
///
/// Ten is a choice, not a measurement: long enough that the busiest frames
/// of a walk or a drive, which do not come every second, find their memory
@ -248,7 +276,8 @@ static CHURN: [std::sync::atomic::AtomicU64; 4] = [const { std::sync::atomic::At
/// since the sweep before is remembered for [`trim`]; what was made and let
/// go meanwhile is said.
pub(super) fn tick() {
remember(&mut SEEN.lock().unwrap_or_else(|e| e.into_inner()), IN_USE.roll(), keep_sweeps());
let sweep = IN_USE.roll();
remember(&mut SEEN.lock().unwrap_or_else(|e| e.into_inner()), sweep, keep_sweeps());
let [made, made_bytes, gone, gone_bytes] = std::array::from_fn(|i| CHURN[i].swap(0, Ordering::Relaxed));
if made + gone > 0 {
let spare: u64 = SPARE.lock().unwrap_or_else(|e| e.into_inner()).iter().map(|m| m.size).sum();
@ -258,28 +287,40 @@ pub(super) fn tick() {
let_go = gone,
let_go_mib = gone_bytes >> 20,
spare_mib = spare >> 20,
needed_at_once_mib = sweep.need >> 20,
"submission send memory since the last sweep (VICTORY_SEND_OWN)"
);
}
}
/// [`tick`]'s window: `most` joins `seen`, which keeps the last `sweeps`.
fn remember(seen: &mut std::collections::VecDeque<[u32; CLASSES]>, most: [u32; CLASSES], sweeps: usize) {
seen.push_back(most);
/// [`tick`]'s window: `sweep` joins `seen`, which keeps the last `sweeps`.
fn remember(seen: &mut std::collections::VecDeque<Sweep>, sweep: Sweep, sweeps: usize) {
seen.push_back(sweep);
while seen.len() > sweeps {
seen.pop_front();
}
}
/// Of each class, the most in use at once in any sweep of `seen`.
fn wanted(seen: &std::collections::VecDeque<[u32; CLASSES]>) -> [u32; CLASSES] {
seen.iter().fold([0; CLASSES], |most, sweep| std::array::from_fn(|class| most[class].max(sweep[class])))
fn wanted(seen: &std::collections::VecDeque<Sweep>) -> [u32; CLASSES] {
seen.iter().fold([0; CLASSES], |most, sweep| std::array::from_fn(|class| most[class].max(sweep.most[class])))
}
/// The bytes of spares that stay under pressure whatever was in use lately,
/// the smallest ([`staying`]): 158d3177's hot floor.
const HOT_FLOOR: u64 = 16 << 20;
/// Free RAM from which the whole of [`kept`] may stay for what the window
/// needed ([`room`]); the copy sources' spares keep theirs from the same
/// line (`keeps_under_pressure` in `host.rs`).
const ROOMY: u64 = 8 << 30;
/// What may stay, beyond what the last sweep needed, under pressure: `cap`
/// (what [`kept`] allows) by the share of [`ROOMY`] that is `free`.
fn room(cap: u64, free: u64) -> u64 {
(u128::from(cap) * u128::from(free.min(ROOMY)) / u128::from(ROOMY)) as u64
}
/// At most this much is kept (of the card's memory, with [`on_card`]); a
/// fraction of it on a small host: these sit in L0, and with under 24 GiB of
/// RAM the L0 caches of ours add up past the 8 GiB Windows gives the card
@ -294,11 +335,11 @@ fn kept() -> u64 {
}
/// Under host memory pressure the spares go down to those submissions had in
/// use lately (of each class, the most at once in the last
/// [`keep_sweeps`] sweeps) or to a hot floor of the smallest, whichever
/// keeps more; all of them go when commit is critical (as the copy sources'
/// spares do, `spare_trim_to` in `host.rs`). The sweep calls this with the
/// others, and those let go are released on a thread of their own
/// use lately (of each class, the most at once in the last [`keep_sweeps`]
/// sweeps) within [`trim_line`]'s bytes, or to a hot floor of the smallest,
/// whichever keeps more; all of them go when commit is critical (as the copy
/// sources' spares do, `spare_trim_to` in `host.rs`). The sweep calls this
/// with the others, and those let go are released on a thread of their own
/// ([`let_go`]).
///
/// Down to the hot floor alone (158d3177), a 32 GB PC lost them every
@ -310,46 +351,77 @@ fn kept() -> u64 {
/// buffers counted there, and pages came in 2-4 times as often. What was in
/// use lately is made again within frames when it goes, so letting it go
/// saves nothing at the frame's peak: what goes now is what no frame of the
/// window used, as a scene's leftovers, and on a small host the spares stay
/// within [`kept`] as before.
/// window used, as a scene's leftovers, and what a burst made past what the
/// free RAM allows; on a small host the spares stay within [`kept`] as
/// before.
pub(super) fn trim() {
let (wanted, floor) = trim_line(crate::residency::critical_commit(), &SEEN.lock().unwrap_or_else(|e| e.into_inner()));
let critical = crate::residency::critical_commit();
let free = if critical { 0 } else { crate::residency::physical_free() };
let line = trim_line(critical, &SEEN.lock().unwrap_or_else(|e| e.into_inner()), free, kept());
let going: Vec<Memory> = {
let mut spare = SPARE.lock().unwrap_or_else(|e| e.into_inner());
let sizes: Vec<u64> = spare.iter().map(|m| m.size).collect();
let (stay, going): (Vec<_>, Vec<_>) =
std::mem::take(&mut *spare).into_iter().zip(staying(&sizes, &wanted, floor)).partition(|(_, stays)| *stays);
std::mem::take(&mut *spare).into_iter().zip(staying(&sizes, &line)).partition(|(_, stays)| *stays);
*spare = stay.into_iter().map(|(m, _)| m).collect();
going.into_iter().map(|(m, _)| m).collect()
};
let_go(going);
}
/// What a trim keeps: of each class, how many, and the bytes of the hot
/// floor; nothing when commit is `critical`.
fn trim_line(critical: bool, seen: &std::collections::VecDeque<[u32; CLASSES]>) -> ([u32; CLASSES], u64) {
if critical { ([0; CLASSES], 0) } else { (wanted(seen), HOT_FLOOR) }
/// What a trim keeps ([`staying`]).
#[derive(Debug, PartialEq)]
struct Line {
/// Of each class, at most this many.
wanted: [u32; CLASSES],
/// Those, the smallest first, while all that stays is within these bytes.
most: u64,
/// Then the smallest others while all that stays is within these.
floor: u64,
}
/// [`trim`]'s line from the last sweeps `seen`, with `free` bytes of RAM
/// free and [`kept`]'s `cap`; nothing at all when commit is `critical`.
///
/// The bytes are what the last sweep's frames needed at once (each request at
/// the size made for it, so not a big spare a small request happened to
/// take), which the next frames make again if it goes; and of what the
/// window's busiest moment needed beyond that, as much as [`room`] allows.
/// That is the busiest frame's sends twice over with two frames in the air
/// (`VICTORY_RELAY`), and counts the frames in the air whatever their number.
/// A burst's buffers (at world load, or a scene's first frames: one sweep
/// made 640 MiB at once in a walk, which then stayed) are not kept for the
/// window with little RAM free.
fn trim_line(critical: bool, seen: &std::collections::VecDeque<Sweep>, free: u64, cap: u64) -> Line {
if critical {
return Line { wanted: [0; CLASSES], most: 0, floor: 0 };
}
let last = seen.back().map_or(0, |sweep| sweep.need);
let busiest = seen.iter().map(|sweep| sweep.need).max().unwrap_or(0);
Line { wanted: wanted(seen), most: last.max(busiest.min(room(cap, free))).max(HOT_FLOOR), floor: HOT_FLOOR }
}
/// [`trim`]'s rule over the spares' `sizes`: which stay. Of each class the
/// `wanted` many, the biggest first; then the smallest of the others while
/// all that stays is within `floor` bytes.
fn staying(sizes: &[u64], wanted: &[u32; CLASSES], floor: u64) -> Vec<bool> {
/// `wanted` many, the smallest classes first and the biggest of a class
/// first, while all that stays is within `most` bytes; then the smallest of
/// the others while it is within `floor`.
fn staying(sizes: &[u64], line: &Line) -> Vec<bool> {
let mut stay = vec![false; sizes.len()];
let mut order: Vec<usize> = (0..sizes.len()).collect();
order.sort_by_key(|&i| std::cmp::Reverse(sizes[i]));
let mut left = *wanted;
order.sort_by_key(|&i| (class(sizes[i]), std::cmp::Reverse(sizes[i])));
let mut left = line.wanted;
let mut kept = 0u64;
for &i in &order {
let class = class(sizes[i]);
if left[class] > 0 {
if left[class] > 0 && kept + sizes[i] <= line.most {
left[class] -= 1;
stay[i] = true;
kept += sizes[i];
}
}
for &i in order.iter().rev() {
if !stay[i] && kept + sizes[i] <= floor {
order.sort_by_key(|&i| sizes[i]);
for &i in &order {
if !stay[i] && kept + sizes[i] <= line.floor {
stay[i] = true;
kept += sizes[i];
}
@ -420,14 +492,25 @@ fn fit(sizes: impl Iterator<Item = u64>, size: u64) -> Option<usize> {
sizes.enumerate().filter(|&(_, s)| s >= size).min_by_key(|&(_, s)| s).map(|(i, _)| i)
}
/// The size send memory for `size` bytes is made at: by doubles, so that the
/// next of about this size finds it (what submissions send differs from
/// frame to frame).
/// The size send memory for `size` bytes is made at: by doubles up to
/// [`STEP`], so that the next of about this size finds it (what submissions
/// send differs from frame to frame), and past it in steps of it. A frame of
/// a walk sends ~46 MB in all, but one sweep of a walk made 640 MiB in two
/// buffers (by doubles those can only be 512 and 128 MiB), and by doubles a
/// request just past 256 MiB is made at 512. In steps, at most 64 MiB more
/// than asked for is made.
///
/// shortcut: a submission's sends stay in one buffer. Split over buffers of
/// a step each they would never take more than a step past what they need,
/// but [`Own::record`], its scatter pass and the copy queue's copy of
/// [`Own::source`] (`upq`) each address one buffer.
fn made_for(size: u64) -> u64 {
size.max(LEAST).next_power_of_two()
let size = size.max(LEAST);
if size <= STEP { size.next_power_of_two() } else { size.next_multiple_of(STEP) }
}
fn memory(host: &Host, size: u64) -> Option<Memory> {
let need = made_for(size);
let taken = {
let mut spare = SPARE.lock().unwrap_or_else(|e| e.into_inner());
fit(spare.iter().map(|m| m.size), size).map(|i| spare.swap_remove(i))
@ -435,7 +518,7 @@ fn memory(host: &Host, size: u64) -> Option<Memory> {
let mut memory = match taken {
Some(memory) => memory,
None => {
let size = made_for(size);
let size = need;
let mapped = if on_card() { host.mapped_card_buffer(size, false) } else { None };
let (buffer, data) = match mapped {
Some(made) => made,
@ -451,19 +534,20 @@ fn memory(host: &Host, size: u64) -> Option<Memory> {
buffer,
data: data as usize,
size,
lent: false,
lent: None,
}
}
};
IN_USE.lend(memory.size);
memory.lent = true;
IN_USE.lend(memory.size, need);
memory.lent = Some(need);
Some(memory)
}
/// The submission has run.
pub(super) fn give_back(mut memory: Memory) {
IN_USE.back(memory.size);
memory.lent = false;
if let Some(need) = memory.lent.take() {
IN_USE.back(memory.size, need);
}
let most = kept();
let mut going = Vec::new();
{
@ -880,14 +964,31 @@ mod tests {
use std::collections::VecDeque;
const MIB: u64 = 1 << 20;
const GIB: u64 = 1 << 30;
/// What [`kept`] allows on a large host.
const CAP: u64 = 768 * MIB;
/// The spares' sizes left by a trim, as [`trim`] decides it.
fn trimmed(spare: Vec<u64>, critical: bool, seen: &VecDeque<[u32; CLASSES]>) -> Vec<u64> {
let (wanted, floor) = trim_line(critical, seen);
let stay = staying(&spare, &wanted, floor);
/// A line for `wanted` within `most` bytes.
fn line(wanted: [u32; CLASSES], most: u64) -> Line {
Line { wanted, most, floor: HOT_FLOOR }
}
/// The spares' sizes `line` leaves, as [`trim`] decides it.
fn trimmed(spare: Vec<u64>, line: &Line) -> Vec<u64> {
let stay = staying(&spare, line);
spare.into_iter().zip(stay).filter(|&(_, stays)| stays).map(|(size, _)| size).collect()
}
/// A sweep that needed `need` bytes at once, and of the classes of `most`
/// as many.
fn sweep(need: u64, most: &[(u64, u32)]) -> Sweep {
let mut sweep = Sweep { need, ..Sweep::default() };
for &(size, n) in most {
sweep.most[class(size)] = n;
}
sweep
}
#[test]
fn send_memory_classes_are_doubles_from_8_mib() {
assert_eq!(class(0), 0);
@ -895,57 +996,68 @@ mod tests {
assert_eq!(class(16 * MIB - 1), 0);
assert_eq!(class(16 * MIB), 1);
assert_eq!(class(64 * MIB), 3);
// One made at its own size is of the double below it.
assert_eq!(class(600 * MIB), class(512 * MIB));
assert_eq!(class(2 << 30), CLASSES - 2);
assert_eq!(class(4 << 30), CLASSES - 1);
// One made in steps is of the double below it.
assert_eq!(class(320 * MIB), class(256 * MIB));
assert_eq!(class(2 * GIB), CLASSES - 2);
assert_eq!(class(4 * GIB), CLASSES - 1);
assert_eq!(class(u64::MAX), CLASSES - 1);
// What `memory` makes, and which spare it takes.
assert_eq!(made_for(1), LEAST);
assert_eq!(made_for(30 * MIB), 32 * MIB);
// Which spare `memory` takes.
assert_eq!(fit([64 * MIB, 16 * MIB, 32 * MIB].into_iter(), 12 * MIB), Some(1));
assert_eq!(fit([8 * MIB].into_iter(), 12 * MIB), None);
}
#[test]
fn send_memory_is_made_by_doubles_to_64_mib_and_in_64_mib_steps_past_it() {
assert_eq!(made_for(1), LEAST);
assert_eq!(made_for(30 * MIB), 32 * MIB);
assert_eq!(made_for(STEP), STEP);
assert_eq!(made_for(STEP + 1), 2 * STEP);
// By doubles these were 512 MiB.
assert_eq!(made_for(257 * MIB), 320 * MIB);
assert_eq!(made_for(300 * MIB), 320 * MIB);
assert_eq!(made_for(100 * MIB), 128 * MIB);
// From the step on, less than a step more than asked for.
for size in [65 * MIB, 200 * MIB + 1, 513 * MIB, 2 * GIB + 1] {
assert!(made_for(size) >= size && made_for(size) - size < STEP, "{size}");
}
// Past 2 GiB not at 4 GiB, which one upload buffer may not be.
assert!(made_for(2 * GIB + 1) < 4 * GIB);
}
#[test]
fn in_use_counts_the_most_at_once_between_sweeps() {
let used = InUse::new();
used.lend(32 * MIB);
used.lend(32 * MIB);
used.lend(8 * MIB);
used.back(32 * MIB);
used.back(32 * MIB);
used.lend(32 * MIB);
let most = used.roll();
assert_eq!((most[class(32 * MIB)], most[class(8 * MIB)]), (2, 1));
used.lend(32 * MIB, 32 * MIB);
used.lend(32 * MIB, 32 * MIB);
// A small request in a big spare: of the spare's class, its own need.
used.lend(128 * MIB, 8 * MIB);
used.back(32 * MIB, 32 * MIB);
used.back(32 * MIB, 32 * MIB);
used.lend(32 * MIB, 32 * MIB);
let s = used.roll();
assert_eq!((s.most[class(32 * MIB)], s.most[class(128 * MIB)], s.need), (2, 1, 72 * MIB));
// Still in use at the sweep: counted in the next one too.
let most = used.roll();
assert_eq!((most[class(32 * MIB)], most[class(8 * MIB)]), (1, 1));
used.back(32 * MIB);
used.back(8 * MIB);
let s = used.roll();
assert_eq!((s.most[class(32 * MIB)], s.most[class(128 * MIB)], s.need), (1, 1, 40 * MIB));
used.back(32 * MIB, 32 * MIB);
used.back(128 * MIB, 8 * MIB);
// More given back than lent does not wrap.
used.back(8 * MIB);
let mut last = [0; CLASSES];
last[class(32 * MIB)] = 1;
last[class(8 * MIB)] = 1;
assert_eq!(used.roll(), last);
assert_eq!(used.roll(), [0; CLASSES]);
used.back(128 * MIB, 8 * MIB);
assert_eq!(used.roll(), sweep(40 * MIB, &[(32 * MIB, 1), (128 * MIB, 1)]));
assert_eq!(used.roll(), Sweep::default());
}
#[test]
fn the_window_wants_the_most_of_each_class_in_its_sweeps() {
let mut seen = VecDeque::new();
let (mut a, mut b) = ([0; CLASSES], [0; CLASSES]);
a[0] = 3;
a[2] = 1;
b[0] = 1;
b[3] = 2;
let a = sweep(0, &[(8 * MIB, 3), (32 * MIB, 1)]);
let b = sweep(0, &[(8 * MIB, 1), (64 * MIB, 2)]);
remember(&mut seen, a, 2);
remember(&mut seen, b, 2);
let w = wanted(&seen);
assert_eq!((w[0], w[2], w[3]), (3, 1, 2));
// The oldest sweep falls out.
remember(&mut seen, [0; CLASSES], 2);
remember(&mut seen, Sweep::default(), 2);
let w = wanted(&seen);
assert_eq!((w[0], w[2], w[3]), (1, 0, 2));
// No window (`VICTORY_SEND_KEEP_SECONDS=0`): nothing is wanted.
@ -955,6 +1067,35 @@ mod tests {
assert_eq!(wanted(&VecDeque::new()), [0; CLASSES]);
}
#[test]
fn the_trim_line_keeps_the_last_sweeps_need_and_of_the_windows_what_free_ram_allows() {
assert_eq!(room(CAP, 8 * GIB), CAP);
assert_eq!(room(CAP, 32 * GIB), CAP);
assert_eq!(room(CAP, 2 * GIB), 192 * MIB);
assert_eq!(room(CAP, 0), 0);
let steady = sweep(112 * MIB, &[]);
let burst = sweep(560 * MIB, &[]);
let seen = |sweeps: &[Sweep]| sweeps.iter().copied().collect::<VecDeque<_>>();
// Steady: what the last sweep needed stays, whatever RAM is free (it
// is made again at once if it goes).
for free in [0, 2 * GIB, 16 * GIB] {
assert_eq!(trim_line(false, &seen(&[steady, steady]), free, CAP).most, 112 * MIB);
}
// A burst in the window: as much of it as the free RAM allows.
let window = seen(&[steady, burst, steady]);
assert_eq!(trim_line(false, &window, 16 * GIB, CAP).most, 560 * MIB);
assert_eq!(trim_line(false, &window, 2 * GIB, CAP).most, 192 * MIB);
assert_eq!(trim_line(false, &window, 0, CAP).most, 112 * MIB);
// A small host's cap is scaled the same.
assert_eq!(trim_line(false, &window, 2 * GIB, 128 * MIB).most, 112 * MIB);
// The burst was the last sweep: it stays for that one.
assert_eq!(trim_line(false, &seen(&[steady, burst]), 0, CAP).most, 560 * MIB);
// Nothing seen: the hot floor.
assert_eq!(trim_line(false, &VecDeque::new(), 16 * GIB, CAP), line([0; CLASSES], HOT_FLOOR));
// Critical commit: nothing.
assert_eq!(trim_line(true, &window, 16 * GIB, CAP), Line { wanted: [0; CLASSES], most: 0, floor: 0 });
}
#[test]
fn a_trim_keeps_what_was_in_use_lately_or_the_hot_floor() {
let mut wanted = [0; CLASSES];
@ -963,20 +1104,35 @@ mod tests {
wanted[class(8 * MIB)] = 1;
// One of each wanted (104 MiB, past the floor), and nothing more.
let sizes = [32 * MIB, 64 * MIB, 8 * MIB, 32 * MIB, 16 * MIB, 8 * MIB];
assert_eq!(staying(&sizes, &wanted, HOT_FLOOR), [true, true, true, false, false, false]);
assert_eq!(staying(&sizes, &line(wanted, CAP)), [true, true, true, false, false, false]);
// What was wanted is under the floor: the smallest others fill it.
let mut small = [0; CLASSES];
small[class(8 * MIB)] = 1;
assert_eq!(staying(&[16 * MIB, 8 * MIB, 8 * MIB], &small, HOT_FLOOR), [false, true, true]);
assert_eq!(staying(&[16 * MIB, 8 * MIB, 8 * MIB], &line(small, CAP)), [false, true, true]);
// More wanted than there are: all stay.
let mut many = [0; CLASSES];
many[class(32 * MIB)] = 5;
assert_eq!(staying(&[32 * MIB, 32 * MIB], &many, 0), [true, true]);
// Of one class the biggest stay first (one made at its own size is
// of the class below it).
assert_eq!(staying(&[32 * MIB, 32 * MIB], &line(many, CAP)), [true, true]);
// Of one class the biggest stays first (one made in steps is of the
// class below it).
let mut one = [0; CLASSES];
one[class(512 * MIB)] = 1;
assert_eq!(staying(&[512 * MIB, 600 * MIB], &one, 0), [false, true]);
one[class(256 * MIB)] = 1;
assert_eq!(staying(&[256 * MIB, 320 * MIB], &line(one, CAP)), [false, true]);
}
#[test]
fn past_its_bytes_a_trim_lets_the_biggest_go_though_they_were_in_use() {
// A burst's 320 and 128 MiB, in use in the window (as when a small
// request took one while the smaller spares were out), and a walk's
// set.
let sizes = [320 * MIB, 128 * MIB, 32 * MIB, 16 * MIB, 8 * MIB];
let mut wanted = [0; CLASSES];
for &size in &sizes {
wanted[class(size)] = 1;
}
assert_eq!(staying(&sizes, &line(wanted, 112 * MIB)), [false, false, true, true, true]);
assert_eq!(staying(&sizes, &line(wanted, 192 * MIB)), [false, true, true, true, true]);
assert_eq!(staying(&sizes, &line(wanted, CAP)), [true; 5]);
}
#[test]
@ -989,9 +1145,9 @@ mod tests {
}
spare
}
let seen = VecDeque::new();
let nothing = trim_line(false, &VecDeque::new(), 16 * GIB, CAP);
for spare in [vec![64 * MIB, 32 * MIB, 16 * MIB, 8 * MIB], vec![8 * MIB; 5], vec![16 * MIB, 8 * MIB], vec![32 * MIB], vec![]] {
let mut now = trimmed(spare.clone(), false, &seen);
let mut now = trimmed(spare.clone(), &nothing);
now.sort_by_key(|&s| std::cmp::Reverse(s));
assert_eq!(now, before(spare, HOT_FLOOR));
}
@ -999,78 +1155,102 @@ mod tests {
#[test]
fn critical_commit_lets_every_spare_go() {
let mut seen = VecDeque::new();
let mut most = [0; CLASSES];
most[class(32 * MIB)] = 4;
remember(&mut seen, most, 10);
assert_eq!(trim_line(true, &seen), ([0; CLASSES], 0));
assert!(trimmed(vec![32 * MIB, 8 * MIB], true, &seen).is_empty());
let seen: VecDeque<_> = [sweep(64 * MIB, &[(32 * MIB, 4)])].into_iter().collect();
assert!(trimmed(vec![32 * MIB, 8 * MIB], &trim_line(true, &seen, 16 * GIB, CAP)).is_empty());
// Not critical: the one in use lately stays (the 8 MiB one is past
// the floor with it).
assert_eq!(trimmed(vec![32 * MIB, 8 * MIB], false, &seen), [32 * MIB]);
assert_eq!(trimmed(vec![32 * MIB, 8 * MIB], &trim_line(false, &seen, 16 * GIB, CAP)), [32 * MIB]);
}
/// A frame's sends of ~46 MB in three submissions in the air at once (30,
/// 12 and 4 MB), 20 frames a second, under pressure that never clears and
/// a trim at each sweep, as `memory` and `give_back` take and give: the
/// send memories made in each second.
fn made_each_second(sweeps: usize) -> Vec<usize> {
/// Seven seconds of a walk under pressure that never clears, as
/// `memory`, `give_back` and the sweep's `tick` and `trim` do it: 20
/// frames a second, each sending ~46 MB in three submissions (30, 12 and
/// 4 MB), its memory given back once `in_air` frames have taken theirs;
/// the first frame of second 1 also sends `burst`. For each second: the
/// send memories made, and the MiB of spares after its sweep.
fn walk(sweeps: usize, in_air: usize, free: u64, burst: &[u64]) -> (Vec<usize>, Vec<u64>) {
let used = InUse::new();
let mut seen = VecDeque::new();
let mut spare: Vec<u64> = Vec::new();
let mut made = Vec::new();
for _second in 0..5 {
let mut air: VecDeque<Vec<(u64, u64)>> = VecDeque::new();
let (mut made, mut kept) = (Vec::new(), Vec::new());
for second in 0..7 {
remember(&mut seen, used.roll(), sweeps);
spare = trimmed(spare, false, &seen);
spare = trimmed(spare, &trim_line(false, &seen, free, CAP));
kept.push(spare.iter().sum::<u64>() / MIB);
let mut n = 0;
for _frame in 0..20 {
let mut out = Vec::new();
for size in [30 * MIB, 12 * MIB, 4 * MIB] {
for frame in 0..20 {
let mut sends = vec![30 * MIB, 12 * MIB, 4 * MIB];
if second == 1 && frame == 0 {
sends.extend_from_slice(burst);
}
let mut taken = Vec::new();
for size in sends {
let need = made_for(size);
let memory = match fit(spare.iter().copied(), size) {
Some(i) => spare.swap_remove(i),
None => {
n += 1;
made_for(size)
need
}
};
used.lend(memory);
out.push(memory);
used.lend(memory, need);
taken.push((memory, need));
}
for memory in out {
used.back(memory);
spare.push(memory);
air.push_back(taken);
while air.len() >= in_air {
for (memory, need) in air.pop_front().unwrap_or_default() {
used.back(memory, need);
spare.push(memory);
}
}
}
made.push(n);
}
made
(made, kept)
}
#[test]
fn under_pressure_that_never_clears_a_steady_frame_makes_nothing_again() {
assert_eq!(made_each_second(10), [3, 0, 0, 0, 0]);
assert_eq!(walk(10, 1, 16 * GIB, &[]).0, [3, 0, 0, 0, 0, 0, 0]);
// Two frames in the air: twice as many, then nothing, with RAM free
// or none.
for free in [16 * GIB, 0] {
assert_eq!(walk(10, 2, free, &[]), (vec![6, 0, 0, 0, 0, 0, 0], vec![0, 56, 56, 56, 56, 56, 56]));
}
// `VICTORY_SEND_KEEP_SECONDS=0`, the hot floor alone (158d3177): the
// 32 and 16 MiB ones are made again every second.
assert_eq!(made_each_second(0), [3, 2, 2, 2, 2]);
assert_eq!(walk(0, 1, 16 * GIB, &[]).0, [3, 2, 2, 2, 2, 2, 2]);
assert_eq!(walk(0, 2, 16 * GIB, &[]).0, [6, 2, 2, 2, 2, 2, 2]);
}
#[test]
fn a_bursts_buffers_stay_only_as_free_ram_allows_and_go_with_the_window() {
// 300 and 100 MB more in one frame: 320 and 128 MiB made (512 and 128
// by doubles), kept the sweep after, then within what the free RAM
// allows, and gone once the window (3 sweeps here) has passed.
let burst = [300 * MIB, 100 * MIB];
assert_eq!(walk(3, 2, 16 * GIB, &burst), (vec![6, 2, 0, 0, 0, 0, 0], vec![0, 56, 504, 504, 504, 56, 56]));
assert_eq!(walk(3, 2, 2 * GIB, &burst), (vec![6, 2, 0, 0, 0, 0, 0], vec![0, 56, 504, 184, 184, 56, 56]));
}
#[test]
fn what_a_scene_left_behind_goes_once_the_window_has_passed() {
let used = InUse::new();
let mut seen = VecDeque::new();
used.lend(64 * MIB);
used.lend(8 * MIB);
used.back(64 * MIB);
used.back(8 * MIB);
used.lend(64 * MIB, 64 * MIB);
used.lend(8 * MIB, 8 * MIB);
used.back(64 * MIB, 64 * MIB);
used.back(8 * MIB, 8 * MIB);
let mut spare = vec![64 * MIB, 8 * MIB];
let mut left = Vec::new();
for _sweep in 0..4 {
remember(&mut seen, used.roll(), 2);
spare = trimmed(spare, false, &seen);
spare = trimmed(spare, &trim_line(false, &seen, 16 * GIB, CAP));
left.push(spare.len());
// Only small sends from here.
used.lend(8 * MIB);
used.back(8 * MIB);
used.lend(8 * MIB, 8 * MIB);
used.back(8 * MIB, 8 * MIB);
}
assert_eq!(left, [2, 2, 1, 1]);
}

View File

@ -11,10 +11,16 @@
went from 3.4 to 6.6 ms a frame, with the fresh buffers counted there, and page-ins were
2-4 times as many. Under pressure the spares now keep, of each size, as many as were in
use at once in the last 10 sweeps (`VICTORY_SEND_KEEP_SECONDS`; `0` is the floor alone,
as before), or the hot floor if that keeps more; all go at critical commit as before, and
the small host's cap (`kept`) is unchanged. Those let go are released on a thread of their
own (`victory-send-free`), not on the submitting thread, and a line at the sweep says what
was made and let go when anything was.
as before), within a line of bytes: what the last sweep's frames needed at once (which
the next frames make again if it goes), and of what the window's busiest moment needed
beyond that only `kept` scaled by the share of 8 GiB of RAM that is free; or the hot floor
if that keeps more. A first cut without the line kept a burst's two buffers (640 MiB made
in one sweep of a walk) for the whole window on a 32 GB PC that paged heavily. Past
64 MiB send memory is made in 64 MiB steps, not by doubles (512 MiB for a request just
past 256). All go at critical commit as before, and the small host's cap (`kept`) is
unchanged. Those let go are released on a thread of their own (`victory-send-free`), not
on the submitting thread, and a line at the sweep says what was made and let go when
anything was.
## 2026-11-29, local time