xref: /freebsd/usr.sbin/iostat/iostat.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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$
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 
121 struct 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 
134 struct statinfo cur, last;
135 int num_devices;
136 struct device_selection *dev_select;
137 int maxshowdevs;
138 volatile sig_atomic_t headercount;
139 int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0;
140 int xflag = 0, zflag = 0;
141 
142 /* local function declarations */
143 static void usage(void);
144 static void needhdr(int signo);
145 static void phdr(void);
146 static void devstats(int perf_select, long double etime, int havelast);
147 static void cpustats(void);
148 static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr,
149 		   size_t len);
150 
151 static void
152 usage(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 
165 int
166 main(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  */
583 void
584 needhdr(int signo)
585 {
586 
587 	headercount = 1;
588 }
589 
590 static void
591 phdr(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 
652 static void
653 devstats(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 	long double busy_pct;
660 	u_int64_t queue_len;
661 	long double total_mb;
662 	long double blocks_per_second, ms_per_transaction;
663 	int firstline = 1;
664 	char *devname;
665 
666 	if (xflag > 0) {
667 		printf("                        extended device statistics  ");
668 		if (Tflag > 0)
669 			printf("     tty ");
670 		if (Cflag > 0)
671 			printf("           cpu ");
672 		printf("\n");
673 		printf("device     r/s   w/s    kr/s    kw/s wait svc_t  %%b  ");
674 		if (Tflag > 0)
675 			printf("tin tout ");
676 		if (Cflag > 0)
677 			printf("us ni sy in id ");
678 		printf("\n");
679 	}
680 
681 	for (dn = 0; dn < num_devices; dn++) {
682 		int di;
683 
684 		if (((perf_select == 0) && (dev_select[dn].selected == 0))
685 		 || (dev_select[dn].selected > maxshowdevs))
686 			continue;
687 
688 		di = dev_select[dn].position;
689 
690 		if (devstat_compute_statistics(&cur.dinfo->devices[di],
691 		    havelast ? &last.dinfo->devices[di] : NULL, etime,
692 		    DSM_TOTAL_BYTES, &total_bytes,
693 		    DSM_TOTAL_TRANSFERS, &total_transfers,
694 		    DSM_TOTAL_BLOCKS, &total_blocks,
695 		    DSM_KB_PER_TRANSFER, &kb_per_transfer,
696 		    DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
697 		    DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read,
698 		    DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write,
699 		    DSM_MB_PER_SECOND, &mb_per_second,
700 		    DSM_MB_PER_SECOND_READ, &mb_per_second_read,
701 		    DSM_MB_PER_SECOND_WRITE, &mb_per_second_write,
702 		    DSM_BLOCKS_PER_SECOND, &blocks_per_second,
703 		    DSM_MS_PER_TRANSACTION, &ms_per_transaction,
704 		    DSM_BUSY_PCT, &busy_pct,
705 		    DSM_QUEUE_LENGTH, &queue_len,
706 		    DSM_NONE) != 0)
707 			errx(1, "%s", devstat_errbuf);
708 
709 		if (perf_select != 0) {
710 			dev_select[dn].bytes = total_bytes;
711 			if ((dev_select[dn].selected == 0)
712 			 || (dev_select[dn].selected > maxshowdevs))
713 				continue;
714 		}
715 
716 		if (Kflag > 0 || xflag > 0) {
717 			int block_size = cur.dinfo->devices[di].block_size;
718 			total_blocks = total_blocks * (block_size ?
719 						       block_size : 512) / 1024;
720 		}
721 
722 		if (xflag > 0) {
723 			asprintf(&devname, "%s%d",
724 			    cur.dinfo->devices[di].device_name,
725 			    cur.dinfo->devices[di].unit_number);
726 			/*
727 			 * If zflag is set, skip any devices with zero I/O.
728 			 */
729 			if (zflag == 0 || transfers_per_second_read > 0.05 ||
730 			    transfers_per_second_write > 0.05 ||
731 			    mb_per_second_read > ((long double).0005)/1024 ||
732 			    mb_per_second_write > ((long double).0005)/1024 ||
733 			    busy_pct > 0.5) {
734 				printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4qu %5.1Lf %3.0Lf ",
735 				    devname, transfers_per_second_read,
736 				    transfers_per_second_write,
737 				    mb_per_second_read * 1024,
738 				    mb_per_second_write * 1024, queue_len,
739 				    ms_per_transaction, busy_pct);
740 				if (firstline) {
741 					/*
742 					 * If this is the first device
743 					 * we're printing, also print
744 					 * CPU or TTY stats if requested.
745 					 */
746 					firstline = 0;
747 					if (Tflag > 0)
748 						printf("%4.0Lf%5.0Lf",
749 						    cur.tk_nin / etime,
750 						    cur.tk_nout / etime);
751 					if (Cflag > 0)
752 						cpustats();
753 				}
754 				printf("\n");
755 			}
756 		} else if (oflag > 0) {
757 			int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
758 
759 			if (Iflag == 0)
760 				printf("%4.0Lf%4.0Lf%5.*Lf ",
761 				       blocks_per_second,
762 				       transfers_per_second,
763 				       msdig,
764 				       ms_per_transaction);
765 			else
766 				printf("%4.1qu%4.1qu%5.*Lf ",
767 				       total_blocks,
768 				       total_transfers,
769 				       msdig,
770 				       ms_per_transaction);
771 		} else {
772 			if (Iflag == 0)
773 				printf(" %5.2Lf %3.0Lf %5.2Lf ",
774 				       kb_per_transfer,
775 				       transfers_per_second,
776 				       mb_per_second);
777 			else {
778 				total_mb = total_bytes;
779 				total_mb /= 1024 * 1024;
780 
781 				printf(" %5.2Lf %3.1qu %5.2Lf ",
782 				       kb_per_transfer,
783 				       total_transfers,
784 				       total_mb);
785 			}
786 		}
787 	}
788 	if (xflag > 0 && zflag > 0 && firstline == 1 &&
789 	    (Tflag > 0 || Cflag > 0)) {
790 		/*
791 		 * If zflag is set and we did not print any device
792 		 * lines I/O because they were all zero,
793 		 * print TTY/CPU stats.
794 		 */
795 		printf("%52s","");
796 		if (Tflag > 0)
797 			printf("%4.0Lf%5.0Lf", cur.tk_nin / etime,
798 			    cur.tk_nout / etime);
799 		if (Cflag > 0)
800 			cpustats();
801 		printf("\n");
802 	}
803 }
804 
805 static void
806 cpustats(void)
807 {
808 	int state;
809 	double time;
810 
811 	time = 0.0;
812 
813 	for (state = 0; state < CPUSTATES; ++state)
814 		time += cur.cp_time[state];
815 	for (state = 0; state < CPUSTATES; ++state)
816 		printf(" %2.0f",
817 		       rint(100. * cur.cp_time[state] / (time ? time : 1)));
818 }
819 
820 static int
821 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len)
822 {
823 	if (kd != NULL) {
824 		ssize_t nbytes;
825 
826 		nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len);
827 
828 		if (nbytes < 0) {
829 			warnx("kvm_read(%s): %s", namelist[nlid].n_name,
830 			    kvm_geterr(kd));
831 			return (1);
832 		}
833 		if (nbytes != len) {
834 			warnx("kvm_read(%s): expected %zu bytes, got %zd bytes",
835 			      namelist[nlid].n_name, len, nbytes);
836 			return (1);
837 		}
838 	} else {
839 		size_t nlen = len;
840 
841 		if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
842 			warn("sysctl(%s...) failed", name);
843 			return (1);
844 		}
845 		if (nlen != len) {
846 			warnx("sysctl(%s...): expected %lu, got %lu", name,
847 			      (unsigned long)len, (unsigned long)nlen);
848 			return (1);
849 		}
850 	}
851 	return (0);
852 }
853