iostat.c revision 1.30 1 /* $NetBSD: iostat.c,v 1.30 2002/11/01 14:02:21 simonb 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.30 2002/11/01 14:02:21 simonb 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
103 #define ISSET(x, a) ((x) & (a))
104 #define SHOW_CPU 1<<0
105 #define SHOW_TTY 1<<1
106 #define SHOW_STATS_1 1<<2
107 #define SHOW_STATS_2 1<<3
108 #define SHOW_STATS_X 1<<4
109 #define SHOW_TOTALS 1<<7
110 #define SHOW_STATS_ALL (SHOW_STATS_1 | SHOW_STATS_2 | SHOW_STATS_X)
111
112 static void cpustats(void);
113 static void disk_stats(double);
114 static void disk_stats2(double);
115 static void disk_statsx(double);
116 static void header(int);
117 static void usage(void);
118 static void display(void);
119 static void selectdrives(int, char **);
120
121 int main(int, char **);
122
123 int
124 main(int argc, char *argv[])
125 {
126 int ch, hdrcnt;
127 struct timespec tv;
128
129 while ((ch = getopt(argc, argv, "Cc:dDIM:N:Tw:x")) != -1)
130 switch(ch) {
131 case 'c':
132 if ((reps = atoi(optarg)) <= 0)
133 errx(1, "repetition count <= 0.");
134 break;
135 case 'C':
136 todo |= SHOW_CPU;
137 break;
138 case 'd':
139 todo &= ~SHOW_STATS_ALL;
140 todo |= SHOW_STATS_1;
141 break;
142 case 'D':
143 todo &= ~SHOW_STATS_ALL;
144 todo |= SHOW_STATS_2;
145 break;
146 case 'I':
147 todo |= SHOW_TOTALS;
148 break;
149 case 'M':
150 memf = optarg;
151 break;
152 case 'N':
153 nlistf = optarg;
154 break;
155 case 'T':
156 todo |= SHOW_TTY;
157 break;
158 case 'w':
159 if ((interval = atoi(optarg)) <= 0)
160 errx(1, "interval <= 0.");
161 break;
162 case 'x':
163 todo &= ~SHOW_STATS_ALL;
164 todo |= SHOW_STATS_X;
165 break;
166 case '?':
167 default:
168 usage();
169 }
170 argc -= optind;
171 argv += optind;
172
173 if (!ISSET(todo, SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL))
174 todo |= SHOW_CPU | SHOW_TTY | SHOW_STATS_1;
175 if (ISSET(todo, SHOW_STATS_X)) {
176 todo &= ~(SHOW_CPU | SHOW_TTY | SHOW_STATS_ALL);
177 todo |= SHOW_STATS_X;
178 }
179
180 dkinit(0);
181 dkreadstats();
182 selectdrives(argc, argv);
183
184 tv.tv_sec = interval;
185 tv.tv_nsec = 0;
186
187 /* print a new header on sigcont */
188 (void)signal(SIGCONT, header);
189
190 for (hdrcnt = 1;;) {
191 if (!--hdrcnt) {
192 header(0);
193 hdrcnt = 20;
194 }
195
196 if (!ISSET(todo, SHOW_TOTALS))
197 dkswap();
198 display();
199
200 if (reps >= 0 && --reps <= 0)
201 break;
202 nanosleep(&tv, NULL);
203 dkreadstats();
204 }
205 exit(0);
206 }
207
208 static void
209 header(int signo)
210 {
211 int i;
212
213 /* Main Headers. */
214 if (ISSET(todo, SHOW_STATS_X)) {
215 if (ISSET(todo, SHOW_TOTALS)) {
216 (void)printf("device read KB/t xfr time MB/s");
217 (void)printf(" write KB/t xfr time MB/s\n");
218 } else {
219 (void)printf("device read KB/t r/s time MB/s");
220 (void)printf(" write KB/t w/s time MB/s\n");
221 }
222 return;
223 }
224
225 if (ISSET(todo, SHOW_TTY))
226 (void)printf(" tty");
227
228 if (ISSET(todo, SHOW_STATS_1))
229 for (i = 0; i < dk_ndrive; i++)
230 if (cur.dk_select[i])
231 (void)printf(" %7.7s ", cur.dk_name[i]);
232
233 if (ISSET(todo, SHOW_STATS_2))
234 for (i = 0; i < dk_ndrive; i++)
235 if (cur.dk_select[i])
236 (void)printf(" %7.7s ", cur.dk_name[i]);
237
238 if (ISSET(todo, SHOW_CPU))
239 (void)printf(" cpu");
240
241 printf("\n");
242
243 /* Sub-Headers. */
244 if (ISSET(todo, SHOW_TTY))
245 printf(" tin tout");
246
247 if (ISSET(todo, SHOW_STATS_1)) {
248 for (i = 0; i < dk_ndrive; i++)
249 if (cur.dk_select[i]) {
250 if (ISSET(todo, SHOW_TOTALS))
251 (void)printf(" KB/t xfr MB ");
252 else
253 (void)printf(" KB/t t/s MB/s ");
254 }
255 }
256
257 if (ISSET(todo, SHOW_STATS_2))
258 for (i = 0; i < dk_ndrive; i++)
259 if (cur.dk_select[i])
260 (void)printf(" KB xfr time ");
261
262 if (ISSET(todo, SHOW_CPU))
263 (void)printf(" us ni sy in id");
264 printf("\n");
265 }
266
267 static void
268 disk_stats(double etime)
269 {
270 int dn;
271 double atime, mbps;
272
273 for (dn = 0; dn < dk_ndrive; ++dn) {
274 if (!cur.dk_select[dn])
275 continue;
276 /* average Kbytes per transfer. */
277 if (cur.dk_rxfer[dn] + cur.dk_wxfer[dn])
278 mbps = ((cur.dk_rbytes[dn] + cur.dk_rbytes[dn]) /
279 (1024.0)) / (cur.dk_rxfer[dn] + cur.dk_rxfer[dn]);
280 else
281 mbps = 0.0;
282 (void)printf(" %5.2f", mbps);
283
284 /* average transfers per second. */
285 (void)printf(" %3.0f", (cur.dk_rxfer[dn] + cur.dk_rxfer[dn]) / etime);
286
287 /* time busy in disk activity */
288 atime = (double)cur.dk_time[dn].tv_sec +
289 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
290
291 /* Megabytes per second. */
292 if (atime != 0.0)
293 mbps = (cur.dk_rbytes[dn] + cur.dk_wbytes[dn]) /
294 (double)(1024 * 1024);
295 else
296 mbps = 0;
297 (void)printf(" %4.2f ", mbps / etime);
298 }
299 }
300
301 static void
302 disk_stats2(double etime)
303 {
304 int dn;
305 double atime;
306
307 for (dn = 0; dn < dk_ndrive; ++dn) {
308 if (!cur.dk_select[dn])
309 continue;
310
311 /* average kbytes per second. */
312 (void)printf(" %4.0f", (cur.dk_rbytes[dn] + cur.dk_rbytes[dn]) / (1024.0) / etime);
313
314 /* average transfers per second. */
315 (void)printf(" %3.0f", (cur.dk_rxfer[dn] + cur.dk_rxfer[dn]) / etime);
316
317 /* average time busy in disk activity */
318 atime = (double)cur.dk_time[dn].tv_sec +
319 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
320 (void)printf(" %4.2f ", atime / etime);
321 }
322 }
323
324 static void
325 disk_statsx(double etime)
326 {
327 int dn;
328 double atime, kbps;
329
330 for (dn = 0; dn < dk_ndrive; ++dn) {
331 (void)printf("%-8.8s", cur.dk_name[dn]);
332
333 /* average read Kbytes per transfer */
334 if (cur.dk_rxfer[dn])
335 kbps = (cur.dk_rbytes[dn] / (1024.0)) / cur.dk_rxfer[dn];
336 else
337 kbps = 0.0;
338 (void)printf(" %8.2f", kbps);
339
340 /* average read transfers (per second) */
341 (void)printf(" %6.0f", cur.dk_rxfer[dn] / etime);
342
343 /* time read busy in disk activity */
344 atime = (double)cur.dk_time[dn].tv_sec +
345 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
346 (void)printf(" %6.2f", atime / etime);
347
348 /* average read megabytes (per second) */
349 (void)printf(" %8.2f",
350 cur.dk_rbytes[dn] / (1024.0 * 1024) / etime);
351
352
353 /* average write Kbytes per transfer */
354 if (cur.dk_wxfer[dn])
355 kbps = (cur.dk_wbytes[dn] / (1024.0)) / cur.dk_wxfer[dn];
356 else
357 kbps = 0.0;
358 (void)printf(" %8.2f", kbps);
359
360 /* average write transfers (per second) */
361 (void)printf(" %6.0f", cur.dk_wxfer[dn] / etime);
362
363 /* time write busy in disk activity */
364 atime = (double)cur.dk_time[dn].tv_sec +
365 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
366 (void)printf(" %6.2f", atime / etime);
367
368 /* average write megabytes (per second) */
369 (void)printf(" %8.2f",
370 cur.dk_wbytes[dn] / (1024.0 * 1024) / etime);
371 /*
372 * Our caller prints a newline; we need one for every line
373 * except the last.
374 */
375 if (dn < dk_ndrive -1)
376 (void)printf("\n");
377 }
378 }
379
380 static void
381 cpustats(void)
382 {
383 int state;
384 double time;
385
386 time = 0;
387 for (state = 0; state < CPUSTATES; ++state)
388 time += cur.cp_time[state];
389 if (!time)
390 time = 1.0;
391 /* States are generally never 100% and can use %3.0f. */
392 for (state = 0; state < CPUSTATES; ++state)
393 printf(" %2.0f", 100. * cur.cp_time[state] / time);
394 }
395
396 static void
397 usage(void)
398 {
399
400 (void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] \
401 [-N system] [-w wait] [drives]\n");
402 exit(1);
403 }
404
405 static void
406 display(void)
407 {
408 double etime;
409
410 /* Sum up the elapsed ticks. */
411 etime = cur.cp_etime;
412
413 /* If we're showing totals only, then don't divide by the
414 * system time.
415 */
416 if (ISSET(todo, SHOW_TOTALS))
417 etime = 1.0;
418
419 if (ISSET(todo, SHOW_TTY))
420 printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
421
422 if (ISSET(todo, SHOW_STATS_1))
423 disk_stats(etime);
424
425 if (ISSET(todo, SHOW_STATS_2))
426 disk_stats2(etime);
427
428 if (ISSET(todo, SHOW_STATS_X))
429 disk_statsx(etime);
430
431 if (ISSET(todo, SHOW_CPU))
432 cpustats();
433
434 (void)printf("\n");
435 (void)fflush(stdout);
436 }
437
438 static void
439 selectdrives(int argc, char *argv[])
440 {
441 int i, ndrives;
442
443 /*
444 * Choose drives to be displayed. Priority goes to (in order) drives
445 * supplied as arguments and default drives. If everything isn't
446 * filled in and there are drives not taken care of, display the first
447 * few that fit.
448 *
449 * The backward compatibility #ifdefs permit the syntax:
450 * iostat [ drives ] [ interval [ count ] ]
451 */
452 if (ISSET(todo, SHOW_STATS_X)) {
453 for (i = 0; i < dk_ndrive; i++)
454 cur.dk_select[i] = 1;
455 }
456
457 #define BACKWARD_COMPATIBILITY
458 for (ndrives = 0; *argv; ++argv) {
459 #ifdef BACKWARD_COMPATIBILITY
460 if (isdigit(**argv))
461 break;
462 #endif
463 if (!ISSET(todo, SHOW_STATS_X))
464 for (i = 0; i < dk_ndrive; i++) {
465 if (strcmp(cur.dk_name[i], *argv))
466 continue;
467 cur.dk_select[i] = 1;
468 ++ndrives;
469 }
470 }
471 #ifdef BACKWARD_COMPATIBILITY
472 if (*argv) {
473 interval = atoi(*argv);
474 if (*++argv)
475 reps = atoi(*argv);
476 }
477 #endif
478
479 if (interval) {
480 if (!reps)
481 reps = -1;
482 } else
483 if (reps)
484 interval = 1;
485
486 /* Pick up to 4 drives if none specified. */
487 if (ndrives == 0)
488 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
489 if (cur.dk_select[i])
490 continue;
491 cur.dk_select[i] = 1;
492 ++ndrives;
493 }
494 }
495