[BACK]Return to dkstats.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / vmstat

Annotation of src/usr.bin/vmstat/dkstats.c, Revision 1.19

1.19    ! deraadt     1: /*     $OpenBSD: dkstats.c,v 1.18 2002/06/19 08:45:52 deraadt Exp $    */
1.2       deraadt     2: /*     $NetBSD: dkstats.c,v 1.1 1996/05/10 23:19:27 thorpej Exp $      */
1.1       tholo       3:
                      4: /*
1.2       deraadt     5:  * Copyright (c) 1996 John M. Vinopal
1.1       tholo       6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *      This product includes software developed for the NetBSD Project
                     19:  *      by John M. Vinopal.
                     20:  * 4. The name of the author may not be used to endorse or promote products
                     21:  *    derived from this software without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     24:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     25:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     26:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     27:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
                     28:  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                     29:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
                     30:  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
                     31:  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
1.10      angelos    36: #include <sys/param.h>
1.1       tholo      37: #include <sys/dkstat.h>
                     38: #include <sys/time.h>
                     39: #include <sys/disk.h>
1.10      angelos    40: #include <sys/sysctl.h>
                     41: #include <sys/tty.h>
1.1       tholo      42:
                     43: #include <err.h>
                     44: #include <fcntl.h>
                     45: #include <kvm.h>
                     46: #include <limits.h>
                     47: #include <nlist.h>
                     48: #include <stdio.h>
                     49: #include <stdlib.h>
                     50: #include <string.h>
                     51: #include <unistd.h>
                     52: #include "dkstats.h"
                     53:
1.16      deraadt    54: #if !defined(NOKVM)
1.1       tholo      55: static struct nlist namelist[] = {
1.18      deraadt    56: #define        X_TK_NIN        0               /* sysctl */
                     57:        { "_tk_nin" },
                     58: #define        X_TK_NOUT       1               /* sysctl */
                     59:        { "_tk_nout" },
                     60: #define        X_CP_TIME       2               /* sysctl */
                     61:        { "_cp_time" },
                     62: #define        X_HZ            3               /* sysctl */
                     63:        { "_hz" },
                     64: #define        X_STATHZ        4               /* sysctl */
1.1       tholo      65:        { "_stathz" },
1.18      deraadt    66: #define X_DISK_COUNT   5               /* sysctl */
                     67:        { "_disk_count" },
                     68: #define X_DISKLIST     6               /* sysctl */
                     69:        { "_disklist" },
1.1       tholo      70:        { NULL },
                     71: };
1.16      deraadt    72: #define        KVM_ERROR(_string) {                                            \
                     73:        warnx("%s", (_string));                                         \
                     74:        errx(1, "%s", kvm_geterr(kd));                                  \
                     75: }
                     76:
                     77: /*
                     78:  * Dereference the namelist pointer `v' and fill in the local copy
                     79:  * 'p' which is of size 's'.
                     80:  */
                     81: #define deref_nl(v, p, s) deref_kptr((void *)namelist[(v)].n_value, (p), (s));
                     82: static void deref_kptr(void *, void *, size_t);
                     83: #endif /* !defined(NOKVM) */
1.1       tholo      84:
                     85: /* Structures to hold the statistics. */
                     86: struct _disk   cur, last;
                     87:
                     88: /* Kernel pointers: nlistf and memf defined in calling program. */
1.19    ! deraadt    89: #if !defined(NOKVM)
1.1       tholo      90: static kvm_t   *kd = NULL;
1.19    ! deraadt    91: #endif
1.1       tholo      92: extern char    *nlistf;
                     93: extern char    *memf;
                     94:
1.19    ! deraadt    95: #if !defined(NOKVM)
1.1       tholo      96: /* Pointer to list of disks. */
                     97: static struct disk     *dk_drivehead = NULL;
1.19    ! deraadt    98: #endif
1.1       tholo      99:
                    100: /* Backward compatibility references. */
                    101: int            dk_ndrive = 0;
                    102: int            *dk_select;
                    103: char           **dr_name;
                    104:
                    105: /* Missing from <sys/time.h> */
1.16      deraadt   106: #define timerset(tvp, uvp) \
                    107:        ((uvp)->tv_sec = (tvp)->tv_sec);                \
                    108:        ((uvp)->tv_usec = (tvp)->tv_usec)
                    109:
                    110: #define SWAP(fld)      tmp = cur.fld;                          \
                    111:                        cur.fld -= last.fld;                    \
                    112:                        last.fld = tmp
