1/*
2 * Copyright (c) 1983, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32/*-
33 * Copyright (c) 2004, 2008, 2009 Silicon Graphics International Corp.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions, and the following disclaimer,
41 *    without modification.
42 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
43 *    substantially similar to the "NO WARRANTY" disclaimer below
44 *    ("Disclaimer") and any redistribution must be conditioned upon
45 *    including a substantially similar Disclaimer requirement for further
46 *    binary redistribution.
47 *
48 * NO WARRANTY
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
52 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR 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,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
58 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGES.
60 */
61
62
63#ifndef lint
64static const char copyright[] =
65"@(#) Copyright (c) 1983, 1989, 1993\n\
66	The Regents of the University of California.  All rights reserved.\n";
67#endif /* not lint */
68
69#ifndef lint
70#if 0
71static char sccsid[] = "@(#)nfsstat.c	8.2 (Berkeley) 3/31/95";
72#endif
73static const char rcsid[] =
74  "$FreeBSD$";
75#endif /* not lint */
76
77#include <sys/param.h>
78#include <sys/module.h>
79#include <sys/mount.h>
80#include <sys/time.h>
81#include <sys/sysctl.h>
82#include <nfs/nfssvc.h>
83
84#include <fs/nfs/nfsproto.h>
85#include <fs/nfs/nfsport.h>
86
87#include <signal.h>
88#include <fcntl.h>
89#include <ctype.h>
90#include <errno.h>
91#include <limits.h>
92#include <nlist.h>
93#include <unistd.h>
94#include <stdio.h>
95#include <stdint.h>
96#include <stdlib.h>
97#include <string.h>
98#include <paths.h>
99#include <devstat.h>
100#include <err.h>
101
102#include <libxo/xo.h>
103
104static int widemode = 0;
105static int zflag = 0;
106static int printtitle = 1;
107static struct nfsstatsv1 ext_nfsstats;
108static int extra_output = 0;
109
110static void intpr(int, int);
111static void printhdr(int, int, int);
112static void usage(void);
113static char *sperc1(int, int);
114static char *sperc2(int, int);
115static void exp_intpr(int, int, int);
116static void exp_sidewaysintpr(u_int, int, int, int);
117static void compute_new_stats(struct nfsstatsv1 *cur_stats,
118    struct nfsstatsv1 *prev_stats, int curop, long double etime,
119    long double *mbsec, long double *kb_per_transfer,
120    long double *transfers_per_second, long double *ms_per_transfer,
121    uint64_t *queue_len, long double *busy_pct);
122
123#define DELTA(field)	(nfsstats.field - lastst.field)
124
125#define	STAT_TYPE_READ		0
126#define	STAT_TYPE_WRITE		1
127#define	STAT_TYPE_COMMIT	2
128#define	NUM_STAT_TYPES		3
129
130struct stattypes {
131	int stat_type;
132	int nfs_type;
133};
134static struct stattypes statstruct[] = {
135	{STAT_TYPE_READ, NFSV4OP_READ},
136	{STAT_TYPE_WRITE, NFSV4OP_WRITE},
137	{STAT_TYPE_COMMIT, NFSV4OP_COMMIT}
138};
139
140#define	STAT_TYPE_TO_NFS(stat_type)	statstruct[stat_type].nfs_type
141
142#define	NFSSTAT_XO_VERSION	"1"
143
144int
145main(int argc, char **argv)
146{
147	u_int interval;
148	int clientOnly = -1;
149	int serverOnly = -1;
150	int newStats = 0;
151	int ch;
152	int mntlen, i;
153	char buf[1024];
154	struct statfs *mntbuf;
155	struct nfscl_dumpmntopts dumpmntopts;
156
157	interval = 0;
158
159	argc = xo_parse_args(argc, argv);
160	if (argc < 0)
161		exit(1);
162
163	xo_set_version(NFSSTAT_XO_VERSION);
164
165	while ((ch = getopt(argc, argv, "cdEesWM:mN:w:zq")) != -1)
166		switch(ch) {
167		case 'M':
168			printf("*** -M option deprecated, ignored\n\n");
169			break;
170		case 'm':
171			/* Display mount options for NFS mount points. */
172			mntlen = getmntinfo(&mntbuf, MNT_NOWAIT);
173			for (i = 0; i < mntlen; i++) {
174				if (strcmp(mntbuf->f_fstypename, "nfs") == 0) {
175					dumpmntopts.ndmnt_fname =
176					    mntbuf->f_mntonname;
177					dumpmntopts.ndmnt_buf = buf;
178					dumpmntopts.ndmnt_blen = sizeof(buf);
179					if (nfssvc(NFSSVC_DUMPMNTOPTS,
180					    &dumpmntopts) >= 0)
181						printf("%s on %s\n%s\n",
182						    mntbuf->f_mntfromname,
183						    mntbuf->f_mntonname, buf);
184					else if (errno == EPERM)
185						errx(1, "Only priviledged users"
186						    " can use the -m option");
187				}
188				mntbuf++;
189			}
190			exit(0);
191		case 'N':
192			printf("*** -N option deprecated, ignored\n\n");
193			break;
194		case 'W':
195			widemode = 1;
196			break;
197		case 'w':
198			interval = atoi(optarg);
199			break;
200		case 'c':
201			clientOnly = 1;
202			if (serverOnly < 0)
203				serverOnly = 0;
204			break;
205		case 'd':
206			newStats = 1;
207			if (interval == 0)
208				interval = 1;
209			break;
210		case 's':
211			serverOnly = 1;
212			if (clientOnly < 0)
213				clientOnly = 0;
214			break;
215		case 'z':
216			zflag = 1;
217			break;
218		case 'E':
219			if (extra_output != 0)
220				xo_err(1, "-e and -E are mutually exclusive");
221			extra_output = 2;
222			break;
223		case 'e':
224			if (extra_output != 0)
225				xo_err(1, "-e and -E are mutually exclusive");
226			extra_output = 1;
227			break;
228		case 'q':
229			printtitle = 0;
230			break;
231		case '?':
232		default:
233			usage();
234		}
235	argc -= optind;
236	argv += optind;
237
238#define	BACKWARD_COMPATIBILITY
239#ifdef	BACKWARD_COMPATIBILITY
240	if (*argv)
241		interval = atoi(*argv);
242#endif
243	if (modfind("nfscommon") < 0)
244		xo_err(1, "NFS client/server not loaded");
245
246	if (interval) {
247		exp_sidewaysintpr(interval, clientOnly, serverOnly,
248		    newStats);
249	} else {
250		xo_open_container("nfsstat");
251		if (extra_output != 0)
252			exp_intpr(clientOnly, serverOnly, extra_output - 1);
253		else
254			intpr(clientOnly, serverOnly);
255		xo_close_container("nfsstat");
256	}
257
258	xo_finish();
259	exit(0);
260}
261
262/*
263 * Print a description of the nfs stats.
264 */
265static void
266intpr(int clientOnly, int serverOnly)
267{
268	int nfssvc_flag;
269
270	nfssvc_flag = NFSSVC_GETSTATS | NFSSVC_NEWSTRUCT;
271	if (zflag != 0) {
272		if (clientOnly != 0)
273			nfssvc_flag |= NFSSVC_ZEROCLTSTATS;
274		if (serverOnly != 0)
275			nfssvc_flag |= NFSSVC_ZEROSRVSTATS;
276	}
277	ext_nfsstats.vers = NFSSTATS_V1;
278	if (nfssvc(nfssvc_flag, &ext_nfsstats) < 0)
279		xo_err(1, "Can't get stats");
280	if (clientOnly) {
281		xo_open_container("clientstats");
282
283		if (printtitle)
284			xo_emit("{T:Client Info:\n");
285
286		xo_open_container("operations");
287		xo_emit("{T:Rpc Counts:}\n");
288
289		xo_emit("{T:Getattr/%13.13s}{T:Setattr/%13.13s}"
290		    "{T:Lookup/%13.13s}{T:Readlink/%13.13s}"
291		    "{T:Read/%13.13s}{T:Write/%13.13s}"
292		  "{T:Create/%13.13s}{T:Remove/%13.13s}\n");
293		xo_emit("{:getattr/%13ju}{:setattr/%13ju}"
294		    "{:lookup/%13ju}{:readlink/%13ju}"
295		    "{:read/%13ju}{:write/%13ju}"
296		    "{:create/%13ju}{:remove/%13ju}\n",
297			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_GETATTR],
298			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SETATTR],
299			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LOOKUP],
300			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READLINK],
301			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READ],
302			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_WRITE],
303			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_CREATE],
304			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_REMOVE]);
305
306		xo_emit("{T:Rename/%13.13s}{T:Link/%13.13s}"
307		    "{T:Symlink/%13.13s}{T:Mkdir/%13.13s}"
308		    "{T:Rmdir/%13.13s}{T:Readdir/%13.13s}"
309		  "{T:RdirPlus/%13.13s}{T:Access/%13.13s}\n");
310		xo_emit("{:rename/%13ju}{:link/%13ju}"
311		    "{:symlink/%13ju}{:mkdir/%13ju}"
312		    "{:rmdir/%13ju}{:readdir/%13ju}"
313		    "{:rdirplus/%13ju}{:access/%13ju}\n",
314		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_RENAME],
315		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LINK],
316		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SYMLINK],
317		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_MKDIR],
318		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_RMDIR],
319		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READDIR],
320		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READDIRPLUS],
321		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_ACCESS]);
322
323		xo_emit("{T:Mknod/%13.13s}{T:Fsstat/%13.13s}"
324		    "{T:Fsinfo/%13.13s}{T:PathConf/%13.13s}"
325		    "{T:Commit/%13.13s}\n");
326		xo_emit("{:mknod/%13ju}{:fsstat/%13ju}"
327		    "{:fsinfo/%13ju}{:pathconf/%13ju}"
328		    "{:commit/%13ju}\n",
329			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_MKNOD],
330			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_FSSTAT],
331			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_FSINFO],
332			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_PATHCONF],
333			(uintmax_t)ext_nfsstats.rpccnt[NFSPROC_COMMIT]);
334
335		xo_close_container("operations");
336
337		xo_open_container("rpcs");
338		xo_emit("{T:Rpc Info:}\n");
339
340		xo_emit("{T:TimedOut/%13.13s}{T:Invalid/%13.13s}"
341		    "{T:X Replies/%13.13s}{T:Retries/%13.13s}"
342		    "{T:Requests/%13.13s}\n");
343		xo_emit("{:timedout/%13ju}{:invalid/%13ju}"
344		    "{:xreplies/%13ju}{:retries/%13ju}"
345		    "{:requests/%13ju}\n",
346			(uintmax_t)ext_nfsstats.rpctimeouts,
347			(uintmax_t)ext_nfsstats.rpcinvalid,
348			(uintmax_t)ext_nfsstats.rpcunexpected,
349			(uintmax_t)ext_nfsstats.rpcretries,
350			(uintmax_t)ext_nfsstats.rpcrequests);
351		xo_close_container("rpcs");
352
353		xo_open_container("cache");
354		xo_emit("{T:Cache Info:}\n");
355
356		xo_emit("{T:Attr Hits/%13.13s}{T:Attr Misses/%13.13s}"
357		    "{T:Lkup Hits/%13.13s}{T:Lkup Misses/%13.13s}"
358		    "{T:BioR Hits/%13.13s}{T:BioR Misses/%13.13s}"
359		    "{T:BioW Hits/%13.13s}{T:BioW Misses/%13.13s}\n");
360		xo_emit("{:attrhits/%13ju}{:attrmisses/%13ju}"
361		    "{:lkuphits/%13ju}{:lkupmisses/%13ju}"
362		    "{:biorhits/%13ju}{:biormisses/%13ju}"
363		    "{:biowhits/%13ju}{:biowmisses/%13ju}\n",
364		    (uintmax_t)ext_nfsstats.attrcache_hits,
365		    (uintmax_t)ext_nfsstats.attrcache_misses,
366		    (uintmax_t)ext_nfsstats.lookupcache_hits,
367		    (uintmax_t)ext_nfsstats.lookupcache_misses,
368		    (uintmax_t)(ext_nfsstats.biocache_reads -
369		    ext_nfsstats.read_bios),
370		    (uintmax_t)ext_nfsstats.read_bios,
371		    (uintmax_t)(ext_nfsstats.biocache_writes -
372		    ext_nfsstats.write_bios),
373		    (uintmax_t)ext_nfsstats.write_bios);
374
375		xo_emit("{T:BioRL Hits/%13.13s}{T:BioRL Misses/%13.13s}"
376		    "{T:BioD Hits/%13.13s}{T:BioD Misses/%13.13s}"
377		    "{T:DirE Hits/%13.13s}{T:DirE Misses/%13.13s}"
378		    "{T:Accs Hits/%13.13s}{T:Accs Misses/%13.13s}\n");
379		xo_emit("{:biosrlhits/%13ju}{:biorlmisses/%13ju}"
380		    "{:biodhits/%13ju}{:biodmisses/%13ju}"
381		    "{:direhits/%13ju}{:diremisses/%13ju}"
382		    "{:accshits/%13ju}{:accsmisses/%13ju}\n",
383		    (uintmax_t)(ext_nfsstats.biocache_readlinks -
384		    ext_nfsstats.readlink_bios),
385		    (uintmax_t)ext_nfsstats.readlink_bios,
386		    (uintmax_t)(ext_nfsstats.biocache_readdirs -
387		    ext_nfsstats.readdir_bios),
388		    (uintmax_t)ext_nfsstats.readdir_bios,
389		    (uintmax_t)ext_nfsstats.direofcache_hits,
390		    (uintmax_t)ext_nfsstats.direofcache_misses,
391		    (uintmax_t)ext_nfsstats.accesscache_hits,
392		    (uintmax_t)ext_nfsstats.accesscache_misses);
393
394		xo_close_container("cache");
395
396		xo_close_container("clientstats");
397	}
398	if (serverOnly) {
399		xo_open_container("serverstats");
400
401		xo_emit("{T:Server Info:}\n");
402		xo_open_container("operations");
403
404		xo_emit("{T:Getattr/%13.13s}{T:Setattr/%13.13s}"
405		    "{T:Lookup/%13.13s}{T:Readlink/%13.13s}"
406		    "{T:Read/%13.13s}{T:Write/%13.13s}"
407		    "{T:Create/%13.13s}{T:Remove/%13.13s}\n");
408		xo_emit("{:getattr/%13ju}{:setattr/%13ju}"
409		    "{:lookup/%13ju}{:readlink/%13ju}"
410		    "{:read/%13ju}{:write/%13ju}"
411		    "{:create/%13ju}{:remove/%13ju}\n",
412		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_GETATTR],
413		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SETATTR],
414		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LOOKUP],
415		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READLINK],
416		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READ],
417		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_WRITE],
418		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_CREATE],
419		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_REMOVE]);
420
421		xo_emit("{T:Rename/%13.13s}{T:Link/%13.13s}"
422		    "{T:Symlink/%13.13s}{T:Mkdir/%13.13s}"
423		    "{T:Rmdir/%13.13s}{T:Readdir/%13.13s}"
424		    "{T:RdirPlus/%13.13s}{T:Access/%13.13s}\n");
425		xo_emit("{:rename/%13ju}{:link/%13ju}"
426		    "{:symlink/%13ju}{:mkdir/%13ju}"
427		    "{:rmdir/%13ju}{:readdir/%13ju}"
428		    "{:rdirplus/%13ju}{:access/%13ju}\n",
429		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RENAME],
430		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LINK],
431		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SYMLINK],
432		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_MKDIR],
433		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RMDIR],
434		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READDIR],
435		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READDIRPLUS],
436		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_ACCESS]);
437
438		xo_emit("{T:Mknod/%13.13s}{T:Fsstat/%13.13s}"
439		    "{T:Fsinfo/%13.13s}{T:PathConf/%13.13s}"
440		    "{T:Commit/%13.13s}\n");
441		xo_emit("{:mknod/%13ju}{:fsstat/%13ju}"
442		    "{:fsinfo/%13ju}{:pathconf/%13ju}"
443		    "{:commit/%13ju}\n",
444		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_MKNOD],
445		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_FSSTAT],
446		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_FSINFO],
447		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_PATHCONF],
448		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_COMMIT]);
449
450		xo_close_container("operations");
451
452		xo_open_container("server");
453
454		xo_emit("{T:Server Write Gathering:/%13.13s}\n");
455
456		xo_emit("{T:WriteOps/%13.13s}{T:WriteRPC/%13.13s}"
457		    "{T:Opsaved/%13.13s}\n");
458		xo_emit("{:writeops/%13ju}{:writerpc/%13ju}"
459		    "{:opsaved/%13ju}\n",
460		/*
461		 * The new client doesn't do write gathering. It was
462		 * only useful for NFSv2.
463		 */
464		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_WRITE],
465		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_WRITE], 0);
466
467		xo_close_container("server");
468
469		xo_open_container("cache");
470		xo_emit("{T:Server Cache Stats:/%13.13s}\n");
471		xo_emit("{T:Inprog/%13.13s}"
472		    "{T:Non-Idem/%13.13s}{T:Misses/%13.13s}\n");
473		xo_emit("{:inprog/%13ju}{:nonidem/%13ju}{:misses/%13ju}\n",
474			(uintmax_t)ext_nfsstats.srvcache_inproghits,
475			(uintmax_t)ext_nfsstats.srvcache_nonidemdonehits,
476			(uintmax_t)ext_nfsstats.srvcache_misses);
477		xo_close_container("cache");
478
479		xo_close_container("serverstats");
480	}
481}
482
483static void
484printhdr(int clientOnly, int serverOnly, int newStats)
485{
486
487	if (newStats) {
488		printf(" [%s Read %s]  [%s Write %s]  "
489		    "%s[=========== Total ============]\n"
490		    " KB/t   tps    MB/s%s  KB/t   tps    MB/s%s  "
491		    "%sKB/t   tps    MB/s    ms  ql  %%b",
492		    widemode ? "========" : "=====",
493		    widemode ? "========" : "=====",
494		    widemode ? "========" : "=====",
495		    widemode ? "======="  : "====",
496		    widemode ? "[Commit ]  " : "",
497		    widemode ? "    ms" : "",
498		    widemode ? "    ms" : "",
499		    widemode ? "tps    ms  " : "");
500	} else {
501		printf("%s%6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s %6.6s",
502		    ((serverOnly && clientOnly) ? "        " : " "),
503		    "GtAttr", "Lookup", "Rdlink", "Read", "Write", "Rename",
504		    "Access", "Rddir");
505		if (widemode && clientOnly) {
506			printf(" Attr Lkup BioR BioW Accs BioD");
507		}
508	}
509	printf("\n");
510	fflush(stdout);
511}
512
513static void
514usage(void)
515{
516	(void)fprintf(stderr,
517	    "usage: nfsstat [-cdemszW] [-w wait]\n");
518	exit(1);
519}
520
521static char SPBuf[64][8];
522static int SPIndex;
523
524static char *
525sperc1(int hits, int misses)
526{
527	char *p = SPBuf[SPIndex];
528
529	if (hits + misses) {
530		sprintf(p, "%3d%%",
531		    (int)(char)((quad_t)hits * 100 / (hits + misses)));
532	} else {
533		sprintf(p, "   -");
534	}
535	SPIndex = (SPIndex + 1) & 63;
536	return(p);
537}
538
539static char *
540sperc2(int ttl, int misses)
541{
542	char *p = SPBuf[SPIndex];
543
544	if (ttl) {
545		sprintf(p, "%3d%%",
546		    (int)(char)((quad_t)(ttl - misses) * 100 / ttl));
547	} else {
548		sprintf(p, "   -");
549	}
550	SPIndex = (SPIndex + 1) & 63;
551	return(p);
552}
553
554#define DELTA_T(field)					\
555	devstat_compute_etime(&cur_stats->field,	\
556	(prev_stats ? &prev_stats->field : NULL))
557
558/*
559 * XXX KDM mostly copied from ctlstat.  We should commonize the code (and
560 * the devstat code) somehow.
561 */
562static void
563compute_new_stats(struct nfsstatsv1 *cur_stats,
564		  struct nfsstatsv1 *prev_stats, int curop,
565		  long double etime, long double *mbsec,
566		  long double *kb_per_transfer,
567		  long double *transfers_per_second,
568		  long double *ms_per_transfer, uint64_t *queue_len,
569		  long double *busy_pct)
570{
571	uint64_t total_bytes = 0, total_operations = 0;
572	struct bintime total_time_bt;
573	struct timespec total_time_ts;
574
575	bzero(&total_time_bt, sizeof(total_time_bt));
576	bzero(&total_time_ts, sizeof(total_time_ts));
577
578	total_bytes = cur_stats->srvbytes[curop];
579	total_operations = cur_stats->srvops[curop];
580	if (prev_stats != NULL) {
581		total_bytes -= prev_stats->srvbytes[curop];
582		total_operations -= prev_stats->srvops[curop];
583	}
584
585	*mbsec = total_bytes;
586	*mbsec /= 1024 * 1024;
587	if (etime > 0.0) {
588		*busy_pct = DELTA_T(busytime);
589		if (*busy_pct < 0)
590			*busy_pct = 0;
591		*busy_pct /= etime;
592		*busy_pct *= 100;
593		if (*busy_pct < 0)
594			*busy_pct = 0;
595		*mbsec /= etime;
596	} else {
597		*busy_pct = 0;
598		*mbsec = 0;
599	}
600	*kb_per_transfer = total_bytes;
601	*kb_per_transfer /= 1024;
602	if (total_operations > 0)
603		*kb_per_transfer /= total_operations;
604	else
605		*kb_per_transfer = 0;
606	if (etime > 0.0) {
607		*transfers_per_second = total_operations;
608		*transfers_per_second /= etime;
609	} else {
610		*transfers_per_second = 0.0;
611	}
612
613	if (total_operations > 0) {
614		*ms_per_transfer = DELTA_T(srvduration[curop]);
615		*ms_per_transfer /= total_operations;
616		*ms_per_transfer *= 1000;
617	} else
618		*ms_per_transfer = 0.0;
619
620	*queue_len = cur_stats->srvstartcnt - cur_stats->srvdonecnt;
621}
622
623/*
624 * Print a description of the nfs stats for the client/server,
625 * including NFSv4.1.
626 */
627static void
628exp_intpr(int clientOnly, int serverOnly, int nfs41)
629{
630	int nfssvc_flag;
631
632	xo_open_container("nfsv4");
633
634	nfssvc_flag = NFSSVC_GETSTATS | NFSSVC_NEWSTRUCT;
635	if (zflag != 0) {
636		if (clientOnly != 0)
637			nfssvc_flag |= NFSSVC_ZEROCLTSTATS;
638		if (serverOnly != 0)
639			nfssvc_flag |= NFSSVC_ZEROSRVSTATS;
640	}
641	ext_nfsstats.vers = NFSSTATS_V1;
642	if (nfssvc(nfssvc_flag, &ext_nfsstats) < 0)
643		xo_err(1, "Can't get stats");
644	if (clientOnly != 0) {
645		xo_open_container("clientstats");
646
647		xo_open_container("operations");
648		if (printtitle) {
649			xo_emit("{T:Client Info:}\n");
650			xo_emit("{T:RPC Counts:}\n");
651		}
652		xo_emit("{T:Getattr/%13.13s}{T:Setattr/%13.13s}"
653		    "{T:Lookup/%13.13s}{T:Readlink/%13.13s}"
654		    "{T:Read/%13.13s}{T:Write/%13.13s}\n");
655		xo_emit("{:getattr/%13ju}{:setattr/%13ju}{:lookup/%13ju}"
656		    "{:readlink/%13ju}{:read/%13ju}{:write/%13ju}\n",
657		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_GETATTR],
658		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SETATTR],
659		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LOOKUP],
660		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READLINK],
661		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READ],
662		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_WRITE]);
663		xo_emit("{T:Create/%13.13s}{T:Remove/%13.13s}"
664		    "{T:Rename/%13.13s}{T:Link/%13.13s}"
665		    "{T:Symlink/%13.13s}{T:Mkdir/%13.13s}\n");
666		xo_emit("{:create/%13ju}{:remove/%13ju}{:rename/%13ju}"
667		  "{:link/%13ju}{:symlink/%13ju}{:mkdir/%13ju}\n",
668		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_CREATE],
669		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_REMOVE],
670		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_RENAME],
671		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LINK],
672		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SYMLINK],
673		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_MKDIR]);
674		xo_emit("{T:Rmdir/%13.13s}{T:Readdir/%13.13s}"
675		    "{T:RdirPlus/%13.13s}{T:Access/%13.13s}"
676		    "{T:Mknod/%13.13s}{T:Fsstat/%13.13s}\n");
677		xo_emit("{:rmdir/%13ju}{:readdir/%13ju}{:rdirplus/%13ju}"
678		    "{:access/%13ju}{:mknod/%13ju}{:fsstat/%13ju}\n",
679		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_RMDIR],
680		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READDIR],
681		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READDIRPLUS],
682		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_ACCESS],
683		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_MKNOD],
684		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_FSSTAT]);
685		xo_emit("{T:FSinfo/%13.13s}{T:pathConf/%13.13s}"
686		    "{T:Commit/%13.13s}{T:SetClId/%13.13s}"
687		    "{T:SetClIdCf/%13.13s}{T:Lock/%13.13s}\n");
688		xo_emit("{:fsinfo/%13ju}{:pathconf/%13ju}{:commit/%13ju}"
689		    "{:setclientid/%13ju}{:setclientidcf/%13ju}{:lock/%13ju}\n",
690		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_FSINFO],
691		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_PATHCONF],
692		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_COMMIT],
693		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SETCLIENTID],
694		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SETCLIENTIDCFRM],
695		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LOCK]);
696		xo_emit("{T:LockT/%13.13s}{T:LockU/%13.13s}"
697		    "{T:Open/%13.13s}{T:OpenCfr/%13.13s}\n");
698		xo_emit("{:lockt/%13ju}{:locku/%13ju}"
699		    "{:open/%13ju}{:opencfr/%13ju}\n",
700		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LOCKT],
701		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LOCKU],
702		    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_OPEN],
703		  (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_OPENCONFIRM]);
704
705		if (nfs41) {
706			xo_open_container("nfsv41");
707
708			xo_emit("{T:OpenDownGr/%13.13s}{T:Close/%13.13s}\n");
709			xo_emit("{:opendowngr/%13ju}{:close/%13ju}\n",
710			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_OPENDOWNGRADE],
711			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_CLOSE]);
712
713			xo_emit("{T:RelLckOwn/%13.13s}{T:FreeStateID/%13.13s}"
714			    "{T:PutRootFH/%13.13s}{T:DelegRet/%13.13s}"
715			    "{T:GetAcl/%13.13s}{T:SetAcl/%13.13s}\n");
716			xo_emit("{:rellckown/%13ju}{:freestateid/%13ju}"
717			    "{:putrootfh/%13ju}{:delegret/%13ju}"
718			    "{:getacl/%13ju}{:setacl/%13ju}\n",
719			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_RELEASELCKOWN],
720			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_FREESTATEID],
721			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_PUTROOTFH],
722			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_DELEGRETURN],
723			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_GETACL],
724			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SETACL]);
725
726			xo_emit("{T:ExchangeId/%13.13s}{T:CreateSess/%13.13s}"
727			    "{T:DestroySess/%13.13s}{T:DestroyClId/%13.13s}"
728			    "{T:LayoutGet/%13.13s}{T:GetDevInfo/%13.13s}\n");
729			xo_emit("{:exchangeid/%13ju}{:createsess/%13ju}"
730			    "{:destroysess/%13ju}{:destroyclid/%13ju}"
731			    "{:layoutget/%13ju}{:getdevinfo/%13ju}\n",
732			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_EXCHANGEID],
733			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_CREATESESSION],
734			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_DESTROYSESSION],
735			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_DESTROYCLIENT],
736			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LAYOUTGET],
737			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_GETDEVICEINFO]);
738
739			xo_emit("{T:LayoutCommit/%13.13s}{T:LayoutReturn/%13.13s}"
740			    "{T:ReclaimCompl/%13.13s}{T:ReadDataS/%13.13s}"
741			    "{T:WriteDataS/%13.13s}{T:CommitDataS/%13.13s}\n");
742			xo_emit("{:layoutcomit/%13ju}{:layoutreturn/%13ju}"
743			    "{:reclaimcompl/%13ju}{:readdatas/%13ju}"
744			    "{:writedatas/%13ju}{:commitdatas/%13ju}\n",
745			  (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LAYOUTCOMMIT],
746			  (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LAYOUTRETURN],
747			  (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_RECLAIMCOMPL],
748			  (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_READDS],
749			  (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_WRITEDS],
750			  (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_COMMITDS]);
751
752			xo_emit("{T:OpenLayout/%13.13s}{T:CreateLayout/%13.13s}\n");
753			xo_emit("{:openlayout/%13ju}{:createlayout/%13ju}\n",
754			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_OPENLAYGET],
755			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_CREATELAYGET]);
756
757			xo_close_container("nfsv41");
758
759			xo_open_container("nfsv42");
760
761			xo_emit("{T:IOAdvise/%13.13s}{T:Allocate/%13.13s}"
762			    "{T:Copy/%13.13s}{T:Seek/%13.13s}"
763			    "{T:SeekDataS/%13.13s}{T:GetExtattr/%13.13s}\n");
764			xo_emit("{:ioadvise/%13ju}{:allocate/%13ju}"
765			    "{:copy/%13ju}{:seek/%13ju}"
766			    "{:seekdatas/%13ju}{:getextattr/%13ju}\n",
767			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_IOADVISE],
768			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_ALLOCATE],
769			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_COPY],
770			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SEEK],
771			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SEEKDS],
772			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_GETEXTATTR]);
773
774			xo_emit("{T:SetExtattr/%13.13s}{T:RmExtattr/%13.13s}"
775			    "{T:ListExtattr/%13.13s}\n");
776			xo_emit("{:setextattr/%13ju}{:rmextattr/%13ju}"
777			    "{:listextattr/%13ju}\n",
778			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_SETEXTATTR],
779			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_RMEXTATTR],
780			    (uintmax_t)ext_nfsstats.rpccnt[NFSPROC_LISTEXTATTR]);
781
782			xo_close_container("nfsv42");
783		}
784		xo_close_container("operations");
785
786		xo_open_container("client");
787		xo_emit("{T:OpenOwner/%13.13s}{T:Opens/%13.13s}"
788		    "{T:LockOwner/%13.13s}{T:Locks/%13.13s}"
789		    "{T:Delegs/%13.13s}{T:LocalOwn/%13.13s}\n");
790		xo_emit("{:openowner/%13ju}{:opens/%13ju}"
791		    "{:lockowner/%13ju}{:locks/%13ju}"
792		    "{:delegs/%13ju}{:localown/%13ju}\n",
793		    (uintmax_t)ext_nfsstats.clopenowners,
794		    (uintmax_t)ext_nfsstats.clopens,
795		    (uintmax_t)ext_nfsstats.cllockowners,
796		    (uintmax_t)ext_nfsstats.cllocks,
797		    (uintmax_t)ext_nfsstats.cldelegates,
798		    (uintmax_t)ext_nfsstats.cllocalopenowners);
799
800		xo_emit("{T:LocalOpen/%13.13s}{T:LocalLown/%13.13s}"
801		    "{T:LocalLock/%13.13s}\n");
802		xo_emit("{:localopen/%13ju}{:locallown/%13ju}"
803		    "{:locallock/%13ju}\n",
804		    (uintmax_t)ext_nfsstats.cllocalopens,
805		    (uintmax_t)ext_nfsstats.cllocallockowners,
806		    (uintmax_t)ext_nfsstats.cllocallocks);
807		xo_close_container("client");
808
809		xo_open_container("rpc");
810		if (printtitle)
811			xo_emit("{T:Rpc Info:}\n");
812		xo_emit("{T:TimedOut/%13.13s}{T:Invalid/%13.13s}"
813		    "{T:X Replies/%13.13s}{T:Retries/%13.13s}"
814		    "{T:Requests/%13.13s}\n");
815		xo_emit("{:timedout/%13ju}{:invalid/%13ju}"
816		    "{:xreplies/%13ju}{:retries/%13ju}"
817		    "{:requests/%13ju}\n",
818		    (uintmax_t)ext_nfsstats.rpctimeouts,
819		    (uintmax_t)ext_nfsstats.rpcinvalid,
820		    (uintmax_t)ext_nfsstats.rpcunexpected,
821		    (uintmax_t)ext_nfsstats.rpcretries,
822		    (uintmax_t)ext_nfsstats.rpcrequests);
823		xo_close_container("rpc");
824
825		xo_open_container("cache");
826		if (printtitle)
827			xo_emit("{T:Cache Info:}\n");
828		xo_emit("{T:Attr Hits/%13.13s}{T:Attr Misses/%13.13s}"
829		    "{T:Lkup Hits/%13.13s}{T:Lkup Misses/%13.13s}\n");
830		xo_emit("{:attrhits/%13ju}{:attrmisses/%13ju}"
831		    "{:lkuphits/%13ju}{:lkupmisses/%13ju}\n",
832		    (uintmax_t)ext_nfsstats.attrcache_hits,
833		    (uintmax_t)ext_nfsstats.attrcache_misses,
834		    (uintmax_t)ext_nfsstats.lookupcache_hits,
835		    (uintmax_t)ext_nfsstats.lookupcache_misses);
836
837		xo_emit("{T:BioR Hits/%13.13s}{T:BioR Misses/%13.13s}"
838		    "{T:BioW Hits/%13.13s}{T:BioW Misses/%13.13s}\n");
839		xo_emit("{:biorhits/%13ju}{:biormisses/%13ju}"
840		    "{:biowhits/%13ju}{:biowmisses/%13ju}\n",
841		    (uintmax_t)(ext_nfsstats.biocache_reads -
842		    ext_nfsstats.read_bios),
843		    (uintmax_t)ext_nfsstats.read_bios,
844		    (uintmax_t)(ext_nfsstats.biocache_writes -
845		    ext_nfsstats.write_bios),
846		    (uintmax_t)ext_nfsstats.write_bios);
847
848		xo_emit("{T:BioRL Hits/%13.13s}{T:BioRL Misses/%13.13s}"
849		    "{T:BioD Hits/%13.13s}{T:BioD Misses/%13.13s}\n");
850		xo_emit("{:biorlhits/%13ju}{:biorlmisses/%13ju}"
851		    "{:biodhits/%13ju}{:biodmisses/%13ju}\n",
852		    (uintmax_t)(ext_nfsstats.biocache_readlinks -
853		    ext_nfsstats.readlink_bios),
854		    (uintmax_t)ext_nfsstats.readlink_bios,
855		    (uintmax_t)(ext_nfsstats.biocache_readdirs -
856		    ext_nfsstats.readdir_bios),
857		    (uintmax_t)ext_nfsstats.readdir_bios);
858
859		xo_emit("{T:DirE Hits/%13.13s}{T:DirE Misses/%13.13s}\n");
860		xo_emit("{:direhits/%13ju}{:diremisses/%13ju}\n",
861		    (uintmax_t)ext_nfsstats.direofcache_hits,
862		    (uintmax_t)ext_nfsstats.direofcache_misses);
863		xo_open_container("cache");
864
865		xo_close_container("clientstats");
866	}
867	if (serverOnly != 0) {
868		xo_open_container("serverstats");
869
870		xo_open_container("operations");
871		if (printtitle)
872			xo_emit("{T:Server Info:}\n");
873		xo_emit("{T:Getattr/%13.13s}{T:Setattr/%13.13s}"
874		    "{T:Lookup/%13.13s}{T:Readlink/%13.13s}"
875		    "{T:Read/%13.13s}{T:Write/%13.13s}\n");
876		xo_emit("{:getattr/%13ju}{:setattr/%13ju}{:lookup/%13ju}"
877		    "{:readlink/%13ju}{:read/%13ju}{:write/%13ju}\n",
878		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_GETATTR],
879		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SETATTR],
880		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LOOKUP],
881		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READLINK],
882		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READ],
883		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_WRITE]);
884		xo_emit("{T:Create/%13.13s}{T:Remove/%13.13s}"
885		    "{T:Rename/%13.13s}{T:Link/%13.13s}"
886		    "{T:Symlink/%13.13s}{T:Mkdir/%13.13s}\n");
887		xo_emit("{:create/%13ju}{:remove/%13ju}{:rename/%13ju}"
888		    "{:link/%13ju}{:symlink/%13ju}{:mkdir/%13ju}\n",
889		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_V3CREATE],
890		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_REMOVE],
891		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RENAME],
892		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LINK],
893		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SYMLINK],
894		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_MKDIR]);
895		xo_emit("{T:Rmdir/%13.13s}{T:Readdir/%13.13s}"
896		    "{T:RdirPlus/%13.13s}{T:Access/%13.13s}"
897		    "{T:Mknod/%13.13s}{T:Fsstat/%13.13s}\n");
898		xo_emit("{:rmdir/%13ju}{:readdir/%13ju}{:rdirplus/%13ju}"
899		    "{:access/%13ju}{:mknod/%13ju}{:fsstat/%13ju}\n",
900		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RMDIR],
901		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READDIR],
902		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READDIRPLUS],
903		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_ACCESS],
904		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_MKNOD],
905		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_FSSTAT]);
906		xo_emit("{T:FSinfo/%13.13s}{T:pathConf/%13.13s}"
907		    "{T:Commit/%13.13s}{T:LookupP/%13.13s}"
908		    "{T:SetClId/%13.13s}{T:SetClIdCf/%13.13s}\n");
909		xo_emit("{:fsinfo/%13ju}{:pathconf/%13ju}{:commit/%13ju}"
910		    "{:lookupp/%13ju}{:setclientid/%13ju}{:setclientidcfrm/%13ju}\n",
911		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_FSINFO],
912		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_PATHCONF],
913		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_COMMIT],
914		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LOOKUPP],
915		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SETCLIENTID],
916		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SETCLIENTIDCFRM]);
917		xo_emit("{T:Open/%13.13s}{T:OpenAttr/%13.13s}"
918		    "{T:OpenDwnGr/%13.13s}{T:OpenCfrm/%13.13s}"
919		    "{T:DelePurge/%13.13s}{T:DelRet/%13.13s}\n");
920		xo_emit("{:open/%13ju}{:openattr/%13ju}{:opendwgr/%13ju}"
921		    "{:opencfrm/%13ju}{:delepurge/%13ju}{:delreg/%13ju}\n",
922		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_OPEN],
923		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_OPENATTR],
924		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_OPENDOWNGRADE],
925		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_OPENCONFIRM],
926		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_DELEGPURGE],
927		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_DELEGRETURN]);
928		xo_emit("{T:GetFH/%13.13s}{T:Lock/%13.13s}"
929		    "{T:LockT/%13.13s}{T:LockU/%13.13s}"
930		    "{T:Close/%13.13s}{T:Verify/%13.13s}\n");
931		xo_emit("{:getfh/%13ju}{:lock/%13ju}{:lockt/%13ju}"
932		    "{:locku/%13ju}{:close/%13ju}{:verify/%13ju}\n",
933		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_GETFH],
934		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LOCK],
935		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LOCKT],
936		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LOCKU],
937		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_CLOSE],
938		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_VERIFY]);
939		xo_emit("{T:NVerify/%13.13s}{T:PutFH/%13.13s}"
940		    "{T:PutPubFH/%13.13s}{T:PutRootFH/%13.13s}"
941		    "{T:Renew/%13.13s}{T:RestoreFH/%13.13s}\n");
942		xo_emit("{:nverify/%13ju}{:putfh/%13ju}{:putpubfh/%13ju}"
943		    "{:putrootfh/%13ju}{:renew/%13ju}{:restore/%13ju}\n",
944		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_NVERIFY],
945		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_PUTFH],
946		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_PUTPUBFH],
947		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_PUTROOTFH],
948		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RENEW],
949		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RESTOREFH]);
950		xo_emit("{T:SaveFH/%13.13s}{T:Secinfo/%13.13s}"
951		    "{T:RelLockOwn/%13.13s}{T:V4Create/%13.13s}\n");
952		xo_emit("{:savefh/%13ju}{:secinfo/%13ju}{:rellockown/%13ju}"
953		    "{:v4create/%13ju}\n",
954		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SAVEFH],
955		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SECINFO],
956		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RELEASELCKOWN],
957		    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_CREATE]);
958		if (nfs41) {
959			xo_open_container("nfsv41");
960			xo_emit("{T:BackChannelCtrl/%13.13s}{T:BindConnToSess/%13.13s}"
961			    "{T:ExchangeID/%13.13s}{T:CreateSess/%13.13s}"
962			    "{T:DestroySess/%13.13s}{T:FreeStateID/%13.13s}\n");
963			xo_emit("{:backchannelctrl/%13ju}{:bindconntosess/%13ju}"
964			    "{:exchangeid/%13ju}{:createsess/%13ju}"
965			    "{:destroysess/%13ju}{:freestateid/%13ju}\n",
966			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_BACKCHANNELCTL],
967			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_BINDCONNTOSESS],
968			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_EXCHANGEID],
969			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_CREATESESSION],
970			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_DESTROYSESSION],
971			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_FREESTATEID]),
972
973			xo_emit("{T:GetDirDeleg/%13.13s}{T:GetDevInfo/%13.13s}"
974			    "{T:GetDevList/%13.13s}{T:layoutCommit/%13.13s}"
975			    "{T:LayoutGet/%13.13s}{T:LayoutReturn/%13.13s}\n");
976			xo_emit("{:getdirdeleg/%13ju}{:getdevinfo/%13ju}"
977			    "{:getdevlist/%13ju}{:layoutcommit/%13ju}"
978			    "{:layoutget/%13ju}{:layoutreturn/%13ju}\n",
979			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_GETDIRDELEG],
980			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_GETDEVINFO],
981			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_GETDEVLIST],
982			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LAYOUTCOMMIT],
983			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LAYOUTGET],
984			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LAYOUTRETURN]);
985
986			xo_emit("{T:SecInfNoName/%13.13s}{T:Sequence/%13.13s}"
987			    "{T:SetSSV/%13.13s}{T:TestStateID/%13.13s}"
988			    "{T:WantDeleg/%13.13s}{T:DestroyClId/%13.13s}\n");
989			xo_emit("{:secinfnoname/%13ju}{:sequence/%13ju}"
990			    "{:setssv/%13ju}{:teststateid/%13ju}{:wantdeleg/%13ju}"
991			    "{:destroyclid/%13ju}\n",
992			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SECINFONONAME],
993			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SEQUENCE],
994			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SETSSV],
995			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_TESTSTATEID],
996			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_WANTDELEG],
997			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_DESTROYCLIENTID]);
998
999			xo_emit("{T:ReclaimCompl/%13.13s}\n");
1000			xo_emit("{:reclaimcompl/%13ju}\n",
1001			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_RECLAIMCOMPL]);
1002
1003			xo_close_container("nfsv41");
1004
1005			xo_open_container("nfsv42");
1006
1007			xo_emit("{T:Allocate/%13.13s}{T:Copy/%13.13s}"
1008			    "{T:CopyNotify/%13.13s}{T:Deallocate/%13.13s}"
1009			    "{T:IOAdvise/%13.13s}{T:LayoutError/%13.13s}\n");
1010			xo_emit("{:allocate/%13ju}{:copy/%13ju}"
1011			    "{:copynotify/%13ju}{:deallocate/%13ju}"
1012			    "{:ioadvise/%13ju}{:layouterror/%13ju}\n",
1013			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_ALLOCATE],
1014			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_COPY],
1015			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_COPYNOTIFY],
1016			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_DEALLOCATE],
1017			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_IOADVISE],
1018			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LAYOUTERROR]);
1019
1020			xo_emit("{T:LayoutStats/%13.13s}{T:OffloadCncl/%13.13s}"
1021			    "{T:OffloadStat/%13.13s}{T:ReadPlus/%13.13s}"
1022			    "{T:Seek/%13.13s}{T:WriteSame/%13.13s}\n");
1023			xo_emit("{:layoutstats/%13ju}{:offloadcncl/%13ju}"
1024			    "{:offloadstat/%13ju}{:readplus/%13ju}"
1025			    "{:seek/%13ju}{:writesame/%13ju}\n",
1026			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LAYOUTSTATS],
1027			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_OFFLOADCANCEL],
1028			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_OFFLOADSTATUS],
1029			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_READPLUS],
1030			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SEEK],
1031			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_WRITESAME]);
1032
1033			xo_emit("{T:Clone/%13.13s}{T:GetExtattr/%13.13s}"
1034			    "{T:SetExtattr/%13.13s}{T:ListExtattr/%13.13s}"
1035			    "{T:RmExtattr/%13.13s}\n");
1036			xo_emit("{:clone/%13ju}{:getextattr/%13ju}"
1037			    "{:setextattr/%13ju}{:listextattr/%13ju}"
1038			    "{:rmextattr/%13ju}\n",
1039			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_CLONE],
1040			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_GETXATTR],
1041			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_SETXATTR],
1042			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_LISTXATTRS],
1043			    (uintmax_t)ext_nfsstats.srvrpccnt[NFSV4OP_REMOVEXATTR]);
1044
1045			xo_close_container("nfsv42");
1046		}
1047
1048		xo_close_container("operations");
1049
1050		if (printtitle)
1051			xo_emit("{T:Server:}\n");
1052		xo_open_container("server");
1053		xo_emit("{T:Clients/%13.13s}{T:OpenOwner/%13.13s}"
1054		    "{T:Opens/%13.13s}{T:LockOwner/%13.13s}{T:Locks/%13.13s}"
1055		    "{T:Delegs/%13.13s}\n");
1056		xo_emit("{:clients/%13ju}{:openowner/%13ju}{:opens/%13ju}"
1057		    "{:lockowner/%13ju}{:locks/%13ju}{:delegs/%13ju}\n",
1058		    (uintmax_t)ext_nfsstats.srvclients,
1059		    (uintmax_t)ext_nfsstats.srvopenowners,
1060		    (uintmax_t)ext_nfsstats.srvopens,
1061		    (uintmax_t)ext_nfsstats.srvlockowners,
1062		    (uintmax_t)ext_nfsstats.srvlocks,
1063		    (uintmax_t)ext_nfsstats.srvdelegates);
1064		xo_close_container("server");
1065
1066		if (printtitle)
1067			xo_emit("{T:Server Cache Stats:}\n");
1068		xo_open_container("cache");
1069		xo_emit("{T:Inprog/%13.13s}"
1070		    "{T:Non-idem/%13.13s}{T:Misses/%13.13s}"
1071		    "{T:CacheSize/%13.13s}{T:TCPPeak/%13.13s}\n");
1072		xo_emit("{:inprog/%13ju}{:nonidem/%13ju}"
1073		    "{:misses/%13ju}{:cachesize/%13ju}{:tcppeak/%13ju}\n",
1074		    (uintmax_t)ext_nfsstats.srvcache_inproghits,
1075		    (uintmax_t)ext_nfsstats.srvcache_nonidemdonehits,
1076		    (uintmax_t)ext_nfsstats.srvcache_misses,
1077		    (uintmax_t)ext_nfsstats.srvcache_size,
1078		    (uintmax_t)ext_nfsstats.srvcache_tcppeak);
1079		xo_close_container("cache");
1080
1081		xo_close_container("serverstats");
1082	}
1083
1084	xo_close_container("nfsv4");
1085}
1086
1087static void
1088compute_totals(struct nfsstatsv1 *total_stats, struct nfsstatsv1 *cur_stats)
1089{
1090	int i;
1091
1092	bzero(total_stats, sizeof(*total_stats));
1093	for (i = 0; i < (NFSV42_NOPS + NFSV4OP_FAKENOPS); i++) {
1094		total_stats->srvbytes[0] += cur_stats->srvbytes[i];
1095		total_stats->srvops[0] += cur_stats->srvops[i];
1096		bintime_add(&total_stats->srvduration[0],
1097			    &cur_stats->srvduration[i]);
1098		total_stats->srvrpccnt[i] = cur_stats->srvrpccnt[i];
1099	}
1100	total_stats->srvstartcnt = cur_stats->srvstartcnt;
1101	total_stats->srvdonecnt = cur_stats->srvdonecnt;
1102	total_stats->busytime = cur_stats->busytime;
1103
1104}
1105
1106/*
1107 * Print a running summary of nfs statistics for the experimental client and/or
1108 * server.
1109 * Repeat display every interval seconds, showing statistics
1110 * collected over that interval.  Assumes that interval is non-zero.
1111 * First line printed at top of screen is always cumulative.
1112 */
1113static void
1114exp_sidewaysintpr(u_int interval, int clientOnly, int serverOnly,
1115    int newStats)
1116{
1117	struct nfsstatsv1 nfsstats, lastst, *ext_nfsstatsp;
1118	struct nfsstatsv1 curtotal, lasttotal;
1119	struct timespec ts, lastts;
1120	int hdrcnt = 1;
1121
1122	ext_nfsstatsp = &lastst;
1123	ext_nfsstatsp->vers = NFSSTATS_V1;
1124	if (nfssvc(NFSSVC_GETSTATS | NFSSVC_NEWSTRUCT, ext_nfsstatsp) < 0)
1125		err(1, "Can't get stats");
1126	clock_gettime(CLOCK_MONOTONIC, &lastts);
1127	compute_totals(&lasttotal, ext_nfsstatsp);
1128	sleep(interval);
1129
1130	for (;;) {
1131		ext_nfsstatsp = &nfsstats;
1132		ext_nfsstatsp->vers = NFSSTATS_V1;
1133		if (nfssvc(NFSSVC_GETSTATS | NFSSVC_NEWSTRUCT, ext_nfsstatsp)
1134		    < 0)
1135			err(1, "Can't get stats");
1136		clock_gettime(CLOCK_MONOTONIC, &ts);
1137
1138		if (--hdrcnt == 0) {
1139			printhdr(clientOnly, serverOnly, newStats);
1140			if (newStats)
1141				hdrcnt = 20;
1142			else if (clientOnly && serverOnly)
1143				hdrcnt = 10;
1144			else
1145				hdrcnt = 20;
1146		}
1147		if (clientOnly && newStats == 0) {
1148		    printf("%s %6ju %6ju %6ju %6ju %6ju %6ju %6ju %6ju",
1149			((clientOnly && serverOnly) ? "Client:" : ""),
1150			(uintmax_t)DELTA(rpccnt[NFSPROC_GETATTR]),
1151			(uintmax_t)DELTA(rpccnt[NFSPROC_LOOKUP]),
1152			(uintmax_t)DELTA(rpccnt[NFSPROC_READLINK]),
1153			(uintmax_t)DELTA(rpccnt[NFSPROC_READ]),
1154			(uintmax_t)DELTA(rpccnt[NFSPROC_WRITE]),
1155			(uintmax_t)DELTA(rpccnt[NFSPROC_RENAME]),
1156			(uintmax_t)DELTA(rpccnt[NFSPROC_ACCESS]),
1157			(uintmax_t)(DELTA(rpccnt[NFSPROC_READDIR]) +
1158			DELTA(rpccnt[NFSPROC_READDIRPLUS]))
1159		    );
1160		    if (widemode) {
1161			    printf(" %s %s %s %s %s %s",
1162				sperc1(DELTA(attrcache_hits),
1163				    DELTA(attrcache_misses)),
1164				sperc1(DELTA(lookupcache_hits),
1165				    DELTA(lookupcache_misses)),
1166				sperc2(DELTA(biocache_reads),
1167				    DELTA(read_bios)),
1168				sperc2(DELTA(biocache_writes),
1169				    DELTA(write_bios)),
1170				sperc1(DELTA(accesscache_hits),
1171				    DELTA(accesscache_misses)),
1172				sperc2(DELTA(biocache_readdirs),
1173				    DELTA(readdir_bios))
1174			    );
1175		    }
1176		    printf("\n");
1177		}
1178
1179		if (serverOnly && newStats) {
1180			long double cur_secs, last_secs, etime;
1181			long double mbsec;
1182			long double kb_per_transfer;
1183			long double transfers_per_second;
1184			long double ms_per_transfer;
1185			uint64_t queue_len;
1186			long double busy_pct;
1187			int i;
1188
1189			cur_secs = ts.tv_sec +
1190			    ((long double)ts.tv_nsec / 1000000000);
1191			last_secs = lastts.tv_sec +
1192			    ((long double)lastts.tv_nsec / 1000000000);
1193			etime = cur_secs - last_secs;
1194
1195			compute_totals(&curtotal, &nfsstats);
1196
1197			for (i = 0; i < NUM_STAT_TYPES; i++) {
1198				compute_new_stats(&nfsstats, &lastst,
1199				    STAT_TYPE_TO_NFS(i), etime, &mbsec,
1200				    &kb_per_transfer,
1201				    &transfers_per_second,
1202				    &ms_per_transfer, &queue_len,
1203				    &busy_pct);
1204
1205				if (i == STAT_TYPE_COMMIT) {
1206					if (widemode == 0)
1207						continue;
1208
1209					printf("%2.0Lf %7.2Lf ",
1210					    transfers_per_second,
1211					    ms_per_transfer);
1212				} else {
1213					printf("%5.2Lf %5.0Lf %7.2Lf ",
1214					    kb_per_transfer,
1215					    transfers_per_second, mbsec);
1216					if (widemode)
1217						printf("%5.2Lf ",
1218						    ms_per_transfer);
1219				}
1220			}
1221
1222			compute_new_stats(&curtotal, &lasttotal, 0, etime,
1223			    &mbsec, &kb_per_transfer, &transfers_per_second,
1224			    &ms_per_transfer, &queue_len, &busy_pct);
1225
1226			printf("%5.2Lf %5.0Lf %7.2Lf %5.2Lf %3ju %3.0Lf\n",
1227			    kb_per_transfer, transfers_per_second, mbsec,
1228			    ms_per_transfer, queue_len, busy_pct);
1229		} else if (serverOnly) {
1230		    printf("%s %6ju %6ju %6ju %6ju %6ju %6ju %6ju %6ju",
1231			((clientOnly && serverOnly) ? "Server:" : ""),
1232			(uintmax_t)DELTA(srvrpccnt[NFSV4OP_GETATTR]),
1233			(uintmax_t)DELTA(srvrpccnt[NFSV4OP_LOOKUP]),
1234			(uintmax_t)DELTA(srvrpccnt[NFSV4OP_READLINK]),
1235			(uintmax_t)DELTA(srvrpccnt[NFSV4OP_READ]),
1236			(uintmax_t)DELTA(srvrpccnt[NFSV4OP_WRITE]),
1237			(uintmax_t)DELTA(srvrpccnt[NFSV4OP_RENAME]),
1238			(uintmax_t)DELTA(srvrpccnt[NFSV4OP_ACCESS]),
1239			(uintmax_t)(DELTA(srvrpccnt[NFSV4OP_READDIR]) +
1240			DELTA(srvrpccnt[NFSV4OP_READDIRPLUS])));
1241		    printf("\n");
1242		}
1243		bcopy(&nfsstats, &lastst, sizeof(lastst));
1244		bcopy(&curtotal, &lasttotal, sizeof(lasttotal));
1245		lastts = ts;
1246		fflush(stdout);
1247		sleep(interval);
1248	}
1249	/*NOTREACHED*/
1250}
1251