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>
120 struct nlist namelist[] = {
131 #define IOSTAT_DEFAULT_ROWS 20 /* Traditional default `wrows' */
133 struct statinfo cur, last;
135 struct device_selection *dev_select;
137 volatile sig_atomic_t headercount;
138 volatile sig_atomic_t wresized; /* Tty resized, when non-zero. */
139 volatile sig_atomic_t alarm_rang;
140 volatile sig_atomic_t return_requested;
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 needreturn(int signo);
150 static void alarm_clock(int signo);
151 static void doresize(void);
152 static void phdr(void);
153 static void devstats(int perf_select, long double etime, int havelast);
154 static void cpustats(void);
155 static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr,
162 * We also support the following 'traditional' syntax:
163 * iostat [drives] [wait [count]]
164 * This isn't mentioned in the man page, or the usage statement,
165 * but it is supported.
167 fprintf(stderr, "usage: iostat [-CdhIKoTxz?] [-c count] [-M core]"
168 " [-n devs] [-N system]\n"
169 "\t [-t type,if,pass] [-w wait] [drives]\n");
173 main(int argc, char **argv)
176 int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
177 int count = 0, waittime = 0;
178 char *memf = NULL, *nlistf = NULL;
179 struct devstat_match *matches;
180 struct itimerval alarmspec;
182 char errbuf[_POSIX2_LINE_MAX];
185 int num_devices_specified;
186 int num_selected, num_selections;
187 long select_generation;
188 char **specified_devices;
189 devstat_select_mode select_mode;
196 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:xz?")) != -1) {
200 count = atoi(optarg);
202 errx(1, "count %d is < 1", count);
224 maxshowdevs = atoi(optarg);
226 errx(1, "number of devices %d is < 0",
237 if (devstat_buildmatch(optarg, &matches,
239 errx(1, "%s", devstat_errbuf);
249 errx(1, "wait time is < 1ms");
267 if (nlistf != NULL || memf != NULL) {
268 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
271 errx(1, "kvm_openfiles: %s", errbuf);
273 if (kvm_nlist(kd, namelist) == -1)
274 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
278 * Make sure that the userland devstat version matches the kernel
279 * devstat version. If not, exit and print a message informing
280 * the user of his mistake.
282 if (devstat_checkversion(kd) < 0)
283 errx(1, "%s", devstat_errbuf);
286 * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is
287 * greater than 0, they may be 0 or non-zero.
289 if (dflag == 0 && xflag == 0) {
294 /* find out how many devices we have */
295 if ((num_devices = devstat_getnumdevs(kd)) < 0)
296 err(1, "can't get number of devices");
299 * Figure out how many devices we should display.
303 maxshowdevs = num_devices;
304 else if (oflag > 0) {
305 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
307 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
312 if ((dflag > 0) && (Cflag == 0))
319 cur.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
320 if (cur.dinfo == NULL)
321 err(1, "calloc failed");
323 last.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo));
324 if (last.dinfo == NULL)
325 err(1, "calloc failed");
328 * Grab all the devices. We don't look to see if the list has
329 * changed here, since it almost certainly has. We only look for
332 if (devstat_getdevs(kd, &cur) == -1)
333 errx(1, "%s", devstat_errbuf);
335 num_devices = cur.dinfo->numdevs;
336 generation = cur.dinfo->generation;
339 * If the user specified any devices on the command line, see if
340 * they are in the list of devices we have now.
342 specified_devices = (char **)malloc(sizeof(char *));
343 if (specified_devices == NULL)
344 err(1, "malloc failed");
346 for (num_devices_specified = 0; *argv; ++argv) {
349 num_devices_specified++;
350 specified_devices = (char **)realloc(specified_devices,
352 num_devices_specified);
353 if (specified_devices == NULL)
354 err(1, "realloc failed");
356 specified_devices[num_devices_specified - 1] = *argv;
359 if (nflag == 0 && maxshowdevs < num_devices_specified)
360 maxshowdevs = num_devices_specified;
364 if ((num_devices_specified == 0) && (num_matches == 0))
365 select_mode = DS_SELECT_ADD;
367 select_mode = DS_SELECT_ONLY;
370 * At this point, selectdevs will almost surely indicate that the
371 * device list has changed, so we don't look for return values of 0
372 * or 1. If we get back -1, though, there is an error.
374 if (devstat_selectdevs(&dev_select, &num_selected,
375 &num_selections, &select_generation, generation,
376 cur.dinfo->devices, num_devices, matches,
377 num_matches, specified_devices,
378 num_devices_specified, select_mode, maxshowdevs,
380 errx(1, "%s", devstat_errbuf);
383 * Look for the traditional wait time and count arguments.
389 /* Let the user know he goofed, but keep going anyway */
391 warnx("discarding previous wait interval, using"
392 " %g instead", waittime / 1000.0);
398 warnx("discarding previous count, using %d"
406 * If the user specified a count, but not an interval, we default
407 * to an interval of 1 second.
409 if ((wflag == 0) && (cflag > 0))
413 * If the user specified a wait time, but not a count, we want to
414 * go on ad infinitum. This can be redundant if the user uses the
415 * traditional method of specifying the wait, since in that case we
416 * already set count = -1 above. Oh well.
418 if ((wflag > 0) && (cflag == 0))
421 bzero(cur.cp_time, sizeof(cur.cp_time));
426 * Set the snap time to the system boot time (ie: zero), so the
427 * stats are calculated since system boot.
432 * If the user stops the program (control-Z) and then resumes it,
433 * print out the header again.
435 (void)signal(SIGCONT, needhdr);
438 * If our standard output is a tty, then install a SIGWINCH handler
439 * and set wresized so that our first iteration through the main
440 * iostat loop will peek at the terminal's current rows to find out
441 * how many lines can fit in a screenful of output.
443 if (isatty(fileno(stdout)) != 0) {
445 (void)signal(SIGWINCH, needresize);
448 wrows = IOSTAT_DEFAULT_ROWS;
452 * Register a SIGINT handler so that we can print out final statistics
453 * when we get that signal
455 (void)signal(SIGINT, needreturn);
458 * Register a SIGALRM handler to implement sleeps if the user uses the
461 (void)signal(SIGALRM, alarm_clock);
462 alarmspec.it_interval.tv_sec = waittime / 1000;
463 alarmspec.it_interval.tv_usec = 1000 * (waittime % 1000);
464 alarmspec.it_value.tv_sec = waittime / 1000;
465 alarmspec.it_value.tv_usec = 1000 * (waittime % 1000);
466 setitimer(ITIMER_REAL, &alarmspec, NULL);
468 for (headercount = 1;;) {
469 struct devinfo *tmp_dinfo;
472 sigset_t sigmask, oldsigmask;
475 if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin,
476 sizeof(cur.tk_nin)) != 0)
477 || (readvar(kd, "kern.tty_nout", X_TK_NOUT,
478 &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) {
480 warnx("disabling TTY statistics");
486 if (readvar(kd, "kern.cp_time", 0,
487 &cur.cp_time, sizeof(cur.cp_time)) != 0)
490 if (kvm_getcptime(kd, cur.cp_time) < 0) {
491 warnx("kvm_getcptime: %s",
497 warnx("disabling CPU time statistics");
500 if (!--headercount) {
507 tmp_dinfo = last.dinfo;
508 last.dinfo = cur.dinfo;
509 cur.dinfo = tmp_dinfo;
511 last.snap_time = cur.snap_time;
514 * Here what we want to do is refresh our device stats.
515 * devstat_getdevs() returns 1 when the device list has changed.
516 * If the device list has changed, we want to go through
517 * the selection process again, in case a device that we
518 * were previously displaying has gone away.
520 switch (devstat_getdevs(kd, &cur)) {
522 errx(1, "%s", devstat_errbuf);
527 num_devices = cur.dinfo->numdevs;
528 generation = cur.dinfo->generation;
529 retval = devstat_selectdevs(&dev_select, &num_selected,
534 num_devices, matches,
537 num_devices_specified,
538 select_mode, maxshowdevs,
542 errx(1, "%s", devstat_errbuf);
560 * We only want to re-select devices if we're in 'top'
561 * mode. This is the only mode where the devices selected
562 * could actually change.
566 retval = devstat_selectdevs(&dev_select, &num_selected,
571 num_devices, matches,
574 num_devices_specified,
575 select_mode, maxshowdevs,
579 errx(1,"%s", devstat_errbuf);
594 cur.tk_nin -= last.tk_nin;
597 cur.tk_nout -= last.tk_nout;
601 etime = cur.snap_time - last.snap_time;
606 for (i = 0; i < CPUSTATES; i++) {
607 tmp = cur.cp_time[i];
608 cur.cp_time[i] -= last.cp_time[i];
609 last.cp_time[i] = tmp;
612 if (xflag == 0 && Tflag > 0)
613 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime,
614 cur.tk_nout / etime);
616 devstats(hflag, etime, havelast);
626 if ((count >= 0 && --count <= 0) || return_requested)
630 * Use sigsuspend to safely sleep until either signal is
634 sigemptyset(&sigmask);
635 sigaddset(&sigmask, SIGINT);
636 sigaddset(&sigmask, SIGALRM);
637 sigprocmask(SIG_BLOCK, &sigmask, &oldsigmask);
638 while (! (alarm_rang || return_requested) ) {
639 sigsuspend(&oldsigmask);
641 sigprocmask(SIG_UNBLOCK, &sigmask, NULL);
650 * Force a header to be prepended to the next output.
660 * When the terminal is resized, force an update of the maximum number of rows
661 * printed between each header repetition. Then force a new header to be
662 * prepended to the next output.
665 needresize(int signo)
673 * Record the alarm so the main loop can break its sleep
676 alarm_clock(int signo)
682 * Request that the main loop exit soon
685 needreturn(int signo)
687 return_requested = 1;
691 * Update the global `wrows' count of terminal rows.
700 status = ioctl(fileno(stdout), TIOCGWINSZ, &w);
701 if (status == -1 && errno == EINTR)
703 else if (status == -1)
706 wrows = w.ws_row - 3;
708 wrows = IOSTAT_DEFAULT_ROWS;
713 * Inhibit doresize() calls until we are rescheduled by SIGWINCH.
725 * If xflag is set, we need a per-loop header, not a page header, so
726 * just return. We'll print the header in devstats().
732 (void)printf(" tty");
733 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){
735 if ((dev_select[i].selected != 0)
736 && (dev_select[i].selected <= maxshowdevs)) {
737 di = dev_select[i].position;
738 snprintf(devbuf, sizeof(devbuf), "%s%d",
739 cur.dinfo->devices[di].device_name,
740 cur.dinfo->devices[di].unit_number);
742 (void)printf("%13.6s ", devbuf);
744 printf("%16.6s ", devbuf);
749 (void)printf(" cpu\n");
754 (void)printf(" tin tout");
756 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){
757 if ((dev_select[i].selected != 0)
758 && (dev_select[i].selected <= maxshowdevs)) {
761 (void)printf(" sps tps msps ");
763 (void)printf(" blk xfr msps ");
766 printf(" KB/t tps MB/s ");
768 printf(" KB/t xfrs MB ");
774 (void)printf(" us ni sy in id\n");
781 devstats(int perf_select, long double etime, int havelast)
784 long double transfers_per_second, transfers_per_second_read, transfers_per_second_write;
785 long double kb_per_transfer, mb_per_second, mb_per_second_read, mb_per_second_write;
786 u_int64_t total_bytes, total_transfers, total_blocks;
787 u_int64_t total_bytes_read, total_transfers_read;
788 u_int64_t total_bytes_write, total_transfers_write;
789 long double busy_pct, busy_time;
791 long double total_mb;
792 long double blocks_per_second, ms_per_transaction, total_duration;
797 printf(" extended device statistics ");
804 printf("device r/s w/s kr/s kw/s qlen "
807 printf("device r/i w/i kr/i"
808 " kw/i qlen tsvc_t/i sb/i ");
813 printf("us ni sy in id ");
817 for (dn = 0; dn < num_devices; dn++) {
820 if (((perf_select == 0) && (dev_select[dn].selected == 0))
821 || (dev_select[dn].selected > maxshowdevs))
824 di = dev_select[dn].position;
826 if (devstat_compute_statistics(&cur.dinfo->devices[di],
827 havelast ? &last.dinfo->devices[di] : NULL, etime,
828 DSM_TOTAL_BYTES, &total_bytes,
829 DSM_TOTAL_BYTES_READ, &total_bytes_read,
830 DSM_TOTAL_BYTES_WRITE, &total_bytes_write,
831 DSM_TOTAL_TRANSFERS, &total_transfers,
832 DSM_TOTAL_TRANSFERS_READ, &total_transfers_read,
833 DSM_TOTAL_TRANSFERS_WRITE, &total_transfers_write,
834 DSM_TOTAL_BLOCKS, &total_blocks,
835 DSM_KB_PER_TRANSFER, &kb_per_transfer,
836 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
837 DSM_TRANSFERS_PER_SECOND_READ, &transfers_per_second_read,
838 DSM_TRANSFERS_PER_SECOND_WRITE, &transfers_per_second_write,
839 DSM_MB_PER_SECOND, &mb_per_second,
840 DSM_MB_PER_SECOND_READ, &mb_per_second_read,
841 DSM_MB_PER_SECOND_WRITE, &mb_per_second_write,
842 DSM_BLOCKS_PER_SECOND, &blocks_per_second,
843 DSM_MS_PER_TRANSACTION, &ms_per_transaction,
844 DSM_BUSY_PCT, &busy_pct,
845 DSM_QUEUE_LENGTH, &queue_len,
846 DSM_TOTAL_DURATION, &total_duration,
847 DSM_TOTAL_BUSY_TIME, &busy_time,
849 errx(1, "%s", devstat_errbuf);
851 if (perf_select != 0) {
852 dev_select[dn].bytes = total_bytes;
853 if ((dev_select[dn].selected == 0)
854 || (dev_select[dn].selected > maxshowdevs))
858 if (Kflag > 0 || xflag > 0) {
859 int block_size = cur.dinfo->devices[di].block_size;
860 total_blocks = total_blocks * (block_size ?
861 block_size : 512) / 1024;
865 if (asprintf(&devname, "%s%d",
866 cur.dinfo->devices[di].device_name,
867 cur.dinfo->devices[di].unit_number) == -1)
870 * If zflag is set, skip any devices with zero I/O.
872 if (zflag == 0 || transfers_per_second_read > 0.05 ||
873 transfers_per_second_write > 0.05 ||
874 mb_per_second_read > ((long double).0005)/1024 ||
875 mb_per_second_write > ((long double).0005)/1024 ||
878 printf("%-8.8s %5.1Lf %5.1Lf %7.1Lf %7.1Lf %4" PRIu64 " %5.1Lf %3.0Lf ",
879 devname, transfers_per_second_read,
880 transfers_per_second_write,
881 mb_per_second_read * 1024,
882 mb_per_second_write * 1024,
884 ms_per_transaction, busy_pct);
886 printf("%-8.8s %11.1Lf %11.1Lf "
887 "%12.1Lf %12.1Lf %4" PRIu64
890 (long double)total_transfers_read,
891 (long double)total_transfers_write,
893 total_bytes_read / 1024,
895 total_bytes_write / 1024,
897 total_duration, busy_time);
900 * If this is the first device
901 * we're printing, also print
902 * CPU or TTY stats if requested.
906 printf("%4.0Lf%5.0Lf",
908 cur.tk_nout / etime);
915 } else if (oflag > 0) {
916 int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
919 printf("%4.0Lf%4.0Lf%5.*Lf ",
921 transfers_per_second,
925 printf("%4.1" PRIu64 "%4.1" PRIu64 "%5.*Lf ",
932 printf(" %5.2Lf %3.0Lf %5.2Lf ",
934 transfers_per_second,
937 total_mb = total_bytes;
938 total_mb /= 1024 * 1024;
940 printf(" %5.2Lf %3.1" PRIu64 " %5.2Lf ",
947 if (xflag > 0 && zflag > 0 && firstline == 1 &&
948 (Tflag > 0 || Cflag > 0)) {
950 * If zflag is set and we did not print any device
951 * lines I/O because they were all zero,
952 * print TTY/CPU stats.
956 printf("%4.0Lf %5.0Lf", cur.tk_nin / etime,
957 cur.tk_nout / etime);
972 for (state = 0; state < CPUSTATES; ++state)
973 time += cur.cp_time[state];
974 for (state = 0; state < CPUSTATES; ++state)
976 rint(100. * cur.cp_time[state] / (time ? time : 1)));
980 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len)
985 nbytes = kvm_read(kd, namelist[nlid].n_value, ptr, len);
988 warnx("kvm_read(%s): %s", namelist[nlid].n_name,
993 warnx("kvm_read(%s): expected %zu bytes, got %zd bytes",
994 namelist[nlid].n_name, len, nbytes);
1000 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
1001 warn("sysctl(%s...) failed", name);
1005 warnx("sysctl(%s...): expected %lu, got %lu", name,
1006 (unsigned long)len, (unsigned long)nlen);