Annotation of 43BSDReno/usr.bin/nfsstat/nfsstat.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1983, 1989 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software contributed to Berkeley by
                      6:  * Rick Macklem at The University of Guelph.
                      7:  *
                      8:  * Redistribution and use in source and binary forms are permitted
                      9:  * provided that: (1) source distributions retain this entire copyright
                     10:  * notice and comment, and (2) distributions including binaries display
                     11:  * the following acknowledgement:  ``This product includes software
                     12:  * developed by the University of California, Berkeley and its contributors''
                     13:  * in the documentation or other materials provided with the distribution
                     14:  * and in all advertising materials mentioning features or use of this
                     15:  * software. Neither the name of the University nor the names of its
                     16:  * contributors may be used to endorse or promote products derived
                     17:  * from this software without specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     19:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     20:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: char copyright[] =
                     25: "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
                     26:  All rights reserved.\n";
                     27: #endif /* not lint */
                     28: 
                     29: #ifndef lint
                     30: static char sccsid[] = "@(#)nfsstat.c  5.5 (Berkeley) 6/1/90";
                     31: #endif /* not lint */
                     32: 
                     33: #include <sys/param.h>
                     34: #include <sys/vmmac.h>
                     35: #include <sys/file.h>
                     36: #include <machine/pte.h>
                     37: #include <sys/namei.h>
                     38: #include <sys/mount.h>
                     39: #include <nfs/nfsv2.h>
                     40: #include <nfs/nfs.h>
                     41: #include <ctype.h>
                     42: #include <errno.h>
                     43: #include <nlist.h>
                     44: #include <stdio.h>
                     45: #include <paths.h>
                     46: 
                     47: #define        YES     1
                     48: #define        NO      0
                     49: 
                     50: struct nlist nl[] = {
                     51: #define        N_NFSSTAT       0
                     52:        { "_nfsstats" },
                     53: #define        N_SYSMAP        1
                     54:        { "_Sysmap" },
                     55: #define        N_SYSSIZE       2
                     56:        { "_Syssize" },
                     57:        "",
                     58: };
                     59: 
                     60: struct pte *Sysmap;
                     61: 
                     62: char   *system = _PATH_UNIX;
                     63: char   *kmemf = _PATH_KMEM;
                     64: int    kmem;
                     65: int    kflag;
                     66: int    interval;
                     67: 
                     68: extern char *malloc();
                     69: extern off_t lseek();
                     70: 
                     71: main(argc, argv)
                     72:        int argc;
                     73:        char *argv[];
                     74: {
                     75:        int ch;
                     76: 
                     77:        interval = 0;
                     78:        argc--;
                     79:        argv++;
                     80:        if (argc > 0) {
                     81:                interval = atoi(argv[0]);
                     82:                if (interval <= 0)
                     83:                        usage();
                     84:                argv++, argc--;
                     85:                if (argc > 0) {
                     86:                        system = *argv;
                     87:                        argv++, argc--;
                     88:                        if (argc > 0) {
                     89:                                kmemf = *argv;
                     90:                                kflag++;
                     91:                        }
                     92:                }
                     93:        }
                     94:        if (nlist(system, nl) < 0 || nl[0].n_type == 0) {
                     95:                fprintf(stderr, "%s: no namelist\n", system);
                     96:                exit(1);
                     97:        }
                     98:        kmem = open(kmemf, O_RDONLY);
                     99:        if (kmem < 0) {
                    100:                perror(kmemf);
                    101:                exit(1);
                    102:        }
                    103:        if (kflag) {
                    104:                off_t off;
                    105: 
                    106:                Sysmap = (struct pte *)
                    107:                   malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
                    108:                if (!Sysmap) {
                    109:                        fputs("nfsstat: can't get memory for Sysmap.\n", stderr);
                    110:                        exit(1);
                    111:                }
                    112:                off = nl[N_SYSMAP].n_value & ~KERNBASE;
                    113:                (void)lseek(kmem, off, L_SET);
                    114:                (void)read(kmem, (char *)Sysmap,
                    115:                        (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
                    116:        }
                    117:        intpr(interval, nl[N_NFSSTAT].n_value);
                    118:        exit(0);
                    119: }
                    120: 
                    121: /*
                    122:  * Seek into the kernel for a value.
                    123:  */
                    124: off_t
                    125: klseek(fd, base, off)
                    126:        int fd, off;
                    127:        off_t base;
                    128: {
                    129:        if (kflag) {
                    130:                /* get kernel pte */
                    131:                base &= ~KERNBASE;
                    132:                base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
                    133:        }
                    134:        return (lseek(fd, base, off));
                    135: }
                    136: 
                    137: usage()
                    138: {
                    139:        fputs("Usage: nfsstat [interval [ system [ corefile ] ] ]\n", stderr);
                    140:        exit(1);
                    141: }
                    142: 
                    143: /*
                    144:  * Print a description of the network interfaces.
                    145:  */
                    146: intpr(interval, nfsstataddr)
                    147:        int interval;
                    148:        off_t nfsstataddr;
                    149: {
                    150:        struct nfsstats nfsstats;
                    151: 
                    152:        if (nfsstataddr == 0) {
                    153:                printf("nfsstat: symbol not defined\n");
                    154:                return;
                    155:        }
                    156:        if (interval) {
                    157:                sidewaysintpr((unsigned)interval, nfsstataddr);
                    158:                return;
                    159:        }
                    160:        klseek(kmem, nfsstataddr, 0);
                    161:        read(kmem, (char *)&nfsstats, sizeof(struct nfsstats));
                    162:        printf("Client Info:\n");
                    163:        printf("Rpc Counts:\n");
                    164:        printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
                    165:                "Getattr", "Setattr", "Lookup", "Readlink", "Read",
                    166:                "Write", "Create", "Remove");
                    167:        printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
                    168:                nfsstats.rpccnt[1],
                    169:                nfsstats.rpccnt[2],
                    170:                nfsstats.rpccnt[4],
                    171:                nfsstats.rpccnt[5],
                    172:                nfsstats.rpccnt[6],
                    173:                nfsstats.rpccnt[8],
                    174:                nfsstats.rpccnt[9],
                    175:                nfsstats.rpccnt[10]);
                    176:        printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
                    177:                "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
                    178:                "Readdir", "Statfs");
                    179:        printf("%9d %9d %9d %9d %9d %9d %9d\n",
                    180:                nfsstats.rpccnt[11],
                    181:                nfsstats.rpccnt[12],
                    182:                nfsstats.rpccnt[13],
                    183:                nfsstats.rpccnt[14],
                    184:                nfsstats.rpccnt[15],
                    185:                nfsstats.rpccnt[16],
                    186:                nfsstats.rpccnt[17]);
                    187:        printf("Rpc Info:\n");
                    188:        printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
                    189:                "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
                    190:        printf("%9d %9d %9d %9d %9d\n",
                    191:                nfsstats.rpctimeouts,
                    192:                nfsstats.rpcinvalid,
                    193:                nfsstats.rpcunexpected,
                    194:                nfsstats.rpcretries,
                    195:                nfsstats.rpcrequests);
                    196:        printf("Cache Info:\n");
                    197:        printf("%9.9s %9.9s %9.9s %9.9s",
                    198:                "Attr Hits", "Misses", "Lkup Hits", "Misses");
                    199:        printf(" %9.9s %9.9s %9.9s %9.9s\n",
                    200:                "BioR Hits", "Misses", "BioW Hits", "Misses");
                    201:        printf("%9d %9d %9d %9d",
                    202:                nfsstats.attrcache_hits, nfsstats.attrcache_misses,
                    203:                nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
                    204:        printf(" %9d %9d %9d %9d\n",
                    205:                nfsstats.biocache_reads-nfsstats.read_bios,
                    206:                nfsstats.read_bios,
                    207:                nfsstats.biocache_writes-nfsstats.write_bios,
                    208:                nfsstats.write_bios);
                    209:        printf("%9.9s %9.9s %9.9s %9.9s",
                    210:                "BioRLHits", "Misses", "BioD Hits", "Misses");
                    211:        printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
                    212:        printf("%9d %9d %9d %9d",
                    213:                nfsstats.biocache_readlinks-nfsstats.readlink_bios,
                    214:                nfsstats.readlink_bios,
                    215:                nfsstats.biocache_readdirs-nfsstats.readdir_bios,
                    216:                nfsstats.readdir_bios);
                    217:        printf(" %9d %9d\n",
                    218:                nfsstats.direofcache_hits, nfsstats.direofcache_misses);
                    219:        printf("\nServer Info:\n");
                    220:        printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
                    221:                "Getattr", "Setattr", "Lookup", "Readlink", "Read",
                    222:                "Write", "Create", "Remove");
                    223:        printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
                    224:                nfsstats.srvrpccnt[1],
                    225:                nfsstats.srvrpccnt[2],
                    226:                nfsstats.srvrpccnt[4],
                    227:                nfsstats.srvrpccnt[5],
                    228:                nfsstats.srvrpccnt[6],
                    229:                nfsstats.srvrpccnt[8],
                    230:                nfsstats.srvrpccnt[9],
                    231:                nfsstats.srvrpccnt[10]);
                    232:        printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
                    233:                "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
                    234:                "Readdir", "Statfs");
                    235:        printf("%9d %9d %9d %9d %9d %9d %9d\n",
                    236:                nfsstats.srvrpccnt[11],
                    237:                nfsstats.srvrpccnt[12],
                    238:                nfsstats.srvrpccnt[13],
                    239:                nfsstats.srvrpccnt[14],
                    240:                nfsstats.srvrpccnt[15],
                    241:                nfsstats.srvrpccnt[16],
                    242:                nfsstats.srvrpccnt[17]);
                    243:        printf("Server Ret-Failed\n");
                    244:        printf("%17d\n", nfsstats.srvrpc_errs);
                    245:        printf("Server Faults\n");
                    246:        printf("%13d\n", nfsstats.srv_errs);
                    247:        printf("Server Cache Stats:\n");
                    248:        printf("%9.9s %9.9s %9.9s %9.9s\n",
                    249:                "Inprog", "Idem", "Non-idem", "Misses");
                    250:        printf("%9d %9d %9d %9d\n",
                    251:                nfsstats.srvcache_inproghits,
                    252:                nfsstats.srvcache_idemdonehits,
                    253:                nfsstats.srvcache_nonidemdonehits,
                    254:                nfsstats.srvcache_misses);
                    255: }
                    256: 
                    257: u_char signalled;                      /* set if alarm goes off "early" */
                    258: 
                    259: /*
                    260:  * Print a running summary of nfs statistics.
                    261:  * Repeat display every interval seconds, showing statistics
                    262:  * collected over that interval.  Assumes that interval is non-zero.
                    263:  * First line printed at top of screen is always cumulative.
                    264:  */
                    265: sidewaysintpr(interval, off)
                    266:        unsigned interval;
                    267:        off_t off;
                    268: {
                    269:        struct nfsstats nfsstats, lastst;
                    270:        register int line;
                    271:        int oldmask;
                    272:        int catchalarm();
                    273: 
                    274:        klseek(kmem, off, 0);
                    275: 
                    276:        (void)signal(SIGALRM, catchalarm);
                    277:        signalled = NO;
                    278:        (void)alarm(interval);
                    279:        bzero((caddr_t)&lastst, sizeof(lastst));
                    280: banner:
                    281:        printf("        %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
                    282:                "Getattr", "Lookup", "Readlink", "Read",
                    283:                "Write", "Rename", "Link", "Readdir");
                    284:        fflush(stdout);
                    285:        line = 0;
                    286: loop:
                    287:        klseek(kmem, off, 0);
                    288:        read(kmem, (char *)&nfsstats, sizeof nfsstats);
                    289:        printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
                    290:                nfsstats.rpccnt[1]-lastst.rpccnt[1],
                    291:                nfsstats.rpccnt[4]-lastst.rpccnt[4],
                    292:                nfsstats.rpccnt[5]-lastst.rpccnt[5],
                    293:                nfsstats.rpccnt[6]-lastst.rpccnt[6],
                    294:                nfsstats.rpccnt[8]-lastst.rpccnt[8],
                    295:                nfsstats.rpccnt[11]-lastst.rpccnt[11],
                    296:                nfsstats.rpccnt[12]-lastst.rpccnt[12],
                    297:                nfsstats.rpccnt[16]-lastst.rpccnt[16]);
                    298:        printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
                    299:                nfsstats.srvrpccnt[1]-lastst.srvrpccnt[1],
                    300:                nfsstats.srvrpccnt[4]-lastst.srvrpccnt[4],
                    301:                nfsstats.srvrpccnt[5]-lastst.srvrpccnt[5],
                    302:                nfsstats.srvrpccnt[6]-lastst.srvrpccnt[6],
                    303:                nfsstats.srvrpccnt[8]-lastst.srvrpccnt[8],
                    304:                nfsstats.srvrpccnt[11]-lastst.srvrpccnt[11],
                    305:                nfsstats.srvrpccnt[12]-lastst.srvrpccnt[12],
                    306:                nfsstats.srvrpccnt[16]-lastst.srvrpccnt[16]);
                    307:        lastst = nfsstats;
                    308:        fflush(stdout);
                    309:        line++;
                    310:        oldmask = sigblock(sigmask(SIGALRM));
                    311:        if (! signalled) {
                    312:                sigpause(0);
                    313:        }
                    314:        sigsetmask(oldmask);
                    315:        signalled = NO;
                    316:        (void)alarm(interval);
                    317:        if (line == 21)
                    318:                goto banner;
                    319:        goto loop;
                    320:        /*NOTREACHED*/
                    321: }
                    322: 
                    323: /*
                    324:  * Called if an interval expires before sidewaysintpr has completed a loop.
                    325:  * Sets a flag to not wait for the alarm.
                    326:  */
                    327: catchalarm()
                    328: {
                    329:        signalled = YES;
                    330: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.