#[cfg(feature = "profiling")] use cpuprofiler::PROFILER;
use failure::Fallible;
use std::path::Path;
pub struct ScopedProfiler {}
impl ScopedProfiler {
#[cfg(not(feature = "profiling"))]
fn real_start<P: AsRef<Path>>(_path: P) -> Fallible<ScopedProfiler> {
Err(format_err!("Compile-time \"profiling\" feature not enabled"))
}
#[cfg(feature = "profiling")]
fn real_start<P: AsRef<Path>>(path: P) -> Fallible<ScopedProfiler> {
let path = path.as_ref();
let path_str = match path.to_str() {
Some(path_str) => path_str,
None => return Err(format_err!("Invalid path {}", path.display())),
};
let mut profiler = PROFILER.lock().unwrap();
info!("Starting CPU profiler and writing results to {}", path_str);
profiler.start(path_str.as_bytes()).unwrap();
Ok(ScopedProfiler {})
}
pub fn start<P: AsRef<Path>>(path: P) -> Fallible<ScopedProfiler> {
ScopedProfiler::real_start(path)
}
#[cfg(not(feature = "profiling"))]
fn real_stop(&mut self) {
}
#[cfg(feature = "profiling")]
fn real_stop(&mut self) {
let mut profiler = PROFILER.lock().unwrap();
profiler.stop().expect("Profiler apparently not active, but it must have been");
info!("CPU profiler stopped");
}
}
impl Drop for ScopedProfiler {
fn drop(&mut self) {
self.real_stop()
}
}