Merge pull request #549 from natoscott/network-types

Fix integer sizing issues in the NetworkIO Meter
This commit is contained in:
Nathan Scott 2021-03-02 13:34:36 +11:00 committed by GitHub
commit 3fe297aa97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 85 additions and 134 deletions

View File

@ -19,65 +19,71 @@ static const int NetworkIOMeter_attributes[] = {
static bool hasData = false; static bool hasData = false;
static unsigned long int cached_rxb_diff = 0; static uint32_t cached_rxb_diff;
static unsigned long int cached_rxp_diff = 0; static uint32_t cached_rxp_diff;
static unsigned long int cached_txb_diff = 0; static uint32_t cached_txb_diff;
static unsigned long int cached_txp_diff = 0; 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 int cached_rxb_total = 0; static uint64_t cached_rxb_total;
static unsigned long int cached_rxp_total = 0; static uint64_t cached_rxp_total;
static unsigned long int cached_txb_total = 0; static uint64_t cached_txb_total;
static unsigned long int cached_txp_total = 0; static uint64_t cached_txp_total;
uint64_t diff;
cached_last_update = timeInMilliSeconds; cached_last_update = timeInMilliSeconds;
unsigned long int bytesReceived, packetsReceived, bytesTransmitted, packetsTransmitted; NetworkIOData data;
hasData = Platform_getNetworkIO(&data);
hasData = Platform_getNetworkIO(&bytesReceived, &packetsReceived, &bytesTransmitted, &packetsTransmitted);
if (!hasData) { if (!hasData) {
xSnprintf(buffer, len, "no data"); xSnprintf(buffer, len, "no data");
return; return;
} }
if (bytesReceived > cached_rxb_total) { if (data.bytesReceived > cached_rxb_total) {
cached_rxb_diff = (bytesReceived - cached_rxb_total) / 1024; /* Meter_humanUnit() expects unit in kilo */ diff = data.bytesReceived - cached_rxb_total;
cached_rxb_diff = 1000.0 * cached_rxb_diff / passedTimeInMs; /* convert to per second */ diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */
diff = (1000 * diff) / passedTimeInMs; /* convert to per second */
cached_rxb_diff = (uint32_t)diff;
} else { } else {
cached_rxb_diff = 0; cached_rxb_diff = 0;
} }
cached_rxb_total = bytesReceived; cached_rxb_total = data.bytesReceived;
if (packetsReceived > cached_rxp_total) { if (data.packetsReceived > cached_rxp_total) {
cached_rxp_diff = packetsReceived - cached_rxp_total; diff = data.packetsReceived - cached_rxp_total;
cached_rxp_diff = (uint32_t)diff;
} else { } else {
cached_rxp_diff = 0; cached_rxp_diff = 0;
} }
cached_rxp_total = packetsReceived; cached_rxp_total = data.packetsReceived;
if (bytesTransmitted > cached_txb_total) { if (data.bytesTransmitted > cached_txb_total) {
cached_txb_diff = (bytesTransmitted - cached_txb_total) / 1024; /* Meter_humanUnit() expects unit in kilo */ diff = data.bytesTransmitted - cached_txb_total;
cached_txb_diff = 1000.0 * cached_txb_diff / passedTimeInMs; /* convert to per second */ diff /= ONE_K; /* Meter_humanUnit() expects unit in kilo */
diff = (1000 * diff) / passedTimeInMs; /* convert to per second */
cached_txb_diff = (uint32_t)diff;
} else { } else {
cached_txb_diff = 0; cached_txb_diff = 0;
} }
cached_txb_total = bytesTransmitted; cached_txb_total = data.bytesTransmitted;
if (packetsTransmitted > cached_txp_total) { if (data.packetsTransmitted > cached_txp_total) {
cached_txp_diff = packetsTransmitted - cached_txp_total; diff = data.packetsTransmitted - cached_txp_total;
cached_txp_diff = (uint32_t)diff;
} else { } else {
cached_txp_diff = 0; cached_txp_diff = 0;
} }
cached_txp_total = packetsTransmitted; cached_txp_total = data.packetsTransmitted;
} }
this->values[0] = cached_rxb_diff; this->values[0] = cached_rxb_diff;
@ -110,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

@ -3,6 +3,13 @@
#include "Meter.h" #include "Meter.h"
typedef struct NetworkIOData_ {
uint64_t bytesReceived;
uint64_t packetsReceived;
uint64_t bytesTransmitted;
uint64_t packetsTransmitted;
} NetworkIOData;
extern const MeterClass NetworkIOMeter_class; extern const MeterClass NetworkIOMeter_class;
#endif /* HEADER_NetworkIOMeter */ #endif /* HEADER_NetworkIOMeter */

View File

@ -334,15 +334,9 @@ bool Platform_getDiskIO(DiskIOData* data) {
return false; return false;
} }
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data) {
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted) {
// TODO // TODO
*bytesReceived = 0; (void)data;
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false; return false;
} }

View File

