Skip to content

Commit

Permalink
fix: The correct resident memory size is chown in the memory column.
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 1, 2024
1 parent dc3b2c1 commit 64761d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
22 changes: 7 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use log::info;
use log::{debug, info};
use procfs::process::Process;
use ratatui::layout::Constraint::Percentage;
use ratatui::widgets::block::Position;
Expand Down Expand Up @@ -118,32 +118,24 @@ impl App {
}
}

#[allow(dead_code)]
fn get_current_process() -> Process {
let me = Process::myself().unwrap();
let (virtual_mem, resident_mem) = get_memory(&me);
info!(
"Current pid {}; uses {}/{} bytes ({:02.2}%).",
me.pid,
virtual_mem,
resident_mem,
resident_mem as f64 / virtual_mem as f64 * 100.0
);
let resident_mem = get_memory(&me);
debug!("Current pid {} uses {} bytes.", me.pid, resident_mem);
me
}

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

fn main() -> Result<()> {
logger::initialize_logging();
initialize_panic_handler();
info!("{NAME} ({VERSION}) started.");
get_current_process();

let _cli = Cli::parse();

Expand Down
11 changes: 4 additions & 7 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use humansize::{format_size, FormatSizeOptions, DECIMAL};
use humansize::{format_size, FormatSizeOptions, BINARY};
use log::warn;
use procfs::process::{all_processes, Process};
use ratatui::layout::Alignment;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn create_row<'a>(process: &BrtProcess) -> Row<'a> {

let special_style = Style::default().fg(Color::Rgb(0x0D, 0xE7, 0x56));

let humansize_options: FormatSizeOptions = FormatSizeOptions::from(DECIMAL)
let humansize_options: FormatSizeOptions = FormatSizeOptions::from(BINARY)
.space_after_value(false)
.decimal_places(1)
.decimal_zeroes(0);
Expand All @@ -50,7 +50,7 @@ pub fn create_row<'a>(process: &BrtProcess) -> Row<'a> {
.style(special_style),
),
Cell::new(username),
Cell::new(format_size(process.virtual_memory, humansize_options)).style(special_style),
Cell::new(format_size(process.resident_memory, humansize_options)).style(special_style),
Cell::new("n/a".to_string()).style(special_style), // TODO: Get CPU
])
}
Expand All @@ -75,7 +75,6 @@ pub struct BrtProcess {
command: String,
number_of_threads: i64,
user: Option<User>,
virtual_memory: u64,
resident_memory: u64,
}

Expand All @@ -88,7 +87,6 @@ impl Default for BrtProcess {
command: "".to_string(),
number_of_threads: -1,
user: None,
virtual_memory: 0,
resident_memory: 0,
}
}
Expand Down Expand Up @@ -136,8 +134,7 @@ fn create_process(process: &Process) -> Option<BrtProcess> {
}

// memory
let (virtual_memory, resident_memory) = crate::get_memory(process);
brt_process.virtual_memory = virtual_memory;
let resident_memory = crate::get_memory(process);
brt_process.resident_memory = resident_memory;
}
Err(_e) => {
Expand Down

0 comments on commit 64761d5

Please sign in to comment.