cubecl_runtime/
kernel_timestamps.rs

1use cubecl_common::benchmark::ProfileDuration;
2use std::time::Instant;
3
4#[derive(Debug, Default)]
5/// A simple struct to keep track of timestamps for kernel execution.
6/// This should be used for servers that do not have native device profiling.
7pub struct KernelTimestamps {
8    start: Option<Instant>,
9}
10
11impl KernelTimestamps {
12    /// Start measuring
13    pub fn start(&mut self) {
14        if self.start.is_some() {
15            panic!("Recursive kernel timestamps are not supported.");
16        }
17        self.start = Some(std::time::Instant::now());
18    }
19
20    /// Stop measuring
21    pub fn stop(&mut self) -> ProfileDuration {
22        ProfileDuration::from_duration(
23            self.start
24                .take()
25                .expect("Stopped timestamp before starting one.")
26                .elapsed(),
27        )
28    }
29}