iscontrol(8): Fix synopsis, sync usage() & improve markup
[dragonfly.git] / usr.bin / systat / iostat.c
blobd49cf8b10271c7ea2d0cb6f8509a7fe772b03fef
1 /*
2 * Copyright (c) 1998 Kenneth D. Merry.
3 * All rights reserved.
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.
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.
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.7 2008/11/10 04:59:45 swildner Exp $
31 * @(#)iostat.c 8.1 (Berkeley) 6/6/93
34 * Copyright (c) 1980, 1992, 1993
35 * The Regents of the University of California. All rights reserved.
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.
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.
66 #include <sys/param.h>
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"
78 struct statinfo cur, last;
79 static struct kinfo_cputime cp_time, old_cp_time;
81 static int linesperregion;
82 static int numbers = 0; /* default display bar graphs */
83 static int kbpt = 0; /* default ms/seek shown */
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);
91 WINDOW *
92 openiostat(void)
94 return (subwin(stdscr, LINES-1-5, 0, 5, 0));
97 void
98 closeiostat(WINDOW *w)
100 if (w == NULL)
101 return;
102 wclear(w);
103 wrefresh(w);
104 delwin(w);
108 initiostat(void)
110 if ((num_devices = getnumdevs()) < 0)
111 return(0);
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));
119 * This value for maxshowdevs (100) is bogus. I'm not sure exactly
120 * how to calculate it, though.
122 if (dsinit(100, &cur, &last, NULL) != 1)
123 return(0);
125 return(1);
128 void
129 fetchiostat(void)
131 struct devinfo *tmp_dinfo;
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;
139 last.busy_time = cur.busy_time;
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.
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;
158 num_devices = cur.dinfo->numdevs;
159 generation = cur.dinfo->generation;
163 #define INSET 10
165 void
166 labeliostat(void)
168 int row;
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);
185 static int
186 numlabels(int row)
188 int i, _col, regions, ndrives;
189 char tmpstr[10];
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);
198 * Deduct -regions for blank line after each scrolling region.
200 linesperregion = (wnd->_maxy - row - regions) / regions;
202 * Minimum region contains space for two
203 * label lines and one line of statistics.
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;
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;
221 if (_col)
222 row += linesperregion + 1;
223 return (row);
226 static int
227 barlabels(int row)
229 int i;
230 char tmpstr[10];
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|");
247 return (row);
251 void
252 showiostat(void)
254 int i, row, _col;
255 struct kinfo_cputime diff_cp_time;
256 uint64_t cp_total;
258 diff_cp_time.cp_user = cp_time.cp_user - old_cp_time.cp_user;
259 diff_cp_time.cp_nice = cp_time.cp_nice - old_cp_time.cp_nice;
260 diff_cp_time.cp_sys = cp_time.cp_sys - old_cp_time.cp_sys;
261 diff_cp_time.cp_intr = cp_time.cp_intr - old_cp_time.cp_intr;
262 diff_cp_time.cp_idle = cp_time.cp_idle - old_cp_time.cp_idle;
263 old_cp_time = cp_time;
265 row = 1;
266 cp_total = diff_cp_time.cp_user + diff_cp_time.cp_nice +
267 diff_cp_time.cp_sys + diff_cp_time.cp_intr + diff_cp_time.cp_idle;
268 stat1(row++, diff_cp_time.cp_user, cp_total);
269 stat1(row++, diff_cp_time.cp_nice, cp_total);
270 stat1(row++, diff_cp_time.cp_sys, cp_total);
271 stat1(row++, diff_cp_time.cp_intr, cp_total);
272 stat1(row++, diff_cp_time.cp_idle, cp_total);
273 if (!numbers) {
274 row += 2;
275 for (i = 0; i < num_devices; i++)
276 if (dev_select[i].selected) {
277 if (row > wnd->_maxy - linesperregion)
278 break;
279 row = devstats(row, INSET, i);
281 return;
283 _col = INSET;
284 wmove(wnd, row + linesperregion, 0);
285 wdeleteln(wnd);
286 wmove(wnd, row + 3, 0);
287 winsertln(wnd);
288 for (i = 0; i < num_devices; i++)
289 if (dev_select[i].selected) {
290 if (_col + COLWIDTH >= wnd->_maxx - INSET) {
291 _col = INSET, row += linesperregion + 1;
292 if (row > wnd->_maxy - (linesperregion + 1))
293 break;
294 wmove(wnd, row + linesperregion, 0);
295 wdeleteln(wnd);
296 wmove(wnd, row + 3, 0);
297 winsertln(wnd);
299 (void) devstats(row + 3, _col, i);
300 _col += COLWIDTH;
304 static int
305 devstats(int row, int _col, int dn)
307 long double transfers_per_second;
308 long double kb_per_transfer, mb_per_second;
309 long double busy_seconds;
310 int di;
312 di = dev_select[dn].position;
314 busy_seconds = compute_etime(cur.busy_time, last.busy_time);
316 if (compute_stats(&cur.dinfo->devices[di], &last.dinfo->devices[di],
317 busy_seconds, NULL, NULL, NULL,
318 &kb_per_transfer, &transfers_per_second,
319 &mb_per_second, NULL, NULL) != 0)
320 errx(1, "%s", devstat_errbuf);
322 if (numbers) {
323 mvwprintw(wnd, row, _col, " %5.2Lf %3.0Lf %5.2Lf ",
324 kb_per_transfer, transfers_per_second,
325 mb_per_second);
326 return(row);
328 wmove(wnd, row++, _col);
329 histogram(mb_per_second, 50, .5);
330 wmove(wnd, row++, _col);
331 histogram(transfers_per_second, 50, .5);
332 if (kbpt) {
333 wmove(wnd, row++, _col);
334 histogram(kb_per_transfer, 50, .5);
337 return(row);
341 static void
342 stat1(int row, uint64_t difference, uint64_t total)
344 double dtime;
346 if (total > 0)
347 dtime = 100.0 * difference / total;
348 else
349 dtime = 0;
350 wmove(wnd, row, INSET);
351 #define CPUSCALE 0.5
352 histogram(dtime, 50, CPUSCALE);
355 static void
356 histogram(long double val, int colwidth, double scale)
358 char buf[10];
359 int k;
360 int v = (int)(val * scale) + 0.5;
362 if (val <= 0)
363 v = 0;
364 k = MIN(v, colwidth);
365 if (v > colwidth) {
366 snprintf(buf, sizeof(buf), "%5.2Lf", val);
367 k -= strlen(buf);
368 while (k--)
369 waddch(wnd, 'X');
370 waddstr(wnd, buf);
371 return;
373 while (k--)
374 waddch(wnd, 'X');
375 wclrtoeol(wnd);
379 cmdiostat(const char *cmd, char *args)
382 if (prefix(cmd, "kbpt"))
383 kbpt = !kbpt;
384 else if (prefix(cmd, "numbers"))
385 numbers = 1;
386 else if (prefix(cmd, "bars"))
387 numbers = 0;
388 else if (!dscmd(cmd, args, 100, &cur))
389 return (0);
390 wclear(wnd);
391 labeliostat();
392 refresh();
393 return (1);