From e7372d18a1a661d8c3dba9f51e1f17b5f94171a7 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Wed, 10 Jan 2024 11:17:08 +0100 Subject: New upstream version 3.3.0 --- NetworkIOMeter.c | 170 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 92 insertions(+), 78 deletions(-) (limited to 'NetworkIOMeter.c') diff --git a/NetworkIOMeter.c b/NetworkIOMeter.c index dd91b75..6bc2d08 100644 --- a/NetworkIOMeter.c +++ b/NetworkIOMeter.c @@ -1,16 +1,24 @@ +/* +htop - NetworkIOMeter.c +(C) 2020-2023 htop dev team +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "config.h" // IWYU pragma: keep + #include "NetworkIOMeter.h" #include -#include #include "CRT.h" +#include "Machine.h" #include "Macros.h" #include "Meter.h" #include "Object.h" #include "Platform.h" -#include "Process.h" -#include "ProcessList.h" #include "RichString.h" +#include "Row.h" #include "XUtils.h" @@ -20,27 +28,25 @@ static const int NetworkIOMeter_attributes[] = { }; static MeterRateStatus status = RATESTATUS_INIT; -static uint32_t cached_rxb_diff; +static double cached_rxb_diff; +static char cached_rxb_diff_str[6]; static uint32_t cached_rxp_diff; -static uint32_t cached_txb_diff; +static double cached_txb_diff; +static char cached_txb_diff_str[6]; static uint32_t cached_txp_diff; static void NetworkIOMeter_updateValues(Meter* this) { - const ProcessList* pl = this->pl; - static uint64_t cached_last_update = 0; + const Machine* host = this->host; - uint64_t passedTimeInMs = pl->realtimeMs - cached_last_update; + 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; @@ -50,50 +56,69 @@ static void NetworkIOMeter_updateValues(Meter* this) { status = RATESTATUS_DATA; } - cached_last_update = pl->realtimeMs; + 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 /= ONE_K; /* Meter_humanUnit() expects unit in kilo */ - diff = (1000 * diff) / passedTimeInMs; /* convert to per second */ - 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 */ + cached_rxb_diff = diff; + } else { + cached_rxb_diff = 0; + } + Meter_humanUnit(cached_rxb_diff_str, cached_rxb_diff / ONE_K, sizeof(cached_rxb_diff_str)); + + 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 */ + cached_txb_diff = diff; + } else { + cached_txb_diff = 0; + } + Meter_humanUnit(cached_txb_diff_str, cached_txb_diff / ONE_K, sizeof(cached_txb_diff_str)); + + 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; - 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 /= ONE_K; /* Meter_humanUnit() expects unit in kilo */ - diff = (1000 * diff) / passedTimeInMs; /* convert to per second */ - 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; - 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; @@ -103,47 +128,36 @@ 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)); - xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "rx:%siB/s tx:%siB/s", bufferBytesReceived, bufferBytesTransmitted); + xSnprintf(this->txtBuffer, sizeof(this->txtBuffer), "rx:%siB/s tx:%siB/s %u/%upkts/s", + cached_rxb_diff_str, cached_txb_diff_str, cached_rxp_diff, cached_txp_diff); } static void NetworkIOMeter_display(ATTR_UNUSED const Object* cast, RichString* out) { switch (status) { - case RATESTATUS_NODATA: - RichString_writeAscii(out, CRT_colors[METER_VALUE_ERROR], "no data"); - return; - case RATESTATUS_INIT: - RichString_writeAscii(out, CRT_colors[METER_VALUE], "initializing..."); - return; - case RATESTATUS_STALE: - RichString_writeAscii(out, CRT_colors[METER_VALUE_WARN], "stale data"); - return; - case RATESTATUS_DATA: - break; + case RATESTATUS_NODATA: + RichString_writeAscii(out, CRT_colors[METER_VALUE_ERROR], "no data"); + return; + case RATESTATUS_INIT: + RichString_writeAscii(out, CRT_colors[METER_VALUE], "initializing..."); + return; + case RATESTATUS_STALE: + RichString_writeAscii(out, CRT_colors[METER_VALUE_WARN], "stale data"); + return; + case RATESTATUS_DATA: + break; } char buffer[64]; - int len; RichString_writeAscii(out, CRT_colors[METER_TEXT], "rx: "); - Meter_humanUnit(buffer, cached_rxb_diff, sizeof(buffer)); - RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], buffer); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], cached_rxb_diff_str); RichString_appendAscii(out, CRT_colors[METER_VALUE_IOREAD], "iB/s"); RichString_appendAscii(out, CRT_colors[METER_TEXT], " tx: "); - Meter_humanUnit(buffer, cached_txb_diff, sizeof(buffer)); - RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer); + RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], cached_txb_diff_str); RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s"); - len = xSnprintf(buffer, sizeof(buffer), " (%u/%u packets) ", cached_rxp_diff, cached_txp_diff); + int len = xSnprintf(buffer, sizeof(buffer), " (%u/%u pkts/s) ", cached_rxp_diff, cached_txp_diff); RichString_appendnAscii(out, CRT_colors[METER_TEXT], buffer, len); } -- cgit v1.2.3