use std::collections::{HashMap, VecDeque}; use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; use std::sync::{Mutex, OnceLock}; pub(crate) const COMPUTE: u32 = 2; #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) enum Engine { Direct = 0, Compute = 1, } pub(crate) const READ: u8 = 1; pub(crate) const STATE: u8 = 2; pub(crate) const TWINS: u8 = 4; pub(crate) const NOW: u8 = 8; pub(crate) const COMPUTE_STATES: u32 = 0x1 | 0x8 | 0x40 | 0x200 | 0x400 | 0x800 | 0x40_0000; const PIXEL_READ: u32 = 0x80; pub(crate) fn holds_of(first: u32, last: u32) -> u8 { let drawing = (first | last) & !COMPUTE_STATES; if drawing == 0 { 0 } else if drawing == PIXEL_READ { READ } else { STATE } } #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) enum Why { Card = 0, Steps, Texture, Value, Tiles, Handoff, Ours, } const WHYS: usize = 7; pub(crate) struct Ask<'a> { pub(crate) number: u64, pub(crate) title: Option, pub(crate) holds: u8, pub(crate) card: bool, pub(crate) steps: bool, pub(crate) tiles: bool, pub(crate) textures: &'a [usize], pub(crate) after: u64, } #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub(crate) struct Placed { pub(crate) engine: Engine, pub(crate) value: u64, pub(crate) wait: Option<(u64, Why)>, } type Sent = (u64, Engine, u64); const REMEMBERED: usize = 4096; #[derive(Default)] pub(crate) struct Plan { sent: [u64; 2], waited: [u64; 2], last: [Option; 2], card: [Option; 2], textures: HashMap, recent: VecDeque, } impl Plan { pub(crate) fn place(&mut self, ask: &Ask, finished: u64) -> Placed { let engine = match ask.title { Some(COMPUTE) if ask.holds & (STATE | TWINS) == 0 => Engine::Compute, _ => Engine::Direct, }; let (q, o) = (engine as usize, 1 - engine as usize); let mut need: Option<(u64, Why)> = None; let mut behind = |dep: Option, why: Why| { if let Some((number, on, value)) = dep && on != engine && number > finished && need.is_none_or(|(v, _)| value > v) { need = Some((value, why)); } }; if ask.title.is_none() { behind(self.last[o], Why::Ours); } if ask.steps { behind(self.last[o], Why::Steps); } if ask.tiles { behind(self.last[o], Why::Tiles); } if ask.card { behind(self.card[o], Why::Card); } for t in ask.textures { behind(self.textures.get(t).copied(), Why::Texture); } if ask.after > finished { let at = self.recent.partition_point(|s| s.0 < ask.after); behind(self.recent.get(at).copied().filter(|s| s.0 == ask.after), Why::Value); } if engine == Engine::Compute && ask.holds & NOW != 0 { self.sent[Engine::Direct as usize] += 1; let handoff = (u64::MAX, Engine::Direct, self.sent[Engine::Direct as usize]); self.last[Engine::Direct as usize] = Some(handoff); behind(Some(handoff), Why::Handoff); } let wait = need.filter(|&(value, _)| value > self.waited[q]); if let Some((value, _)) = wait { self.waited[q] = value; } self.sent[q] += 1; let sent = (ask.number, engine, self.sent[q]); self.last[q] = Some(sent); if ask.card { self.card[q] = Some(sent); } for &t in ask.textures { self.textures.insert(t, sent); } if self.textures.len() > 4 * REMEMBERED { self.textures.retain(|_, s| s.0 > finished); } self.recent.push_back(sent); if self.recent.len() > REMEMBERED { self.recent.pop_front(); } Placed { engine, value: self.sent[q], wait, } } } pub(crate) fn on() -> bool { static ON: OnceLock = OnceLock::new(); *ON.get_or_init(|| match std::env::var("EONACAT_XS_ASYNC_QUEUE").as_deref() { Ok("plan") => { tracing::info!("submissions are planned for a compute queue beside the direct queue (EONACAT_XS_ASYNC_QUEUE=plan); everything runs as before"); true } Ok("1") => { tracing::warn!( "EONACAT_XS_ASYNC_QUEUE=1: the title's compute work on a host compute queue is not built (its lists are recorded as \ direct lists); planned only, as with EONACAT_XS_ASYNC_QUEUE=plan, and everything runs on the direct queue" ); true } _ => false, }) } thread_local! { static TITLE: std::cell::Cell> = const { std::cell::Cell::new(None) }; } pub(crate) fn for_title(kind: Option, after: u64, send: impl FnOnce() -> R) -> R { let was = TITLE.replace(kind.map(|k| (k, after))); let sent = send(); TITLE.set(was); sent } #[cfg(feature = "diagnostics")] pub(crate) fn title_kind() -> Option { TITLE.get().map(|t| t.0) } static AFTER: Mutex> = Mutex::new(Vec::new()); pub(crate) fn value_passed(queue: usize, wait: &crate::list::ValueWait) { if !on() { return; } let Some(by) = super::late_met_by(wait) else { return; }; let mut after = AFTER.lock().unwrap_or_else(|e| e.into_inner()); match after.iter_mut().find(|a| a.0 == queue) { Some(a) => a.1 = a.1.max(by), None => after.push((queue, by)), } } pub(crate) fn take_after(queue: usize) -> u64 { if !on() { return 0; } let mut after = AFTER.lock().unwrap_or_else(|e| e.into_inner()); match after.iter().position(|a| a.0 == queue) { Some(i) => after.swap_remove(i).1, None => 0, } } static TILES: AtomicU64 = AtomicU64::new(0); pub(crate) fn tiles_mapped() { TILES.fetch_add(1, Relaxed); } pub(crate) struct Probe { title: Option<(u32, u64)>, holds: u8, textures: Vec, tiles: u64, } pub(crate) fn probe(runs: &[&super::HostList]) -> Probe { let mut holds = 0; let mut textures = Vec::new(); for list in runs { let states = list.states.lock().unwrap_or_else(|e| e.into_inner()); for (key, (first, last, copy)) in states.iter() { textures.push(*key); holds |= holds_of(*first, *last); if copy.state.load(Relaxed) & !COMPUTE_STATES != 0 { holds |= NOW; } } drop(states); if !list.twins.lock().unwrap_or_else(|e| e.into_inner()).is_empty() { holds |= TWINS; } } textures.sort_unstable(); textures.dedup(); Probe { title: TITLE.get(), holds, textures, tiles: TILES.load(Relaxed), } } static PLAN: Mutex> = Mutex::new(None); static STATS: [AtomicU64; 12 + WHYS] = [const { AtomicU64::new(0) }; 12 + WHYS]; const LISTS: usize = 4; const HELD: usize = 5; const PLACED: usize = 9; const FREE: usize = 10; const COMPUTE_WAITS: usize = 11; const WAITS: usize = 12; pub(crate) fn place(probe: Probe, number: u64, card: bool, steps: bool, lists: usize, finished: u64) { let title = probe.title.map(|t| t.0); let ask = Ask { number, title, holds: probe.holds, card, steps, tiles: TILES.load(Relaxed) != probe.tiles, textures: &probe.textures, after: probe.title.map_or(0, |t| t.1), }; let placed = { let mut plan = PLAN.lock().unwrap_or_else(|e| e.into_inner()); plan.get_or_insert_with(Plan::default).place(&ask, finished) }; let by = match title { Some(0) => 0, Some(COMPUTE) => 1, Some(3) => 2, _ => 3, }; STATS[by].fetch_add(1, Relaxed); if title == Some(COMPUTE) { STATS[LISTS].fetch_add(lists as u64, Relaxed); for (i, bit) in [READ, STATE, TWINS, NOW].into_iter().enumerate() { if ask.holds & bit != 0 { STATS[HELD + i].fetch_add(1, Relaxed); } } } if placed.engine == Engine::Compute { STATS[PLACED].fetch_add(1, Relaxed); if placed.wait.is_none() { STATS[FREE].fetch_add(1, Relaxed); } } if let Some((_, why)) = placed.wait { STATS[WAITS + why as usize].fetch_add(1, Relaxed); if placed.engine == Engine::Compute { STATS[COMPUTE_WAITS].fetch_add(1, Relaxed); } } } pub(crate) fn line(frames: f64) { if !on() { return; } let s: Vec = STATS.iter().map(|s| s.swap(0, Relaxed) as f64 / frames).collect(); let waits: f64 = s[WAITS..].iter().sum(); tracing::info!( "two host queues, planned per frame (EONACAT_XS_ASYNC_QUEUE; everything runs on the direct queue): submissions of the \ title's direct queue {:.1}, of its compute queues {:.1} ({:.1} lists), of its copy queues {:.1}, ours or not known {:.1}; \ of the compute queues' submissions, {:.1} read a texture in both read states (a compute list would name the first \ alone), {:.1} begin or end a texture in a drawing state and {:.1} copy depth twins (both stay on the direct queue), \ {:.1} name a texture in a drawing state now (moved on the direct queue first); {:.1} would go to a compute queue, \ {:.1} of them without a wait; waits between the queues {waits:.1}, {:.1} of them by the compute queue: for copies in \ the card's memory {:.1}, steps on the card {:.1}, textures {:.1}, memory values a flight of the other queue writes \ {:.1}, tile mappings {:.1}, a texture's state moved first {:.1}, ours {:.1}", s[0], s[1], s[LISTS], s[2], s[3], s[HELD], s[HELD + 1], s[HELD + 2], s[HELD + 3], s[PLACED], s[FREE], s[COMPUTE_WAITS], s[WAITS + Why::Card as usize], s[WAITS + Why::Steps as usize], s[WAITS + Why::Texture as usize], s[WAITS + Why::Value as usize], s[WAITS + Why::Tiles as usize], s[WAITS + Why::Handoff as usize], s[WAITS + Why::Ours as usize], ); }