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
6 changes: 6 additions & 0 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4050,6 +4050,12 @@ fn late_line(frames: f64) {
{runs:.0} changed runs, {:.2} MB",
bytes / (1 << 20) as f64
);
let found = [0, 1].map(|i| crate::shadow::LOOKUPS[i].swap(0, Ordering::Relaxed) as f64 / frames);
tracing::info!(
"asks for a buffer's host copy per frame: {:.0} answered from the asking thread's memory of them, {:.0} from the tables (VICTORY_LOOKUP_CACHE)",
found[0],
found[1]
);
}
}

Expand Down
92 changes: 92 additions & 0 deletions crates/victory-gpu/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,7 @@ pub(crate) fn let_go_idle() {
idle.into_iter().filter_map(|key| map.remove(&key)).collect()
};
if !gone.is_empty() {
tables_changed();
let (went, bytes) = (gone.len(), gone.iter().map(|s| s.len).sum::<u64>());
crate::host::trail(|| {
format!("sweep: {went} host copies of windows of title memory let go ({} KiB), held by no list and no slot", bytes >> 10)
Expand Down Expand Up @@ -4075,6 +4076,7 @@ fn buffers() -> std::sync::MutexGuard<'static, BTreeMap<(u64, usize), Entry>> {

/// A title buffer exists at `start` (`len` bytes).
pub(crate) fn register(resource: usize, start: u64, len: u64) {
tables_changed();
buffers().insert(
(start, resource),
Entry {
Expand All @@ -4087,6 +4089,7 @@ pub(crate) fn register(resource: usize, start: u64, len: u64) {
/// The title released it; lists that still hold its copy keep it alive.
pub(crate) fn unregister(resource: usize, start: u64) {
buffers().remove(&(start, resource));
tables_changed();
}

/// The title buffer holding `address`, as (start, length), without making
Expand Down Expand Up @@ -4127,6 +4130,9 @@ pub(crate) fn in_place_let_go(at: u64, len: u64) -> u64 {
}
}
let n = gone.len() as u64;
if n > 0 {
tables_changed();
}
for shadow in gone {
crate::host::retire(shadow);
}
Expand Down Expand Up @@ -4175,7 +4181,90 @@ pub(crate) fn find_any(address: u64, len: u64) -> Option<Arc<Shadow>> {
lookup(address, len, true)
}

/// Counts the changes of the tables [`lookup`] reads that can change what an
/// ask finds: a buffer registered or released, a copy taken out of a table,
/// a window made. A thread's remembered answers ([`FOUND`]) are from one
/// count.
static TABLES_CHANGED: AtomicU64 = AtomicU64::new(0);

fn tables_changed() {
TABLES_CHANGED.fetch_add(1, Ordering::Release);
}

/// Since the last frame line: asks answered from a thread's own memory of
/// them, and asks that went to the tables.
pub(crate) static LOOKUPS: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2];

/// Whether a thread remembers what its asks found
/// (`VICTORY_LOOKUP_CACHE=0`: every ask goes to the tables, as before).
///
/// Every bind of a draw asks for the host copy of each buffer it reads:
/// under the tables' one lock, among the 64 buffers that start below the
/// address, then, for memory outside buffers, under the windows' lock. The
/// title's six recording threads spent 54 ms a frame there together in the
/// prologue cell (`VICTORY_PROFILE`), and asked for the same ranges again
/// and again. A thread now keeps what each ask (address, length, kind)
/// found, weakly (a copy nothing else holds can still be let go), for as
/// long as the tables have not changed ([`TABLES_CHANGED`]).
fn lookup_cache() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_LOOKUP_CACHE").is_none_or(|v| v != "0"))
}

/// The most asks a thread remembers before it starts again.
const FOUND_MOST: usize = 8192;

type Found = std::collections::HashMap<(u64, u64, bool), std::sync::Weak<Shadow>>;

thread_local! {
/// What this thread's asks found, and the count of table changes they
/// are from.
static FOUND: std::cell::RefCell<(u64, Found)> = std::cell::RefCell::new((u64::MAX, Found::new()));
}

fn lookup(address: u64, len: u64, any: bool) -> Option<Arc<Shadow>> {
if !lookup_cache() {
return lookup_tables(address, len, any);
}
let changes = TABLES_CHANGED.load(Ordering::Acquire);
let found = FOUND.with(|f| {
let f = &mut *f.borrow_mut();
if f.0 != changes {
f.1.clear();
f.0 = changes;
}
f.1.get(&(address, len, any)).and_then(std::sync::Weak::upgrade)
});
if let Some(shadow) = found {
LOOKUPS[0].fetch_add(1, Ordering::Relaxed);
if shadow.window {
// As `loose` notes an ask of a window it hands out.
shadow.used.store(crate::host::moment(), Ordering::Relaxed);
shadow.asked[0].fetch_min(address, Ordering::Relaxed);
shadow.asked[1].fetch_max(address.saturating_add(len.max(1)), Ordering::Relaxed);
if len <= 1 {
shadow.unsized_asks.fetch_add(1, Ordering::Relaxed);
}
}
return Some(shadow);
}
LOOKUPS[1].fetch_add(1, Ordering::Relaxed);
let shadow = lookup_tables(address, len, any)?;
FOUND.with(|f| {
let f = &mut *f.borrow_mut();
// (from the count read before the tables were: a change since
// clears it at the next ask)
if f.0 == changes {
if f.1.len() >= FOUND_MOST {
f.1.clear();
}
f.1.insert((address, len, any), Arc::downgrade(&shadow));
}
});
Some(shadow)
}

fn lookup_tables(address: u64, len: u64, any: bool) -> Option<Arc<Shadow>> {
let end = address.checked_add(len.max(1))?;
let mut map = buffers();
let key = map
Expand Down Expand Up @@ -4425,6 +4514,7 @@ fn loose(address: u64, end: u64, unsized_ask: bool, any: bool) -> Option<Arc<Sha
let shadow = Arc::new(Shadow::placed(placed, start, len, true));
note(&shadow);
map.insert((start, len), shadow.clone());
tables_changed();
return Some(shadow);
}
// In a GPU upload heap, if asked for and there is room: one buffer in
Expand All @@ -4439,6 +4529,7 @@ fn loose(address: u64, end: u64, unsized_ask: bool, any: bool) -> Option<Arc<Sha
let shadow = Arc::new(shadow);
note(&shadow);
map.insert((start, len), shadow.clone());
tables_changed();
return Some(shadow);
}
let (buffer, data) = host()?.shared_buffer(len)?;
Expand Down Expand Up @@ -4477,6 +4568,7 @@ fn loose(address: u64, end: u64, unsized_ask: bool, any: bool) -> Option<Arc<Sha
}
note(&shadow);
map.insert((start, len), shadow.clone());
tables_changed();
Some(shadow)
}

Expand Down