diff --git a/src/apple/macos/process.rs b/src/apple/macos/process.rs index 116138650..102a8a6bc 100644 --- a/src/apple/macos/process.rs +++ b/src/apple/macos/process.rs @@ -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, } } diff --git a/src/apple/macos/system.rs b/src/apple/macos/system.rs index 7c8b957a5..4fb752383 100644 --- a/src/apple/macos/system.rs +++ b/src/apple/macos/system.rs @@ -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); } } } diff --git a/src/freebsd/cpu.rs b/src/freebsd/cpu.rs index 217e4ee36..6946b6a7e 100644 --- a/src/freebsd/cpu.rs +++ b/src/freebsd/cpu.rs @@ -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 { diff --git a/src/freebsd/system.rs b/src/freebsd/system.rs index 8955bd0bb..bccb6ad1b 100644 --- a/src/freebsd/system.rs +++ b/src/freebsd/system.rs @@ -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 _) diff --git a/src/linux/process.rs b/src/linux/process.rs index 88757ca60..7c2d599e0 100644 --- a/src/linux/process.rs +++ b/src/linux/process.rs @@ -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); diff --git a/src/linux/system.rs b/src/linux/system.rs index eef5a58ff..25dd08722 100644 --- a/src/linux/system.rs +++ b/src/linux/system.rs @@ -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)) = ( @@ -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 diff --git a/src/windows/cpu.rs b/src/windows/cpu.rs index f32ddf832..3a998868e 100644 --- a/src/windows/cpu.rs +++ b/src/windows/cpu.rs @@ -529,7 +529,12 @@ pub(crate) fn get_physical_core_count() -> Option { } 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 _); diff --git a/src/windows/process.rs b/src/windows/process.rs index 72919a952..0e9ff1cd5 100644 --- a/src/windows/process.rs +++ b/src/windows/process.rs @@ -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, } }