oni_trace/
scope.rs

1use time::precise_time_ns;
2use std::mem::{replace, zeroed};
3use crate::{
4    local::LOCAL,
5    trace::Args,
6};
7
8pub struct ScopeComplete {
9    name: &'static str,
10    args: Args,
11    start: u64,
12}
13
14impl ScopeComplete {
15    #[inline]
16    pub fn new(name: &'static str, args: Args) -> Self {
17        let start = precise_time_ns();
18        Self { name, args, start }
19    }
20    #[inline]
21    pub fn with_empty_args(name: &'static str) -> Self {
22        Self::new(name, Args::Empty)
23    }
24}
25
26impl Drop for ScopeComplete {
27    /// When the Scope is dropped it records the
28    /// length of time it was alive for and records it
29    /// against the Profiler.
30    fn drop(&mut self) {
31        let end = precise_time_ns();
32        let start = self.start;
33        let name = self.name;
34        let args = unsafe {
35            replace(&mut self.args, zeroed())
36        };
37
38        LOCAL.with(|profiler| match *profiler.borrow() {
39            Some(ref profiler) => profiler.complete(start, end, name.into(), None, args),
40            None => println!("ERROR: ProfileScope {} on unregistered thread!", name),
41        });
42    }
43}