From 445280a1b1d740f084f39efa3f583980adfe1dfa Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 16 Nov 2021 19:32:38 +0100 Subject: [PATCH] Allow disabling perf access via `RA_DISABLE_PERF` --- crates/profile/src/stop_watch.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/profile/src/stop_watch.rs b/crates/profile/src/stop_watch.rs index 43b1ce9e6dab..625832848296 100644 --- a/crates/profile/src/stop_watch.rs +++ b/crates/profile/src/stop_watch.rs @@ -23,16 +23,27 @@ impl StopWatch { pub fn start() -> StopWatch { #[cfg(target_os = "linux")] let counter = { - let mut counter = perf_event::Builder::new() - .build() - .map_err(|err| eprintln!("Failed to create perf counter: {}", err)) - .ok(); - if let Some(counter) = &mut counter { - if let Err(err) = counter.enable() { - eprintln!("Failed to start perf counter: {}", err) + // When debugging rust-analyzer using rr, the perf-related syscalls cause it to abort. + // We allow disabling perf by setting the env var `RA_DISABLE_PERF`. + + use once_cell::sync::Lazy; + static PERF_ENABLED: Lazy = + Lazy::new(|| std::env::var_os("RA_DISABLE_PERF").is_none()); + + if *PERF_ENABLED { + let mut counter = perf_event::Builder::new() + .build() + .map_err(|err| eprintln!("Failed to create perf counter: {}", err)) + .ok(); + if let Some(counter) = &mut counter { + if let Err(err) = counter.enable() { + eprintln!("Failed to start perf counter: {}", err) + } } + counter + } else { + None } - counter }; let time = Instant::now(); StopWatch {