Skip to content

Commit

Permalink
feat: Added bin that shows the cpu usage of a process.
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 64761d5 commit aeac113
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 3 deletions.
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: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ rust-version = "1.74.1"
[[bin]]
name = "brt"

[[bin]]
name = "processbar"
path = "src/processbar.rs"

[dependencies]
crossterm = "0.27.0"
Expand All @@ -29,6 +32,7 @@ procfs = "0.16.0"
uzers = "0.11.3"
nix = "0.28.0"
humansize = "2.1.3"
owo-colors = "4.0.0"


[dev-dependencies.cargo-husky]
Expand Down
2 changes: 2 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ toolchain = "nightly"
command = "cargo"
args = [
"rustdoc",
"--bin",
"brt",
"--no-default-features",
"--",
"-Zunstable-options",
Expand Down
21 changes: 18 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crossterm::{
};
use log::{debug, info};
use procfs::process::Process;
use procfs::{ticks_per_second, Current, Uptime, WithCurrentSystemInfo};
use ratatui::layout::Constraint::Percentage;
use ratatui::widgets::block::Position;
use ratatui::widgets::{
Expand All @@ -26,6 +27,7 @@ use ratatui::{

mod logger;
mod model;
mod processbar;

const NAME: &str = env!("CARGO_PKG_NAME");
const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -118,6 +120,7 @@ impl App {
}
}

#[allow(dead_code)]
fn get_current_process() -> Process {
let me = Process::myself().unwrap();
let resident_mem = get_memory(&me);
Expand All @@ -126,16 +129,28 @@ fn get_current_process() -> Process {
}

fn get_memory(process: &Process) -> u64 {
let stat = process.statm().unwrap();
let statm = process.statm().unwrap();
let page_size = procfs::page_size();
stat.resident * page_size
statm.resident * page_size
}

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
}

fn main() -> Result<()> {
logger::initialize_logging();
initialize_panic_handler();

info!("{NAME} ({VERSION}) started.");
get_current_process();

let _cli = Cli::parse();

Expand Down
6 changes: 6 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub struct BrtProcess {
number_of_threads: i64,
user: Option<User>,
resident_memory: u64,
cpu: f64,
}

impl Default for BrtProcess {
Expand All @@ -88,6 +89,7 @@ impl Default for BrtProcess {
number_of_threads: -1,
user: None,
resident_memory: 0,
cpu: 0.0,
}
}
}
Expand Down Expand Up @@ -136,6 +138,10 @@ fn create_process(process: &Process) -> Option<BrtProcess> {
// memory
let resident_memory = crate::get_memory(process);
brt_process.resident_memory = resident_memory;

// cpu
let cpu = crate::get_cpu(process);
brt_process.cpu = cpu
}
Err(_e) => {
warn!("Stat not found for process {}.", process.pid().to_string());
Expand Down
47 changes: 47 additions & 0 deletions src/processbar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use anyhow::{Context, Result};
use clap::Parser;
use log::debug;
use owo_colors::OwoColorize;
use procfs::process::Process;
use procfs::{page_size, ticks_per_second, Current, Uptime};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(long)]
pid: i32,
}

#[allow(dead_code)]
fn main() -> Result<()> {
let args = Args::parse();

let pid = args.pid;

debug!("Checking pid {}...", pid);
let process = Process::new(pid).with_context(|| format!("Pid {pid} not found."))?;
let stat = process.stat().unwrap();

debug!("ticks per second: {}", ticks_per_second());
debug!("pagesize: {}", page_size());
let usage = stat.utime / ticks_per_second() + stat.stime / ticks_per_second();
debug!("usage {} ", usage);

let uptime = Uptime::current().unwrap().uptime_duration().as_secs();
debug!("Uptime: {}", uptime);
let starttime = stat.starttime / ticks_per_second();
debug!("Starttime: {}", starttime);
let runtime = uptime - starttime;
debug!("runtime: {}", runtime);
let percentage = usage * 100 / runtime;
println!(
"{} ({}) has used {}% of the cpu.",
stat.comm.green(),
pid.yellow(),
percentage.red(),
);
Ok(())
}

#[derive(Debug)]
struct ProcessbarError(String);

0 comments on commit aeac113

Please sign in to comment.