@ -16,6 +16,7 @@ in the source distribution for its full text.
#include "CPUMeter.h" #include "CPUMeter.h"
#include "DarwinProcess.h" #include "DarwinProcess.h"
#include "DiskIOMeter.h" #include "DiskIOMeter.h"
#include "NetworkIOMeter.h"
#include "ProcessLocksScreen.h" #include "ProcessLocksScreen.h"
#include "SignalsPanel.h" #include "SignalsPanel.h"
@ -62,10 +63,7 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data); bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data);
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted);
void Platform_getBattery(double *percent, ACPresence *isOnAC); void Platform_getBattery(double *percent, ACPresence *isOnAC);

View File

@ -233,15 +233,9 @@ bool Platform_getDiskIO(DiskIOData* data) {
return false; return false;
} }
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data) {
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted) {
// TODO // TODO
*bytesReceived = 0; (void)data;
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false; return false;
} }

View File

@ -14,6 +14,7 @@ in the source distribution for its full text.
#include "Action.h" #include "Action.h"
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include "DiskIOMeter.h" #include "DiskIOMeter.h"
#include "NetworkIOMeter.h"
#include "ProcessLocksScreen.h" #include "ProcessLocksScreen.h"
#include "SignalsPanel.h" #include "SignalsPanel.h"
@ -52,10 +53,7 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data); bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data);
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted);
void Platform_getBattery(double* percent, ACPresence* isOnAC); void Platform_getBattery(double* percent, ACPresence* isOnAC);

View File

@ -315,24 +315,17 @@ bool Platform_getDiskIO(DiskIOData* data) {
return true; return true;
} }
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data) {
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted) {
int r;
// get number of interfaces // get number of interfaces
int count; int count;
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 };
r = sysctl(countMib, ARRAYSIZE(countMib), &count, &countLen, NULL, 0); int r = sysctl(countMib, ARRAYSIZE(countMib), &count, &countLen, NULL, 0);
if (r < 0) if (r < 0)
return false; return false;
memset(data, 0, sizeof(NetworkIOData));
unsigned long int bytesReceivedSum = 0, packetsReceivedSum = 0, bytesTransmittedSum = 0, packetsTransmittedSum = 0;
for (int i = 1; i <= count; i++) { for (int i = 1; i <= count; i++) {
struct ifmibdata ifmd; struct ifmibdata ifmd;
size_t ifmdLen = sizeof(ifmd); size_t ifmdLen = sizeof(ifmd);
@ -346,16 +339,12 @@ bool Platform_getNetworkIO(unsigned long int* bytesReceived,
if (ifmd.ifmd_flags & IFF_LOOPBACK) if (ifmd.ifmd_flags & IFF_LOOPBACK)
continue; continue;
bytesReceivedSum += ifmd.ifmd_data.ifi_ibytes; data->bytesReceived += ifmd.ifmd_data.ifi_ibytes;
packetsReceivedSum += ifmd.ifmd_data.ifi_ipackets; data->packetsReceived += ifmd.ifmd_data.ifi_ipackets;
bytesTransmittedSum += ifmd.ifmd_data.ifi_obytes; data->bytesTransmitted += ifmd.ifmd_data.ifi_obytes;
packetsTransmittedSum += ifmd.ifmd_data.ifi_opackets; data->packetsTransmitted += ifmd.ifmd_data.ifi_opackets;
} }
*bytesReceived = bytesReceivedSum;
*packetsReceived = packetsReceivedSum;
*bytesTransmitted = bytesTransmittedSum;
*packetsTransmitted = packetsTransmittedSum;
return true; return true;
} }

View File

@ -14,6 +14,7 @@ in the source distribution for its full text.
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include "DiskIOMeter.h" #include "DiskIOMeter.h"
#include "Meter.h" #include "Meter.h"
#include "NetworkIOMeter.h"
#include "Process.h" #include "Process.h"
#include "ProcessLocksScreen.h" #include "ProcessLocksScreen.h"
#include "SignalsPanel.h" #include "SignalsPanel.h"
@ -57,10 +58,7 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data); bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data);
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted);
void Platform_getBattery(double* percent, ACPresence* isOnAC); void Platform_getBattery(double* percent, ACPresence* isOnAC);

View File

