Skip to content

Commit

Permalink
Use Instant to calculate the period time
Browse files Browse the repository at this point in the history
  • Loading branch information
jfernandez committed Feb 14, 2024
1 parent 72783a4 commit d974fd6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 48 deletions.
66 changes: 24 additions & 42 deletions src/bpf_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*/

use std::time::Instant;

#[derive(Clone)]
pub struct BpfProgram {
pub id: String,
Expand All @@ -25,8 +27,8 @@ pub struct BpfProgram {
pub run_time_ns: u64,
pub prev_run_cnt: u64,
pub run_cnt: u64,
pub prev_timestamp_ns: u128,
pub timestamp_ns: u128,
pub instant: Instant,
pub period_ns: u128,
}

impl BpfProgram {
Expand Down Expand Up @@ -55,24 +57,20 @@ impl BpfProgram {
self.run_cnt - self.prev_run_cnt
}

pub fn timestamp_delta(&self) -> u128 {
self.timestamp_ns - self.prev_timestamp_ns
}

pub fn events_per_second(&self) -> i64 {
if self.timestamp_delta() == 0 {
if self.period_ns == 0 {
return 0;
}
let events_per_second =
self.run_cnt_delta() as f64 / self.timestamp_delta() as f64 * 1_000_000_000.0;
self.run_cnt_delta() as f64 / self.period_ns as f64 * 1_000_000_000.0;
events_per_second.round() as i64
}

pub fn cpu_time_percent(&self) -> f64 {
if self.run_time_ns == 0 {
if self.period_ns == 0 {
return 0.0;
}
self.runtime_delta() as f64 / self.timestamp_delta() as f64 * 100.0
self.runtime_delta() as f64 / self.period_ns as f64 * 100.0
}
}

Expand All @@ -90,8 +88,8 @@ mod tests {
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
prev_timestamp_ns: 1000,
timestamp_ns: 2000,
instant: Instant::now(),
period_ns: 0,
};
assert_eq!(prog.period_average_runtime_ns(), 100);
}
Expand All @@ -106,8 +104,8 @@ mod tests {
run_time_ns: 1000,
prev_run_cnt: 1,
run_cnt: 5,
prev_timestamp_ns: 1000,
timestamp_ns: 2000,
instant: Instant::now(),
period_ns: 1000,
};
assert_eq!(prog.total_average_runtime_ns(), 200);
}
Expand All @@ -122,8 +120,8 @@ mod tests {
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
prev_timestamp_ns: 1000,
timestamp_ns: 2000,
instant: Instant::now(),
period_ns: 0,
};
assert_eq!(prog.runtime_delta(), 100);
}
Expand All @@ -138,28 +136,12 @@ mod tests {
run_time_ns: 200,
prev_run_cnt: 5,
run_cnt: 8,
prev_timestamp_ns: 1000,
timestamp_ns: 2000,
instant: Instant::now(),
period_ns: 0,
};
assert_eq!(prog.run_cnt_delta(), 3);
}

#[test]
fn test_timestamp_delta() {
let prog = BpfProgram {
id: "test".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
run_cnt: 2,
prev_timestamp_ns: 1000,
timestamp_ns: 3000,
};
assert_eq!(prog.timestamp_delta(), 2000);
}

#[test]
fn test_events_per_second() {
let prog = BpfProgram {
Expand All @@ -170,8 +152,8 @@ mod tests {
run_time_ns: 200,
prev_run_cnt: 10,
run_cnt: 50,
prev_timestamp_ns: 1_000_000_000,
timestamp_ns: 2_000_000_000,
instant: Instant::now(),
period_ns: 1_000_000_000,
};
assert_eq!(prog.events_per_second(), 40);
}
Expand All @@ -182,14 +164,14 @@ mod tests {
id: "test".to_string(),
bpf_type: "test".to_string(),
name: "test".to_string(),
prev_runtime_ns: 100,
run_time_ns: 200,
prev_run_cnt: 1,
prev_runtime_ns: 100_000_000,
run_time_ns: 200_000_000,
prev_run_cnt: 0,
run_cnt: 2,
prev_timestamp_ns: 1000,
timestamp_ns: 2000,
instant: Instant::now(),
period_ns: 1_000_000_000,
};
// Calculate expected value: (200 - 100) / (2000 - 1000) * 100 = 10.0
// Calculate expected value: (200_000_000 - 100_000_000) / 1_000_000_000 * 100 = 10.0
let expected = 10.0;
assert_eq!(prog.cpu_time_percent(), expected);
}
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::fs::File;
use std::io;
use std::os::fd::FromRawFd;
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
use std::time::{Duration, Instant};
use std::{println, thread, vec};
use std::collections::HashMap;

Expand Down Expand Up @@ -84,14 +84,14 @@ impl App {

let iter = ProgInfoIter::default();
for prog in iter {
let timestamp_ns = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos();
let instant = Instant::now();

let prog_name = prog.name.to_str().unwrap().to_string();

if prog_name.is_empty() {
continue;
}

let mut bpf_program = BpfProgram {
id: prog.id.to_string(),
bpf_type: prog.ty.to_string(),
Expand All @@ -100,14 +100,14 @@ impl App {
run_time_ns: prog.run_time_ns,
prev_run_cnt: 0,
run_cnt: prog.run_cnt,
prev_timestamp_ns: 0,
timestamp_ns,
instant,
period_ns: 0,
};

if let Some(prev_bpf_program) = map.get(&bpf_program.id) {
bpf_program.prev_runtime_ns = prev_bpf_program.run_time_ns;
bpf_program.prev_run_cnt = prev_bpf_program.run_cnt;
bpf_program.prev_timestamp_ns = prev_bpf_program.timestamp_ns;
bpf_program.period_ns = prev_bpf_program.instant.elapsed().as_nanos();
}

items.push(bpf_program);
Expand Down

0 comments on commit d974fd6

Please sign in to comment.