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 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * Ideas for the new iostat statistics output modes taken from the NetBSD
67 * Copyright (c) 1996 John M. Vinopal
68 * All rights reserved.
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
78 * 3. All advertising materials mentioning features or use of this software
79 * must display the following acknowledgement:
80 * This product includes software developed for the NetBSD Project
82 * 4. The name of the author may not be used to endorse or promote products
83 * derived from this software without specific prior written permission.
85 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
86 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
87 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
88 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
89 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
90 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
91 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
92 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
93 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 #include <sys/param.h>
100 #include <sys/errno.h>
101 #include <sys/resource.h>
102 #include <sys/sysctl.h>
108 #include <inttypes.h>
119 struct nlist namelist[] = {
130 #define IOSTAT_DEFAULT_ROWS 20 /* Traditional default `wrows' */
132 struct statinfo cur, last;
134 struct device_selection *dev_select;
136 volatile sig_atomic_t headercount;
137 volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */
138 unsigned short wrows; /* Current number of tty rows. */
139 int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0;
140 int xflag = 0, zflag = 0;
142 /* local function declarations */
143 static void usage(void);
144 static void needhdr(int signo);
145 static void needresize(int signo);
146 static void doresize(void);
147 static void phdr(void);
148 static void devstats(int perf_select, long double etime, int havelast);
149 static void cpustats(void);
150 static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr,
157 * We also support the following 'traditional' syntax:
158 * iostat [drives] [wait [count]]
159 * This isn't mentioned in the man page, or the usage statement,
160 * but it is supported.
162 fprintf(stderr, "usage: iostat [-CdhIKoTxz?] [-c count] [-M core]"
163 " [-n devs] [-N system]\n"
164 "\t [-t type,if,pass] [-w wait] [drives]\n");
168 main(int argc, char **argv)
171 int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
172 int count = 0, waittime = 0;
173 char *memf = NULL, *nlistf = NULL;
174 struct devstat_match *matches;
176 char errbuf[_POSIX2_LINE_MAX];
179 int num_devices_specified;
180 int num_selected, num_selections;
181 long select_generation;
182 char **specified_devices;
183 devstat_select_mode select_mode;
190 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) {
194 count = atoi(optarg);
196 errx(1, "count %d is < 1", count);
218 maxshowdevs = atoi(optarg);
220 errx(1, "number of devices %d is < 0",
231 if (devstat_buildmatch(optarg, &matches,
233 errx(1, "%s", devstat_errbuf);
243 errx(1, "wait time is < 1ms");
261 if (nlistf != NULL || memf != NULL) {
262 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
265 errx(1, "kvm_openfiles: %s", errbuf);
267 if (kvm_nlist(kd, namelist) == -1)
268 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
272 * Make sure that the userland devstat version matches the kernel
273 * devstat version. If not, exit and print a message informing
274 * the user of his mistake.
276 if (devstat_checkversion(kd) < 0)
277 errx(1, "%s", devstat_errbuf);
280 * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is
281 * greater than 0, they may be 0 or non-zero.
283 if (dflag == 0 && xflag == 0) {
288 /* find out how many devices we have */
289 if ((num_devices = devstat_getnumdevs(kd)) < 0)
290 err(1, "can't get number of devices");
293 * Figure out how many devices we should display.
297 maxshowdevs = num_devices;
298 else if (oflag > 0) {
299 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
301 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
306 if ((dflag > 0) && (Cflag == 0))
313 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
314 if (cur.dinfo == NULL)
315 err(1, "calloc failed");
317 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
318 if (last.dinfo == NULL)
319 err(1, "calloc failed");
322 * Grab all the devices. We don't look to see if the list has
323 * changed here, since it almost certainly has. We only look for
326 if (devstat_getdevs(kd, &cur) == -1)
327 errx(1, "%s", devstat_errbuf);
329 num_devices = cur.dinfo->numdevs;
330 generation = cur.dinfo->generation;
333 * If the user specified any devices on the command line, see if
334 * they are in the list of devices we have now.
336 specified_devices = (char **)malloc(sizeof(char *));
337 if (specified_devices == NULL)
338 err(1, "malloc failed");
340 for (num_devices_specified = 0; *argv; ++argv) {
343 num_devices_specified++;
344 specified_devices = (char **)realloc(specified_devices,
346 num_devices_specified);
347 if (specified_devices == NULL)
348 err(1, "realloc failed");
350 specified_devices[num_devices_specified - 1] = *argv;
353 if (nflag == 0 && maxshowdevs < num_devices_specified)
354 maxshowdevs = num_devices_specified;
358 if ((num_devices_specified == 0) && (num_matches == 0))
359 select_mode = DS_SELECT_ADD;
361 select_mode = DS_SELECT_ONLY;
364 * At this point, selectdevs will almost surely indicate that the
365 * device list has changed, so we don't look for return values of 0
366 * or 1. If we get back -1, though, there is an error.
368 if (devstat_selectdevs(&dev_select, &num_selected,
369 &num_selections, &select_generation, generation,
370 cur.dinfo->devices, num_devices, matches,
371 num_matches, specified_devices,
372 num_devices_specified, select_mode, maxshowdevs,
374 errx(1, "%s", devstat_errbuf);
377 * Look for the traditional wait time and count arguments.
383 /* Let the user know he goofed, but keep going anyway */
385 warnx("discarding previous wait interval, using"
386 " %g instead", waittime / 1000.0);
392 warnx("discarding previous count, using %d"
400 * If the user specified a count, but not an interval, we default
401 * to an interval of 1 second.
403 if ((wflag == 0) && (cflag > 0))
407 * If the user specified a wait time, but not a count, we want to
408 * go on ad infinitum. This can be redundant if the user uses the
409 * traditional method of specifying the wait, since in that case we
410 * already set count = -1 above. Oh well.
412 if ((wflag > 0) && (cflag == 0))
415 bzero(cur.cp_time, sizeof(cur.cp_time));
420 * Set the snap time to the system boot time (ie: zero), so the
421 * stats are calculated since system boot.
426 * If the user stops the program (control-Z) and then resumes it,
427 * print out the header again.
429 (void)signal(SIGCONT, needhdr);
432 * If our standard output is a tty, then install a SIGWINCH handler
433 * and set wresized so that our first iteration through the main
434 * iostat loop will peek at the terminal's current rows to find out
435 * how many lines can fit in a screenful of output.
437 if (isatty(fileno(stdout)) != 0) {
439 (void)signal(SIGWINCH, needresize);
442 wrows = IOSTAT_DEFAULT_ROWS;
445 for (headercount = 1;;) {
446 struct devinfo *tmp_dinfo;
451 if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin,
452 sizeof(cur.tk_nin)) != 0)
453 || (readvar(kd, "kern.tty_nout", X_TK_NOUT,
454 &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) {
456 warnx("disabling TTY statistics");
462 if (readvar(kd, "kern.cp_time", 0,
463 &cur.cp_time, sizeof(cur.cp_time)) != 0)
466 if (kvm_getcptime(kd, cur.cp_time) < 0) {
467 warnx("kvm_getcptime: %s",
473 warnx("disabling CPU time statistics");
476 if (!--headercount) {
483 tmp_dinfo = last.dinfo;
484 last.dinfo = cur.dinfo;
485 cur.dinfo = tmp_dinfo;
487 last.snap_time = cur.snap_time;
490 * Here what we want to do is refresh our device stats.
491 * devstat_getdevs() returns 1 when the device list has changed.
492 * If the device list has changed, we want to go through
493 * the selection process again, in case a device that we
494 * were previously displaying has gone away.
496 switch (devstat_getdevs(kd, &cur)) {
498 errx(1, "%s", devstat_errbuf);
503 num_devices = cur.dinfo->numdevs;
504 generation = cur.dinfo->generation;
505 retval = devstat_selectdevs(&dev_select, &num_selected,
510 num_devices, matches,
513 num_devices_specified,
514 select_mode, maxshowdevs,
518 errx(1, "%s", devstat_errbuf);
536 * We only want to re-select devices if we're in 'top'
537 * mode. This is the only mode where the devices selected
538 * could actually change.
542 retval = devstat_selectdevs(&dev_select, &num_selected,
547 num_devices, matches,
550 num_devices_specified,
551 select_mode, maxshowdevs,
555 errx(1,"%s", devstat_errbuf);
570 cur.tk_nin -= last.tk_nin;
573 cur.tk_nout -= last.tk_nout;
577 etime = cur.snap_time - last.snap_time;
582 for (i = 0; i < CPUSTATES; i++) {
583 tmp = cur.cp_time[i];
584 cur.cp_time[i] -= last.cp_time[i];
585 last.cp_time[i] = tmp;
588 if (xflag == 0 && Tflag > 0)
589 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime,
590 cur.tk_nout / etime);
592 devstats(hflag, etime, havelast);
602 if (count >= 0 && --count <= 0)
605 usleep(waittime * 1000);
613 * Force a header to be prepended to the next output.
623 * When the terminal is resized, force an update of the maximum number of rows
624 * printed between each header repetition. Then force a new header to be
625 * prepended to the next output.
628 needresize(int signo)
636 * Update the global `wrows' count of terminal rows.
645 status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
646 if (status == -1 && errno == EINTR)
648 else if (status == -1)
651 wrows = w.ws_row - 3;
653 wrows = IOSTAT_DEFAULT_ROWS;
658 * Inhibit doresize() calls until we are rescheduled by SIGWINCH.
670 * If xflag is set, we need a per-loop header, not a page header, so
671 * just return. We'll print the header in devstats().
677 (void)printf(" tty");
678 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){
680 if ((dev_select[i].selected != 0)
681 && (dev_select[i].selected <= maxshowdevs)) {
682 di = dev_select[i].position;
683 snprintf(devbuf, sizeof(devbuf), "%s%d",
684 cur.dinfo->devices[di].device_name,
685 cur.dinfo->devices[di].unit_number);
687 (void)printf("%13.6s ", devbuf);
689 printf("%16.6s ", devbuf);
694 (void)printf(" cpu\n");
699 (void)printf(" tin tout");
701 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){
702 if ((dev_select[i].selected != 0)
703 && (dev_select[i].selected <= maxshowdevs)) {
706 (void)printf(" sps tps msps ");
708 (void)printf(" blk xfr msps ");
711 printf(" KB/t tps MB/s ");
713 printf(" KB/t xfrs MB ");
719 (void)printf(" us ni sy in id\n");
726 devstats(int perf_select, long double etime, int havelast)
729 long double transfers_per_second, transfers_per_second_read, transfers_per_second_write;
730 long double kb_per_transfer, mb_per_second, mb_per_second_read, mb_per_second_write;
731 u_int64_t total_bytes, total_transfers, total_blocks;
732 u_int64_t total_bytes_read, total_transfers_read;
733 u_int64_t total_bytes_write, total_transfers_write;
734 long double busy_pct;
736 long double total_mb;
737 long double blocks_per_second, ms_per_transaction;
742 printf(" extended device statistics ");
750 "device r/s w/s kr/s kw/s qlen svc_t %%b "
754 "device r/i w/i kr/i kw/i qlen svc_t %%b "
759 printf("us ni sy in id ");
763 for (dn = 0; dn < num_devices; dn++) {
766 if (((perf_select == 0) && (dev_select[dn].selected == 0))
767 || (dev_select[dn].selected > maxshowdevs))
770 di = dev_select[dn].position;
772 if (devstat_compute_statistics(&cur.dinfo->devices[di],
773 havelast ? &last.dinfo->devices[di] : NULL, etime,
774 DSM_TOTAL_BYTES, &total_bytes,
775 DSM_TOTAL_BYTES_READ, &total_bytes_read,
776 DSM_TOTAL_BYTES_WRITE, &total_bytes_write,
777 DSM_TOTAL_TRANSFERS, &total_transfers,
778 DSM_TOTAL_TRANSFERS_READ, &total_transfers_read,
779 DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write,
780 DSM_TOTAL_BLOCKS, &total_blocks,
781 DSM_KB_PER_TRANSFER, &kb_per_transfer,
782 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
783 DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read,
784 DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write,
785 DSM_MB_PER_SECOND, &mb_per_second,
786 DSM_MB_PER_SECOND_READ, &mb_per_second_read,
787 DSM_MB_PER_SECOND_WRITE, &mb_per_second_write,
788 DSM_BLOCKS_PER_SECOND, &blocks_per_second,
789 DSM_MS_PER_TRANSACTION, &ms_per_transaction,
790 DSM_BUSY_PCT, &busy_pct,
791 DSM_QUEUE_LENGTH, &queue_len,
793 errx(1, "%s", devstat_errbuf);
795 if (perf_select != 0) {
796 dev_select[dn].bytes = total_bytes;
797 if ((dev_select[dn].selected == 0)
798 || (dev_select[dn].selected > maxshowdevs))
802 if (Kflag > 0 || xflag > 0) {
803 int block_size = cur.dinfo->devices[di].block_size;
804 total_blocks = total_blocks * (block_size ?
805 block_size : 512) / 1024;
809 if (asprintf(&devname, "%s%d",
810 cur.dinfo->devices[di].device_name,
811 cur.dinfo->devices[di].unit_number) == -1)
814 * If zflag is set, skip any devices with zero I/O.
816 if (zflag == 0 || transfers_per_second_read > 0.05 ||
817 transfers_per_second_write > 0.05 ||
818 mb_per_second_read > ((long double).0005)/1024 ||
819 mb_per_second_write > ((long double).0005)/1024 ||
822 printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4" PRIu64 " %5.1Lf %3.0Lf ",
823 devname, transfers_per_second_read,
824 transfers_per_second_write,
825 mb_per_second_read * 1024,
826 mb_per_second_write * 1024,
828 ms_per_transaction, busy_pct);
830 printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4" PRIu64 " %5.1Lf %3.0Lf ",
832 (long double)total_transfers_read,
833 (long double)total_transfers_write,
835 total_bytes_read / 1024,
837 total_bytes_write / 1024,
839 ms_per_transaction, busy_pct);
842 * If this is the first device
843 * we're printing, also print
844 * CPU or TTY stats if requested.
848 printf("%4.0Lf%5.0Lf",
850 cur.tk_nout / etime);
857 } else if (oflag > 0) {
858 int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
861 printf("%4.0Lf%4.0Lf%5.*Lf ",
863 transfers_per_second,
867 printf("%4.1" PRIu64 "%4.1" PRIu64 "%5.*Lf ",
874 printf(" %5.2Lf %3.0Lf %5.2Lf ",
876 transfers_per_second,
879 total_mb = total_bytes;
880 total_mb /= 1024 * 1024;
882 printf(" %5.2Lf %3.1" PRIu64 " %5.2Lf ",
889 if (xflag > 0 && zflag > 0 && firstline == 1 &&
890 (Tflag > 0 || Cflag > 0)) {
892 * If zflag is set and we did not print any device
893 * lines I/O because they were all zero,
894 * print TTY/CPU stats.
898 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime,
899 cur.tk_nout / etime);
914 for (state = 0; state < CPUSTATES; ++state)
915 time += cur.cp_time[state];
916 for (state = 0; state < CPUSTATES; ++state)
918 rint(100. * cur.cp_time[state] / (time ? time : 1)));
922 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len)
927 nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len);
930 warnx("kvm_read(%s): %s", namelist[nlid].n_name,
935 warnx("kvm_read(%s): expected %zu bytes, got %zd bytes",
936 namelist[nlid].n_name, len, nbytes);
942 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
943 warn("sysctl(%s...) failed", name);
947 warnx("sysctl(%s...): expected %lu, got %lu", name,
948 (unsigned long)len, (unsigned long)nlen);