Skip to content

Commit

Permalink
Add feature to display graphs for selected program
Browse files Browse the repository at this point in the history
  • Loading branch information
jfernandez committed Feb 19, 2024
1 parent 354b871 commit ca67f07
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build/
target/
root/
root/
deploy.sh
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ regex = "1.8.1"
anyhow = "1.0.71"
ratatui = { version = "0.26.1", default-features = false, features = ['crossterm'] }
nix = "0.27.1"

[profile.release]
debug = false
circular-buffer = "0.1.6"
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: build

build:
cross build --release
92 changes: 88 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,51 @@ use std::{
time::{Duration, Instant},
};

use circular_buffer::CircularBuffer;
use libbpf_rs::query::ProgInfoIter;
use ratatui::widgets::TableState;

use crate::bpf_program::BpfProgram;

pub struct App {
pub state: TableState,
pub state: Arc<Mutex<TableState>>,
pub items: Arc<Mutex<Vec<BpfProgram>>>,
pub data_buf: Arc<Mutex<CircularBuffer<20, PeriodMeasure>>>,
pub show_graphs: bool,
pub max_cpu: f64,
pub max_eps: i64,
pub max_runtime: u64,
}

pub struct PeriodMeasure {
pub cpu_time_percent: f64,
pub events_per_sec: i64,
pub average_runtime_ns: u64,
}

impl App {
pub fn new() -> App {
App {
state: TableState::default(),
state: Arc::new(Mutex::new(TableState::default())),
items: Arc::new(Mutex::new(vec![])),
data_buf: Arc::new(Mutex::new(CircularBuffer::<20, PeriodMeasure>::new())),
show_graphs: false,
max_cpu: 0.0,
max_eps: 0,
max_runtime: 0,
}
}

pub fn start_background_thread(&self) {
let items_clone = Arc::clone(&self.items);
let items = Arc::clone(&self.items);
let data_buf = Arc::clone(&self.data_buf);
let state = Arc::clone(&self.state);

thread::spawn(move || loop {
// Lock items for this thread's exclusive use.
let mut items = items_clone.lock().unwrap();
let mut items = items.lock().unwrap();
let mut data_buf = data_buf.lock().unwrap();
let state = state.lock().unwrap();

let items_copy = items.clone();
let map: HashMap<String, &BpfProgram> = items_copy
Expand Down Expand Up @@ -85,10 +106,73 @@ impl App {
items.push(bpf_program);
}

if let Some(index) = state.selected() {
let bpf_program = &items[index];
data_buf.push_back(PeriodMeasure {
cpu_time_percent: bpf_program.cpu_time_percent(),
events_per_sec: bpf_program.events_per_second(),
average_runtime_ns: bpf_program.period_average_runtime_ns(),
});
}

// Explicitly drop the MutexGuard returned by lock() to unlock before sleeping.
drop(items);
drop(data_buf);
drop(state);

thread::sleep(Duration::from_secs(1));
});
}

pub fn toggle_graphs(&mut self) {
self.data_buf.lock().unwrap().clear();
self.max_cpu = 0.0;
self.max_eps = 0;
self.max_runtime = 0;
self.show_graphs = !self.show_graphs;
}

pub fn selected_program(&self) -> Option<BpfProgram> {
let items = self.items.lock().unwrap().clone();
let state = self.state.lock().unwrap();

match state.selected() {
Some(i) => Some(items[i].clone()),
None => None,
}
}

pub fn next(&mut self) {
let items = self.items.lock().unwrap().clone();
let mut state = self.state.lock().unwrap();

let i = match state.selected() {
Some(i) => {
if i >= items.len() - 1 {
0
} else {
i + 1
}
}
None => 0,
};
state.select(Some(i));
}

pub fn previous(&mut self) {
let items = self.items.lock().unwrap().clone();
let mut state = self.state.lock().unwrap();

let i = match state.selected() {
Some(i) => {
if i == 0 {
items.len() - 1
} else {
i - 1
}
}
None => 0,
};
state.select(Some(i));
}
}
Loading

0 comments on commit ca67f07

Please sign in to comment.