2 * Copyright (c) 1997, 1998, 2000, 2001 Kenneth D. Merry
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 * Parts of this program are derived from the original FreeBSD iostat
35 * Copyright (c) 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
67 * Ideas for the new iostat statistics output modes taken from the NetBSD
71 * Copyright (c) 1996 John M. Vinopal
72 * All rights reserved.
74 * Redistribution and use in source and binary forms, with or without
75 * modification, are permitted provided that the following conditions
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
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.
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
103 #include <sys/param.h>
104 #include <sys/errno.h>
105 #include <sys/resource.h>
106 #include <sys/sysctl.h>
122 struct nlist namelist[] = {
133 #define IOSTAT_DEFAULT_ROWS 20 /* Traditional default `wrows' */
135 struct statinfo cur, last;
137 struct device_selection *dev_select;
139 volatile sig_atomic_t headercount;
140 volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */
141 unsigned short wrows; /* Current number of tty rows. */
142 int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0;
143 int xflag = 0, zflag = 0;
145 /* local function declarations */
146 static void usage(void);
147 static void needhdr(int signo);
148 static void needresize(int signo);
149 static void doresize(void);
150 static void phdr(void);
151 static void devstats(int perf_select, long double etime, int havelast);
152 static void cpustats(void);
153 static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr,
160 * We also support the following 'traditional' syntax:
161 * iostat [drives] [wait [count]]
162 * This isn't mentioned in the man page, or the usage statement,
163 * but it is supported.
165 fprintf(stderr, "usage: iostat [-CdhIKoTxz?] [-c count] [-M core]"
166 " [-n devs] [-N system]\n"
167 "\t [-t type,if,pass] [-w wait] [drives]\n");
171 main(int argc, char **argv)
174 int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
175 int count = 0, waittime = 0;
176 char *memf = NULL, *nlistf = NULL;
177 struct devstat_match *matches;
179 char errbuf[_POSIX2_LINE_MAX];
182 int num_devices_specified;
183 int num_selected, num_selections;
184 long select_generation;
185 char **specified_devices;
186 devstat_select_mode select_mode;
192 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) {
196 count = atoi(optarg);
198 errx(1, "count %d is < 1", count);
220 maxshowdevs = atoi(optarg);
222 errx(1, "number of devices %d is < 0",
233 if (devstat_buildmatch(optarg, &matches,
235 errx(1, "%s", devstat_errbuf);
242 waittime = atoi(optarg);
244 errx(1, "wait time is < 1");
262 if (nlistf != NULL || memf != NULL) {
263 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
266 errx(1, "kvm_openfiles: %s", errbuf);
268 if (kvm_nlist(kd, namelist) == -1)
269 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
273 * Make sure that the userland devstat version matches the kernel
274 * devstat version. If not, exit and print a message informing
275 * the user of his mistake.
277 if (devstat_checkversion(kd) < 0)
278 errx(1, "%s", devstat_errbuf);
281 * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is
282 * greater than 0, they may be 0 or non-zero.
284 if (dflag == 0 && xflag == 0) {
289 /* find out how many devices we have */
290 if ((num_devices = devstat_getnumdevs(kd)) < 0)
291 err(1, "can't get number of devices");
294 * Figure out how many devices we should display.
298 maxshowdevs = num_devices;
299 else if (oflag > 0) {
300 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
302 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
307 if ((dflag > 0) && (Cflag == 0))
314 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
315 if (cur.dinfo == NULL)
316 err(1, "malloc failed");
318 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
319 if (last.dinfo == NULL)
320 err(1, "malloc failed");
322 bzero(cur.dinfo, sizeof(struct devinfo));
323 bzero(last.dinfo, sizeof(struct devinfo));
326 * Grab all the devices. We don't look to see if the list has
327 * changed here, since it almost certainly has. We only look for
330 if (devstat_getdevs(kd, &cur) == -1)
331 errx(1, "%s", devstat_errbuf);
333 num_devices = cur.dinfo->numdevs;
334 generation = cur.dinfo->generation;
337 * If the user specified any devices on the command line, see if
338 * they are in the list of devices we have now.
340 specified_devices = (char **)malloc(sizeof(char *));
341 if (specified_devices == NULL)
342 err(1, "malloc failed");
344 for (num_devices_specified = 0; *argv; ++argv) {
347 num_devices_specified++;
348 specified_devices = (char **)realloc(specified_devices,
350 num_devices_specified);
351 if (specified_devices == NULL)
352 err(1, "realloc failed");
354 specified_devices[num_devices_specified - 1] = *argv;
357 if (nflag == 0 && maxshowdevs < num_devices_specified)
358 maxshowdevs = num_devices_specified;
362 if ((num_devices_specified == 0) && (num_matches == 0))
363 select_mode = DS_SELECT_ADD;
365 select_mode = DS_SELECT_ONLY;
368 * At this point, selectdevs will almost surely indicate that the
369 * device list has changed, so we don't look for return values of 0
370 * or 1. If we get back -1, though, there is an error.
372 if (devstat_selectdevs(&dev_select, &num_selected,
373 &num_selections, &select_generation, generation,
374 cur.dinfo->devices, num_devices, matches,
375 num_matches, specified_devices,
376 num_devices_specified, select_mode, maxshowdevs,
378 errx(1, "%s", devstat_errbuf);
381 * Look for the traditional wait time and count arguments.
384 waittime = atoi(*argv);
386 /* Let the user know he goofed, but keep going anyway */
388 warnx("discarding previous wait interval, using"
389 " %d instead", waittime);
395 warnx("discarding previous count, using %d"
403 * If the user specified a count, but not an interval, we default
404 * to an interval of 1 second.
406 if ((wflag == 0) && (cflag > 0))
410 * If the user specified a wait time, but not a count, we want to
411 * go on ad infinitum. This can be redundant if the user uses the
412 * traditional method of specifying the wait, since in that case we
413 * already set count = -1 above. Oh well.
415 if ((wflag > 0) && (cflag == 0))
418 bzero(cur.cp_time, sizeof(cur.cp_time));
423 * Set the snap time to the system boot time (ie: zero), so the
424 * stats are calculated since system boot.
429 * If the user stops the program (control-Z) and then resumes it,
430 * print out the header again.
432 (void)signal(SIGCONT, needhdr);
435 * If our standard output is a tty, then install a SIGWINCH handler
436 * and set wresized so that our first iteration through the main
437 * iostat loop will peek at the terminal's current rows to find out
438 * how many lines can fit in a screenful of output.
440 if (isatty(fileno(stdout)) != 0) {
442 (void)signal(SIGWINCH, needresize);
445 wrows = IOSTAT_DEFAULT_ROWS;
448 for (headercount = 1;;) {
449 struct devinfo *tmp_dinfo;
454 if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin,
455 sizeof(cur.tk_nin)) != 0)
456 || (readvar(kd, "kern.tty_nout", X_TK_NOUT,
457 &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) {
459 warnx("disabling TTY statistics");
465 if (readvar(kd, "kern.cp_time", 0,
466 &cur.cp_time, sizeof(cur.cp_time)) != 0)
469 if (kvm_getcptime(kd, cur.cp_time) < 0) {
470 warnx("kvm_getcptime: %s",
476 warnx("disabling CPU time statistics");
479 if (!--headercount) {
486 tmp_dinfo = last.dinfo;
487 last.dinfo = cur.dinfo;
488 cur.dinfo = tmp_dinfo;
490 last.snap_time = cur.snap_time;
493 * Here what we want to do is refresh our device stats.
494 * devstat_getdevs() returns 1 when the device list has changed.
495 * If the device list has changed, we want to go through
496 * the selection process again, in case a device that we
497 * were previously displaying has gone away.
499 switch (devstat_getdevs(kd, &cur)) {
501 errx(1, "%s", devstat_errbuf);
506 num_devices = cur.dinfo->numdevs;
507 generation = cur.dinfo->generation;
508 retval = devstat_selectdevs(&dev_select, &num_selected,
513 num_devices, matches,
516 num_devices_specified,
517 select_mode, maxshowdevs,
521 errx(1, "%s", devstat_errbuf);
539 * We only want to re-select devices if we're in 'top'
540 * mode. This is the only mode where the devices selected
541 * could actually change.
545 retval = devstat_selectdevs(&dev_select, &num_selected,
550 num_devices, matches,
553 num_devices_specified,
554 select_mode, maxshowdevs,
558 errx(1,"%s", devstat_errbuf);
573 cur.tk_nin -= last.tk_nin;
576 cur.tk_nout -= last.tk_nout;
580 etime = cur.snap_time - last.snap_time;
585 for (i = 0; i < CPUSTATES; i++) {
586 tmp = cur.cp_time[i];
587 cur.cp_time[i] -= last.cp_time[i];
588 last.cp_time[i] = tmp;
591 if (xflag == 0 && Tflag > 0)
592 printf("%4.0Lf%5.0Lf", cur.tk_nin / etime,
593 cur.tk_nout / etime);
595 devstats(hflag, etime, havelast);
605 if (count >= 0 && --count <= 0)
616 * Force a header to be prepended to the next output.
626 * When the terminal is resized, force an update of the maximum number of rows
627 * printed between each header repetition. Then force a new header to be
628 * prepended to the next output.
631 needresize(int signo)
639 * Update the global `wrows' count of terminal rows.
648 status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
649 if (status == -1 && errno == EINTR)
651 else if (status == -1)
654 wrows = w.ws_row - 3;
656 wrows = IOSTAT_DEFAULT_ROWS;
661 * Inhibit doresize() calls until we are rescheduled by SIGWINCH.
672 * If xflag is set, we need a per-loop header, not a page header, so
673 * just return. We'll print the header in devstats().
679 (void)printf(" tty");
680 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){
682 if ((dev_select[i].selected != 0)
683 && (dev_select[i].selected <= maxshowdevs)) {
684 di = dev_select[i].position;
686 (void)printf("%12.6s%d ",
687 cur.dinfo->devices[di].device_name,
688 cur.dinfo->devices[di].unit_number);
691 cur.dinfo->devices[di].device_name,
692 cur.dinfo->devices[di].unit_number);
697 (void)printf(" cpu\n");
702 (void)printf(" tin tout");
704 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){
705 if ((dev_select[i].selected != 0)
706 && (dev_select[i].selected <= maxshowdevs)) {
709 (void)printf(" sps tps msps ");
711 (void)printf(" blk xfr msps ");
714 printf(" KB/t tps MB/s ");
716 printf(" KB/t xfrs MB ");
722 (void)printf(" us ni sy in id\n");
729 devstats(int perf_select, long double etime, int havelast)
732 long double transfers_per_second, transfers_per_second_read, transfers_per_second_write;
733 long double kb_per_transfer, mb_per_second, mb_per_second_read, mb_per_second_write;
734 u_int64_t total_bytes, total_transfers, total_blocks;
735 u_int64_t total_bytes_read, total_transfers_read;
736 u_int64_t total_bytes_write, total_transfers_write;
737 long double busy_pct;
739 long double total_mb;
740 long double blocks_per_second, ms_per_transaction;
745 printf(" extended device statistics ");
753 "device r/s w/s kr/s kw/s wait svc_t %%b "
757 "device r/i w/i kr/i kw/i wait svc_t %%b "
762 printf("us ni sy in id ");
766 for (dn = 0; dn < num_devices; dn++) {
769 if (((perf_select == 0) && (dev_select[dn].selected == 0))
770 || (dev_select[dn].selected > maxshowdevs))
773 di = dev_select[dn].position;
775 if (devstat_compute_statistics(&cur.dinfo->devices[di],
776 havelast ? &last.dinfo->devices[di] : NULL, etime,
777 DSM_TOTAL_BYTES, &total_bytes,
778 DSM_TOTAL_BYTES_READ, &total_bytes_read,
779 DSM_TOTAL_BYTES_WRITE, &total_bytes_write,
780 DSM_TOTAL_TRANSFERS, &total_transfers,
781 DSM_TOTAL_TRANSFERS_READ, &total_transfers_read,
782 DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write,
783 DSM_TOTAL_BLOCKS, &total_blocks,
784 DSM_KB_PER_TRANSFER, &kb_per_transfer,
785 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
786 DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read,
787 DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write,
788 DSM_MB_PER_SECOND, &mb_per_second,
789 DSM_MB_PER_SECOND_READ, &mb_per_second_read,
790 DSM_MB_PER_SECOND_WRITE, &mb_per_second_write,
791 DSM_BLOCKS_PER_SECOND, &blocks_per_second,
792 DSM_MS_PER_TRANSACTION, &ms_per_transaction,
793 DSM_BUSY_PCT, &busy_pct,
794 DSM_QUEUE_LENGTH, &queue_len,
796 errx(1, "%s", devstat_errbuf);
798 if (perf_select != 0) {
799 dev_select[dn].bytes = total_bytes;
800 if ((dev_select[dn].selected == 0)
801 || (dev_select[dn].selected > maxshowdevs))
805 if (Kflag > 0 || xflag > 0) {
806 int block_size = cur.dinfo->devices[di].block_size;
807 total_blocks = total_blocks * (block_size ?
808 block_size : 512) / 1024;
812 if (asprintf(&devname, "%s%d",
813 cur.dinfo->devices[di].device_name,
814 cur.dinfo->devices[di].unit_number) == -1)
817 * If zflag is set, skip any devices with zero I/O.
819 if (zflag == 0 || transfers_per_second_read > 0.05 ||
820 transfers_per_second_write > 0.05 ||
821 mb_per_second_read > ((long double).0005)/1024 ||
822 mb_per_second_write > ((long double).0005)/1024 ||
825 printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4qu %5.1Lf %3.0Lf ",
826 devname, transfers_per_second_read,
827 transfers_per_second_write,
828 mb_per_second_read * 1024,
829 mb_per_second_write * 1024,
831 ms_per_transaction, busy_pct);
833 printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4qu %5.1Lf %3.0Lf ",
835 (long double)total_transfers_read,
836 (long double)total_transfers_write,
838 total_bytes_read / 1024,
840 total_bytes_write / 1024,
842 ms_per_transaction, busy_pct);
845 * If this is the first device
846 * we're printing, also print
847 * CPU or TTY stats if requested.
851 printf("%4.0Lf%5.0Lf",
853 cur.tk_nout / etime);
860 } else if (oflag > 0) {
861 int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
864 printf("%4.0Lf%4.0Lf%5.*Lf ",
866 transfers_per_second,
870 printf("%4.1qu%4.1qu%5.*Lf ",
877 printf(" %5.2Lf %3.0Lf %5.2Lf ",
879 transfers_per_second,
882 total_mb = total_bytes;
883 total_mb /= 1024 * 1024;
885 printf(" %5.2Lf %3.1qu %5.2Lf ",
892 if (xflag > 0 && zflag > 0 && firstline == 1 &&
893 (Tflag > 0 || Cflag > 0)) {
895 * If zflag is set and we did not print any device
896 * lines I/O because they were all zero,
897 * print TTY/CPU stats.
901 printf("%4.0Lf%5.0Lf", cur.tk_nin / etime,
902 cur.tk_nout / etime);
917 for (state = 0; state < CPUSTATES; ++state)
918 time += cur.cp_time[state];
919 for (state = 0; state < CPUSTATES; ++state)
921 rint(100. * cur.cp_time[state] / (time ? time : 1)));
925 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len)
930 nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len);
933 warnx("kvm_read(%s): %s", namelist[nlid].n_name,
938 warnx("kvm_read(%s): expected %zu bytes, got %zd bytes",
939 namelist[nlid].n_name, len, nbytes);
945 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
946 warn("sysctl(%s...) failed", name);
950 warnx("sysctl(%s...): expected %lu, got %lu", name,
951 (unsigned long)len, (unsigned long)nlen);