Skip to content

Commit

Permalink
feat(prctl): add perf events and I/O flusher interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cptpcrd committed Jun 26, 2022
1 parent 21fbc33 commit 12ec8ba
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/prctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,77 @@ pub fn get_tid_address() -> crate::Result<*mut libc::c_int> {
Ok(addr)
}

/// Enable all performance counters attached to the current process.
///
/// This is the opposite of [`disable_perf_events()].
#[inline]
pub fn enable_perf_events() -> crate::Result<()> {
unsafe {
crate::raw_prctl(
libc::PR_TASK_PERF_EVENTS_ENABLE,
0,
0,
0,
0,
)?;
}

Ok(())
}

/// Disable all performance counters attached to the current process.
///
/// Performance counters are created using perf_event_open(2) and can be used to measure performance
/// information.
///
/// See prctl(2) for more information.
#[inline]
pub fn disable_perf_events() -> crate::Result<()> {
unsafe {
crate::raw_prctl(
libc::PR_TASK_PERF_EVENTS_DISABLE,
0,
0,
0,
0,
)?;
}

Ok(())
}

/// Get the "I/O flusher" flag for the current process.
///
/// See [`set_io_flusher()`] for more details.
#[inline]
pub fn get_io_flusher() -> crate::Result<bool> {
let res = unsafe { crate::raw_prctl(crate::sys::PR_GET_IO_FLUSHER, 0, 0, 0, 0) }?;

Ok(res != 0)
}

/// Set the "I/O flusher" flag for the current process. (Linux 5.6+)
///
/// User processes which are involved in filesystem I/O (e.g. FUSE daemons) and which may allocate
/// memory while handling requests should set this flag to `true`. This gives the process special
/// treatment when it tries to allocate memory; see prctl(2) for details.
///
/// Changing the I/O flusher status requires the `CAP_SYS_RESOURCE` capability.
#[inline]
pub fn set_io_flusher(flusher: bool) -> crate::Result<()> {
unsafe {
crate::raw_prctl(
crate::sys::PR_SET_IO_FLUSHER,
flusher as libc::c_ulong,
0,
0,
0,
)?;
}

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub const PR_SPEC_DISABLE: libc::c_int = 1 << 2;
pub const PR_SPEC_FORCE_DISABLE: libc::c_int = 1 << 3;
pub const PR_SPEC_DISABLE_NOEXEC: libc::c_int = 1 << 4;

pub const PR_SET_IO_FLUSHER: libc::c_int = 57;
pub const PR_GET_IO_FLUSHER: libc::c_int = 58;

// File capabilities constants
#[cfg(feature = "std")]
mod file {
Expand Down

0 comments on commit 12ec8ba

Please sign in to comment.