@ -534,42 +534,35 @@ bool Platform_getDiskIO(DiskIOData* data) {
return true; return true;
} }
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data) {
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted) {
FILE* fd = fopen(PROCDIR "/net/dev", "r"); FILE* fd = fopen(PROCDIR "/net/dev", "r");
if (!fd) if (!fd)
return false; return false;
unsigned long int bytesReceivedSum = 0, packetsReceivedSum = 0, bytesTransmittedSum = 0, packetsTransmittedSum = 0; memset(data, 0, sizeof(NetworkIOData));
char lineBuffer[512]; char lineBuffer[512];
while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { while (fgets(lineBuffer, sizeof(lineBuffer), fd)) {
char interfaceName[32]; char interfaceName[32];
unsigned long int bytesReceivedParsed, packetsReceivedParsed, bytesTransmittedParsed, packetsTransmittedParsed; unsigned long long int bytesReceived, packetsReceived, bytesTransmitted, packetsTransmitted;
if (sscanf(lineBuffer, "%31s %lu %lu %*u %*u %*u %*u %*u %*u %lu %lu", if (sscanf(lineBuffer, "%31s %llu %llu %*u %*u %*u %*u %*u %*u %llu %llu",
interfaceName, interfaceName,
&bytesReceivedParsed, &bytesReceived,
&packetsReceivedParsed, &packetsReceived,
&bytesTransmittedParsed, &bytesTransmitted,
&packetsTransmittedParsed) != 5) &packetsTransmitted) != 5)
continue; continue;
if (String_eq(interfaceName, "lo:")) if (String_eq(interfaceName, "lo:"))
continue; continue;
bytesReceivedSum += bytesReceivedParsed; data->bytesReceived += bytesReceived;
packetsReceivedSum += packetsReceivedParsed; data->packetsReceived += packetsReceived;
bytesTransmittedSum += bytesTransmittedParsed; data->bytesTransmitted += bytesTransmitted;
packetsTransmittedSum += packetsTransmittedParsed; data->packetsTransmitted += packetsTransmitted;
} }
fclose(fd); fclose(fd);
*bytesReceived = bytesReceivedSum;
*packetsReceived = packetsReceivedSum;
*bytesTransmitted = bytesTransmittedSum;
*packetsTransmitted = packetsTransmittedSum;
return true; return true;
} }

View File

@ -15,6 +15,7 @@ in the source distribution for its full text.
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include "DiskIOMeter.h" #include "DiskIOMeter.h"
#include "Meter.h" #include "Meter.h"
#include "NetworkIOMeter.h"
#include "Process.h" #include "Process.h"
#include "ProcessLocksScreen.h" #include "ProcessLocksScreen.h"
#include "SignalsPanel.h" #include "SignalsPanel.h"
@ -67,10 +68,7 @@ void Platform_getPressureStall(const char *file, bool some, double* ten, double*
bool Platform_getDiskIO(DiskIOData* data); bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data);
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted);
void Platform_getBattery(double *percent, ACPresence *isOnAC); void Platform_getBattery(double *percent, ACPresence *isOnAC);

View File

@ -283,15 +283,9 @@ bool Platform_getDiskIO(DiskIOData* data) {
return false; return false;
} }
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data) {
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted) {
// TODO // TODO
*bytesReceived = 0; (void)data;
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false; return false;
} }

View File

@ -15,6 +15,7 @@ in the source distribution for its full text.
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include "DiskIOMeter.h" #include "DiskIOMeter.h"
#include "Meter.h" #include "Meter.h"
#include "NetworkIOMeter.h"
#include "Process.h" #include "Process.h"
#include "ProcessLocksScreen.h" #include "ProcessLocksScreen.h"
#include "SignalsPanel.h" #include "SignalsPanel.h"
@ -55,10 +56,7 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data); bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data);
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted);
void Platform_getBattery(double* percent, ACPresence* isOnAC); void Platform_getBattery(double* percent, ACPresence* isOnAC);

View File

@ -296,15 +296,9 @@ bool Platform_getDiskIO(DiskIOData* data) {
return false; return false;
} }
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data) {
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted) {
// TODO // TODO
*bytesReceived = 0; (void)data;
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false; return false;
} }

View File

@ -19,6 +19,7 @@ in the source distribution for its full text.
#include "Action.h" #include "Action.h"
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include "DiskIOMeter.h" #include "DiskIOMeter.h"
#include "NetworkIOMeter.h"
#include "ProcessLocksScreen.h" #include "ProcessLocksScreen.h"
#include "SignalsPanel.h" #include "SignalsPanel.h"
@ -74,10 +75,7 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data); bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data);
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted);
void Platform_getBattery(double* percent, ACPresence* isOnAC); void Platform_getBattery(double* percent, ACPresence* isOnAC);

View File

@ -137,14 +137,8 @@ bool Platform_getDiskIO(DiskIOData* data) {
return false; return false;
} }
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data) {
unsigned long int* packetsReceived, (void)data;
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted) {
*bytesReceived = 0;
*packetsReceived = 0;
*bytesTransmitted = 0;
*packetsTransmitted = 0;
return false; return false;
} }

View File

@ -11,6 +11,7 @@ in the source distribution for its full text.
#include "Action.h" #include "Action.h"
#include "BatteryMeter.h" #include "BatteryMeter.h"
#include "DiskIOMeter.h" #include "DiskIOMeter.h"
#include "NetworkIOMeter.h"
#include "ProcessLocksScreen.h" #include "ProcessLocksScreen.h"
#include "SignalsPanel.h" #include "SignalsPanel.h"
#include "UnsupportedProcess.h" #include "UnsupportedProcess.h"
@ -52,10 +53,7 @@ FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid);
bool Platform_getDiskIO(DiskIOData* data); bool Platform_getDiskIO(DiskIOData* data);
bool Platform_getNetworkIO(unsigned long int* bytesReceived, bool Platform_getNetworkIO(NetworkIOData* data);
unsigned long int* packetsReceived,
unsigned long int* bytesTransmitted,
unsigned long int* packetsTransmitted);
void Platform_getBattery(double *percent, ACPresence *isOnAC); void Platform_getBattery(double *percent, ACPresence *isOnAC);