* Silence -Wold-style-definition (i.e., ansify).
[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.6 2008/10/16 01:52:33 swildner 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
68 #include <err.h>
69 #include <devstat.h>
70 #include <kinfo.h>
71 #include <paths.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include "systat.h"
75 #include "extern.h"
76 #include "devs.h"
77
78 struct statinfo cur, last;
79 static struct kinfo_cputime cp_time, old_cp_time;
80
81 static  int linesperregion;
82 static  int numbers = 0;                /* default display bar graphs */
83 static  int kbpt = 0;                   /* default ms/seek shown */
84
85 static int barlabels(int);
86 static void histogram(long double, int, double);
87 static int numlabels(int);
88 static int devstats(int, int, int);
89 static void stat1(int, uint64_t, uint64_t);
90
91 WINDOW *
92 openiostat(void)
93 {
94         return (subwin(stdscr, LINES-1-5, 0, 5, 0));
95 }
96
97 void
98 closeiostat(WINDOW *w)
99 {
100         if (w == NULL)
101                 return;
102         wclear(w);
103         wrefresh(w);
104         delwin(w);
105 }
106
107 int
108 initiostat(void)
109 {
110         if (num_devices = getnumdevs() < 0)
111                 return(0);
112
113         cur.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
114         last.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo));
115         bzero(cur.dinfo, sizeof(struct devinfo));
116         bzero(last.dinfo, sizeof(struct devinfo));
117         
118         /*
119          * This value for maxshowdevs (100) is bogus.  I'm not sure exactly
120          * how to calculate it, though.
121          */
122         if (dsinit(100, &cur, &last, NULL) != 1)
123                 return(0);
124
125         return(1);
126 }
127
128 void
129 fetchiostat(void)
130 {
131         struct devinfo *tmp_dinfo;
132
133         if (kinfo_get_sched_cputime(&cp_time))
134                 err(1, "kinfo_get_sched_cputime");
135         tmp_dinfo = last.dinfo;
136         last.dinfo = cur.dinfo;
137         cur.dinfo = tmp_dinfo;
138          
139         last.busy_time = cur.busy_time;
140
141         /*
142          * Here what we want to do is refresh our device stats.
143          * getdevs() returns 1 when the device list has changed.
144          * If the device list has changed, we want to go through
145          * the selection process again, in case a device that we
146          * were previously displaying has gone away.
147          */
148         switch (getdevs(&cur)) {
149         case -1:
150                 errx(1, "%s", devstat_errbuf);
151                 break;
152         case 1:
153                 cmdiostat("refresh", NULL);
154                 break;
155         default:
156                 break;
157         }
158         num_devices = cur.dinfo->numdevs;
159         generation = cur.dinfo->generation;
160
161 }
162
163 #define INSET   10
164
165 void
166 labeliostat(void)
167 {
168         int row;
169
170         row = 0;
171         wmove(wnd, row, 0); wclrtobot(wnd);
172         mvwaddstr(wnd, row++, INSET,
173             "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
174         mvwaddstr(wnd, row++, 0, "cpu  user|");
175         mvwaddstr(wnd, row++, 0, "     nice|");
176         mvwaddstr(wnd, row++, 0, "   system|");
177         mvwaddstr(wnd, row++, 0, "interrupt|");
178         mvwaddstr(wnd, row++, 0, "     idle|");
179         if (numbers)
180                 row = numlabels(row + 1);
181         else
182                 row = barlabels(row + 1);
183 }
184
185 static int
186 numlabels(int row)
187 {
188         int i, col, regions, ndrives;
189         char tmpstr[10];
190
191 #define COLWIDTH        17
192 #define DRIVESPERLINE   ((wnd->_maxx - INSET) / COLWIDTH)
193         for (ndrives = 0, i = 0; i < num_devices; i++)
194                 if (dev_select[i].selected)
195                         ndrives++;
196         regions = howmany(ndrives, DRIVESPERLINE);
197         /*
198          * Deduct -regions for blank line after each scrolling region.
199          */
200         linesperregion = (wnd->_maxy - row - regions) / regions;
201         /*
202          * Minimum region contains space for two
203          * label lines and one line of statistics.
204          */
205         if (linesperregion < 3)
206                 linesperregion = 3;
207         col = INSET;
208         for (i = 0; i < num_devices; i++)
209                 if (dev_select[i].selected) {
210                         if (col + COLWIDTH >= wnd->_maxx - INSET) {
211                                 col = INSET, row += linesperregion + 1;
212                                 if (row > wnd->_maxy - (linesperregion + 1))
213                                         break;
214                         }
215                         sprintf(tmpstr, "%s%d", dev_select[i].device_name,
216                                 dev_select[i].unit_number);
217                         mvwaddstr(wnd, row, col + 4, tmpstr);
218                         mvwaddstr(wnd, row + 1, col, "  KB/t tps  MB/s ");
219                         col += COLWIDTH;
220                 }
221         if (col)
222                 row += linesperregion + 1;
223         return (row);
224 }
225
226 static int
227 barlabels(int row)
228 {
229         int i;
230         char tmpstr[10];
231
232         mvwaddstr(wnd, row++, INSET,
233             "/0   /10  /20  /30  /40  /50  /60  /70  /80  /90  /100");
234         linesperregion = 2 + kbpt;
235         for (i = 0; i < num_devices; i++)
236                 if (dev_select[i].selected) {
237                         if (row > wnd->_maxy - linesperregion)
238                                 break;
239                         sprintf(tmpstr, "%s%d", dev_select[i].device_name,
240                                 dev_select[i].unit_number);
241                         mvwprintw(wnd, row++, 0, "%-5.5s MB/s|", 
242                                   tmpstr);
243                         mvwaddstr(wnd, row++, 0, "      tps|");
244                         if (kbpt)
245                                 mvwaddstr(wnd, row++, 0, "     KB/t|");
246                 }
247         return (row);
248 }
249
250
251 void
252 showiostat(void)
253 {
254         long t;
255         int i, row, col;
256         struct kinfo_cputime diff_cp_time;
257         uint64_t cp_total;
258
259         diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
260         diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
261         diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
262         diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
263         diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
264         old_cp_time = cp_time;
265
266         row = 1;
267         cp_total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
268             diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle;
269         stat1(row++, diff_cp_time.cp_user, cp_total);
270         stat1(row++, diff_cp_time.cp_nice, cp_total);
271         stat1(row++, diff_cp_time.cp_sys,  cp_total);
272         stat1(row++, diff_cp_time.cp_intr, cp_total);
273         stat1(row++, diff_cp_time.cp_idle, cp_total);
274         if (!numbers) {
275                 row += 2;
276                 for (i = 0; i < num_devices; i++)
277                         if (dev_select[i].selected) {
278                                 if (row > wnd->_maxy - linesperregion)
279                                         break;
280                                 row = devstats(row, INSET, i);
281                         }
282                 return;
283         }
284         col = INSET;
285         wmove(wnd, row + linesperregion, 0);
286         wdeleteln(wnd);
287         wmove(wnd, row + 3, 0);
288         winsertln(wnd);
289         for (i = 0; i < num_devices; i++)
290                 if (dev_select[i].selected) {
291                         if (col + COLWIDTH >= wnd->_maxx - INSET) {
292                                 col = INSET, row += linesperregion + 1;
293                                 if (row > wnd->_maxy - (linesperregion + 1))
294                                         break;
295                                 wmove(wnd, row + linesperregion, 0);
296                                 wdeleteln(wnd);
297                                 wmove(wnd, row + 3, 0);
298                                 winsertln(wnd);
299                         }
300                         (void) devstats(row + 3, col, i);
301                         col += COLWIDTH;
302                 }
303 }
304
305 static int
306 devstats(int row, int col, int dn)
307 {
308         long double transfers_per_second;
309         long double kb_per_transfer, mb_per_second;
310         long double busy_seconds;
311         int di;
312         
313         di = dev_select[dn].position;
314
315         busy_seconds = compute_etime(cur.busy_time, last.busy_time);
316
317         if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
318                           busy_seconds, NULL, NULL, NULL,
319                           &kb_per_transfer, &transfers_per_second,
320                           &mb_per_second, NULL, NULL) != 0)
321                 errx(1, "%s", devstat_errbuf);
322
323         if (numbers) {
324                 mvwprintw(wnd, row, col, " %5.2Lf %3.0Lf %5.2Lf ",
325                          kb_per_transfer, transfers_per_second,
326                          mb_per_second);
327                 return(row);
328         }
329         wmove(wnd, row++, col);
330         histogram(mb_per_second, 50, .5);
331         wmove(wnd, row++, col);
332         histogram(transfers_per_second, 50, .5);
333         if (kbpt) {
334                 wmove(wnd, row++, col);
335                 histogram(kb_per_transfer, 50, .5);
336         }
337
338         return(row);
339
340 }
341
342 static void
343 stat1(int row, uint64_t difference, uint64_t total)
344 {
345         int i;
346         double time;
347
348         if (total > 0)
349                 time = 100.0 * difference / total;
350         else
351                 time = 0;
352         wmove(wnd, row, INSET);
353 #define CPUSCALE        0.5
354         histogram(time, 50, CPUSCALE);
355 }
356
357 static void
358 histogram(long double val, int colwidth, double scale)
359 {
360         char buf[10];
361         int k;
362         int v = (int)(val * scale) + 0.5;
363
364         if (val <= 0)
365                 v = 0;
366         k = MIN(v, colwidth);
367         if (v > colwidth) {
368                 snprintf(buf, sizeof(buf), "%5.2Lf", val);
369                 k -= strlen(buf);
370                 while (k--)
371                         waddch(wnd, 'X');
372                 waddstr(wnd, buf);
373                 return;
374         }
375         while (k--)
376                 waddch(wnd, 'X');
377         wclrtoeol(wnd);
378 }
379
380 int
381 cmdiostat(char *cmd, char *args)
382 {
383
384         if (prefix(cmd, "kbpt"))
385                 kbpt = !kbpt;
386         else if (prefix(cmd, "numbers"))
387                 numbers = 1;
388         else if (prefix(cmd, "bars"))
389                 numbers = 0;
390         else if (!dscmd(cmd, args, 100, &cur))
391                 return (0);
392         wclear(wnd);
393         labeliostat();
394         refresh();
395         return (1);
396 }