diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2023-06-18 06:11:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-18 02:11:57 -0400 |
commit | 7c0eda10347f7e9c96a6fcb04fc69aaa87318d55 (patch) | |
tree | 1d8db797ccdcdd81cbd5ae25aa97fdace0c8dfe7 | |
parent | 9cd953e0ca0ed75413d05151ed06431608b78606 (diff) |
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
-rw-r--r-- | src/app/data_harvester/processes.rs | 4 | ||||
-rw-r--r-- | src/app/data_harvester/processes/linux.rs | 11 | ||||
-rw-r--r-- | src/app/data_harvester/processes/unix/process_ext.rs | 6 | ||||
-rw-r--r-- | src/app/data_harvester/processes/windows.rs | 4 | ||||
-rw-r--r-- | src/app/frozen_state.rs | 1 | ||||
-rw-r--r-- | src/app/query.rs | 7 | ||||
-rw-r--r-- | src/clap.rs | 2 | ||||
-rw-r--r-- | src/widgets/process_table/proc_widget_data.rs | 5 |
8 files changed, 24 insertions, 16 deletions
diff --git a/src/app/data_harvester/processes.rs b/src/app/data_harvester/processes.rs index 44f132d9..5d14930e 100644 --- a/src/app/data_harvester/processes.rs +++ b/src/app/data_harvester/processes.rs @@ -45,10 +45,10 @@ pub struct ProcessHarvest { pub parent_pid: Option<Pid>, /// CPU usage as a percentage. - pub cpu_usage_percent: f64, + pub cpu_usage_percent: f32, /// Memory usage as a percentage. - pub mem_usage_percent: f64, + pub mem_usage_percent: f32, /// Memory usage as bytes. pub mem_usage_bytes: u64, diff --git a/src/app/data_harvester/processes/linux.rs b/src/app/data_harvester/processes/linux.rs index 37341865..89bf9db4 100644 --- a/src/app/data_harvester/processes/linux.rs +++ b/src/app/data_harvester/processes/linux.rs @@ -110,7 +110,7 @@ fn cpu_usage_calculation(prev_idle: &mut f64, prev_non_idle: &mut f64) -> error: fn get_linux_cpu_usage( stat: &Stat, cpu_usage: f64, cpu_fraction: f64, prev_proc_times: u64, use_current_cpu_total: bool, -) -> (f64, u64) { +) -> (f32, u64) { // Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556 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. @@ -118,9 +118,12 @@ fn get_linux_cpu_usage( if cpu_usage == 0.0 { (0.0, new_proc_times) } 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 { - ((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 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! let (total_read_bytes, total_write_bytes, read_bytes_per_sec, write_bytes_per_sec) = diff --git a/src/app/data_harvester/processes/unix/process_ext.rs b/src/app/data_harvester/processes/unix/process_ext.rs index 1048fd2d..999a893f 100644 --- a/src/app/data_harvester/processes/unix/process_ext.rs +++ b/src/app/data_harvester/processes/unix/process_ext.rs @@ -61,7 +61,7 @@ pub(crate) trait UnixProcessExt { pcu / cpu_usage } else { pcu - }; + } as f32; let disk_usage = process_val.disk_usage(); let process_state = { @@ -76,7 +76,7 @@ pub(crate) trait UnixProcessExt { name, command, 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 { 0.0 }, @@ -114,7 +114,7 @@ pub(crate) trait UnixProcessExt { *cpu_usages.get(&process.pid).unwrap() } else { *cpu_usages.get(&process.pid).unwrap() / num_processors - }; + } as f32; } } } diff --git a/src/app/data_harvester/processes/windows.rs b/src/app/data_harvester/processes/windows.rs index 3e30e52e..b0b93b16 100644 --- a/src/app/data_harvester/processes/windows.rs +++ b/src/app/data_harvester/processes/windows.rs @@ -63,7 +63,7 @@ pub fn sysinfo_process_data( pcu / cpu_usage } else { pcu - }; + } as f32; let disk_usage = process_val.disk_usage(); 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 } else { 0.0 - }, + } as f32, mem_usage_bytes: process_val.memory(), cpu_usage_percent: process_cpu_usage, read_bytes_per_sec: disk_usage.read_bytes, diff --git a/src/app/frozen_state.rs b/src/app/frozen_state.rs index 4fc7293d..6be62651 100644 --- a/src/app/frozen_state.rs +++ b/src/app/frozen_state.rs @@ -37,6 +37,7 @@ impl FrozenState { self.thaw(); false } else { + // Could we use an Arc instead? Is it worth it? self.freeze(Box::new(data.clone())); true } diff --git a/src/app/query.rs b/src/app/query.rs index 17cc1c44..67131893 100644 --- a/src/app/query.rs +++ b/src/app/query.rs @@ -711,7 +711,12 @@ impl Prefix { } 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 { QueryComparison::Equal => (lhs - rhs).abs() < std::f64::EPSILON, QueryComparison::Less => lhs < rhs, diff --git a/src/clap.rs b/src/clap.rs index 91fb63cb..36b4c0d8 100644 --- a/src/clap.rs +++ b/src/clap.rs @@ -257,7 +257,7 @@ pub fn build_app() -> Command { 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") .long("color") .action(ArgAction::Set) diff --git a/src/widgets/process_table/proc_widget_data.rs b/src/widgets/process_table/proc_widget_data.rs index 59d26ba3..a466f58d 100644 --- a/src/widgets/process_table/proc_widget_data.rs +++ b/src/widgets/process_table/proc_widget_data.rs @@ -82,10 +82,9 @@ impl Display for Id { } } -// TODO: Can reduce this to 32 bytes. #[derive(PartialEq, Clone, Debug)] pub enum MemUsage { - Percent(f64), + Percent(f32), Bytes(u64), } @@ -170,7 +169,7 @@ pub struct ProcWidgetData { pub pid: Pid, pub ppid: Option<Pid>, pub id: Id, - pub cpu_usage_percent: f64, + pub cpu_usage_percent: f32, pub mem_usage: MemUsage, pub rps: u64, pub wps: u64, |