10777: internal: Allow disabling perf access via `RA_DISABLE_PERF` r=lnicola a=jonas-schievink

https://github.com/rr-debugger/rr does not support the perf-specific ioctls, so add a way to avoid them.

bors r+

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-11-16 18:44:04 +00:00 committed by GitHub
commit bf408ef5fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<bool> =
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 {