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