Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/systat PR bin/51204



details:   https://anonhg.NetBSD.org/src/rev/fd13891b8e25
branches:  trunk
changeset: 346780:fd13891b8e25
user:      scole <scole%NetBSD.org@localhost>
date:      Tue Aug 02 15:56:09 2016 +0000

description:
PR bin/51204

Add ifstat command to systat.

Imported from FreeBSD

diffstat:

 usr.bin/systat/Makefile  |   10 +-
 usr.bin/systat/cmds.c    |   16 +-
 usr.bin/systat/cmdtab.c  |   14 +-
 usr.bin/systat/convtbl.c |  157 +++++++++++++
 usr.bin/systat/convtbl.h |   59 +++++
 usr.bin/systat/extern.h  |   14 +-
 usr.bin/systat/ifcmds.c  |   83 +++++++
 usr.bin/systat/ifstat.c  |  544 +++++++++++++++++++++++++++++++++++++++++++++++
 usr.bin/systat/systat.1  |   53 ++++-
 9 files changed, 938 insertions(+), 12 deletions(-)

diffs (truncated from 1118 to 300 lines):

diff -r d8d1b5b4c0a4 -r fd13891b8e25 usr.bin/systat/Makefile
--- a/usr.bin/systat/Makefile   Tue Aug 02 14:54:03 2016 +0000
+++ b/usr.bin/systat/Makefile   Tue Aug 02 15:56:09 2016 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.38 2016/01/23 21:22:50 christos Exp $
+#      $NetBSD: Makefile,v 1.39 2016/08/02 15:56:09 scole Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/6/93
 
 .include <bsd.own.mk>
@@ -11,10 +11,10 @@
 CPPFLAGS+=-I${NETBSDSRCDIR}/usr.bin/vmstat -DSUPPORT_UTMP -DSUPPORT_UTMPX \
        -I${NETBSDSRCDIR}/usr.bin/who -D_KMEMUSER
 CWARNFLAGS+=    -Wno-format-y2k
-SRCS=  bufcache.c cmds.c cmdtab.c disks.c df.c drvstats.c fetch.c \
-       globalcmds.c icmp.c iostat.c ip.c keyboard.c main.c mbufs.c \
-       netcmds.c netstat.c pigs.c ps.c swap.c tcp.c vmstat.c utmpentry.c \
-       syscall.c
+SRCS=  bufcache.c cmds.c cmdtab.c convtbl.c disks.c df.c drvstats.c \
+       fetch.c globalcmds.c icmp.c ifcmds.c ifstat.c iostat.c ip.c \
+       keyboard.c main.c mbufs.c netcmds.c netstat.c pigs.c ps.c swap.c \
+       tcp.c vmstat.c utmpentry.c syscall.c
 DPADD= ${LIBCURSES} ${LIBTERMINFO} ${LIBM} ${LIBKVM}
 LDADD= -lutil -lcurses -lterminfo -lm -lkvm
 BINGRP=        kmem
diff -r d8d1b5b4c0a4 -r fd13891b8e25 usr.bin/systat/cmds.c
--- a/usr.bin/systat/cmds.c     Tue Aug 02 14:54:03 2016 +0000
+++ b/usr.bin/systat/cmds.c     Tue Aug 02 15:56:09 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cmds.c,v 1.28 2004/11/04 07:18:47 dsl Exp $    */
+/*     $NetBSD: cmds.c,v 1.29 2016/08/02 15:56:09 scole Exp $  */
 
 /*-
  * Copyright (c) 1980, 1992, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)cmds.c     8.2 (Berkeley) 4/29/95";
 #endif
-__RCSID("$NetBSD: cmds.c,v 1.28 2004/11/04 07:18:47 dsl Exp $");
+__RCSID("$NetBSD: cmds.c,v 1.29 2016/08/02 15:56:09 scole Exp $");
 #endif /* not lint */
 
 #include <ctype.h>
@@ -152,3 +152,15 @@
 {
        error("Showing %s, refresh every %d seconds.", curmode->c_name, naptime);
 }
+
+int
+prefix(const char *s1, const char *s2)
+{
+
+       while (*s1 == *s2) {
+               if (*s1 == '\0')
+                       return (1);
+               s1++, s2++;
+       }
+       return (*s1 == '\0');
+}
diff -r d8d1b5b4c0a4 -r fd13891b8e25 usr.bin/systat/cmdtab.c
--- a/usr.bin/systat/cmdtab.c   Tue Aug 02 14:54:03 2016 +0000
+++ b/usr.bin/systat/cmdtab.c   Tue Aug 02 15:56:09 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cmdtab.c,v 1.24 2012/01/06 14:08:08 drochner Exp $     */
+/*     $NetBSD: cmdtab.c,v 1.25 2016/08/02 15:56:09 scole Exp $        */
 
 /*-
  * Copyright (c) 1980, 1992, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)cmdtab.c   8.1 (Berkeley) 6/6/93";
 #endif
-__RCSID("$NetBSD: cmdtab.c,v 1.24 2012/01/06 14:08:08 drochner Exp $");
+__RCSID("$NetBSD: cmdtab.c,v 1.25 2016/08/02 15:56:09 scole Exp $");
 #endif /* not lint */
 
 #include "systat.h"
@@ -69,6 +69,13 @@
        { .c_name = NULL }
 };
 
