[dev] [slstatus] [patch] netspeed_*: allow reporting transfer speeds of more than one network interface (on linux)
diff --git a/components/netspeeds.c b/components/netspeeds.c
index 02c030e..1814d86 100644
--- a/components/netspeeds.c
+++ b/components/netspeeds.c
_AT_@ -1,35 +1,70 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
#include <limits.h>
#include "../util.h"
+#define NINTERFACES 2
+
#if defined(__linux__)
#include <stdint.h>
+ struct netspeed_xif {
+ char *iface;
+ uintmax_t xbytes;
+ };
+ int find_interface_idx(const char * interface, int * nextidx, struct
netspeed_xif * interfaces[]) {
+ int i;
+ int found = 0;
+ for (i=0; i<*nextidx; i++) {
+ if (!strcmp(interfaces[i]->iface, interface)) {
+ found = 1;
+ return i;
+ }
+ }
+ if (!found) {
+ interfaces[*nextidx] = malloc(sizeof(interfaces[*nextidx]));
+ interfaces[*nextidx]->iface = malloc(sizeof(interface));
+ sprintf(interfaces[*nextidx]->iface, "%s", interface);
+ interfaces[*nextidx]->xbytes = 0;
+ i = *nextidx;
+ *nextidx = *nextidx+1;
+ return i;
+ }
+ return -1;
+ }
+
const char *
netspeed_rx(const char *interface)
{
uintmax_t oldrxbytes;
- static uintmax_t rxbytes;
+ static struct netspeed_xif * rxbytes[NINTERFACES];
+ static int rx_nextidx = 0;
+ int idx = 0;
extern const unsigned int interval;
char path[PATH_MAX];
- oldrxbytes = rxbytes;
+ idx = find_interface_idx(interface, &rx_nextidx, rxbytes);
+ if (idx < 0) {
+ return NULL;
+ }
+
+ oldrxbytes = rxbytes[idx]->xbytes;
if (esnprintf(path, sizeof(path),
"/sys/class/net/%s/statistics/rx_bytes",
interface) < 0) {
return NULL;
}
- if (pscanf(path, "%ju", &rxbytes) != 1) {
+ if (pscanf(path, "%ju", &rxbytes[idx]->xbytes) != 1) {
return NULL;
}
if (oldrxbytes == 0) {
return NULL;
}
-
- return fmt_human((rxbytes - oldrxbytes) * 1000 / interval,
+ return fmt_human((rxbytes[idx]->xbytes - oldrxbytes) * 1000 / interval,
1024);
}
_AT_@ -37,25 +72,32 @@
netspeed_tx(const char *interface)
{
uintmax_t oldtxbytes;
- static uintmax_t txbytes;
+ static struct netspeed_xif * txbytes[NINTERFACES];
+ static int tx_nextidx = 0;
+ int idx;
extern const unsigned int interval;
char path[PATH_MAX];
- oldtxbytes = txbytes;
+ idx = find_interface_idx(interface, &tx_nextidx, txbytes);
+ if (idx < 0) {
+ return NULL;
+ }
+
+ oldtxbytes = txbytes[idx]->xbytes;
if (esnprintf(path, sizeof(path),
"/sys/class/net/%s/statistics/tx_bytes",
interface) < 0) {
return NULL;
}
- if (pscanf(path, "%ju", &txbytes) != 1) {
+ if (pscanf(path, "%ju", &txbytes[idx]->xbytes) != 1) {
return NULL;
}
if (oldtxbytes == 0) {
return NULL;
}
- return fmt_human((txbytes - oldtxbytes) * 1000 / interval,
+ return fmt_human((txbytes[idx]->xbytes - oldtxbytes) * 1000 / interval,
1024);
}
#elif defined(__OpenBSD__)
Received on Tue Oct 16 2018 - 12:16:36 CEST
This archive was generated by hypermail 2.3.0
: Tue Oct 16 2018 - 12:24:07 CEST