mirror of
https://github.com/ClementTsang/bottom
synced 2025-02-18 14:18:27 +00:00
Some refactoring... I also tried to make the processes part mutlithreaded, but that saved negliglble time and increase cpu usage...
This commit is contained in:
parent
e05b5c46fe
commit
7a6d8f088c
1 changed files with 41 additions and 31 deletions
|
@ -116,10 +116,9 @@ fn get_process_cpu_stats(pid: u32) -> std::io::Result<f64> {
|
||||||
/// Note that cpu_fraction should be represented WITHOUT the \times 100 factor!
|
/// Note that cpu_fraction should be represented WITHOUT the \times 100 factor!
|
||||||
fn linux_cpu_usage<S: core::hash::BuildHasher>(
|
fn linux_cpu_usage<S: core::hash::BuildHasher>(
|
||||||
pid: u32, cpu_usage: f64, cpu_fraction: f64,
|
pid: u32, cpu_usage: f64, cpu_fraction: f64,
|
||||||
prev_pid_stats: &HashMap<String, (f64, Instant), S>,
|
prev_pid_stats: &HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool,
|
||||||
new_pid_stats: &mut HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool,
|
|
||||||
curr_time: Instant,
|
curr_time: Instant,
|
||||||
) -> std::io::Result<f64> {
|
) -> std::io::Result<(f64, (String, (f64, Instant)))> {
|
||||||
// Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556
|
// Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556
|
||||||
let before_proc_val: f64 = if prev_pid_stats.contains_key(&pid.to_string()) {
|
let before_proc_val: f64 = if prev_pid_stats.contains_key(&pid.to_string()) {
|
||||||
prev_pid_stats
|
prev_pid_stats
|
||||||
|
@ -140,27 +139,36 @@ fn linux_cpu_usage<S: core::hash::BuildHasher>(
|
||||||
(after_proc_val - before_proc_val) / cpu_usage * 100_f64
|
(after_proc_val - before_proc_val) / cpu_usage * 100_f64
|
||||||
);*/
|
);*/
|
||||||
|
|
||||||
new_pid_stats.insert(pid.to_string(), (after_proc_val, curr_time));
|
let new_dict_entry = (pid.to_string(), (after_proc_val, curr_time));
|
||||||
if use_current_cpu_total {
|
if use_current_cpu_total {
|
||||||
Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64)
|
Ok((
|
||||||
|
(after_proc_val - before_proc_val) / cpu_usage * 100_f64,
|
||||||
|
new_dict_entry,
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
Ok((after_proc_val - before_proc_val) / cpu_usage * 100_f64 * cpu_fraction)
|
Ok((
|
||||||
|
(after_proc_val - before_proc_val) / cpu_usage * 100_f64 * cpu_fraction,
|
||||||
|
new_dict_entry,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn convert_ps<S: core::hash::BuildHasher>(
|
fn convert_ps<S: core::hash::BuildHasher>(
|
||||||
process: &str, cpu_usage: f64, cpu_fraction: f64,
|
process: &str, cpu_usage: f64, cpu_fraction: f64,
|
||||||
prev_pid_stats: &HashMap<String, (f64, Instant), S>,
|
prev_pid_stats: &HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool,
|
||||||
new_pid_stats: &mut HashMap<String, (f64, Instant), S>, use_current_cpu_total: bool,
|
|
||||||
curr_time: Instant,
|
curr_time: Instant,
|
||||||
) -> std::io::Result<ProcessHarvest> {
|
) -> std::io::Result<(ProcessHarvest, (String, (f64, Instant)))> {
|
||||||
if process.trim().to_string().is_empty() {
|
if process.trim().to_string().is_empty() {
|
||||||
return Ok(ProcessHarvest {
|
let dummy_result = (String::default(), (0.0, Instant::now()));
|
||||||
pid: 0,
|
return Ok((
|
||||||
name: "".to_string(),
|
ProcessHarvest {
|
||||||
mem_usage_percent: 0.0,
|
pid: 0,
|
||||||
cpu_usage_percent: 0.0,
|
name: "".to_string(),
|
||||||
});
|
mem_usage_percent: 0.0,
|
||||||
|
cpu_usage_percent: 0.0,
|
||||||
|
},
|
||||||
|
dummy_result,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let pid = (&process[..11])
|
let pid = (&process[..11])
|
||||||
|
@ -175,20 +183,23 @@ fn convert_ps<S: core::hash::BuildHasher>(
|
||||||
.parse::<f64>()
|
.parse::<f64>()
|
||||||
.unwrap_or(0_f64);
|
.unwrap_or(0_f64);
|
||||||
|
|
||||||
Ok(ProcessHarvest {
|
let (cpu_usage_percent, new_entry) = linux_cpu_usage(
|
||||||
pid,
|
pid,
|
||||||
name,
|
cpu_usage,
|
||||||
mem_usage_percent,
|
cpu_fraction,
|
||||||
cpu_usage_percent: linux_cpu_usage(
|
prev_pid_stats,
|
||||||
|
use_current_cpu_total,
|
||||||
|
curr_time,
|
||||||
|
)?;
|
||||||
|
Ok((
|
||||||
|
ProcessHarvest {
|
||||||
pid,
|
pid,
|
||||||
cpu_usage,
|
name,
|
||||||
cpu_fraction,
|
mem_usage_percent,
|
||||||
prev_pid_stats,
|
cpu_usage_percent: cpu_usage_percent,
|
||||||
new_pid_stats,
|
},
|
||||||
use_current_cpu_total,
|
new_entry,
|
||||||
curr_time,
|
))
|
||||||
)?,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_sorted_processes_list(
|
pub fn get_sorted_processes_list(
|
||||||
|
@ -199,8 +210,6 @@ pub fn get_sorted_processes_list(
|
||||||
let mut process_vector: Vec<ProcessHarvest> = Vec::new();
|
let mut process_vector: Vec<ProcessHarvest> = Vec::new();
|
||||||
|
|
||||||
if cfg!(target_os = "linux") {
|
if cfg!(target_os = "linux") {
|
||||||
// Linux specific - this is a massive pain... ugh.
|
|
||||||
|
|
||||||
let ps_result = Command::new("ps")
|
let ps_result = Command::new("ps")
|
||||||
.args(&["-axo", "pid:10,comm:50,%mem:5", "--noheader"])
|
.args(&["-axo", "pid:10,comm:50,%mem:5", "--noheader"])
|
||||||
.output()?;
|
.output()?;
|
||||||
|
@ -213,18 +222,19 @@ pub fn get_sorted_processes_list(
|
||||||
let mut new_pid_stats: HashMap<String, (f64, Instant), RandomState> = HashMap::new();
|
let mut new_pid_stats: HashMap<String, (f64, Instant), RandomState> = HashMap::new();
|
||||||
|
|
||||||
for process in process_stream {
|
for process in process_stream {
|
||||||
if let Ok(process_object) = convert_ps(
|
if let Ok((process_object, new_entry)) = convert_ps(
|
||||||
process,
|
process,
|
||||||
cpu_usage,
|
cpu_usage,
|
||||||
cpu_fraction,
|
cpu_fraction,
|
||||||
&prev_pid_stats,
|
&prev_pid_stats,
|
||||||
&mut new_pid_stats,
|
|
||||||
use_current_cpu_total,
|
use_current_cpu_total,
|
||||||
curr_time,
|
curr_time,
|
||||||
) {
|
) {
|
||||||
if !process_object.name.is_empty() {
|
if !process_object.name.is_empty() {
|
||||||
process_vector.push(process_object);
|
process_vector.push(process_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new_pid_stats.insert(new_entry.0, new_entry.1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue