VICTORY_SYNC_IN_PLACE=1: the heaps' records read where they are and compared as read
Ported from Belobug/VIctory-Belobug-AMD 15ab94e (and the test's type from 8822ed4; that commit's turning it on by default is not taken). The round of the heaps' records (descriptor::sync_heaps) read each changed page through the kernel into a copy, then compared the copy with the records last seen: two passes over every page. With the switch each record of 32 bytes is read in place once by the fault-safe copy code (safecopy::changed, AVX2), compared with the one last seen, and put over it where it differs; a page the title unmaps meanwhile fails the read as the kernel's read failed, the records before it taken as read. Measured there: copy 7.4 and compare 5.3 ms a frame of six threads for 35 MB. Here the round's reading and comparing was 2.6-3.1 ms a frame (release run 233354), and the submitting thread waits 2.7-5.8 ms a frame for rounds. Changes nothing of what is mirrored (the same records found changed); reads title memory in place instead of through the kernel. Off by default. With it on, the frame line "the heaps' records read where they are" counts pages and those not all there; the heaps' records line's "reading and comparing" and "ms waited for" show the gain. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
66f7e545e3
commit
769fe52887
3 files changed
+230
-20
No files matched your search
@@ -134,6 +134,43 @@ std::arch::global_asm!(
|
||||
"mov qword ptr [rax + 8], r11",
|
||||
"mov rax, r9",
|
||||
"ret",
|
||||
".globl victory_safe_changed",
|
||||
".globl victory_safe_changed_at",
|
||||
".globl victory_safe_changed_after",
|
||||
".globl victory_safe_changed_failed",
|
||||
// rcx title memory, rdx what was last seen there, r8 records of 32
|
||||
// bytes, r9 a bit per record (zeroed by the caller): each record of
|
||||
// title memory is read once; where it differs from the one last seen it
|
||||
// goes over that one, and then its bit is set. eax: whether all could
|
||||
// be read (where one could not, those before it are done).
|
||||
"victory_safe_changed:",
|
||||
"xor r10d, r10d",
|
||||
"test r8, r8",
|
||||
"jz 3f",
|
||||
"2:",
|
||||
"victory_safe_changed_at:",
|
||||
"vmovdqu ymm0, ymmword ptr [rcx]",
|
||||
"victory_safe_changed_after:",
|
||||
"vpcmpeqb ymm1, ymm0, ymmword ptr [rdx]",
|
||||
"vpmovmskb eax, ymm1",
|
||||
"cmp eax, -1",
|
||||
"je 4f",
|
||||
"vmovdqu ymmword ptr [rdx], ymm0",
|
||||
"bts qword ptr [r9], r10",
|
||||
"4:",
|
||||
"add rcx, 32",
|
||||
"add rdx, 32",
|
||||
"inc r10",
|
||||
"cmp r10, r8",
|
||||
"jb 2b",
|
||||
"3:",
|
||||
"vzeroupper",
|
||||
"mov eax, 1",
|
||||
"ret",
|
||||
"victory_safe_changed_failed:",
|
||||
"vzeroupper",
|
||||
"xor eax, eax",
|
||||
"ret",
|
||||
);
|
||||
|
||||
unsafe extern "system" {
|
||||
@@ -149,6 +186,27 @@ unsafe extern "system" {
|
||||
fn victory_safe_merge_at();
|
||||
fn victory_safe_merge_after();
|
||||
fn victory_safe_merge_failed();
|
||||
fn victory_safe_changed(title: *const u8, seen: *mut u8, records: usize, bits: *mut u64) -> u32;
|
||||
fn victory_safe_changed_at();
|
||||
fn victory_safe_changed_after();
|
||||
fn victory_safe_changed_failed();
|
||||
}
|
||||
|
||||
/// The records of 32 bytes at `title` (`records` of them) that differ from
|
||||
/// those in `seen`: each is read once, goes over the one in `seen`, and gets
|
||||
/// its bit in `bits` (record n: bit n % 64 of word n / 64). False if a page
|
||||
/// of title memory could not be read: then the records before it are done,
|
||||
/// and the others are as they were.
|
||||
///
|
||||
/// # Safety
|
||||
/// `seen` holds `records * 32` bytes of ours that nothing else reads or
|
||||
/// writes meanwhile, and `bits` at least `records.div_ceil(64)` words, all
|
||||
/// 0; `title` is as [`copy`]'s source. [`on`] must have been asked.
|
||||
pub(crate) unsafe fn changed(title: *const u8, seen: *mut u8, records: usize, bits: &mut [u64]) -> bool {
|
||||
assert!(bits.len() >= records.div_ceil(64));
|
||||
// SAFETY: a fault reading title memory leaves through the failing exit
|
||||
// ([`failing_exit`]); the rest is the caller's.
|
||||
unsafe { victory_safe_changed(title, seen, records, bits.as_mut_ptr()) != 0 }
|
||||
}
|
||||
|
||||
/// What [`merge`] did.
|
||||
@@ -354,6 +412,10 @@ pub(crate) fn failing_exit(at: usize) -> Option<usize> {
|
||||
if (from..to).contains(&at) {
|
||||
return Some(victory_safe_put_failed as *const () as usize);
|
||||
}
|
||||
let (from, to) = (victory_safe_changed_at as *const () as usize, victory_safe_changed_after as *const () as usize);
|
||||
if (from..to).contains(&at) {
|
||||
return Some(victory_safe_changed_failed as *const () as usize);
|
||||
}
|
||||
let (from, to) = (victory_safe_merge_at as *const () as usize, victory_safe_merge_after as *const () as usize);
|
||||
(from..to).contains(&at).then_some(victory_safe_merge_failed as *const () as usize)
|
||||
}
|
||||
@@ -409,6 +471,47 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The records that differ get their bits and go over those last seen;
|
||||
/// a page that is not there fails, with the records before it done.
|
||||
#[test]
|
||||
fn changed_records_are_found_and_taken() {
|
||||
assert!(on());
|
||||
let pages = Pages::new(3 << 12);
|
||||
pages.fill();
|
||||
let records: usize = (3 << 12) / 32;
|
||||
// SAFETY: inside the allocation.
|
||||
let title = unsafe { std::slice::from_raw_parts(pages.0, pages.1) }.to_vec();
|
||||
let mut seen = title.clone();
|
||||
let mut differ = Vec::new();
|
||||
for n in [0usize, 1, 63, 64, 65, 200, 383] {
|
||||
seen[n * 32 + n % 32] ^= 0x11;
|
||||
differ.push(n);
|
||||
}
|
||||
let mut bits = vec![0u64; records.div_ceil(64)];
|
||||
// SAFETY: inside the allocation; `seen` and `bits` are ours.
|
||||
assert!(unsafe { changed(pages.0, seen.as_mut_ptr(), records, &mut bits) });
|
||||
let found: Vec<usize> = (0..records).filter(|n| bits[n / 64] >> (n % 64) & 1 != 0).collect();
|
||||
assert_eq!(found, differ);
|
||||
assert_eq!(seen, title);
|
||||
bits.fill(0);
|
||||
// SAFETY: as above.
|
||||
assert!(unsafe { changed(pages.0, seen.as_mut_ptr(), records, &mut bits) });
|
||||
assert!(bits.iter().all(|&b| b == 0));
|
||||
// The second page is not there: records 0 and 200 (on the third) differ.
|
||||
for n in [0usize, 200] {
|
||||
seen[n * 32] ^= 0x22;
|
||||
}
|
||||
// SAFETY: a page of our allocation.
|
||||
unsafe { VirtualFree(pages.0.add(4096).cast(), 4096, MEM_DECOMMIT) };
|
||||
let mut bits = vec![0u64; records.div_ceil(64)];
|
||||
// SAFETY: as above; the page that is not there fails the call.
|
||||
assert!(!unsafe { changed(pages.0, seen.as_mut_ptr(), records, &mut bits) });
|
||||
let found: Vec<usize> = (0..records).filter(|n| bits[n / 64] >> (n % 64) & 1 != 0).collect();
|
||||
assert_eq!(found, [0]);
|
||||
assert_eq!(seen[..32], title[..32]);
|
||||
assert_ne!(seen[200 * 32..201 * 32], title[200 * 32..201 * 32]);
|
||||
}
|
||||
|
||||
/// The dwords that changed are stored, and no byte of any other, at
|
||||
/// every alignment and length; a page that is not there fails.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user