Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / systat / iostat.c
1 /*
2  * Copyright (c) 1998 Kenneth D. Merry.
3  * 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. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/usr.bin/systat/iostat.c,v 1.9.2.1 2000/07/02 10:03:17 ps Exp $
29  * $DragonFly: src/usr.bin/systat/iostat.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
30  *
31  * @(#)iostat.c 8.1 (Berkeley) 6/6/93
32  */
33 /*
34  * Copyright (c) 1980, 1992, 1993
35  *      The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *      This product includes software developed by the University of
48  *      California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  */
65
66 #include <sys/param.h>
67 #include <sys/dkstat.h>
68
69 #include <string.h>
70 #include <stdlib.h>
71 #include <nlist.h>
72 #include <paths.h>
73 #include <devstat.h>
74 #include <err.h>
75 #include "systat.h"
76 #include "extern.h"
77 #include "devs.h"
78
79 static struct nlist namelist[] = {
80 #define X_CP_TIME       0
81         { "_cp_time" },
82 #ifdef vax
83 #define X_MBDINIT       (X_CP_TIME+1)
84         { "_mbdinit" },
85 #define X_UBDINIT       (X_CP_TIME+2)
86         { "_ubdinit" },
87 #endif
88 #ifdef tahoe
89 #define X_VBDINIT       (X_CP_TIME+1)
90         { "_vbdinit" },
91 #endif
92         { "" },
93 };
94
95 struct statinfo cur, last;
96
97 static  int linesperregion;
98 static  double etime;
99 static  int numbers = 0;                /* default display bar graphs */
100 static  int kbpt = 0;                   /* default ms/seek shown */
101
102 static int barlabels __P((int));
103 static void histogram __P((long double, int, double));
104 static int numlabels __P((int));
105 static int devstats __P((int, int, int));
106 static void stat1 __P((int, int));
107
108 WINDOW *
109 openiostat()
110 {
111         return (subwin(stdscr, LINES-1-5, 0, 5, 0));
112 }
113
114 void
115 closeiostat(w)
116         WINDOW *w;
117 {
118         if (w == NULL)
119                 return;
120         wclear(w);
121         wrefresh(w);
122         delwin(w);
123 }
124
125 int
126 initiostat()
127 {
128         if (num_devices = getnumdevs() < 0)
129                 return(0);
130
131         cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
132         last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
133         bzero(cur.dinfo, sizeof(struct devinfo));
134         bzero(last.dinfo, sizeof(struct devinfo));
135         
136         /*
137          * This value for maxshowdevs (100) is bogus.  I'm not sure exactly
138          * how to calculate it, though.
139          */
140         if (dsinit(100, &cur, &last, NULL) != 1)
141                 return(0);
142
143         if (kvm_nlist(kd, namelist)) {
144                 nlisterr(namelist);
145                 return(0);
146         }
147
148         return(1);
149 }
150
151 void
152 fetchiostat()
153 {
154         struct devinfo *tmp_dinfo;
155
156         NREAD(X_CP_TIME, cur.cp_time, sizeof(cur.cp_time));
157         tmp_dinfo = last.dinfo;
158         last.dinfo = cur.dinfo;
159         cur.dinfo = tmp_dinfo;
160          
161         last.busy_time = cur.busy_time;
162
163         /*
164          * Here what we want to do is refresh our device stats.
165          * getdevs() returns 1 when the device list has changed.
166          * If the device list has changed, we want to go through
167          * the selection process again, in case a device that we
168          * were previously displaying has gone away.
169          */
170         switch (getdevs(&cur)) {
171         case -1:
172                 errx(1, "%s", devstat_errbuf);
173                 break;
174         case 1:
175                 cmdiostat("refresh", NULL);
176                 break;
177         default:
178                 break;
179         }
180         num_devices = cur.dinfo->numdevs;
181         generation = cur.dinfo->generation;
182
183 }
184
185 #define INSET   10
186
187 void
188 labeliostat()
189 {
190         int row;
191
192         row = 0;
193         wmove(wnd, row, 0); wclrtobot(wnd);
194         mvwaddstr(wnd, row++, INSET,
195             "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
196         mvwaddstr(wnd, row++, 0, "cpu  user|");
197         mvwaddstr(wnd, row++, 0, "     nice|");
198         mvwaddstr(wnd, row++, 0, "   system|");
199         mvwaddstr(wnd, row++, 0, "interrupt|");
200         mvwaddstr(wnd, row++, 0, "     idle|");
201         if (numbers)
202                 row = numlabels(row + 1);
203         else
204                 row = barlabels(row + 1);
205 }
206
207 static int
208 numlabels(row)
209         int row;
210 {
211         int i, col, regions, ndrives;
212         char tmpstr[10];
213
214 #define COLWIDTH        17
215 #define DRIVESPERLINE   ((wnd->_maxx - INSET) / COLWIDTH)
216         for (ndrives = 0, i = 0; i < num_devices; i++)
217                 if (dev_select[i].selected)
218                         ndrives++;
219         regions = howmany(ndrives, DRIVESPERLINE);
220         /*
221          * Deduct -regions for blank line after each scrolling region.
222          */
223         linesperregion = (wnd->_maxy - row - regions) / regions;
224         /*
225          * Minimum region contains space for two
226          * label lines and one line of statistics.
227          */
228         if (linesperregion < 3)
229                 linesperregion = 3;
230         col = INSET;
231         for (i = 0; i < num_devices; i++)
232                 if (dev_select[i].selected) {
233                         if (col + COLWIDTH >= wnd->_maxx - INSET) {
234                                 col = INSET, row += linesperregion + 1;
235                                 if (row > wnd->_maxy - (linesperregion + 1))
236                                         break;
237                         }
238                         sprintf(tmpstr, "%s%d", dev_select[i].device_name,
239                                 dev_select[i].unit_number);
240                         mvwaddstr(wnd, row, col + 4, tmpstr);
241                         mvwaddstr(wnd, row + 1, col, "  KB/t tps  MB/s ");
242                         col += COLWIDTH;
243                 }
244         if (col)
245                 row += linesperregion + 1;
246         return (row);
247 }
248
249 static int
250 barlabels(row)
251         int row;
252 {
253         int i;
254         char tmpstr[10];
255
256         mvwaddstr(wnd, row++, INSET,
257             "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
258         linesperregion = 2 + kbpt;
259         for (i = 0; i < num_devices; i++)
260                 if (dev_select[i].selected) {
261                         if (row > wnd->_maxy - linesperregion)
262                                 break;
263                         sprintf(tmpstr, "%s%d", dev_select[i].device_name,
264                                 dev_select[i].unit_number);
265                         mvwprintw(wnd, row++, 0, "%-5.5s MB/s|", 
266                                   tmpstr);
267                         mvwaddstr(wnd, row++, 0, "      tps|");
268                         if (kbpt)
269                                 mvwaddstr(wnd, row++, 0, "     KB/t|");
270                 }
271         return (row);
272 }
273
274
275 void
276 showiostat()
277 {
278         register long t;
279         register int i, row, col;
280
281 #define X(fld)  t = cur.fld[i]; cur.fld[i] -= last.fld[i]; last.fld[i] = t
282         etime = 0;
283         for(i = 0; i < CPUSTATES; i++) {
284                 X(cp_time);
285                 etime += cur.cp_time[i];
286         }
287         if (etime == 0.0)
288                 etime = 1.0;
289         etime /= hertz;
290         row = 1;
291         for (i = 0; i < CPUSTATES; i++)
292                 stat1(row++, i);
293         if (!numbers) {
294                 row += 2;
295                 for (i = 0; i < num_devices; i++)
296                         if (dev_select[i].selected) {
297                                 if (row > wnd->_maxy - linesperregion)
298                                         break;
299                                 row = devstats(row, INSET, i);
300                         }
301                 return;
302         }
303         col = INSET;
304         wmove(wnd, row + linesperregion, 0);
305         wdeleteln(wnd);
306         wmove(wnd, row + 3, 0);
307         winsertln(wnd);
308         for (i = 0; i < num_devices; i++)
309                 if (dev_select[i].selected) {
310                         if (col + COLWIDTH >= wnd->_maxx - INSET) {
311                                 col = INSET, row += linesperregion + 1;
312                                 if (row > wnd->_maxy - (linesperregion + 1))
313                                         break;
314                                 wmove(wnd, row + linesperregion, 0);
315                                 wdeleteln(wnd);
316                                 wmove(wnd, row + 3, 0);
317                                 winsertln(wnd);
318                         }
319                         (void) devstats(row + 3, col, i);
320                         col += COLWIDTH;
321                 }
322 }
323
324 static int
325 devstats(row, col, dn)
326         int row, col, dn;
327 {
328         long double transfers_per_second;
329         long double kb_per_transfer, mb_per_second;
330         long double busy_seconds;
331         int di;
332         
333         di = dev_select[dn].position;
334
335         busy_seconds = compute_etime(cur.busy_time, last.busy_time);
336
337         if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
338                           busy_seconds, NULL, NULL, NULL,
339                           &kb_per_transfer, &transfers_per_second,
340                           &mb_per_second, NULL, NULL) != 0)
341                 errx(1, "%s", devstat_errbuf);
342
343         if (numbers) {
344                 mvwprintw(wnd, row, col, " %5.2Lf %3.0Lf %5.2Lf ",
345                          kb_per_transfer, transfers_per_second,
346                          mb_per_second);
347                 return(row);
348         }
349         wmove(wnd, row++, col);
350         histogram(mb_per_second, 50, .5);
351         wmove(wnd, row++, col);
352         histogram(transfers_per_second, 50, .5);
353         if (kbpt) {
354                 wmove(wnd, row++, col);
355                 histogram(kb_per_transfer, 50, .5);
356         }
357
358         return(row);
359
360 }
361
362 static void
363 stat1(row, o)
364         int row, o;
365 {
366         register int i;
367         double time;
368
369         time = 0;
370         for (i = 0; i < CPUSTATES; i++)
371                 time += cur.cp_time[i];
372         if (time == 0.0)
373                 time = 1.0;
374         wmove(wnd, row, INSET);
375 #define CPUSCALE        0.5
376         histogram(100.0 * cur.cp_time[o] / time, 50, CPUSCALE);
377 }
378
379 static void
380 histogram(val, colwidth, scale)
381         long double val;
382         int colwidth;
383         double scale;
384 {
385         char buf[10];
386         register int k;
387         register int v = (int)(val * scale) + 0.5;
388
389         k = MIN(v, colwidth);
390         if (v > colwidth) {
391                 snprintf(buf, sizeof(buf), "%5.2Lf", val);
392                 k -= strlen(buf);
393                 while (k--)
394                         waddch(wnd, 'X');
395                 waddstr(wnd, buf);
396                 return;
397         }
398         while (k--)
399                 waddch(wnd, 'X');
400         wclrtoeol(wnd);
401 }
402
403 int
404 cmdiostat(cmd, args)
405         char *cmd, *args;
406 {
407
408         if (prefix(cmd, "kbpt"))
409                 kbpt = !kbpt;
410         else if (prefix(cmd, "numbers"))
411                 numbers = 1;
412         else if (prefix(cmd, "bars"))
413                 numbers = 0;
414         else if (!dscmd(cmd, args, 100, &cur))
415                 return (0);
416         wclear(wnd);
417         labeliostat();
418         refresh();
419         return (1);
420 }