Skip to content

Commit

Permalink
Add missing saturating_* calls
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 13, 2023
1 parent 5e4aed2 commit 19c0b13
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/apple/macos/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ impl ProcessExt for Process {

fn disk_usage(&self) -> DiskUsage {
DiskUsage {
read_bytes: self.read_bytes - self.old_read_bytes,
read_bytes: self.read_bytes.saturating_sub(self.old_read_bytes),
total_read_bytes: self.read_bytes,
written_bytes: self.written_bytes - self.old_written_bytes,
written_bytes: self.written_bytes.saturating_sub(self.old_written_bytes),
total_written_bytes: self.written_bytes,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/apple/macos/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl SystemTimeInfo {
&*self.old_cpu_info.cpu_load.offset(i as _);
for (new, old) in new_load.cpu_ticks.iter().zip(old_load.cpu_ticks.iter()) {
if new > old {
total += new - old;
total += new.saturating_sub(old);

Check failure on line 120 in src/apple/macos/system.rs

View workflow job for this annotation

GitHub Actions / Check stable / x86_64-apple-darwin

mismatched types

Check failure on line 120 in src/apple/macos/system.rs

View workflow job for this annotation

GitHub Actions / Check nightly / x86_64-apple-darwin

mismatched types
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/freebsd/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ impl CpusWrapper {
// We obviously don't want to get the idle part of the CPU usage, otherwise
// we would always be at 100%...
if i != libc::CP_IDLE as usize {
cp_diff += new_cp_time[i] - old_cp_time[i];
cp_diff = cp_diff.saturating_add(new_cp_time[i].saturating_sub(old_cp_time[i]));
}
let mut tmp: u64 = new_cp_time[i] as _;
total_new += tmp;
tmp = old_cp_time[i] as _;
total_old += tmp;
total_new = total_new.saturating_add(new_cp_time[i] as _);
total_old = total_old.saturating_add(old_cp_time[i] as _);
}

let total_diff = total_new - total_old;
let total_diff = total_new.saturating_sub(total_old);
if total_diff < 1 {
proc_.cpu_usage = 0.;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/freebsd/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ impl SystemInfo {
// We need to subtract "ZFS ARC" from the "wired memory" because it should belongs to cache
// but the kernel reports it as "wired memory" instead...
if let Some(arc_size) = self.zfs.arc_size() {
mem_wire -= arc_size;
mem_wire = mem_wire.saturating_sub(arc_size);
}
mem_active
.saturating_mul(self.page_size as _)
Expand Down
6 changes: 4 additions & 2 deletions src/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ pub(crate) fn compute_cpu_usage(p: &mut Process, total_time: f32, max_value: f32

// We use `max_value` to ensure that the process CPU usage will never get bigger than:
// `"number of CPUs" * 100.`
p.cpu_usage = ((p.utime.saturating_sub(p.old_utime) + p.stime.saturating_sub(p.old_stime))
as f32
p.cpu_usage = (p
.utime
.saturating_sub(p.old_utime)
.saturating_add(p.stime.saturating_sub(p.old_stime)) as f32
/ total_time
* 100.)
.min(max_value);
Expand Down
13 changes: 7 additions & 6 deletions src/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,12 @@ impl SystemExt for System {
// So it should fallback to the old way of estimating available memory
// https://github.com/KittyKatt/screenFetch/issues/386#issuecomment-249312716
if !mem_available_found {
self.mem_available = self.mem_free
+ self.mem_buffers
+ self.mem_page_cache
+ self.mem_slab_reclaimable
- self.mem_shmem;
self.mem_available = self
.mem_free
.saturating_add(self.mem_buffers)
.saturating_add(self.mem_page_cache)
.saturating_add(self.mem_slab_reclaimable)
.saturating_sub(self.mem_shmem);
}

if let (Some(mem_cur), Some(mem_max)) = (
Expand All @@ -300,7 +301,7 @@ impl SystemExt for System {
_ => return,
};
*field = value;
self.mem_free -= value;
self.mem_free = self.mem_free.saturating_sub(value);
});
} else if let (Some(mem_cur), Some(mem_max)) = (
// cgroups v1
Expand Down
7 changes: 6 additions & 1 deletion src/windows/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,12 @@ pub(crate) fn get_physical_core_count() -> Option<usize> {
} else {
break;
}
buf.reserve(needed_size as usize - buf.capacity());
let reserve = if needed_size as usize > buf.capacity() {
needed_size as usize - buf.capacity()
} else {
1
};
buf.reserve(reserve);
}

buf.set_len(needed_size as _);
Expand Down
4 changes: 2 additions & 2 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ impl ProcessExt for Process {

fn disk_usage(&self) -> DiskUsage {
DiskUsage {
written_bytes: self.written_bytes - self.old_written_bytes,
written_bytes: self.written_bytes.saturating_sub(self.old_written_bytes),
total_written_bytes: self.written_bytes,
read_bytes: self.read_bytes - self.old_read_bytes,
read_bytes: self.read_bytes.saturating_sub(self.old_read_bytes),
total_read_bytes: self.read_bytes,
}
}
Expand Down

0 comments on commit 19c0b13

Please sign in to comment.