xref: /freebsd/usr.sbin/iostat/iostat.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
1 /*-
2  * Copyright (c) 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1986, 1991, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)iostat.c	8.2 (Berkeley) 1/26/94";
43 #endif
44 static const char rcsid[] =
45 	"$Id$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/buf.h>
50 #include <sys/dkstat.h>
51 
52 #include <err.h>
53 #include <ctype.h>
54 #include <fcntl.h>
55 #include <kvm.h>
56 #include <limits.h>
57 #include <nlist.h>
58 #include <paths.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 
65 struct nlist namelist[] = {
66 #define	X_DK_TIME	0
67 	{ "_dk_time" },
68 #define	X_DK_XFER	1
69 	{ "_dk_xfer" },
70 #define	X_DK_WDS	2
71 	{ "_dk_wds" },
72 #define	X_TK_NIN	3
73 	{ "_tk_nin" },
74 #define	X_TK_NOUT	4
75 	{ "_tk_nout" },
76 #define	X_DK_SEEK	5
77 	{ "_dk_seek" },
78 #define	X_CP_TIME	6
79 	{ "_cp_time" },
80 #define	X_DK_WPMS	7
81 	{ "_dk_wpms" },
82 #define	X_HZ		8
83 	{ "_hz" },
84 #define	X_STATHZ	9
85 	{ "_stathz" },
86 #define	X_DK_NDRIVE	10
87 	{ "_dk_ndrive" },
88 #define	X_END		10
89 #if defined(hp300) || defined(luna68k)
90 #define	X_HPDINIT	(X_END+1)
91 	{ "_hp_dinit" },
92 #endif
93 #if defined(i386)
94 #define X_DK_NAMES	(X_END+1)
95 	{ "_dk_names" },
96 #endif
97 #ifdef mips
98 #define	X_SCSI_DINIT	(X_END+1)
99 	{ "_scsi_dinit" },
100 #endif
101 #ifdef tahoe
102 #define	X_VBDINIT	(X_END+1)
103 	{ "_vbdinit" },
104 #endif
105 #ifdef vax
106 	{ "_mbdinit" },
107 #define X_MBDINIT	(X_END+1)
108 	{ "_ubdinit" },
109 #define X_UBDINIT	(X_END+2)
110 #endif
111 	{ NULL },
112 };
113 
114 struct _disk {
115 	long	cp_time[CPUSTATES];
116 	long	*dk_time;
117 	long	*dk_wds;
118 	long	*dk_seek;
119 	long	*dk_xfer;
120 	long	tk_nin;
121 	long	tk_nout;
122 } cur, last;
123 
124 kvm_t	 *kd;
125 double	  etime;
126 long	 *dk_wpms;
127 int	  dk_ndrive, *dr_select, hz, kmemfd, ndrives;
128 char	**dr_name;
129 
130 #define nlread(x, v) \
131 	kvm_read(kd, namelist[x].n_value, &(v), sizeof(v))
132 
133 #include "names.c"				/* XXX */
134 
135 void cpustats __P((void));
136 void dkstats __P((void));
137 void phdr __P((int));
138 static void usage __P((void));
139 
140 int
141 main(argc, argv)
142 	int argc;
143 	char *argv[];
144 {
145 	register int i;
146 	long tmp;
147 	int ch, hdrcnt, reps, interval, stathz, ndrives;
148 	char **cp, *memf, *nlistf, buf[30];
149         char errbuf[_POSIX2_LINE_MAX];
150 
151 	interval = reps = 0;
152 	nlistf = memf = NULL;
153 	while ((ch = getopt(argc, argv, "c:M:N:w:")) != -1)
154 		switch(ch) {
155 		case 'c':
156 			if ((reps = atoi(optarg)) <= 0)
157 				errx(1, "repetition count <= 0");
158 			break;
159 		case 'M':
160 			memf = optarg;
161 			break;
162 		case 'N':
163 			nlistf = optarg;
164 			break;
165 		case 'w':
166 			if ((interval = atoi(optarg)) <= 0)
167 				errx(1, "interval <= 0");
168 			break;
169 		case '?':
170 		default:
171 			usage();
172 		}
173 	argc -= optind;
174 	argv += optind;
175 
176 	/*
177 	 * Discard setgid privileges if not the running kernel so that bad
178 	 * guys can't print interesting stuff from kernel memory.
179 	 */
180 	if (nlistf != NULL || memf != NULL)
181 		setgid(getgid());
182 
183         kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
184 	if (kd == 0)
185 		errx(1, "kvm_openfiles: %s", errbuf);
186 	if (kvm_nlist(kd, namelist) == -1)
187 		errx(1, "kvm_nlist: %s", kvm_geterr(kd));
188 	if (namelist[X_DK_NDRIVE].n_type == 0)
189 		errx(1, "dk_ndrive not found in namelist");
190 	(void)nlread(X_DK_NDRIVE, dk_ndrive);
191 	if (dk_ndrive < 0)
192 		errx(1, "invalid dk_ndrive %d", dk_ndrive);
193 
194 	cur.dk_time = calloc(dk_ndrive, sizeof(long));
195 	cur.dk_wds = calloc(dk_ndrive, sizeof(long));
196 	cur.dk_seek = calloc(dk_ndrive, sizeof(long));
197 	cur.dk_xfer = calloc(dk_ndrive, sizeof(long));
198 	last.dk_time = calloc(dk_ndrive, sizeof(long));
199 	last.dk_wds = calloc(dk_ndrive, sizeof(long));
200 	last.dk_seek = calloc(dk_ndrive, sizeof(long));
201 	last.dk_xfer = calloc(dk_ndrive, sizeof(long));
202 	dr_select = calloc(dk_ndrive, sizeof(int));
203 	dr_name = calloc(dk_ndrive, sizeof(char *));
204 	dk_wpms = calloc(dk_ndrive, sizeof(long));
205 
206 	for (i = 0; i < dk_ndrive; i++) {
207 		(void)sprintf(buf, "dk%d", i);
208 		dr_name[i] = strdup(buf);
209 	}
210 	if (!read_names())
211 		exit(1);
212 	(void)nlread(X_HZ, hz);
213 	(void)nlread(X_STATHZ, stathz);
214 	if (stathz)
215 		hz = stathz;
216 	(void)kvm_read(kd, namelist[X_DK_WPMS].n_value, dk_wpms,
217 		dk_ndrive * sizeof(dk_wpms));
218 
219 	/*
220 	 * Choose drives to be displayed.  Priority goes to (in order) drives
221 	 * supplied as arguments and default drives.  If everything isn't
222 	 * filled in and there are drives not taken care of, display the first
223 	 * few that fit.
224 	 *
225 	 * The backward compatibility #ifdefs permit the syntax:
226 	 *	iostat [ drives ] [ interval [ count ] ]
227 	 */
228 #define	BACKWARD_COMPATIBILITY
229 	for (ndrives = 0; *argv; ++argv) {
230 #ifdef	BACKWARD_COMPATIBILITY
231 		if (isdigit(**argv))
232 			break;
233 #endif
234 		for (i = 0; i < dk_ndrive; i++) {
235 			if (strcmp(dr_name[i], *argv))
236 				continue;
237 			dr_select[i] = 1;
238 			++ndrives;
239 		}
240 	}
241 #ifdef	BACKWARD_COMPATIBILITY
242 	if (*argv) {
243 		interval = atoi(*argv);
244 		if (*++argv)
245 			reps = atoi(*argv);
246 	}
247 #endif
248 
249 	if (interval) {
250 		if (!reps)
251 			reps = -1;
252 	} else
253 		if (reps)
254 			interval = 1;
255 
256 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
257 		if (dr_select[i] || dk_wpms[i] == 0)
258 			continue;
259 		for (cp = defdrives; *cp; cp++)
260 			if (strcmp(dr_name[i], *cp) == 0) {
261 				dr_select[i] = 1;
262 				++ndrives;
263 				break;
264 			}
265 	}
266 	for (i = 0; i < dk_ndrive && ndrives < 4; i++) {
267 		if (dr_select[i])
268 			continue;
269 		dr_select[i] = 1;
270 		++ndrives;
271 	}
272 
273 	(void)signal(SIGCONT, phdr);
274 
275 	for (hdrcnt = 1;;) {
276 		if (!--hdrcnt) {
277 			phdr(0);
278 			hdrcnt = 20;
279 		}
280 		(void)kvm_read(kd, namelist[X_DK_TIME].n_value,
281 		    cur.dk_time, dk_ndrive * sizeof(long));
282 		(void)kvm_read(kd, namelist[X_DK_XFER].n_value,
283 		    cur.dk_xfer, dk_ndrive * sizeof(long));
284 		(void)kvm_read(kd, namelist[X_DK_WDS].n_value,
285 		    cur.dk_wds, dk_ndrive * sizeof(long));
286 		(void)kvm_read(kd, namelist[X_DK_SEEK].n_value,
287 		    cur.dk_seek, dk_ndrive * sizeof(long));
288 		(void)kvm_read(kd, namelist[X_TK_NIN].n_value,
289 		    &cur.tk_nin, sizeof(cur.tk_nin));
290 		(void)kvm_read(kd, namelist[X_TK_NOUT].n_value,
291 		    &cur.tk_nout, sizeof(cur.tk_nout));
292 		(void)kvm_read(kd, namelist[X_CP_TIME].n_value,
293 		    cur.cp_time, sizeof(cur.cp_time));
294 		for (i = 0; i < dk_ndrive; i++) {
295 			if (!dr_select[i])
296 				continue;
297 #define X(fld)	tmp = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = tmp
298 			X(dk_xfer);
299 			X(dk_seek);
300 			X(dk_wds);
301 			X(dk_time);
302 		}
303 		tmp = cur.tk_nin;
304 		cur.tk_nin -= last.tk_nin;
305 		last.tk_nin = tmp;
306 		tmp = cur.tk_nout;
307 		cur.tk_nout -= last.tk_nout;
308 		last.tk_nout = tmp;
309 		etime = 0;
310 		for (i = 0; i < CPUSTATES; i++) {
311 			X(cp_time);
312 			etime += cur.cp_time[i];
313 		}
314 		if (etime == 0.0)
315 			etime = 1.0;
316 		etime /= (float)hz;
317 		(void)printf("%4.0f%5.0f",
318 		    cur.tk_nin / etime, cur.tk_nout / etime);
319 		dkstats();
320 		cpustats();
321 		(void)printf("\n");
322 		(void)fflush(stdout);
323 
324 		if (reps >= 0 && --reps <= 0)
325 			break;
326 		(void)sleep(interval);
327 	}
328 	exit(0);
329 }
330 
331 /* ARGUSED */
332 void
333 phdr(signo)
334 	int signo;
335 {
336 	register int i;
337 
338 	(void)printf("      tty");
339 	for (i = 0; i < dk_ndrive; i++)
340 		if (dr_select[i])
341 			(void)printf("         %4.4s ", dr_name[i]);
342 	(void)printf("         cpu\n tin tout");
343 	for (i = 0; i < dk_ndrive; i++)
344 		if (dr_select[i])
345 			(void)printf(" sps tps msps ");
346 	(void)printf(" us ni sy in id\n");
347 }
348 
349 void
350 dkstats()
351 {
352 	register int dn;
353 	double atime, itime, msps, words, xtime;
354 
355 	for (dn = 0; dn < dk_ndrive; ++dn) {
356 		if (!dr_select[dn])
357 			continue;
358 		words = (double)cur.dk_wds[dn] * 32;	/* words xfer'd */
359 		(void)printf("%4.0f",			/* sectors */
360 		    words / (DEV_BSIZE / 2) / etime);
361 
362 		(void)printf("%4.0f", cur.dk_xfer[dn] / etime);
363 
364 		if (dk_wpms[dn] && cur.dk_xfer[dn]) {
365 			atime = cur.dk_time[dn];	/* ticks disk busy */
366 			atime /= (float)hz;		/* ticks to seconds */
367 			xtime = words / dk_wpms[dn];	/* transfer time */
368 			itime = atime - xtime;		/* time not xfer'ing */
369 			if (itime < 0)
370 				msps = 0;
371 			else
372 				msps = itime * 1000 / cur.dk_xfer[dn];
373 		} else
374 			msps = 0;
375 		(void)printf("%5.1f ", msps);
376 	}
377 }
378 
379 void
380 cpustats()
381 {
382 	register int state;
383 	double time;
384 
385 	time = 0;
386 	for (state = 0; state < CPUSTATES; ++state)
387 		time += cur.cp_time[state];
388 	for (state = 0; state < CPUSTATES; ++state)
389 		(void)printf("%3.0f",
390 		    100. * cur.cp_time[state] / (time ? time : 1));
391 }
392 
393 static void
394 usage()
395 {
396 	(void)fprintf(stderr,
397 "usage: iostat [-c count] [-M core] [-N system] [-w wait] [drives]\n");
398 	exit(1);
399 }
400