forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

cacheline.rs78 lines, 3.0 KiBHistoryRawDownload
1//! Which cache lines a CPU write-back of a mapped DMA-BUF has to visit.
2//!
3//! `CLFLUSH` takes an address and writes back the whole cache line holding it,
4//! so a buffer needs one flush per line, not one per byte. The incumbent
5//! stack flushed every byte (64 instructions per line, 6.2 ms per 1080p
6//! picture on radeonsi). The base of a mapping is not guaranteed to be cache
7//! aligned, so the last byte's line is flushed too: a range that starts
8//! mid-line can straddle one more line than its length suggests.
9
10/// Bytes per cache line on every x86-64 part this crate targets.
11pub const CACHE_LINE: usize = 64;
12
13/// Offsets from a mapping's base at which to issue a flush for a buffer of
14/// `len` bytes: one per [`CACHE_LINE`], plus the last byte when the stride
15/// does not land on it. Empty for `len == 0`, which has no line to flush.
16pub fn flush_offsets(len: usize) -> Vec<usize> {
17 if len == 0 {
18 return Vec::new();
19 }
20 let last = len - 1;
21 let mut offsets: Vec<usize> = (0..len).step_by(CACHE_LINE).collect();
22 if offsets.last() != Some(&last) {
23 offsets.push(last);
24 }
25 offsets
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31
32 /// The absolute cache-line indices a flush at `offsets` writes back, for a
33 /// mapping whose base sits `base_mod` bytes into a line.
34 fn lines_flushed(base_mod: usize, offsets: &[usize]) -> Vec<usize> {
35 let base = 1000 * CACHE_LINE + base_mod;
36 let mut lines: Vec<usize> = offsets.iter().map(|o| (base + o) / CACHE_LINE).collect();
37 lines.sort_unstable();
38 lines.dedup();
39 lines
40 }
41
42 #[test]
43 fn empty_buffer_has_nothing_to_flush() {
44 assert!(flush_offsets(0).is_empty());
45 }
46
47 #[test]
48 fn one_flush_per_line_not_per_byte() {
49 assert_eq!(flush_offsets(1), vec![0]);
50 // 64 bytes from an unaligned base can still straddle two lines: the last byte too.
51 assert_eq!(flush_offsets(64), vec![0, 63]);
52 assert_eq!(flush_offsets(65), vec![0, 64]);
53 assert_eq!(flush_offsets(128), vec![0, 64, 127]);
54 // A 1080p NV12 picture (~3.1 MB) is ~48K flushes, not ~3.1M.
55 assert!(flush_offsets(1920 * 1620).len() <= 1920 * 1620 / CACHE_LINE + 2);
56 }
57
58 // Regression for the straddle: with an unaligned base, the bytes of a
59 // range can sit in one more line than a stride from the base reaches, so
60 // every byte's line must be among the flushed lines for every alignment.
61 #[test]
62 fn every_byte_is_covered_for_every_base_alignment() {
63 for base_mod in 0..CACHE_LINE {
64 for len in 0..=300 {
65 let offsets = flush_offsets(len);
66 let flushed = lines_flushed(base_mod, &offsets);
67 let base = 1000 * CACHE_LINE + base_mod;
68 for i in 0..len {
69 let line = (base + i) / CACHE_LINE;
70 assert!(
71 flushed.contains(&line),
72 "base_mod {base_mod} len {len}: byte {i} (line {line}) not flushed"
73 );
74 }
75 }
76 }
77 }
78}