nfsstat.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1983, 1989 Regents of the University of California.
3 1.1 cgd * 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.1 cgd char copyright[] =
39 1.1 cgd "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
40 1.1 cgd All rights reserved.\n";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.1 cgd static char sccsid[] = "@(#)nfsstat.c 5.9 (Berkeley) 7/1/91";
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd #include <sys/param.h>
48 1.1 cgd #if BSD >= 199103
49 1.1 cgd #define NEWVM
50 1.1 cgd #endif
51 1.1 cgd #ifndef NEWVM
52 1.1 cgd #include <sys/vmmac.h>
53 1.1 cgd #include <machine/pte.h>
54 1.1 cgd #endif
55 1.1 cgd #include <sys/mount.h>
56 1.1 cgd #include <nfs/nfsv2.h>
57 1.1 cgd #include <nfs/nfs.h>
58 1.1 cgd #include <signal.h>
59 1.1 cgd #include <fcntl.h>
60 1.1 cgd #include <ctype.h>
61 1.1 cgd #include <errno.h>
62 1.1 cgd #include <nlist.h>
63 1.1 cgd #include <unistd.h>
64 1.1 cgd #include <stdio.h>
65 1.1 cgd #include <stdlib.h>
66 1.1 cgd #include <string.h>
67 1.1 cgd #include <paths.h>
68 1.1 cgd
69 1.1 cgd struct nlist nl[] = {
70 1.1 cgd #define N_NFSSTAT 0
71 1.1 cgd { "_nfsstats" },
72 1.1 cgd #ifndef NEWVM
73 1.1 cgd #define N_SYSMAP 1
74 1.1 cgd { "_Sysmap" },
75 1.1 cgd #define N_SYSSIZE 2
76 1.1 cgd { "_Syssize" },
77 1.1 cgd #endif
78 1.1 cgd "",
79 1.1 cgd };
80 1.1 cgd
81 1.1 cgd #ifndef NEWVM
82 1.1 cgd struct pte *Sysmap;
83 1.1 cgd #endif
84 1.1 cgd
85 1.1 cgd int kflag, kmem;
86 1.1 cgd char *kernel = _PATH_UNIX;
87 1.1 cgd char *kmemf = _PATH_KMEM;
88 1.1 cgd
89 1.1 cgd off_t klseek();
90 1.1 cgd void intpr(), printhdr(), sidewaysintpr(), usage();
91 1.1 cgd
92 1.1 cgd main(argc, argv)
93 1.1 cgd int argc;
94 1.1 cgd char **argv;
95 1.1 cgd {
96 1.1 cgd extern int optind;
97 1.1 cgd extern char *optarg;
98 1.1 cgd u_int interval;
99 1.1 cgd int ch;
100 1.1 cgd
101 1.1 cgd interval = 0;
102 1.1 cgd while ((ch = getopt(argc, argv, "M:N:w:")) != EOF)
103 1.1 cgd switch(ch) {
104 1.1 cgd case 'M':
105 1.1 cgd kmemf = optarg;
106 1.1 cgd kflag = 1;
107 1.1 cgd break;
108 1.1 cgd case 'N':
109 1.1 cgd kernel = optarg;
110 1.1 cgd break;
111 1.1 cgd case 'w':
112 1.1 cgd interval = atoi(optarg);
113 1.1 cgd break;
114 1.1 cgd case '?':
115 1.1 cgd default:
116 1.1 cgd usage();
117 1.1 cgd }
118 1.1 cgd argc -= optind;
119 1.1 cgd argv += optind;
120 1.1 cgd
121 1.1 cgd #define BACKWARD_COMPATIBILITY
122 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
123 1.1 cgd if (*argv) {
124 1.1 cgd kernel = *++argv;
125 1.1 cgd if (*++argv) {
126 1.1 cgd kmemf = *argv;
127 1.1 cgd kflag = 1;
128 1.1 cgd }
129 1.1 cgd }
130 1.1 cgd #endif
131 1.1 cgd if (nlist(kernel, nl) < 0 || nl[0].n_type == 0) {
132 1.1 cgd (void)fprintf(stderr, "nfsstate: %s: no namelist\n", kernel);
133 1.1 cgd exit(1);
134 1.1 cgd }
135 1.1 cgd kmem = open(kmemf, O_RDONLY);
136 1.1 cgd if (kmem < 0) {
137 1.1 cgd (void)fprintf(stderr,
138 1.1 cgd "nfsstat: %s: %s\n", kmemf, strerror(errno));
139 1.1 cgd exit(1);
140 1.1 cgd }
141 1.1 cgd if (kflag) {
142 1.1 cgd #ifdef NEWVM
143 1.1 cgd (void)fprintf(stderr, "nfsstat: can't do core files yet\n");
144 1.1 cgd exit(1);
145 1.1 cgd #else
146 1.1 cgd off_t off;
147 1.1 cgd
148 1.1 cgd Sysmap = (struct pte *)
149 1.1 cgd malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
150 1.1 cgd if (!Sysmap) {
151 1.1 cgd (void)fprintf(stderr, "nfsstat: %s\n", strerror(errno));
152 1.1 cgd exit(1);
153 1.1 cgd }
154 1.1 cgd off = nl[N_SYSMAP].n_value & ~KERNBASE;
155 1.1 cgd (void)lseek(kmem, off, L_SET);
156 1.1 cgd (void)read(kmem, (char *)Sysmap,
157 1.1 cgd (int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
158 1.1 cgd #endif
159 1.1 cgd }
160 1.1 cgd
161 1.1 cgd if (!nl[N_NFSSTAT].n_value) {
162 1.1 cgd (void)fprintf(stderr, "nfsstat: nfsstats symbol not defined\n");
163 1.1 cgd exit(1);
164 1.1 cgd }
165 1.1 cgd if (interval)
166 1.1 cgd sidewaysintpr(interval, nl[N_NFSSTAT].n_value);
167 1.1 cgd else
168 1.1 cgd intpr(nl[N_NFSSTAT].n_value);
169 1.1 cgd exit(0);
170 1.1 cgd }
171 1.1 cgd
172 1.1 cgd /*
173 1.1 cgd * Print a description of the network interfaces.
174 1.1 cgd */
175 1.1 cgd void
176 1.1 cgd intpr(nfsstataddr)
177 1.1 cgd off_t nfsstataddr;
178 1.1 cgd {
179 1.1 cgd struct nfsstats nfsstats;
180 1.1 cgd
181 1.1 cgd klseek(kmem, nfsstataddr, 0L);
182 1.1 cgd read(kmem, (char *)&nfsstats, sizeof(struct nfsstats));
183 1.1 cgd printf("Client Info:\n");
184 1.1 cgd printf("Rpc Counts:\n");
185 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
186 1.1 cgd "Getattr", "Setattr", "Lookup", "Readlink", "Read",
187 1.1 cgd "Write", "Create", "Remove");
188 1.1 cgd printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
189 1.1 cgd nfsstats.rpccnt[1],
190 1.1 cgd nfsstats.rpccnt[2],
191 1.1 cgd nfsstats.rpccnt[4],
192 1.1 cgd nfsstats.rpccnt[5],
193 1.1 cgd nfsstats.rpccnt[6],
194 1.1 cgd nfsstats.rpccnt[8],
195 1.1 cgd nfsstats.rpccnt[9],
196 1.1 cgd nfsstats.rpccnt[10]);
197 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
198 1.1 cgd "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
199 1.1 cgd "Readdir", "Statfs");
200 1.1 cgd printf("%9d %9d %9d %9d %9d %9d %9d\n",
201 1.1 cgd nfsstats.rpccnt[11],
202 1.1 cgd nfsstats.rpccnt[12],
203 1.1 cgd nfsstats.rpccnt[13],
204 1.1 cgd nfsstats.rpccnt[14],
205 1.1 cgd nfsstats.rpccnt[15],
206 1.1 cgd nfsstats.rpccnt[16],
207 1.1 cgd nfsstats.rpccnt[17]);
208 1.1 cgd printf("Rpc Info:\n");
209 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s %9.9s\n",
210 1.1 cgd "TimedOut", "Invalid", "X Replies", "Retries", "Requests");
211 1.1 cgd printf("%9d %9d %9d %9d %9d\n",
212 1.1 cgd nfsstats.rpctimeouts,
213 1.1 cgd nfsstats.rpcinvalid,
214 1.1 cgd nfsstats.rpcunexpected,
215 1.1 cgd nfsstats.rpcretries,
216 1.1 cgd nfsstats.rpcrequests);
217 1.1 cgd printf("Cache Info:\n");
218 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s",
219 1.1 cgd "Attr Hits", "Misses", "Lkup Hits", "Misses");
220 1.1 cgd printf(" %9.9s %9.9s %9.9s %9.9s\n",
221 1.1 cgd "BioR Hits", "Misses", "BioW Hits", "Misses");
222 1.1 cgd printf("%9d %9d %9d %9d",
223 1.1 cgd nfsstats.attrcache_hits, nfsstats.attrcache_misses,
224 1.1 cgd nfsstats.lookupcache_hits, nfsstats.lookupcache_misses);
225 1.1 cgd printf(" %9d %9d %9d %9d\n",
226 1.1 cgd nfsstats.biocache_reads-nfsstats.read_bios,
227 1.1 cgd nfsstats.read_bios,
228 1.1 cgd nfsstats.biocache_writes-nfsstats.write_bios,
229 1.1 cgd nfsstats.write_bios);
230 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s",
231 1.1 cgd "BioRLHits", "Misses", "BioD Hits", "Misses");
232 1.1 cgd printf(" %9.9s %9.9s\n", "DirE Hits", "Misses");
233 1.1 cgd printf("%9d %9d %9d %9d",
234 1.1 cgd nfsstats.biocache_readlinks-nfsstats.readlink_bios,
235 1.1 cgd nfsstats.readlink_bios,
236 1.1 cgd nfsstats.biocache_readdirs-nfsstats.readdir_bios,
237 1.1 cgd nfsstats.readdir_bios);
238 1.1 cgd printf(" %9d %9d\n",
239 1.1 cgd nfsstats.direofcache_hits, nfsstats.direofcache_misses);
240 1.1 cgd printf("\nServer Info:\n");
241 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
242 1.1 cgd "Getattr", "Setattr", "Lookup", "Readlink", "Read",
243 1.1 cgd "Write", "Create", "Remove");
244 1.1 cgd printf("%9d %9d %9d %9d %9d %9d %9d %9d\n",
245 1.1 cgd nfsstats.srvrpccnt[1],
246 1.1 cgd nfsstats.srvrpccnt[2],
247 1.1 cgd nfsstats.srvrpccnt[4],
248 1.1 cgd nfsstats.srvrpccnt[5],
249 1.1 cgd nfsstats.srvrpccnt[6],
250 1.1 cgd nfsstats.srvrpccnt[8],
251 1.1 cgd nfsstats.srvrpccnt[9],
252 1.1 cgd nfsstats.srvrpccnt[10]);
253 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s %9.9s %9.9s %9.9s\n",
254 1.1 cgd "Rename", "Link", "Symlink", "Mkdir", "Rmdir",
255 1.1 cgd "Readdir", "Statfs");
256 1.1 cgd printf("%9d %9d %9d %9d %9d %9d %9d\n",
257 1.1 cgd nfsstats.srvrpccnt[11],
258 1.1 cgd nfsstats.srvrpccnt[12],
259 1.1 cgd nfsstats.srvrpccnt[13],
260 1.1 cgd nfsstats.srvrpccnt[14],
261 1.1 cgd nfsstats.srvrpccnt[15],
262 1.1 cgd nfsstats.srvrpccnt[16],
263 1.1 cgd nfsstats.srvrpccnt[17]);
264 1.1 cgd printf("Server Ret-Failed\n");
265 1.1 cgd printf("%17d\n", nfsstats.srvrpc_errs);
266 1.1 cgd printf("Server Faults\n");
267 1.1 cgd printf("%13d\n", nfsstats.srv_errs);
268 1.1 cgd printf("Server Cache Stats:\n");
269 1.1 cgd printf("%9.9s %9.9s %9.9s %9.9s\n",
270 1.1 cgd "Inprog", "Idem", "Non-idem", "Misses");
271 1.1 cgd printf("%9d %9d %9d %9d\n",
272 1.1 cgd nfsstats.srvcache_inproghits,
273 1.1 cgd nfsstats.srvcache_idemdonehits,
274 1.1 cgd nfsstats.srvcache_nonidemdonehits,
275 1.1 cgd nfsstats.srvcache_misses);
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd u_char signalled; /* set if alarm goes off "early" */
279 1.1 cgd
280 1.1 cgd /*
281 1.1 cgd * Print a running summary of nfs statistics.
282 1.1 cgd * Repeat display every interval seconds, showing statistics
283 1.1 cgd * collected over that interval. Assumes that interval is non-zero.
284 1.1 cgd * First line printed at top of screen is always cumulative.
285 1.1 cgd */
286 1.1 cgd void
287 1.1 cgd sidewaysintpr(interval, off)
288 1.1 cgd u_int interval;
289 1.1 cgd off_t off;
290 1.1 cgd {
291 1.1 cgd struct nfsstats nfsstats, lastst;
292 1.1 cgd int hdrcnt, oldmask;
293 1.1 cgd void catchalarm();
294 1.1 cgd
295 1.1 cgd klseek(kmem, off, 0L);
296 1.1 cgd
297 1.1 cgd (void)signal(SIGALRM, catchalarm);
298 1.1 cgd signalled = 0;
299 1.1 cgd (void)alarm(interval);
300 1.1 cgd bzero((caddr_t)&lastst, sizeof(lastst));
301 1.1 cgd
302 1.1 cgd for (hdrcnt = 1;;) {
303 1.1 cgd if (!--hdrcnt) {
304 1.1 cgd printhdr();
305 1.1 cgd hdrcnt = 20;
306 1.1 cgd }
307 1.1 cgd klseek(kmem, off, 0L);
308 1.1 cgd read(kmem, (char *)&nfsstats, sizeof nfsstats);
309 1.1 cgd printf("Client: %8d %8d %8d %8d %8d %8d %8d %8d\n",
310 1.1 cgd nfsstats.rpccnt[1]-lastst.rpccnt[1],
311 1.1 cgd nfsstats.rpccnt[4]-lastst.rpccnt[4],
312 1.1 cgd nfsstats.rpccnt[5]-lastst.rpccnt[5],
313 1.1 cgd nfsstats.rpccnt[6]-lastst.rpccnt[6],
314 1.1 cgd nfsstats.rpccnt[8]-lastst.rpccnt[8],
315 1.1 cgd nfsstats.rpccnt[11]-lastst.rpccnt[11],
316 1.1 cgd nfsstats.rpccnt[12]-lastst.rpccnt[12],
317 1.1 cgd nfsstats.rpccnt[16]-lastst.rpccnt[16]);
318 1.1 cgd printf("Server: %8d %8d %8d %8d %8d %8d %8d %8d\n",
319 1.1 cgd nfsstats.srvrpccnt[1]-lastst.srvrpccnt[1],
320 1.1 cgd nfsstats.srvrpccnt[4]-lastst.srvrpccnt[4],
321 1.1 cgd nfsstats.srvrpccnt[5]-lastst.srvrpccnt[5],
322 1.1 cgd nfsstats.srvrpccnt[6]-lastst.srvrpccnt[6],
323 1.1 cgd nfsstats.srvrpccnt[8]-lastst.srvrpccnt[8],
324 1.1 cgd nfsstats.srvrpccnt[11]-lastst.srvrpccnt[11],
325 1.1 cgd nfsstats.srvrpccnt[12]-lastst.srvrpccnt[12],
326 1.1 cgd nfsstats.srvrpccnt[16]-lastst.srvrpccnt[16]);
327 1.1 cgd lastst = nfsstats;
328 1.1 cgd fflush(stdout);
329 1.1 cgd oldmask = sigblock(sigmask(SIGALRM));
330 1.1 cgd if (!signalled)
331 1.1 cgd sigpause(0);
332 1.1 cgd sigsetmask(oldmask);
333 1.1 cgd signalled = 0;
334 1.1 cgd (void)alarm(interval);
335 1.1 cgd }
336 1.1 cgd /*NOTREACHED*/
337 1.1 cgd }
338 1.1 cgd
339 1.1 cgd void
340 1.1 cgd printhdr()
341 1.1 cgd {
342 1.1 cgd printf(" %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s %8.8s\n",
343 1.1 cgd "Getattr", "Lookup", "Readlink", "Read", "Write", "Rename",
344 1.1 cgd "Link", "Readdir");
345 1.1 cgd fflush(stdout);
346 1.1 cgd }
347 1.1 cgd
348 1.1 cgd /*
349 1.1 cgd * Called if an interval expires before sidewaysintpr has completed a loop.
350 1.1 cgd * Sets a flag to not wait for the alarm.
351 1.1 cgd */
352 1.1 cgd void
353 1.1 cgd catchalarm()
354 1.1 cgd {
355 1.1 cgd signalled = 1;
356 1.1 cgd }
357 1.1 cgd
358 1.1 cgd /*
359 1.1 cgd * Seek into the kernel for a value.
360 1.1 cgd */
361 1.1 cgd off_t
362 1.1 cgd klseek(fd, base, off)
363 1.1 cgd int fd, off;
364 1.1 cgd off_t base;
365 1.1 cgd {
366 1.1 cgd #ifndef NEWVM
367 1.1 cgd if (kflag) {
368 1.1 cgd /* get kernel pte */
369 1.1 cgd base &= ~KERNBASE;
370 1.1 cgd base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
371 1.1 cgd }
372 1.1 cgd #endif
373 1.1 cgd return (lseek(fd, base, off));
374 1.1 cgd }
375 1.1 cgd
376 1.1 cgd void
377 1.1 cgd usage()
378 1.1 cgd {
379 1.1 cgd (void)fprintf(stderr,
380 1.1 cgd "usage: nfsstat [-M core] [-N system] [-w interval]\n");
381 1.1 cgd exit(1);
382 1.1 cgd }
383