use std::sync::Arc;
use crate::{Error, Result, Timestamp};
use super::{Device, Engine, Event, TimestampPair};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TimerDomain {
#[default]
Auto,
Host,
Device,
}
pub struct Timer {
domain: TimerDomain,
name: String,
host_start: Option<Timestamp>,
host_end: Option<Timestamp>,
host_running: bool,
host_complete: bool,
device_pair: Option<TimestampPair>,
device_event: Option<Event>,
last_milliseconds: f64,
last_units: f64,
}
impl Timer {
pub fn new(domain: TimerDomain, name: impl Into<String>) -> Self {
Self {
domain,
name: name.into(),
host_start: None,
host_end: None,
host_running: false,
host_complete: false,
device_pair: None,
device_event: None,
last_milliseconds: 0.0,
last_units: 1.0,
}
}
pub fn init(&mut self, engine: &Engine, name: impl Into<String>) -> Result<()> {
let name = name.into();
if !name.is_empty() {
self.name = name;
}
if self.domain == TimerDomain::Auto {
self.domain = TimerDomain::Device;
}
if self.domain == TimerDomain::Host {
return Ok(());
}
self.device_pair = Some(engine.device_timestamp_pair()?);
self.device_event = None;
Ok(())
}
pub fn begin(&mut self) -> Result<()> {
if self.domain == TimerDomain::Auto {
self.domain = TimerDomain::Host;
}
if self.domain != TimerDomain::Host {
return Err(Error::failed_precondition(
"device timer regions are recorded by Engine::checkpoint_with_timer",
));
}
if self.host_running {
return Err(Error::failed_precondition(
"host timer begin requires the previous region to end",
));
}
self.host_start = Some(Timestamp::now());
self.host_running = true;
self.host_complete = false;
Ok(())
}
pub fn end(&mut self) -> Result<()> {
if self.domain != TimerDomain::Host || !self.host_running {
return Err(Error::failed_precondition(
"host timer end requires a matching begin",
));
}
self.host_end = Some(Timestamp::now());
self.host_running = false;
self.host_complete = true;
Ok(())
}
pub fn commit(&mut self, units: f64) -> Result<f64> {
self.last_milliseconds = match self.domain {
TimerDomain::Device => {
let pair = self.device_pair.as_ref().ok_or_else(|| {
Error::failed_precondition("device timer commit requires init and a submitted region")
})?;
let event = self.device_event.as_ref().ok_or_else(|| {
Error::failed_precondition("device timer commit requires a submitted region")
})?;
if !event.is_complete()? {
return Err(Error::not_ready("device timer submission is still pending"));
}
pair.duration()?.as_secs_f64() * 1e3
}
TimerDomain::Host => {
if !self.host_complete {
return Err(Error::failed_precondition(
"host timer commit requires a completed begin/end region",
));
}
self.host_complete = false;
(self.host_end.expect("complete region has an end")
- self.host_start.expect("complete region has a start"))
.to_ms()
}
TimerDomain::Auto => {
return Err(Error::failed_precondition("timer has no measured region"));
}
};
self.last_units = units;
Ok(self.last_milliseconds)
}
pub const fn elapsed_milliseconds(&self) -> f64 {
self.last_milliseconds
}
pub const fn throughput(&self) -> f64 {
if self.last_milliseconds <= 0.0 {
return 0.0;
}
self.last_units / (self.last_milliseconds / 1000.0)
}
pub const fn domain(&self) -> TimerDomain {
self.domain
}
pub const fn is_host(&self) -> bool {
matches!(self.domain, TimerDomain::Host)
}
pub const fn is_device(&self) -> bool {
matches!(self.domain, TimerDomain::Device)
}
pub fn is_initialized(&self) -> bool {
match self.domain {
TimerDomain::Host => true,
TimerDomain::Device => self.device_pair.is_some(),
TimerDomain::Auto => false,
}
}
pub fn is_pending(&self) -> bool {
match self.domain {
TimerDomain::Host => self.host_running,
TimerDomain::Device => self
.device_event
.as_ref()
.is_some_and(|event| !event.is_complete().unwrap_or(true)),
TimerDomain::Auto => false,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub(in crate::runtime) fn prepare_device_submit(&mut self, device: &Arc<Device>) -> Result<()> {
if self.domain != TimerDomain::Device {
return Err(Error::failed_precondition(
"engine submission requires an initialized device timer",
));
}
let pair = self.device_pair.as_ref().ok_or_else(|| {
Error::failed_precondition("device timer requires init before timed submission")
})?;
if !pair.belongs_to(device) {
return Err(Error::invalid_argument(
"device timer was initialized by another engine",
));
}
if self.is_pending() {
return Err(Error::failed_precondition(
"device timer is still pending; commit the previous region before resubmitting",
));
}
Ok(())
}
pub(in crate::runtime) fn device_pair(&self) -> Option<&TimestampPair> {
self.device_pair.as_ref()
}
pub(in crate::runtime) fn attach_event(&mut self, event: Event) {
self.device_event = Some(event);
}
}
#[cfg(test)]
#[path = "../../../test/rs/runtime/timer_unit.rs"]
mod tests;