aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_prof/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_prof/src/lib.rs')
-rw-r--r--crates/ra_prof/src/lib.rs32
1 files changed, 22 insertions, 10 deletions
diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs
index 6d44fef33..d32a289be 100644
--- a/crates/ra_prof/src/lib.rs
+++ b/crates/ra_prof/src/lib.rs
@@ -1,4 +1,6 @@
1mod memory_usage; 1mod memory_usage;
2#[cfg(feature = "cpu_profiler")]
3mod google_cpu_profiler;
2 4
3use std::{ 5use std::{
4 cell::RefCell, 6 cell::RefCell,
@@ -268,25 +270,35 @@ impl Drop for Scope {
268 } 270 }
269} 271}
270 272
271/// A wrapper around https://github.com/AtheMathmo/cpuprofiler 273/// A wrapper around google_cpu_profiler.
272/// 274///
273/// It can be used to capture sampling profiles of sections of code. 275/// Usage:
274/// It is not exactly out-of-the-box, as it relies on gperftools. 276/// 1. Install gpref_tools (https://github.com/gperftools/gperftools), probably packaged with your Linux distro.
275/// See the docs for the crate for more! 277/// 2. Build with `cpu_profiler` feature.
278/// 3. Tun the code, the *raw* output would be in the `./out.profile` file.
279/// 4. Install pprof for visualization (https://github.com/google/pprof).
280/// 5. Use something like `pprof -svg target/release/ra_cli ./out.profile` to see the results.
281///
282/// For example, here's how I run profiling on NixOS:
283///
284/// ```bash
285/// $ nix-shell -p gperftools --run \
286/// 'cargo run --release -p ra_cli -- parse < ~/projects/rustbench/parser.rs > /dev/null'
287/// ```
276#[derive(Debug)] 288#[derive(Debug)]
277pub struct CpuProfiler { 289pub struct CpuProfiler {
278 _private: (), 290 _private: (),
279} 291}
280 292
281pub fn cpu_profiler() -> CpuProfiler { 293pub fn cpu_profiler() -> CpuProfiler {
282 #[cfg(feature = "cpuprofiler")] 294 #[cfg(feature = "cpu_profiler")]
283 { 295 {
284 cpuprofiler::PROFILER.lock().unwrap().start("./out.profile").unwrap(); 296 google_cpu_profiler::start("./out.profile".as_ref())
285 } 297 }
286 298
287 #[cfg(not(feature = "cpuprofiler"))] 299 #[cfg(not(feature = "cpu_profiler"))]
288 { 300 {
289 eprintln!("cpuprofiler feature is disabled") 301 eprintln!("cpu_profiler feature is disabled")
290 } 302 }
291 303
292 CpuProfiler { _private: () } 304 CpuProfiler { _private: () }
@@ -294,9 +306,9 @@ pub fn cpu_profiler() -> CpuProfiler {
294 306
295impl Drop for CpuProfiler { 307impl Drop for CpuProfiler {
296 fn drop(&mut self) { 308 fn drop(&mut self) {
297 #[cfg(feature = "cpuprofiler")] 309 #[cfg(feature = "cpu_profiler")]
298 { 310 {
299 cpuprofiler::PROFILER.lock().unwrap().stop().unwrap(); 311 google_cpu_profiler::stop()
300 } 312 }
301 } 313 }
302} 314}