Switch NetworkIO Meter to using uint32_t and uint64_t

From review via @BenBE, this is now a whole lot cleaner.
This commit is contained in:
Nathan Scott 2021-03-02 12:14:44 +11:00
parent 2d1839289e
commit 88a11859a0
3 changed files with 26 additions and 27 deletions

View File

@ -19,26 +19,26 @@ static const int NetworkIOMeter_attributes[] = {
static bool hasData = false; static bool hasData = false;
static unsigned long int cached_rxb_diff; static uint32_t cached_rxb_diff;
static unsigned long int cached_rxp_diff; static uint32_t cached_rxp_diff;
static unsigned long int cached_txb_diff; static uint32_t cached_txb_diff;
static unsigned long int cached_txp_diff; static uint32_t cached_txp_diff;
static void NetworkIOMeter_updateValues(Meter* this, char* buffer, size_t len) { static void NetworkIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
static unsigned long long int cached_last_update = 0; static uint64_t cached_last_update = 0;
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
unsigned long long int timeInMilliSeconds = (unsigned long long int)tv.tv_sec * 1000 + (unsigned long long int)tv.tv_usec / 1000; uint64_t timeInMilliSeconds = (uint64_t)tv.tv_sec * 1000 + (uint64_t)tv.tv_usec / 1000;
unsigned long long int passedTimeInMs = timeInMilliSeconds - cached_last_update; uint64_t passedTimeInMs = timeInMilliSeconds - cached_last_update;
/* update only every 500ms */ /* update only every 500ms */
if (passedTimeInMs > 500) { if (passedTimeInMs > 500) {
static unsigned long long int cached_rxb_total; static uint64_t cached_rxb_total;
static unsigned long long int cached_rxp_total; static uint64_t cached_rxp_total;
static unsigned long long int cached_txb_total; static uint64_t cached_txb_total;
static unsigned long long int cached_txp_total; static uint64_t cached_txp_total;
unsigned long long diff; uint64_t diff;
cached_last_update = timeInMilliSeconds; cached_last_update = timeInMilliSeconds;
@ -53,17 +53,17 @@ static void NetworkIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
diff = data.bytesReceived - cached_rxb_total; diff = data.bytesReceived - cached_rxb_total;
diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */ diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */
diff = (1000 * diff) / passedTimeInMs; /* convert to per second */ diff = (1000 * diff) / passedTimeInMs; /* convert to per second */
cached_rxb_diff = (unsigned long)diff; cached_rxb_diff = (uint32_t)diff;
} else { } else {
cached_rxb_diff = 0UL; cached_rxb_diff = 0;
} }
cached_rxb_total = data.bytesReceived; cached_rxb_total = data.bytesReceived;
if (data.packetsReceived > cached_rxp_total) { if (data.packetsReceived > cached_rxp_total) {
diff = data.packetsReceived - cached_rxp_total; diff = data.packetsReceived - cached_rxp_total;
cached_rxp_diff = (unsigned long)diff; cached_rxp_diff = (uint32_t)diff;
} else { } else {
cached_rxp_diff = 0UL; cached_rxp_diff = 0;
} }
cached_rxp_total = data.packetsReceived; cached_rxp_total = data.packetsReceived;
@ -71,17 +71,17 @@ static void NetworkIOMeter_updateValues(Meter* this, char* buffer, size_t len) {
diff = data.bytesTransmitted - cached_txb_total; diff = data.bytesTransmitted - cached_txb_total;
diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */ diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */
diff = (1000 * diff) / passedTimeInMs; /* convert to per second */ diff = (1000 * diff) / passedTimeInMs; /* convert to per second */
cached_txb_diff = (unsigned long)diff; cached_txb_diff = (uint32_t)diff;
} else { } else {
cached_txb_diff = 0UL; cached_txb_diff = 0;
} }
cached_txb_total = data.bytesTransmitted; cached_txb_total = data.bytesTransmitted;
if (data.packetsTransmitted > cached_txp_total) { if (data.packetsTransmitted > cached_txp_total) {
diff = data.packetsTransmitted - cached_txp_total; diff = data.packetsTransmitted - cached_txp_total;
cached_txp_diff = (unsigned long)diff; cached_txp_diff = (uint32_t)diff;
} else { } else {
cached_txp_diff = 0UL; cached_txp_diff = 0;
} }
cached_txp_total = data.packetsTransmitted; cached_txp_total = data.packetsTransmitted;
} }
@ -116,7 +116,7 @@ static void NetworkIOMeter_display(ATTR_UNUSED const Object* cast, RichString* o
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer); RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], buffer);
RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s"); RichString_appendAscii(out, CRT_colors[METER_VALUE_IOWRITE], "iB/s");
xSnprintf(buffer, sizeof(buffer), " (%lu/%lu packets) ", cached_rxp_diff, cached_txp_diff); xSnprintf(buffer, sizeof(buffer), " (%u/%u packets) ", cached_rxp_diff, cached_txp_diff);
RichString_appendAscii(out, CRT_colors[METER_TEXT], buffer); RichString_appendAscii(out, CRT_colors[METER_TEXT], buffer);
} }

View File

@ -4,10 +4,10 @@
#include "Meter.h" #include "Meter.h"
typedef struct NetworkIOData_ { typedef struct NetworkIOData_ {
unsigned long long int bytesReceived; uint64_t bytesReceived;
unsigned long long int packetsReceived; uint64_t packetsReceived;
unsigned long long int bytesTransmitted; uint64_t bytesTransmitted;
unsigned long long int packetsTransmitted; uint64_t packetsTransmitted;
} NetworkIOData; } NetworkIOData;
extern const MeterClass NetworkIOMeter_class; extern const MeterClass NetworkIOMeter_class;

View File

@ -321,8 +321,7 @@ bool Platform_getNetworkIO(NetworkIOData* data) {
size_t countLen = sizeof(count); size_t countLen = sizeof(count);
const int countMib[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT }; const int countMib[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT };
int r; int r = sysctl(countMib, ARRAYSIZE(countMib), &count, &countLen, NULL, 0);
r = sysctl(countMib, ARRAYSIZE(countMib), &count, &countLen, NULL, 0);
if (r < 0) if (r < 0)
return false; return false;