Skip to content

Commit

Permalink
Switch to SwCpuClock on ENOENT error
Browse files Browse the repository at this point in the history
The `perf_event_open` manpage says that ENOENT is "Returned if the type
setting is not valid.". Prior to this fix, samply only switched to
SwCpuClick on an EPERM error. This fix means that we try it
speculatively whatever the error, unless it's a EPERM that can be fixed
by changing the paranoia level.
  • Loading branch information
rkday-pro authored and mstange committed Sep 11, 2023
1 parent ee1c70e commit 422530a
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions samply/src/linux/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,10 @@ fn init_profiler(
attach_mode,
);

let mut perf = match perf {
Ok(perf) => perf,
Err(error) if error.kind() == std::io::ErrorKind::PermissionDenied => {
match paranoia_level() {
Some(level) if level > 1 => {
if let Err(error) = &perf {
if error.kind() == std::io::ErrorKind::PermissionDenied {
if let Some(level) = paranoia_level() {
if level > 1 {
eprintln!();
eprintln!(
"'/proc/sys/kernel/perf_event_paranoid' is currently set to {level}."
Expand All @@ -372,32 +371,34 @@ fn init_profiler(
eprintln!();
std::process::exit(1);
}
_ => {
// Permission denied even though parania was probably not the reason.
// Another reason for the error could be the type of perf event:
// The "Hardware CPU cycles" event is not supported in some contexts, for example in VMs.
// Try a different event type.
let perf = PerfGroup::open(
pid,
frequency,
stack_size,
EventSource::SwCpuClock,
regs_mask,
attach_mode,
);
match perf {
Ok(perf) => perf, // Success!
Err(error) => {
eprintln!("Failed to start profiling: {error}");
std::process::exit(1);
}
}
}
}
}
Err(error) => {
eprintln!("Failed to start profiling: {error}");
std::process::exit(1);
}

let mut perf = match perf {
Ok(perf) => perf,
Err(_) => {
// We've already checked for permission denied due to paranoia
// level, and exited with a warning in that case.

// Another reason for the error could be the type of perf event:
// The "Hardware CPU cycles" event is not supported in some contexts, for example in VMs.
// Try a different event type.
let perf = PerfGroup::open(
pid,
frequency,
stack_size,
EventSource::SwCpuClock,
regs_mask,
attach_mode,
);
match perf {
Ok(perf) => perf, // Success!
Err(error) => {
eprintln!("Failed to start profiling: {error}");
std::process::exit(1);
}
}
}
};

Expand Down

0 comments on commit 422530a

Please sign in to comment.