-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aya+ebpf: Implement read+write methods for PerfEventArray #649
Open
TheElectronWill
wants to merge
1
commit into
aya-rs:main
Choose a base branch
from
TheElectronWill:bpf-perf-event-read
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
//! Perf event programs. | ||
|
||
use std::os::fd::AsFd as _; | ||
use std::os::fd::{AsFd as _, OwnedFd}; | ||
|
||
pub use crate::generated::{ | ||
perf_hw_cache_id, perf_hw_cache_op_id, perf_hw_cache_op_result_id, perf_hw_id, perf_sw_ids, | ||
|
@@ -20,10 +20,10 @@ use crate::{ | |
perf_attach::{PerfLinkIdInner, PerfLinkInner}, | ||
FdLink, LinkError, ProgramData, ProgramError, | ||
}, | ||
sys::{bpf_link_get_info_by_fd, perf_event_open, SyscallError}, | ||
sys::{self, bpf_link_get_info_by_fd, SyscallError}, | ||
}; | ||
|
||
/// The type of perf event | ||
/// The type of perf event. | ||
#[repr(u32)] | ||
#[derive(Debug, Clone)] | ||
pub enum PerfTypeId { | ||
|
@@ -41,7 +41,7 @@ pub enum PerfTypeId { | |
Breakpoint = PERF_TYPE_BREAKPOINT as u32, | ||
} | ||
|
||
/// Sample Policy | ||
/// Sample Policy. | ||
#[derive(Debug, Clone)] | ||
pub enum SamplePolicy { | ||
/// Period | ||
|
@@ -50,30 +50,44 @@ pub enum SamplePolicy { | |
Frequency(u64), | ||
} | ||
|
||
/// The scope of a PerfEvent | ||
/// A flag whose bits indicate the fields to include in the event samples. | ||
#[derive(Debug, Clone)] | ||
pub struct SampleType(u64); | ||
|
||
/// "Wake up" overflow notification policy. | ||
/// Overflows are generated only by sampling events. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline between the two lines. The format is: [summary] |
||
#[derive(Debug, Clone)] | ||
pub enum WakeupPolicy { | ||
/// Wake up after n events. | ||
Events(u32), | ||
/// Wake up after n bytes. | ||
Watermark(u32), | ||
} | ||
|
||
/// The scope of a PerfEvent. | ||
#[derive(Debug, Clone)] | ||
#[allow(clippy::enum_variant_names)] | ||
pub enum PerfEventScope { | ||
/// Calling process, any cpu | ||
/// Calling process, any cpu. | ||
CallingProcessAnyCpu, | ||
/// calling process, one cpu | ||
/// Calling process, one cpu. | ||
CallingProcessOneCpu { | ||
/// cpu id | ||
cpu: u32, | ||
}, | ||
/// one process, any cpu | ||
/// One process, any cpu. | ||
OneProcessAnyCpu { | ||
/// process id | ||
pid: u32, | ||
}, | ||
/// one process, one cpu | ||
/// One process, one cpu. | ||
OneProcessOneCpu { | ||
/// cpu id | ||
cpu: u32, | ||
/// process id | ||
pid: u32, | ||
}, | ||
/// all processes, one cpu | ||
/// All processes, one cpu. | ||
AllProcessesOneCpu { | ||
/// cpu id | ||
cpu: u32, | ||
|
@@ -147,33 +161,11 @@ impl PerfEvent { | |
) -> Result<PerfEventLinkId, ProgramError> { | ||
let prog_fd = self.fd()?; | ||
let prog_fd = prog_fd.as_fd(); | ||
let (sample_period, sample_frequency) = match sample_policy { | ||
SamplePolicy::Period(period) => (period, None), | ||
SamplePolicy::Frequency(frequency) => (0, Some(frequency)), | ||
}; | ||
let (pid, cpu) = match scope { | ||
PerfEventScope::CallingProcessAnyCpu => (0, -1), | ||
PerfEventScope::CallingProcessOneCpu { cpu } => (0, cpu as i32), | ||
PerfEventScope::OneProcessAnyCpu { pid } => (pid as i32, -1), | ||
PerfEventScope::OneProcessOneCpu { cpu, pid } => (pid as i32, cpu as i32), | ||
PerfEventScope::AllProcessesOneCpu { cpu } => (-1, cpu as i32), | ||
}; | ||
let fd = perf_event_open( | ||
perf_type as u32, | ||
config, | ||
pid, | ||
cpu, | ||
sample_period, | ||
sample_frequency, | ||
false, | ||
0, | ||
) | ||
.map_err(|(_code, io_error)| SyscallError { | ||
call: "perf_event_open", | ||
io_error, | ||
})?; | ||
|
||
let link = perf_attach(prog_fd, fd)?; | ||
let sampling = Some((sample_policy, SampleType(PERF_TYPE_RAW as u64))); | ||
let event_fd = perf_event_open(perf_type as u32, config, scope, sampling, None, 0)?; | ||
|
||
let link = perf_attach(prog_fd, event_fd)?; | ||
self.data.links.insert(PerfEventLink::new(link)) | ||
} | ||
|
||
|
@@ -225,3 +217,65 @@ define_link_wrapper!( | |
PerfLinkInner, | ||
PerfLinkIdInner | ||
); | ||
|
||
/// Performs a call to `perf_event_open` and returns the event's file descriptor. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `perf_type` - the type of event, see [`crate::generated::perf_type_id`] for a list of types. Note that this list is non-exhaustive, because PMUs (Performance Monitoring Units) can be added to the system. Their ids can be read from the sysfs (see the kernel documentation on perf_event_open). | ||
/// * `config` - the event that we want to open | ||
/// * `scope` - which process and cpu to monitor (logical cpu, not physical socket) | ||
/// * `sampling` - if not None, enables the sampling mode with the given parameters | ||
/// * `wakeup` - if not None, sets up the wake-up for the overflow notifications | ||
/// * `flags` - various flags combined with a binary OR (for ex. `FLAG_A | FLAG_B`), zero means no flag | ||
pub fn perf_event_open( | ||
perf_type: u32, | ||
config: u64, | ||
scope: PerfEventScope, | ||
sampling: Option<(SamplePolicy, SampleType)>, | ||
wakeup: Option<WakeupPolicy>, | ||
flags: u32, | ||
) -> Result<OwnedFd, ProgramError> { | ||
let mut attr = sys::init_perf_event_attr(); | ||
|
||
// Fill in the attributes | ||
attr.type_ = perf_type; | ||
attr.config = config; | ||
match sampling { | ||
Some((SamplePolicy::Frequency(f), SampleType(t))) => { | ||
attr.set_freq(1); | ||
attr.__bindgen_anon_1.sample_freq = f; | ||
attr.sample_type = t; | ||
} | ||
Some((SamplePolicy::Period(p), SampleType(t))) => { | ||
attr.__bindgen_anon_1.sample_period = p; | ||
attr.sample_type = t; | ||
} | ||
None => (), | ||
}; | ||
match wakeup { | ||
Some(WakeupPolicy::Events(n)) => { | ||
attr.__bindgen_anon_2.wakeup_events = n; | ||
} | ||
Some(WakeupPolicy::Watermark(n)) => { | ||
attr.set_watermark(1); | ||
attr.__bindgen_anon_2.wakeup_watermark = n; | ||
} | ||
None => (), | ||
}; | ||
|
||
let (pid, cpu) = match scope { | ||
PerfEventScope::CallingProcessAnyCpu => (0, -1), | ||
PerfEventScope::CallingProcessOneCpu { cpu } => (0, cpu as i32), | ||
PerfEventScope::OneProcessAnyCpu { pid } => (pid as i32, -1), | ||
PerfEventScope::OneProcessOneCpu { cpu, pid } => (pid as i32, cpu as i32), | ||
PerfEventScope::AllProcessesOneCpu { cpu } => (-1, cpu as i32), | ||
}; | ||
|
||
sys::perf_event_sys(attr, pid, cpu, flags).map_err(|(_, io_error)| { | ||
ProgramError::SyscallError(SyscallError { | ||
call: "perf_event_open", | ||
io_error, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if I set an index that has already been
open()
-ed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I have not tested this case. If the fd has already been opened, it could stay open? The doc does not give any clear hint about this case.