use measureme::TimingGuard;
use std::{
error::Error,
path::PathBuf,
sync::atomic::{AtomicBool, Ordering},
thread::ThreadId,
};
#[cfg(not(windows))]
type Sink = measureme::MmapSerializationSink;
#[cfg(windows)]
type Sink = measureme::FileSerializationSink;
pub struct Guard<'guard> {
_inner: Option<TimingGuard<'guard, Sink>>,
}
pub struct Profiler {
profiler: measureme::Profiler<Sink>,
enabled: AtomicBool,
}
impl Profiler {
pub fn from_path(path: impl Into<PathBuf>) -> Result<Self, Box<dyn Error>> {
let path = path.into();
match path.parent() {
Some(parent) if !path.exists() => {
std::fs::create_dir_all(parent)?;
}
_ => {}
}
Ok(Self {
profiler: measureme::Profiler::new(path.as_ref())?,
enabled: AtomicBool::default(),
})
}
pub fn from_name(name: impl AsRef<str>) -> Result<Self, Box<dyn Error>> {
let path = format!("./trace/{}-{}", name.as_ref(), std::process::id());
Self::from_path(path)
}
pub fn enable(&self) {
self.enabled.store(true, Ordering::SeqCst);
}
pub fn disable(&self) {
self.enabled.store(false, Ordering::SeqCst);
}
#[allow(clippy::cast_possible_truncation)]
pub fn trace(&self, category: &str, label: &str) -> Guard<'_> {
if !self.enabled.load(Ordering::SeqCst) {
return Guard { _inner: None };
}
let kind = self.profiler.alloc_string(category);
let label = self.profiler.alloc_string(label);
let id = measureme::EventId::from_label(label);
let thread_id = current_thread_id() as u32;
let inner = self
.profiler
.start_recording_interval_event(kind, id, thread_id);
Guard {
_inner: Some(inner),
}
}
}
fn current_thread_id() -> u64 {
let tid = std::thread::current().id();
unsafe { std::mem::transmute::<ThreadId, u64>(tid) }
}