rust/crates/profile/src/lib.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

129 lines
3.8 KiB
Rust
Raw Normal View History

2020-04-25 08:02:09 -05:00
//! A collection of tools for profiling rust-analyzer.
mod stop_watch;
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;
mod tree;
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::{
hprof::{heartbeat, heartbeat_span, init, init_from, span},
2020-04-25 08:02:09 -05:00
memory_usage::{Bytes, MemoryUsage},
stop_watch::{StopWatch, StopWatchSpan},
2020-04-25 08:02:09 -05: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));
/// Allows to check if the current code is withing some dynamic scope, can be
/// 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-08-17 07:29:57 -05:00
/// Usage:
/// 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.
/// 3. Run the code, the *raw* output would be in the `./out.profile` file.
/// 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
/// $ 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:
///
/// <https://github.com/rust-analyzer/rust-analyzer/pull/5306>
#[derive(Debug)]
2020-08-12 09:32:36 -05:00
pub struct CpuSpan {
_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-08-17 07:29:57 -05:00
google_cpu_profiler::start("./out.profile".as_ref())
}
2019-08-17 07:29:57 -05:00
#[cfg(not(feature = "cpu_profiler"))]
{
eprintln!(
r#"cpu profiling is disabled, uncomment `default = [ "cpu_profiler" ]` in Cargo.toml to enable."#
);
}
2020-08-12 09:32:36 -05:00
CpuSpan { _private: () }
}
2020-08-12 09:32:36 -05:00
impl Drop for CpuSpan {
fn drop(&mut self) {
2019-08-17 07:29:57 -05:00
#[cfg(feature = "cpu_profiler")]
{
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");
std::fs::write(&svg, &out.stdout).unwrap();
eprintln!("Profile rendered to:\n\n {}\n", svg.display());
}
_ => {
eprintln!("Failed to run:\n\n {:?}\n", cmd);
}
}
}
}
}
pub fn memory_usage() -> MemoryUsage {
2021-05-22 08:53:47 -05:00
MemoryUsage::now()
}