Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 151 additions & 13 deletions crates/xcl/src/gpu/uma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,47 @@ fn take_sparse_in(
Some(at)
}

/// `take_sparse_in` from the top down: the highest `align`-aligned block of `size` bytes
/// (`size <= WINDOW`) in `[lo_bound, hi_bound)` that does not straddle a 4 GiB window boundary.
fn take_sparse_top_in(
g: &mut (BTreeMap<usize, usize>, BTreeMap<usize, usize>),
lo_bound: usize,
hi_bound: usize,
size: usize,
align: usize,
) -> Option<usize> {
if lo_bound >= hi_bound || size == 0 || size > WINDOW {
return None;
}
let last_fit = |(&o, &l): (&usize, &usize)| {
let lo = o.max(lo_bound);
let hi = o.saturating_add(l).min(hi_bound);
let mut top = hi;
loop {
let at = top.checked_sub(size)? & !(align - 1);
if at < lo {
return None;
}
if at / WINDOW == (at + size - 1) / WINDOW {
return Some((o, l, at));
}
// it straddles: end it at the window boundary inside it
top = (at + size - 1) / WINDOW * WINDOW;
}
};
// (free ranges are disjoint: from the first one ending at or below lo_bound on, none reaches it)
let (start, len, at) = g.0.range(..hi_bound).rev().take_while(|(&o, &l)| o + l > lo_bound).find_map(last_fit)?;
g.0.remove(&start);
if at > start {
g.0.insert(start, at - start);
}
if at + size < start + len {
g.0.insert(at + size, start + len - (at + size));
}
g.1.insert(at, size);
Some(at)
}

/// The reservation of `take_sparse_in` at `p` goes back to the free ranges (merged with its
/// neighbours): its bytes.
fn give_back(g: &mut (BTreeMap<usize, usize>, BTreeMap<usize, usize>), p: usize) -> Option<usize> {
Expand All @@ -219,6 +260,12 @@ fn give_back(g: &mut (BTreeMap<usize, usize>, BTreeMap<usize, usize>), p: usize)
Some(size)
}

/// The free bytes of `[lo, hi)`: in all and the largest run.
fn free_runs_in(g: &(BTreeMap<usize, usize>, BTreeMap<usize, usize>), lo: usize, hi: usize) -> (usize, usize) {
let runs = g.0.iter().map(|(&o, &l)| (o.max(lo), (o + l).min(hi))).filter(|&(a, e)| a < e).map(|(a, e)| e - a);
runs.fold((0, 0), |(all, big), n| (all + n, big.max(n)))
}

/// The reservation of `take_sparse_in` holding `p`: (start, bytes).
fn reservation_at(g: &(BTreeMap<usize, usize>, BTreeMap<usize, usize>), p: usize) -> Option<(usize, usize)> {
g.1.range(..=p).next_back().map(|(&a, &l)| (a, l)).filter(|&(a, l)| p < a + l)
Expand Down Expand Up @@ -609,30 +656,77 @@ impl Uma {
}
}

