Skip to content

Commit

Permalink
other: use f32 for process percentage values (#1212)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ClementTsang authored Jun 18, 2023
1 parent 9cd953e commit 7c0eda1
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/app/data_harvester/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions src/app/data_harvester/processes/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,20 @@ 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.

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,
)
}
}

Expand Down Expand Up @@ -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) =
Expand Down
6 changes: 3 additions & 3 deletions src/app/data_harvester/processes/unix/process_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
},
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/data_harvester/processes/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/app/frozen_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion src/app/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/widgets/process_table/proc_widget_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7c0eda1

Please sign in to comment.