From d621533f15871ce233e3a1dcc0fb10a631090678 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 26 Jun 2019 11:11:28 +0300 Subject: add cpuprofile to ra_prof Now, one can use `let _p = ra_prof::cpu_profiler()` to capture profile of a block of code. This is not an out of the box experience, as that relies on gperfools See the docs on https://github.com/AtheMathmo/cpuprofiler for more! --- crates/ra_prof/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'crates/ra_prof/src') diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index de67b4031..1e8d780ab 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs @@ -255,6 +255,39 @@ impl Drop for Scope { } } +/// A wrapper around https://github.com/AtheMathmo/cpuprofiler +/// +/// It can be used to capture sampling profiles of sections of code. +/// It is not exactly out-of-the-box, as it relies on gperftools. +/// See the docs for the crate for more! +#[derive(Debug)] +pub struct CpuProfiler { + _private: (), +} + +pub fn cpu_profiler() -> CpuProfiler { + #[cfg(feature = "cpuprofiler")] + { + cpuprofiler::PROFILER.lock().unwrap().start("./out.profile").unwrap(); + } + + #[cfg(not(feature = "cpuprofiler"))] + { + eprintln!("cpuprofiler feature is disabled") + } + + CpuProfiler { _private: () } +} + +impl Drop for CpuProfiler { + fn drop(&mut self) { + #[cfg(feature = "cpuprofiler")] + { + cpuprofiler::PROFILER.lock().unwrap().stop().unwrap(); + } + } +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3