From f7f6aca39d371a02482a6eb722510360377488f9 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Wed, 1 Nov 2023 19:20:58 +0800 Subject: Always update 'values' member of DiskIOMeter Always update the `values` member during the call of DiskIOMeter_updateValues(). This makes the values of every instance of DiskIOMeter consistent. The user might see non-zero bars (or graph) during the "stale" status of the meter after this commit. The side effect is not a bug. Signed-off-by: Kang-Che Sung --- DiskIOMeter.c | 81 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 37 deletions(-) (limited to 'DiskIOMeter.c') diff --git a/DiskIOMeter.c b/DiskIOMeter.c index b69fb40b..6662918b 100644 --- a/DiskIOMeter.c +++ b/DiskIOMeter.c @@ -36,16 +36,13 @@ static void DiskIOMeter_updateValues(Meter* this) { static uint64_t cached_last_update; uint64_t passedTimeInMs = host->realtimeMs - cached_last_update; + bool hasNewData = false; + DiskIOData data; /* update only every 500ms to have a sane span for rate calculation */ if (passedTimeInMs > 500) { - static uint64_t cached_read_total; - static uint64_t cached_write_total; - static uint64_t cached_msTimeSpend_total; - uint64_t diff; - - DiskIOData data; - if (!Platform_getDiskIO(&data)) { + hasNewData = Platform_getDiskIO(&data); + if (!hasNewData) { status = RATESTATUS_NODATA; } else if (cached_last_update == 0) { status = RATESTATUS_INIT; @@ -56,42 +53,54 @@ static void DiskIOMeter_updateValues(Meter* this) { } cached_last_update = host->realtimeMs; + } - if (status == RATESTATUS_NODATA) { - xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data"); - return; - } + if (hasNewData) { + static uint64_t cached_read_total; + static uint64_t cached_write_total; + static uint64_t cached_msTimeSpend_total; - if (data.totalBytesRead > cached_read_total) { - diff = data.totalBytesRead - cached_read_total; - diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ - diff /= ONE_K; /* convert to KiB/s */ - cached_read_diff = (uint32_t)diff; - } else { - cached_read_diff = 0; + if (status != RATESTATUS_INIT) { + uint64_t diff; + + if (data.totalBytesRead > cached_read_total) { + diff = data.totalBytesRead - cached_read_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ + diff /= ONE_K; /* convert to KiB/s */ + cached_read_diff = (uint32_t)diff; + } else { + cached_read_diff = 0; + } + + if (data.totalBytesWritten > cached_write_total) { + diff = data.totalBytesWritten - cached_write_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ + diff /= ONE_K; /* convert to KiB/s */ + cached_write_diff = (uint32_t)diff; + } else { + cached_write_diff = 0; + } + + if (data.totalMsTimeSpend > cached_msTimeSpend_total) { + diff = data.totalMsTimeSpend - cached_msTimeSpend_total; + cached_utilisation_diff = 100.0 * (double)diff / passedTimeInMs; + cached_utilisation_diff = MINIMUM(cached_utilisation_diff, 100.0); + } else { + cached_utilisation_diff = 0.0; + } } - cached_read_total = data.totalBytesRead; - if (data.totalBytesWritten > cached_write_total) { - diff = data.totalBytesWritten - cached_write_total; - diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ - diff /= ONE_K; /* convert to KiB/s */ - cached_write_diff = (uint32_t)diff; - } else { - cached_write_diff = 0; - } + cached_read_total = data.totalBytesRead; cached_write_total = data.totalBytesWritten; - - if (data.totalMsTimeSpend > cached_msTimeSpend_total) { - diff = data.totalMsTimeSpend - cached_msTimeSpend_total; - cached_utilisation_diff = 100.0 * (double)diff / passedTimeInMs; - cached_utilisation_diff = MINIMUM(cached_utilisation_diff, 100.0); - } else { - cached_utilisation_diff = 0.0; - } cached_msTimeSpend_total = data.totalMsTimeSpend; } + this->values[0] = cached_utilisation_diff; + + if (status == RATESTATUS_NODATA) { + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "no data"); + return; + } if (status == RATESTATUS_INIT) { xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "init"); return; @@ -101,8 +110,6 @@ static void DiskIOMeter_updateValues(Meter* this) { return; } - this->values[0] = cached_utilisation_diff; - char bufferRead[12], bufferWrite[12]; Meter_humanUnit(bufferRead, cached_read_diff, sizeof(bufferRead)); Meter_humanUnit(bufferWrite, cached_write_diff, sizeof(bufferWrite)); -- cgit v1.2.3