2020-04-25 08:02:09 -05:00
|
|
|
//! A collection of tools for profiling rust-analyzer.
|
2019-09-30 03:58:53 -05:00
|
|
|
|
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::{
|
|
|
|
hprof::{init, init_from, profile},
|
|
|
|
memory_usage::{Bytes, MemoryUsage},
|
|
|
|
};
|
2019-06-30 05:30:17 -05:00
|
|
|
|
2019-06-03 16:25:43 -05:00
|
|
|
/// Prints backtrace to stderr, useful for debugging.
|
2020-02-19 09:28:25 -06:00
|
|
|
#[cfg(feature = "backtrace")]
|
2019-06-03 16:25:43 -05:00
|
|
|
pub fn print_backtrace() {
|
|
|
|
let bt = backtrace::Backtrace::new();
|
|
|
|
eprintln!("{:?}", bt);
|
|
|
|
}
|
2020-03-28 07:19:05 -05:00
|
|
|
#[cfg(not(feature = "backtrace"))]
|
|
|
|
pub fn print_backtrace() {
|
|
|
|
eprintln!(
|
|
|
|
r#"enable the backtrace feature:
|
|
|
|
ra_prof = {{ path = "../ra_prof", features = [ "backtrace"] }}
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
}
|
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-06-26 03:11:28 -05:00
|
|
|
///
|
2019-08-17 07:29:57 -05:00
|
|
|
/// Usage:
|
|
|
|
/// 1. Install gpref_tools (https://github.com/gperftools/gperftools), probably packaged with your Linux distro.
|
|
|
|
/// 2. Build with `cpu_profiler` feature.
|
|
|
|
/// 3. Tun 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
|
|
|
|
/// $ nix-shell -p gperftools --run \
|
2020-02-18 05:33:16 -06:00
|
|
|
/// 'cargo run --release -p rust-analyzer -- parse < ~/projects/rustbench/parser.rs > /dev/null'
|
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
|
2019-06-26 03:11:28 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CpuProfiler {
|
|
|
|
_private: (),
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:04:37 -05:00
|
|
|
#[must_use]
|
2019-06-26 03:11:28 -05:00
|
|
|
pub fn cpu_profiler() -> CpuProfiler {
|
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
|
|
|
{
|
2019-08-17 07:29:57 -05:00
|
|
|
eprintln!("cpu_profiler feature is disabled")
|
2019-06-26 03:11:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CpuProfiler { _private: () }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for CpuProfiler {
|
|
|
|
fn drop(&mut self) {
|
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::stop()
|
2019-06-26 03:11:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 05:30:17 -05:00
|
|
|
pub fn memory_usage() -> MemoryUsage {
|
|
|
|
MemoryUsage::current()
|
|
|
|
}
|