From e34a9fcc8cda01e4a58f59b4a9d990eda8effd60 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Wed, 1 Nov 2023 19:43:28 +0800 Subject: Always update 'values' member of NetworkIOMeter Always update the `values` member during the call of NetworkIOMeter_updateValues(). This makes the values of every instance of NetworkIOMeter 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 --- NetworkIOMeter.c | 109 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 51 deletions(-) (limited to 'NetworkIOMeter.c') diff --git a/NetworkIOMeter.c b/NetworkIOMeter.c index 76ae2ccc..945b9f0c 100644 --- a/NetworkIOMeter.c +++ b/NetworkIOMeter.c @@ -34,20 +34,16 @@ static uint32_t cached_txp_diff; static void NetworkIOMeter_updateValues(Meter* this) { const Machine* host = this->host; - static uint64_t cached_last_update = 0; + static uint64_t cached_last_update = 0; uint64_t passedTimeInMs = host->realtimeMs - cached_last_update; + bool hasNewData = false; + NetworkIOData data; /* update only every 500ms to have a sane span for rate calculation */ if (passedTimeInMs > 500) { - static uint64_t cached_rxb_total; - static uint64_t cached_rxp_total; - static uint64_t cached_txb_total; - static uint64_t cached_txp_total; - uint64_t diff; - - NetworkIOData data; - if (!Platform_getNetworkIO(&data)) { + hasNewData = Platform_getNetworkIO(&data); + if (!hasNewData) { status = RATESTATUS_NODATA; } else if (cached_last_update == 0) { status = RATESTATUS_INIT; @@ -58,51 +54,68 @@ static void NetworkIOMeter_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_rxb_total; + static uint64_t cached_rxp_total; + static uint64_t cached_txb_total; + static uint64_t cached_txp_total; - if (data.bytesReceived > cached_rxb_total) { - diff = data.bytesReceived - cached_rxb_total; - diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ - diff /= ONE_K; /* convert to KiB/s */ - cached_rxb_diff = (uint32_t)diff; - } else { - cached_rxb_diff = 0; + if (status != RATESTATUS_INIT) { + uint64_t diff; + + if (data.bytesReceived > cached_rxb_total) { + diff = data.bytesReceived - cached_rxb_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ + diff /= ONE_K; /* convert to KiB/s */ + cached_rxb_diff = (uint32_t)diff; + } else { + cached_rxb_diff = 0; + } + + if (data.packetsReceived > cached_rxp_total) { + diff = data.packetsReceived - cached_rxp_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to pkts/s */ + cached_rxp_diff = (uint32_t)diff; + } else { + cached_rxp_diff = 0; + } + + if (data.bytesTransmitted > cached_txb_total) { + diff = data.bytesTransmitted - cached_txb_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ + diff /= ONE_K; /* convert to KiB/s */ + cached_txb_diff = (uint32_t)diff; + } else { + cached_txb_diff = 0; + } + + if (data.packetsTransmitted > cached_txp_total) { + diff = data.packetsTransmitted - cached_txp_total; + diff = (1000 * diff) / passedTimeInMs; /* convert to pkts/s */ + cached_txp_diff = (uint32_t)diff; + } else { + cached_txp_diff = 0; + } } - cached_rxb_total = data.bytesReceived; - if (data.packetsReceived > cached_rxp_total) { - diff = data.packetsReceived - cached_rxp_total; - diff = (1000 * diff) / passedTimeInMs; /* convert to pkts/s */ - cached_rxp_diff = (uint32_t)diff; - } else { - cached_rxp_diff = 0; - } + cached_rxb_total = data.bytesReceived; cached_rxp_total = data.packetsReceived; - - if (data.bytesTransmitted > cached_txb_total) { - diff = data.bytesTransmitted - cached_txb_total; - diff = (1000 * diff) / passedTimeInMs; /* convert to B/s */ - diff /= ONE_K; /* convert to KiB/s */ - cached_txb_diff = (uint32_t)diff; - } else { - cached_txb_diff = 0; - } cached_txb_total = data.bytesTransmitted; - - if (data.packetsTransmitted > cached_txp_total) { - diff = data.packetsTransmitted - cached_txp_total; - diff = (1000 * diff) / passedTimeInMs; /* convert to pkts/s */ - cached_txp_diff = (uint32_t)diff; - } else { - cached_txp_diff = 0; - } cached_txp_total = data.packetsTransmitted; } + this->values[0] = cached_rxb_diff; + this->values[1] = cached_txb_diff; + if (cached_rxb_diff + cached_txb_diff > this->total) { + this->total = cached_rxb_diff + cached_txb_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; @@ -112,12 +125,6 @@ static void NetworkIOMeter_updateValues(Meter* this) { return; } - this->values[0] = cached_rxb_diff; - this->values[1] = cached_txb_diff; - if (cached_rxb_diff + cached_txb_diff > this->total) { - this->total = cached_rxb_diff + cached_txb_diff; - } - char bufferBytesReceived[12], bufferBytesTransmitted[12]; Meter_humanUnit(bufferBytesReceived, cached_rxb_diff, sizeof(bufferBytesReceived)); Meter_humanUnit(bufferBytesTransmitted, cached_txb_diff, sizeof(bufferBytesTransmitted)); -- cgit v1.2.3