rust/crates/profile/src/lib.rs

102 lines
2.6 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.
/// 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
#[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"))]
{
2019-08-17 07:29:57 -05:00
eprintln!("cpu_profiler feature is disabled")
}
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")]
{
2019-08-17 07:29:57 -05:00
google_cpu_profiler::stop()
}
}
}
pub fn memory_usage() -> MemoryUsage {
MemoryUsage::current()
}