Browse Source

aya: expose run_time_ns and run_cnt fields in ProgramInfo

Added functions to expose `run_time_ns` & `run_cnt` statistics from
ProgramInfo/bpf_prog_info.
tyrone-wu 8 months ago
parent
commit
a25f501ece

+ 22 - 0
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<P: AsRef<Path>>(path: P) -> Result<Self, ProgramError> {
         use std::os::unix::ffi::OsStrExt as _;

+ 2 - 1
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,
 }

+ 2 - 0
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]

+ 2 - 0
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