internal: revive google_cpu_profile infra

This commit is contained in:
Aleksey Kladov 2021-03-30 17:20:27 +03:00
parent 8447ecf747
commit fb00b92dde
3 changed files with 46 additions and 12 deletions

View file

@ -14,26 +14,31 @@ extern "C" {
fn ProfilerStop(); fn ProfilerStop();
} }
static PROFILER_STATE: AtomicUsize = AtomicUsize::new(OFF);
const OFF: usize = 0; const OFF: usize = 0;
const ON: usize = 1; const ON: usize = 1;
const PENDING: usize = 2; const PENDING: usize = 2;
pub fn start(path: &Path) { fn transition(current: usize, new: usize) -> bool {
if PROFILER_STATE.compare_and_swap(OFF, PENDING, Ordering::SeqCst) != OFF { static STATE: AtomicUsize = AtomicUsize::new(OFF);
STATE.compare_exchange(current, new, Ordering::SeqCst, Ordering::SeqCst).is_ok()
}
pub(crate) fn start(path: &Path) {
if !transition(OFF, PENDING) {
panic!("profiler already started"); panic!("profiler already started");
} }
let path = CString::new(path.display().to_string()).unwrap(); let path = CString::new(path.display().to_string()).unwrap();
if unsafe { ProfilerStart(path.as_ptr()) } == 0 { if unsafe { ProfilerStart(path.as_ptr()) } == 0 {
panic!("profiler failed to start") panic!("profiler failed to start")
} }
assert!(PROFILER_STATE.compare_and_swap(PENDING, ON, Ordering::SeqCst) == PENDING); assert!(transition(PENDING, ON));
} }
pub fn stop() { pub(crate) fn stop() {
if PROFILER_STATE.compare_and_swap(ON, PENDING, Ordering::SeqCst) != ON { if !transition(ON, PENDING) {
panic!("profiler is not started") panic!("profiler is not started")
} }
unsafe { ProfilerStop() }; unsafe { ProfilerStop() };
assert!(PROFILER_STATE.compare_and_swap(PENDING, OFF, Ordering::SeqCst) == PENDING); assert!(transition(PENDING, OFF));
} }

View file

@ -52,7 +52,7 @@ impl Drop for Scope {
/// Usage: /// Usage:
/// 1. Install gpref_tools (https://github.com/gperftools/gperftools), probably packaged with your Linux distro. /// 1. Install gpref_tools (https://github.com/gperftools/gperftools), probably packaged with your Linux distro.
/// 2. Build with `cpu_profiler` feature. /// 2. Build with `cpu_profiler` feature.
/// 3. Tun the code, the *raw* output would be in the `./out.profile` file. /// 3. Run the code, the *raw* output would be in the `./out.profile` file.
/// 4. Install pprof for visualization (https://github.com/google/pprof). /// 4. Install pprof for visualization (https://github.com/google/pprof).
/// 5. Bump sampling frequency to once per ms: `export CPUPROFILE_FREQUENCY=1000` /// 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. /// 6. Use something like `pprof -svg target/release/rust-analyzer ./out.profile` to see the results.
@ -60,8 +60,17 @@ impl Drop for Scope {
/// For example, here's how I run profiling on NixOS: /// For example, here's how I run profiling on NixOS:
/// ///
/// ```bash /// ```bash
/// $ nix-shell -p gperftools --run \ /// $ bat -p shell.nix
/// 'cargo run --release -p rust-analyzer -- parse < ~/projects/rustbench/parser.rs > /dev/null' /// 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
/// ``` /// ```
/// ///
/// See this diff for how to profile completions: /// See this diff for how to profile completions:
@ -81,7 +90,9 @@ pub fn cpu_span() -> CpuSpan {
#[cfg(not(feature = "cpu_profiler"))] #[cfg(not(feature = "cpu_profiler"))]
{ {
eprintln!("cpu_profiler feature is disabled") eprintln!(
r#"cpu profiling is disabled, uncomment `default = [ "cpu_profiler" ]` in Cargo.toml to enable."#
)
} }
CpuSpan { _private: () } CpuSpan { _private: () }
@ -91,7 +102,23 @@ impl Drop for CpuSpan {
fn drop(&mut self) { fn drop(&mut self) {
#[cfg(feature = "cpu_profiler")] #[cfg(feature = "cpu_profiler")]
{ {
google_cpu_profiler::stop() 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);
}
}
} }
} }
} }

View file

@ -51,6 +51,7 @@ fn benchmark_integrated_highlighting() {
} }
profile::init_from("*>100"); profile::init_from("*>100");
// let _s = profile::heartbeat_span();
{ {
let _it = stdx::timeit("change"); let _it = stdx::timeit("change");
@ -63,6 +64,7 @@ fn benchmark_integrated_highlighting() {
{ {
let _it = stdx::timeit("after change"); let _it = stdx::timeit("after change");
let _span = profile::cpu_span();
let analysis = host.analysis(); let analysis = host.analysis();
analysis.highlight_as_html(file_id, false).unwrap(); analysis.highlight_as_html(file_id, false).unwrap();
} }