1.1       tholo     113:
                    114: /*
                    115:  * Take the delta between the present values and the last recorded
                    116:  * values, storing the present values in the 'last' structure, and
                    117:  * the delta values in the 'cur' structure.
                    118:  */
                    119: void
                    120: dkswap()
                    121: {
                    122:        u_int64_t tmp;
                    123:        int     i;
                    124:
                    125:        for (i = 0; i < dk_ndrive; i++) {
                    126:                struct timeval  tmp_timer;
                    127:
                    128:                if (!cur.dk_select[i])
                    129:                        continue;
                    130:
                    131:                /* Delta Values. */
                    132:                SWAP(dk_xfer[i]);
                    133:                SWAP(dk_seek[i]);
                    134:                SWAP(dk_bytes[i]);
                    135:
                    136:                /* Delta Time. */
                    137:                timerclear(&tmp_timer);
                    138:                timerset(&(cur.dk_time[i]), &tmp_timer);
                    139:                timersub(&tmp_timer, &(last.dk_time[i]), &(cur.dk_time[i]));
                    140:                timerclear(&(last.dk_time[i]));
                    141:                timerset(&tmp_timer, &(last.dk_time[i]));
                    142:        }
                    143:        for (i = 0; i < CPUSTATES; i++) {
                    144:                SWAP(cp_time[i]);
                    145:        }
                    146:        SWAP(tk_nin);
                    147:        SWAP(tk_nout);
                    148:
                    149: #undef SWAP
                    150: }
                    151:
                    152: /*
                    153:  * Read the disk statistics for each disk in the disk list.
                    154:  * Also collect statistics for tty i/o and cpu ticks.
                    155:  */
                    156: void
                    157: dkreadstats()
                    158: {
1.19    ! deraadt   159: #if !defined(NOKVM)
1.1       tholo     160:        struct disk     cur_disk, *p;
1.19    ! deraadt   161: #endif
1.10      angelos   162:        int             i, mib[3];
                    163:        size_t          size;
1.13      angelos   164:        struct diskstats *q;
1.1       tholo     165:
1.10      angelos   166:        if (nlistf == NULL && memf == NULL) {
1.13      angelos   167:                size = dk_ndrive * sizeof(struct diskstats);
1.10      angelos   168:                mib[0] = CTL_HW;
                    169:                mib[1] = HW_DISKSTATS;
1.13      angelos   170:                q = malloc(size);
                    171:                if (q == NULL)
1.10      angelos   172:                        err(1, NULL);
1.13      angelos   173:                if (sysctl(mib, 2, q, &size, NULL, 0) < 0) {
1.10      angelos   174:                        warn("could not read hw.diskstats");
1.14      deraadt   175:                        bzero(q, dk_ndrive * sizeof(struct diskstats));
1.10      angelos   176:                }
                    177:
                    178:                for (i = 0; i < dk_ndrive; i++) {
1.13      angelos   179:                        cur.dk_xfer[i] = q[i].ds_xfer;
                    180:                        cur.dk_seek[i] = q[i].ds_seek;
                    181:                        cur.dk_bytes[i] = q[i].ds_bytes;
                    182:                        timerset(&(q[i].ds_time), &(cur.dk_time[i]));
1.10      angelos   183:                }
1.14      deraadt   184:                free(q);
1.10      angelos   185:
                    186:                size = sizeof(cur.cp_time);
                    187:                mib[0] = CTL_KERN;
                    188:                mib[1] = KERN_CPTIME;
                    189:                if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0) {
1.11      angelos   190:                        warn("could not read kern.cp_time");
1.10      angelos   191:                        bzero(cur.cp_time, sizeof(cur.cp_time));
                    192:                }
                    193:                size = sizeof(cur.tk_nin);
                    194:                mib[0] = CTL_KERN;
                    195:                mib[1] = KERN_TTY;
                    196:                mib[2] = KERN_TTY_TKNIN;
                    197:                if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0) {
1.11      angelos   198:                        warn("could not read kern.tty.tk_nin");
1.10      angelos   199:                        cur.tk_nin = 0;
                    200:                }
                    201:                size = sizeof(cur.tk_nin);
                    202:                mib[0] = CTL_KERN;
                    203:                mib[1] = KERN_TTY;
                    204:                mib[2] = KERN_TTY_TKNOUT;
                    205:                if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0) {
1.11      angelos   206:                        warn("could not read kern.tty.tk_nout");
1.10      angelos   207:                        cur.tk_nout = 0;
                    208:                }
                    209:        } else {
1.16      deraadt   210: #if !defined(NOKVM)
1.10      angelos   211:                p = dk_drivehead;
                    212:
                    213:                for (i = 0; i < dk_ndrive; i++) {
                    214:                        deref_kptr(p, &cur_disk, sizeof(cur_disk));
                    215:                        cur.dk_xfer[i] = cur_disk.dk_xfer;
                    216:                        cur.dk_seek[i] = cur_disk.dk_seek;
                    217:                        cur.dk_bytes[i] = cur_disk.dk_bytes;
                    218:                        timerset(&(cur_disk.dk_time), &(cur.dk_time[i]));
                    219:                        p = cur_disk.dk_link.tqe_next;
                    220:                }
                    221:                deref_nl(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
                    222:                deref_nl(X_TK_NIN, &cur.tk_nin, sizeof(cur.tk_nin));
                    223:                deref_nl(X_TK_NOUT, &cur.tk_nout, sizeof(cur.tk_nout));
1.16      deraadt   224: #endif /* !defined(NOKVM) */
1.1       tholo     225:        }
                    226: }
                    227:
                    228: /*
                    229:  * Perform all of the initialization and memory allocation needed to
                    230:  * track disk statistics.
                    231:  */
                    232: int
                    233: dkinit(select)
                    234: int    select;
                    235: {
1.19    ! deraadt   236: #if !defined(NOKVM)
1.1       tholo     237:        struct disklist_head disk_head;
                    238:        struct disk     cur_disk, *p;
                    239:         char           errbuf[_POSIX2_LINE_MAX];
1.19    ! deraadt   240: #endif
1.1       tholo     241:        static int      once = 0;
                    242:        extern int      hz;
1.10      angelos   243:        int             i, mib[2];
                    244:        size_t          size;
                    245:        struct clockinfo clkinfo;
                    246:        char            *disknames, *name, *bufpp;
1.1       tholo     247:
                    248:        if (once)
                    249:                return(1);
                    250:
1.10      angelos   251:        if (nlistf != NULL || memf != NULL) {
1.16      deraadt   252: #if !defined(NOKVM)
1.10      angelos   253:                /* Open the kernel. */
                    254:                if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
                    255:                    errbuf)) == NULL)
                    256:                        errx(1, "kvm_openfiles: %s", errbuf);
                    257:
                    258:                /* Obtain the namelist symbols from the kernel. */
                    259:                if (kvm_nlist(kd, namelist))
                    260:                        KVM_ERROR("kvm_nlist failed to read symbols.");
                    261:
                    262:                /* Get the number of attached drives. */
                    263:                deref_nl(X_DISK_COUNT, &dk_ndrive, sizeof(dk_ndrive));
                    264:
                    265:                if (dk_ndrive < 0)
                    266:                        errx(1, "invalid _disk_count %d.", dk_ndrive);
                    267:
