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