+struct command ifstat_commands[] = {
+       { "scale",      ifstat_scale,   "modify scale of display"},
+       { "pps",        ifstat_pps,     "toggle packets per second display"},
+       { "match",      ifstat_match,   "display matching interfaces"},
+       { .c_name = NULL }
+};
+
 struct command iostat_commands[] = {
        { "bars",       iostat_bars,    "show io stats as a bar graph"},
        { "numbers",    iostat_numbers, "show io stats numerically"},
@@ -159,6 +166,9 @@
        { "df",         showdf,         fetchdf,        labeldf,
          initdf,       opendf,         closedf,        df_commands,
          CF_LOADAV },
+       { "ifstat",     showifstat,     fetchifstat,    labelifstat,
+         initifstat,   openifstat,     closeifstat,    ifstat_commands,
+         CF_LOADAV },
        { "inet.icmp",  showicmp,       fetchicmp,      labelicmp,
          initicmp,     openicmp,       closeicmp,      icmp_commands,
          CF_LOADAV },
diff -r d8d1b5b4c0a4 -r fd13891b8e25 usr.bin/systat/convtbl.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/systat/convtbl.c  Tue Aug 02 15:56:09 2016 +0000
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2003, Trent Nelson, <trent%arpa.com@localhost>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: releng/10.1/usr.bin/systat/convtbl.c 175387 2008-01-16 19:27:43Z delphij $
+ */
+
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: convtbl.c,v 1.1 2016/08/02 15:56:09 scole Exp $");
+#endif /* not lint */
+
+#include <sys/types.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "convtbl.h"
+
+#define BIT            (8)
+#define BITS           (1)
+#define KILOBIT                (1000LL)
+#define MEGABIT                (KILOBIT * 1000)
+#define GIGABIT                (MEGABIT * 1000)
+#define TERABIT                (GIGABIT * 1000)
+
+#define BYTE           (1)
+#define BYTES          (1)
+#define KILOBYTE       (1024LL)
+#define MEGABYTE       (KILOBYTE * 1024)
+#define GIGABYTE       (MEGABYTE * 1024)
+#define TERABYTE       (GIGABYTE * 1024)
+
+struct convtbl {
+       uintmax_t        mul;
+       uintmax_t        scale;
+       const char      *str;
+       const char      *name;
+};
+
+static struct convtbl convtbl[] = {
+       /* mul, scale, str, name */
+       [SC_BYTE] =     { BYTE, BYTES, "B", "byte" },
+       [SC_KILOBYTE] = { BYTE, KILOBYTE, "KB", "kbyte" },
+       [SC_MEGABYTE] = { BYTE, MEGABYTE, "MB", "mbyte" },
+       [SC_GIGABYTE] = { BYTE, GIGABYTE, "GB", "gbyte" },
+       [SC_TERABYTE] = { BYTE, TERABYTE, "TB", "tbyte" },
+
+       [SC_BIT] =      { BIT, BITS, "b", "bit" },
+       [SC_KILOBIT] =  { BIT, KILOBIT, "Kb", "kbit" },
+       [SC_MEGABIT] =  { BIT, MEGABIT, "Mb", "mbit" },
+       [SC_GIGABIT] =  { BIT, GIGABIT, "Gb", "gbit" },
+       [SC_TERABIT] =  { BIT, TERABIT, "Tb", "tbit" },
+
+       [SC_AUTO] =     { 0, 0, "", "auto" }
+};
+
+static
+struct convtbl *
+get_tbl_ptr(const uintmax_t size, const int scale)
+{
+       uintmax_t        tmp;
+       int              idx;
+
+       /* If our index is out of range, default to auto-scaling. */
+       idx = scale < SC_AUTO ? scale : SC_AUTO;
+
+       if (idx == SC_AUTO)
+               /*
+                * Simple but elegant algorithm.  Count how many times
+                * we can shift our size value right by a factor of ten,
+                * incrementing an index each time.  We then use the
+                * index as the array index into the conversion table.
+                */
+               for (tmp = size, idx = SC_KILOBYTE;
+                    tmp >= MEGABYTE && idx < SC_BIT - 1;
+                    tmp >>= 10, idx++);
+
+       return (&convtbl[idx]);
+}
+
+double
+convert(const uintmax_t size, const int scale)
+{
+       struct convtbl  *tp;
+
+       tp = get_tbl_ptr(size, scale);
+       return ((double)size * tp->mul / tp->scale);
+
+}
+
+const char *
+get_string(const uintmax_t size, const int scale)
+{
+       struct convtbl  *tp;
+
+       tp = get_tbl_ptr(size, scale);
+       return (tp->str);
+}
+
+int
+get_scale(const char *name)
+{
+       int i;
+
+       for (i = 0; i <= SC_AUTO; i++)
+               if (strcmp(convtbl[i].name, name) == 0)
+                       return (i);
+       return (-1);
+}
+
+const char *
+get_helplist(void)
+{
+       int i;
+       size_t len;
+       static char *buf;
+
+       if (buf == NULL) {
+               len = 0;
+               for (i = 0; i <= SC_AUTO; i++)
+                       len += strlen(convtbl[i].name) + 2;
+               if ((buf = malloc(len)) != NULL) {
+                       buf[0] = '\0';
+                       for (i = 0; i <= SC_AUTO; i++) {
+                               strcat(buf, convtbl[i].name);
+                               if (i < SC_AUTO)
+                                       strcat(buf, ", ");
+                       }
+               } else
+                       return ("");
+       }
+       return (buf);
+}
diff -r d8d1b5b4c0a4 -r fd13891b8e25 usr.bin/systat/convtbl.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/systat/convtbl.h  Tue Aug 02 15:56:09 2016 +0000
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2003, Trent Nelson, <trent%arpa.com@localhost>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: releng/10.1/usr.bin/systat/convtbl.h 164675 2006-11-27 16:33:44Z yar $
+ */
+
+#ifndef _CONVTBL_H_
+#define _CONVTBL_H_
+
+#include <sys/types.h>
+#include <stdint.h>



Home | Main Index | Thread Index | Old Index