iostat.c revision 1.33 1 /* $NetBSD: iostat.c,v 1.33 2003/03/01 05:38:11 christos Exp $ */
2
3 /*
4 * Copyright (c) 1996 John M. Vinopal
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by John M. Vinopal.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*-
36 * Copyright (c) 1986, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
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.
54 *
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
65 * SUCH DAMAGE.
66 */
67
68 #include <sys/cdefs.h>
69 #ifndef lint
70 __COPYRIGHT("@(#) Copyright (c) 1986, 1991, 1993\n\
71 The Regents of the University of California. All rights reserved.\n");
72 #endif /* not lint */
73
74 #ifndef lint
75 #if 0
76 static char sccsid[] = "@(#)iostat.c 8.3 (Berkeley) 4/28/95";
77 #else
78 __RCSID("$NetBSD: iostat.c,v 1.33 2003/03/01 05:38:11 christos Exp $");
79 #endif
80 #endif /* not lint */
81
82 #include <sys/types.h>
83 #include <sys/sched.h>
84 #include <sys/dkstat.h>
85 #include <sys/time.h>
86
87 #include <err.h>
88 #include <ctype.h>
89 #include <signal.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <unistd.h>
94
95 #include "dkstats.h"
96
97 /* Namelist and memory files. */
98 char *nlistf, *memf;
99
100 int hz, reps, interval;
101 static int todo = 0;
102 static int ndrives;
103
104 #define ISSET(x, a) ((x) & (a))
105 #define SHOW_CPU 1<<0
106 #define SHOW_TTY 1<<1
107 #define SHOW_STATS_1 1<<2
108 #define SHOW_STATS_2 1<<3
109 #define SHOW_STATS_X 1<<4
110 #define SHOW_TOTALS 1<<7
111 #define SHOW_STATS_ALL (SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
112
113 static void cpustats(void);
114 static void disk_stats(double);
115 static void disk_stats2(double);
116 static void disk_statsx(double);
117 static void header(int);
118 static void usage(void);
119 static void display(void);
120 static void selectdrives(int, char **);
121
122 int main(int, char **);
123
124 int
125 main(int argc, char *argv[])
126 {
127 int ch, hdrcnt;
128 struct timespec tv;
129
130 while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
131 switch (ch) {
132 case 'c':
133 if ((reps = atoi(optarg)) <= 0)
134 errx(1, "repetition count <= 0.");
135 break;
136 case 'C':
137 todo |= SHOW_CPU;
138 break;
139 case 'd':
140 todo &= ~SHOW_STATS_ALL;
141 todo |= SHOW_STATS_1;
142 break;
143 case 'D':
144 todo &= ~SHOW_STATS_ALL;
145 todo |= SHOW_STATS_2;
146 break;
147 case 'I':
148 todo |= SHOW_TOTALS;
149 break;
150 case 'M':
151 memf = optarg;
152 break;
153 case 'N':
154 nlistf = optarg;
155 break;
156 case 'T':
157 todo |= SHOW_TTY;
158 break;
159 case 'w':
160 if ((interval = atoi(optarg)) <= 0)
161 errx(1, "interval <= 0.");
162 break;
163 case 'x':
164 todo &= ~SHOW_STATS_ALL;
165 todo |= SHOW_STATS_X;
166 break;
167 case '?':
168 default:
169 usage();
170 }
171 argc -= optind;
172 argv += optind;
173
174 if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
175 todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
176 if (ISSET(todo, SHOW_STATS_X)) {
177 todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
178 todo |= SHOW_STATS_X;
179 }
180
181 dkinit(0);
182 dkreadstats();
183 selectdrives(argc, argv);
184
185 tv.tv_sec = interval;
186 tv.tv_nsec = 0;
187
188 /* print a new header on sigcont */
189 (void)signal(SIGCONT, header);
190
191 for (hdrcnt = 1;;) {
192 if (!--hdrcnt) {
193 header(0);
194 hdrcnt = 20;
195 }
196
197 if (!ISSET(todo, SHOW_TOTALS))
198 dkswap();
199 display();
200
201 if (reps >= 0 && --reps <= 0)
202 break;
203 nanosleep(&tv, NULL);
204 dkreadstats();
205 }
206 exit(0);
207 }
208
209 static void
210 header(int signo)
211 {
212 int i;
213
214 /* Main Headers. */
215 if (ISSET(todo, SHOW_STATS_X)) {
216 if (ISSET(todo, SHOW_TOTALS)) {
217 (void)printf(
218 "device read KB/t xfr time MB/s");
219 (void)printf(" write KB/t xfr time MB/s\n");
220 } else {
221 (void)printf(
222 "device read KB/t r/s time MB/s");
223 (void)printf(" write KB/t w/s time MB/s\n");
224 }
225 return;
226 }
227
228 if (ISSET(todo, SHOW_TTY))
229 (void)printf(" tty");
230
231 if (ISSET(todo, SHOW_STATS_1))
232 for (i = 0; i < dk_ndrive; i++)
233 if (cur.dk_select[i])
234 (void)printf(" %7.7s ", cur.dk_name[i]);
235
236 if (ISSET(todo, SHOW_STATS_2))
237 for (i = 0; i < dk_ndrive; i++)
238 if (cur.dk_select[i])
239 (void)printf(" %7.7s ", cur.dk_name[i]);
240
241 if (ISSET(todo, SHOW_CPU))
242 (void)printf(" cpu");
243
244 printf("\n");
245
246 /* Sub-Headers. */
247 if (ISSET(todo, SHOW_TTY))
248 printf(" tin tout");
249
250 if (ISSET(todo, SHOW_STATS_1)) {
251 for (i = 0; i < dk_ndrive; i++)
252 if (cur.dk_select[i]) {
253 if (ISSET(todo, SHOW_TOTALS))
254 (void)printf(" KB/t xfr MB ");
255 else
256 (void)printf(" KB/t t/s MB/s ");
257 }
258 }
259
260 if (ISSET(todo, SHOW_STATS_2))
261 for (i = 0; i < dk_ndrive; i++)
262 if (cur.dk_select[i])
263 (void)printf(" KB xfr time ");
264
265 if (ISSET(todo, SHOW_CPU))
266 (void)printf(" us ni sy in id");
267 printf("\n");
268 }
269
270 static void
271 disk_stats(double etime)
272 {
273 int dn;
274 double atime, mbps;
275
276 for (dn = 0; dn < dk_ndrive; ++dn) {
277 if (!cur.dk_select[dn])
278 continue;
279 /* average Kbytes per transfer. */
280 if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
281 mbps = ((cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
282 1024.0) / (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]);
283 else
284 mbps = 0.0;
285 (void)printf(" %5.2f", mbps);
286
287 /* average transfers per second. */
288 (void)printf(" %3.0f",
289 (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
290
291 /* time busy in disk activity */
292 atime = (double)cur.dk_time[dn].tv_sec +
293 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
294
295 /* Megabytes per second. */
296 if (atime != 0.0)
297 mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
298 (double)(1024 * 1024);
299 else
300 mbps = 0;
301 (void)printf(" %4.2f ", mbps / etime);
302 }
303 }
304
305 static void
306 disk_stats2(double etime)
307 {
308 int dn;
309 double atime;
310
311 for (dn = 0; dn < dk_ndrive; ++dn) {
312 if (!cur.dk_select[dn])
313 continue;
314
315 /* average kbytes per second. */
316 (void)printf(" %4.0f",
317 (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) / 1024.0 / etime);
318
319 /* average transfers per second. */
320 (void)printf(" %3.0f",
321 (cur.dk_rxfer[dn] + cur.dk_wxfer[dn]) / etime);
322
323 /* average time busy in disk activity */
324 atime = (double)cur.dk_time[dn].tv_sec +
325 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
326 (void)printf(" %4.2f ", atime / etime);
327 }
328 }
329
330 static void
331 disk_statsx(double etime)
332 {
333 int dn;
334 double atime, kbps;
335
336 for (dn = 0; dn < dk_ndrive; ++dn) {
337 if (!cur.dk_select[dn])
338 continue;
339
340 (void)printf("%-8.8s", cur.dk_name[dn]);
341
342 /* average read Kbytes per transfer */
343 if (cur.dk_rxfer[dn])
344 kbps = (cur.dk_rbytes[dn] / 1024.0) / cur.dk_rxfer[dn];
345 else
346 kbps = 0.0;
347 (void)printf(" %8.2f", kbps);
348
349 /* average read transfers
350 (per second) */
351 (void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
352
353 /* time read busy in disk activity */
354 atime = (double)cur.dk_time[dn].tv_sec +
355 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
356 (void)printf(" %6.2f", atime / etime);
357
358 /* average read megabytes
359 (per second) */
360 (void)printf(" %8.2f",
361 cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
362
363
364 /* average write Kbytes per transfer */
365 if (cur.dk_wxfer[dn])
366 kbps = (cur.dk_wbytes[dn] / 1024.0) / cur.dk_wxfer[dn];
367 else
368 kbps = 0.0;
369 (void)printf(" %8.2f", kbps);
370
371 /* average write transfers
372 (per second) */
373 (void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
374
375 /* time write busy in disk activity */
376 atime = (double)cur.dk_time[dn].tv_sec +
377 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
378 (void)printf(" %6.2f", atime / etime);
379
380 /* average write megabytes
381 (per second) */
382 (void)printf(" %8.2f\n",
383 cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
384 }
385 }
386
387 static void
388 cpustats(void)
389 {
390 int state;
391 double time;
392
393 time = 0;
394 for (state = 0; state < CPUSTATES; ++state)
395 time += cur.cp_time[state];
396 if (!time)
397 time = 1.0;
398 /* States are generally never 100% and can use %3.0f. */
399 for (state = 0; state < CPUSTATES; ++state)
400 printf(" %2.0f", 100. * cur.cp_time[state] / time);
401 }
402
403 static void
404 usage(void)
405 {
406
407 (void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] "
408 "[-N system] [-w wait] [drives]\n");
409 exit(1);
410 }
411
412 static void
413 display(void)
414 {
415 double etime;
416 int newline = 1;
417
418 /* Sum up the elapsed ticks. */
419 etime = cur.cp_etime;
420
421 /* If we're showing totals only, then don't divide by the
422 * system time.
423 */
424 if (ISSET(todo, SHOW_TOTALS))
425 etime = 1.0;
426
427 if (ISSET(todo, SHOW_TTY))
428 printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
429
430 if (ISSET(todo, SHOW_STATS_1))
431 disk_stats(etime);
432
433 if (ISSET(todo, SHOW_STATS_2))
434 disk_stats2(etime);
435
436 if (ISSET(todo, SHOW_STATS_X)) {
437 disk_statsx(etime);
438 newline = 0;
439 }
440
441 if (ISSET(todo, SHOW_CPU))
442 cpustats();
443
444 if (newline)
445 (void)printf("\n");
446 (void)fflush(stdout);
447 }
448
449 static void
450 selectdrives(int argc, char *argv[])
451 {
452 int i;
453
454 /*
455 * Choose drives to be displayed. Priority goes to (in order) drives
456 * supplied as arguments and default drives. If everything isn't
457 * filled in and there are drives not taken care of, display the first
458 * few that fit.
459 *
460 * The backward compatibility #ifdefs permit the syntax:
461 * iostat [ drives ] [ interval [ count ] ]
462 */
463
464 #define BACKWARD_COMPATIBILITY
465 for (ndrives = 0; *argv; ++argv) {
466 #ifdef BACKWARD_COMPATIBILITY
467 if (isdigit(**argv))
468 break;
469 #endif
470 for (i = 0; i < dk_ndrive; i++) {
471 if (cur.dk_select[i])
472 continue;
473 if (strcmp(cur.dk_name[i], *argv) != 0)
474 continue;
475 cur.dk_select[i] = 1;
476 ++ndrives;
477 }
478 }
479 #ifdef BACKWARD_COMPATIBILITY
480 if (*argv) {
481 interval = atoi(*argv);
482 if (*++argv)
483 reps = atoi(*argv);
484 }
485 #endif
486
487 if (interval) {
488 if (!reps)
489 reps = -1;
490 } else
491 if (reps)
492 interval = 1;
493
494 /* Pick up to 4 drives if none specified. */
495 if (ndrives == 0) {
496 int maxdrives = ISSET(todo, SHOW_STATS_X) ? dk_ndrive : 4;
497 for (i = 0; i < dk_ndrive && ndrives < maxdrives; i++) {
498 if (cur.dk_select[i])
499 continue;
500 cur.dk_select[i] = 1;
501 ++ndrives;
502 }
503 }
504 }
505