mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-25 22:55:06 +02:00
other: use f32 for process percentage values (#1212)
* other: use f32 for process percentage values This cuts down memory by a tiny bit, and we don't need a full f64 for percentage values. * fix for macos and windows
This commit is contained in:
parent
9cd953e0ca
commit
7c0eda1034
@ -45,10 +45,10 @@ pub struct ProcessHarvest {
|
|||||||
pub parent_pid: Option<Pid>,
|
pub parent_pid: Option<Pid>,
|
||||||
|
|
||||||
/// CPU usage as a percentage.
|
/// CPU usage as a percentage.
|
||||||
pub cpu_usage_percent: f64,
|
pub cpu_usage_percent: f32,
|
||||||
|
|
||||||
/// Memory usage as a percentage.
|
/// Memory usage as a percentage.
|
||||||
pub mem_usage_percent: f64,
|
pub mem_usage_percent: f32,
|
||||||
|
|
||||||
/// Memory usage as bytes.
|
/// Memory usage as bytes.
|
||||||
pub mem_usage_bytes: u64,
|
pub mem_usage_bytes: u64,
|
||||||
|
@ -110,7 +110,7 @@ fn cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> error:
|
|||||||
fn get_linux_cpu_usage(
|
fn get_linux_cpu_usage(
|
||||||
stat: &Stat, cpu_usage: f64, cpu_fraction: f64, prev_proc_times: u64,
|
stat: &Stat, cpu_usage: f64, cpu_fraction: f64, prev_proc_times: u64,
|
||||||
use_current_cpu_total: bool,
|
use_current_cpu_total: bool,
|
||||||
) -> (f64, u64) {
|
) -> (f32, u64) {
|
||||||
// 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 new_proc_times = stat.utime + stat.stime;
|
let new_proc_times = stat.utime + stat.stime;
|
||||||
let diff = (new_proc_times - prev_proc_times) as f64; // No try_from for u64 -> f64... oh well.
|
let diff = (new_proc_times - prev_proc_times) as f64; // No try_from for u64 -> f64... oh well.
|
||||||
@ -118,9 +118,12 @@ fn get_linux_cpu_usage(
|
|||||||
if cpu_usage == 0.0 {
|
if cpu_usage == 0.0 {
|
||||||
(0.0, new_proc_times)
|
(0.0, new_proc_times)
|
||||||
} else if use_current_cpu_total {
|
} else if use_current_cpu_total {
|
||||||
((diff / cpu_usage) * 100.0, new_proc_times)
|
(((diff / cpu_usage) * 100.0) as f32, new_proc_times)
|
||||||
} else {
|
} else {
|
||||||
((diff / cpu_usage) * 100.0 * cpu_fraction, new_proc_times)
|
(
|
||||||
|
((diff / cpu_usage) * 100.0 * cpu_fraction) as f32,
|
||||||
|
new_proc_times,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,7 +184,7 @@ fn read_proc(
|
|||||||
);
|
);
|
||||||
let parent_pid = Some(stat.ppid);
|
let parent_pid = Some(stat.ppid);
|
||||||
let mem_usage_bytes = stat.rss_bytes();
|
let mem_usage_bytes = stat.rss_bytes();
|
||||||
let mem_usage_percent = mem_usage_bytes as f64 / total_memory as f64 * 100.0;
|
let mem_usage_percent = (mem_usage_bytes as f64 / total_memory as f64 * 100.0) as f32;
|
||||||
|
|
||||||
// This can fail if permission is denied!
|
// This can fail if permission is denied!
|
||||||
let (total_read_bytes, total_write_bytes, read_bytes_per_sec, write_bytes_per_sec) =
|
let (total_read_bytes, total_write_bytes, read_bytes_per_sec, write_bytes_per_sec) =
|
||||||
|
@ -61,7 +61,7 @@ pub(crate) trait UnixProcessExt {
|
|||||||
pcu / cpu_usage
|
pcu / cpu_usage
|
||||||
} else {
|
} else {
|
||||||
pcu
|
pcu
|
||||||
};
|
} as f32;
|
||||||
|
|
||||||
let disk_usage = process_val.disk_usage();
|
let disk_usage = process_val.disk_usage();
|
||||||
let process_state = {
|
let process_state = {
|
||||||
@ -76,7 +76,7 @@ pub(crate) trait UnixProcessExt {
|
|||||||
name,
|
name,
|
||||||
command,
|
command,
|
||||||
mem_usage_percent: if total_memory > 0 {
|
mem_usage_percent: if total_memory > 0 {
|
||||||
process_val.memory() as f64 * 100.0 / total_memory as f64
|
(process_val.memory() as f64 * 100.0 / total_memory as f64) as f32
|
||||||
} else {
|
} else {
|
||||||
0.0
|
0.0
|
||||||
},
|
},
|
||||||
@ -114,7 +114,7 @@ pub(crate) trait UnixProcessExt {
|
|||||||
*cpu_usages.get(&process.pid).unwrap()
|
*cpu_usages.get(&process.pid).unwrap()
|
||||||
} else {
|
} else {
|
||||||
*cpu_usages.get(&process.pid).unwrap() / num_processors
|
*cpu_usages.get(&process.pid).unwrap() / num_processors
|
||||||
};
|
} as f32;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ pub fn sysinfo_process_data(
|
|||||||
pcu / cpu_usage
|
pcu / cpu_usage
|
||||||
} else {
|
} else {
|
||||||
pcu
|
pcu
|
||||||
};
|
} as f32;
|
||||||
|
|
||||||
let disk_usage = process_val.disk_usage();
|
let disk_usage = process_val.disk_usage();
|
||||||
let process_state = (process_val.status().to_string(), 'R');
|
let process_state = (process_val.status().to_string(), 'R');
|
||||||
@ -76,7 +76,7 @@ pub fn sysinfo_process_data(
|
|||||||
process_val.memory() as f64 * 100.0 / total_memory as f64
|
process_val.memory() as f64 * 100.0 / total_memory as f64
|
||||||
} else {
|
} else {
|
||||||
0.0
|
0.0
|
||||||
},
|
} as f32,
|
||||||
mem_usage_bytes: process_val.memory(),
|
mem_usage_bytes: process_val.memory(),
|
||||||
cpu_usage_percent: process_cpu_usage,
|
cpu_usage_percent: process_cpu_usage,
|
||||||
read_bytes_per_sec: disk_usage.read_bytes,
|
read_bytes_per_sec: disk_usage.read_bytes,
|
||||||
|
@ -37,6 +37,7 @@ impl FrozenState {
|
|||||||
self.thaw();
|
self.thaw();
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
|
// Could we use an Arc instead? Is it worth it?
|
||||||
self.freeze(Box::new(data.clone()));
|
self.freeze(Box::new(data.clone()));
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -711,7 +711,12 @@ impl Prefix {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn check(&self, process: &ProcessHarvest, is_using_command: bool) -> bool {
|
pub fn check(&self, process: &ProcessHarvest, is_using_command: bool) -> bool {
|
||||||
fn matches_condition(condition: &QueryComparison, lhs: f64, rhs: f64) -> bool {
|
fn matches_condition<I: Into<f64>, J: Into<f64>>(
|
||||||
|
condition: &QueryComparison, lhs: I, rhs: J,
|
||||||
|
) -> bool {
|
||||||
|
let lhs: f64 = lhs.into();
|
||||||
|
let rhs: f64 = rhs.into();
|
||||||
|
|
||||||
match condition {
|
match condition {
|
||||||
QueryComparison::Equal => (lhs - rhs).abs() < std::f64::EPSILON,
|
QueryComparison::Equal => (lhs - rhs).abs() < std::f64::EPSILON,
|
||||||
QueryComparison::Less => lhs < rhs,
|
QueryComparison::Less => lhs < rhs,
|
||||||
|
@ -257,7 +257,7 @@ pub fn build_app() -> Command {
|
|||||||
If it doesn't exist, one is created.",
|
If it doesn't exist, one is created.",
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Fix this, its broken in the manpage
|
// TODO: File an issue with manpage, it cannot render charts correctly.
|
||||||
let color = Arg::new("color")
|
let color = Arg::new("color")
|
||||||
.long("color")
|
.long("color")
|
||||||
.action(ArgAction::Set)
|
.action(ArgAction::Set)
|
||||||
|
@ -82,10 +82,9 @@ impl Display for Id {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Can reduce this to 32 bytes.
|
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
pub enum MemUsage {
|
pub enum MemUsage {
|
||||||
Percent(f64),
|
Percent(f32),
|
||||||
Bytes(u64),
|
Bytes(u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,7 +169,7 @@ pub struct ProcWidgetData {
|
|||||||
pub pid: Pid,
|
pub pid: Pid,
|
||||||
pub ppid: Option<Pid>,
|
pub ppid: Option<Pid>,
|
||||||
pub id: Id,
|
pub id: Id,
|
||||||
pub cpu_usage_percent: f64,
|
pub cpu_usage_percent: f32,
|
||||||
pub mem_usage: MemUsage,
|
pub mem_usage: MemUsage,
|
||||||
pub rps: u64,
|
pub rps: u64,
|
||||||
pub wps: u64,
|
pub wps: u64,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user