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.4 2004/03/23 07:45:34 cpressey 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>
105 #include <sys/dkstat.h>
118 struct nlist namelist[] = {
133 struct statinfo cur, last;
135 struct device_selection *dev_select;
137 int dflag = 0, Iflag = 0, Cflag = 0, Tflag = 0, oflag = 0, Kflag = 0;
139 #define nlread(x, v) \
140 kvm_read(kd, namelist[x].n_value, &(v), sizeof(v))
142 /* local function declarations */
143 static void usage(void);
144 static void phdr(int signo);
145 static void devstats(int perf_select);
146 static void cpustats(void);
152 * We also support the following 'traditional' syntax:
153 * iostat [drives] [wait [count]]
154 * This isn't mentioned in the man page, or the usage statement,
155 * but it is supported.
157 fprintf(stderr, "usage: iostat [-CdhIKoT?] [-c count] [-M core]"
158 " [-n devs] [-N system]\n"
159 "\t [-t type,if,pass] [-w wait] [drives]\n");
163 main(int argc, char **argv)
167 int tflag = 0, hflag = 0, cflag = 0, wflag = 0, nflag = 0;
168 int count = 0, waittime = 0;
169 char *memf = NULL, *nlistf = NULL;
170 struct devstat_match *matches;
172 char errbuf[_POSIX2_LINE_MAX];
177 int num_devices_specified;
178 int num_selected, num_selections;
179 long select_generation;
180 char **specified_devices;
181 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 (buildmatch(optarg, &matches,
229 errx(1, "%s", devstat_errbuf);
236 waittime = atoi(optarg);
238 errx(1, "wait time is < 1");
251 * Discard setgid privileges if not the running kernel so that bad
252 * guys can't print interesting stuff from kernel memory.
254 if (nlistf != NULL || memf != NULL)
258 * Make sure that the userland devstat version matches the kernel
259 * devstat version. If not, exit and print a message informing
260 * the user of his mistake.
262 if (checkversion() < 0)
263 errx(1, "%s", devstat_errbuf);
266 * Figure out how many devices we should display.
270 if ((dflag > 0) && (Cflag == 0) && (Tflag == 0))
272 else if ((dflag > 0) && (Tflag > 0) && (Cflag == 0))
277 if ((dflag > 0) && (Cflag == 0))
284 /* find out how many devices we have */
285 if ((num_devices = getnumdevs()) < 0)
286 err(1, "can't get number of devices");
288 if ((cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo))) ==
290 err(1, "devinfo malloc failed");
291 if ((last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo))) ==
293 err(1, "devinfo malloc failed");
294 bzero(cur.dinfo, sizeof(struct devinfo));
295 bzero(last.dinfo, sizeof(struct devinfo));
298 * Grab all the devices. We don't look to see if the list has
299 * changed here, since it almost certainly has. We only look for
302 if (getdevs(&cur) == -1)
303 errx(1, "%s", devstat_errbuf);
305 num_devices = cur.dinfo->numdevs;
306 generation = cur.dinfo->generation;
309 * If the user specified any devices on the command line, see if
310 * they are in the list of devices we have now.
312 if ((specified_devices = (char **)malloc(sizeof(char *))) == NULL)
313 err(1, "specified_devices malloc failed");
314 for (num_devices_specified = 0; *argv; ++argv) {
317 num_devices_specified++;
318 specified_devices = (char **)realloc(specified_devices,
320 num_devices_specified);
321 specified_devices[num_devices_specified - 1] = *argv;
324 if (nflag == 0 && maxshowdevs < num_devices_specified)
325 maxshowdevs = num_devices_specified;
329 if ((num_devices_specified == 0) && (num_matches == 0))
330 select_mode = DS_SELECT_ADD;
332 select_mode = DS_SELECT_ONLY;
335 * At this point, selectdevs will almost surely indicate that the
336 * device list has changed, so we don't look for return values of 0
337 * or 1. If we get back -1, though, there is an error.
339 if (selectdevs(&dev_select, &num_selected,
340 &num_selections, &select_generation,
341 generation, cur.dinfo->devices, num_devices,
342 matches, num_matches,
343 specified_devices, num_devices_specified,
344 select_mode, maxshowdevs, hflag) == -1)
345 errx(1, "%s", devstat_errbuf);
348 * Look for the traditional wait time and count arguments.
351 waittime = atoi(*argv);
353 /* Let the user know he goofed, but keep going anyway */
355 warnx("discarding previous wait interval, using"
356 " %d instead", waittime);
362 warnx("discarding previous count, using %d"
370 * If the user specified a count, but not an interval, we default
371 * to an interval of 1 second.
373 if ((wflag == 0) && (cflag > 0))
377 * If the user specified a wait time, but not a count, we want to
378 * go on ad infinitum. This can be redundant if the user uses the
379 * traditional method of specifying the wait, since in that case we
380 * already set count = -1 above. Oh well.
382 if ((wflag > 0) && (cflag == 0))
385 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
388 errx(1, "kvm_openfiles: %s", errbuf);
390 if (kvm_nlist(kd, namelist) == -1)
391 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
394 nlread(X_STATHZ, stathz);
399 * If the user stops the program (control-Z) and then resumes it,
400 * print out the header again.
402 signal(SIGCONT, phdr);
404 for (headercount = 1;;) {
405 struct devinfo *tmp_dinfo;
409 if (!--headercount) {
413 kvm_read(kd, namelist[X_TK_NIN].n_value,
414 &cur.tk_nin, sizeof(cur.tk_nin));
415 kvm_read(kd, namelist[X_TK_NOUT].n_value,
416 &cur.tk_nout, sizeof(cur.tk_nout));
417 kvm_read(kd, namelist[X_CP_TIME].n_value,
418 cur.cp_time, sizeof(cur.cp_time));
420 tmp_dinfo = last.dinfo;
421 last.dinfo = cur.dinfo;
422 cur.dinfo = tmp_dinfo;
424 last.busy_time = cur.busy_time;
427 * Here what we want to do is refresh our device stats.
428 * getdevs() returns 1 when the device list has changed.
429 * If the device list has changed, we want to go through
430 * the selection process again, in case a device that we
431 * were previously displaying has gone away.
433 switch (getdevs(&cur)) {
435 errx(1, "%s", devstat_errbuf);
440 num_devices = cur.dinfo->numdevs;
441 generation = cur.dinfo->generation;
442 retval = selectdevs(&dev_select, &num_selected,
443 &num_selections, &select_generation,
444 generation, cur.dinfo->devices,
445 num_devices, matches, num_matches,
447 num_devices_specified,
448 select_mode, maxshowdevs, hflag);
451 errx(1, "%s", devstat_errbuf);
467 * We only want to re-select devices if we're in 'top'
468 * mode. This is the only mode where the devices selected
469 * could actually change.
473 retval = selectdevs(&dev_select, &num_selected,
474 &num_selections, &select_generation,
475 generation, cur.dinfo->devices,
476 num_devices, matches, num_matches,
478 num_devices_specified,
479 select_mode, maxshowdevs, hflag);
482 errx(1,"%s", devstat_errbuf);
494 cur.tk_nin -= last.tk_nin;
497 cur.tk_nout -= last.tk_nout;
502 for (i = 0; i < CPUSTATES; i++) {
503 tmp = cur.cp_time[i];
504 cur.cp_time[i] -= last.cp_time[i];
505 last.cp_time[i] = tmp;
506 etime += cur.cp_time[i];
511 if ((dflag == 0) || (Tflag > 0))
512 printf("%4.0f%5.0f", cur.tk_nin / etime,
515 if ((dflag == 0) || (Cflag > 0))
520 if (count >= 0 && --count <= 0)
535 if ((dflag == 0) || (Tflag > 0))
537 for (i = 0, printed=0;(i < num_devices) && (printed < maxshowdevs);i++){
539 if ((dev_select[i].selected != 0)
540 && (dev_select[i].selected <= maxshowdevs)) {
541 di = dev_select[i].position;
544 cur.dinfo->devices[di].device_name,
545 cur.dinfo->devices[di].unit_number);
548 cur.dinfo->devices[di].device_name,
549 cur.dinfo->devices[di].unit_number);
553 if ((dflag == 0) || (Cflag > 0))
558 if ((dflag == 0) || (Tflag > 0))
561 for (i=0, printed = 0;(i < num_devices) && (printed < maxshowdevs);i++){
562 if ((dev_select[i].selected != 0)
563 && (dev_select[i].selected <= maxshowdevs)) {
566 printf(" sps tps msps ");
568 printf(" blk xfr msps ");
571 printf(" KB/t tps MB/s ");
573 printf(" KB/t xfrs MB ");
578 if ((dflag == 0) || (Cflag > 0))
579 printf(" us ni sy in id\n");
586 devstats(int perf_select)
589 long double transfers_per_second;
590 long double kb_per_transfer, mb_per_second;
591 u_int64_t total_bytes, total_transfers, total_blocks;
592 long double busy_seconds;
593 long double total_mb;
594 long double blocks_per_second, ms_per_transaction;
597 * Calculate elapsed time up front, since it's the same for all
600 busy_seconds = compute_etime(cur.busy_time, last.busy_time);
602 for (dn = 0; dn < num_devices; dn++) {
605 if (((perf_select == 0) && (dev_select[dn].selected == 0))
606 || (dev_select[dn].selected > maxshowdevs))
609 di = dev_select[dn].position;
611 if (compute_stats(&cur.dinfo->devices[di],
612 &last.dinfo->devices[di], busy_seconds,
613 &total_bytes, &total_transfers,
614 &total_blocks, &kb_per_transfer,
615 &transfers_per_second, &mb_per_second,
616 &blocks_per_second, &ms_per_transaction)!= 0)
617 errx(1, "%s", devstat_errbuf);
619 if (perf_select != 0) {
620 dev_select[dn].bytes = total_bytes;
621 if ((dev_select[dn].selected == 0)
622 || (dev_select[dn].selected > maxshowdevs))
627 int block_size = cur.dinfo->devices[di].block_size;
628 total_blocks = total_blocks * (block_size ?
629 block_size : 512) / 1024;
633 int msdig = (ms_per_transaction < 100.0) ? 1 : 0;
636 printf("%4.0Lf%4.0Lf%5.*Lf ",
638 transfers_per_second,
642 printf("%4.1qu%4.1qu%5.*Lf ",
649 printf(" %5.2Lf %3.0Lf %5.2Lf ",
651 transfers_per_second,
654 total_mb = total_bytes;
655 total_mb /= 1024 * 1024;
657 printf(" %5.2Lf %3.1qu %5.2Lf ",
674 for (state = 0; state < CPUSTATES; ++state)
675 time += cur.cp_time[state];
676 for (state = 0; state < CPUSTATES; ++state)
678 100. * cur.cp_time[state] / (time ? time : 1));