rust-analyzer/crates/ra_prof/src/lib.rs

107 lines
2.7 KiB
Rust
Raw Normal View History

2020-04-25 13:02:09 +00:00
//! A collection of tools for profiling rust-analyzer.
mod memory_usage;
2019-08-17 12:29:57 +00:00
#[cfg(feature = "cpu_profiler")]
mod google_cpu_profiler;
2020-04-25 13:02:09 +00:00
mod hprof;
mod tree;
2020-04-25 13:02:09 +00:00
use std::cell::RefCell;
2019-03-27 15:09:51 +00:00
2020-04-25 13:02:09 +00:00
pub use crate::{
hprof::{init, init_from, profile},
memory_usage::{Bytes, MemoryUsage},
};
// We use jemalloc mainly to get heap usage statistics, actual performance
// difference is not measures.
#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
2019-06-03 21:25:43 +00:00
/// Prints backtrace to stderr, useful for debugging.
2020-02-19 15:28:25 +00:00
#[cfg(feature = "backtrace")]
2019-06-03 21:25:43 +00:00
pub fn print_backtrace() {
let bt = backtrace::Backtrace::new();
eprintln!("{:?}", bt);
}
2020-03-28 12:19:05 +00: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 21:25:43 +00: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 11:46:22 +00:00
prev: bool,
2019-06-03 21:25:43 +00:00
}
impl Scope {
pub fn enter() -> Scope {
2019-06-04 11:46:22 +00:00
let prev = IN_SCOPE.with(|slot| std::mem::replace(&mut *slot.borrow_mut(), true));
Scope { prev }
2019-06-03 21:25:43 +00:00
}
pub fn is_active() -> bool {
IN_SCOPE.with(|slot| *slot.borrow())
}
}
impl Drop for Scope {
fn drop(&mut self) {
2019-06-04 11:46:22 +00:00
IN_SCOPE.with(|slot| *slot.borrow_mut() = self.prev);
2019-06-03 21:25:43 +00:00
}
}
2019-08-17 12:29:57 +00:00
/// A wrapper around google_cpu_profiler.
///
2019-08-17 12:29:57 +00: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-02-18 11:33:16 +00:00
/// 5. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results.
2019-08-17 12:29:57 +00:00
///
/// For example, here's how I run profiling on NixOS:
///
/// ```bash
/// $ nix-shell -p gperftools --run \
2020-02-18 11:33:16 +00:00
/// 'cargo run --release -p rust-analyzer -- parse < ~/projects/rustbench/parser.rs > /dev/null'
2019-08-17 12:29:57 +00:00
/// ```
#[derive(Debug)]
pub struct CpuProfiler {
_private: (),
}
pub fn cpu_profiler() -> CpuProfiler {
2019-08-17 12:29:57 +00:00
#[cfg(feature = "cpu_profiler")]
{
2019-08-17 12:29:57 +00:00
google_cpu_profiler::start("./out.profile".as_ref())
}
2019-08-17 12:29:57 +00:00
#[cfg(not(feature = "cpu_profiler"))]
{
2019-08-17 12:29:57 +00:00
eprintln!("cpu_profiler feature is disabled")
}
CpuProfiler { _private: () }
}
impl Drop for CpuProfiler {
fn drop(&mut self) {
2019-08-17 12:29:57 +00:00
#[cfg(feature = "cpu_profiler")]
{
2019-08-17 12:29:57 +00:00
google_cpu_profiler::stop()
}
}
}
pub fn memory_usage() -> MemoryUsage {
MemoryUsage::current()
}