Remove advertising header from all userland binaries.
[dragonfly.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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#) Copyright (c) 1983, 1989, 1993 The Regents of the University of California.  All rights reserved.
33  * @(#)nfsstat.c        8.2 (Berkeley) 3/31/95
34  * $FreeBSD: src/usr.bin/nfsstat/nfsstat.c,v 1.15.2.3 2001/06/06 20:25:58 tmm Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/time.h>
40 #include <sys/sysctl.h>
41 #include <nfs/rpcv2.h>
42 #include <nfs/nfsproto.h>
43 #include <nfs/nfs.h>
44 #include <signal.h>
45 #include <fcntl.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <kvm.h>
49 #include <limits.h>
50 #include <nlist.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <paths.h>
56 #include <err.h>
57
58 struct nlist nl[] = {
59 #define N_NFSSTAT       0
60         { "_nfsstats" },
61         "",
62 };
63 kvm_t *kd;
64
65 static int deadkernel = 0;
66 static int widemode = 0;
67
68 void intpr(int, int);
69 void printhdr(int, int);
70 void sidewaysintpr(u_int, int, int);
71 void usage(void);
72 char *sperc1(int, int);
73 char *sperc2(int, int);
74
75 #define DELTA(field)    (nfsstats.field - lastst.field)
76
77 int
78 main(int argc, char **argv)
79 {
80         u_int interval;
81         int clientOnly = -1;
82         int serverOnly = -1;
83         int ch;
84         char *memf, *nlistf;
85         char errbuf[_POSIX2_LINE_MAX];
86
87         interval = 0;
88         memf = nlistf = NULL;
89         while ((ch = getopt(argc, argv, "csWM:N:w:")) != -1)
90                 switch(ch) {
91                 case 'M':
92                         memf = optarg;
93                         break;
94                 case 'N':
95                         nlistf = optarg;
96                         break;
97                 case 'W':
98                         widemode = 1;
99                         break;
100                 case 'w':
101                         interval = atoi(optarg);
102                         break;
103                 case 'c':
104                         clientOnly = 1;
105                         if (serverOnly < 0)
106                                 serverOnly = 0;
107                         break;
108                 case 's':
109                         serverOnly = 1;
110                         if (clientOnly < 0)
111                                 clientOnly = 0;
112                         break;
113                 case '?':
114                 default:
115                         usage();
116                 }
117         argc -= optind;
118         argv += optind;
119
120 #define BACKWARD_COMPATIBILITY
121 #ifdef  BACKWARD_COMPATIBILITY
122         if (*argv) {
123                 interval = atoi(*argv);
124                 if (*++argv) {
125                         nlistf = *argv;
126                         if (*++argv)
127                                 memf = *argv;
128                 }
129         }
130 #endif
131         if (nlistf != NULL || memf != NULL) {
132                 deadkernel = 1;
133
134                 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
135                                         errbuf)) == NULL) {
136                         errx(1, "kvm_openfiles: %s", errbuf);
137                 }
138                 if (kvm_nlist(kd, nl) != 0) {
139                         errx(1, "kvm_nlist: can't get names");
140                 }
141         }
142
143         if (interval)
144                 sidewaysintpr(interval, clientOnly, serverOnly);
145         else
146                 intpr(clientOnly, serverOnly);
147         exit(0);
148 }
149
150 /*
151  * Read the nfs stats using sysctl(3) for live kernels, or kvm_read
152  * for dead ones.
153  */
154 void
155 readstats(struct nfsstats *stp)
156 {
157         if(deadkernel) {
158                 if(kvm_read(kd, (u_long)nl[N_NFSSTAT].n_value, stp,
159                             sizeof *stp) < 0) {
160                         err(1, "kvm_read");
161                 }
162         } else {
163                 int name[3];
164                 size_t buflen = sizeof *stp;
165                 struct vfsconf vfc;
166
167                 if (getvfsbyname("nfs", &vfc) < 0)
168                         err(1, "getvfsbyname: NFS not compiled into kernel");
169                 name[0] = CTL_VFS;
170                 name[1] = vfc.vfc_typenum;
171                 name[2] = NFS_NFSSTATS;
172                 if (sysctl(name, 3, stp, &buflen, NULL, (size_t)0) < 0) {
173                         err(1, "sysctl");
174                 }
175         }
176 }
177
178 /*
179  * Print a description of the nfs stats.
180  */
181 void
182 intpr(int clientOnly, int serverOnly)
183 {
184         struct nfsstats nfsstats;
185
186         readstats(&nfsstats);
187
188         if (clientOnly) {
189                 printf("Client Info:\n");
190                 printf("Rpc Counts:\n");
191                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
192                         "Getattr", "Setattr", "Lookup", "Readlink", "Read",
193                         "Write", "Create", "Remove");
194                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
195                         nfsstats.rpccnt[NFSPROC_GETATTR],
196                         nfsstats.rpccnt[NFSPROC_SETATTR],
197                         nfsstats.rpccnt[NFSPROC_LOOKUP],
198                         nfsstats.rpccnt[NFSPROC_READLINK],
199                         nfsstats.rpccnt[NFSPROC_READ],
200                         nfsstats.rpccnt[NFSPROC_WRITE],
201                         nfsstats.rpccnt[NFSPROC_CREATE],
202                         nfsstats.rpccnt[NFSPROC_REMOVE]);
203                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
204                         "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
205                         "Readdir", "RdirPlus", "Access");
206                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
207                         nfsstats.rpccnt[NFSPROC_RENAME],
208                         nfsstats.rpccnt[NFSPROC_LINK],
209                         nfsstats.rpccnt[NFSPROC_SYMLINK],
210                         nfsstats.rpccnt[NFSPROC_MKDIR],
211                         nfsstats.rpccnt[NFSPROC_RMDIR],
212                         nfsstats.rpccnt[NFSPROC_READDIR],
213                         nfsstats.rpccnt[NFSPROC_READDIRPLUS],
214                         nfsstats.rpccnt[NFSPROC_ACCESS]);
215                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
216                         "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
217                         "GLease", "Vacate", "Evict");
218                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
219                         nfsstats.rpccnt[NFSPROC_MKNOD],
220                         nfsstats.rpccnt[NFSPROC_FSSTAT],
221                         nfsstats.rpccnt[NFSPROC_FSINFO],
222                         nfsstats.rpccnt[NFSPROC_PATHCONF],
223                         nfsstats.rpccnt[NFSPROC_COMMIT],
224                         nfsstats.rpccnt[NQNFSPROC_GETLEASE],
225                         nfsstats.rpccnt[NQNFSPROC_VACATED],
226                         nfsstats.rpccnt[NQNFSPROC_EVICTED]);
227                 printf("Rpc Info:\n");
228                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
229                         "TimedOut", "Invalid", "X Replies", "Retries", 
230                         "Requests");
231                 printf("%9d %9d %9d %9d %9d\n",
232                         nfsstats.rpctimeouts,
233                         nfsstats.rpcinvalid,
234                         nfsstats.rpcunexpected,
235                         nfsstats.rpcretries,
236                         nfsstats.rpcrequests);
237                 printf("Cache Info:\n");
238                 printf("%9.9s %9.9s %9.9s %9.9s",
239                         "Attr Hits", "Misses", "Lkup Hits", "Misses");
240                 printf(" %9.9s %9.9s %9.9s %9.9s\n",
241                         "BioR Hits", "Misses", "BioW Hits", "Misses");
242                 printf("%9d %9d %9d %9d",
243                         nfsstats.attrcache_hits, nfsstats.attrcache_misses,
244                         nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
245                 printf(" %9d %9d %9d %9d\n",
246                         nfsstats.biocache_reads-nfsstats.read_bios,
247                         nfsstats.read_bios,
248                         nfsstats.biocache_writes-nfsstats.write_bios,
249                         nfsstats.write_bios);
250                 printf("%9.9s %9.9s %9.9s %9.9s",
251                         "BioRLHits", "Misses", "BioD Hits", "Misses");
252                 printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
253                 printf("%9d %9d %9d %9d",
254                         nfsstats.biocache_readlinks-nfsstats.readlink_bios,
255                         nfsstats.readlink_bios,
256                         nfsstats.biocache_readdirs-nfsstats.readdir_bios,
257                         nfsstats.readdir_bios);
258                 printf(" %9d %9d\n",
259                         nfsstats.direofcache_hits, nfsstats.direofcache_misses);
260         }
261         if (serverOnly) {
262                 printf("\nServer Info:\n");
263                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
264                         "Getattr", "Setattr", "Lookup", "Readlink", "Read",
265                         "Write", "Create", "Remove");
266                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
267                         nfsstats.srvrpccnt[NFSPROC_GETATTR],
268                         nfsstats.srvrpccnt[NFSPROC_SETATTR],
269                         nfsstats.srvrpccnt[NFSPROC_LOOKUP],
270                         nfsstats.srvrpccnt[NFSPROC_READLINK],
271                         nfsstats.srvrpccnt[NFSPROC_READ],
272                         nfsstats.srvrpccnt[NFSPROC_WRITE],
273                         nfsstats.srvrpccnt[NFSPROC_CREATE],
274                         nfsstats.srvrpccnt[NFSPROC_REMOVE]);
275                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
276                         "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
277                         "Readdir", "RdirPlus", "Access");
278                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
279                         nfsstats.srvrpccnt[NFSPROC_RENAME],
280                         nfsstats.srvrpccnt[NFSPROC_LINK],
281                         nfsstats.srvrpccnt[NFSPROC_SYMLINK],
282                         nfsstats.srvrpccnt[NFSPROC_MKDIR],
283                         nfsstats.srvrpccnt[NFSPROC_RMDIR],
284                         nfsstats.srvrpccnt[NFSPROC_READDIR],
285                         nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
286                         nfsstats.srvrpccnt[NFSPROC_ACCESS]);
287                 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
288                         "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
289                         "GLease", "Vacate", "Evict");
290                 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
291                         nfsstats.srvrpccnt[NFSPROC_MKNOD],
292                         nfsstats.srvrpccnt[NFSPROC_FSSTAT],
293                         nfsstats.srvrpccnt[NFSPROC_FSINFO],
294                         nfsstats.srvrpccnt[NFSPROC_PATHCONF],
295                         nfsstats.srvrpccnt[NFSPROC_COMMIT],
296                         nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
297                         nfsstats.srvrpccnt[NQNFSPROC_VACATED],
298                         nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
299                 printf("Server Ret-Failed\n");
300                 printf("%17d\n", nfsstats.srvrpc_errs);
301                 printf("Server Faults\n");
302                 printf("%13d\n", nfsstats.srv_errs);
303                 printf("Server Cache Stats:\n");
304                 printf("%9.9s %9.9s %9.9s %9.9s\n",
305                         "Inprog", "Idem", "Non-idem", "Misses");
306                 printf("%9d %9d %9d %9d\n",
307                         nfsstats.srvcache_inproghits,
308                         nfsstats.srvcache_idemdonehits,
309                         nfsstats.srvcache_nonidemdonehits,
310                         nfsstats.srvcache_misses);
311                 printf("Server Lease Stats:\n");
312                 printf("%9.9s %9.9s %9.9s\n",
313                 "Leases", "PeakL", "GLeases");
314                 printf("%9d %9d %9d\n",
315                         nfsstats.srvnqnfs_leases,
316                         nfsstats.srvnqnfs_maxleases,
317                         nfsstats.srvnqnfs_getleases);
318                 printf("Server Write Gathering:\n");
319                 printf("%9.9s %9.9s %9.9s\n",
320                         "WriteOps", "WriteRPC", "Opsaved");
321                 printf("%9d %9d %9d\n",
322                         nfsstats.srvvop_writes,
323                         nfsstats.srvrpccnt[NFSPROC_WRITE],
324                         nfsstats.srvrpccnt[NFSPROC_WRITE] - 
325                             nfsstats.srvvop_writes);
326         }
327 }
328
329 u_char  signalled;                      /* set if alarm goes off "early" */
330
331 /*
332  * Print a running summary of nfs statistics.
333  * Repeat display every interval seconds, showing statistics
334  * collected over that interval.  Assumes that interval is non-zero.
335  * First line printed at top of screen is always cumulative.
336  */
337 void
338 sidewaysintpr(u_int interval, int clientOnly, int serverOnly)
339 {
340         struct nfsstats nfsstats, lastst;
341         int hdrcnt = 1;
342
343         readstats(&lastst);
344         sleep(interval);
345
346         for (;;) {
347                 readstats(&nfsstats);
348
349                 if (--hdrcnt == 0) {
350                         printhdr(clientOnly, serverOnly);
351                         if (clientOnly && serverOnly)
352                                 hdrcnt = 10;
353                         else
354                                 hdrcnt = 20;
355                 }
356                 if (clientOnly) {
357                     printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
358                         ((clientOnly && serverOnly) ? "Client:" : ""),
359                         DELTA(attrcache_hits) + DELTA(attrcache_misses),
360                         DELTA(lookupcache_hits) + DELTA(lookupcache_misses),
361                         DELTA(biocache_readlinks),
362                         DELTA(biocache_reads),
363                         DELTA(biocache_writes),
364                         nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
365                         DELTA(accesscache_hits) + DELTA(accesscache_misses),
366                         DELTA(biocache_readdirs)
367                     );
368                     if (widemode) {
369                             printf(" %s %s %s %s %s %s",
370                                 sperc1(DELTA(attrcache_hits),
371                                     DELTA(attrcache_misses)),
372                                 sperc1(DELTA(lookupcache_hits), 
373                                     DELTA(lookupcache_misses)),
374                                 sperc2(DELTA(biocache_reads),
375                                     DELTA(read_bios)),
376                                 sperc2(DELTA(biocache_writes),
377                                     DELTA(write_bios)),
378                                 sperc1(DELTA(accesscache_hits),
379                                     DELTA(accesscache_misses)),
380                                 sperc2(DELTA(biocache_readdirs),
381                                     DELTA(readdir_bios))
382                             );
383                     }
384                     printf("\n");
385                 }
386                 if (serverOnly) {
387                     printf("%s %6d %6d %6d %6d %6d %6d %6d %6d",
388                         ((clientOnly && serverOnly) ? "Server:" : ""),
389                         nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
390                         nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
391                         nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
392                         nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
393                         nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
394                         nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
395                         nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
396                         (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
397                         +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
398                     printf("\n");
399                 }
400                 lastst = nfsstats;
401                 fflush(stdout);
402                 sleep(interval);
403         }
404         /*NOTREACHED*/
405 }
406
407 void
408 printhdr(int clientOnly, int serverOnly)
409 {
410         printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
411             ((serverOnly && clientOnly) ? "        " : " "),
412             "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename",
413             "Access", "Rddir");
414         if (widemode && clientOnly) {
415                 printf(" Attr Lkup BioR BioW Accs BioD");
416         }
417         printf("\n");
418         fflush(stdout);
419 }
420
421 void
422 usage(void)
423 {
424         (void)fprintf(stderr,
425             "usage: nfsstat [-csW] [-M core] [-N system] [-w interval]\n");
426         exit(1);
427 }
428
429 static char SPBuf[64][8];
430 static int SPIndex;
431
432 char * 
433 sperc1(int hits, int misses)
434 {
435         char *p = SPBuf[SPIndex];
436
437         if (hits + misses) {
438                 sprintf(p, "%3d%%", 
439                     (int)(char)((quad_t)hits * 100 / (hits + misses)));
440         } else {
441                 sprintf(p, "   -");
442         }
443         SPIndex = (SPIndex + 1) & 63;
444         return(p);
445 }
446
447 char * 
448 sperc2(int ttl, int misses)
449 {
450         char *p = SPBuf[SPIndex];
451
452         if (ttl) {
453                 sprintf(p, "%3d%%",
454                     (int)(char)((quad_t)(ttl - misses) * 100 / ttl));
455         } else {
456                 sprintf(p, "   -");
457         }
458         SPIndex = (SPIndex + 1) & 63;
459         return(p);
460 }
461