iostat.c revision 169558
1/*
2 * Copyright (c) 1997, 1998, 2000, 2001  Kenneth D. Merry
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/usr.sbin/iostat/iostat.c 169558 2007-05-14 18:04:07Z maxim $
29 */
30/*
31 * Parts of this program are derived from the original FreeBSD iostat
32 * program:
33 */
34/*-
35 * Copyright (c) 1986, 1991, 1993
36 *	The Regents of the University of California.  All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 *    notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 *    notice, this list of conditions and the following disclaimer in the
45 *    documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *	This product includes software developed by the University of
49 *	California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 *    may be used to endorse or promote products derived from this software
52 *    without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 */
66/*
67 * Ideas for the new iostat statistics output modes taken from the NetBSD
68 * version of iostat:
69 */
70/*
71 * Copyright (c) 1996 John M. Vinopal
72 * All rights reserved.
73 *
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
76 * are met:
77 * 1. Redistributions of source code must retain the above copyright
78 *    notice, this list of conditions and the following disclaimer.
79 * 2. Redistributions in binary form must reproduce the above copyright
80 *    notice, this list of conditions and the following disclaimer in the
81 *    documentation and/or other materials provided with the distribution.
82 * 3. All advertising materials mentioning features or use of this software
83 *    must display the following acknowledgement:
84 *      This product includes software developed for the NetBSD Project
85 *      by John M. Vinopal.
86 * 4. The name of the author may not be used to endorse or promote products
87 *    derived from this software without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
90 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
91 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
92 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
93 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
94 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
95 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
96 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 */
101
102
103#include <sys/param.h>
104#include <sys/errno.h>
105#include <sys/resource.h>
106#include <sys/sysctl.h>
107
108#include <err.h>
109#include <ctype.h>
110#include <fcntl.h>
111#include <kvm.h>
112#include <nlist.h>
113#include <stdio.h>
114#include <stdlib.h>
115#include <string.h>
116#include <unistd.h>
117#include <limits.h>
118#include <devstat.h>
119#include <math.h>
120
121struct nlist namelist[] = {
122#define X_TK_NIN	0
123	{ "_tk_nin" },
124#define X_TK_NOUT	1
125	{ "_tk_nout" },
126#define X_CP_TIME	2
127	{ "_cp_time" },
128#define X_BOOTTIME	3
129	{ "_boottime" },
130#define X_END		3
131	{ NULL },
132};
133
134struct statinfo cur, last;
135int num_devices;
136struct device_selection *dev_select;
137int maxshowdevs;
138volatile sig_atomic_t headercount;
139int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0;
140int xflag = 0, zflag = 0;
141
142/* local function declarations */
143static void usage(void);
144static void needhdr(int signo);
145static void phdr(void);
146static void devstats(int perf_select, long double etime, int havelast);
147static void cpustats(void);
148static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr,
149		   size_t len);
150
151static void
152usage(void)
153{
154	/*
155	 * We also support the following 'traditional' syntax:
156	 * iostat [drives] [wait [count]]
157	 * This isn't mentioned in the man page, or the usage statement,
158	 * but it is supported.
159	 */
160	fprintf(stderr, "usage: iostat [-CdhIKoTxz?] [-c count] [-M core]"
161		" [-n devs] [-N system]\n"
162		"\t      [-t type,if,pass] [-w wait] [drives]\n");
163}
164
165int
166main(int argc, char **argv)
167{
168	int c, i;
169	int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
170	int count = 0, waittime = 0;
171	char *memf = NULL, *nlistf = NULL;
172	struct devstat_match *matches;
173	int num_matches = 0;
174	char errbuf[_POSIX2_LINE_MAX];
175	kvm_t *kd = NULL;
176	long generation;
177	int num_devices_specified;
178	int num_selected, num_selections;
179	long select_generation;
180	char **specified_devices;
181	devstat_select_mode select_mode;
182	int havelast = 0;
183
184	matches = NULL;
185	maxshowdevs = 3;
186
187	while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) {
188		switch(c) {
189			case 'c':
190				cflag++;
191				count = atoi(optarg);
192				if (count < 1)
193					errx(1, "count %d is < 1", count);
194				break;
195			case 'C':
196				Cflag++;
197				break;
198			case 'd':
199				dflag++;
200				break;
201			case 'h':
202				hflag++;
203				break;
204			case 'I':
205				Iflag++;
206				break;
207			case 'K':
208				Kflag++;
209				break;
210			case 'M':
211				memf = optarg;
212				break;
213			case 'n':
214				nflag++;
215				maxshowdevs = atoi(optarg);
216				if (maxshowdevs < 0)
217					errx(1, "number of devices %d is < 0",
218					     maxshowdevs);
219				break;
220			case 'N':
221				nlistf = optarg;
222				break;
223			case 'o':
224				oflag++;
225				break;
226			case 't':
227				tflag++;
228				if (devstat_buildmatch(optarg, &matches,
229						       &num_matches) != 0)
230					errx(1, "%s", devstat_errbuf);
231				break;
232			case 'T':
233				Tflag++;
234				break;
235			case 'w':
236				wflag++;
237				waittime = atoi(optarg);
238				if (waittime < 1)
239					errx(1, "wait time is < 1");
240				break;
241			case 'x':
242				xflag++;
243				break;
244			case 'z':
245				zflag++;
246				break;
247			default:
248				usage();
249				exit(1);
250				break;
251		}
252	}
253
254	argc -= optind;
255	argv += optind;
256
257	if (nlistf != NULL || memf != NULL) {
258		kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
259
260		if (kd == NULL)
261			errx(1, "kvm_openfiles: %s", errbuf);
262
263		if (kvm_nlist(kd, namelist) == -1)
264			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
265	}
266
267	/*
268	 * Make sure that the userland devstat version matches the kernel
269	 * devstat version.  If not, exit and print a message informing
270	 * the user of his mistake.
271	 */
272	if (devstat_checkversion(kd) < 0)
273		errx(1, "%s", devstat_errbuf);
274
275	/*
276	 * Make sure Tflag and/or Cflag are set if dflag == 0.  If dflag is
277	 * greater than 0, they may be 0 or non-zero.
278	 */
279	if (dflag == 0 && xflag == 0) {
280		Cflag = 1;
281		Tflag = 1;
282	}
283
284	/*
285	 * Figure out how many devices we should display.
286	 */
287	if (nflag == 0) {
288		if (oflag > 0) {
289			if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
290				maxshowdevs = 5;
291			else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
292				maxshowdevs = 5;
293			else
294				maxshowdevs = 4;
295		} else {
296			if ((dflag > 0) && (Cflag == 0))
297				maxshowdevs = 4;
298			else
299				maxshowdevs = 3;
300		}
301	}
302
303	/* find out how many devices we have */
304	if ((num_devices = devstat_getnumdevs(kd)) < 0)
305		err(1, "can't get number of devices");
306
307	cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
308	if (cur.dinfo == NULL)
309		err(1, "malloc failed");
310
311	last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
312	if (last.dinfo == NULL)
313		err(1, "malloc failed");
314
315	bzero(cur.dinfo, sizeof(struct devinfo));
316	bzero(last.dinfo, sizeof(struct devinfo));
317
318	/*
319	 * Grab all the devices.  We don't look to see if the list has
320	 * changed here, since it almost certainly has.  We only look for
321	 * errors.
322	 */
323	if (devstat_getdevs(kd, &cur) == -1)
324		errx(1, "%s", devstat_errbuf);
325
326	num_devices = cur.dinfo->numdevs;
327	generation = cur.dinfo->generation;
328
329	/*
330	 * If the user specified any devices on the command line, see if
331	 * they are in the list of devices we have now.
332	 */
333	specified_devices = (char **)malloc(sizeof(char *));
334	if (specified_devices == NULL)
335		err(1, "malloc failed");
336
337	for (num_devices_specified = 0; *argv; ++argv) {
338		if (isdigit(**argv))
339			break;
340		num_devices_specified++;
341		specified_devices = (char **)realloc(specified_devices,
342						     sizeof(char *) *
343						     num_devices_specified);
344		if (specified_devices == NULL)
345			err(1, "realloc failed");
346
347		specified_devices[num_devices_specified - 1] = *argv;
348
349	}
350	if (nflag == 0 && maxshowdevs < num_devices_specified)
351		maxshowdevs = num_devices_specified;
352
353	dev_select = NULL;
354
355	if ((num_devices_specified == 0) && (num_matches == 0))
356		select_mode = DS_SELECT_ADD;
357	else
358		select_mode = DS_SELECT_ONLY;
359
360	/*
361	 * At this point, selectdevs will almost surely indicate that the
362	 * device list has changed, so we don't look for return values of 0
363	 * or 1.  If we get back -1, though, there is an error.
364	 */
365	if (devstat_selectdevs(&dev_select, &num_selected,
366			       &num_selections, &select_generation, generation,
367			       cur.dinfo->devices, num_devices, matches,
368			       num_matches, specified_devices,
369			       num_devices_specified, select_mode, maxshowdevs,
370			       hflag) == -1)
371		errx(1, "%s", devstat_errbuf);
372
373	/*
374	 * Look for the traditional wait time and count arguments.
375	 */
376	if (*argv) {
377		waittime = atoi(*argv);
378
379		/* Let the user know he goofed, but keep going anyway */
380		if (wflag != 0)
381			warnx("discarding previous wait interval, using"
382			      " %d instead", waittime);
383		wflag++;
384
385		if (*++argv) {
386			count = atoi(*argv);
387			if (cflag != 0)
388				warnx("discarding previous count, using %d"
389				      " instead", count);
390			cflag++;
391		} else
392			count = -1;
393	}
394
395	/*
396	 * If the user specified a count, but not an interval, we default
397	 * to an interval of 1 second.
398	 */
399	if ((wflag == 0) && (cflag > 0))
400		waittime = 1;
401
402	/*
403	 * If the user specified a wait time, but not a count, we want to
404	 * go on ad infinitum.  This can be redundant if the user uses the
405	 * traditional method of specifying the wait, since in that case we
406	 * already set count = -1 above.  Oh well.
407	 */
408	if ((wflag > 0) && (cflag == 0))
409		count = -1;
410
411	bzero(&cur.cp_time, sizeof(cur.cp_time));
412	cur.tk_nout = 0;
413	cur.tk_nin = 0;
414
415	/*
416	 * Set the snap time to the system boot time (ie: zero), so the
417	 * stats are calculated since system boot.
418	 */
419	cur.snap_time = 0;
420
421	/*
422	 * If the user stops the program (control-Z) and then resumes it,
423	 * print out the header again.
424	 */
425	(void)signal(SIGCONT, needhdr);
426
427	for (headercount = 1;;) {
428		struct devinfo *tmp_dinfo;
429		long tmp;
430		long double etime;
431
432		if (Tflag > 0) {
433			if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin,
434			     sizeof(cur.tk_nin)) != 0)
435			 || (readvar(kd, "kern.tty_nout", X_TK_NOUT,
436			     &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) {
437				Tflag = 0;
438				warnx("disabling TTY statistics");
439			}
440		 }
441
442		if (Cflag > 0) {
443			if (readvar(kd, "kern.cp_time", X_CP_TIME,
444			    &cur.cp_time, sizeof(cur.cp_time)) != 0) {
445				Cflag = 0;
446				warnx("disabling CPU time statistics");
447			}
448		}
449
450		if (!--headercount) {
451			phdr();
452			headercount = 20;
453		}
454
455		tmp_dinfo = last.dinfo;
456		last.dinfo = cur.dinfo;
457		cur.dinfo = tmp_dinfo;
458
459		last.snap_time = cur.snap_time;
460
461		/*
462		 * Here what we want to do is refresh our device stats.
463		 * devstat_getdevs() returns 1 when the device list has changed.
464		 * If the device list has changed, we want to go through
465		 * the selection process again, in case a device that we
466		 * were previously displaying has gone away.
467		 */
468		switch (devstat_getdevs(kd, &cur)) {
469		case -1:
470			errx(1, "%s", devstat_errbuf);
471			break;
472		case 1: {
473			int retval;
474
475			num_devices = cur.dinfo->numdevs;
476			generation = cur.dinfo->generation;
477			retval = devstat_selectdevs(&dev_select, &num_selected,
478						    &num_selections,
479						    &select_generation,
480						    generation,
481						    cur.dinfo->devices,
482						    num_devices, matches,
483						    num_matches,
484						    specified_devices,
485						    num_devices_specified,
486						    select_mode, maxshowdevs,
487						    hflag);
488			switch(retval) {
489			case -1:
490				errx(1, "%s", devstat_errbuf);
491				break;
492			case 1:
493				phdr();
494				headercount = 20;
495				break;
496			default:
497				break;
498			}
499			break;
500		}
501		default:
502			break;
503		}
504
505		/*
506		 * We only want to re-select devices if we're in 'top'
507		 * mode.  This is the only mode where the devices selected
508		 * could actually change.
509		 */
510		if (hflag > 0) {
511			int retval;
512			retval = devstat_selectdevs(&dev_select, &num_selected,
513						    &num_selections,
514						    &select_generation,
515						    generation,
516						    cur.dinfo->devices,
517						    num_devices, matches,
518						    num_matches,
519						    specified_devices,
520						    num_devices_specified,
521						    select_mode, maxshowdevs,
522						    hflag);
523			switch(retval) {
524			case -1:
525				errx(1,"%s", devstat_errbuf);
526				break;
527			case 1:
528				phdr();
529				headercount = 20;
530				break;
531			default:
532				break;
533			}
534		}
535
536		if (Tflag > 0) {
537			tmp = cur.tk_nin;
538			cur.tk_nin -= last.tk_nin;
539			last.tk_nin = tmp;
540			tmp = cur.tk_nout;
541			cur.tk_nout -= last.tk_nout;
542			last.tk_nout = tmp;
543		}
544
545		etime = cur.snap_time - last.snap_time;
546
547		if (etime == 0.0)
548			etime = 1.0;
549
550		for (i = 0; i < CPUSTATES; i++) {
551			tmp = cur.cp_time[i];
552			cur.cp_time[i] -= last.cp_time[i];
553			last.cp_time[i] = tmp;
554		}
555
556		if (xflag == 0 && Tflag > 0)
557			printf("%4.0Lf%5.0Lf", cur.tk_nin / etime,
558			    cur.tk_nout / etime);
559
560		devstats(hflag, etime, havelast);
561
562		if (xflag == 0) {
563			if (Cflag > 0)
564				cpustats();
565
566			printf("\n");
567		}
568		fflush(stdout);
569
570		if (count >= 0 && --count <= 0)
571			break;
572
573		sleep(waittime);
574		havelast = 1;
575	}
576
577	exit(0);
578}
579
580/*
581 * Force a header to be prepended to the next output.
582 */
583void
584needhdr(int signo)
585{
586
587	headercount = 1;
588}
589
590static void
591phdr(void)
592{
593	int i, printed;
594
595	/*
596	 * If xflag is set, we need a per-loop header, not a page header, so
597	 * just return.  We'll print the header in devstats().
598	 */
599	if (xflag > 0)
600		return;
601
602	if (Tflag > 0)
603		(void)printf("      tty");
604	for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){
605		int di;
606		if ((dev_select[i].selected != 0)
607		 && (dev_select[i].selected <= maxshowdevs)) {
608			di = dev_select[i].position;
609			if (oflag > 0)
610				(void)printf("%12.6s%d ",
611					    cur.dinfo->devices[di].device_name,
612					    cur.dinfo->devices[di].unit_number);
613			else
614				printf("%15.6s%d ",
615					    cur.dinfo->devices[di].device_name,
616					    cur.dinfo->devices[di].unit_number);
617			printed++;
618		}
619	}
620	if (Cflag > 0)
621		(void)printf("            cpu\n");
622	else
623		(void)printf("\n");
624
625	if (Tflag > 0)
626		(void)printf(" tin tout");
627
628	for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){
629		if ((dev_select[i].selected != 0)
630		 && (dev_select[i].selected <= maxshowdevs)) {
631			if (oflag > 0) {
632				if (Iflag == 0)
633					(void)printf(" sps tps msps ");
634				else
635					(void)printf(" blk xfr msps ");
636			} else {
637				if (Iflag == 0)
638					printf("  KB/t tps  MB/s ");
639				else
640					printf("  KB/t xfrs   MB ");
641			}
642			printed++;
643		}
644	}
645	if (Cflag > 0)
646		(void)printf(" us ni sy in id\n");
647	else
648		printf("\n");
649
650}
651
652static void
653devstats(int perf_select, long double etime, int havelast)
654{
655	int dn;
656	long double transfers_per_second, transfers_per_second_read, transfers_per_second_write;
657	long double kb_per_transfer, mb_per_second, mb_per_second_read, mb_per_second_write;
658	u_int64_t total_bytes, total_transfers, total_blocks;
659	u_int64_t total_bytes_read, total_transfers_read;
660	u_int64_t total_bytes_write, total_transfers_write;
661	long double busy_pct;
662	u_int64_t queue_len;
663	long double total_mb;
664	long double blocks_per_second, ms_per_transaction;
665	int firstline = 1;
666	char *devname;
667
668	if (xflag > 0) {
669		printf("                        extended device statistics  ");
670		if (Tflag > 0)
671			printf("     tty ");
672		if (Cflag > 0)
673			printf("           cpu ");
674		printf("\n");
675		if (Iflag == 0)
676			printf(
677		"device     r/s   w/s    kr/s    kw/s wait svc_t  %b  "
678			    );
679		else
680			printf(
681		"device     r/i   w/i    kr/i    kw/i wait svc_t  %b  "
682			    );
683		if (Tflag > 0)
684			printf("tin tout ");
685		if (Cflag > 0)
686			printf("us ni sy in id ");
687		printf("\n");
688	}
689
690	for (dn = 0; dn < num_devices; dn++) {
691		int di;
692
693		if (((perf_select == 0) && (dev_select[dn].selected == 0))
694		 || (dev_select[dn].selected > maxshowdevs))
695			continue;
696
697		di = dev_select[dn].position;
698
699		if (devstat_compute_statistics(&cur.dinfo->devices[di],
700		    havelast ? &last.dinfo->devices[di] : NULL, etime,
701		    DSM_TOTAL_BYTES, &total_bytes,
702		    DSM_TOTAL_BYTES_READ, &total_bytes_read,
703		    DSM_TOTAL_BYTES_WRITE, &total_bytes_write,
704		    DSM_TOTAL_TRANSFERS, &total_transfers,
705		    DSM_TOTAL_TRANSFERS_READ, &total_transfers_read,
706		    DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write,
707		    DSM_TOTAL_BLOCKS, &total_blocks,
708		    DSM_KB_PER_TRANSFER, &kb_per_transfer,
709		    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
710		    DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read,
711		    DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write,
712		    DSM_MB_PER_SECOND, &mb_per_second,
713		    DSM_MB_PER_SECOND_READ, &mb_per_second_read,
714		    DSM_MB_PER_SECOND_WRITE, &mb_per_second_write,
715		    DSM_BLOCKS_PER_SECOND, &blocks_per_second,
716		    DSM_MS_PER_TRANSACTION, &ms_per_transaction,
717		    DSM_BUSY_PCT, &busy_pct,
718		    DSM_QUEUE_LENGTH, &queue_len,
719		    DSM_NONE) != 0)
720			errx(1, "%s", devstat_errbuf);
721
722		if (perf_select != 0) {
723			dev_select[dn].bytes = total_bytes;
724			if ((dev_select[dn].selected == 0)
725			 || (dev_select[dn].selected > maxshowdevs))
726				continue;
727		}
728
729		if (Kflag > 0 || xflag > 0) {
730			int block_size = cur.dinfo->devices[di].block_size;
731			total_blocks = total_blocks * (block_size ?
732						       block_size : 512) / 1024;
733		}
734
735		if (xflag > 0) {
736			asprintf(&devname, "%s%d",
737			    cur.dinfo->devices[di].device_name,
738			    cur.dinfo->devices[di].unit_number);
739			/*
740			 * If zflag is set, skip any devices with zero I/O.
741			 */
742			if (zflag == 0 || transfers_per_second_read > 0.05 ||
743			    transfers_per_second_write > 0.05 ||
744			    mb_per_second_read > ((long double).0005)/1024 ||
745			    mb_per_second_write > ((long double).0005)/1024 ||
746			    busy_pct > 0.5) {
747				if (Iflag == 0)
748					printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4qu %5.1Lf %3.0Lf ",
749					    devname, transfers_per_second_read,
750					    transfers_per_second_write,
751					    mb_per_second_read * 1024,
752					    mb_per_second_write * 1024,
753					    queue_len,
754					    ms_per_transaction, busy_pct);
755				else
756					printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4qu %5.1Lf %3.0Lf ",
757					    devname,
758					    (long double)total_transfers_read,
759					    (long double)total_transfers_write,
760					    (long double)
761					        total_bytes_read / 1024,
762					    (long double)
763					        total_bytes_write / 1024,
764					    queue_len,
765					    ms_per_transaction, busy_pct);
766				if (firstline) {
767					/*
768					 * If this is the first device
769					 * we're printing, also print
770					 * CPU or TTY stats if requested.
771					 */
772					firstline = 0;
773					if (Tflag > 0)
774						printf("%4.0Lf%5.0Lf",
775						    cur.tk_nin / etime,
776						    cur.tk_nout / etime);
777					if (Cflag > 0)
778						cpustats();
779				}
780				printf("\n");
781			}
782		} else if (oflag > 0) {
783			int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
784
785			if (Iflag == 0)
786				printf("%4.0Lf%4.0Lf%5.*Lf ",
787				       blocks_per_second,
788				       transfers_per_second,
789				       msdig,
790				       ms_per_transaction);
791			else
792				printf("%4.1qu%4.1qu%5.*Lf ",
793				       total_blocks,
794				       total_transfers,
795				       msdig,
796				       ms_per_transaction);
797		} else {
798			if (Iflag == 0)
799				printf(" %5.2Lf %3.0Lf %5.2Lf ",
800				       kb_per_transfer,
801				       transfers_per_second,
802				       mb_per_second);
803			else {
804				total_mb = total_bytes;
805				total_mb /= 1024 * 1024;
806
807				printf(" %5.2Lf %3.1qu %5.2Lf ",
808				       kb_per_transfer,
809				       total_transfers,
810				       total_mb);
811			}
812		}
813	}
814	if (xflag > 0 && zflag > 0 && firstline == 1 &&
815	    (Tflag > 0 || Cflag > 0)) {
816		/*
817		 * If zflag is set and we did not print any device
818		 * lines I/O because they were all zero,
819		 * print TTY/CPU stats.
820		 */
821		printf("%52s","");
822		if (Tflag > 0)
823			printf("%4.0Lf%5.0Lf", cur.tk_nin / etime,
824			    cur.tk_nout / etime);
825		if (Cflag > 0)
826			cpustats();
827		printf("\n");
828	}
829}
830
831static void
832cpustats(void)
833{
834	int state;
835	double time;
836
837	time = 0.0;
838
839	for (state = 0; state < CPUSTATES; ++state)
840		time += cur.cp_time[state];
841	for (state = 0; state < CPUSTATES; ++state)
842		printf(" %2.0f",
843		       rint(100. * cur.cp_time[state] / (time ? time : 1)));
844}
845
846static int
847readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len)
848{
849	if (kd != NULL) {
850		ssize_t nbytes;
851
852		nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len);
853
854		if (nbytes < 0) {
855			warnx("kvm_read(%s): %s", namelist[nlid].n_name,
856			    kvm_geterr(kd));
857			return (1);
858		}
859		if (nbytes != len) {
860			warnx("kvm_read(%s): expected %zu bytes, got %zd bytes",
861			      namelist[nlid].n_name, len, nbytes);
862			return (1);
863		}
864	} else {
865		size_t nlen = len;
866
867		if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
868			warn("sysctl(%s...) failed", name);
869			return (1);
870		}
871		if (nlen != len) {
872			warnx("sysctl(%s...): expected %lu, got %lu", name,
873			      (unsigned long)len, (unsigned long)nlen);
874			return (1);
875		}
876	}
877	return (0);
878}
879