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/dkstat.h>
106 #include <sys/sysctl.h>
120 struct nlist namelist[] = {
133 struct statinfo cur, last;
135 struct device_selection *dev_select;
137 volatile sig_atomic_t headercount;
138 int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0;
140 /* local function declarations */
141 static void usage(void);
142 static void needhdr(int signo);
143 static void phdr(void);
144 static void devstats(int perf_select, long double etime, int havelast);
145 static void cpustats(void);
146 static int readvar(kvm_t *kd, const char *name, int nlid, void *ptr,
153 * We also support the following 'traditional' syntax:
154 * iostat [drives] [wait [count]]
155 * This isn't mentioned in the man page, or the usage statement,
156 * but it is supported.
158 fprintf(stderr, "usage: iostat [-CdhIKoT?] [-c count] [-M core]"
159 " [-n devs] [-N system]\n"
160 "\t [-t type,if,pass] [-w wait] [drives]\n");
164 main(int argc, char **argv)
168 int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
169 int count = 0, waittime = 0;
170 char *memf = NULL, *nlistf = NULL;
171 struct devstat_match *matches;
173 char errbuf[_POSIX2_LINE_MAX];
176 int num_devices_specified;
177 int num_selected, num_selections;
178 long select_generation;
179 char **specified_devices;
180 devstat_select_mode select_mode;
186 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:?")) != -1) {
190 count = atoi(optarg);
192 errx(1, "count %d is < 1", count);
214 maxshowdevs = atoi(optarg);
216 errx(1, "number of devices %d is < 0",
227 if (devstat_buildmatch(optarg, &matches,
229 errx(1, "%s", devstat_errbuf);
236 waittime = atoi(optarg);
238 errx(1, "wait time is < 1");
250 if (nlistf != NULL || memf != NULL) {
251 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
254 errx(1, "kvm_openfiles: %s", errbuf);
256 if (kvm_nlist(kd, namelist) == -1)
257 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
261 * Make sure that the userland devstat version matches the kernel
262 * devstat version. If not, exit and print a message informing
263 * the user of his mistake.
265 if (devstat_checkversion(kd) < 0)
266 errx(1, "%s", devstat_errbuf);
269 * Make sure Tflag and/or Cflag are set if dflag == 0. If dflag is
270 * greater than 0, they may be 0 or non-zero.
278 * Figure out how many devices we should display.
282 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
284 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
289 if ((dflag > 0) && (Cflag == 0))
296 /* find out how many devices we have */
297 if ((num_devices = devstat_getnumdevs(kd)) < 0)
298 err(1, "can't get number of devices");
300 cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
301 if (cur.dinfo == NULL)
302 err(1, "malloc failed");
304 last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
305 if (last.dinfo == NULL)
306 err(1, "malloc failed");
308 bzero(cur.dinfo, sizeof(struct devinfo));
309 bzero(last.dinfo, sizeof(struct devinfo));
312 * Grab all the devices. We don't look to see if the list has
313 * changed here, since it almost certainly has. We only look for
316 if (devstat_getdevs(kd, &cur) == -1)
317 errx(1, "%s", devstat_errbuf);
319 num_devices = cur.dinfo->numdevs;
320 generation = cur.dinfo->generation;
323 * If the user specified any devices on the command line, see if
324 * they are in the list of devices we have now.
326 specified_devices = (char **)malloc(sizeof(char *));
327 if (specified_devices == NULL)
328 err(1, "malloc failed");
330 for (num_devices_specified = 0; *argv; ++argv) {
333 num_devices_specified++;
334 specified_devices = (char **)realloc(specified_devices,
336 num_devices_specified);
337 if (specified_devices == NULL)
338 err(1, "realloc failed");
340 specified_devices[num_devices_specified - 1] = *argv;
343 if (nflag == 0 && maxshowdevs < num_devices_specified)
344 maxshowdevs = num_devices_specified;
348 if ((num_devices_specified == 0) && (num_matches == 0))
349 select_mode = DS_SELECT_ADD;
351 select_mode = DS_SELECT_ONLY;
354 * At this point, selectdevs will almost surely indicate that the
355 * device list has changed, so we don't look for return values of 0
356 * or 1. If we get back -1, though, there is an error.
358 if (devstat_selectdevs(&dev_select, &num_selected,
359 &num_selections, &select_generation, generation,
360 cur.dinfo->devices, num_devices, matches,
361 num_matches, specified_devices,
362 num_devices_specified, select_mode, maxshowdevs,
364 errx(1, "%s", devstat_errbuf);
367 * Look for the traditional wait time and count arguments.
370 waittime = atoi(*argv);
372 /* Let the user know he goofed, but keep going anyway */
374 warnx("discarding previous wait interval, using"
375 " %d instead", waittime);
381 warnx("discarding previous count, using %d"
389 * If the user specified a count, but not an interval, we default
390 * to an interval of 1 second.
392 if ((wflag == 0) && (cflag > 0))
396 * If the user specified a wait time, but not a count, we want to
397 * go on ad infinitum. This can be redundant if the user uses the
398 * traditional method of specifying the wait, since in that case we
399 * already set count = -1 above. Oh well.
401 if ((wflag > 0) && (cflag == 0))
404 bzero(&cur.cp_time, sizeof(cur.cp_time));
409 * Set the busy time to the system boot time, so the stats are
410 * calculated since system boot.
412 if (readvar(kd, "kern.boottime", X_BOOTTIME, &cur.busy_time,
413 sizeof(cur.busy_time)) != 0)
417 * If the user stops the program (control-Z) and then resumes it,
418 * print out the header again.
420 (void)signal(SIGCONT, needhdr);
422 for (headercount = 1;;) {
423 struct devinfo *tmp_dinfo;
428 if ((readvar(kd, "kern.tty_nin", X_TK_NIN, &cur.tk_nin,
429 sizeof(cur.tk_nin)) != 0)
430 || (readvar(kd, "kern.tty_nout", X_TK_NOUT,
431 &cur.tk_nout, sizeof(cur.tk_nout))!= 0)) {
433 warnx("disabling TTY statistics");
438 if (readvar(kd, "kern.cp_time", X_CP_TIME,
439 &cur.cp_time, sizeof(cur.cp_time)) != 0) {
441 warnx("disabling CPU time statistics");
445 if (!--headercount) {
450 tmp_dinfo = last.dinfo;
451 last.dinfo = cur.dinfo;
452 cur.dinfo = tmp_dinfo;
454 last.busy_time = cur.busy_time;
457 * Here what we want to do is refresh our device stats.
458 * devstat_getdevs() returns 1 when the device list has changed.
459 * If the device list has changed, we want to go through
460 * the selection process again, in case a device that we
461 * were previously displaying has gone away.
463 switch (devstat_getdevs(kd, &cur)) {
465 errx(1, "%s", devstat_errbuf);
470 num_devices = cur.dinfo->numdevs;
471 generation = cur.dinfo->generation;
472 retval = devstat_selectdevs(&dev_select, &num_selected,
477 num_devices, matches,
480 num_devices_specified,
481 select_mode, maxshowdevs,
485 errx(1, "%s", devstat_errbuf);
501 * We only want to re-select devices if we're in 'top'
502 * mode. This is the only mode where the devices selected
503 * could actually change.
507 retval = devstat_selectdevs(&dev_select, &num_selected,
512 num_devices, matches,
515 num_devices_specified,
516 select_mode, maxshowdevs,
520 errx(1,"%s", devstat_errbuf);
533 cur.tk_nin -= last.tk_nin;
536 cur.tk_nout -= last.tk_nout;
540 etime = devstat_compute_etime(cur.busy_time, last.busy_time);
545 for (i = 0; i < CPUSTATES; i++) {
546 tmp = cur.cp_time[i];
547 cur.cp_time[i] -= last.cp_time[i];
548 last.cp_time[i] = tmp;
552 printf("%4.0Lf%5.0Lf", cur.tk_nin / etime,
555 devstats(hflag, etime, havelast);
563 if (count >= 0 && --count <= 0)
574 * Force a header to be prepended to the next output.
590 (void)printf(" tty");
591 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){
593 if ((dev_select[i].selected != 0)
594 && (dev_select[i].selected <= maxshowdevs)) {
595 di = dev_select[i].position;
597 (void)printf("%12.6s%d ",
598 cur.dinfo->devices[di].device_name,
599 cur.dinfo->devices[di].unit_number);
602 cur.dinfo->devices[di].device_name,
603 cur.dinfo->devices[di].unit_number);
608 (void)printf(" cpu\n");
613 (void)printf(" tin tout");
615 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){
616 if ((dev_select[i].selected != 0)
617 && (dev_select[i].selected <= maxshowdevs)) {
620 (void)printf(" sps tps msps ");
622 (void)printf(" blk xfr msps ");
625 printf(" KB/t tps MB/s ");
627 printf(" KB/t xfrs MB ");
633 (void)printf(" us ni sy in id\n");
640 devstats(int perf_select, long double etime, int havelast)
643 long double transfers_per_second;
644 long double kb_per_transfer, mb_per_second;
645 u_int64_t total_bytes, total_transfers, total_blocks;
646 long double total_mb;
647 long double blocks_per_second, ms_per_transaction;
649 for (dn = 0; dn < num_devices; dn++) {
652 if (((perf_select == 0) && (dev_select[dn].selected == 0))
653 || (dev_select[dn].selected > maxshowdevs))
656 di = dev_select[dn].position;
658 if (devstat_compute_statistics(&cur.dinfo->devices[di],
659 havelast ? &last.dinfo->devices[di] : NULL, etime,
660 DSM_TOTAL_BYTES, &total_bytes,
661 DSM_TOTAL_TRANSFERS, &total_transfers,
662 DSM_TOTAL_BLOCKS, &total_blocks,
663 DSM_KB_PER_TRANSFER, &kb_per_transfer,
664 DSM_TRANSFERS_PER_SECOND, &transfers_per_second,
665 DSM_MB_PER_SECOND, &mb_per_second,
666 DSM_BLOCKS_PER_SECOND, &blocks_per_second,
667 DSM_MS_PER_TRANSACTION, &ms_per_transaction,
669 errx(1, "%s", devstat_errbuf);
671 if (perf_select != 0) {
672 dev_select[dn].bytes = total_bytes;
673 if ((dev_select[dn].selected == 0)
674 || (dev_select[dn].selected > maxshowdevs))
679 int block_size = cur.dinfo->devices[di].block_size;
680 total_blocks = total_blocks * (block_size ?
681 block_size : 512) / 1024;
685 int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
688 printf("%4.0Lf%4.0Lf%5.*Lf ",
690 transfers_per_second,
694 printf("%4.1qu%4.1qu%5.*Lf ",
701 printf(" %5.2Lf %3.0Lf %5.2Lf ",
703 transfers_per_second,
706 total_mb = total_bytes;
707 total_mb /= 1024 * 1024;
709 printf(" %5.2Lf %3.1qu %5.2Lf ",
726 for (state = 0; state < CPUSTATES; ++state)
727 time += cur.cp_time[state];
728 for (state = 0; state < CPUSTATES; ++state)
730 rint(100. * cur.cp_time[state] / (time ? time : 1)));
734 readvar(kvm_t *kd, const char *name, int nlid, void *ptr, size_t len)
739 nbytes = kvm_read(kd, nlid, ptr, len);
742 warnx("kvm_read(%s): %s", name, kvm_geterr(kd));
746 warnx("kvm_read(%s): expected %lu bytes, got %ld bytes",
747 name, (unsigned long)len, (long)nbytes);
753 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
754 warn("sysctl(%s...) failed", name);
758 warnx("sysctl(%s...): expected %lu, got %lu", name,
759 (unsigned long)len, (unsigned long)nlen);