Skip to content

Commit

Permalink
aya: move mmap from perf_buffer.rs to sys/mod.rs
Browse files Browse the repository at this point in the history
mmap() is needed for the ring buffer implementation, so move it to a common module
  • Loading branch information
willfindlay authored and tamird committed Oct 11, 2023
1 parent b73c0a4 commit 4af9d1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
23 changes: 2 additions & 21 deletions aya/src/maps/perf/perf_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
ffi::{c_int, c_void},
ffi::c_void,
io, mem,
os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd},
ptr, slice,
Expand All @@ -15,7 +15,7 @@ use crate::{
perf_event_header, perf_event_mmap_page,
perf_event_type::{PERF_RECORD_LOST, PERF_RECORD_SAMPLE},
},
sys::{perf_event_ioctl, perf_event_open_bpf, SysResult},
sys::{mmap, perf_event_ioctl, perf_event_open_bpf, SysResult},
PERF_EVENT_IOC_DISABLE, PERF_EVENT_IOC_ENABLE,
};

Expand Down Expand Up @@ -282,25 +282,6 @@ impl Drop for PerfBuffer {
}
}

#[cfg_attr(test, allow(unused_variables))]
unsafe fn mmap(
addr: *mut c_void,
len: usize,
prot: c_int,
flags: c_int,
fd: BorrowedFd<'_>,
offset: libc::off_t,
) -> *mut c_void {
#[cfg(not(test))]
return libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset);

#[cfg(test)]
use crate::sys::TEST_MMAP_RET;

#[cfg(test)]
TEST_MMAP_RET.with(|ret| *ret.borrow())
}

#[derive(Debug)]
#[repr(C)]
struct Sample {
Expand Down
18 changes: 17 additions & 1 deletion aya/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod perf_event;
mod fake;

use std::{
ffi::{c_int, c_long},
ffi::{c_int, c_long, c_void},
io, mem,
os::fd::{AsRawFd as _, BorrowedFd},
};
Expand Down Expand Up @@ -115,3 +115,19 @@ fn syscall(call: Syscall<'_>) -> SysResult<c_long> {
ret => Err((ret, io::Error::last_os_error())),
}
}

#[cfg_attr(test, allow(unused_variables))]
pub(crate) unsafe fn mmap(
addr: *mut c_void,
len: usize,
prot: c_int,
flags: c_int,
fd: BorrowedFd<'_>,
offset: libc::off_t,
) -> *mut c_void {
#[cfg(not(test))]
return libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset);

#[cfg(test)]
TEST_MMAP_RET.with(|ret| *ret.borrow())
}

0 comments on commit 4af9d1b

Please sign in to comment.