1 //! A collection of tools for profiling rust-analyzer.
3 #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
7 #[cfg(feature = "cpu_profiler")]
8 mod google_cpu_profiler;
12 use std::cell::RefCell;
15 hprof::{heartbeat, heartbeat_span, init, init_from, span},
16 memory_usage::{Bytes, MemoryUsage},
17 stop_watch::{StopWatch, StopWatchSpan},
21 /// Include `_c: Count<Self>` field in important structs to count them.
23 /// To view the counts, run with `RA_COUNT=1`. The overhead of disabled count is
25 pub use countme::Count;
27 thread_local!(static IN_SCOPE: RefCell<bool> = RefCell::new(false));
29 /// Allows to check if the current code is withing some dynamic scope, can be
30 /// useful during debugging to figure out why a function is called.
37 pub fn enter() -> Scope {
38 let prev = IN_SCOPE.with(|slot| std::mem::replace(&mut *slot.borrow_mut(), true));
41 pub fn is_active() -> bool {
42 IN_SCOPE.with(|slot| *slot.borrow())
48 IN_SCOPE.with(|slot| *slot.borrow_mut() = self.prev);
52 /// A wrapper around google_cpu_profiler.
55 /// 1. Install gpref_tools (<https://github.com/gperftools/gperftools>), probably packaged with your Linux distro.
56 /// 2. Build with `cpu_profiler` feature.
57 /// 3. Run the code, the *raw* output would be in the `./out.profile` file.
58 /// 4. Install pprof for visualization (<https://github.com/google/pprof>).
59 /// 5. Bump sampling frequency to once per ms: `export CPUPROFILE_FREQUENCY=1000`
60 /// 6. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results.
62 /// For example, here's how I run profiling on NixOS:
65 /// $ bat -p shell.nix
66 /// with import <nixpkgs> {};
68 /// buildInputs = [ gperftools ];
70 /// export LD_LIBRARY_PATH="${gperftools}/lib:"
73 /// $ set -x CPUPROFILE_FREQUENCY 1000
74 /// $ nix-shell --run 'cargo test --release --package rust-analyzer --lib -- benchmarks::benchmark_integrated_highlighting --exact --nocapture'
75 /// $ pprof -svg target/release/deps/rust_analyzer-8739592dc93d63cb crates/rust-analyzer/out.profile > profile.svg
78 /// See this diff for how to profile completions:
80 /// <https://github.com/rust-lang/rust-analyzer/pull/5306>
87 pub fn cpu_span() -> CpuSpan {
88 #[cfg(feature = "cpu_profiler")]
90 google_cpu_profiler::start("./out.profile".as_ref())
93 #[cfg(not(feature = "cpu_profiler"))]
96 r#"cpu profiling is disabled, uncomment `default = [ "cpu_profiler" ]` in Cargo.toml to enable."#
100 CpuSpan { _private: () }
103 impl Drop for CpuSpan {
105 #[cfg(feature = "cpu_profiler")]
107 google_cpu_profiler::stop();
108 let profile_data = std::env::current_dir().unwrap().join("out.profile");
109 eprintln!("Profile data saved to:\n\n {}\n", profile_data.display());
110 let mut cmd = std::process::Command::new("pprof");
111 cmd.arg("-svg").arg(std::env::current_exe().unwrap()).arg(&profile_data);
112 let out = cmd.output();
115 Ok(out) if out.status.success() => {
116 let svg = profile_data.with_extension("svg");
117 std::fs::write(&svg, &out.stdout).unwrap();
118 eprintln!("Profile rendered to:\n\n {}\n", svg.display());
121 eprintln!("Failed to run:\n\n {:?}\n", cmd);
128 pub fn memory_usage() -> MemoryUsage {