]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/nfsstat/nfsstat.c
MFC 261512,261514:
[FreeBSD/stable/8.git] / usr.bin / nfsstat / nfsstat.c
1 /*
2  * Copyright (c) 1983, 1989, 1993
3  *      The Regents of the University of California.  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, 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 by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1983, 1989, 1993\n\
40         The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)nfsstat.c   8.2 (Berkeley) 3/31/95";
46 #endif
47 static const char rcsid[] =
48   "$FreeBSD$";
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/module.h>
53 #include <sys/mount.h>
54 #include <sys/time.h>
55 #include <sys/sysctl.h>
56 #include <nfs/nfsproto.h>
57 #include <nfsclient/nfs.h>
58 #include <nfsserver/nfs.h>
59 #include <nfs/nfssvc.h>
60
61 #include <fs/nfs/nfsport.h>
62
63 #include <signal.h>
64 #include <fcntl.h>
65 #include <ctype.h>
66 #include <errno.h>
67 #include <kvm.h>
68 #include <limits.h>
69 #include <nlist.h>
70 #include <unistd.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <paths.h>
75 #include <err.h>
76
77 struct nlist nl[] = {
78 #define N_NFSSTAT       0
79         { .n_name = "nfsstats" },
80 #define N_NFSRVSTAT     1
81         { .n_name = "nfsrvstats" },
82         { .n_name = NULL },
83 };
84 kvm_t *kd;
85
86 static int deadkernel = 0;
87 static int widemode = 0;
88 static int zflag = 0;
89 static int run_v4 = 0;
90 static int printtitle = 1;
91 static struct ext_nfsstats ext_nfsstats;
92
93 void intpr(int, int);
94 void printhdr(int, int);
95 void sidewaysintpr(u_int, int, int);
96 void usage(void);
97 char *sperc1(int, int);
98 char *sperc2(int, int);
99 void exp_intpr(int, int);
100 void exp_sidewaysintpr(u_int, int, int);
101
102 #define DELTA(field)    (nfsstats.field - lastst.field)
103
104 int
105 main(int argc, char **argv)
106 {
107         u_int interval;
108         int clientOnly = -1;
109         int serverOnly = -1;
110         int ch;
111         char *memf, *nlistf;
112         char errbuf[_POSIX2_LINE_MAX];
113
114         interval = 0;
115         memf = nlistf = NULL;
116         while ((ch = getopt(argc, argv, "cesWM:N:w:z")) != -1)
117                 switch(ch) {
118                 case 'M':
119                         memf = optarg;
120                         break;
121                 case 'N':
122                         nlistf = optarg;
123                         break;
124                 case 'W':
125                         widemode = 1;
126                         break;
127                 case 'w':
128                         interval = atoi(optarg);
129                         break;
130                 case 'c':
131                         clientOnly = 1;
132                         if (serverOnly < 0)
133                                 serverOnly = 0;
134                         break;
135                 case 's':
136                         serverOnly = 1;
137                         if (clientOnly < 0)
138                                 clientOnly = 0;
139                         break;
140                 case 'z':
141                         zflag = 1;
142                         break;
143                 case 'e':
144                         run_v4 = 1;
145                         break;
146                 case '?':
147                 default:
148                         usage();
149                 }
150         argc -= optind;
151         argv += optind;
152
153 #define BACKWARD_COMPATIBILITY
154 #ifdef  BACKWARD_COMPATIBILITY
155         if (*argv) {
156                 interval = atoi(*argv);
157                 if (*++argv) {
158                         nlistf = *argv;
159                         if (*++argv)
160                                 memf = *argv;
161                 }
162         }
163 #endif
164         if (run_v4 != 0 && modfind("nfscommon") < 0)
165                 errx(1, "experimental client/server not loaded");
166
167         if (run_v4 == 0 && (nlistf != NULL || memf != NULL)) {
168                 deadkernel = 1;
169
170                 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
171                                         errbuf)) == 0) {
172                         errx(1, "kvm_openfiles: %s", errbuf);
173                 }
174                 if (kvm_nlist(kd, nl) != 0) {
175                         errx(1, "kvm_nlist: can't get names");
176                 }
177         }
178
179         if (interval) {
180                 if (run_v4 > 0)
181                         exp_sidewaysintpr(interval, clientOnly, serverOnly);
182                 else
183                         sidewaysintpr(interval, clientOnly, serverOnly);
184         } else {
185                 if (run_v4 > 0)
186                         exp_intpr(clientOnly, serverOnly);
187                 else
188                         intpr(clientOnly, serverOnly);
189         }
190         exit(0);
191 }
192
193 /*
194  * Read the nfs stats using sysctl(3) for live kernels, or kvm_read
195  * for dead ones.
196  */
197 static void
198 readstats(struct nfsstats **stp, struct nfsrvstats **srvstp, int zero)
199 {
200         union {
201                 struct nfsstats client;
202                 struct nfsrvstats server;
203         } zerostat;
204         size_t buflen;
205
206         if (deadkernel) {
207                 if (*stp != NULL && kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value,
208                     *stp, sizeof(struct nfsstats)) < 0) {
209                         *stp = NULL;
210                 }
211                 if (*srvstp != NULL && kvm_read(kd,
212                     (u_long)nl[N_NFSRVSTAT].n_value, *srvstp,
213                     sizeof(struct nfsrvstats)) < 0) {
214                         *srvstp = NULL;
215                 }
216         } else {
217                 if (zero)
218                         bzero(&zerostat, sizeof(zerostat));
219                 buflen = sizeof(struct nfsstats);
220                 if (*stp != NULL && sysctlbyname("vfs.nfs.nfsstats", *stp,
221                     &buflen, zero ? &zerostat : NULL, zero ? buflen : 0) < 0) {
222                         if (errno != ENOENT)
223                                 err(1, "sysctl: vfs.nfs.nfsstats");
224                         *stp = NULL;
225                 }
226                 buflen = sizeof(struct nfsrvstats);
227                 if (*srvstp != NULL && sysctlbyname("vfs.nfsrv.nfsrvstats",
228                     *srvstp, &buflen, zero ? &zerostat : NULL,
229                     zero ? buflen : 0) < 0) {
230                         if (errno != ENOENT)
231                                 err(1, "sysctl: vfs.nfsrv.nfsrvstats");
232                         *srvstp = NULL;
233                 }
234         }
235 }
236
237 /*
238  * Print a description of the nfs stats.
239  */
240 void
241 intpr(int clientOnly, int serverOnly)
242 {
243         struct nfsstats nfsstats, *nfsstatsp;
244         struct nfsrvstats nfsrvstats, *nfsrvstatsp;
245
246         /*
247          * Only read the stats we are going to display to avoid zeroing
248          * stats the user didn't request.
249          */
250         if (clientOnly)
251                 nfsstatsp = &nfsstats;
252         else
253                 nfsstatsp = NULL;
254         if (serverOnly)
255                 nfsrvstatsp = &nfsrvstats;
256         else
257                 nfsrvstatsp = NULL;
258
259         readstats(&nfsstatsp, &nfsrvstatsp, zflag);
260
261         if (clientOnly && !nfsstatsp) {
262                 printf("Client not present!\n");
263                 clientOnly = 0;
264         }
265         if (clientOnly) {
266                 printf("Client Info:\n");
267                 printf("Rpc Counts:\n");
268                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
269                         "Getattr", "Setattr", "Lookup", "Readlink", "Read",
270                         "Write", "Create", "Remove");
271                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
272                         nfsstats.rpccnt[NFSPROC_GETATTR],
273                         nfsstats.rpccnt[NFSPROC_SETATTR],
274                         nfsstats.rpccnt[NFSPROC_LOOKUP],
275                         nfsstats.rpccnt[NFSPROC_READLINK],
276                         nfsstats.rpccnt[NFSPROC_READ],
277                         nfsstats.rpccnt[NFSPROC_WRITE],
278                         nfsstats.rpccnt[NFSPROC_CREATE],
279                         nfsstats.rpccnt[NFSPROC_REMOVE]);
280                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
281                         "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
282                         "Readdir", "RdirPlus", "Access");
283                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
284                         nfsstats.rpccnt[NFSPROC_RENAME],
285                         nfsstats.rpccnt[NFSPROC_LINK],
286                         nfsstats.rpccnt[NFSPROC_SYMLINK],
287                         nfsstats.rpccnt[NFSPROC_MKDIR],
288                         nfsstats.rpccnt[NFSPROC_RMDIR],
289                         nfsstats.rpccnt[NFSPROC_READDIR],
290                         nfsstats.rpccnt[NFSPROC_READDIRPLUS],
291                         nfsstats.rpccnt[NFSPROC_ACCESS]);
292                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
293                         "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
294                 printf("%9d %9d %9d %9d %9d\n",
295                         nfsstats.rpccnt[NFSPROC_MKNOD],
296                         nfsstats.rpccnt[NFSPROC_FSSTAT],
297                         nfsstats.rpccnt[NFSPROC_FSINFO],
298                         nfsstats.rpccnt[NFSPROC_PATHCONF],
299                         nfsstats.rpccnt[NFSPROC_COMMIT]);
300                 printf("Rpc Info:\n");
301                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
302                         "TimedOut", "Invalid", "X Replies", "Retries", 
303                         "Requests");
304                 printf("%9d %9d %9d %9d %9d\n",
305                         nfsstats.rpctimeouts,
306                         nfsstats.rpcinvalid,
307                         nfsstats.rpcunexpected,
308                         nfsstats.rpcretries,
309                         nfsstats.rpcrequests);
310                 printf("Cache Info:\n");
311                 printf("%9.9s %9.9s %9.9s %9.9s",
312                         "Attr Hits", "Misses", "Lkup Hits", "Misses");
313                 printf(" %9.9s %9.9s %9.9s %9.9s\n",
314                         "BioR Hits", "Misses", "BioW Hits", "Misses");
315                 printf("%9d %9d %9d %9d",
316                         nfsstats.attrcache_hits, nfsstats.attrcache_misses,
317                         nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
318                 printf(" %9d %9d %9d %9d\n",
319                         nfsstats.biocache_reads-nfsstats.read_bios,
320                         nfsstats.read_bios,
321                         nfsstats.biocache_writes-nfsstats.write_bios,
322                         nfsstats.write_bios);
323                 printf("%9.9s %9.9s %9.9s %9.9s",
324                         "BioRLHits", "Misses", "BioD Hits", "Misses");
325                 printf(" %9.9s %9.9s %9.9s %9.9s\n", "DirE Hits", "Misses", "Accs Hits", "Misses");
326                 printf("%9d %9d %9d %9d",
327                         nfsstats.biocache_readlinks-nfsstats.readlink_bios,
328                         nfsstats.readlink_bios,
329                         nfsstats.biocache_readdirs-nfsstats.readdir_bios,
330                         nfsstats.readdir_bios);
331                 printf(" %9d %9d %9d %9d\n",
332                         nfsstats.direofcache_hits, nfsstats.direofcache_misses,
333                         nfsstats.accesscache_hits, nfsstats.accesscache_misses);
334         }
335         if (serverOnly && !nfsrvstatsp) {
336                 printf("Server not present!\n");
337                 serverOnly = 0;
338         }
339         if (serverOnly) {
340                 printf("\nServer Info:\n");
341                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
342                         "Getattr", "Setattr", "Lookup", "Readlink", "Read",
343                         "Write", "Create", "Remove");
344                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
345                         nfsrvstats.srvrpccnt[NFSPROC_GETATTR],
346                         nfsrvstats.srvrpccnt[NFSPROC_SETATTR],
347                         nfsrvstats.srvrpccnt[NFSPROC_LOOKUP],
348                         nfsrvstats.srvrpccnt[NFSPROC_READLINK],
349                         nfsrvstats.srvrpccnt[NFSPROC_READ],
350                         nfsrvstats.srvrpccnt[NFSPROC_WRITE],
351                         nfsrvstats.srvrpccnt[NFSPROC_CREATE],
352                         nfsrvstats.srvrpccnt[NFSPROC_REMOVE]);
353                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
354                         "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
355                         "Readdir", "RdirPlus", "Access");
356                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
357                         nfsrvstats.srvrpccnt[NFSPROC_RENAME],
358                         nfsrvstats.srvrpccnt[NFSPROC_LINK],
359                         nfsrvstats.srvrpccnt[NFSPROC_SYMLINK],
360                         nfsrvstats.srvrpccnt[NFSPROC_MKDIR],
361                         nfsrvstats.srvrpccnt[NFSPROC_RMDIR],
362                         nfsrvstats.srvrpccnt[NFSPROC_READDIR],
363                         nfsrvstats.srvrpccnt[NFSPROC_READDIRPLUS],
364                         nfsrvstats.srvrpccnt[NFSPROC_ACCESS]);
365                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
366                         "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit");
367                 printf("%9d %9d %9d %9d %9d\n",
368                         nfsrvstats.srvrpccnt[NFSPROC_MKNOD],
369                         nfsrvstats.srvrpccnt[NFSPROC_FSSTAT],
370                         nfsrvstats.srvrpccnt[NFSPROC_FSINFO],
371                         nfsrvstats.srvrpccnt[NFSPROC_PATHCONF],
372                         nfsrvstats.srvrpccnt[NFSPROC_COMMIT]);
373                 printf("Server Ret-Failed\n");
374                 printf("%17d\n", nfsrvstats.srvrpc_errs);
375                 printf("Server Faults\n");
376                 printf("%13d\n", nfsrvstats.srv_errs);
377                 printf("Server Cache Stats:\n");
378                 printf("%9.9s %9.9s %9.9s %9.9s\n",
379                         "Inprog", "Idem", "Non-idem", "Misses");
380                 printf("%9d %9d %9d %9d\n",
381                         nfsrvstats.srvcache_inproghits,
382                         nfsrvstats.srvcache_idemdonehits,
383                         nfsrvstats.srvcache_nonidemdonehits,
384                         nfsrvstats.srvcache_misses);
385                 printf("Server Write Gathering:\n");
386                 printf("%9.9s %9.9s %9.9s\n",
387                         "WriteOps", "WriteRPC", "Opsaved");
388                 printf("%9d %9d %9d\n",
389                         nfsrvstats.srvvop_writes,
390                         nfsrvstats.srvrpccnt[NFSPROC_WRITE],
391                         nfsrvstats.srvrpccnt[NFSPROC_WRITE] - 
392                             nfsrvstats.srvvop_writes);
393         }
394 }
395
396 u_char  signalled;                      /* set if alarm goes off "early" */
397
398 /*
399  * Print a running summary of nfs statistics.
400  * Repeat display every interval seconds, showing statistics
401  * collected over that interval.  Assumes that interval is non-zero.
402  * First line printed at top of screen is always cumulative.
403  */
404 void
405 sidewaysintpr(u_int interval, int clientOnly, int serverOnly)
406 {
407         struct nfsstats nfsstats, lastst, *nfsstatsp;
408         struct nfsrvstats nfsrvstats, lastsrvst, *nfsrvstatsp;
409         int hdrcnt = 1;
410
411         nfsstatsp = &lastst;
412         nfsrvstatsp = &lastsrvst;
413         readstats(&nfsstatsp, &nfsrvstatsp, 0);
414         if (clientOnly && !nfsstatsp) {
415                 printf("Client not present!\n");
416                 clientOnly = 0;
417         }
418         if (serverOnly && !nfsrvstatsp) {
419                 printf("Server not present!\n");
420                 serverOnly = 0;
421         }
422         sleep(interval);
423
424         for (;;) {
425                 nfsstatsp = &nfsstats;
426                 nfsrvstatsp = &nfsrvstats;
427                 readstats(&nfsstatsp, &nfsrvstatsp, 0);
428
429                 if (--hdrcnt == 0) {
430                         printhdr(clientOnly, serverOnly);
431                         if (clientOnly && serverOnly)
432                                 hdrcnt = 10;
433                         else
434                                 hdrcnt = 20;
435                 }
436                 if (clientOnly) {
437                     printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
438                         ((clientOnly && serverOnly) ? "Client:" : ""),
439                         DELTA(rpccnt[NFSPROC_GETATTR]),
440                         DELTA(rpccnt[NFSPROC_LOOKUP]),
441                         DELTA(rpccnt[NFSPROC_READLINK]),
442                         DELTA(rpccnt[NFSPROC_READ]),
443                         DELTA(rpccnt[NFSPROC_WRITE]),
444                         DELTA(rpccnt[NFSPROC_RENAME]),
445                         DELTA(rpccnt[NFSPROC_ACCESS]),
446                         DELTA(rpccnt[NFSPROC_READDIR]) +
447                         DELTA(rpccnt[NFSPROC_READDIRPLUS])
448                     );
449                     if (widemode) {
450                             printf(" %s %s %s %s %s %s",
451                                 sperc1(DELTA(attrcache_hits),
452                                     DELTA(attrcache_misses)),
453                                 sperc1(DELTA(lookupcache_hits), 
454                                     DELTA(lookupcache_misses)),
455                                 sperc2(DELTA(biocache_reads),
456                                     DELTA(read_bios)),
457                                 sperc2(DELTA(biocache_writes),
458                                     DELTA(write_bios)),
459                                 sperc1(DELTA(accesscache_hits),
460                                     DELTA(accesscache_misses)),
461                                 sperc2(DELTA(biocache_readdirs),
462                                     DELTA(readdir_bios))
463                             );
464                     }
465                     printf("\n");
466                     lastst = nfsstats;
467                 }
468                 if (serverOnly) {
469                     printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
470                         ((clientOnly && serverOnly) ? "Server:" : ""),
471                         nfsrvstats.srvrpccnt[NFSPROC_GETATTR]-lastsrvst.srvrpccnt[NFSPROC_GETATTR],
472                         nfsrvstats.srvrpccnt[NFSPROC_LOOKUP]-lastsrvst.srvrpccnt[NFSPROC_LOOKUP],
473                         nfsrvstats.srvrpccnt[NFSPROC_READLINK]-lastsrvst.srvrpccnt[NFSPROC_READLINK],
474                         nfsrvstats.srvrpccnt[NFSPROC_READ]-lastsrvst.srvrpccnt[NFSPROC_READ],
475                         nfsrvstats.srvrpccnt[NFSPROC_WRITE]-lastsrvst.srvrpccnt[NFSPROC_WRITE],
476                         nfsrvstats.srvrpccnt[NFSPROC_RENAME]-lastsrvst.srvrpccnt[NFSPROC_RENAME],
477                         nfsrvstats.srvrpccnt[NFSPROC_ACCESS]-lastsrvst.srvrpccnt[NFSPROC_ACCESS],
478                         (nfsrvstats.srvrpccnt[NFSPROC_READDIR]-lastsrvst.srvrpccnt[NFSPROC_READDIR])
479                         +(nfsrvstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastsrvst.srvrpccnt[NFSPROC_READDIRPLUS]));
480                     printf("\n");
481                     lastsrvst = nfsrvstats;
482                 }
483                 fflush(stdout);
484                 sleep(interval);
485         }
486         /*NOTREACHED*/
487 }
488
489 void
490 printhdr(int clientOnly, int serverOnly)
491 {
492         printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
493             ((serverOnly && clientOnly) ? "        " : " "),
494             "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename",
495             "Access", "Rddir");
496         if (widemode && clientOnly) {
497                 printf(" Attr Lkup BioR BioW Accs BioD");
498         }
499         printf("\n");
500         fflush(stdout);
501 }
502
503 void
504 usage(void)
505 {
506         (void)fprintf(stderr,
507             "usage: nfsstat [-ceszW] [-M core] [-N system] [-w interval]\n");
508         exit(1);
509 }
510
511 static char SPBuf[64][8];
512 static int SPIndex;
513
514 char * 
515 sperc1(int hits, int misses)
516 {
517         char *p = SPBuf[SPIndex];
518
519         if (hits + misses) {
520                 sprintf(p, "%3d%%", 
521                     (int)(char)((quad_t)hits * 100 / (hits + misses)));
522         } else {
523                 sprintf(p, "   -");
524         }
525         SPIndex = (SPIndex + 1) & 63;
526         return(p);
527 }
528
529 char * 
530 sperc2(int ttl, int misses)
531 {
532         char *p = SPBuf[SPIndex];
533
534         if (ttl) {
535                 sprintf(p, "%3d%%",
536                     (int)(char)((quad_t)(ttl - misses) * 100 / ttl));
537         } else {
538                 sprintf(p, "   -");
539         }
540         SPIndex = (SPIndex + 1) & 63;
541         return(p);
542 }
543
544 /*
545  * Print a description of the nfs stats for the experimental client/server.
546  */
547 void
548 exp_intpr(int clientOnly, int serverOnly)
549 {
550         int nfssvc_flag;
551
552         nfssvc_flag = NFSSVC_GETSTATS;
553         if (zflag != 0) {
554                 if (clientOnly != 0)
555                         nfssvc_flag |= NFSSVC_ZEROCLTSTATS;
556                 if (serverOnly != 0)
557                         nfssvc_flag |= NFSSVC_ZEROSRVSTATS;
558         }
559         if (nfssvc(nfssvc_flag, &ext_nfsstats) < 0)
560                 err(1, "Can't get stats");
561         if (clientOnly != 0) {
562                 if (printtitle) {
563                         printf("Client Info:\n");
564                         printf("Rpc Counts:\n");
565                         printf(
566                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
567                             , "Getattr", "Setattr", "Lookup", "Readlink",
568                             "Read", "Write", "Create", "Remove");
569                 }
570                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
571                     ext_nfsstats.rpccnt[NFSPROC_GETATTR],
572                     ext_nfsstats.rpccnt[NFSPROC_SETATTR],
573                     ext_nfsstats.rpccnt[NFSPROC_LOOKUP],
574                     ext_nfsstats.rpccnt[NFSPROC_READLINK],
575                     ext_nfsstats.rpccnt[NFSPROC_READ],
576                     ext_nfsstats.rpccnt[NFSPROC_WRITE],
577                     ext_nfsstats.rpccnt[NFSPROC_CREATE],
578                     ext_nfsstats.rpccnt[NFSPROC_REMOVE]);
579                 if (printtitle)
580                         printf(
581                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
582                             , "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
583                             "Readdir", "RdirPlus", "Access");
584                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
585                     ext_nfsstats.rpccnt[NFSPROC_RENAME],
586                     ext_nfsstats.rpccnt[NFSPROC_LINK],
587                     ext_nfsstats.rpccnt[NFSPROC_SYMLINK],
588                     ext_nfsstats.rpccnt[NFSPROC_MKDIR],
589                     ext_nfsstats.rpccnt[NFSPROC_RMDIR],
590                     ext_nfsstats.rpccnt[NFSPROC_READDIR],
591                     ext_nfsstats.rpccnt[NFSPROC_READDIRPLUS],
592                     ext_nfsstats.rpccnt[NFSPROC_ACCESS]);
593                 if (printtitle)
594                         printf(
595                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
596                             , "Mknod", "Fsstat", "Fsinfo", "PathConf",
597                             "Commit", "SetClId", "SetClIdCf", "Lock");
598                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
599                     ext_nfsstats.rpccnt[NFSPROC_MKNOD],
600                     ext_nfsstats.rpccnt[NFSPROC_FSSTAT],
601                     ext_nfsstats.rpccnt[NFSPROC_FSINFO],
602                     ext_nfsstats.rpccnt[NFSPROC_PATHCONF],
603                     ext_nfsstats.rpccnt[NFSPROC_COMMIT],
604                     ext_nfsstats.rpccnt[NFSPROC_SETCLIENTID],
605                     ext_nfsstats.rpccnt[NFSPROC_SETCLIENTIDCFRM],
606                     ext_nfsstats.rpccnt[NFSPROC_LOCK]);
607                 if (printtitle)
608                         printf("%9.9s %9.9s %9.9s %9.9s\n",
609                             "LockT", "LockU", "Open", "OpenCfr");
610                 printf("%9d %9d %9d %9d\n",
611                     ext_nfsstats.rpccnt[NFSPROC_LOCKT],
612                     ext_nfsstats.rpccnt[NFSPROC_LOCKU],
613                     ext_nfsstats.rpccnt[NFSPROC_OPEN],
614                     ext_nfsstats.rpccnt[NFSPROC_OPENCONFIRM]);
615                 if (printtitle)
616                         printf(
617                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
618                             , "OpenOwner", "Opens", "LockOwner",
619                             "Locks", "Delegs", "LocalOwn",
620                             "LocalOpen", "LocalLOwn");
621                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
622                     ext_nfsstats.clopenowners,
623                     ext_nfsstats.clopens,
624                     ext_nfsstats.cllockowners,
625                     ext_nfsstats.cllocks,
626                     ext_nfsstats.cldelegates,
627                     ext_nfsstats.cllocalopenowners,
628                     ext_nfsstats.cllocalopens,
629                     ext_nfsstats.cllocallockowners);
630                 if (printtitle)
631                         printf("%9.9s\n", "LocalLock");
632                 printf("%9d\n", ext_nfsstats.cllocallocks);
633                 if (printtitle) {
634                         printf("Rpc Info:\n");
635                         printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
636                             "TimedOut", "Invalid", "X Replies", "Retries",
637                             "Requests");
638                 }
639                 printf("%9d %9d %9d %9d %9d\n",
640                     ext_nfsstats.rpctimeouts,
641                     ext_nfsstats.rpcinvalid,
642                     ext_nfsstats.rpcunexpected,
643                     ext_nfsstats.rpcretries,
644                     ext_nfsstats.rpcrequests);
645                 if (printtitle) {
646                         printf("Cache Info:\n");
647                         printf("%9.9s %9.9s %9.9s %9.9s",
648                             "Attr Hits", "Misses", "Lkup Hits", "Misses");
649                         printf(" %9.9s %9.9s %9.9s %9.9s\n",
650                             "BioR Hits", "Misses", "BioW Hits", "Misses");
651                 }
652                 printf("%9d %9d %9d %9d",
653                     ext_nfsstats.attrcache_hits,
654                     ext_nfsstats.attrcache_misses,
655                     ext_nfsstats.lookupcache_hits,
656                     ext_nfsstats.lookupcache_misses);
657                 printf(" %9d %9d %9d %9d\n",
658                     ext_nfsstats.biocache_reads - ext_nfsstats.read_bios,
659                     ext_nfsstats.read_bios,
660                     ext_nfsstats.biocache_writes - ext_nfsstats.write_bios,
661                     ext_nfsstats.write_bios);
662                 if (printtitle) {
663                         printf("%9.9s %9.9s %9.9s %9.9s",
664                             "BioRLHits", "Misses", "BioD Hits", "Misses");
665                         printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
666                 }
667                 printf("%9d %9d %9d %9d",
668                     ext_nfsstats.biocache_readlinks -
669                     ext_nfsstats.readlink_bios,
670                     ext_nfsstats.readlink_bios,
671                     ext_nfsstats.biocache_readdirs -
672                     ext_nfsstats.readdir_bios,
673                     ext_nfsstats.readdir_bios);
674                 printf(" %9d %9d\n",
675                     ext_nfsstats.direofcache_hits,
676                     ext_nfsstats.direofcache_misses);
677         }
678         if (serverOnly != 0) {
679                 if (printtitle) {
680                         printf("\nServer Info:\n");
681                         printf(
682                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
683                             , "Getattr", "Setattr", "Lookup", "Readlink",
684                             "Read", "Write", "Create", "Remove");
685                 }
686                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
687                     ext_nfsstats.srvrpccnt[NFSV4OP_GETATTR],
688                     ext_nfsstats.srvrpccnt[NFSV4OP_SETATTR],
689                     ext_nfsstats.srvrpccnt[NFSV4OP_LOOKUP],
690                     ext_nfsstats.srvrpccnt[NFSV4OP_READLINK],
691                     ext_nfsstats.srvrpccnt[NFSV4OP_READ],
692                     ext_nfsstats.srvrpccnt[NFSV4OP_WRITE],
693                     ext_nfsstats.srvrpccnt[NFSV4OP_V3CREATE],
694                     ext_nfsstats.srvrpccnt[NFSV4OP_REMOVE]);
695                 if (printtitle)
696                         printf(
697                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
698                             , "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
699                             "Readdir", "RdirPlus", "Access");
700                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
701                     ext_nfsstats.srvrpccnt[NFSV4OP_RENAME],
702                     ext_nfsstats.srvrpccnt[NFSV4OP_LINK],
703                     ext_nfsstats.srvrpccnt[NFSV4OP_SYMLINK],
704                     ext_nfsstats.srvrpccnt[NFSV4OP_MKDIR],
705                     ext_nfsstats.srvrpccnt[NFSV4OP_RMDIR],
706                     ext_nfsstats.srvrpccnt[NFSV4OP_READDIR],
707                     ext_nfsstats.srvrpccnt[NFSV4OP_READDIRPLUS],
708                     ext_nfsstats.srvrpccnt[NFSV4OP_ACCESS]);
709                 if (printtitle)
710                         printf(
711                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
712                             , "Mknod", "Fsstat", "Fsinfo", "PathConf",
713                             "Commit", "LookupP", "SetClId", "SetClIdCf");
714                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
715                     ext_nfsstats.srvrpccnt[NFSV4OP_MKNOD],
716                     ext_nfsstats.srvrpccnt[NFSV4OP_FSSTAT],
717                     ext_nfsstats.srvrpccnt[NFSV4OP_FSINFO],
718                     ext_nfsstats.srvrpccnt[NFSV4OP_PATHCONF],
719                     ext_nfsstats.srvrpccnt[NFSV4OP_COMMIT],
720                     ext_nfsstats.srvrpccnt[NFSV4OP_LOOKUPP],
721                     ext_nfsstats.srvrpccnt[NFSV4OP_SETCLIENTID],
722                     ext_nfsstats.srvrpccnt[NFSV4OP_SETCLIENTIDCFRM]);
723                 if (printtitle)
724                         printf(
725                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
726                             , "Open", "OpenAttr", "OpenDwnGr", "OpenCfrm",
727                             "DelePurge", "DeleRet", "GetFH", "Lock");
728                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
729                     ext_nfsstats.srvrpccnt[NFSV4OP_OPEN],
730                     ext_nfsstats.srvrpccnt[NFSV4OP_OPENATTR],
731                     ext_nfsstats.srvrpccnt[NFSV4OP_OPENDOWNGRADE],
732                     ext_nfsstats.srvrpccnt[NFSV4OP_OPENCONFIRM],
733                     ext_nfsstats.srvrpccnt[NFSV4OP_DELEGPURGE],
734                     ext_nfsstats.srvrpccnt[NFSV4OP_DELEGRETURN],
735                     ext_nfsstats.srvrpccnt[NFSV4OP_GETFH],
736                     ext_nfsstats.srvrpccnt[NFSV4OP_LOCK]);
737                 if (printtitle)
738                         printf(
739                             "%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n"
740                             , "LockT", "LockU", "Close", "Verify", "NVerify",
741                             "PutFH", "PutPubFH", "PutRootFH");
742                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
743                     ext_nfsstats.srvrpccnt[NFSV4OP_LOCKT],
744                     ext_nfsstats.srvrpccnt[NFSV4OP_LOCKU],
745                     ext_nfsstats.srvrpccnt[NFSV4OP_CLOSE],
746                     ext_nfsstats.srvrpccnt[NFSV4OP_VERIFY],
747                     ext_nfsstats.srvrpccnt[NFSV4OP_NVERIFY],
748                     ext_nfsstats.srvrpccnt[NFSV4OP_PUTFH],
749                     ext_nfsstats.srvrpccnt[NFSV4OP_PUTPUBFH],
750                     ext_nfsstats.srvrpccnt[NFSV4OP_PUTROOTFH]);
751                 if (printtitle)
752                         printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
753                             "Renew", "RestoreFH", "SaveFH", "Secinfo",
754                             "RelLckOwn", "V4Create");
755                 printf("%9d %9d %9d %9d %9d %9d\n",
756                     ext_nfsstats.srvrpccnt[NFSV4OP_RENEW],
757                     ext_nfsstats.srvrpccnt[NFSV4OP_RESTOREFH],
758                     ext_nfsstats.srvrpccnt[NFSV4OP_SAVEFH],
759                     ext_nfsstats.srvrpccnt[NFSV4OP_SECINFO],
760                     ext_nfsstats.srvrpccnt[NFSV4OP_RELEASELCKOWN],
761                     ext_nfsstats.srvrpccnt[NFSV4OP_CREATE]);
762                 if (printtitle) {
763                         printf("Server:\n");
764                         printf("%9.9s %9.9s %9.9s\n",
765                             "Retfailed", "Faults", "Clients");
766                 }
767                 printf("%9d %9d %9d\n",
768                     ext_nfsstats.srv_errs, ext_nfsstats.srvrpc_errs,
769                     ext_nfsstats.srvclients);
770                 if (printtitle)
771                         printf("%9.9s %9.9s %9.9s %9.9s %9.9s \n",
772                             "OpenOwner", "Opens", "LockOwner",
773                             "Locks", "Delegs");
774                 printf("%9d %9d %9d %9d %9d \n",
775                     ext_nfsstats.srvopenowners,
776                     ext_nfsstats.srvopens,
777                     ext_nfsstats.srvlockowners,
778                     ext_nfsstats.srvlocks,
779                     ext_nfsstats.srvdelegates);
780                 if (printtitle) {
781                         printf("Server Cache Stats:\n");
782                         printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
783                             "Inprog", "Idem", "Non-idem", "Misses", 
784                             "CacheSize", "TCPPeak");
785                 }
786                 printf("%9d %9d %9d %9d %9d %9d\n",
787                     ext_nfsstats.srvcache_inproghits,
788                     ext_nfsstats.srvcache_idemdonehits,
789                     ext_nfsstats.srvcache_nonidemdonehits,
790                     ext_nfsstats.srvcache_misses,
791                     ext_nfsstats.srvcache_size,
792                     ext_nfsstats.srvcache_tcppeak);
793         }
794 }
795
796 /*
797  * Print a running summary of nfs statistics for the experimental client and/or
798  * server.
799  * Repeat display every interval seconds, showing statistics
800  * collected over that interval.  Assumes that interval is non-zero.
801  * First line printed at top of screen is always cumulative.
802  */
803 void
804 exp_sidewaysintpr(u_int interval, int clientOnly, int serverOnly)
805 {
806         struct ext_nfsstats nfsstats, lastst, *ext_nfsstatsp;
807         int hdrcnt = 1;
808
809         ext_nfsstatsp = &lastst;
810         if (nfssvc(NFSSVC_GETSTATS, ext_nfsstatsp) < 0)
811                 err(1, "Can't get stats");
812         sleep(interval);
813
814         for (;;) {
815                 ext_nfsstatsp = &nfsstats;
816                 if (nfssvc(NFSSVC_GETSTATS, ext_nfsstatsp) < 0)
817                         err(1, "Can't get stats");
818
819                 if (--hdrcnt == 0) {
820                         printhdr(clientOnly, serverOnly);
821                         if (clientOnly && serverOnly)
822                                 hdrcnt = 10;
823                         else
824                                 hdrcnt = 20;
825                 }
826                 if (clientOnly) {
827                     printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
828                         ((clientOnly && serverOnly) ? "Client:" : ""),
829                         DELTA(rpccnt[NFSPROC_GETATTR]),
830                         DELTA(rpccnt[NFSPROC_LOOKUP]),
831                         DELTA(rpccnt[NFSPROC_READLINK]),
832                         DELTA(rpccnt[NFSPROC_READ]),
833                         DELTA(rpccnt[NFSPROC_WRITE]),
834                         DELTA(rpccnt[NFSPROC_RENAME]),
835                         DELTA(rpccnt[NFSPROC_ACCESS]),
836                         DELTA(rpccnt[NFSPROC_READDIR]) +
837                         DELTA(rpccnt[NFSPROC_READDIRPLUS])
838                     );
839                     if (widemode) {
840                             printf(" %s %s %s %s %s %s",
841                                 sperc1(DELTA(attrcache_hits),
842                                     DELTA(attrcache_misses)),
843                                 sperc1(DELTA(lookupcache_hits), 
844                                     DELTA(lookupcache_misses)),
845                                 sperc2(DELTA(biocache_reads),
846                                     DELTA(read_bios)),
847                                 sperc2(DELTA(biocache_writes),
848                                     DELTA(write_bios)),
849                                 sperc1(DELTA(accesscache_hits),
850                                     DELTA(accesscache_misses)),
851                                 sperc2(DELTA(biocache_readdirs),
852                                     DELTA(readdir_bios))
853                             );
854                     }
855                     printf("\n");
856                     lastst = nfsstats;
857                 }
858                 if (serverOnly) {
859                     printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
860                         ((clientOnly && serverOnly) ? "Server:" : ""),
861                         DELTA(srvrpccnt[NFSV4OP_GETATTR]),
862                         DELTA(srvrpccnt[NFSV4OP_LOOKUP]),
863                         DELTA(srvrpccnt[NFSV4OP_READLINK]),
864                         DELTA(srvrpccnt[NFSV4OP_READ]),
865                         DELTA(srvrpccnt[NFSV4OP_WRITE]),
866                         DELTA(srvrpccnt[NFSV4OP_RENAME]),
867                         DELTA(srvrpccnt[NFSV4OP_ACCESS]),
868                         DELTA(srvrpccnt[NFSV4OP_READDIR]) +
869                         DELTA(srvrpccnt[NFSV4OP_READDIRPLUS]));
870                     printf("\n");
871                     lastst = nfsstats;
872                 }
873                 fflush(stdout);
874                 sleep(interval);
875         }
876         /*NOTREACHED*/
877 }
878