Skip to content

Commit

Permalink
fix: The cpu usage now takes the num_cores into account.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Willems <jw@elevenbits.com>
  • Loading branch information
jw committed Mar 8, 2024
1 parent 80da813 commit 3ec74f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 5 additions & 2 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};
use procfs::{ticks_per_second, CpuInfo, Current, Uptime};
use ratatui::layout::Constraint::Percentage;
use ratatui::widgets::block::Position;
use ratatui::widgets::{
Expand Down Expand Up @@ -149,7 +149,10 @@ fn get_cpu(process: &Process) -> f64 {
let runtime = uptime - starttime;
debug!("runtime: {}s", runtime);

usage as f64 * 100.0 / runtime as f64
let num_cores = CpuInfo::current().unwrap().num_cores();
debug!("Uptime: {}s", uptime);

usage as f64 * 100.0 / runtime as f64 / num_cores as f64
}

fn main() -> Result<()> {
Expand Down
6 changes: 4 additions & 2 deletions src/processbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use log::debug;
use owo_colors::OwoColorize;
use procfs::process::Process;
use procfs::{page_size, ticks_per_second, Current, Uptime};
use procfs::{page_size, ticks_per_second, CpuInfo, Current, Uptime};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -33,7 +33,9 @@ fn main() -> Result<()> {
debug!("Starttime: {}", starttime);
let runtime = uptime - starttime;
debug!("runtime: {}", runtime);
let percentage = usage as f64 * 100.0 / runtime as f64;
let num_cores = CpuInfo::current().unwrap().num_cores();
debug!("num cores: {}", num_cores);
let percentage = usage as f64 * 100.0 / runtime as f64 / num_cores as f64;
println!(
"{} ({}) has used {:.2}% of the cpu.",
stat.comm.green(),
Expand Down

0 comments on commit 3ec74f3

Please sign in to comment.