Skip to content

Commit

Permalink
feat: The cpu usage is now shown as the rightmost column.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Willems <[email protected]>
  • Loading branch information
jw committed Mar 7, 2024
1 parent 1dbd9e3 commit 80da813
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crossterm::{
};
use log::{debug, info};
use procfs::process::Process;
use procfs::{ticks_per_second, Current, Uptime, WithCurrentSystemInfo};
use procfs::{ticks_per_second, Current, Uptime};
use ratatui::layout::Constraint::Percentage;
use ratatui::widgets::block::Position;
use ratatui::widgets::{
Expand Down Expand Up @@ -136,14 +136,20 @@ fn get_memory(process: &Process) -> u64 {

fn get_cpu(process: &Process) -> f64 {
let stat = process.stat().unwrap();
info!("{}: starttime: {}", process.pid, stat.starttime);
info!("utime: {}", (stat.utime / ticks_per_second()) as f64);
info!("stime: {}", stat.stime / ticks_per_second());
info!("u+s: {}", (stat.utime + stat.stime) / ticks_per_second());

info!("Uptime: {:?}", Uptime::current().unwrap().uptime_duration());
info!("rss_bytes: {}", stat.rss_bytes().get());
0.0

let usage = stat.utime / ticks_per_second() + stat.stime / ticks_per_second();
debug!("usage: {}s", usage);

let uptime = Uptime::current().unwrap().uptime_duration().as_secs();
debug!("Uptime: {}s", uptime);

let starttime = stat.starttime / ticks_per_second();
debug!("Starttime: {}s", starttime);

let runtime = uptime - starttime;
debug!("runtime: {}s", runtime);

usage as f64 * 100.0 / runtime as f64
}

fn main() -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn create_row<'a>(process: &BrtProcess) -> Row<'a> {
),
Cell::new(username),
Cell::new(format_size(process.resident_memory, humansize_options)).style(special_style),
Cell::new("n/a".to_string()).style(special_style), // TODO: Get CPU
Cell::new(format!("{:.2}", process.cpu)).style(special_style),
])
}

Expand Down

0 comments on commit 80da813

Please sign in to comment.