From 918644c4e6c9ccdf635a72ee40781a23d4a4d6af Mon Sep 17 00:00:00 2001 From: Maxim <59533214+biryukovmaxim@users.noreply.github.com> Date: Mon, 2 Oct 2023 14:05:00 +0400 Subject: [PATCH] Add warning for negative IO delta values (#277) Added a warning message in case the new IO read or write bytes values are less than the previous values. This was done using the unwrap_or_else() function to prevent the program from potentially crashing when the subtraction operation results in a negative value. --- metrics/perf_monitor/src/lib.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/metrics/perf_monitor/src/lib.rs b/metrics/perf_monitor/src/lib.rs index 5a805553d..e7215d71d 100644 --- a/metrics/perf_monitor/src/lib.rs +++ b/metrics/perf_monitor/src/lib.rs @@ -4,7 +4,7 @@ use kaspa_core::{ service::{AsyncService, AsyncServiceFuture}, tick::{TickReason, TickService}, }, - trace, + trace, warn, }; use workflow_perf_monitor::{ cpu::{processor_numbers, ProcessStat}, @@ -48,8 +48,15 @@ impl> Monitor { let IOStats { read_bytes: disk_io_read_bytes, write_bytes: disk_io_write_bytes, .. } = get_process_io_stats()?; let time_delta = last_log_time.elapsed(); - let read_delta = disk_io_read_bytes - last_read; - let write_delta = disk_io_write_bytes - last_written; + + let read_delta = disk_io_read_bytes.checked_sub(last_read).unwrap_or_else(|| { + warn!("new io read bytes value is less than previous, new: {disk_io_read_bytes}, previous: {last_read}"); + 0 + }); + let write_delta = disk_io_write_bytes.checked_sub(last_written).unwrap_or_else(|| { + warn!("new io write bytes value is less than previous, new: {disk_io_write_bytes}, previous: {last_written}"); + 0 + }); let now = Instant::now(); last_log_time = now;