Skip to content

Commit

Permalink
PerfEvent: add option to set the inherit bit
Browse files Browse the repository at this point in the history
The inherit bit allows extending perf scope to the new child
processes.
  • Loading branch information
akhramov committed Oct 5, 2023
1 parent 0cd620a commit 548c131
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions aya/src/programs/perf_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ pub enum PerfEventScope {
/// process id
pid: u32,
},
/// one process, any cpu, including child processes
OneProcessIncludingChildren {
/// process id
pid: u32,
},
/// one process, one cpu
OneProcessOneCpu {
/// cpu id
Expand Down Expand Up @@ -157,6 +162,7 @@ impl PerfEvent {
PerfEventScope::OneProcessAnyCpu { pid } => (pid as i32, -1),
PerfEventScope::OneProcessOneCpu { cpu, pid } => (pid as i32, cpu as i32),
PerfEventScope::AllProcessesOneCpu { cpu } => (-1, cpu as i32),
PerfEventScope::OneProcessIncludingChildren { pid } => (pid as i32, -1),
};
let fd = perf_event_open(
perf_type as u32,
Expand All @@ -167,6 +173,7 @@ impl PerfEvent {
sample_frequency,
false,
0,
matches!(scope, PerfEventScope::OneProcessIncludingChildren { .. }),
)
.map_err(|(_code, io_error)| SyscallError {
call: "perf_event_open",
Expand Down
4 changes: 3 additions & 1 deletion aya/src/sys/perf_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ pub(crate) fn perf_event_open(
sample_frequency: Option<u64>,
wakeup: bool,
flags: u32,
inherit: bool,
) -> SysResult<OwnedFd> {
let mut attr = unsafe { mem::zeroed::<perf_event_attr>() };

attr.config = config;
attr.size = mem::size_of::<perf_event_attr>() as u32;
attr.type_ = perf_type;
attr.sample_type = PERF_SAMPLE_RAW as u64;
// attr.inherits = if pid > 0 { 1 } else { 0 };
attr.set_inherit(if inherit { 1 } else { 0 });
attr.__bindgen_anon_2.wakeup_events = u32::from(wakeup);

if let Some(frequency) = sample_frequency {
Expand All @@ -55,6 +56,7 @@ pub(crate) fn perf_event_open_bpf(cpu: c_int) -> SysResult<OwnedFd> {
None,
true,
PERF_FLAG_FD_CLOEXEC,
false,
)
}

Expand Down
4 changes: 4 additions & 0 deletions xtask/public-api/aya.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3989,6 +3989,8 @@ pub aya::programs::perf_event::PerfEventScope::CallingProcessOneCpu
pub aya::programs::perf_event::PerfEventScope::CallingProcessOneCpu::cpu: u32
pub aya::programs::perf_event::PerfEventScope::OneProcessAnyCpu
pub aya::programs::perf_event::PerfEventScope::OneProcessAnyCpu::pid: u32
pub aya::programs::perf_event::PerfEventScope::OneProcessIncludingChildren
pub aya::programs::perf_event::PerfEventScope::OneProcessIncludingChildren::pid: u32
pub aya::programs::perf_event::PerfEventScope::OneProcessOneCpu
pub aya::programs::perf_event::PerfEventScope::OneProcessOneCpu::cpu: u32
pub aya::programs::perf_event::PerfEventScope::OneProcessOneCpu::pid: u32
Expand Down Expand Up @@ -5214,6 +5216,8 @@ pub aya::programs::PerfEventScope::CallingProcessOneCpu
pub aya::programs::PerfEventScope::CallingProcessOneCpu::cpu: u32
pub aya::programs::PerfEventScope::OneProcessAnyCpu
pub aya::programs::PerfEventScope::OneProcessAnyCpu::pid: u32
pub aya::programs::PerfEventScope::OneProcessIncludingChildren
pub aya::programs::PerfEventScope::OneProcessIncludingChildren::pid: u32
pub aya::programs::PerfEventScope::OneProcessOneCpu
pub aya::programs::PerfEventScope::OneProcessOneCpu::cpu: u32
pub aya::programs::PerfEventScope::OneProcessOneCpu::pid: u32
Expand Down

0 comments on commit 548c131

Please sign in to comment.