aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_prof
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_prof')
-rw-r--r--crates/ra_prof/Cargo.toml8
-rw-r--r--crates/ra_prof/src/lib.rs13
2 files changed, 20 insertions, 1 deletions
diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml
index c33b5121a..b3d52985a 100644
--- a/crates/ra_prof/Cargo.toml
+++ b/crates/ra_prof/Cargo.toml
@@ -4,6 +4,7 @@ name = "ra_prof"
4version = "0.1.0" 4version = "0.1.0"
5authors = ["rust-analyzer developers"] 5authors = ["rust-analyzer developers"]
6publish = false 6publish = false
7license = "MIT OR Apache-2.0"
7 8
8[lib] 9[lib]
9doctest = false 10doctest = false
@@ -12,6 +13,7 @@ doctest = false
12ra_arena = { path = "../ra_arena" } 13ra_arena = { path = "../ra_arena" }
13once_cell = "1.3.1" 14once_cell = "1.3.1"
14backtrace = { version = "0.3.44", optional = true } 15backtrace = { version = "0.3.44", optional = true }
16mimalloc = { version = "0.1.19", default-features = false, optional = true }
15 17
16[target.'cfg(not(target_env = "msvc"))'.dependencies] 18[target.'cfg(not(target_env = "msvc"))'.dependencies]
17jemallocator = { version = "0.3.2", optional = true } 19jemallocator = { version = "0.3.2", optional = true }
@@ -20,3 +22,9 @@ jemalloc-ctl = { version = "0.3.3", optional = true }
20[features] 22[features]
21jemalloc = [ "jemallocator", "jemalloc-ctl" ] 23jemalloc = [ "jemallocator", "jemalloc-ctl" ]
22cpu_profiler = [] 24cpu_profiler = []
25
26# Uncomment to enable for the whole crate graph
27# default = [ "backtrace" ]
28# default = [ "jemalloc" ]
29# default = [ "mimalloc" ]
30# default = [ "cpu_profiler" ]
diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs
index 89df7f04b..b54531b4e 100644
--- a/crates/ra_prof/src/lib.rs
+++ b/crates/ra_prof/src/lib.rs
@@ -19,6 +19,10 @@ pub use crate::{
19#[global_allocator] 19#[global_allocator]
20static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; 20static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
21 21
22#[cfg(all(feature = "mimalloc"))]
23#[global_allocator]
24static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
25
22/// Prints backtrace to stderr, useful for debugging. 26/// Prints backtrace to stderr, useful for debugging.
23#[cfg(feature = "backtrace")] 27#[cfg(feature = "backtrace")]
24pub fn print_backtrace() { 28pub fn print_backtrace() {
@@ -43,6 +47,7 @@ pub struct Scope {
43} 47}
44 48
45impl Scope { 49impl Scope {
50 #[must_use]
46 pub fn enter() -> Scope { 51 pub fn enter() -> Scope {
47 let prev = IN_SCOPE.with(|slot| std::mem::replace(&mut *slot.borrow_mut(), true)); 52 let prev = IN_SCOPE.with(|slot| std::mem::replace(&mut *slot.borrow_mut(), true));
48 Scope { prev } 53 Scope { prev }
@@ -65,7 +70,8 @@ impl Drop for Scope {
65/// 2. Build with `cpu_profiler` feature. 70/// 2. Build with `cpu_profiler` feature.
66/// 3. Tun the code, the *raw* output would be in the `./out.profile` file. 71/// 3. Tun the code, the *raw* output would be in the `./out.profile` file.
67/// 4. Install pprof for visualization (https://github.com/google/pprof). 72/// 4. Install pprof for visualization (https://github.com/google/pprof).
68/// 5. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results. 73/// 5. Bump sampling frequency to once per ms: `export CPUPROFILE_FREQUENCY=1000`
74/// 6. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results.
69/// 75///
70/// For example, here's how I run profiling on NixOS: 76/// For example, here's how I run profiling on NixOS:
71/// 77///
@@ -73,11 +79,16 @@ impl Drop for Scope {
73/// $ nix-shell -p gperftools --run \ 79/// $ nix-shell -p gperftools --run \
74/// 'cargo run --release -p rust-analyzer -- parse < ~/projects/rustbench/parser.rs > /dev/null' 80/// 'cargo run --release -p rust-analyzer -- parse < ~/projects/rustbench/parser.rs > /dev/null'
75/// ``` 81/// ```
82///
83/// See this diff for how to profile completions:
84///
85/// https://github.com/rust-analyzer/rust-analyzer/pull/5306
76#[derive(Debug)] 86#[derive(Debug)]
77pub struct CpuProfiler { 87pub struct CpuProfiler {
78 _private: (), 88 _private: (),
79} 89}
80 90
91#[must_use]
81pub fn cpu_profiler() -> CpuProfiler { 92pub fn cpu_profiler() -> CpuProfiler {
82 #[cfg(feature = "cpu_profiler")] 93 #[cfg(feature = "cpu_profiler")]
83 { 94 {