gpu: port lock-free submit-thread watch/shadow/batch/card_steps/send optimizations
This commit is contained in:
1 parent
84b4509fa3
commit
6f34121d31
5 files changed
+325
-143
No files matched your search
+111
-27
@@ -319,22 +319,52 @@ const AHEAD_GAP: usize = (8 << 20) / PAGE;
|
||||
/// The most one asking covers (its answer needs 8 bytes per 4 KiB page).
|
||||
const AHEAD_MAX: usize = (1 << 30) / PAGE;
|
||||
|
||||
/// Fast atomic mirror of `JOURNAL.kept`: true while a submission's `ask_once`
|
||||
/// covers its ranges so per-flush `ask_ahead`/`ask_ahead_done` skip locking
|
||||
/// `JOURNAL` and building range vectors.
|
||||
static AHEAD_KEPT: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
|
||||
/// Whether the submission's one-shot asking (`ask_once`) already covers
|
||||
/// write-watch queries, so per-flush `ask_ahead` calls are no-ops.
|
||||
#[inline]
|
||||
pub(crate) fn kept_active() -> bool {
|
||||
AHEAD_KEPT.load(std::sync::atomic::Ordering::Acquire)
|
||||
}
|
||||
|
||||
fn sort_merge_ahead(ranges: &mut Vec<(usize, usize)>) {
|
||||
if ranges.len() <= 1 {
|
||||
return;
|
||||
}
|
||||
ranges.sort_unstable();
|
||||
let mut write = 0usize;
|
||||
for read in 1..ranges.len() {
|
||||
let (a, b) = ranges[read];
|
||||
if a <= ranges[write].1 {
|
||||
ranges[write].1 = ranges[write].1.max(b);
|
||||
} else {
|
||||
write += 1;
|
||||
ranges[write] = (a, b);
|
||||
}
|
||||
}
|
||||
ranges.truncate(write + 1);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn ahead_contains(ahead: &[(usize, usize)], first: usize, last: usize) -> bool {
|
||||
let idx = ahead.partition_point(|&(a, _)| a <= first);
|
||||
idx > 0 && ahead[idx - 1].1 >= last
|
||||
}
|
||||
|
||||
/// Asks the kernel about many ranges `(start, end)` at once, before the
|
||||
/// [`writes`] of each: ranges close together are one asking, and `writes`
|
||||
/// then answers from the journal until [`ask_ahead_done`]. The world's
|
||||
/// Whether the submission's one-shot asking (`ask_once`) already covers
|
||||
/// write-watch queries, so per-flush `ask_ahead` calls are no-ops.
|
||||
pub(crate) fn kept_active() -> bool {
|
||||
journal().kept
|
||||
}
|
||||
|
||||
/// submissions refresh thousands of host copies (6,664 askings a frame,
|
||||
/// 456 ms, in run 14). Ranges that are not write-watched, or lie alone, are
|
||||
/// left to `writes`.
|
||||
pub(crate) fn ask_ahead(ranges: &[(usize, usize)]) {
|
||||
// The submission's one asking stands ([`ask_once`]): check before
|
||||
// allocating, filtering, or sorting thousands of ranges.
|
||||
if journal().kept {
|
||||
if kept_active() {
|
||||
return;
|
||||
}
|
||||
let mut pieces: Vec<(usize, usize)> = ranges
|
||||
@@ -365,6 +395,7 @@ pub(crate) fn ask_ahead(ranges: &[(usize, usize)]) {
|
||||
ask_run(&pieces[i..k], &mut asked, &mut written);
|
||||
i = k;
|
||||
}
|
||||
sort_merge_ahead(&mut asked);
|
||||
note_hot(&written);
|
||||
let mut j = journal_mut();
|
||||
j.ahead_asked = next_asking();
|
||||
@@ -513,6 +544,9 @@ fn ask_early(every: std::time::Duration) {
|
||||
|
||||
/// The submission's refreshes are done: `writes` asks the kernel again.
|
||||
pub(crate) fn ask_ahead_done() {
|
||||
if kept_active() {
|
||||
return;
|
||||
}
|
||||
let mut j = journal_mut();
|
||||
if !j.kept {
|
||||
j.ahead.clear();
|
||||
@@ -569,18 +603,24 @@ fn ask_rest(ranges: &[(usize, usize)]) {
|
||||
// askings a frame with the switch on, as without it). They are asked
|
||||
// here, once; what is not write-watched stays with `writes`.
|
||||
let mut written = Vec::new();
|
||||
let mut extra = Vec::new();
|
||||
for &(start, end) in ranges {
|
||||
if start >= end || published(start, end) {
|
||||
continue;
|
||||
}
|
||||
let (first, last) = (start / PAGE, end.div_ceil(PAGE));
|
||||
if !ahead.iter().any(|&(a, b)| a <= first && last <= b)
|
||||
if !ahead_contains(&ahead, first, last)
|
||||
&& !extra.iter().any(|&(a, b)| a <= first && last <= b)
|
||||
&& let Some(pages) = kernel_written(first, last)
|
||||
{
|
||||
written.extend(pages);
|
||||
ahead.push((first, last));
|
||||
extra.push((first, last));
|
||||
}
|
||||
}
|
||||
if !extra.is_empty() {
|
||||
ahead.extend(extra);
|
||||
sort_merge_ahead(&mut ahead);
|
||||
}
|
||||
note_hot(&written);
|
||||
let mut j = journal_mut();
|
||||
// A number of its own, and the one this asking stands under: between
|
||||
@@ -592,13 +632,19 @@ fn ask_rest(ranges: &[(usize, usize)]) {
|
||||
for (page, parts) in written {
|
||||
j.note(page, parts, now);
|
||||
}
|
||||
j.kept = !ahead.is_empty();
|
||||
let kept = !ahead.is_empty();
|
||||
j.kept = kept;
|
||||
j.ahead = ahead;
|
||||
AHEAD_KEPT.store(kept, std::sync::atomic::Ordering::Release);
|
||||
}
|
||||
|
||||
/// The submission's one asking stands no longer: its refreshes are done,
|
||||
/// or title memory was written in a way the journal was not told.
|
||||
pub(crate) fn ask_once_end() {
|
||||
if !kept_active() {
|
||||
return;
|
||||
}
|
||||
AHEAD_KEPT.store(false, std::sync::atomic::Ordering::Release);
|
||||
let mut j = journal_mut();
|
||||
if j.kept {
|
||||
j.kept = false;
|
||||
@@ -666,9 +712,7 @@ fn ask_run(pieces: &[(usize, usize)], asked: &mut Vec<(usize, usize)>, written:
|
||||
pub(crate) fn asked(start: usize, end: usize) -> Option<(u64, Vec<(usize, u64)>)> {
|
||||
let (first, last) = (start / PAGE, end.div_ceil(PAGE));
|
||||
let j = journal();
|
||||
j.ahead
|
||||
.iter()
|
||||
.any(|&(a, b)| a <= first && last <= b)
|
||||
ahead_contains(&j.ahead, first, last)
|
||||
.then(|| (j.ahead_asked, j.written.range(first..last).map(|(&p, &w)| (p, w)).collect()))
|
||||
}
|
||||
|
||||
@@ -687,7 +731,7 @@ pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Ve
|
||||
let (first, last) = (start / PAGE, end.div_ceil(PAGE));
|
||||
let of = |j: &Journal| -> Vec<(usize, u64)> { j.written.range(first..last).map(|(&p, &w)| (p, w)).collect() };
|
||||
// Asked ahead with the submission's other ranges: the journal has it.
|
||||
let ahead = |j: &Journal| j.ahead.iter().any(|&(a, b)| a <= first && last <= b).then(|| (j.ahead_asked, of(j)));
|
||||
let ahead = |j: &Journal| ahead_contains(&j.ahead, first, last).then(|| (j.ahead_asked, of(j)));
|
||||
if let Some(known) = ahead(&journal()) {
|
||||
return Some(known);
|
||||
}
|
||||
@@ -771,7 +815,7 @@ pub(crate) fn nothing_new(start: usize, end: usize, base: usize, copied: &[u64],
|
||||
crate::faultbook::note(crate::faultbook::What::Known, first, last, 1);
|
||||
}
|
||||
let j = journal();
|
||||
if !known && !j.ahead.iter().any(|&(a, b)| a <= first && last <= b) {
|
||||
if !known && !ahead_contains(&j.ahead, first, last) {
|
||||
return false;
|
||||
}
|
||||
let Some(taken) = first.checked_sub(base).and_then(|from| copied.get(from..last - base)) else {
|
||||
@@ -1472,7 +1516,7 @@ pub(crate) fn asks() -> [u64; 13] {
|
||||
}
|
||||
|
||||
/// The guards' lock, the wait for it counted.
|
||||
fn guards_timed() -> std::sync::MutexGuard<'static, Guards> {
|
||||
fn guards_timed() -> std::sync::RwLockWriteGuard<'static, Guards> {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
let g = guards();
|
||||
@@ -1481,6 +1525,16 @@ fn guards_timed() -> std::sync::MutexGuard<'static, Guards> {
|
||||
g
|
||||
}
|
||||
|
||||
/// The guards' shared read lock, the wait for it counted.
|
||||
fn guards_read_timed() -> std::sync::RwLockReadGuard<'static, Guards> {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
let g = guards_read();
|
||||
#[cfg(feature = "diagnostics")]
|
||||
tally(7, started.elapsed().as_nanos() as u64);
|
||||
g
|
||||
}
|
||||
|
||||
/// A set of blocks, a bit each: a fault, or a look at a range of thousands of
|
||||
/// blocks, is a few words of it and no walk through a tree of every guarded
|
||||
/// block there is.
|
||||
@@ -1746,7 +1800,7 @@ impl Guards {
|
||||
}
|
||||
}
|
||||
|
||||
static GUARDS: Mutex<Guards> = Mutex::new(Guards {
|
||||
static GUARDS: std::sync::RwLock<Guards> = std::sync::RwLock::new(Guards {
|
||||
protected: Blocks::new(),
|
||||
clean: Blocks::new(),
|
||||
absent: Blocks::new(),
|
||||
@@ -1760,17 +1814,29 @@ static GUARDS: Mutex<Guards> = Mutex::new(Guards {
|
||||
|
||||
/// The guards' lock, with what faults opened without it settled first
|
||||
/// ([`settle`]).
|
||||
fn guards() -> std::sync::MutexGuard<'static, Guards> {
|
||||
let mut g = match GUARDS.try_lock() {
|
||||
fn guards() -> std::sync::RwLockWriteGuard<'static, Guards> {
|
||||
let mut g = match GUARDS.try_write() {
|
||||
Ok(g) => g,
|
||||
// (Held by this, whoever panicked with it: not to be asked for again.)
|
||||
Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(),
|
||||
Err(std::sync::TryLockError::WouldBlock) => waited(3, || GUARDS.lock().unwrap_or_else(|e| e.into_inner())),
|
||||
Err(std::sync::TryLockError::WouldBlock) => waited(3, || GUARDS.write().unwrap_or_else(|e| e.into_inner())),
|
||||
};
|
||||
settle(&mut g);
|
||||
g
|
||||
}
|
||||
|
||||
/// Shared read lock on the guards, settling any unlocked faults first.
|
||||
fn guards_read() -> std::sync::RwLockReadGuard<'static, Guards> {
|
||||
if TO_SETTLE.load(std::sync::atomic::Ordering::SeqCst) != 0 {
|
||||
drop(guards());
|
||||
}
|
||||
match GUARDS.try_read() {
|
||||
Ok(g) => g,
|
||||
Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(),
|
||||
Err(std::sync::TryLockError::WouldBlock) => waited(3, || GUARDS.read().unwrap_or_else(|e| e.into_inner())),
|
||||
}
|
||||
}
|
||||
|
||||
/// The 4 KiB pages of title memory that what shaders wrote was put into,
|
||||
/// and the frame it last was: in pieces made when first needed and never let
|
||||
/// go. Some 20,000 pages a frame are noted by the relay and the landing
|
||||
@@ -1964,7 +2030,7 @@ pub(crate) fn written_since_taken(at: usize, len: usize) -> bool {
|
||||
if len == 0 {
|
||||
return false;
|
||||
}
|
||||
let g = guards();
|
||||
let g = guards_read();
|
||||
(at / BLOCK..=(at + len - 1) / BLOCK)
|
||||
.all(|b| !g.clean.contains(b) && !g.absent.contains(b) && !g.protected.contains(b))
|
||||
}
|
||||
@@ -1984,7 +2050,7 @@ fn guard(start: usize, end: usize) -> Option<Vec<usize>> {
|
||||
let (first, last) = (start / BLOCK, end.div_ceil(BLOCK));
|
||||
let known = !*ASK.get_or_init(|| std::env::var_os("VICTORY_GUARD_ASK").is_some_and(|v| v == "1"))
|
||||
&& (PROTECTED.any(first, first + 1) || {
|
||||
let g = guards_timed();
|
||||
let g = guards_read_timed();
|
||||
g.protected.contains(first) || g.absent.contains(first)
|
||||
});
|
||||
if !known && !is_view(start, end) {
|
||||
@@ -1996,8 +2062,23 @@ fn guard(start: usize, end: usize) -> Option<Vec<usize>> {
|
||||
// other thread asks about it: `ASKING`); what the runtime tells meanwhile
|
||||
// takes the block out of the set, and it is looked at as any other.
|
||||
let unsure: Vec<usize> = {
|
||||
let g = guards_timed();
|
||||
if g.clean.range(first..last).count() == last - first && !BESIDE.any(first, last) && !unsettled(first, last) {
|
||||
let g = guards_read_timed();
|
||||
// Fast path under shared read lock: every block in `first..last` is
|
||||
// either clean (guarded, not BESIDE) or a known placeholder hole
|
||||
// (`absent & held`), and none is OPENING/TOLD, so no VirtualQuery or
|
||||
// write lock is needed.
|
||||
let mut all_clean_or_held = true;
|
||||
for word in first / 64..last.div_ceil(64) {
|
||||
let mask = in_word(word * 64, first, last);
|
||||
let ok = ((g.clean.word(word) & !BESIDE.word(word)) | (g.absent.word(word) & g.held.word(word)))
|
||||
& !OPENING.word(word)
|
||||
& !TOLD.word(word);
|
||||
if (!ok & mask) != 0 {
|
||||
all_clean_or_held = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if all_clean_or_held {
|
||||
// Nothing written, mapped or new since the range was last taken.
|
||||
#[cfg(feature = "diagnostics")]
|
||||
tally(3, 1);
|
||||
@@ -2544,12 +2625,15 @@ pub fn title_memory_written(at: usize, len: usize, pages: bool) {
|
||||
}
|
||||
}
|
||||
{
|
||||
let mut g = guards();
|
||||
let guarded = g.protected.range(at / BLOCK..end.div_ceil(BLOCK)).count() as u64;
|
||||
let guarded = {
|
||||
let g = guards_read();
|
||||
g.protected.range(at / BLOCK..end.div_ceil(BLOCK)).count() as u64
|
||||
};
|
||||
if pages {
|
||||
WRITTEN[2].fetch_add(guarded, Relaxed);
|
||||
}
|
||||
if mode & 1 != 0 && guarded != 0 {
|
||||
let mut g = guards();
|
||||
open(&mut g, at, end);
|
||||
}
|
||||
}
|
||||
@@ -2911,7 +2995,7 @@ fn allocation_end(at: usize, inside: usize) -> usize {
|
||||
|
||||
/// Tells what keeps track of what title memory shows; the guards, with no
|
||||
/// block of the range known unmapped.
|
||||
fn title_memory_changed(at: usize, len: usize) -> std::sync::MutexGuard<'static, Guards> {
|
||||
fn title_memory_changed(at: usize, len: usize) -> std::sync::RwLockWriteGuard<'static, Guards> {
|
||||
// A placed texture there arrives, goes or changes with no descriptor
|
||||
// written.
|
||||
crate::descriptor::host::remapped(at, len);
|
||||
|
||||
Reference in new issue
Block a user