1.1       tholo     268:                /* Get a pointer to the first disk. */
                    269:                deref_nl(X_DISKLIST, &disk_head, sizeof(disk_head));
                    270:                dk_drivehead = disk_head.tqh_first;
1.10      angelos   271:
                    272:                /* Get ticks per second. */
                    273:                deref_nl(X_STATHZ, &hz, sizeof(hz));
                    274:                if (!hz)
                    275:                  deref_nl(X_HZ, &hz, sizeof(hz));
1.16      deraadt   276: #endif /* !defined(NOKVM) */
1.10      angelos   277:        } else {
                    278:                /* Get the number of attached drives. */
                    279:                mib[0] = CTL_HW;
                    280:                mib[1] = HW_DISKCOUNT;
                    281:                size = sizeof(dk_ndrive);
                    282:                if (sysctl(mib, 2, &dk_ndrive, &size, NULL, 0) < 0 ) {
1.11      angelos   283:                        warn("could not read hw.diskcount");
1.10      angelos   284:                        dk_ndrive = 0;
                    285:                }
                    286:
                    287:                /* Get ticks per second. */
                    288:                mib[0] = CTL_KERN;
                    289:                mib[1] = KERN_CLOCKRATE;
                    290:                size = sizeof(clkinfo);
                    291:                if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) {
1.11      angelos   292:                        warn("could not read kern.clockrate");
1.10      angelos   293:                        hz = 0;
                    294:                } else
                    295:                        hz = clkinfo.stathz;
