Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/vmstat Drop kvm-based access for driver statistics.



details:   https://anonhg.NetBSD.org/src/rev/c1d127c80c0d
branches:  trunk
changeset: 329663:c1d127c80c0d
user:      joerg <joerg%NetBSD.org@localhost>
date:      Mon Jun 02 19:29:00 2014 +0000

description:
Drop kvm-based access for driver statistics.

diffstat:

 usr.bin/vmstat/drvstats.c |  290 ++++++++++++---------------------------------
 1 files changed, 77 insertions(+), 213 deletions(-)

diffs (truncated from 384 to 300 lines):

diff -r ae2ae6ea2d12 -r c1d127c80c0d usr.bin/vmstat/drvstats.c
--- a/usr.bin/vmstat/drvstats.c Mon Jun 02 19:17:08 2014 +0000
+++ b/usr.bin/vmstat/drvstats.c Mon Jun 02 19:29:00 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: drvstats.c,v 1.6 2012/11/13 14:09:58 chs Exp $ */
+/*     $NetBSD: drvstats.c,v 1.7 2014/06/02 19:29:00 joerg Exp $       */
 
 /*
  * Copyright (c) 1996 John M. Vinopal
@@ -49,29 +49,9 @@
 #include <unistd.h>
 #include "drvstats.h"
 
-static struct nlist namelist[] = {
-#define        X_TK_NIN        0
-       { .n_name = "_tk_nin" },                /* tty characters in */
-#define        X_TK_NOUT       1
-       { .n_name = "_tk_nout" },               /* tty characters out */
-#define        X_HZ            2
-       { .n_name = "_hz" },            /* ticks per second */
-#define        X_STATHZ        3
-       { .n_name = "_stathz" },
-#define        X_DRIVE_COUNT   4
-       { .n_name = "_iostat_count" },  /* number of drives */
-#define        X_DRIVELIST     5
-       { .n_name = "_iostatlist" },    /* TAILQ of drives */
-       { .n_name = NULL },
-};
-
 /* Structures to hold the statistics. */
 struct _drive  cur, last;
 
-/* Kernel pointers: nlistf and memf defined in calling program. */
-static kvm_t   *kd = NULL;
-extern char    *nlistf;
-extern char    *memf;
 extern int     hz;
 
 /* Pointer to list of drives. */
@@ -84,27 +64,12 @@
 int            *drv_select;
 char           **dr_name;
 
-#define        KVM_ERROR(_string) do {                                         \
-       warnx("%s", (_string));                                         \
-       errx(1, "%s", kvm_geterr(kd));                                  \
-} while (/* CONSTCOND */0)
-
-/*
- * Dereference the namelist pointer `v' and fill in the local copy
- * 'p' which is of size 's'.
- */
-#define        deref_nl(v, p, s) do {                                          \
-       deref_kptr((void *)namelist[(v)].n_value, (p), (s));            \
-} while (/* CONSTCOND */0)
-
 /* Missing from <sys/time.h> */
 #define        timerset(tvp, uvp) do {                                         \
        ((uvp)->tv_sec = (tvp)->tv_sec);                                \
        ((uvp)->tv_usec = (tvp)->tv_usec);                              \
 } while (/* CONSTCOND */0)
 
