diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index 4b12c6bb..898070e5 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -1000,6 +1000,7 @@ impl_info!( ); /// Provides information about a loaded program, like name, id and statistics +#[doc(alias = "bpf_prog_info")] #[derive(Debug)] pub struct ProgramInfo(bpf_prog_info); @@ -1099,6 +1100,27 @@ impl ProgramInfo { Ok(ProgramFd(fd)) } + /// The accumulated time that the program has been actively running. + /// + /// This is not to be confused with the duration since the program was + /// first loaded on the host. + /// + /// Note this field is only updated for as long as + /// [`enable_stats`](crate::sys::enable_stats) is enabled + /// with [`Stats::RunTime`](crate::sys::Stats::RunTime). + pub fn run_time(&self) -> Duration { + Duration::from_nanos(self.0.run_time_ns) + } + + /// The accumulated execution count of the program. + /// + /// Note this field is only updated for as long as + /// [`enable_stats`](crate::sys::enable_stats) is enabled + /// with [`Stats::RunTime`](crate::sys::Stats::RunTime). + pub fn run_count(&self) -> u64 { + self.0.run_cnt + } + /// Loads a program from a pinned path in bpffs. pub fn from_pin>(path: P) -> Result { use std::os::unix::ffi::OsStrExt as _; diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index 98307bb5..05f8b4dd 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -146,7 +146,8 @@ pub(crate) unsafe fn mmap( #[doc(alias = "bpf_stats_type")] #[derive(Copy, Clone, Debug)] pub enum Stats { - /// Tracks `run_time_ns` and `run_cnt`. + /// Tracks [`run_time`](crate::programs::ProgramInfo::run_time) and + /// [`run_count`](crate::programs::ProgramInfo::run_count) fields. #[doc(alias = "BPF_STATS_RUN_TIME")] RunTime, } diff --git a/test/integration-test/src/tests/smoke.rs b/test/integration-test/src/tests/smoke.rs index 708b889b..b5bb30a2 100644 --- a/test/integration-test/src/tests/smoke.rs +++ b/test/integration-test/src/tests/smoke.rs @@ -98,6 +98,8 @@ fn list_loaded_programs() { prog.verified_instruction_count(); prog.loaded_at(); prog.fd().unwrap(); + prog.run_time(); + prog.run_count(); } #[test] diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index 21fe6ae4..b47fc579 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -7923,6 +7923,8 @@ pub fn aya::programs::ProgramInfo::memory_locked(&self) -> core::result::Result< pub fn aya::programs::ProgramInfo::name(&self) -> &[u8] pub fn aya::programs::ProgramInfo::name_as_str(&self) -> core::option::Option<&str> pub fn aya::programs::ProgramInfo::program_type(&self) -> u32 +pub fn aya::programs::ProgramInfo::run_count(&self) -> u64 +pub fn aya::programs::ProgramInfo::run_time(&self) -> core::time::Duration pub fn aya::programs::ProgramInfo::size_jitted(&self) -> u32 pub fn aya::programs::ProgramInfo::size_translated(&self) -> u32 pub fn aya::programs::ProgramInfo::tag(&self) -> u64