Merge pull request #775 from aya-rs/perf-as-raw-fd

async_perf_event_array: access inner through async
reviewable/pr781/r1
Tamir Duberstein 1 year ago committed by GitHub
commit 92d3056db3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,8 +1,5 @@
use bytes::BytesMut; use bytes::BytesMut;
use std::{ use std::borrow::{Borrow, BorrowMut};
borrow::{Borrow, BorrowMut},
os::fd::{AsRawFd as _, RawFd},
};
// See https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features. // See https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features.
// //
@ -92,7 +89,7 @@ pub struct AsyncPerfEventArray<T> {
perf_map: PerfEventArray<T>, perf_map: PerfEventArray<T>,
} }
impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArray<T> { impl<T: BorrowMut<MapData>> AsyncPerfEventArray<T> {
/// Opens the perf buffer at the given index. /// Opens the perf buffer at the given index.
/// ///
/// The returned buffer will receive all the events eBPF programs send at the given index. /// The returned buffer will receive all the events eBPF programs send at the given index.
@ -103,16 +100,11 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArray<T> {
) -> Result<AsyncPerfEventArrayBuffer<T>, PerfBufferError> { ) -> Result<AsyncPerfEventArrayBuffer<T>, PerfBufferError> {
let Self { perf_map } = self; let Self { perf_map } = self;
let buf = perf_map.open(index, page_count)?; let buf = perf_map.open(index, page_count)?;
let fd = buf.as_raw_fd();
Ok(AsyncPerfEventArrayBuffer {
buf,
#[cfg(feature = "async_tokio")] #[cfg(feature = "async_tokio")]
async_tokio_fd: AsyncFd::new(fd)?, let buf = AsyncFd::new(buf)?;
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_std_fd: Async::new(fd)?, let buf = Async::new(buf)?;
}) Ok(AsyncPerfEventArrayBuffer { buf })
} }
} }
@ -131,17 +123,18 @@ impl<T: Borrow<MapData>> AsyncPerfEventArray<T> {
/// ///
/// See the [`AsyncPerfEventArray` documentation](AsyncPerfEventArray) for an overview of how to /// See the [`AsyncPerfEventArray` documentation](AsyncPerfEventArray) for an overview of how to
/// use perf buffers. /// use perf buffers.
pub struct AsyncPerfEventArrayBuffer<T> { pub struct AsyncPerfEventArrayBuffer<T: BorrowMut<MapData>> {
#[cfg(not(any(feature = "async_tokio", feature = "async_std")))]
buf: PerfEventArrayBuffer<T>, buf: PerfEventArrayBuffer<T>,
#[cfg(feature = "async_tokio")] #[cfg(feature = "async_tokio")]
async_tokio_fd: AsyncFd<RawFd>, buf: AsyncFd<PerfEventArrayBuffer<T>>,
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_std_fd: Async<RawFd>, buf: Async<PerfEventArrayBuffer<T>>,
} }
impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> { impl<T: BorrowMut<MapData>> AsyncPerfEventArrayBuffer<T> {
/// Reads events from the buffer. /// Reads events from the buffer.
/// ///
/// This method reads events into the provided slice of buffers, filling /// This method reads events into the provided slice of buffers, filling
@ -155,21 +148,20 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
&mut self, &mut self,
buffers: &mut [BytesMut], buffers: &mut [BytesMut],
) -> Result<Events, PerfBufferError> { ) -> Result<Events, PerfBufferError> {
let Self { let Self { buf } = self;
buf,
#[cfg(feature = "async_tokio")]
async_tokio_fd,
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_std_fd,
} = self;
loop { loop {
#[cfg(feature = "async_tokio")] #[cfg(feature = "async_tokio")]
let mut guard = async_tokio_fd.readable_mut().await?; let mut guard = buf.readable_mut().await?;
#[cfg(feature = "async_tokio")]
let buf = guard.get_inner_mut();
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
if !buf.readable() { let buf = {
async_std_fd.readable().await?; if !buf.get_ref().readable() {
buf.readable().await?;
} }
buf.get_mut()
};
let events = buf.read_events(buffers)?; let events = buf.read_events(buffers)?;
const EMPTY: Events = Events { read: 0, lost: 0 }; const EMPTY: Events = Events { read: 0, lost: 0 };

@ -31,7 +31,7 @@ pub struct PerfEventArrayBuffer<T> {
buf: PerfBuffer, buf: PerfBuffer,
} }
impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArrayBuffer<T> { impl<T: BorrowMut<MapData>> PerfEventArrayBuffer<T> {
/// Returns true if the buffer contains events that haven't been read. /// Returns true if the buffer contains events that haven't been read.
pub fn readable(&self) -> bool { pub fn readable(&self) -> bool {
self.buf.readable() self.buf.readable()
@ -55,7 +55,7 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArrayBuffer<T> {
} }
} }
impl<T: BorrowMut<MapData> + Borrow<MapData>> AsRawFd for PerfEventArrayBuffer<T> { impl<T: BorrowMut<MapData>> AsRawFd for PerfEventArrayBuffer<T> {
fn as_raw_fd(&self) -> RawFd { fn as_raw_fd(&self) -> RawFd {
self.buf.as_raw_fd() self.buf.as_raw_fd()
} }
@ -169,7 +169,7 @@ impl<T: Borrow<MapData>> PerfEventArray<T> {
} }
} }
impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArray<T> { impl<T: BorrowMut<MapData>> PerfEventArray<T> {
/// Opens the perf buffer at the given index. /// Opens the perf buffer at the given index.
/// ///
/// The returned buffer will receive all the events eBPF programs send at the given index. /// The returned buffer will receive all the events eBPF programs send at the given index.
@ -180,7 +180,6 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> PerfEventArray<T> {
) -> Result<PerfEventArrayBuffer<T>, PerfBufferError> { ) -> Result<PerfEventArrayBuffer<T>, PerfBufferError> {
// FIXME: keep track of open buffers // FIXME: keep track of open buffers
// this cannot fail as new() checks that the fd is open
let map_data: &MapData = self.map.deref().borrow(); let map_data: &MapData = self.map.deref().borrow();
let map_fd = map_data.fd; let map_fd = map_data.fd;
let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?; let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?;

@ -372,7 +372,7 @@ pub fn aya::maps::perf::PerfBufferError::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya::maps::perf::PerfBufferError impl<T> core::convert::From<T> for aya::maps::perf::PerfBufferError
pub fn aya::maps::perf::PerfBufferError::from(t: T) -> T pub fn aya::maps::perf::PerfBufferError::from(t: T) -> T
pub struct aya::maps::perf::AsyncPerfEventArray<T> pub struct aya::maps::perf::AsyncPerfEventArray<T>
impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T>
pub fn aya::maps::perf::AsyncPerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::AsyncPerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError> pub fn aya::maps::perf::AsyncPerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::AsyncPerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData> impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>
pub type aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError pub type aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError
@ -404,8 +404,8 @@ impl<T> core::borrow::BorrowMut<T> for aya::maps::perf::AsyncPerfEventArray<T> w
pub fn aya::maps::perf::AsyncPerfEventArray<T>::borrow_mut(&mut self) -> &mut T pub fn aya::maps::perf::AsyncPerfEventArray<T>::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya::maps::perf::AsyncPerfEventArray<T> impl<T> core::convert::From<T> for aya::maps::perf::AsyncPerfEventArray<T>
pub fn aya::maps::perf::AsyncPerfEventArray<T>::from(t: T) -> T pub fn aya::maps::perf::AsyncPerfEventArray<T>::from(t: T) -> T
pub struct aya::maps::perf::AsyncPerfEventArrayBuffer<T> pub struct aya::maps::perf::AsyncPerfEventArrayBuffer<T: core::borrow::BorrowMut<aya::maps::MapData>>
impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArrayBuffer<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArrayBuffer<T>
pub async fn aya::maps::perf::AsyncPerfEventArrayBuffer<T>::read_events(&mut self, buffers: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result<aya::maps::perf::Events, aya::maps::perf::PerfBufferError> pub async fn aya::maps::perf::AsyncPerfEventArrayBuffer<T>::read_events(&mut self, buffers: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result<aya::maps::perf::Events, aya::maps::perf::PerfBufferError>
impl<T> core::marker::Send for aya::maps::perf::AsyncPerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync impl<T> core::marker::Send for aya::maps::perf::AsyncPerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
impl<T> core::marker::Sync for aya::maps::perf::AsyncPerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync impl<T> core::marker::Sync for aya::maps::perf::AsyncPerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
@ -460,7 +460,7 @@ pub fn aya::maps::perf::Events::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya::maps::perf::Events impl<T> core::convert::From<T> for aya::maps::perf::Events
pub fn aya::maps::perf::Events::from(t: T) -> T pub fn aya::maps::perf::Events::from(t: T) -> T
pub struct aya::maps::perf::PerfEventArray<T> pub struct aya::maps::perf::PerfEventArray<T>
impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T>
pub fn aya::maps::perf::PerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::PerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError> pub fn aya::maps::perf::PerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::PerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::PerfEventArray<aya::maps::MapData> impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::PerfEventArray<aya::maps::MapData>
pub type aya::maps::perf::PerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError pub type aya::maps::perf::PerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError
@ -493,10 +493,10 @@ pub fn aya::maps::perf::PerfEventArray<T>::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya::maps::perf::PerfEventArray<T> impl<T> core::convert::From<T> for aya::maps::perf::PerfEventArray<T>
pub fn aya::maps::perf::PerfEventArray<T>::from(t: T) -> T pub fn aya::maps::perf::PerfEventArray<T>::from(t: T) -> T
pub struct aya::maps::perf::PerfEventArrayBuffer<T> pub struct aya::maps::perf::PerfEventArrayBuffer<T>
impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::PerfEventArrayBuffer<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::PerfEventArrayBuffer<T>
pub fn aya::maps::perf::PerfEventArrayBuffer<T>::read_events(&mut self, out_bufs: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result<aya::maps::perf::Events, aya::maps::perf::PerfBufferError> pub fn aya::maps::perf::PerfEventArrayBuffer<T>::read_events(&mut self, out_bufs: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result<aya::maps::perf::Events, aya::maps::perf::PerfBufferError>
pub fn aya::maps::perf::PerfEventArrayBuffer<T>::readable(&self) -> bool pub fn aya::maps::perf::PerfEventArrayBuffer<T>::readable(&self) -> bool
impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> std::os::fd::raw::AsRawFd for aya::maps::perf::PerfEventArrayBuffer<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> std::os::fd::raw::AsRawFd for aya::maps::perf::PerfEventArrayBuffer<T>
pub fn aya::maps::perf::PerfEventArrayBuffer<T>::as_raw_fd(&self) -> std::os::fd::raw::RawFd pub fn aya::maps::perf::PerfEventArrayBuffer<T>::as_raw_fd(&self) -> std::os::fd::raw::RawFd
impl<T> core::marker::Send for aya::maps::perf::PerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync impl<T> core::marker::Send for aya::maps::perf::PerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
impl<T> core::marker::Sync for aya::maps::perf::PerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync impl<T> core::marker::Sync for aya::maps::perf::PerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync
@ -1067,7 +1067,7 @@ pub fn aya::maps::array::Array<T, V>::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya::maps::array::Array<T, V> impl<T> core::convert::From<T> for aya::maps::array::Array<T, V>
pub fn aya::maps::array::Array<T, V>::from(t: T) -> T pub fn aya::maps::array::Array<T, V>::from(t: T) -> T
pub struct aya::maps::AsyncPerfEventArray<T> pub struct aya::maps::AsyncPerfEventArray<T>
impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::AsyncPerfEventArray<T>
pub fn aya::maps::perf::AsyncPerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::AsyncPerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError> pub fn aya::maps::perf::AsyncPerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::AsyncPerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData> impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>
pub type aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError pub type aya::maps::perf::AsyncPerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError
@ -1460,7 +1460,7 @@ pub fn aya::maps::PerCpuValues<T>::borrow_mut(&mut self) -> &mut T
impl<T> core::convert::From<T> for aya::maps::PerCpuValues<T> impl<T> core::convert::From<T> for aya::maps::PerCpuValues<T>
pub fn aya::maps::PerCpuValues<T>::from(t: T) -> T pub fn aya::maps::PerCpuValues<T>::from(t: T) -> T
pub struct aya::maps::PerfEventArray<T> pub struct aya::maps::PerfEventArray<T>
impl<T: core::borrow::BorrowMut<aya::maps::MapData> + core::borrow::Borrow<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T> impl<T: core::borrow::BorrowMut<aya::maps::MapData>> aya::maps::perf::PerfEventArray<T>
pub fn aya::maps::perf::PerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::PerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError> pub fn aya::maps::perf::PerfEventArray<T>::open(&mut self, index: u32, page_count: core::option::Option<usize>) -> core::result::Result<aya::maps::perf::PerfEventArrayBuffer<T>, aya::maps::perf::PerfBufferError>
impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::PerfEventArray<aya::maps::MapData> impl core::convert::TryFrom<aya::maps::Map> for aya::maps::perf::PerfEventArray<aya::maps::MapData>
pub type aya::maps::perf::PerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError pub type aya::maps::perf::PerfEventArray<aya::maps::MapData>::Error = aya::maps::MapError

Loading…
Cancel
Save