-static void deref_kptr(void *, void *, size_t);
-
 /*
  * Take the delta between the present values and the last recorded
  * values, storing the present values in the 'last' structure, and
@@ -183,70 +148,47 @@
 void
 drvreadstats(void)
 {
-       struct io_stats cur_drive, *p;
+       struct io_stats *p;
        size_t          size, i;
        int             mib[3];
 
        p = iostathead;
 
-       if (memf == NULL) {
-               mib[0] = CTL_HW;
-               mib[1] = HW_IOSTATS;
-               mib[2] = sizeof(struct io_sysctl);
+       mib[0] = CTL_HW;
+       mib[1] = HW_IOSTATS;
+       mib[2] = sizeof(struct io_sysctl);
 
-               size = ndrive * sizeof(struct io_sysctl);
-               if (sysctl(mib, 3, drives, &size, NULL, 0) < 0)
-                       err(1, "sysctl hw.iostats failed");
-               for (i = 0; i < ndrive; i++) {
-                       cur.rxfer[i] = drives[i].rxfer;
-                       cur.wxfer[i] = drives[i].wxfer;
-                       cur.seek[i] = drives[i].seek;
-                       cur.rbytes[i] = drives[i].rbytes;
-                       cur.wbytes[i] = drives[i].wbytes;
-                       cur.time[i].tv_sec = drives[i].time_sec;
-                       cur.time[i].tv_usec = drives[i].time_usec;
-               }
+       size = ndrive * sizeof(struct io_sysctl);
+       if (sysctl(mib, 3, drives, &size, NULL, 0) < 0)
+               err(1, "sysctl hw.iostats failed");
+       for (i = 0; i < ndrive; i++) {
+               cur.rxfer[i] = drives[i].rxfer;
+               cur.wxfer[i] = drives[i].wxfer;
+               cur.seek[i] = drives[i].seek;
+               cur.rbytes[i] = drives[i].rbytes;
+               cur.wbytes[i] = drives[i].wbytes;
+               cur.time[i].tv_sec = drives[i].time_sec;
+               cur.time[i].tv_usec = drives[i].time_usec;
+       }
 
                mib[0] = CTL_KERN;
-               mib[1] = KERN_TKSTAT;
-               mib[2] = KERN_TKSTAT_NIN;
-               size = sizeof(cur.tk_nin);
-               if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0)
-                       cur.tk_nin = 0;
+       mib[1] = KERN_TKSTAT;
+       mib[2] = KERN_TKSTAT_NIN;
+       size = sizeof(cur.tk_nin);
+       if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0)
+               cur.tk_nin = 0;
 
-               mib[2] = KERN_TKSTAT_NOUT;
-               size = sizeof(cur.tk_nout);
-               if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0)
-                       cur.tk_nout = 0;
-       } else {
-               for (i = 0; i < ndrive; i++) {
-                       deref_kptr(p, &cur_drive, sizeof(cur_drive));
-                       cur.rxfer[i] = cur_drive.io_rxfer;
-                       cur.wxfer[i] = cur_drive.io_wxfer;
-                       cur.seek[i] = cur_drive.io_seek;
-                       cur.rbytes[i] = cur_drive.io_rbytes;
-                       cur.wbytes[i] = cur_drive.io_wbytes;
-                       timerset(&(cur_drive.io_time), &(cur.time[i]));
-                       p = cur_drive.io_link.tqe_next;
-               }
+       mib[2] = KERN_TKSTAT_NOUT;
+       size = sizeof(cur.tk_nout);
+       if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0)
+               cur.tk_nout = 0;
 
-               deref_nl(X_TK_NIN, &cur.tk_nin, sizeof(cur.tk_nin));
-               deref_nl(X_TK_NOUT, &cur.tk_nout, sizeof(cur.tk_nout));
-       }
-
-       /*
-        * XXX Need to locate the `correct' CPU when looking for this
-        * XXX in crash dumps.  Just don't report it for now, in that
-        * XXX case.
-        */
        size = sizeof(cur.cp_time);
        (void)memset(cur.cp_time, 0, size);
-       if (memf == NULL) {
-               mib[0] = CTL_KERN;
-               mib[1] = KERN_CP_TIME;
-               if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0)
-                       (void)memset(cur.cp_time, 0, sizeof(cur.cp_time));
-       }
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_CP_TIME;
+       if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0)
+               (void)memset(cur.cp_time, 0, sizeof(cur.cp_time));
 }
 
 /*
@@ -259,22 +201,17 @@
        size_t          size;
        int             mib[3];
 
-       if (memf == NULL) {
-               mib[0] = CTL_KERN;
-               mib[1] = KERN_TKSTAT;
-               mib[2] = KERN_TKSTAT_NIN;
-               size = sizeof(cur.tk_nin);
-               if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0)
-                       cur.tk_nin = 0;
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_TKSTAT;
+       mib[2] = KERN_TKSTAT_NIN;
+       size = sizeof(cur.tk_nin);
+       if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0)
+               cur.tk_nin = 0;
 
-               mib[2] = KERN_TKSTAT_NOUT;
-               size = sizeof(cur.tk_nout);
-               if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0)
-                       cur.tk_nout = 0;
-       } else {
-               deref_nl(X_TK_NIN, &cur.tk_nin, sizeof(cur.tk_nin));
-               deref_nl(X_TK_NOUT, &cur.tk_nout, sizeof(cur.tk_nout));
-       }
+       mib[2] = KERN_TKSTAT_NOUT;
+       size = sizeof(cur.tk_nout);
+       if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0)
+               cur.tk_nout = 0;
 }
 
 /*
@@ -287,19 +224,12 @@
        size_t          size;
        int             mib[2];
 
-       /*
-        * XXX Need to locate the `correct' CPU when looking for this
-        * XXX in crash dumps.  Just don't report it for now, in that
-        * XXX case.
-        */
        size = sizeof(cur.cp_time);
        (void)memset(cur.cp_time, 0, size);
