Refreshes copy and send the 4 KiB parts of a page that were written (VICTORY_PAGE_PARTS=0: the 64 KiB page); the early asking is off
Where the kernel or a landing names them; what is checked stays a page. VICTORY_ASK_EARLY=<ms> turns the early asking on. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
f672130364
commit
3ead56e175
3 files changed
+200
-24
No files matched your search
@@ -8756,7 +8756,8 @@ mod refresh_tests {
|
||||
assert_eq!(look(true, "taken"), 0);
|
||||
// SAFETY: the test's own memory.
|
||||
unsafe { ((region + 600) as *mut u8).write_volatile(1) };
|
||||
assert_eq!(look(false, "written at its end"), PAGE - 500);
|
||||
// (The kernel names the 4 KiB, the guards only the block.)
|
||||
assert_eq!(look(false, "written at its end"), if view { PAGE } else { 4096 } - 500);
|
||||
assert_eq!(look(true, "taken again"), 0);
|
||||
// Outside the range, on its last page.
|
||||
// SAFETY: as above.
|
||||
|
||||
@@ -2388,6 +2388,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::*;
|
||||
@@ -3451,6 +3511,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() {
|
||||
@@ -3471,6 +3533,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
|
||||
{
|
||||
@@ -3486,6 +3551,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.
|
||||
|
||||
+132
-23
@@ -36,6 +36,58 @@ pub(crate) const PAGE: usize = 64 << 10;
|
||||
/// The kernel's page, the unit of its answers.
|
||||
const KERNEL_PAGE: usize = 4096;
|
||||
|
||||
/// The parts of a page.
|
||||
const PARTS: usize = PAGE / KERNEL_PAGE;
|
||||
/// All of them.
|
||||
const WHOLE: u16 = u16::MAX;
|
||||
|
||||
/// Whether the parts of a page that the kernel names are kept, and a refresh
|
||||
/// copies those (`VICTORY_PAGE_PARTS=0`: the 64 KiB page, as before).
|
||||
///
|
||||
/// The kernel says which 4 KiB pages were written, and a copy took the 64 KiB
|
||||
/// around each: 29 MB a frame written so were most of 114 MB copied, and as
|
||||
/// much sent up and hashed again on the card (session play-1125-0203, run
|
||||
/// cpu-2). What is checked is as before, a page.
|
||||
fn parts_on() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_PAGE_PARTS").is_none_or(|v| v != "0"))
|
||||
}
|
||||
|
||||
impl Journal {
|
||||
/// Page `page` was found written by asking `now`: the parts in `parts`
|
||||
/// (a bit each). The asking that found it written before (0: none).
|
||||
fn note(&mut self, page: usize, parts: u16, now: u64) -> u64 {
|
||||
let before = self.written.insert(page, now).unwrap_or(0);
|
||||
if parts == WHOLE || !parts_on() {
|
||||
self.parts.remove(&page);
|
||||
return before;
|
||||
}
|
||||
let all = self.parts.entry(page).or_insert([before; PARTS]);
|
||||
for (k, at) in all.iter_mut().enumerate() {
|
||||
if parts >> k & 1 != 0 {
|
||||
*at = now;
|
||||
}
|
||||
}
|
||||
before
|
||||
}
|
||||
|
||||
/// The parts of `page` written by an asking after `taken`.
|
||||
fn parts_since(&self, page: usize, taken: u64) -> u16 {
|
||||
match self.parts.get(&page) {
|
||||
Some(all) => all.iter().enumerate().filter(|(_, at)| **at > taken).fold(0, |m, (k, _)| m | 1 << k),
|
||||
None => WHOLE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Of the pages of a copy that were written after it took them (page number,
|
||||
/// the asking it took the page at): the parts to take again, a bit for each
|
||||
/// 4 KiB, in the same order.
|
||||
pub(crate) fn parts_written(pages: &[(usize, u64)]) -> Vec<u16> {
|
||||
let j = journal();
|
||||
pages.iter().map(|&(page, taken)| j.parts_since(page, taken)).collect()
|
||||
}
|
||||
|
||||
/// Numbers the askings; 0 is "never". Counted up with the journal held
|
||||
/// (shared will do: what notes an answer holds it alone).
|
||||
static ASKED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
@@ -68,6 +120,10 @@ pub(crate) fn track(start: usize, len: usize, views: bool) -> Option<Tracked> {
|
||||
struct Journal {
|
||||
/// Page number → the asking that last found it written.
|
||||
written: BTreeMap<usize, u64>,
|
||||
/// The same for each of a page's sixteen parts ([`KERNEL_PAGE`]), where
|
||||
/// the parts are known ([`parts_on`]); a page that is not here was
|
||||
/// written whole, as far as anyone knows.
|
||||
parts: std::collections::HashMap<usize, [u64; PARTS], std::hash::BuildHasherDefault<PageHash>>,
|
||||
/// Page ranges asked ahead for a submission's refreshes ([`ask_ahead`])
|
||||
/// and the asking that did: `writes` answers for a range inside one
|
||||
/// from the journal, without another kernel call.
|
||||
@@ -87,6 +143,7 @@ struct Journal {
|
||||
/// it, found it waiting for that lock (run pooloff1).
|
||||
static JOURNAL: RwLock<Journal> = RwLock::new(Journal {
|
||||
written: BTreeMap::new(),
|
||||
parts: std::collections::HashMap::with_hasher(std::hash::BuildHasherDefault::new()),
|
||||
ahead: Vec::new(),
|
||||
ahead_asked: 0,
|
||||
kept: false,
|
||||
@@ -142,9 +199,9 @@ thread_local! {
|
||||
}
|
||||
|
||||
/// The pages of `[first, last)` the kernel says were written since it was
|
||||
/// last asked, in order, each once; `None` if the range is not write-watched
|
||||
/// last asked, in order, each once, with their written parts; `None` if the range is not write-watched
|
||||
/// memory of one reservation. The caller holds the range ([`Asking`]).
|
||||
fn kernel_written(first: usize, last: usize) -> Option<Vec<usize>> {
|
||||
fn kernel_written(first: usize, last: usize) -> Option<Vec<(usize, u16)>> {
|
||||
use windows_sys::Win32::System::Memory::GetWriteWatch;
|
||||
const WRITE_WATCH_FLAG_RESET: u32 = 1;
|
||||
FOUND.with(|found| {
|
||||
@@ -185,8 +242,14 @@ fn kernel_written(first: usize, last: usize) -> Option<Vec<usize>> {
|
||||
tally(0, 1);
|
||||
tally(1, ((last - first) * (PAGE >> 10)) as u64);
|
||||
}
|
||||
let mut pages: Vec<usize> = found[..count].iter().map(|&at| at / PAGE).collect();
|
||||
pages.dedup();
|
||||
let mut pages: Vec<(usize, u16)> = Vec::new();
|
||||
for &at in &found[..count] {
|
||||
let (page, part) = (at / PAGE, 1u16 << (at % PAGE / KERNEL_PAGE));
|
||||
match pages.last_mut() {
|
||||
Some((last, parts)) if *last == page => *parts |= part,
|
||||
_ => pages.push((page, part)),
|
||||
}
|
||||
}
|
||||
Some(pages)
|
||||
})
|
||||
}
|
||||
@@ -244,7 +307,9 @@ pub(crate) fn ask_ahead(ranges: &[(usize, usize)]) {
|
||||
j.ahead_asked = next_asking();
|
||||
j.ahead = asked;
|
||||
let now = j.ahead_asked;
|
||||
j.written.extend(written.into_iter().map(|page| (page, now)));
|
||||
for (page, parts) in written {
|
||||
j.note(page, parts, now);
|
||||
}
|
||||
}
|
||||
|
||||
/// Since the last frame line: the kernel askings that took a millisecond
|
||||
@@ -273,8 +338,9 @@ pub(crate) fn slow_asks() -> [u64; 3] {
|
||||
}
|
||||
|
||||
/// Whether a thread asks, between a submission's askings, about the memory
|
||||
/// they found written (`VICTORY_ASK_EARLY=<ms>` between its rounds, 2 if not
|
||||
/// given; `0`: no such thread).
|
||||
/// they found written (`VICTORY_ASK_EARLY=<ms>` between its rounds; off
|
||||
/// unless given: in run cpu-2 the submissions' own askings were none the
|
||||
/// cheaper for its 650 a frame, 12 to 18 ms where 9 were).
|
||||
///
|
||||
/// An asking's cost is the pages it finds written and resets, some 1.4
|
||||
/// microseconds each, and the title writes 7,000 a frame: the first
|
||||
@@ -292,7 +358,7 @@ fn early() -> Option<std::time::Duration> {
|
||||
*EVERY.get_or_init(|| {
|
||||
let ms = match std::env::var("VICTORY_ASK_EARLY") {
|
||||
Ok(v) => v.trim().parse::<f64>().unwrap_or(2.0),
|
||||
Err(_) => 2.0,
|
||||
Err(_) => 0.0,
|
||||
};
|
||||
std::time::Duration::try_from_secs_f64(ms / 1000.0).ok().filter(|d| !d.is_zero())
|
||||
})
|
||||
@@ -318,12 +384,12 @@ pub(crate) fn early_asks() -> [u64; 2] {
|
||||
}
|
||||
|
||||
/// A submission's asking found `written` (pages).
|
||||
fn note_hot(written: &[usize]) {
|
||||
fn note_hot(written: &[(usize, u16)]) {
|
||||
let Some(every) = early() else {
|
||||
return;
|
||||
};
|
||||
let mut hot = HOT.lock().unwrap_or_else(|e| e.into_inner());
|
||||
for page in written {
|
||||
for (page, _) in written {
|
||||
let n = hot.entry(page / EARLY_PIECE).or_default();
|
||||
*n = (*n + 1).min(1 << 10);
|
||||
}
|
||||
@@ -460,7 +526,9 @@ fn ask_rest(ranges: &[(usize, usize)]) {
|
||||
// part's number that write was older than the copy, and never copied.
|
||||
let now = next_asking();
|
||||
j.ahead_asked = now;
|
||||
j.written.extend(written.into_iter().map(|page| (page, now)));
|
||||
for (page, parts) in written {
|
||||
j.note(page, parts, now);
|
||||
}
|
||||
j.kept = !ahead.is_empty();
|
||||
j.ahead = ahead;
|
||||
}
|
||||
@@ -489,10 +557,12 @@ pub(crate) fn wrote(at: usize, len: usize) {
|
||||
// until the next asking copy the pages again.
|
||||
j.ahead_asked = now;
|
||||
for page in at / PAGE..at.saturating_add(len).div_ceil(PAGE) {
|
||||
j.written.insert(page, now);
|
||||
j.note(page, WHOLE, now);
|
||||
}
|
||||
for (_, others) in elsewhere(at, len) {
|
||||
j.written.extend(others.into_iter().map(|other| (other / PAGE, now)));
|
||||
for other in others {
|
||||
j.note(other / PAGE, WHOLE, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -509,7 +579,7 @@ fn elsewhere(at: usize, len: usize) -> Vec<(usize, Vec<usize>)> {
|
||||
/// 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>) {
|
||||
fn ask_run(pieces: &[(usize, usize)], asked: &mut Vec<(usize, usize)>, written: &mut Vec<(usize, u16)>) {
|
||||
if pieces.len() < 2 {
|
||||
return;
|
||||
}
|
||||
@@ -575,7 +645,7 @@ pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Ve
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let guarding = std::time::Instant::now();
|
||||
let written = guard(first * PAGE, last * PAGE);
|
||||
let written = guard(first * PAGE, last * PAGE).map(|pages| pages.into_iter().map(|p| (p, WHOLE)).collect::<Vec<_>>());
|
||||
#[cfg(feature = "diagnostics")]
|
||||
{
|
||||
use crate::copystat::{C, add};
|
||||
@@ -598,14 +668,16 @@ pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Ve
|
||||
let answer = {
|
||||
let mut j = journal_mut();
|
||||
let now = next_asking();
|
||||
j.written.extend(pages.iter().map(|&page| (page, now)));
|
||||
for &(page, parts) in &pages {
|
||||
j.note(page, parts, now);
|
||||
}
|
||||
(now, of(&j))
|
||||
};
|
||||
// Noted: what the guards made clean of it is known by its bits from
|
||||
// now on. (The range is still held: no other asking of any of it came
|
||||
// between.)
|
||||
if guards_asked && light() {
|
||||
publish(&pages);
|
||||
publish(&pages.iter().map(|p| p.0).collect::<Vec<_>>());
|
||||
}
|
||||
Some(answer)
|
||||
}
|
||||
@@ -1098,16 +1170,21 @@ pub(crate) fn landed_beside(pages: &[usize], spans: &[(usize, usize)]) -> (u64,
|
||||
let mut j = journal_mut();
|
||||
let now = next_asking();
|
||||
noted = now;
|
||||
let mut last = usize::MAX;
|
||||
let mut blocks: Vec<(usize, u16)> = Vec::with_capacity(spans.len());
|
||||
for &page in pages {
|
||||
let block = page * KERNEL_PAGE / PAGE;
|
||||
if block != last {
|
||||
before.push((block, j.written.insert(block, now).unwrap_or(0)));
|
||||
last = block;
|
||||
let (block, part) = (page / PARTS, 1u16 << (page % PARTS));
|
||||
match blocks.last_mut() {
|
||||
Some((last, parts)) if *last == block => *parts |= part,
|
||||
_ => blocks.push((block, part)),
|
||||
}
|
||||
}
|
||||
for (block, parts) in blocks {
|
||||
before.push((block, j.note(block, parts, now)));
|
||||
}
|
||||
for (_, others) in &elsewhere {
|
||||
j.written.extend(others.iter().map(|other| (other / PAGE, now)));
|
||||
for other in others {
|
||||
j.note(other / PAGE, WHOLE, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
for &(at, len) in spans {
|
||||
@@ -2114,6 +2191,38 @@ fn writable(at: usize) -> bool {
|
||||
n != 0 && info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod parts_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_parts_of_a_page_written_since_it_was_taken() {
|
||||
let mut j = Journal {
|
||||
written: BTreeMap::new(),
|
||||
parts: std::collections::HashMap::with_hasher(std::hash::BuildHasherDefault::new()),
|
||||
ahead: Vec::new(),
|
||||
ahead_asked: 0,
|
||||
kept: false,
|
||||
};
|
||||
assert_eq!(j.parts_since(7, 0), WHOLE);
|
||||
assert_eq!(j.note(7, WHOLE, 3), 0);
|
||||
// Parts 0 and 5 at 4, part 5 again at 6: at 2 the whole page is due.
|
||||
assert_eq!(j.note(7, 0b10_0001, 4), 3);
|
||||
assert_eq!(j.note(7, 0b10_0000, 6), 4);
|
||||
assert_eq!(j.parts_since(7, 2), WHOLE);
|
||||
assert_eq!(j.parts_since(7, 3), 0b10_0001);
|
||||
assert_eq!(j.parts_since(7, 4), 0b10_0000);
|
||||
assert_eq!(j.parts_since(7, 5), 0b10_0000);
|
||||
assert_eq!(j.parts_since(7, 6), 0);
|
||||
// Written whole: all of it, whoever took it before.
|
||||
assert_eq!(j.note(7, WHOLE, 8), 6);
|
||||
assert_eq!(j.parts_since(7, 7), WHOLE);
|
||||
// A page never written before: what was not named was never written.
|
||||
assert_eq!(j.note(9, 0b100, 9), 0);
|
||||
assert_eq!(j.parts_since(9, 5), 0b100);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in new issue
Block a user