use std::sync::OnceLock; use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; use std::time::Instant; const SLOTS: usize = 8; const LAG: u64 = 3; const FLIGHTS: usize = 512; const NONE: u64 = u64::MAX; #[derive(Default)] struct Slot { frame: AtomicU64, start: AtomicU64, present_call: AtomicU64, gate_us: AtomicU64, next_began: AtomicU64, submit_first: AtomicU64, submit_last: AtomicU64, submits: AtomicU64, go_first: AtomicU64, go_last: AtomicU64, go_busy_us: AtomicU64, go_jobs: AtomicU64, idle_empty_us: AtomicU64, idle_standing_us: AtomicU64, sent: AtomicU64, card_first: AtomicU64, card_last: AtomicU64, flights: AtomicU64, told_first: AtomicU64, told_last: AtomicU64, told: AtomicU64, told_lag_us: AtomicU64, } impl Slot { fn reset(&self, frame: u64, start: u64) { for (field, value) in [ (&self.present_call, 0), (&self.gate_us, 0), (&self.next_began, NONE), (&self.submit_first, NONE), (&self.submit_last, 0), (&self.submits, 0), (&self.go_first, NONE), (&self.go_last, 0), (&self.go_busy_us, 0), (&self.go_jobs, 0), (&self.idle_empty_us, 0), (&self.idle_standing_us, 0), (&self.sent, 0), (&self.card_first, NONE), (&self.card_last, 0), (&self.flights, 0), (&self.told_first, NONE), (&self.told_last, 0), (&self.told, 0), (&self.told_lag_us, 0), ] { field.store(value, Relaxed); } self.start.store(start, Relaxed); self.frame.store(frame, Relaxed); } } static TABLE: [Slot; SLOTS] = [const { Slot { frame: AtomicU64::new(0), start: AtomicU64::new(0), present_call: AtomicU64::new(0), gate_us: AtomicU64::new(0), next_began: AtomicU64::new(NONE), submit_first: AtomicU64::new(NONE), submit_last: AtomicU64::new(0), submits: AtomicU64::new(0), go_first: AtomicU64::new(NONE), go_last: AtomicU64::new(0), go_busy_us: AtomicU64::new(0), go_jobs: AtomicU64::new(0), idle_empty_us: AtomicU64::new(0), idle_standing_us: AtomicU64::new(0), sent: AtomicU64::new(0), card_first: AtomicU64::new(NONE), card_last: AtomicU64::new(0), flights: AtomicU64::new(0), told_first: AtomicU64::new(NONE), told_last: AtomicU64::new(0), told: AtomicU64::new(0), told_lag_us: AtomicU64::new(0), } }; SLOTS]; static SUBMIT_FRAME: AtomicU64 = AtomicU64::new(1); static FLIGHT_FRAMES: [(AtomicU64, AtomicU64); FLIGHTS] = [const { (AtomicU64::new(0), AtomicU64::new(0)) }; FLIGHTS]; static LAST_LANDED_FRAME: AtomicU64 = AtomicU64::new(0); #[cfg(windows)] pub(crate) fn on() -> bool { cfg!(feature = "diagnostics") && crate::asked_for!("EONACAT_XS_FRAME_CHAIN") } #[cfg(not(windows))] pub(crate) fn on() -> bool { false } fn epoch() -> Instant { static EPOCH: OnceLock = OnceLock::new(); *EPOCH.get_or_init(Instant::now) } fn now() -> u64 { epoch().elapsed().as_micros() as u64 + 1 } fn slot(frame: u64) -> Option<&'static Slot> { let slot = &TABLE[(frame as usize) % SLOTS]; (frame != 0 && slot.frame.load(Relaxed) == frame).then_some(slot) } pub(crate) fn present_called(frame: u64) { if !on() { return; } let t = now(); if let Some(s) = slot(frame) { s.present_call.store(t, Relaxed); } TABLE[((frame + 1) as usize) % SLOTS].reset(frame + 1, t); if frame > LAG { write(frame - LAG); } } pub(crate) fn present_gate(frame: u64, us: u64) { if !on() { return; } if let Some(s) = slot(frame) { s.gate_us.store(us, Relaxed); } } pub(crate) fn origin(presented: u64) { if !on() { return; } if let Some(s) = slot(presented) { s.next_began.fetch_min(now(), Relaxed); } } pub(crate) fn title_submit(presented: u64) { if !on() { return; } if let Some(s) = slot(presented + 1) { let t = now(); s.submit_first.fetch_min(t, Relaxed); s.submit_last.fetch_max(t, Relaxed); s.submits.fetch_add(1, Relaxed); } } pub(crate) fn submit_frame() -> u64 { SUBMIT_FRAME.load(Relaxed) } pub(crate) fn job(began: u64, present: Option) { if !on() { return; } let t = now(); let frame = present.unwrap_or_else(submit_frame); if let Some(s) = slot(frame) { s.go_first.fetch_min(began, Relaxed); s.go_last.fetch_max(t, Relaxed); s.go_busy_us.fetch_add(t.saturating_sub(began), Relaxed); s.go_jobs.fetch_add(1, Relaxed); if present.is_some() { s.sent.store(t, Relaxed); } } if let Some(frame) = present { SUBMIT_FRAME.fetch_max(frame + 1, Relaxed); } } pub(crate) fn stamp() -> u64 { if on() { now() } else { 0 } } pub(crate) fn submit_idle(empty: bool, us: u64) { if !on() { return; } if let Some(s) = slot(submit_frame()) { let field = if empty { &s.idle_empty_us } else { &s.idle_standing_us }; field.fetch_add(us, Relaxed); } } pub(crate) fn flight(fence: u64) { if !on() { return; } let (at, frame) = &FLIGHT_FRAMES[(fence as usize) % FLIGHTS]; frame.store(submit_frame(), Relaxed); at.store(fence, Relaxed); } fn frame_of(fence: u64) -> Option { let (at, frame) = &FLIGHT_FRAMES[(fence as usize) % FLIGHTS]; (at.load(Relaxed) == fence).then(|| frame.load(Relaxed)) } pub(crate) fn card_done(fence: u64) -> u64 { if !on() { return 0; } let Some(frame) = frame_of(fence) else { return LAST_LANDED_FRAME.load(Relaxed); }; LAST_LANDED_FRAME.store(frame, Relaxed); if let Some(s) = slot(frame) { let t = now(); s.card_first.fetch_min(t, Relaxed); s.card_last.fetch_max(t, Relaxed); s.flights.fetch_add(1, Relaxed); } frame } pub(crate) fn landed_frame() -> u64 { LAST_LANDED_FRAME.load(Relaxed) } pub(crate) fn told(frame: u64, lag_us: u64) { if !on() { return; } if let Some(s) = slot(frame) { let t = now(); s.told_first.fetch_min(t, Relaxed); s.told_last.fetch_max(t, Relaxed); s.told.fetch_add(1, Relaxed); s.told_lag_us.fetch_max(lag_us, Relaxed); } } fn ms(at: u64, start: u64) -> String { if at == 0 || at == NONE { "-".into() } else { format!("{:+.1}", (at as f64 - start as f64) / 1000.0) } } #[derive(Debug, Clone, Copy, PartialEq)] struct Read { frame: u64, start: u64, present_call: u64, gate_us: u64, next_began: u64, submit: (u64, u64, u64), go: (u64, u64, u64, u64), idle: (u64, u64), sent: u64, card: (u64, u64, u64), told: (u64, u64, u64, u64), } fn read(s: &Slot) -> Read { let l = |a: &AtomicU64| a.load(Relaxed); Read { frame: l(&s.frame), start: l(&s.start), present_call: l(&s.present_call), gate_us: l(&s.gate_us), next_began: l(&s.next_began), submit: (l(&s.submit_first), l(&s.submit_last), l(&s.submits)), go: (l(&s.go_first), l(&s.go_last), l(&s.go_busy_us), l(&s.go_jobs)), idle: (l(&s.idle_empty_us), l(&s.idle_standing_us)), sent: l(&s.sent), card: (l(&s.card_first), l(&s.card_last), l(&s.flights)), told: (l(&s.told_first), l(&s.told_last), l(&s.told), l(&s.told_lag_us)), } } fn line(r: &Read) -> String { let s = r.start; let length = if r.present_call > s { format!("{:.1} ms", (r.present_call - s) as f64 / 1000.0) } else { "-".into() }; format!( "frame chain: frame {} ({length} from the present before to its own): the title submitted {} times {}..{}, called \ present {} and stood {:.1} ms at the line's gate, began the next frame {}; our submitting thread {}..{}, busy {:.1} ms \ in {} jobs, stood {:.1} ms with the line empty and {:.1} ms at a head not free to go, sent the present {}; the card \ ran {} flights, seen done {}..{}; the title told {} times {}..{}, at most {:.1} ms after the relay took the flight", r.frame, r.submit.2, ms(r.submit.0, s), ms(r.submit.1, s), ms(r.present_call, s), r.gate_us as f64 / 1000.0, ms(r.next_began, s), ms(r.go.0, s), ms(r.go.1, s), r.go.2 as f64 / 1000.0, r.go.3, r.idle.0 as f64 / 1000.0, r.idle.1 as f64 / 1000.0, ms(r.sent, s), r.card.2, ms(r.card.0, s), ms(r.card.1, s), r.told.2, ms(r.told.0, s), ms(r.told.1, s), r.told.3 as f64 / 1000.0, ) } fn write(frame: u64) { let Some(s) = slot(frame) else { return; }; tracing::info!("{}", line(&read(s))); }