-       if (memf == NULL) {
-               mib[0] = CTL_KERN;
-               mib[1] = KERN_CP_TIME;
-               if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0)
-                       (void)memset(cur.cp_time, 0, sizeof(cur.cp_time));
-       }
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_CP_TIME;
+       if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0)
+               (void)memset(cur.cp_time, 0, sizeof(cur.cp_time));
 }
 
 /*
@@ -309,10 +239,7 @@
 int
 drvinit(int selected)
 {
-       struct iostatlist_head iostat_head;
-       struct io_stats cur_drive, *p;
        struct clockinfo clockinfo;
-       char            errbuf[_POSIX2_LINE_MAX];
        size_t          size, i;
        static int      once = 0;
        int             mib[3];
@@ -320,66 +247,34 @@
        if (once)
                return (1);
 
-       if (memf == NULL) {
-               mib[0] = CTL_HW;
-               mib[1] = HW_NCPU;
-               size = sizeof(cur.cp_ncpu);
-               if (sysctl(mib, 2, &cur.cp_ncpu, &size, NULL, 0) == -1)
-                       err(1, "sysctl hw.ncpu failed");
-
-               mib[0] = CTL_KERN;
-               mib[1] = KERN_CLOCKRATE;
-               size = sizeof(clockinfo);
-               if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) == -1)
-                       err(1, "sysctl kern.clockrate failed");
-               hz = clockinfo.stathz;
-               if (!hz)
-                       hz = clockinfo.hz;
-
-               mib[0] = CTL_HW;
-               mib[1] = HW_IOSTATS;
-               mib[2] = sizeof(struct io_sysctl);
-               if (sysctl(mib, 3, NULL, &size, NULL, 0) == -1)
-                       err(1, "sysctl hw.drivestats failed");
-               ndrive = size / sizeof(struct io_sysctl);
+       mib[0] = CTL_HW;
+       mib[1] = HW_NCPU;
+       size = sizeof(cur.cp_ncpu);
+       if (sysctl(mib, 2, &cur.cp_ncpu, &size, NULL, 0) == -1)
+               err(1, "sysctl hw.ncpu failed");
 
-               if (size == 0) {
-                       warnx("No drives attached.");
-               } else {
-                       drives = (struct io_sysctl *)malloc(size);
-                       if (drives == NULL)
-                               errx(1, "Memory allocation failure.");
-               }
-       } else {
-               int drive_count;
-               /* Open the kernel. */
-               if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
-                   errbuf)) == NULL)
-                       errx(1, "kvm_openfiles: %s", errbuf);
-
-               /* Obtain the namelist symbols from the kernel. */
-               if (kvm_nlist(kd, namelist))
-                       KVM_ERROR("kvm_nlist failed to read symbols.");
+       mib[0] = CTL_KERN;
+       mib[1] = KERN_CLOCKRATE;
+       size = sizeof(clockinfo);
+       if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) == -1)
+               err(1, "sysctl kern.clockrate failed");
+       hz = clockinfo.stathz;
+       if (!hz)
+               hz = clockinfo.hz;
 
-               /* Get the number of attached drives. */
-               deref_nl(X_DRIVE_COUNT, &drive_count, sizeof(drive_count));
+       mib[0] = CTL_HW;
+       mib[1] = HW_IOSTATS;
+       mib[2] = sizeof(struct io_sysctl);
+       if (sysctl(mib, 3, NULL, &size, NULL, 0) == -1)
+               err(1, "sysctl hw.drivestats failed");



Home | Main Index | Thread Index | Old Index