use std::sync::OnceLock; use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; pub(crate) fn on() -> bool { static ON: OnceLock = OnceLock::new(); *ON.get_or_init(|| std::env::var_os("EONACAT_XS_FETCH_STEPDOWN").is_none_or(|v| v != "0")) } #[allow(dead_code)] pub(crate) fn sizes(total: u64, tight: bool) -> Vec { let mut sizes = Vec::with_capacity(3); if !tight { sizes.push(total.next_multiple_of(16 << 20)); } sizes.push(total.next_multiple_of(1 << 20)); sizes.push(total.next_multiple_of(1 << 16).max(1 << 16)); sizes.dedup(); sizes } static COUNTS: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3]; #[allow(dead_code)] pub(crate) fn buffer( total: u64, tight: bool, mut make: impl FnMut(u64) -> Option, pause: impl FnOnce(), ) -> Option<(T, u64)> { if !on() { let size = total.next_multiple_of(16 << 20); return make(size).map(|b| (b, size)); } let tries = sizes(total, tight); let usual = tries[0]; for &size in &tries { if let Some(b) = make(size) { if size < usual { COUNTS[2].fetch_add(1, Relaxed); } return Some((b, size)); } } pause(); let least = tries[tries.len() - 1]; let b = make(least)?; COUNTS[2].fetch_add(1, Relaxed); Some((b, least)) } pub(crate) fn skipped(what: &str, total: u64, pieces: usize) { let n = COUNTS[0].fetch_add(1, Relaxed) + 1; let all = COUNTS[1].fetch_add(total, Relaxed) + total; if n == 1 || n.is_power_of_two() { tracing::error!( what, total, pieces, skipped = n, stale_bytes_so_far = all, "fetch memory: NO MEMORY TO FETCH what shaders wrote to the card's memory: the fetch is skipped and the title goes on; those bytes in title memory stay as they were until the next fetch of the pieces" ); } }