From 45772fe26577ada6e425355d01c04f39448537b5 Mon Sep 17 00:00:00 2001 From: Kikoarl <7354010+Kikoarl@users.noreply.github.com> Date: Fri, 27 Nov 2026 22:48:10 +0100 Subject: [PATCH] memory: recycle a 512 MB budget of ray-tracing fault pages Port the recycled ray-tracing fault-page budget from fxceless/victory-fork commit 3e573d2556adc5a5b22ccd2d6d92231a72ee55bb (including the fault-miss breakdown from 52dfd12 carried in 3e573d2) into crates/victory-gpu/src/memory.rs while preserving PR #88 (30b979ee) in Memory::another. --- crates/victory-gpu/src/memory.rs | 469 ++++++++++++++++++++++++++++--- 1 file changed, 423 insertions(+), 46 deletions(-) diff --git a/crates/victory-gpu/src/memory.rs b/crates/victory-gpu/src/memory.rs index 78faa10f83..949c02dac6 100644 --- a/crates/victory-gpu/src/memory.rs +++ b/crates/victory-gpu/src/memory.rs @@ -15,7 +15,8 @@ //! //! - the **directory**: per 64 KiB page of title address space the number //! of the copy that holds the page (0: none) and the page's byte offset -//! in it; one for all lists, entries only ever go from empty to filled. +//! in it; one for all lists. An entry goes from empty to filled, and back +//! to empty when its page is given back. //! The CPU writes it in memory of its own and shaders read a copy of that //! on the card, brought up to date as title memory is (over the bus, every //! step of every ray waited for it); @@ -24,7 +25,9 @@ //! copy); //! - the **faults**: the page numbers shaders found empty, read back here //! once a frame, so the pages are there from the next frame on. A pointer a -//! shader takes out of memory cannot be known before it runs. +//! shader takes out of memory cannot be known before it runs. The programs +//! that walk leave theirs in the first half of the notes, the builder in +//! the second: what the builder found empty it may store in later. //! //! **A page that is not there when a shader touches it is a lost access**: //! the read gives 0, the store is dropped, and the fault note only helps @@ -62,11 +65,20 @@ //! A packed structure is gone when title memory has no header of it any more //! (looked at over [`SWEEP`] frames); the pages it alone has are given back. //! -//! shortcut: a page held for anything else is kept for the rest of the run. +//! **Pages from faults go round**: at most [`FAULT_MB`] of arena pages are +//! held because a shader found them empty. A present page leaves no note, so +//! whether such a page is still read is not known; at the limit the oldest +//! goes for the new one, and if it was still read its note comes back in the +//! next frame and it is made again from title memory (one frame of zeros +//! there; counted, see [`Memory::report`]). A page a list names, or a packed +//! structure is unpacked in, is no longer one of these: it may hold what is +//! on the card alone. A page whose title memory is gone goes at once. +//! +//! shortcut: a page a list named is kept for the rest of the run. //! shortcut: pages a list does not name are looked at again in turn, over //! [`SWEEP`] frames. -use std::collections::HashMap; +use std::collections::{HashMap, VecDeque}; use std::sync::{Arc, Mutex}; use victory_shader::emit::{MEMORY_FAULTS, MEMORY_PAGES, MEMORY_READ_ONLY}; @@ -101,11 +113,34 @@ const VIEW: u64 = 256 << 10; const BUILT: u64 = 4 << 20; /// The biggest title buffer whose builder results come back. const BACK: u64 = 256 << 10; -/// Pages taken from the faults, of one frame and in all, at most: a ray in a -/// structure that is not right goes anywhere, and 2,048 a frame took all the -/// pages there are in the first seconds (run light-4). +/// Pages taken from the faults in one frame, at most: a ray in a structure +/// that is not right goes anywhere, and 2,048 a frame took all the pages +/// there are in the first seconds (run light-4). The world's first two +/// frames leave 3,754 to 3,903 and 2,508 to 2,902 notes (runs 405, 406; the +/// notes had 4,095 places), 256 pages are made of them and the third frame +/// leaves 4 to 10: that burst ends by itself, and of its lowest pages more +/// than half were not the title's memory (157 of 285, 222 of 350). const FAULTS: usize = 128; -const FAULTS_ALL: usize = 2048; +/// Arena pages held from the faults, at most, in MiB. Not a count for the +/// run any more (2,048 pages until run 403, 8,192 in runs 405 and 406: used +/// up one to three minutes into the world, and from then on every page +/// touched read zeros for the rest of the run, 81 to 2,385 a frame): what +/// rays touch follows where the player goes, so at the limit the oldest such +/// page is given back for the new one ([`Memory::give_back`]). +/// 512 is the 8,192 pages both runs came to hold from faults, with the card +/// at 9,252 MB of a budget of 10,095 MB and the texture queue 478 MB under +/// its line (run 406, 15:28:03): no more of the card than those runs took. +/// Whether it is enough the report says: pages given back and wanted again. +const FAULT_MB: usize = 512; +const FAULT_PAGES: usize = (FAULT_MB << 20) / PAGE as usize; +/// A page from a fault is not given back for so many frames after it came. +/// When more new pages are wanted than that lets through (all of +/// [`FAULT_PAGES`] in under so many frames: rays that go anywhere), the new +/// ones wait, instead of the pages in use going round at [`FAULTS`] a frame. +const FAULT_YOUNG: u64 = 128; +/// A page given back and wanted again within so many frames was still read +/// (a submission lands up to two frames late). +const FAULT_SOON: u64 = 4; /// Frames over which every page is looked at once. const SWEEP: usize = 128; @@ -128,6 +163,11 @@ struct Held { /// The frame it was last looked at in, and the one it came in. looked: u64, born: u64, + /// Which of all holdings this is (a page given back may come again). + serial: u64, + /// Taken from a fault and named by no list since: 1 if the programs that + /// walk found it empty, 2 if the builder did (0: not so). + fault: u8, /// Packed structures that have bytes in it, and whether it is held for /// them alone. owners: u32, @@ -155,8 +195,9 @@ struct Memory { arenas: Vec<(usize, usize)>, /// By title page. pages: HashMap, - /// Their numbers in the order they came, and where the sweep is. - order: Vec, + /// Their numbers and serials in the order they came, and where the sweep + /// is. + order: Vec<(u64, u64)>, sweep: usize, /// Windows of title memory itself ([`Memory::map`]). windows: usize, @@ -169,8 +210,28 @@ struct Memory { free: Vec<(usize, usize)>, /// Copies of title buffers the builder may store in. open: std::collections::HashSet, - /// Pages taken from faults. + /// Pages taken from faults, in all. faulted: usize, + /// Holdings so far ([`Held::serial`]). + holdings: u64, + /// The arena pages held from faults (the walking programs', the + /// builder's) with their serials, in the order they came; how many of + /// them are still so. + fault_order: [VecDeque<(u64, u64)>; 2], + fault_held: [usize; 2], + /// Pages given back for others: the frame, and whether a shader was seen + /// to want the page again within [`FAULT_SOON`] frames. + given: HashMap, + /// A bit per page above 4 GiB a shader found empty, in all and since the + /// last report; how many are set. + wanted: [Vec; 2], + wanted_pages: [u64; 2], + /// Whether the card has room for another arena, once asked this frame. + room: Option, + /// No room was found for a page from a fault this frame (not looked for + /// again in it); the page last asked for got no place for want of room. + full: bool, + turned: bool, /// Per host view heap (by address): the first slot of its run, and how /// many copies have their view there. heaps: HashMap, @@ -203,6 +264,18 @@ struct Stats { /// view, a structure named in upload memory. came: [u64; 4], micros: u64, + /// Of the pages shaders found empty: made; left over the frame's limit, + /// without room, not the title's; below 4 GiB; left by the builder. + made: u64, + left: [u64; 3], + low: u64, + builders: u64, + /// Pages from faults given back for others, of them wanted again at + /// once; gone with the title's memory; named by a list since. + given: u64, + again: u64, + dead: u64, + adopted: u64, } // SAFETY: the mappings are of memory the struct owns; all access is under @@ -214,6 +287,7 @@ static MEMORY: Mutex> = Mutex::new(None); /// What a draw binds. pub(crate) struct Ready { pub(crate) directory: u64, + /// The half of the notes this draw's shader leaves its faults in. pub(crate) faults: u64, /// The view heap slot of the null view; copy `n`'s view is `n` further. pub(crate) base: u32, @@ -227,10 +301,12 @@ impl Memory { let entries = crate::upload::reserve(bytes as usize)?; let directory = crate::shadow::by_address(entries as u64, bytes)?; crate::host::name(&directory.buffer, || "title memory by address: directory".into()); - let (faults, data) = host.shared_buffer(u64::from(MEMORY_FAULTS) * 4)?; + // Twice the notes a shader knows of: the programs that walk leave + // theirs in the first half, the builder in the second ([`ready`]). + let (faults, data) = host.shared_buffer(u64::from(MEMORY_FAULTS) * 8)?; crate::host::name(&faults, || "title memory by address: faults".into()); // SAFETY: a fresh mapping of at least that many bytes. - unsafe { std::ptr::write_bytes(data, 0, MEMORY_FAULTS as usize * 4) }; + unsafe { std::ptr::write_bytes(data, 0, MEMORY_FAULTS as usize * 8) }; // SAFETY: a live buffer. let gpu = unsafe { faults.GetGPUVirtualAddress() }; tracing::info!("title memory by address: a shader reads or writes through raw addresses"); @@ -250,6 +326,15 @@ impl Memory { free: Vec::new(), open: Default::default(), faulted: 0, + holdings: 0, + fault_order: Default::default(), + fault_held: [0; 2], + given: HashMap::new(), + wanted: [vec![0; MEMORY_PAGES as usize / 64], vec![0; MEMORY_PAGES as usize / 64]], + wanted_pages: [0; 2], + room: None, + full: false, + turned: false, heaps: HashMap::new(), frame: u64::MAX, refused: Default::default(), @@ -318,10 +403,20 @@ impl Memory { Some(n) } - /// Gives `page` a place, if it has none and the memory is the title's; - /// whether it has one then. + /// Gives `page` a place for a list that names it, if it has none and the + /// memory is the title's; whether it has one then. fn map(&mut self, page: u64) -> bool { + self.map_for(page, None) + } + + /// [`Memory::map`]; `fault`: for a page a shader found empty (whether the + /// builder did), which takes an arena's place only within + /// [`Memory::fault_room`]. + fn map_for(&mut self, page: u64, fault: Option) -> bool { if self.copy_of(page).is_some() { + if fault.is_none() { + self.adopt(page); + } return true; } if !self.possible(page) { @@ -369,25 +464,121 @@ impl Memory { } return false; } + if fault.is_some() && (self.full || !self.fault_room()) { + (self.full, self.turned) = (true, true); + return false; + } if self.pages.len() >= MOST { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| tracing::warn!(MOST, "title memory by address: no more pages are taken; others read zeros")); return false; } - if let Some((copy, at)) = self.free.pop() { - self.hold(page, at, copy); - return true; - } - if !self.another() { + if self.free.is_empty() && !self.another() { return false; } let Some((copy, at)) = self.free.pop() else { return false; }; self.hold(page, at, copy); + if let Some(builds) = fault + && let Some(held) = self.pages.get_mut(&page) + { + let kind = usize::from(builds); + held.fault = kind as u8 + 1; + self.fault_order[kind].push_back((page, held.serial)); + self.fault_held[kind] += 1; + } true } + /// A list names `page`, or a packed structure is unpacked in it: if it + /// was held from a fault it is not given back any more (it may hold what + /// is on the card alone, as any page a list names). + fn adopt(&mut self, page: u64) { + if self.fault_held == [0, 0] { + return; + } + if let Some(held) = self.pages.get_mut(&page) + && held.fault != 0 + { + self.fault_held[usize::from(held.fault) - 1] -= 1; + held.fault = 0; + self.stats.adopted += 1; + } + } + + /// Whether the card has room for another arena, by the rule its copy is + /// made by ([`crate::shadow::by_address`]: under 97 hundredths of the + /// budget; past it the arena lies in the PC's memory and every step of + /// every ray crosses the bus). Asked once a frame. + fn card_room(&mut self) -> bool { + *self.room.get_or_insert_with(|| { + host().and_then(|h| h.video_memory()).is_none_or(|[budget, used, ..]| used + ARENA < budget / 100 * 97) + }) + } + + /// Room for one more page from a fault: under [`FAULT_PAGES`], while the + /// card has room for the arena it may come to take; else the place of + /// the oldest such page. + fn fault_room(&mut self) -> bool { + (self.fault_held[0] + self.fault_held[1] < FAULT_PAGES && self.card_room()) || self.give_back() + } + + /// Gives back the oldest page held from a fault, unless it is younger + /// than [`FAULT_YOUNG`] frames. Whether it is still read is not known (a + /// present page leaves no note): if it is, its note comes back with the + /// next frame and [`Memory::take_faults`] makes it again, first. + fn give_back(&mut self) -> bool { + let kind = goes_first(self.fault_held); + while let Some(&(page, serial)) = self.fault_order[kind].front() { + let Some(born) = self.pages.get(&page).filter(|h| h.serial == serial && h.fault != 0).map(|h| h.born) else { + // Named by a list since, or gone. + self.fault_order[kind].pop_front(); + continue; + }; + if self.frame.wrapping_sub(born) < FAULT_YOUNG { + return false; + } + self.fault_order[kind].pop_front(); + self.let_go(page); + self.given.insert(page, (self.frame, false)); + self.stats.given += 1; + return true; + } + false + } + + /// `page`, held from a fault, is given up: its place is free and shaders + /// find it empty again. + fn let_go(&mut self, page: u64) { + let Some(held) = self.pages.remove(&page) else { return }; + if held.fault != 0 { + self.fault_held[usize::from(held.fault) - 1] -= 1; + } + self.free.push((held.copy, held.at)); + // SAFETY: an entry of the directory, under the mutex. + unsafe { self.entry(page).write_volatile(0) }; + } + + /// The age in frames of the oldest page held from a fault. + fn oldest(&mut self) -> u64 { + let mut age = 0; + for kind in 0..2 { + while let Some(&(page, serial)) = self.fault_order[kind].front() { + match self.pages.get(&page) { + Some(h) if h.serial == serial && h.fault != 0 => { + age = age.max(self.frame.wrapping_sub(h.born)); + break; + } + _ => { + self.fault_order[kind].pop_front(); + } + } + } + } + age + } + /// Another arena's pages. /// /// After one that could not be made, no other is tried before @@ -439,8 +630,13 @@ impl Memory { /// `page` is held at `at` in the arena that is `copy`. fn hold(&mut self, page: u64, at: usize, copy: usize) { - self.pages.insert(page, Held { at, copy, taken: 0, looked: u64::MAX, born: self.frame, owners: 0, theirs: false }); - self.order.push(page); + self.holdings += 1; + let serial = self.holdings; + self.pages.insert( + page, + Held { at, copy, taken: 0, looked: u64::MAX, born: self.frame, serial, fault: 0, owners: 0, theirs: false }, + ); + self.order.push((page, serial)); fill(self.entry(page), copy, at as u64 - self.copies[copy].span().0); } @@ -474,8 +670,19 @@ impl Memory { fn look_again(&mut self) { for _ in 0..self.order.len().div_ceil(SWEEP) { self.sweep = (self.sweep + 1) % self.order.len(); - let page = self.order[self.sweep]; - if self.pages.contains_key(&page) { + let (page, serial) = self.order[self.sweep]; + let fault = self.pages.get(&page).filter(|h| h.serial == serial).map(|h| h.fault); + // The title's memory under a page from a fault is gone: so is + // what was read there. (A question to the system: put for a + // page every fourth time the sweep comes to it.) + let dead = fault.is_some_and(|f| f != 0) + && (self.frame / SWEEP as u64 + page).is_multiple_of(4) + && !crate::texture::committed((page * PAGE) as *const u8, PAGE as usize); + if dead { + self.let_go(page); + self.stats.dead += 1; + } + if fault.is_some() && !dead { self.bring(page, page + 1); } else { self.order.swap_remove(self.sweep); @@ -634,6 +841,8 @@ impl Memory { held.owners += 1; held.theirs |= new; } + // (Unpacked in place, on the card alone.) + self.adopt(page); } self.stats.packed_bytes += len; } @@ -690,48 +899,116 @@ impl Memory { } } - /// The pages shaders found empty since the last frame. + /// The pages shaders found empty since the last frame: those that are + /// the title's get a place, at most [`FAULTS`] a frame, and at most + /// [`FAULT_PAGES`] arena pages are held so. fn take_faults(&mut self) { - let mut pages = Vec::new(); - for i in 1..MEMORY_FAULTS as usize { + let mut raw = Vec::new(); + // (Dword 0 of each half is a sink.) + for i in (1..2 * MEMORY_FAULTS as usize).filter(|&i| i != MEMORY_FAULTS as usize) { // SAFETY: inside the faults' mapping; the GPU may be writing, // a number missed now is written again next frame. let at = unsafe { self.faults.1.add(i) }; // SAFETY: as above. let page = unsafe { at.read_volatile() }; if page != 0 { - pages.push(u64::from(page)); + raw.push((u64::from(page), i > MEMORY_FAULTS as usize)); // SAFETY: as above. unsafe { at.write_volatile(0) }; } } - if pages.is_empty() { + if raw.is_empty() { return; } - pages.sort_unstable(); - let mut made = 0; - for &page in &pages { - if made < FAULTS && self.faulted < FAULTS_ALL && self.copy_of(page).is_none() && self.map(page) { - made += 1; - self.faulted += 1; - self.bring(page, page + 1); + let mut pages = noted(raw); + let builders = pages.iter().filter(|p| p.1).count(); + // Below 4 GiB there is no title memory a shader reads by address: a + // pointer made of zeros, or of something that is no pointer. + let low = pages.partition_point(|p| p.0 < 0x1_0000); + let frame = self.frame; + let mut again = 0; + for &(page, _) in &pages[low..] { + for i in 0..2 { + self.wanted_pages[i] += u64::from(mark(&mut self.wanted[i], page)); + } + if let Some(given) = self.given.get_mut(&page) + && !given.1 + && frame.wrapping_sub(given.0) <= FAULT_SOON + { + given.1 = true; + again += 1; } } - self.stats.faults += pages.len() as u64; // Each is an access that read 0 or was dropped. static SAID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); let said = SAID.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - if said < 40 || said.is_multiple_of(100) { - let some: Vec = pages - .iter() - .step_by(pages.len().div_ceil(12)) - .map(|&p| format!("{:#x}", p * PAGE)) - .collect(); + let say = said < 40 || said.is_multiple_of(100); + // (Of those above 4 GiB: the others are counted, and tell nothing.) + let some: Vec = pages[low..] + .iter() + .step_by(pages[low..].len().div_ceil(12).max(1)) + .filter(|_| say) + .map(|&(p, _)| format!("{:#x}", p * PAGE)) + .collect(); + // Those given back and wanted again first: they are read. + if !self.given.is_empty() { + let given = &self.given; + pages.sort_by_cached_key(|p| !given.contains_key(&p.0)); + } + let (mut made, gave) = (0, self.stats.given); + // Why the others got none: the frame's limit; no room (the limit of + // pages from faults with its oldest too young to go, or the card's + // memory: only arena pages, a title buffer's copy takes none); not + // the title's memory; no place (a copy that cannot be numbered, no + // arena). + let mut left = [0u32; 4]; + for &(page, builds) in &pages { + if self.copy_of(page).is_some() { + continue; + } + if page < 0x1_0000 || page >= u64::from(MEMORY_PAGES) - 1 || self.refused.contains(&page) { + left[2] += 1; + } else if made >= FAULTS { + left[0] += 1; + } else if self.map_for(page, Some(builds)) { + made += 1; + self.faulted += 1; + self.given.remove(&page); + self.bring(page, page + 1); + } else if self.refused.contains(&page) { + left[2] += 1; + } else if std::mem::take(&mut self.turned) { + left[1] += 1; + } else { + left[3] += 1; + } + } + let s = &mut self.stats; + s.faults += pages.len() as u64; + s.made += made as u64; + for (sum, n) in s.left.iter_mut().zip(left) { + *sum += u64::from(n); + } + s.low += low as u64; + s.builders += builders as u64; + s.again += again; + if say { + let (given_back, oldest) = (self.stats.given - gave, self.oldest()); tracing::warn!( frame = self.frame, pages = pages.len(), made, held = self.pages.len(), + over_the_frame = left[0], + no_room = left[1], + not_the_titles = left[2], + no_place = left[3], + below_4_gib = low, + by_the_builder = builders, + from_faults = self.fault_held[0] + self.fault_held[1], + given_back, + wanted_again = again, + oldest, ?some, "title memory by address: shaders touched pages that had no copy (those accesses read 0 or were dropped); those that are the title's are there now" ); @@ -795,12 +1072,30 @@ impl Memory { return; } let frames = self.frame.wrapping_sub(frame).max(1) as f64; + let oldest = self.oldest(); + let wanted = self.wanted_pages; + self.wanted[1].fill(0); + self.wanted_pages[1] = 0; + // (What is not asked about any more.) + let now_frame = self.frame; + self.given.retain(|_, given| now_frame.wrapping_sub(given.0) < 2 * FAULT_YOUNG); + for kind in 0..2 { + if self.fault_order[kind].len() > 2 * self.fault_held[kind] + 4096 { + let pages = &self.pages; + self.fault_order[kind].retain(|&(p, serial)| pages.get(&p).is_some_and(|h| h.serial == serial && h.fault != 0)); + } + } let s = std::mem::take(&mut self.stats); tracing::info!( "title memory by address: {} pages ({} MB) in {} arenas, {} copies in all; per frame {:.0} dispatches that read, {:.0} that build, \ {:.1} pages that had no copy, {:.1} pages turned down, {:.1} results brought back; {:.0} pages looked at, {:.1} KB changed, \ {:.1} packed structures ({} there), {:.2} ms; {} windows; since the last line {} KB of packed structures came and {} of them went, \ - and pages for constants {}, views written {}, others {}, upload memory {}, faults in all {}", + and pages for constants {}, views written {}, others {}, upload memory {}, faults in all {}; \ + from faults {} pages held ({} MB of at most {}: the walking programs' {}, the builder's {}), the oldest {} frames old; \ + since the last line {} made, {} given back for others ({} of them wanted again within {} frames), \ + {} gone with the title's memory, {} named by a list since; per frame {:.1} not the title's ({:.1} of them below 4 GiB), \ + {:.1} over the frame's limit, {:.1} without room, {:.1} left by the builder; \ + pages wanted above 4 GiB: {} since the last line, {} in all", self.pages.len(), (self.pages.len() as u64 * PAGE) >> 20, self.arenas.len(), @@ -822,7 +1117,26 @@ impl Memory { s.came[1], s.came[2], s.came[3], - self.faulted + self.faulted, + self.fault_held[0] + self.fault_held[1], + ((self.fault_held[0] + self.fault_held[1]) as u64 * PAGE) >> 20, + FAULT_MB, + self.fault_held[0], + self.fault_held[1], + oldest, + s.made, + s.given, + s.again, + FAULT_SOON, + s.dead, + s.adopted, + s.left[2] as f64 / frames, + s.low as f64 / frames, + s.left[0] as f64 / frames, + s.left[1] as f64 / frames, + s.builders as f64 / frames, + wanted[1], + wanted[0] ); self.stats.from = Some((now, self.frame)); } @@ -877,6 +1191,38 @@ pub(crate) fn asked_sizes<'a>(lists: impl Iterator) -> bool asked } +/// The notes of a frame (the page, whether the builder left it) in page +/// order, a page both left once, as the builder's. +fn noted(mut raw: Vec<(u64, bool)>) -> Vec<(u64, bool)> { + raw.sort_unstable(); + raw.dedup_by(|later, first| { + let same = first.0 == later.0; + first.1 |= same && later.1; + same + }); + raw +} + +/// Whose page from a fault is given back first, of `held` (the walking +/// programs', the builder's): the walking programs', which read the title's +/// own bytes and get them again whole. What the builder found empty it may +/// have stored in since, and that is on the card alone: its pages go only +/// once they are more than half of [`FAULT_PAGES`], or all there are. +fn goes_first(held: [usize; 2]) -> usize { + usize::from(held[0] == 0 || held[1] > FAULT_PAGES / 2) +} + +/// Sets `page`'s bit; whether it was not set. +fn mark(bits: &mut [u64], page: u64) -> bool { + let Some(word) = bits.get_mut((page / 64) as usize) else { + return false; + }; + let bit = 1 << (page % 64); + let new = *word & bit == 0; + *word |= bit; + new +} + /// [`fill`] of a page stores by address do not reach. fn close(entry: *mut u32, n: usize, offset: u64) { fill(entry, n | MEMORY_READ_ONLY as usize, offset); @@ -947,6 +1293,7 @@ pub(crate) fn ready( if m.frame != frame { m.frame = frame; m.refused.clear(); + (m.room, m.full) = (None, false); m.take_faults(); m.look_again(); m.report(); @@ -1100,7 +1447,8 @@ pub(crate) fn ready( let directory = &m.directory.1; Some(Ready { directory: directory.gpu(directory.span().0), - faults: m.faults.2, + // (The shader's places are the same in either half.) + faults: m.faults.2 + if builds { u64::from(MEMORY_FAULTS) * 4 } else { 0 }, base, }) } @@ -1325,6 +1673,35 @@ mod tests { assert_eq!(ours, title); } + #[test] + fn a_frames_notes_come_once_a_page_and_the_builders_stay_the_builders() { + let notes = noted(vec![(0x2_0000, false), (0x10, true), (0x1_0005, true), (0x1_0005, false), (0x1_0004, false)]); + assert_eq!(notes, [(0x10, true), (0x1_0004, false), (0x1_0005, true), (0x2_0000, false)]); + // Those below 4 GiB are the first of them. + assert_eq!(notes.partition_point(|p| p.0 < 0x1_0000), 1); + } + + #[test] + fn the_walking_programs_pages_from_faults_go_first() { + assert_eq!(FAULT_PAGES, 8192); + assert_eq!(goes_first([100, 100]), 0); + assert_eq!(goes_first([1, FAULT_PAGES / 2]), 0); + // The builder's once they are more than half, or all there are. + assert_eq!(goes_first([100, FAULT_PAGES / 2 + 1]), 1); + assert_eq!(goes_first([0, 3]), 1); + } + + #[test] + fn a_page_wanted_is_counted_once() { + let mut bits = vec![0u64; 4]; + assert!(mark(&mut bits, 65)); + assert!(!mark(&mut bits, 65)); + assert!(mark(&mut bits, 64)); + assert_eq!(bits, [0, 3, 0, 0]); + // Past the directory: not counted. + assert!(!mark(&mut bits, 4 * 64)); + } + #[test] fn a_packed_structure_is_known_by_its_header() { let header = |version: u32, nodes: u32| u64::from(version) | u64::from(nodes) << 32;