use std::cell::Cell;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use web_time::Instant;
const KEPT: usize = 4 * crate::submission_planner::MAX_BANDS as usize;
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct FrameMark {
pub time_ticks: i64,
pub frame_index: Option<u64>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SubmissionReport {
pub gpu_ms: f64,
pub frame: Option<FrameMark>,
pub effects: Vec<String>,
pub predicted_ms: Option<f64>,
}
#[derive(Debug)]
struct Entry {
submitted_at: Instant,
done_at: Option<Instant>,
frame: Option<FrameMark>,
effects: Vec<String>,
predicted_ms: Option<f64>,
}
#[derive(Debug, Default)]
struct ClockState {
first: u64,
recorded: u64,
completed: u64,
watched: Vec<(u64, PreviousCompletion)>,
entries: VecDeque<Entry>,
frame: Option<FrameMark>,
effects: Vec<String>,
}
#[derive(Debug, Default)]
pub(crate) struct SubmissionClock {
state: Mutex<ClockState>,
}
impl SubmissionClock {
fn state(&self) -> std::sync::MutexGuard<'_, ClockState> {
self.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub(crate) fn mark_frame(&self, frame: Option<FrameMark>) {
self.state().frame = frame;
}
pub(crate) fn note_effect(&self, effect: &crate::EffectId) {
let name = format!("{}@{}", effect.as_str(), effect.version());
let mut state = self.state();
if !state.effects.contains(&name) {
state.effects.push(name);
}
}
pub(crate) fn record(
&self,
at: Instant,
predicted_ms: Option<f64>,
watch_previous: bool,
) -> Recorded {
let mut state = self.state();
let queue_idle = state.completed == state.recorded;
let previous = (watch_previous && !queue_idle).then(|| {
let watch = PreviousCompletion::default();
let sequence = state.recorded - 1;
state.watched.push((sequence, watch.clone()));
watch
});
state.recorded += 1;
let effects = std::mem::take(&mut state.effects);
let frame = state.frame;
state.entries.push_back(Entry {
submitted_at: at,
done_at: None,
frame,
effects,
predicted_ms,
});
if state.entries.len() > KEPT {
state.entries.pop_front();
state.first += 1;
}
Recorded {
sequence: state.recorded - 1,
queue_idle,
previous,
}
}
pub(crate) fn complete(&self, sequence: u64, at: Instant, as_it_happens: bool) {
let mut state = self.state();
state.completed = state.completed.max(sequence + 1);
state.watched.retain(|(watched, completion)| {
if *watched > sequence {
return true;
}
completion.set(at, as_it_happens && *watched == sequence);
false
});
let Some(position) = sequence.checked_sub(state.first) else {
return;
};
let Ok(position) = usize::try_from(position) else {
return;
};
for entry in state.entries.iter_mut().take(position + 1) {
entry.done_at.get_or_insert(at);
}
}
fn spans(state: &ClockState, now: Instant) -> Vec<(f64, &Entry)> {
let mut spans = Vec::new();
let mut previous_done: Option<Instant> = None;
for entry in &state.entries {
let start =
previous_done.map_or(entry.submitted_at, |done| done.max(entry.submitted_at));
let Some(done) = entry.done_at else {
spans.push((milliseconds(now.saturating_duration_since(start)), entry));
break;
};
spans.push((milliseconds(done.saturating_duration_since(start)), entry));
previous_done = Some(done);
}
spans
}
pub(crate) fn longest_at(&self, now: Instant) -> Option<SubmissionReport> {
let state = self.state();
Self::spans(&state, now)
.into_iter()
.max_by(|(a, _), (b, _)| a.total_cmp(b))
.map(|(gpu_ms, entry)| SubmissionReport {
gpu_ms,
frame: entry.frame,
effects: entry.effects.clone(),
predicted_ms: entry.predicted_ms,
})
}
pub(crate) fn gpu_ms(&self, sequences: std::ops::Range<u64>) -> Option<f64> {
let state = self.state();
let first = state.first;
let spans = Self::spans(&state, Instant::now());
let mut total = 0.0;
let mut counted = 0;
for (position, (ms, entry)) in spans.into_iter().enumerate() {
let sequence = first + u64::try_from(position).unwrap_or(u64::MAX);
if sequences.contains(&sequence) {
entry.done_at?;
total += ms;
counted += 1;
}
}
(counted == sequences.end.saturating_sub(sequences.start)).then_some(total)
}
pub(crate) fn longest(&self) -> Option<SubmissionReport> {
self.longest_at(Instant::now())
}
pub(crate) fn hung(&self) -> Option<SubmissionReport> {
self.longest()
.filter(|report| report.gpu_ms >= f64::from(alpha2_gpu::hung_threshold_ms()))
}
}
#[derive(Clone, Debug)]
pub(crate) struct Recorded {
pub(crate) sequence: u64,
pub(crate) queue_idle: bool,
pub(crate) previous: Option<PreviousCompletion>,
}
impl Recorded {
pub(crate) fn started(&self, at: Instant) -> Option<Instant> {
if self.queue_idle {
return Some(at);
}
let (done, as_it_happens) = self.previous.as_ref()?.get()?;
as_it_happens.then(|| done.max(at))
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct PreviousCompletion(Arc<Mutex<Option<(Instant, bool)>>>);
impl PreviousCompletion {
fn set(&self, at: Instant, as_it_happens: bool) {
*self
.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some((at, as_it_happens));
}
fn get(&self) -> Option<(Instant, bool)> {
*self
.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
}
thread_local! {
static WAITING: Cell<Option<(usize, u64)>> = const { Cell::new(None) };
}
impl SubmissionClock {
fn address(self: &Arc<Self>) -> usize {
Arc::as_ptr(self) as usize
}
pub(crate) fn sees_as_it_happens(self: &Arc<Self>, sequence: u64) -> bool {
WAITING
.get()
.is_some_and(|(clock, waited)| clock == self.address() && sequence >= waited)
}
}
#[cfg(not(target_arch = "wasm32"))]
struct Waiting(Option<(usize, u64)>);
#[cfg(not(target_arch = "wasm32"))]
impl Waiting {
fn on(clock: usize, sequence: u64) -> Self {
Self(WAITING.replace(Some((clock, sequence))))
}
}
#[cfg(not(target_arch = "wasm32"))]
impl Drop for Waiting {
fn drop(&mut self) {
WAITING.set(self.0);
}
}
#[derive(Clone, Debug)]
#[cfg_attr(
target_arch = "wasm32",
expect(dead_code, reason = "a browser cannot wait for a submission")
)]
pub(crate) struct ClockedSubmission {
pub(crate) index: wgpu::SubmissionIndex,
clock: Arc<SubmissionClock>,
sequence: u64,
}
impl ClockedSubmission {
pub(crate) const fn new(
index: wgpu::SubmissionIndex,
clock: Arc<SubmissionClock>,
sequence: u64,
) -> Self {
Self {
index,
clock,
sequence,
}
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn wait(
&self,
gpu: &alpha2_gpu::GpuContext,
timeout: std::time::Duration,
) -> Result<(), crate::RenderError> {
gpu.device()
.poll(wgpu::PollType::Poll)
.map_err(crate::error::poll_error)?;
let _waiting = Waiting::on(self.clock.address(), self.sequence);
gpu.device()
.poll(wgpu::PollType::Wait {
submission_index: Some(self.index.clone()),
timeout: Some(timeout),
})
.map_err(crate::error::poll_error)?;
Ok(())
}
}
fn milliseconds(duration: std::time::Duration) -> f64 {
duration.as_secs_f64() * 1_000.0
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
#[test]
fn gpu_time_excludes_queueing_behind_earlier_submissions() {
let clock = SubmissionClock::default();
let start = Instant::now();
let at = |ms: u64| start + Duration::from_millis(ms);
let first = clock.record(at(0), None, false).sequence;
let second = clock.record(at(0), None, false).sequence;
let third = clock.record(at(0), None, false).sequence;
clock.complete(first, at(10), false);
clock.complete(second, at(400), false);
clock.complete(third, at(405), false);
let longest = clock.longest_at(at(1_000)).expect("submissions");
assert!((longest.gpu_ms - 390.0).abs() < 0.5, "{longest:?}");
let fourth = clock.record(at(2_000), None, false).sequence;
clock.complete(fourth, at(2_005), false);
assert!((clock.longest_at(at(3_000)).expect("kept").gpu_ms - 390.0).abs() < 0.5);
let _ = clock.record(at(3_000), None, false).sequence;
assert!((clock.longest_at(at(3_700)).expect("running").gpu_ms - 700.0).abs() < 0.5);
}
#[test]
fn a_completion_finishes_every_earlier_submission() {
let clock = SubmissionClock::default();
let start = Instant::now();
let first = clock.record(start, None, false).sequence;
let second = clock.record(start, None, false).sequence;
clock.complete(second, start + Duration::from_millis(8), false);
let _ = first;
assert_eq!(
clock
.longest_at(start + Duration::from_secs(5))
.map(|r| r.gpu_ms.round()),
Some(8.0)
);
}
#[test]
fn a_frames_gpu_time_survives_completions_seen_together() {
let clock = SubmissionClock::default();
let start = Instant::now();
let at = |ms: u64| start + Duration::from_millis(ms);
let earlier = clock.record(at(0), None, false).sequence;
clock.complete(earlier, at(5), false);
let first = clock.record(at(10), Some(100.0), false).sequence;
let second = clock.record(at(10), Some(100.0), false).sequence;
assert_eq!(clock.gpu_ms(first..second + 1), None, "not finished");
clock.complete(second, at(210), false);
let total = clock.gpu_ms(first..second + 1).expect("finished");
assert!((total - 200.0).abs() < 0.5, "{total}");
assert_eq!(
clock
.longest_at(at(300))
.and_then(|report| report.predicted_ms),
Some(100.0)
);
}
#[test]
fn the_clock_keeps_the_frame_and_effects_of_each_submission() {
let clock = SubmissionClock::default();
let start = Instant::now();
clock.mark_frame(Some(FrameMark {
time_ticks: 4_000,
frame_index: Some(1),
}));
clock.note_effect(&crate::EffectId::from_static("blur", 2));
let heavy = clock.record(start, None, false).sequence;
clock.complete(heavy, start + Duration::from_millis(30), false);
let light = clock
.record(start + Duration::from_millis(40), None, false)
.sequence;
clock.complete(light, start + Duration::from_millis(41), false);
let report = clock
.longest_at(start + Duration::from_millis(50))
.expect("kept");
assert_eq!(report.effects, ["blur@2"]);
assert_eq!(report.frame.and_then(|frame| frame.frame_index), Some(1));
}
#[test]
fn a_submission_starts_on_an_idle_queue_only_once_every_earlier_one_completed() {
let clock = SubmissionClock::default();
let start = Instant::now();
let first = clock.record(start, None, false);
assert!(first.queue_idle);
let second = clock.record(start, None, false);
assert!(!second.queue_idle);
clock.complete(first.sequence, start, false);
assert!(!clock.record(start, None, false).queue_idle);
clock.complete(second.sequence + 1, start, false);
assert!(clock.record(start, None, false).queue_idle);
}
#[test]
fn only_completions_inside_a_wait_on_them_or_earlier_ones_are_seen_as_they_happen() {
let clock = Arc::new(SubmissionClock::default());
let other = Arc::new(SubmissionClock::default());
assert!(!clock.sees_as_it_happens(0));
WAITING.set(Some((clock.address(), 5)));
assert!(!clock.sees_as_it_happens(4));
assert!(clock.sees_as_it_happens(5));
assert!(clock.sees_as_it_happens(6));
assert!(!other.sees_as_it_happens(6));
WAITING.set(None);
}
#[test]
fn a_frame_behind_a_completion_seen_as_it_happened_starts_at_that_completion() {
let clock = SubmissionClock::default();
let start = Instant::now();
let at = |ms: u64| start + Duration::from_millis(ms);
let first = clock.record(at(0), None, false);
assert_eq!(first.started(at(0)), Some(at(0)));
let behind = clock.record(at(1), None, true);
let late = clock.record(at(2), None, true);
assert_eq!(behind.started(at(1)), None, "not complete yet");
clock.complete(first.sequence, at(10), true);
clock.complete(behind.sequence, at(30), false);
assert_eq!(behind.started(at(1)), Some(at(10)));
assert_eq!(late.started(at(2)), None, "seen late");
clock.complete(late.sequence, at(35), true);
assert!(clock.record(at(40), None, true).previous.is_none());
}
}