2 * Copyright (c) 1997, 1998 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
28 * $FreeBSD: src/usr.sbin/iostat/iostat.c,v 1.17.2.2 2001/07/19 04:15:42 kris Exp $
29 * $DragonFly: src/usr.sbin/iostat/iostat.c,v 1.5 2004/12/22 11:01:49 joerg Exp $
32 * Parts of this program are derived from the original FreeBSD iostat
36 * Copyright (c) 1986, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * Ideas for the new iostat statistics output modes taken from the NetBSD
72 * Copyright (c) 1996 John M. Vinopal
73 * All rights reserved.
75 * Redistribution and use in source and binary forms, with or without
76 * modification, are permitted provided that the following conditions
78 * 1. Redistributions of source code must retain the above copyright
79 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in the
82 * documentation and/or other materials provided with the distribution.
83 * 3. All advertising materials mentioning features or use of this software
84 * must display the following acknowledgement:
85 * This product includes software developed for the NetBSD Project
87 * 4. The name of the author may not be used to endorse or promote products
88 * derived from this software without specific prior written permission.
90 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
91 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
92 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
93 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
94 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
95 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
96 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
97 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
98 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
99 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
103 #include <sys/param.h>
104 #include <sys/errno.h>
117 struct statinfo cur, last;
118 uint64_t tk_nin, old_tk_nin, diff_tk_nin;
119 uint64_t tk_nout, old_tk_nout, diff_tk_nout;
120 struct kinfo_cputime cp_time, old_cp_time, diff_cp_time;
121 double cp_time_total;
123 struct device_selection *dev_select;
125 int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0;
127 /* local function declarations */
128 static void usage(void);
129 static void phdr(int signo);
130 static void devstats(int perf_select);
131 static void cpustats(void);
137 * We also support the following 'traditional' syntax:
138 * iostat [drives] [wait [count]]
139 * This isn't mentioned in the man page, or the usage statement,
140 * but it is supported.
142 fprintf(stderr, "usage: iostat [-CdhIKoT?] [-c count]"
144 "\t [-t type,if,pass] [-w wait] [drives]\n");
148 main(int argc, char **argv)
152 int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
153 int count = 0, waittime = 0;
154 struct devstat_match *matches;
156 char errbuf[_POSIX2_LINE_MAX];
160 int num_devices_specified;
161 int num_selected, num_selections;
162 long select_generation;
163 char **specified_devices;
164 devstat_select_mode select_mode;
169 while ((c = getopt(argc, argv, "c:CdhIKM:n:N:ot:Tw:?")) != -1) {
173 count = atoi(optarg);
175 errx(1, "count %d is < 1", count);
194 maxshowdevs = atoi(optarg);
196 errx(1, "number of devices %d is < 0",
204 if (buildmatch(optarg, &matches,
206 errx(1, "%s", devstat_errbuf);
213 waittime = atoi(optarg);
215 errx(1, "wait time is < 1");
228 * Make sure that the userland devstat version matches the kernel
229 * devstat version. If not, exit and print a message informing
230 * the user of his mistake.
232 if (checkversion() < 0)
233 errx(1, "%s", devstat_errbuf);
236 * Figure out how many devices we should display.
240 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
242 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
247 if ((dflag > 0) && (Cflag == 0))
254 /* find out how many devices we have */
255 if ((num_devices = getnumdevs()) < 0)
256 err(1, "can't get number of devices");
258 if ((cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo))) ==
260 err(1, "devinfo malloc failed");
261 if ((last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo))) ==
263 err(1, "devinfo malloc failed");
264 bzero(cur.dinfo, sizeof(struct devinfo));
265 bzero(last.dinfo, sizeof(struct devinfo));
268 * Grab all the devices. We don't look to see if the list has
269 * changed here, since it almost certainly has. We only look for
272 if (getdevs(&cur) == -1)
273 errx(1, "%s", devstat_errbuf);
275 num_devices = cur.dinfo->numdevs;
276 generation = cur.dinfo->generation;
279 * If the user specified any devices on the command line, see if
280 * they are in the list of devices we have now.
282 if ((specified_devices = (char **)malloc(sizeof(char *))) == NULL)
283 err(1, "specified_devices malloc failed");
284 for (num_devices_specified = 0; *argv; ++argv) {
287 num_devices_specified++;
288 specified_devices = (char **)realloc(specified_devices,
290 num_devices_specified);
291 specified_devices[num_devices_specified - 1] = *argv;
294 if (nflag == 0 && maxshowdevs < num_devices_specified)
295 maxshowdevs = num_devices_specified;
299 if ((num_devices_specified == 0) && (num_matches == 0))
300 select_mode = DS_SELECT_ADD;
302 select_mode = DS_SELECT_ONLY;
305 * At this point, selectdevs will almost surely indicate that the
306 * device list has changed, so we don't look for return values of 0
307 * or 1. If we get back -1, though, there is an error.
309 if (selectdevs(&dev_select, &num_selected,
310 &num_selections, &select_generation,
311 generation, cur.dinfo->devices, num_devices,
312 matches, num_matches,
313 specified_devices, num_devices_specified,
314 select_mode, maxshowdevs, hflag) == -1)
315 errx(1, "%s", devstat_errbuf);
318 * Look for the traditional wait time and count arguments.
321 waittime = atoi(*argv);
323 /* Let the user know he goofed, but keep going anyway */
325 warnx("discarding previous wait interval, using"
326 " %d instead", waittime);
332 warnx("discarding previous count, using %d"
340 * If the user specified a count, but not an interval, we default
341 * to an interval of 1 second.
343 if ((wflag == 0) && (cflag > 0))
347 * If the user specified a wait time, but not a count, we want to
348 * go on ad infinitum. This can be redundant if the user uses the
349 * traditional method of specifying the wait, since in that case we
350 * already set count = -1 above. Oh well.
352 if ((wflag > 0) && (cflag == 0))
355 if (kinfo_get_sched_hz(&hz))
356 err(1, "kinfo_get_sched_hz");
357 if (kinfo_get_sched_stathz(&hz))
358 err(1, "kinfo_get_sched_stathz");
361 * If the user stops the program (control-Z) and then resumes it,
362 * print out the header again.
364 signal(SIGCONT, phdr);
366 for (headercount = 1;;) {
367 struct devinfo *tmp_dinfo;
370 if (!--headercount) {
374 if (kinfo_get_tty_tk_nin(&tk_nin))
375 err(1, "kinfo_get_tty_tk_nin");
376 if (kinfo_get_tty_tk_nout(&tk_nout))
377 err(1, "kinfo_get_tty_tk_nout");
378 if (kinfo_get_sched_cputime(&cp_time))
379 err(1, "kinfo_get_sched_cputime");
381 tmp_dinfo = last.dinfo;
382 last.dinfo = cur.dinfo;
383 cur.dinfo = tmp_dinfo;
385 last.busy_time = cur.busy_time;
388 * Here what we want to do is refresh our device stats.
389 * getdevs() returns 1 when the device list has changed.
390 * If the device list has changed, we want to go through
391 * the selection process again, in case a device that we
392 * were previously displaying has gone away.
394 switch (getdevs(&cur)) {
396 errx(1, "%s", devstat_errbuf);
401 num_devices = cur.dinfo->numdevs;
402 generation = cur.dinfo->generation;
403 retval = selectdevs(&dev_select, &num_selected,
404 &num_selections, &select_generation,
405 generation, cur.dinfo->devices,
406 num_devices, matches, num_matches,
408 num_devices_specified,
409 select_mode, maxshowdevs, hflag);
412 errx(1, "%s", devstat_errbuf);
428 * We only want to re-select devices if we're in 'top'
429 * mode. This is the only mode where the devices selected
430 * could actually change.
434 retval = selectdevs(&dev_select, &num_selected,
435 &num_selections, &select_generation,
436 generation, cur.dinfo->devices,
437 num_devices, matches, num_matches,
439 num_devices_specified,
440 select_mode, maxshowdevs, hflag);
443 errx(1,"%s", devstat_errbuf);
454 diff_tk_nin = tk_nin - old_tk_nin;
456 diff_tk_nout = tk_nout - old_tk_nout;
457 old_tk_nout = tk_nout;
459 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
460 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
461 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
462 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
463 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
464 cp_time_total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
465 diff_cp_time.cp_sys + diff_cp_time.cp_intr +
466 diff_cp_time.cp_idle;
467 old_cp_time = cp_time;
469 if (cp_time_total == 0.0)
472 if ((dflag == 0) || (Tflag > 0))
473 printf("%4.0f%5.0f", diff_tk_nin / cp_time_total * 1e6,
474 diff_tk_nout / cp_time_total * 1e6);
476 if ((dflag == 0) || (Cflag > 0))
481 if (count >= 0 && --count <= 0)
496 if ((dflag == 0) || (Tflag > 0))
498 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){
500 if ((dev_select[i].selected != 0)
501 && (dev_select[i].selected <= maxshowdevs)) {
502 di = dev_select[i].position;
505 cur.dinfo->devices[di].device_name,
506 cur.dinfo->devices[di].unit_number);
509 cur.dinfo->devices[di].device_name,
510 cur.dinfo->devices[di].unit_number);
514 if ((dflag == 0) || (Cflag > 0))
519 if ((dflag == 0) || (Tflag > 0))
522 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){
523 if ((dev_select[i].selected != 0)
524 && (dev_select[i].selected <= maxshowdevs)) {
527 printf(" sps tps msps ");
529 printf(" blk xfr msps ");
532 printf(" KB/t tps MB/s ");
534 printf(" KB/t xfrs MB ");
539 if ((dflag == 0) || (Cflag > 0))
540 printf(" us ni sy in id\n");
547 devstats(int perf_select)
550 long double transfers_per_second;
551 long double kb_per_transfer, mb_per_second;
552 u_int64_t total_bytes, total_transfers, total_blocks;
553 long double busy_seconds;
554 long double total_mb;
555 long double blocks_per_second, ms_per_transaction;
558 * Calculate elapsed time up front, since it's the same for all
561 busy_seconds = compute_etime(cur.busy_time, last.busy_time);
563 for (dn = 0; dn < num_devices; dn++) {
566 if (((perf_select == 0) && (dev_select[dn].selected == 0))
567 || (dev_select[dn].selected > maxshowdevs))
570 di = dev_select[dn].position;
572 if (compute_stats(&cur.dinfo->devices[di],
573 &last.dinfo->devices[di], busy_seconds,
574 &total_bytes, &total_transfers,
575 &total_blocks, &kb_per_transfer,
576 &transfers_per_second, &mb_per_second,
577 &blocks_per_second, &ms_per_transaction)!= 0)
578 errx(1, "%s", devstat_errbuf);
580 if (perf_select != 0) {
581 dev_select[dn].bytes = total_bytes;
582 if ((dev_select[dn].selected == 0)
583 || (dev_select[dn].selected > maxshowdevs))
588 int block_size = cur.dinfo->devices[di].block_size;
589 total_blocks = total_blocks * (block_size ?
590 block_size : 512) / 1024;
594 int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
597 printf("%4.0Lf%4.0Lf%5.*Lf ",
599 transfers_per_second,
603 printf("%4.1qu%4.1qu%5.*Lf ",
610 printf(" %5.2Lf %3.0Lf %5.2Lf ",
612 transfers_per_second,
615 total_mb = total_bytes;
616 total_mb /= 1024 * 1024;
618 printf(" %5.2Lf %3.1qu %5.2Lf ",
632 if (cp_time_total == 0.0)
635 printf(" %2.0f", 100. * diff_cp_time.cp_user / cp_time_total);
636 printf(" %2.0f", 100. * diff_cp_time.cp_nice / cp_time_total);
637 printf(" %2.0f", 100. * diff_cp_time.cp_sys / cp_time_total);
638 printf(" %2.0f", 100. * diff_cp_time.cp_intr / cp_time_total);
639 printf(" %2.0f", 100. * diff_cp_time.cp_idle / cp_time_total);