Skip to content

Commit

Permalink
integration-test: fix load time and add test
Browse files Browse the repository at this point in the history
Time since boot is defined as the UNIX_EPOCH plus the duration
since boot. which is realtime - boottime NOT boottime - realtime.

Add a integration test to ensure this doesn't happen again.

Signed-off-by: astoycos <[email protected]>
  • Loading branch information
astoycos committed Sep 22, 2023
1 parent 42fd82e commit cc97658
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion aya/src/programs/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) fn boot_time() -> SystemTime {
};
let since_boot = get_time(libc::CLOCK_BOOTTIME);
let since_epoch = get_time(libc::CLOCK_REALTIME);
UNIX_EPOCH + since_boot - since_epoch
UNIX_EPOCH + since_epoch - since_boot
}

/// Get the specified information from a file descriptor's fdinfo.
Expand Down
28 changes: 26 additions & 2 deletions test/integration-test/src/tests/load.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{convert::TryInto as _, thread, time};
use std::{
convert::TryInto as _,
thread,
time::{Duration, SystemTime},
};

use aya::{
maps::Array,
Expand All @@ -11,7 +15,7 @@ use aya::{
};

const MAX_RETRIES: usize = 100;
const RETRY_DURATION: time::Duration = time::Duration::from_millis(10);
const RETRY_DURATION: Duration = Duration::from_millis(10);

#[test]
fn long_name() {
Expand Down Expand Up @@ -145,6 +149,26 @@ fn unload_xdp() {
assert_unloaded("pass");
}

#[test]
fn test_loaded_at() {
let mut bpf = Bpf::load(crate::TEST).unwrap();
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
let load_time = SystemTime::now();
prog.load().unwrap();
assert_loaded("pass");

let loaded_at = prog.info().unwrap().loaded_at();

// make sure loaded_at() api is correct with a millisecond tolerance.
assert_eq!(
(loaded_at.duration_since(load_time).unwrap()).as_millis(),
0
);
prog.unload().unwrap();

assert_unloaded("pass");
}

#[test]
fn unload_kprobe() {
let mut bpf = Bpf::load(crate::TEST).unwrap();
Expand Down

0 comments on commit cc97658

Please sign in to comment.