reviewable/pr637/r10
Andrew Stoycos 2 years ago
parent 365ed3a614
commit a034e9aa99
No known key found for this signature in database
GPG Key ID: 66735B92BB71C096

@ -71,7 +71,7 @@ use std::{
io,
os::unix::io::{AsRawFd, RawFd},
path::{Path, PathBuf},
time::{Duration, SystemTime, UNIX_EPOCH},
time::{Duration, SystemTime},
};
use thiserror::Error;
@ -109,7 +109,7 @@ use crate::{
maps::MapError,
obj::{self, btf::BtfError, Function},
pin::PinError,
programs::utils::{get_fdinfo, time_since_boot, time_since_epoch},
programs::utils::{get_fdinfo, time_since_boot},
sys::{
bpf_btf_get_fd_by_id, bpf_get_object, bpf_load_program, bpf_pin_object,
bpf_prog_get_fd_by_id, bpf_prog_get_info_by_fd, bpf_prog_get_next_id, bpf_prog_query,
@ -1001,15 +1001,14 @@ impl ProgramInfo {
self.0.id
}
/// The program tag is a SHA sum of the program's instructions which can be
/// used as an additional program identifier. A program's id can vary every time
/// it's loaded or unloaded, but the tag will remain the same.
/// The program tag is a SHA sum of the program's instructions which be used as an alternative to
/// [`Self::id()`]". A program's id can vary every time it's loaded or unloaded, but the tag
/// will remain the same.
pub fn tag(&self) -> u64 {
u64::from_be_bytes(self.0.tag)
}
/// The type of a bpf program expressed as an integer. To understand the
/// integer to type mappings please see the linux kernel enum
/// The program type as defined by the linux kernel enum
/// [`bpf_prog_type`](https://elixir.bootlin.com/linux/v6.4.4/source/include/uapi/linux/bpf.h#L948).
pub fn program_type(&self) -> u32 {
self.0.type_
@ -1073,13 +1072,9 @@ impl ProgramInfo {
///
/// The load time is specified by the kernel as nanoseconds since system boot,
/// this function converts that u64 value to a [`std::time::SystemTime`]
/// for easy consumption. It is calculated by first finding the realtime of system
/// boot (i.e the [`Duration`] since [`std::time::UNIX_EPOCH`] minus the [`Duration`]
/// since boot), adding the load_time [`Duration`], and finally converting the
/// [`Duration`] to a readable [`SystemTime`] by adding to [`std::time::UNIX_EPOCH`].
/// for easy consumption.
pub fn loaded_at(&self) -> SystemTime {
UNIX_EPOCH
+ ((time_since_epoch() - time_since_boot()) + Duration::from_nanos(self.0.load_time))
time_since_boot(Duration::from_nanos(self.0.load_time))
}
/// Returns the fd associated with the program.

@ -6,13 +6,9 @@ use std::{
io::{BufRead, BufReader},
os::unix::io::RawFd,
path::Path,
time::Duration,
time::{Duration, SystemTime, UNIX_EPOCH},
};
// for docs link
#[allow(unused)]
use std::time::UNIX_EPOCH;
use crate::{
programs::{FdLink, Link, ProgramData, ProgramError},
sys::bpf_raw_tracepoint_open,
@ -64,28 +60,19 @@ pub(crate) fn find_tracefs_path() -> Result<&'static Path, ProgramError> {
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "tracefs not found").into())
}
/// Get time since boot. The returned duration represents the combined
/// seconds and nanoseconds since the machine was booted.
pub(crate) fn time_since_boot() -> Duration {
/// Get the real (wall-clock) time of a duration since boot.
pub(crate) fn time_since_boot(dur_since_load: Duration) -> SystemTime {
let mut time = unsafe { std::mem::zeroed::<libc::timespec>() };
let ret = unsafe { libc::clock_gettime(libc::CLOCK_BOOTTIME, &mut time) };
assert_eq!(ret, 0, "failed to get system bootime");
let tv_sec = time.tv_sec as u64;
let tv_nsec = time.tv_nsec as u32;
Duration::new(tv_sec, tv_nsec)
}
/// Get the system-wide real (wall-clock) time. The returned Duration represents
/// the combined seconds and nanoseconds since [`std::time::UNIX_EPOCH`].
pub(crate) fn time_since_epoch() -> Duration {
let mut time = unsafe { std::mem::zeroed::<libc::timespec>() };
let dur_since_boot = Duration::new(time.tv_sec as u64, time.tv_nsec as u32);
let ret = unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut time) };
assert_eq!(ret, 0, "failed to get system realtime");
let tv_sec = time.tv_sec as u64;
let tv_nsec = time.tv_nsec as u32;
Duration::new(tv_sec, tv_nsec)
let dur_since_epoch = Duration::new(time.tv_sec as u64, time.tv_nsec as u32);
UNIX_EPOCH + ((dur_since_epoch - dur_since_boot) + dur_since_load)
}
/// Get the specified information from a file descriptor's fdinfo.
@ -99,7 +86,7 @@ pub(crate) fn get_fdinfo(fd: RawFd, key: &str) -> Result<u32, ProgramError> {
continue;
}
let val = l.rsplit_once('\t').unwrap().1;
let (_key, val) = l.rsplit_once('\t').unwrap();
return Ok(val.parse().unwrap());
}

Loading…
Cancel
Save