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