Unify and simplify netspeed logic and types

This commit is contained in:
Aaron Marcher 2018-05-19 13:07:05 +02:00
parent 2321aecff0
commit e3ccac3298

View file

@ -5,48 +5,46 @@
#include "../util.h" #include "../util.h"
#if defined(__linux__) #if defined(__linux__)
#include <stdint.h>
const char * const char *
netspeed_rx(const char *interface) netspeed_rx(const char *interface)
{ {
static int valid; uint64_t oldrxbytes;
static unsigned long long rxbytes; static uint64_t rxbytes = 0;
unsigned long oldrxbytes;
extern const unsigned int interval; extern const unsigned int interval;
char path[PATH_MAX]; char path[PATH_MAX];
oldrxbytes = rxbytes; oldrxbytes = rxbytes;
snprintf(path, sizeof(path), "/sys/class/net/%s/statistics/rx_bytes", interface);
snprintf(path, sizeof(path),
"/sys/class/net/%s/statistics/rx_bytes", interface);
if (pscanf(path, "%llu", &rxbytes) != 1) { if (pscanf(path, "%llu", &rxbytes) != 1) {
return NULL; return NULL;
} }
if (!valid) {
valid = 1;
return NULL;
}
return fmt_scaled((rxbytes - oldrxbytes) / interval * 1000); return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
interval * 1000) : NULL;
} }
const char * const char *
netspeed_tx(const char *interface) netspeed_tx(const char *interface)
{ {
static int valid; uint64_t oldtxbytes;
static unsigned long long txbytes; static uint64_t txbytes = 0;
unsigned long oldtxbytes;
extern const unsigned int interval; extern const unsigned int interval;
char path[PATH_MAX]; char path[PATH_MAX];
oldtxbytes = txbytes; oldtxbytes = txbytes;
snprintf(path, sizeof(path), "/sys/class/net/%s/statistics/tx_bytes", interface);
snprintf(path, sizeof(path),
"/sys/class/net/%s/statistics/tx_bytes", interface);
if (pscanf(path, "%llu", &txbytes) != 1) { if (pscanf(path, "%llu", &txbytes) != 1) {
return NULL; return NULL;
} }
if (!valid) {
valid = 1;
return NULL;
}
return fmt_scaled((txbytes - oldtxbytes) / interval * 1000); return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
interval * 1000) : NULL;
} }
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
#include <string.h> #include <string.h>
@ -60,9 +58,8 @@
{ {
struct ifaddrs *ifal, *ifa; struct ifaddrs *ifal, *ifa;
struct if_data *ifd; struct if_data *ifd;
static uint64_t oldrxbytes; uint64_t oldrxbytes;
uint64_t rxbytes = 0; static uint64_t rxbytes = 0;
const char *rxs;
extern const unsigned int interval; extern const unsigned int interval;
char if_ok = 0; char if_ok = 0;
@ -82,9 +79,8 @@
return NULL; return NULL;
} }
rxs = oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) / return oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
interval * 1000) : NULL; interval * 1000) : NULL;
return (oldrxbytes = rxbytes, rxs);
} }
const char * const char *
@ -92,9 +88,8 @@
{ {
struct ifaddrs *ifal, *ifa; struct ifaddrs *ifal, *ifa;
struct if_data *ifd; struct if_data *ifd;
static uint64_t oldtxbytes; uint64_t oldtxbytes;
uint64_t txbytes = 0; static uint64_t txbytes = 0;
const char *txs;
extern const unsigned int interval; extern const unsigned int interval;
char if_ok = 0; char if_ok = 0;
@ -114,8 +109,7 @@
return NULL; return NULL;
} }
txs = oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) / return oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
interval * 1000) : NULL; interval * 1000) : NULL;
return (oldtxbytes = txbytes, txs);
} }
#endif #endif