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