iostat.c revision 1.25 1 /* $NetBSD: iostat.c,v 1.25 2002/01/28 02:16:56 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.25 2002/01/28 02:16:56 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 timeval 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 /*
181 * Discard setgid privileges if not the running kernel so that bad
182 * guys can't print interesting stuff from kernel memory.
183 */
184 if (nlistf != NULL || memf != NULL)
185 setgid(getgid());
186
187 dkinit(0);
188 dkreadstats();
189 selectdrives(argc, argv);
190
191 tv.tv_sec = interval;
192 tv.tv_usec = 0;
193
194 /* print a new header on sigcont */
195 (void)signal(SIGCONT, header);
196
197 for (hdrcnt = 1;;) {
198 if (!--hdrcnt) {
199 header(0);
200 hdrcnt = 20;
201 }
202
203 if (!ISSET(todo, SHOW_TOTALS))
204 dkswap();
205 display();
206
207 if (reps >= 0 && --reps <= 0)
208 break;
209 select(0, NULL, NULL, NULL, &tv);
210 dkreadstats();
211 }
212 exit(0);
213 }
214
215 static void
216 header(int signo)
217 {
218 int i;
219
220 if (ISSET(todo, SHOW_STATS_X))
221 return;
222
223 /* Main Headers. */
224 if (ISSET(todo, SHOW_TTY))
225 (void)printf(" tty");
226
227 if (ISSET(todo, SHOW_STATS_1))
228 for (i = 0; i < dk_ndrive; i++)
229 if (cur.dk_select[i])
230 (void)printf(
231 " %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(
237 " %7.7s ", cur.dk_name[i]);
238
239 if (ISSET(todo, SHOW_CPU))
240 (void)printf(" cpu");
241
242 printf("\n");
243
244 /* Sub-Headers. */
245 if (ISSET(todo, SHOW_TTY))
246 printf(" tin tout");
247
248 if (ISSET(todo, SHOW_STATS_1)) {
249 for (i = 0; i < dk_ndrive; i++)
250 if (cur.dk_select[i]) {
251 if (ISSET(todo, SHOW_TOTALS))
252 (void)printf(" KB/t xfr MB ");
253 else
254 (void)printf(" KB/t t/s MB/s ");
255 }
256 }
257
258 if (ISSET(todo, SHOW_STATS_2))
259 for (i = 0; i < dk_ndrive; i++)
260 if (cur.dk_select[i])
261 (void)printf(" KB xfr time ");
262
263 if (ISSET(todo, SHOW_CPU))
264 (void)printf(" us ni sy in id");
265 printf("\n");
266 }
267
268 static void
269 disk_stats(double etime)
270 {
271 int dn;
272 double atime, mbps;
273
274 for (dn = 0; dn < dk_ndrive; ++dn) {
275 if (!cur.dk_select[dn])
276 continue;
277
278 /* average Kbytes per transfer. */
279 if (cur.dk_xfer[dn])
280 mbps = (cur.dk_bytes[dn] / (1024.0)) / cur.dk_xfer[dn];
281 else
282 mbps = 0.0;
283 (void)printf(" %5.2f", mbps);
284
285 /* average transfers per second. */
286 (void)printf(" %3.0f", cur.dk_xfer[dn] / etime);
287
288 /* time busy in disk activity */
289 atime = (double)cur.dk_time[dn].tv_sec +
290 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
291
292 /* Megabytes per second. */
293 if (atime != 0.0)
294 mbps = cur.dk_bytes[dn] / (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_bytes[dn] / (1024.0) / etime);
313
314 /* average transfers per second. */
315 (void)printf(" %3.0f", cur.dk_xfer[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 if (ISSET(todo, SHOW_TOTALS))
331 (void)printf("device KB/t xfr time MB");
332 else
333 (void)printf("device KB/t t/s time MB/s");
334
335 for (dn = 0; dn < dk_ndrive; ++dn) {
336 (void)printf("\n");
337 (void)printf("%-8.8s", cur.dk_name[dn]);
338
339 /* average Kbytes per transfer */
340 if (cur.dk_xfer[dn])
341 kbps = (cur.dk_bytes[dn] / (1024.0)) / cur.dk_xfer[dn];
342 else
343 kbps = 0.0;
344 (void)printf(" %8.2f", kbps);
345
346 /* average transfers (per second) */
347 (void)printf(" %8.0f", cur.dk_xfer[dn] / etime);
348
349 /* time busy in disk activity */
350 atime = (double)cur.dk_time[dn].tv_sec +
351 ((double)cur.dk_time[dn].tv_usec / (double)1000000);
352 (void)printf(" %8.2f", atime / etime);
353
354 /* average megabytes (per second) */
355 (void)printf(" %8.2f",
356 cur.dk_bytes[dn] / (1024.0 * 1024) / etime);
357
358 }
359 }
360
361 static void
362 cpustats(void)
363 {
364 int state;
365 double time;
366
367 time = 0;
368 for (state = 0; state < CPUSTATES; ++state)
369 time += cur.cp_time[state];
370 if (!time)
371 time = 1.0;
372 /* States are generally never 100% and can use %3.0f. */
373 for (state = 0; state < CPUSTATES; ++state)
374 printf(" %2.0f", 100. * cur.cp_time[state] / time);
375 }
376
377 static void
378 usage(void)
379 {
380
381 (void)fprintf(stderr, "usage: iostat [-CdDITx] [-c count] [-M core] \
382 [-N system] [-w wait] [drives]\n");
383 exit(1);
384 }
385
386 static void
387 display(void)
388 {
389 int i;
390 double etime;
391
392 /* Sum up the elapsed ticks. */
393 etime = 0.0;
394 for (i = 0; i < CPUSTATES; i++) {
395 etime += cur.cp_time[i];
396 }
397 if (etime == 0.0)
398 etime = 1.0;
399 /* Convert to seconds. */
400 etime /= (float)hz;
401
402 /* If we're showing totals only, then don't divide by the
403 * system time.
404 */
405 if (ISSET(todo, SHOW_TOTALS))
406 etime = 1.0;
407
408 if (ISSET(todo, SHOW_TTY))
409 printf("%4.0f %4.0f", cur.tk_nin / etime, cur.tk_nout / etime);
410
411 if (ISSET(todo, SHOW_STATS_1))
412 disk_stats(etime);
413
414 if (ISSET(todo, SHOW_STATS_2))
415 disk_stats2(etime);
416
417 if (ISSET(todo, SHOW_STATS_X))
418 disk_statsx(etime);
419
420 if (ISSET(todo, SHOW_CPU))
421 cpustats();
422
423 (void)printf("\n");
424 (void)fflush(stdout);
425 }
426
427 static void
428 selectdrives(int argc, char *argv[])
429 {
430 int i, ndrives;
431
432 /*
433 * Choose drives to be displayed. Priority goes to (in order) drives
434 * supplied as arguments and default drives. If everything isn't
435 * filled in and there are drives not taken care of, display the first
436 * few that fit.
437 *
438 * The backward compatibility #ifdefs permit the syntax:
439 * iostat [ drives ] [ interval [ count ] ]
440 */
441 if (ISSET(todo, SHOW_STATS_X)) {
442 for (i = 0; i < dk_ndrive; i++)
443 cur.dk_select[i] = 1;
444 }
445
446 #define BACKWARD_COMPATIBILITY
447 for (ndrives = 0; *argv; ++argv) {
448 #ifdef BACKWARD_COMPATIBILITY
449 if (isdigit(**argv))
450 break;
451 #endif
452 if (!ISSET(todo, SHOW_STATS_X))
453 for (i = 0; i < dk_ndrive; i++) {
454 if (strcmp(cur.dk_name[i], *argv))
455 continue;
456 cur.dk_select[i] = 1;
457 ++ndrives;
458 }
459 }
460 #ifdef BACKWARD_COMPATIBILITY
461 if (*argv) {
462 interval = atoi(*argv);
463 if (*++argv)
464 reps = atoi(*argv);
465 }
466 #endif
467
468 if (interval) {
469 if (!reps)
470 reps = -1;
471 } else
472 if (reps)
473 interval = 1;
474
475 /* Pick up to 4 drives if none specified. */
476 if (ndrives == 0)
477 for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
478 if (cur.dk_select[i])
479 continue;
480 cur.dk_select[i] = 1;
481 ++ndrives;
482 }
483 }
484