top: add network in & out bytes to the top display.

inspired by the macos top(1).

the first value displayed is the total in/out bytes since boot,
but each update is the amount since the prior update.  the new
fetching code heavily based upon netstat/if.c.

old version:

Swap: 128G Total, 128G Free / Pools: 13G Used

new version:

Swap: 128G Total, 128G Free / Pools: 13G Used, / Network: 26M In, 804K Out

update the list of people who have contributed to m_netbsd.c.
This commit is contained in:
mrg 2023-10-21 06:30:23 +00:00
parent 879813d8b2
commit 369cd1d703
1 changed files with 69 additions and 6 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: m_netbsd.c,v 1.26 2022/07/15 06:39:06 mrg Exp $ */
/* $NetBSD: m_netbsd.c,v 1.27 2023/10/21 06:30:23 mrg Exp $ */
/*
* top - a top users display for Unix
@ -20,6 +20,14 @@
* and should work for:
* NetBSD-2.0 (when released)
* -
* NetBSD-4.0 updates from Christos Zoulas.
* NetBSD-5.0 updates from Andrew Doran, Mindaugas Rasiukevicius and
* Christos Zoulas.
* NetBSD-6.0 updates from matthew green, Christos Zoulas, and
* Mindaugas Rasiukevicius.
* NetBSD-8 updates from Leonardo Taccari.
* NetBSD-10 updates from Christos Zoulas and matthew green.
*
* top does not need to be installed setuid or setgid with this module.
*
* LIBS: -lkvm
@ -37,12 +45,12 @@
* Andrew Doran <ad@NetBSD.org>
*
*
* $Id: m_netbsd.c,v 1.26 2022/07/15 06:39:06 mrg Exp $
* $Id: m_netbsd.c,v 1.27 2023/10/21 06:30:23 mrg Exp $
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: m_netbsd.c,v 1.26 2022/07/15 06:39:06 mrg Exp $");
__RCSID("$NetBSD: m_netbsd.c,v 1.27 2023/10/21 06:30:23 mrg Exp $");
#endif
#include <sys/param.h>
@ -50,6 +58,9 @@ __RCSID("$NetBSD: m_netbsd.c,v 1.26 2022/07/15 06:39:06 mrg Exp $");
#include <sys/sysctl.h>
#include <sys/sched.h>
#include <sys/swap.h>
#include <sys/socket.h>
#include <net/route.h>
#include <uvm/uvm_extern.h>
@ -166,9 +177,10 @@ const char *memorynames[] = {
NULL
};
long swap_stats[6];
long swap_stats[9];
const char *swapnames[] = {
"K Total, ", "K Used, ", "K Free ", " Pools: ", "K Used, ",
" Network: ", "K In, ", "K Out, ",
NULL
};
@ -445,11 +457,60 @@ format_header(char *uname_field)
return(header);
}
static void
get_network_kilobytes(long *kb_in, long *kb_out)
{
struct if_msghdr *ifm;
int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
struct rt_msghdr *rtm;
struct if_data *ifd = NULL;
static char *buf = NULL;
static size_t olen;
char *next, *lim;
size_t len;
static uint64_t last_bytes_in;
static uint64_t last_bytes_out;
uint64_t cur_bytes_in = 0;
uint64_t cur_bytes_out = 0;
if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
err(1, "sysctl");
if (len > olen) {
free(buf);
if ((buf = malloc(len)) == NULL)
err(1, NULL);
olen = len;
}
if (sysctl(mib, 6, buf, &len, NULL, 0) == -1)
err(1, "sysctl");
lim = buf + len;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
if (rtm->rtm_version != RTM_VERSION)
continue;
switch (rtm->rtm_type) {
case RTM_IFINFO:
ifm = (struct if_msghdr *)next;
ifd = &ifm->ifm_data;
cur_bytes_in += ifd->ifi_ibytes;
cur_bytes_out += ifd->ifi_obytes;
break;
}
}
*kb_in = (cur_bytes_in - last_bytes_in) / 1024;
*kb_out = (cur_bytes_out - last_bytes_out) / 1024;
last_bytes_in = cur_bytes_in;
last_bytes_out = cur_bytes_out;
}
void
get_system_info(struct system_info *si)
{
size_t ssize;
int mib[2];
int mib[6];
struct uvmexp_sysctl uvmexp;
struct swapent *sep;
u_int64_t totalsize, totalinuse;
@ -531,8 +592,10 @@ get_system_info(struct system_info *si)
swap_stats[4] = pagetok(uvmexp.poolpages);
get_network_kilobytes(&swap_stats[6], &swap_stats[7]);
memory_stats[6] = -1;
swap_stats[3] = swap_stats[5] = -1;
swap_stats[3] = swap_stats[5] = swap_stats[8] = -1;
/* set arrays and strings */
si->cpustates = cpu_states;