- Get interface stats via getifaddrs(3) instead of kvm(3).

- Get vmstat via sysctl(3) instead of kvm(3).

Approved by:	bapt (blanket)
This commit is contained in:
Gleb Smirnoff 2014-10-02 20:39:15 +00:00
parent a89f42920f
commit f181dacce2
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=369861
2 changed files with 223 additions and 1 deletions

View File

@ -3,7 +3,7 @@
PORTNAME= xosview
PORTVERSION= 1.16
PORTREVISION= 0
PORTREVISION= 1
CATEGORIES= sysutils
MASTER_SITES= http://www.pogo.org.uk/~mark/xosview/releases/

View File

@ -0,0 +1,222 @@
--- bsd/kernel.cc.orig 2013-08-23 13:40:35.000000000 +0400
+++ bsd/kernel.cc 2014-10-03 00:35:05.000000000 +0400
@@ -28,6 +28,7 @@
#include <string.h>
#include <err.h>
#include <errno.h>
+#include <ifaddrs.h>
#include <sysexits.h>
#include <sys/types.h>
#include <sys/queue.h>
@@ -43,7 +44,6 @@
#if defined(XOSVIEW_FREEBSD) || defined(XOSVIEW_DFBSD)
static const char ACPIDEV[] = "/dev/acpi";
static const char APMDEV[] = "/dev/apm";
-#include <net/if_var.h>
#include <sys/ioctl.h>
#include <dev/acpica/acpiio.h>
#include <machine/apm_bios.h>
@@ -115,56 +115,41 @@
// this later on. This keeps the indices within the nlist constant.
#define DUMMY_SYM "dummy_sym"
-#if defined(XOSVIEW_FREEBSD)
-{ "_cnt" },
-#define VMMETER_SYM_INDEX 0
-#else
-{ DUMMY_SYM },
-#define DUMMY_0
-#endif
-#if !defined(XOSVIEW_OPENBSD)
-{ "_ifnet" },
-#define IFNET_SYM_INDEX 1
-#else
-{ DUMMY_SYM },
-#define DUMMY_1
-#endif
-
#if defined(XOSVIEW_OPENBSD)
{ "_disklist" },
-#define DISKLIST_SYM_INDEX 2
+#define DISKLIST_SYM_INDEX 0
#else
{ DUMMY_SYM },
-#define DUMMY_2
+#define DUMMY_0
#endif
#if defined(XOSVIEW_NETBSD)
{ "_allevents" },
-#define ALLEVENTS_SYM_INDEX 3
+#define ALLEVENTS_SYM_INDEX 1
{ "_bufmem" },
-#define BUFMEM_SYM_INDEX 4
+#define BUFMEM_SYM_INDEX 2
#else
{ DUMMY_SYM },
-#define DUMMY_3
+#define DUMMY_1
{ DUMMY_SYM },
-#define DUMMY_4
+#define DUMMY_2
#endif
#if defined(XOSVIEW_FREEBSD)
{ "_intrnames" },
-#define INTRNAMES_SYM_INDEX 5
+#define INTRNAMES_SYM_INDEX 3
# if __FreeBSD_version >= 900040
{ "_sintrnames" },
# else
{ "_eintrnames" },
# endif
-#define EINTRNAMES_SYM_INDEX 6
+#define EINTRNAMES_SYM_INDEX 4
{ "_intrcnt" },
-#define INTRCNT_SYM_INDEX 7
+#define INTRCNT_SYM_INDEX 5
# if __FreeBSD_version >= 900040
{ "_sintrcnt" },
# else
{ "_eintrcnt" },
# endif
-#define EINTRCNT_SYM_INDEX 8
+#define EINTRCNT_SYM_INDEX 6
#endif
{ NULL }
};
@@ -337,7 +322,21 @@
#else /* HAVE_UVM */
struct vmmeter vm;
#if defined(XOSVIEW_FREEBSD)
- safe_kvm_read_symbol(VMMETER_SYM_INDEX, &vm, sizeof(vm));
+ size_t size = sizeof(unsigned int);
+
+#define GET_VM_STATS(name) \
+ sysctlbyname("vm.stats.vm." #name, &vm.name, &size, NULL, 0)
+ GET_VM_STATS(v_active_count);
+ GET_VM_STATS(v_inactive_count);
+ GET_VM_STATS(v_wire_count);
+ GET_VM_STATS(v_cache_count);
+ GET_VM_STATS(v_free_count);
+ GET_VM_STATS(v_page_size);
+ GET_VM_STATS(v_vnodepgsin);
+ GET_VM_STATS(v_vnodepgsout);
+ GET_VM_STATS(v_swappgsin);
+ GET_VM_STATS(v_swappgsout);
+#undef GET_VM_STATS
#else /* XOSVIEW_DFBSD */
struct vmstats vms;
size_t size = sizeof(vms);
@@ -421,99 +420,37 @@
int
BSDNetInit() {
OpenKDIfNeeded();
-#if defined(XOSVIEW_NETBSD)
- return ValidSymbol(IFNET_SYM_INDEX);
-#else
return 1;
-#endif
}
void
BSDGetNetInOut(unsigned long long *inbytes, unsigned long long *outbytes, const char *netIface, bool ignored) {
- char ifname[IFNAMSIZ];
+ struct ifaddrs *ifap, *ifa;
*inbytes = 0;
*outbytes = 0;
-#if defined(XOSVIEW_OPENBSD)
- size_t size;
- char *buf, *next;
- struct if_msghdr *ifm;
- struct if_data ifd;
- struct sockaddr_dl *sdl;
-
- if ( sysctl(mib_ifl, 6, NULL, &size, NULL, 0) < 0 )
- err(EX_OSERR, "BSDGetNetInOut(): sysctl 1 failed");
- if ( (buf = (char *)malloc(size)) == NULL)
- err(EX_OSERR, "BSDGetNetInOut(): malloc failed");
- if ( sysctl(mib_ifl, 6, buf, &size, NULL, 0) < 0 )
- err(EX_OSERR, "BSDGetNetInOut(): sysctl 2 failed");
- for (next = buf; next < buf + size; next += ifm->ifm_msglen) {
- bool skipif = false;
- ifm = (struct if_msghdr *)next;
- if (ifm->ifm_type != RTM_IFINFO || ifm->ifm_addrs & RTAX_IFP == 0)
- continue;
- ifd = ifm->ifm_data;
- sdl = (struct sockaddr_dl *)(ifm + 1);
- if (sdl->sdl_family != AF_LINK)
- continue;
- if ( strncmp(netIface, "False", 5) != 0 ) {
- memcpy(ifname, sdl->sdl_data, (sdl->sdl_nlen >= IFNAMSIZ ? IFNAMSIZ - 1 : sdl->sdl_nlen));
- if ( (!ignored && strncmp(sdl->sdl_data, netIface, sdl->sdl_nlen) != 0) ||
- ( ignored && strncmp(sdl->sdl_data, netIface, sdl->sdl_nlen) == 0) )
- skipif = true;
- }
- if (!skipif) {
- *inbytes += ifd.ifi_ibytes;
- *outbytes += ifd.ifi_obytes;
- }
- }
- free(buf);
-#else /* XOSVIEW_OPENBSD */
- struct ifnet *ifnetp;
- struct ifnet ifnet;
-#if defined (XOSVIEW_NETBSD)
- struct ifnet_head ifnethd;
-#else
- struct ifnethead ifnethd;
-#endif
- safe_kvm_read(nlst[IFNET_SYM_INDEX].n_value, &ifnethd, sizeof(ifnethd));
- ifnetp = TAILQ_FIRST(&ifnethd);
+ if (getifaddrs(&ifap) != 0)
+ return;
- while (ifnetp) {
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
bool skipif = false;
- // Now, dereference the pointer to get the ifnet struct.
- safe_kvm_read((u_long)ifnetp, &ifnet, sizeof(ifnet));
- strlcpy(ifname, ifnet.if_xname, sizeof(ifname));
-#if defined(XOSVIEW_NETBSD)
- ifnetp = TAILQ_NEXT(&ifnet, if_list);
-#else
- ifnetp = TAILQ_NEXT(&ifnet, if_link);
-#endif
- if (!(ifnet.if_flags & IFF_UP))
+
+ if (ifa->ifa_addr->sa_family != AF_LINK)
continue;
+
if ( strncmp(netIface, "False", 5) != 0 ) {
- if ( (!ignored && strncmp(ifname, netIface, 256) != 0) ||
- ( ignored && strncmp(ifname, netIface, 256) == 0) )
+ if ( (!ignored && strncmp(ifa->ifa_name, netIface, 256) != 0) ||
+ ( ignored && strncmp(ifa->ifa_name, netIface, 256) == 0) )
skipif = true;
}
+#define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
if (!skipif) {
-#if defined(XOSVIEW_DFBSD) && __DragonFly_version > 300304
- struct ifdata_pcpu *ifdatap = ifnet.if_data_pcpu;
- struct ifdata_pcpu ifdata;
- int ncpus = BSDCountCpus();
- for (int cpu = 0; cpu < ncpus; cpu++) {
- safe_kvm_read((u_long)ifdatap + cpu * sizeof(ifdata),
- &ifdata, sizeof(ifdata));
- *inbytes += ifdata.ifd_ibytes;
- *outbytes += ifdata.ifd_obytes;
- }
-#else
- *inbytes += ifnet.if_ibytes;
- *outbytes += ifnet.if_obytes;
-#endif
+ *inbytes += IFA_STAT(ibytes);
+ *outbytes += IFA_STAT(obytes);
}
+#undef IFA_STAT
}
-#endif /* XOSVIEW_OPENBSD */
+ freeifaddrs(ifap);
}