-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aya+ebpf: Add integration test for reading perf_events from bpf program
- Loading branch information
1 parent
6cba3c5
commit 352ec0e
Showing
9 changed files
with
248 additions
and
42 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use aya_bpf::{ | ||
bindings::bpf_perf_event_value, | ||
helpers::bpf_get_smp_processor_id, | ||
macros::{map, perf_event}, | ||
maps::PerfEventArray, | ||
programs::PerfEventContext, | ||
}; | ||
|
||
#[repr(C)] | ||
struct EventData { | ||
value: u64, | ||
cpu_id: u32, | ||
tag: u8, | ||
} | ||
|
||
/// Input map: file descriptors of the perf events, obtained by calling | ||
/// `perf_event_open` in user space. | ||
#[map] | ||
static mut DESCRIPTORS: PerfEventArray<i32> = PerfEventArray::with_max_entries(1, 0); | ||
|
||
#[map] | ||
static mut OUTPUT: PerfEventArray<EventData> = PerfEventArray::with_max_entries(1, 0); | ||
|
||
#[perf_event] | ||
pub fn on_perf_event(ctx: PerfEventContext) -> i64 { | ||
match read_event(&ctx).map(|res| write_output(&ctx, res)) { | ||
Ok(_) => 0, | ||
Err(e) => e, | ||
} | ||
} | ||
|
||
fn read_event(ctx: &PerfEventContext) -> Result<EventData, i64> { | ||
// read the event value using the file descriptor in the DESCRIPTORS array | ||
let event: bpf_perf_event_value = unsafe { DESCRIPTORS.read_current_cpu() }?; | ||
|
||
let cpu_id = unsafe { bpf_get_smp_processor_id() }; | ||
let res = EventData { | ||
value: event.counter, | ||
cpu_id, | ||
tag: 0xAB, | ||
}; | ||
Ok(res) | ||
} | ||
|
||
fn write_output(ctx: &PerfEventContext, output: EventData) -> Result<(), i64> { | ||
unsafe { OUTPUT.output_current_cpu(ctx, &output) } | ||
} | ||
|
||
#[cfg(not(test))] | ||
#[panic_handler] | ||
fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
loop {} | ||
} |
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
Oops, something went wrong.