1.1       tholo     296:        }
                    297:
                    298:        /* allocate space for the statistics */
                    299:        cur.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
                    300:        cur.dk_xfer = calloc(dk_ndrive, sizeof(u_int64_t));
                    301:        cur.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
                    302:        cur.dk_bytes = calloc(dk_ndrive, sizeof(u_int64_t));
                    303:        last.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
                    304:        last.dk_xfer = calloc(dk_ndrive, sizeof(u_int64_t));
                    305:        last.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
                    306:        last.dk_bytes = calloc(dk_ndrive, sizeof(u_int64_t));
                    307:        cur.dk_select = calloc(dk_ndrive, sizeof(int));
                    308:        cur.dk_name = calloc(dk_ndrive, sizeof(char *));
                    309:
1.9       deraadt   310:        if (!cur.dk_time || !cur.dk_xfer || !cur.dk_seek || !cur.dk_bytes ||
                    311:            !last.dk_time || !last.dk_xfer || !last.dk_seek ||
                    312:            !last.dk_bytes || !cur.dk_select || !cur.dk_name)
1.1       tholo     313:                errx(1, "Memory allocation failure.");
                    314:
                    315:        /* Set up the compatibility interfaces. */
                    316:        dk_select = cur.dk_select;
                    317:        dr_name = cur.dk_name;
                    318:
                    319:        /* Read the disk names and set intial selection. */
1.10      angelos   320:        if (nlistf == NULL && memf == NULL) {
                    321:                mib[0] = CTL_HW;
                    322:                mib[1] = HW_DISKNAMES;
                    323:                size = 0;
                    324:                if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0)
                    325:                        err(1, "can't get hw.disknames");
1.12      angelos   326:                disknames = malloc(size);
1.10      angelos   327:                if (disknames == NULL)
                    328:                        err(1, NULL);
                    329:                if (sysctl(mib, 2, disknames, &size, NULL, 0) < 0)
                    330:                        err(1, "can't get hw.disknames");
                    331:                bufpp = disknames;
                    332:                i = 0;
                    333:                while ((name = strsep(&bufpp, ",")) != NULL) {
                    334:                    cur.dk_name[i] = name;
                    335:                    cur.dk_select[i++] = select;
                    336:                }
                    337:        } else {
1.16      deraadt   338: #if !defined(NOKVM)
1.10      angelos   339:                p = dk_drivehead;
                    340:                for (i = 0; i < dk_ndrive; i++) {
                    341:                        char    buf[10];
1.16      deraadt   342:
1.10      angelos   343:                        deref_kptr(p, &cur_disk, sizeof(cur_disk));
                    344:                        deref_kptr(cur_disk.dk_name, buf, sizeof(buf));
                    345:                        cur.dk_name[i] = strdup(buf);
                    346:                        if (!cur.dk_name[i])
                    347:                                errx(1, "Memory allocation failure.");
                    348:                        cur.dk_select[i] = select;
1.1       tholo     349:
1.10      angelos   350:                        p = cur_disk.dk_link.tqe_next;
                    351:                }
1.16      deraadt   352: #endif /* !defined(NOKVM) */
1.1       tholo     353:        }
                    354:
                    355:        /* Never do this initalization again. */
                    356:        once = 1;
                    357:        return(1);
                    358: }
                    359:
1.16      deraadt   360: #if !defined(NOKVM)
1.1       tholo     361: /*
                    362:  * Dereference the kernel pointer `kptr' and fill in the local copy
                    363:  * pointed to by `ptr'.  The storage space must be pre-allocated,
                    364:  * and the size of the copy passed in `len'.
                    365:  */
                    366: static void
                    367: deref_kptr(kptr, ptr, len)
                    368:        void *kptr, *ptr;
                    369:        size_t len;
                    370: {
                    371:        char buf[128];
                    372:
1.6       art       373:        if (kvm_read(kd, (u_long)kptr, ptr, len) != len) {
1.1       tholo     374:                bzero(buf, sizeof(buf));
                    375:                snprintf(buf, (sizeof(buf) - 1),
1.2       deraadt   376:                     "can't dereference kptr 0x%lx", (u_long)kptr);
1.1       tholo     377:                KVM_ERROR(buf);
                    378:        }
                    379: }
1.17      drahn     380: #endif /* !defined(NOKVM) */