/// `sparse_alloc` for memory shaders never address with 32 bits: windows 1+ first (as
/// `sparse_reserve`), window 0 only when they have no room – it stands for the console's low
/// 4 GiB, where a title's own descriptor heaps must lie (`console_heap::title_memory`).
/// `sparse_alloc` for memory shaders never address with 32 bits: windows 1+ first, from the
/// top of the last one down, window 0 only when they have no room – it stands for the
/// console's low 4 GiB, where a title's own descriptor heaps must lie
/// (`console_heap::title_memory`). From the top, so the title's reservations
/// (`sparse_reserve`, first fit from window 1 up) keep one free run between: a retail game's
/// page pool of 63632 pages (3.9 GiB, across windows 1 and 2) found no room once physical
/// page homes had gone first-fit into windows 1+, and was plain memory. Window 0 from its
/// top down too, so its low addresses stay free for the title's 32-bit memory (`sparse_alloc`,
/// first fit from the bottom).
/// Above a window the same as `sparse_alloc`.
pub fn sparse_alloc_above(&self, size: usize, align: usize) -> Option<(usize, usize)> {
if size <= WINDOW && self.has_sparse() {
let size = size.max(1).checked_next_multiple_of(GRAIN)?;
let align = align.max(GRAIN).checked_next_power_of_two()?;
let above_0 = (self.base + WINDOW).max(self.title_sparse_start());
if let Some(at) = take_sparse_in(&mut self.sparse_free.lock().unwrap(), above_0, self.sparse_end(), size, align, true) {
let lo = self.title_sparse_start();
let above_0 = (self.base + WINDOW).max(lo);
let mut g = self.sparse_free.lock().unwrap();
if let Some(at) = take_sparse_top_in(&mut g, above_0, self.sparse_end(), size, align) {
return Some((at, size));
}
if self.is_sparse_window(0) {
return take_sparse_top_in(&mut g, lo, self.base + WINDOW, size, align).map(|at| (at, size));
}
return None;
}
self.sparse_alloc(size, align)
}

/// Homes of physical pages (`kernelx::alloc_physical_home`): window 0 from its top down while
/// it keeps a free run of `w0_keep` bytes (its largest) after the home, otherwise `sparse_alloc_above`
/// (windows 1+ from the top of the last one down, window 0 past the margin, from its top,
/// only when they have no room). Window 0 first, because windows 1+ are where the title's own reservations
/// go (`sparse_reserve`): a retail game's page pool of 3977 MB, across windows 1 and 2, found
/// no room with 600 MB of homes at the top of window 2. From its top down and with a margin,
/// because its low addresses are where the title's 32-bit descriptor heaps go
/// (`sparse_alloc`, first fit; `console_heap::title_memory`): with homes first-fit there a
/// Series S run's 38 MB view heap found no room in window 0 (3292b096).
pub fn sparse_alloc_home(&self, size: usize, align: usize, w0_keep: usize) -> Option<(usize, usize)> {
if size <= WINDOW && self.is_sparse_window(0) {
let size = size.max(1).checked_next_multiple_of(GRAIN)?;
let align = align.max(GRAIN).checked_next_power_of_two()?;
let (lo, hi) = (self.title_sparse_start(), self.base + WINDOW);
let mut g = self.sparse_free.lock().unwrap();
if let Some(at) = take_sparse_top_in(&mut g, lo, hi, size, align) {
if free_runs_in(&g, lo, hi).1 >= w0_keep {
return Some((at, size));
}
give_back(&mut g, at);
}
}
self.sparse_alloc_above(size, align)
}

/// Diagnostic: the free bytes of sparse window `k` the title may take (not `desc_range`), in
/// all and the largest run.
pub fn sparse_free_in(&self, k: usize) -> (usize, usize) {
let (lo, hi) = (self.base + k * WINDOW, self.base + (k + 1) * WINDOW);
let lo = if k == 0 { lo.max(self.title_sparse_start()) } else { lo };
let g = self.sparse_free.lock().unwrap();
let runs = g.0.iter().map(|(&o, &l)| (o.max(lo), (o + l).min(hi))).filter(|&(a, e)| a < e).map(|(a, e)| e - a);
runs.fold((0, 0), |(all, big), n| (all + n, big.max(n)))
free_runs_in(&self.sparse_free.lock().unwrap(), lo, hi)
}

/// Diagnostic: the free bytes of all the sparse windows the title may take (not
/// `desc_range`), in all and the largest run (runs go across window boundaries).
pub fn sparse_free_all(&self) -> (usize, usize) {
free_runs_in(&self.sparse_free.lock().unwrap(), self.title_sparse_start(), self.sparse_end())
}

/// The bytes of the sparse windows the title's memory may take (all of them less
/// `desc_range`): no reservation larger than this ever fits there.
pub fn sparse_title_capacity(&self) -> usize {
self.sparse_end().saturating_sub(self.title_sparse_start())
}

/// The bytes of the sparse reservation starting at `p` (it is gone after this).
Expand Down Expand Up @@ -1491,23 +1585,67 @@ mod tests {
let u = Uma::for_runtime_windows(16 << 20, RUNTIME_COMMITTED_CAP, MAX_HEAP_WINDOWS).expect("16-window aperture");
let b = u.base();
let (h, n) = u.sparse_alloc_above(664 << 20, GRAIN).unwrap();
assert_eq!((h, n), (b + WINDOW, 664 << 20));
assert_eq!((h, n), (b + 3 * WINDOW - (664 << 20), 664 << 20), "the top of the last sparse window");
// window 0 still has the room a 38 MB view heap needs after 2.9 GB of GPU heaps
let heaps = u.sparse_alloc(2892 << 20, 2 << 20).unwrap();
assert_eq!(u.window_of(heaps.0), Some(0));
assert_eq!(u.sparse_alloc(0x260_0000, GRAIN).map(|(a, _)| u.window_of(a)), Some(Some(0)));
let used = (2892 << 20) + 0x260_0000;
assert_eq!(u.sparse_free_in(0), (WINDOW - 0x2001_0000 - used, WINDOW - 0x2020_0000 - used));
assert_eq!(u.sparse_free_in(1), (WINDOW - (664 << 20), WINDOW - (664 << 20)));
// windows 1..2 full: window 0
assert_eq!(u.sparse_free_in(1), (WINDOW, WINDOW));
assert_eq!(u.sparse_free_in(2), (WINDOW - (664 << 20), WINDOW - (664 << 20)));
// the rest of windows 1..2 is one run (with the end of window 0 before it): a title
// reservation of all of it fits
assert_eq!(u.sparse_free_all().1, 2 * WINDOW - (664 << 20) + u.sparse_free_in(0).1);
let rest = u.sparse_reserve(2 * WINDOW - (664 << 20)).unwrap();
assert_eq!(u.window_of(rest), Some(1));
assert_eq!(rest, b + WINDOW);
// windows 1..2 full: window 0
let (w0, _) = u.sparse_alloc_above(GRAIN, GRAIN).unwrap();
assert_eq!(u.window_of(w0), Some(0));
assert_eq!(w0, b + WINDOW - GRAIN, "window 0 from its top down: its low addresses stay free");
assert!(w0 >= u.title_sparse_start(), "never the descriptor headroom");
assert!(u.sparse_alloc_above(WINDOW + GRAIN, GRAIN).is_none(), "no room above a window");
}

/// `take_sparse_top_in` with a free range that starts below `lo_bound` and ends above it:
/// the block is the range's top, never below `lo_bound`, and what is left keeps its start;
/// one that would reach below `lo_bound` does not fit, nor does it take a free range lying
/// wholly below.
#[test]
fn take_sparse_top_in_a_free_range_across_lo_bound() {
let m = 1usize << 20;
let mut g = (BTreeMap::from([(16 * m, 4 * m), (32 * m, 32 * m)]), BTreeMap::new());
let (lo, hi) = (48 * m, 128 * m);
assert_eq!(take_sparse_top_in(&mut g, lo, hi, 17 * m, GRAIN), None, "only 16 MB of it is at or above lo_bound");
assert_eq!(take_sparse_top_in(&mut g, lo, hi, 10 * m, GRAIN), Some(54 * m));
assert_eq!(take_sparse_top_in(&mut g, lo, hi, 6 * m, GRAIN), Some(48 * m), "down to lo_bound exactly");
assert_eq!(take_sparse_top_in(&mut g, lo, hi, GRAIN, GRAIN), None, "nothing left above lo_bound");
assert_eq!(g.0, BTreeMap::from([(16 * m, 4 * m), (32 * m, 16 * m)]));
assert_eq!(g.1, BTreeMap::from([(48 * m, 6 * m), (54 * m, 10 * m)]));
}

/// From the top down, a block of `sparse_alloc_above` still never straddles a window
/// boundary: one that would ends at the boundary instead, and with no room on either side
/// it goes to window 0.
#[test]
fn sparse_alloc_above_from_the_top_keeps_within_a_window() {
let u = Uma::for_runtime_windows(16 << 20, RUNTIME_COMMITTED_CAP, MAX_HEAP_WINDOWS).expect("16-window aperture");
let b = u.base();
let title = u.sparse_reserve(WINDOW - (100 << 20)).unwrap();
assert_eq!(title, b + WINDOW);
let (top, _) = u.sparse_alloc_above(WINDOW - (50 << 20), GRAIN).unwrap();
assert_eq!(top, b + 2 * WINDOW + (50 << 20));
// free above window 0: the last 100 MB of window 1 and the first 50 MB of window 2
assert_eq!((u.sparse_free_in(1), u.sparse_free_in(2)), ((100 << 20, 100 << 20), (50 << 20, 50 << 20)));
let (at, n) = u.sparse_alloc_above(80 << 20, 1 << 20).unwrap();
assert_eq!((at, n), (b + 2 * WINDOW - (80 << 20), 80 << 20), "ends at the boundary");
let (w0, _) = u.sparse_alloc_above(60 << 20, GRAIN).unwrap();
assert_eq!(w0, b + WINDOW - (60 << 20), "20 MB in window 1 and 50 MB in window 2 hold no 60 MB: the top of window 0");
for p in [w0, at, top, title] {
assert!(u.sparse_release(p).is_some());
}
assert_eq!(u.sparse_free_all(), (u.sparse_title_capacity(), u.sparse_title_capacity()));
}

/// With 16 windows nothing of Windows' lands in the aperture: shaders read every window of
/// it, and a window without memory of its own once read whatever Windows had put there.
#[test]
Expand Down
Loading
Loading