nfsstat.c revision 1.8 1 /* $NetBSD: nfsstat.c,v 1.8 1997/03/03 22:22:48 explorer Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1983, 1989, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "from: @(#)nfsstat.c 8.1 (Berkeley) 6/6/93";
48 #else
49 static char *rcsid = "$NetBSD: nfsstat.c,v 1.8 1997/03/03 22:22:48 explorer Exp $";
50 #endif
51 #endif /* not lint */
52
53 #include <sys/param.h>
54 #include <sys/mount.h>
55 #include <sys/sysctl.h>
56
57 #include <nfs/rpcv2.h>
58 #include <nfs/nfsproto.h>
59 #include <nfs/nfs.h>
60
61 #include <ctype.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <kvm.h>
66 #include <limits.h>
67 #include <nlist.h>
68 #include <paths.h>
69 #include <signal.h>
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include <string.h>
73 #include <unistd.h>
74
75 struct nlist nl[] = {
76 #define N_NFSSTAT 0
77 { "_nfsstats" },
78 "",
79 };
80 kvm_t *kd;
81
82 void intpr(), printhdr(), sidewaysintpr(), usage();
83
84 main(argc, argv)
85 int argc;
86 char **argv;
87 {
88 extern int optind;
89 extern char *optarg;
90 u_int interval;
91 int ch;
92 char *memf, *nlistf;
93 char errbuf[_POSIX2_LINE_MAX];
94
95 interval = 0;
96 memf = nlistf = NULL;
97 while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
98 switch(ch) {
99 case 'M':
100 memf = optarg;
101 break;
102 case 'N':
103 nlistf = optarg;
104 break;
105 case 'w':
106 interval = atoi(optarg);
107 break;
108 case '?':
109 default:
110 usage();
111 }
112 argc -= optind;
113 argv += optind;
114
115 #define BACKWARD_COMPATIBILITY
116 #ifdef BACKWARD_COMPATIBILITY
117 if (*argv) {
118 interval = atoi(*argv);
119 if (*++argv) {
120 nlistf = *argv;
121 if (*++argv)
122 memf = *argv;
123 }
124 }
125 #endif
126 /*
127 * Discard setgid privileges if not the running kernel so that bad
128 * guys can't print interesting stuff from kernel memory.
129 */
130 if (nlistf != NULL || memf != NULL)
131 setgid(getgid());
132
133 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == 0) {
134 fprintf(stderr, "nfsstat: kvm_openfiles: %s\n", errbuf);
135 exit(1);
136 }
137 if (kvm_nlist(kd, nl) != 0) {
138 fprintf(stderr, "nfsstat: kvm_nlist: can't get names\n");
139 exit(1);
140 }
141
142 if (interval)
143 sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
144 else
145 intpr(nl[N_NFSSTAT].n_value);
146 exit(0);
147 }
148
149 /*
150 * Print a description of the nfs stats.
151 */
152 void
153 intpr(nfsstataddr)
154 u_long nfsstataddr;
155 {
156 struct nfsstats nfsstats;
157
158 if (kvm_read(kd, (u_long)nfsstataddr, (char *)&nfsstats, sizeof(struct nfsstats)) < 0) {
159 fprintf(stderr, "nfsstat: kvm_read failed\n");
160 exit(1);
161 }
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[NFSPROC_GETATTR],
169 nfsstats.rpccnt[NFSPROC_SETATTR],
170 nfsstats.rpccnt[NFSPROC_LOOKUP],
171 nfsstats.rpccnt[NFSPROC_READLINK],
172 nfsstats.rpccnt[NFSPROC_READ],
173 nfsstats.rpccnt[NFSPROC_WRITE],
174 nfsstats.rpccnt[NFSPROC_CREATE],
175 nfsstats.rpccnt[NFSPROC_REMOVE]);
176 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
177 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
178 "Readdir", "RdirPlus", "Access");
179 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
180 nfsstats.rpccnt[NFSPROC_RENAME],
181 nfsstats.rpccnt[NFSPROC_LINK],
182 nfsstats.rpccnt[NFSPROC_SYMLINK],
183 nfsstats.rpccnt[NFSPROC_MKDIR],
184 nfsstats.rpccnt[NFSPROC_RMDIR],
185 nfsstats.rpccnt[NFSPROC_READDIR],
186 nfsstats.rpccnt[NFSPROC_READDIRPLUS],
187 nfsstats.rpccnt[NFSPROC_ACCESS]);
188 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
189 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
190 "GLease", "Vacate", "Evict");
191 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
192 nfsstats.rpccnt[NFSPROC_MKNOD],
193 nfsstats.rpccnt[NFSPROC_FSSTAT],
194 nfsstats.rpccnt[NFSPROC_FSINFO],
195 nfsstats.rpccnt[NFSPROC_PATHCONF],
196 nfsstats.rpccnt[NFSPROC_COMMIT],
197 nfsstats.rpccnt[NQNFSPROC_GETLEASE],
198 nfsstats.rpccnt[NQNFSPROC_VACATED],
199 nfsstats.rpccnt[NQNFSPROC_EVICTED]);
200 printf("Rpc Info:\n");
201 printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
202 "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
203 printf("%9d %9d %9d %9d %9d\n",
204 nfsstats.rpctimeouts,
205 nfsstats.rpcinvalid,
206 nfsstats.rpcunexpected,
207 nfsstats.rpcretries,
208 nfsstats.rpcrequests);
209 printf("Cache Info:\n");
210 printf("%9.9s %9.9s %9.9s %9.9s",
211 "Attr Hits", "Misses", "Lkup Hits", "Misses");
212 printf(" %9.9s %9.9s %9.9s %9.9s\n",
213 "BioR Hits", "Misses", "BioW Hits", "Misses");
214 printf("%9d %9d %9d %9d",
215 nfsstats.attrcache_hits, nfsstats.attrcache_misses,
216 nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
217 printf(" %9d %9d %9d %9d\n",
218 nfsstats.biocache_reads-nfsstats.read_bios,
219 nfsstats.read_bios,
220 nfsstats.biocache_writes-nfsstats.write_bios,
221 nfsstats.write_bios);
222 printf("%9.9s %9.9s %9.9s %9.9s",
223 "BioRLHits", "Misses", "BioD Hits", "Misses");
224 printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
225 printf("%9d %9d %9d %9d",
226 nfsstats.biocache_readlinks-nfsstats.readlink_bios,
227 nfsstats.readlink_bios,
228 nfsstats.biocache_readdirs-nfsstats.readdir_bios,
229 nfsstats.readdir_bios);
230 printf(" %9d %9d\n",
231 nfsstats.direofcache_hits, nfsstats.direofcache_misses);
232 printf("\nServer Info:\n");
233 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
234 "Getattr", "Setattr", "Lookup", "Readlink", "Read",
235 "Write", "Create", "Remove");
236 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
237 nfsstats.srvrpccnt[NFSPROC_GETATTR],
238 nfsstats.srvrpccnt[NFSPROC_SETATTR],
239 nfsstats.srvrpccnt[NFSPROC_LOOKUP],
240 nfsstats.srvrpccnt[NFSPROC_READLINK],
241 nfsstats.srvrpccnt[NFSPROC_READ],
242 nfsstats.srvrpccnt[NFSPROC_WRITE],
243 nfsstats.srvrpccnt[NFSPROC_CREATE],
244 nfsstats.srvrpccnt[NFSPROC_REMOVE]);
245 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
246 "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
247 "Readdir", "RdirPlus", "Access");
248 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
249 nfsstats.srvrpccnt[NFSPROC_RENAME],
250 nfsstats.srvrpccnt[NFSPROC_LINK],
251 nfsstats.srvrpccnt[NFSPROC_SYMLINK],
252 nfsstats.srvrpccnt[NFSPROC_MKDIR],
253 nfsstats.srvrpccnt[NFSPROC_RMDIR],
254 nfsstats.srvrpccnt[NFSPROC_READDIR],
255 nfsstats.srvrpccnt[NFSPROC_READDIRPLUS],
256 nfsstats.srvrpccnt[NFSPROC_ACCESS]);
257 printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
258 "Mknod", "Fsstat", "Fsinfo", "PathConf", "Commit",
259 "GLease", "Vacate", "Evict");
260 printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
261 nfsstats.srvrpccnt[NFSPROC_MKNOD],
262 nfsstats.srvrpccnt[NFSPROC_FSSTAT],
263 nfsstats.srvrpccnt[NFSPROC_FSINFO],
264 nfsstats.srvrpccnt[NFSPROC_PATHCONF],
265 nfsstats.srvrpccnt[NFSPROC_COMMIT],
266 nfsstats.srvrpccnt[NQNFSPROC_GETLEASE],
267 nfsstats.srvrpccnt[NQNFSPROC_VACATED],
268 nfsstats.srvrpccnt[NQNFSPROC_EVICTED]);
269 printf("Server Ret-Failed\n");
270 printf("%17d\n", nfsstats.srvrpc_errs);
271 printf("Server Faults\n");
272 printf("%13d\n", nfsstats.srv_errs);
273 printf("Server Cache Stats:\n");
274 printf("%9.9s %9.9s %9.9s %9.9s\n",
275 "Inprog", "Idem", "Non-idem", "Misses");
276 printf("%9d %9d %9d %9d\n",
277 nfsstats.srvcache_inproghits,
278 nfsstats.srvcache_idemdonehits,
279 nfsstats.srvcache_nonidemdonehits,
280 nfsstats.srvcache_misses);
281 printf("Server Lease Stats:\n");
282 printf("%9.9s %9.9s %9.9s\n",
283 "Leases", "PeakL", "GLeases");
284 printf("%9d %9d %9d\n",
285 nfsstats.srvnqnfs_leases,
286 nfsstats.srvnqnfs_maxleases,
287 nfsstats.srvnqnfs_getleases);
288 printf("Server Write Gathering:\n");
289 printf("%9.9s %9.9s %9.9s\n",
290 "WriteOps", "WriteRPC", "Opsaved");
291 printf("%9d %9d %9d\n",
292 nfsstats.srvvop_writes,
293 nfsstats.srvrpccnt[NFSPROC_WRITE],
294 nfsstats.srvrpccnt[NFSPROC_WRITE] - nfsstats.srvvop_writes);
295 }
296
297 u_char signalled; /* set if alarm goes off "early" */
298
299 /*
300 * Print a running summary of nfs statistics.
301 * Repeat display every interval seconds, showing statistics
302 * collected over that interval. Assumes that interval is non-zero.
303 * First line printed at top of screen is always cumulative.
304 */
305 void
306 sidewaysintpr(interval, off)
307 u_int interval;
308 u_long off;
309 {
310 struct nfsstats nfsstats, lastst;
311 int hdrcnt, oldmask;
312 void catchalarm();
313
314 (void)signal(SIGALRM, catchalarm);
315 signalled = 0;
316 (void)alarm(interval);
317 bzero((caddr_t)&lastst, sizeof(lastst));
318
319 for (hdrcnt = 1;;) {
320 if (!--hdrcnt) {
321 printhdr();
322 hdrcnt = 20;
323 }
324 if (kvm_read(kd, off, (char *)&nfsstats, sizeof nfsstats) < 0) {
325 fprintf(stderr, "nfsstat: kvm_read failed\n");
326 exit(1);
327 }
328 printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
329 nfsstats.rpccnt[NFSPROC_GETATTR]-lastst.rpccnt[NFSPROC_GETATTR],
330 nfsstats.rpccnt[NFSPROC_LOOKUP]-lastst.rpccnt[NFSPROC_LOOKUP],
331 nfsstats.rpccnt[NFSPROC_READLINK]-lastst.rpccnt[NFSPROC_READLINK],
332 nfsstats.rpccnt[NFSPROC_READ]-lastst.rpccnt[NFSPROC_READ],
333 nfsstats.rpccnt[NFSPROC_WRITE]-lastst.rpccnt[NFSPROC_WRITE],
334 nfsstats.rpccnt[NFSPROC_RENAME]-lastst.rpccnt[NFSPROC_RENAME],
335 nfsstats.rpccnt[NFSPROC_ACCESS]-lastst.rpccnt[NFSPROC_ACCESS],
336 (nfsstats.rpccnt[NFSPROC_READDIR]-lastst.rpccnt[NFSPROC_READDIR])
337 +(nfsstats.rpccnt[NFSPROC_READDIRPLUS]-lastst.rpccnt[NFSPROC_READDIRPLUS]));
338 printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
339 nfsstats.srvrpccnt[NFSPROC_GETATTR]-lastst.srvrpccnt[NFSPROC_GETATTR],
340 nfsstats.srvrpccnt[NFSPROC_LOOKUP]-lastst.srvrpccnt[NFSPROC_LOOKUP],
341 nfsstats.srvrpccnt[NFSPROC_READLINK]-lastst.srvrpccnt[NFSPROC_READLINK],
342 nfsstats.srvrpccnt[NFSPROC_READ]-lastst.srvrpccnt[NFSPROC_READ],
343 nfsstats.srvrpccnt[NFSPROC_WRITE]-lastst.srvrpccnt[NFSPROC_WRITE],
344 nfsstats.srvrpccnt[NFSPROC_RENAME]-lastst.srvrpccnt[NFSPROC_RENAME],
345 nfsstats.srvrpccnt[NFSPROC_ACCESS]-lastst.srvrpccnt[NFSPROC_ACCESS],
346 (nfsstats.srvrpccnt[NFSPROC_READDIR]-lastst.srvrpccnt[NFSPROC_READDIR])
347 +(nfsstats.srvrpccnt[NFSPROC_READDIRPLUS]-lastst.srvrpccnt[NFSPROC_READDIRPLUS]));
348 lastst = nfsstats;
349 fflush(stdout);
350 oldmask = sigblock(sigmask(SIGALRM));
351 if (!signalled)
352 sigpause(0);
353 sigsetmask(oldmask);
354 signalled = 0;
355 (void)alarm(interval);
356 }
357 /*NOTREACHED*/
358 }
359
360 void
361 printhdr()
362 {
363 printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
364 "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
365 "Access", "Readdir");
366 fflush(stdout);
367 }
368
369 /*
370 * Called if an interval expires before sidewaysintpr has completed a loop.
371 * Sets a flag to not wait for the alarm.
372 */
373 void
374 catchalarm()
375 {
376 signalled = 1;
377 }
378
379 void
380 usage()
381 {
382 (void)fprintf(stderr,
383 "usage: nfsstat [-M core] [-N system] [-w interval]\n");
384 exit(1);
385 }
386