async_perf_event_array: access inner through async

Avoid holding onto raw file descriptors.
reviewable/pr775/r1
Tamir Duberstein 2 years ago
parent b84d1da605
commit 145b3ba32d
No known key found for this signature in database

@ -1,8 +1,5 @@
use bytes::BytesMut;
use std::{
borrow::Borrow,
os::fd::{AsRawFd as _, RawFd},
};
use std::borrow::Borrow;
// See https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features.
//
@ -103,16 +100,11 @@ impl<T: Borrow<MapData>> AsyncPerfEventArray<T> {
) -> Result<AsyncPerfEventArrayBuffer<T>, PerfBufferError> {
let Self { perf_map } = self;
let buf = perf_map.open(index, page_count)?;
let fd = buf.as_raw_fd();
Ok(AsyncPerfEventArrayBuffer {
buf,
#[cfg(feature = "async_tokio")]
async_tokio_fd: AsyncFd::new(fd)?,
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_std_fd: Async::new(fd)?,
})
#[cfg(feature = "async_tokio")]
let buf = AsyncFd::new(buf)?;
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
let buf = Async::new(buf)?;
Ok(AsyncPerfEventArrayBuffer { buf })
}
}
@ -131,14 +123,15 @@ impl<T: Borrow<MapData>> AsyncPerfEventArray<T> {
///
/// See the [`AsyncPerfEventArray` documentation](AsyncPerfEventArray) for an overview of how to
/// use perf buffers.
pub struct AsyncPerfEventArrayBuffer<T> {
pub struct AsyncPerfEventArrayBuffer<T: Borrow<MapData>> {
#[cfg(not(any(feature = "async_tokio", feature = "async_std")))]
buf: PerfEventArrayBuffer<T>,
#[cfg(feature = "async_tokio")]
async_tokio_fd: AsyncFd<RawFd>,
buf: AsyncFd<PerfEventArrayBuffer<T>>,
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_std_fd: Async<RawFd>,
buf: Async<PerfEventArrayBuffer<T>>,
}
impl<T: Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
@ -155,21 +148,20 @@ impl<T: Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
&mut self,
buffers: &mut [BytesMut],
) -> Result<Events, PerfBufferError> {
let Self {
buf,
#[cfg(feature = "async_tokio")]
async_tokio_fd,
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_std_fd,
} = self;
let Self { buf } = self;
loop {
#[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"))]
if !buf.readable() {
async_std_fd.readable().await?;
}
let buf = {
if !buf.get_ref().readable() {
buf.readable().await?;
}
buf.get_mut()
};
let events = buf.read_events(buffers)?;
const EMPTY: Events = Events { read: 0, lost: 0 };

@ -180,7 +180,6 @@ impl<T: Borrow<MapData>> PerfEventArray<T> {
) -> Result<PerfEventArrayBuffer<T>, PerfBufferError> {
// 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_fd = map_data.fd;
let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?;

@ -404,7 +404,7 @@ 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
impl<T> core::convert::From<T> for aya::maps::perf::AsyncPerfEventArray<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::Borrow<aya::maps::MapData>>
impl<T: core::borrow::Borrow<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>
impl<T> core::marker::Send for aya::maps::perf::AsyncPerfEventArrayBuffer<T> where T: core::marker::Send + core::marker::Sync

Loading…
Cancel
Save