2020-04-25 08:02:09 -05:00
|
|
|
//! A collection of tools for profiling rust-analyzer.
|
2019-09-30 03:58:53 -05:00
|
|
|
|
2023-12-05 04:35:09 -06:00
|
|
|
#![warn(rust_2018_idioms, unused_lifetimes)]
|
2022-07-20 07:59:42 -05:00
|
|
|
|
2020-07-30 02:44:21 -05:00
|
|
|
mod stop_watch;
|
2019-06-30 05:30:17 -05:00
|
|
|
mod memory_usage;
|
2019-08-17 07:29:57 -05:00
|
|
|
#[cfg(feature = "cpu_profiler")]
|
|
|
|
mod google_cpu_profiler;
|
2020-04-25 08:02:09 -05:00
|
|
|
mod hprof;
|
2020-04-25 14:04:04 -05:00
|
|
|
mod tree;
|
2019-06-30 05:30:17 -05:00
|
|
|
|
2020-04-25 08:02:09 -05:00
|
|
|
use std::cell::RefCell;
|
2019-03-27 10:09:51 -05:00
|
|
|
|
2020-04-25 08:02:09 -05:00
|
|
|
pub use crate::{
|
2021-03-29 13:08:07 -05:00
|
|
|
hprof::{heartbeat, heartbeat_span, init, init_from, span},
|
2020-04-25 08:02:09 -05:00
|
|
|
memory_usage::{Bytes, MemoryUsage},
|
2020-07-30 02:44:21 -05:00
|
|
|
stop_watch::{StopWatch, StopWatchSpan},
|
2020-04-25 08:02:09 -05:00
|
|
|
};
|
2019-06-30 05:30:17 -05:00
|
|
|
|
2021-01-21 10:04:50 -06:00
|
|
|
pub use countme;
|
|
|
|
/// Include `_c: Count<Self>` field in important structs to count them.
|
|
|
|
///
|
|
|
|
/// To view the counts, run with `RA_COUNT=1`. The overhead of disabled count is
|
|
|
|
/// almost zero.
|
|
|
|
pub use countme::Count;
|
|
|
|
|
2019-06-03 16:25:43 -05:00
|
|
|
thread_local!(static IN_SCOPE: RefCell<bool> = RefCell::new(false));
|
|
|
|
|
2023-02-01 11:06:09 -06:00
|
|
|
/// Allows to check if the current code is within some dynamic scope, can be
|
2019-06-03 16:25:43 -05:00
|
|
|
/// useful during debugging to figure out why a function is called.
|
|
|
|
pub struct Scope {
|
2019-06-04 06:46:22 -05:00
|
|
|
prev: bool,
|
2019-06-03 16:25:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Scope {
|
2020-07-10 20:04:37 -05:00
|
|
|
#[must_use]
|
2019-06-03 16:25:43 -05:00
|
|
|
pub fn enter() -> Scope {
|
2019-06-04 06:46:22 -05:00
|
|
|
let prev = IN_SCOPE.with(|slot| std::mem::replace(&mut *slot.borrow_mut(), true));
|
|
|
|
Scope { prev }
|
2019-06-03 16:25:43 -05:00
|
|
|
}
|
|
|
|
pub fn is_active() -> bool {
|
|
|
|
IN_SCOPE.with(|slot| *slot.borrow())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Scope {
|
|
|
|
fn drop(&mut self) {
|
2019-06-04 06:46:22 -05:00
|
|
|
IN_SCOPE.with(|slot| *slot.borrow_mut() = self.prev);
|
2019-06-03 16:25:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 07:29:57 -05:00
|
|
|
/// A wrapper around google_cpu_profiler.
|
2019-06-26 03:11:28 -05:00
|
|
|
///
|
2019-08-17 07:29:57 -05:00
|
|
|
/// Usage:
|
2021-06-13 23:57:10 -05:00
|
|
|
/// 1. Install gpref_tools (<https://github.com/gperftools/gperftools>), probably packaged with your Linux distro.
|
2019-08-17 07:29:57 -05:00
|
|
|
/// 2. Build with `cpu_profiler` feature.
|
2021-03-30 09:20:27 -05:00
|
|
|
/// 3. Run the code, the *raw* output would be in the `./out.profile` file.
|
2021-06-13 23:57:10 -05:00
|
|
|
/// 4. Install pprof for visualization (<https://github.com/google/pprof>).
|
2020-07-10 20:39:44 -05:00
|
|
|
/// 5. Bump sampling frequency to once per ms: `export CPUPROFILE_FREQUENCY=1000`
|
|
|
|
/// 6. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results.
|
2019-08-17 07:29:57 -05:00
|
|
|
///
|
|
|
|
/// For example, here's how I run profiling on NixOS:
|
|
|
|
///
|
|
|
|
/// ```bash
|
2021-03-30 09:20:27 -05:00
|
|
|
/// $ bat -p shell.nix
|
|
|
|
/// with import <nixpkgs> {};
|
|
|
|
/// mkShell {
|
|
|
|
/// buildInputs = [ gperftools ];
|
|
|
|
/// shellHook = ''
|
|
|
|
/// export LD_LIBRARY_PATH="${gperftools}/lib:"
|
|
|
|
/// '';
|
|
|
|
/// }
|
|
|
|
/// $ set -x CPUPROFILE_FREQUENCY 1000
|
|
|
|
/// $ nix-shell --run 'cargo test --release --package rust-analyzer --lib -- benchmarks::benchmark_integrated_highlighting --exact --nocapture'
|
|
|
|
/// $ pprof -svg target/release/deps/rust_analyzer-8739592dc93d63cb crates/rust-analyzer/out.profile > profile.svg
|
2019-08-17 07:29:57 -05:00
|
|
|
/// ```
|
2020-07-10 20:39:44 -05:00
|
|
|
///
|
|
|
|
/// See this diff for how to profile completions:
|
|
|
|
///
|
2022-07-08 08:44:49 -05:00
|
|
|
/// <https://github.com/rust-lang/rust-analyzer/pull/5306>
|
2019-06-26 03:11:28 -05:00
|
|
|
#[derive(Debug)]
|
2020-08-12 09:32:36 -05:00
|
|
|
pub struct CpuSpan {
|
2019-06-26 03:11:28 -05:00
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:04:37 -05:00
|
|
|
#[must_use]
|
2020-08-12 09:32:36 -05:00
|
|
|
pub fn cpu_span() -> CpuSpan {
|
2019-08-17 07:29:57 -05:00
|
|
|
#[cfg(feature = "cpu_profiler")]
|
2019-06-26 03:11:28 -05:00
|
|
|
{
|
2019-08-17 07:29:57 -05:00
|
|
|
google_cpu_profiler::start("./out.profile".as_ref())
|
2019-06-26 03:11:28 -05:00
|
|
|
}
|
|
|
|
|
2019-08-17 07:29:57 -05:00
|
|
|
#[cfg(not(feature = "cpu_profiler"))]
|
2019-06-26 03:11:28 -05:00
|
|
|
{
|
2021-03-30 09:20:27 -05:00
|
|
|
eprintln!(
|
|
|
|
r#"cpu profiling is disabled, uncomment `default = [ "cpu_profiler" ]` in Cargo.toml to enable."#
|
2021-10-03 07:39:43 -05:00
|
|
|
);
|
2019-06-26 03:11:28 -05:00
|
|
|
}
|
|
|
|
|
2020-08-12 09:32:36 -05:00
|
|
|
CpuSpan { _private: () }
|
2019-06-26 03:11:28 -05:00
|
|
|
}
|
|
|
|
|
2020-08-12 09:32:36 -05:00
|
|
|
impl Drop for CpuSpan {
|
2019-06-26 03:11:28 -05:00
|
|
|
fn drop(&mut self) {
|
2019-08-17 07:29:57 -05:00
|
|
|
#[cfg(feature = "cpu_profiler")]
|
2019-06-26 03:11:28 -05:00
|
|
|
{
|
2021-03-30 09:20:27 -05:00
|
|
|
google_cpu_profiler::stop();
|
|
|
|
let profile_data = std::env::current_dir().unwrap().join("out.profile");
|
|
|
|
eprintln!("Profile data saved to:\n\n {}\n", profile_data.display());
|
|
|
|
let mut cmd = std::process::Command::new("pprof");
|
|
|
|
cmd.arg("-svg").arg(std::env::current_exe().unwrap()).arg(&profile_data);
|
|
|
|
let out = cmd.output();
|
|
|
|
|
|
|
|
match out {
|
|
|
|
Ok(out) if out.status.success() => {
|
|
|
|
let svg = profile_data.with_extension("svg");
|
2022-12-30 02:05:03 -06:00
|
|
|
std::fs::write(&svg, out.stdout).unwrap();
|
2021-03-30 09:20:27 -05:00
|
|
|
eprintln!("Profile rendered to:\n\n {}\n", svg.display());
|
|
|
|
}
|
|
|
|
_ => {
|
2022-12-30 01:59:11 -06:00
|
|
|
eprintln!("Failed to run:\n\n {cmd:?}\n");
|
2021-03-30 09:20:27 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-26 03:11:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:30:17 -05:00
|
|
|
pub fn memory_usage() -> MemoryUsage {
|
2021-05-22 08:53:47 -05:00
|
|
|
MemoryUsage::now()
|
2019-06-30 05:30:17 -05:00
|
|
|
}
|