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