2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/devicestat.h>
37 #include <sys/resource.h>
56 static int flag_a, flag_b, flag_B, flag_c, flag_d, flag_o, flag_p, flag_s;
57 static int flag_I = 1000000;
59 #define PRINTMSG(...) do { \
60 if ((flag_b && !loop) || (flag_B)) \
61 printf(__VA_ARGS__); \
63 printw(__VA_ARGS__); \
66 static void usage(void);
76 main(int argc, char **argv)
79 int curx, cury, maxx, maxy, line_len, loop, max_flen;
80 struct devstat *gsp, *gsq;
83 struct timespec tp, tq;
88 regex_t f_re, tmp_f_re;
91 char f_s[100], pf_s[100], tmp_f_s[100];
104 /* Turn on batch mode if output is not tty. */
105 if (!isatty(fileno(stdout)))
109 while ((i = getopt(argc, argv, "abBdcf:I:ops")) != -1) {
128 if (strlen(optarg) > sizeof(f_s) - 1)
129 errx(EX_USAGE, "Filter string too long");
130 if (regcomp(&f_re, optarg, REG_EXTENDED) != 0)
132 "Invalid filter - see re_format(7)");
133 strlcpy(f_s, optarg, sizeof(f_s));
140 i = strtoul(optarg, &p, 0);
141 if (p == optarg || errno == EINVAL ||
143 errx(1, "Invalid argument to -I");
144 } else if (!strcmp(p, "s"))
146 else if (!strcmp(p, "ms"))
148 else if (!strcmp(p, "us"))
168 i = geom_gettree(&gmp);
170 err(1, "geom_gettree = %d", i);
171 error = geom_stats_open();
173 err(1, "geom_stats_open()");
175 sq = geom_stats_snapshot_get();
177 err(1, "geom_stats_snapshot()");
180 hist = history_init();
182 err(EX_SOFTWARE, "history_init()");
183 history(hist, &hist_ev, H_SETSIZE, 100);
184 el = el_init("gstat", stdin, stdout, stderr);
186 err(EX_SOFTWARE, "el_init");
187 el_set(el, EL_EDITOR, "emacs");
188 el_set(el, EL_SIGNAL, 1);
189 el_set(el, EL_HIST, history, hist);
190 el_set(el, EL_PROMPT, el_prompt);
192 history(hist, &hist_ev, H_ENTER, f_s);
196 use_default_colors();
197 pair_content(0, &cf, &cb);
198 init_pair(1, COLOR_GREEN, cb);
199 init_pair(2, COLOR_MAGENTA, cb);
200 init_pair(3, COLOR_RED, cb);
205 intrflush(stdscr, FALSE);
206 keypad(stdscr, TRUE);
208 geom_stats_snapshot_timestamp(sq, &tq);
209 for (quit = 0; !quit;) {
210 sp = geom_stats_snapshot_get();
212 err(1, "geom_stats_snapshot()");
213 geom_stats_snapshot_timestamp(sp, &tp);
214 dt = tp.tv_sec - tq.tv_sec;
215 dt += (tp.tv_nsec - tq.tv_nsec) * 1e-9;
218 geom_stats_snapshot_reset(sp);
219 geom_stats_snapshot_reset(sq);
222 PRINTMSG("dT: %5.3fs w: %.3fs", dt, (float)flag_I / 1000000);
223 if (f_s[0] != '\0') {
224 PRINTMSG(" filter: ");
226 getyx(stdscr, cury, curx);
227 getmaxyx(stdscr, maxy, maxx);
229 strlcpy(pf_s, f_s, sizeof(pf_s));
230 max_flen = maxx - curx - 1;
231 if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
233 pf_s[max_flen - 3] = '.';
235 pf_s[max_flen - 2] = '.';
237 pf_s[max_flen - 1] = '.';
238 pf_s[max_flen] = '\0';
240 PRINTMSG("%s", pf_s);
243 PRINTMSG(" L(q) ops/s ");
245 PRINTMSG(" r/s kB kBps ms/r ");
246 PRINTMSG(" w/s kB kBps ms/w ");
249 PRINTMSG(" r/s kBps ms/r ");
250 PRINTMSG(" w/s kBps ms/w ");
254 PRINTMSG(" d/s kB kBps ms/d ");
256 PRINTMSG(" d/s kBps ms/d ");
259 PRINTMSG(" o/s ms/o ");
260 PRINTMSG("%%busy Name\n");
262 gsp = geom_stats_snapshot_next(sp);
263 gsq = geom_stats_snapshot_next(sq);
264 if (gsp == NULL || gsq == NULL)
268 gid = geom_lookupid(&gmp, gsp->id);
270 geom_deletetree(&gmp);
271 i = geom_gettree(&gmp);
273 err(1, "geom_gettree = %d", i);
274 gid = geom_lookupid(&gmp, gsp->id);
278 if (gid->lg_what == ISCONSUMER && !flag_c)
280 if (flag_p && gid->lg_what == ISPROVIDER &&
281 ((struct gprovider *)(gid->lg_ptr))->lg_geom->lg_rank != 1)
283 /* Do not print past end of window */
285 getyx(stdscr, cury, curx);
289 if ((gid->lg_what == ISPROVIDER
290 || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
292 if ((regexec(&f_re, pp->lg_name, 0, NULL, 0)
296 if (gsp->sequence0 != gsp->sequence1) {
300 devstat_compute_statistics(gsp, gsq, dt,
301 DSM_QUEUE_LENGTH, &u64,
302 DSM_TRANSFERS_PER_SECOND, &ld[0],
304 DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
305 DSM_MB_PER_SECOND_READ, &ld[2],
306 DSM_MS_PER_TRANSACTION_READ, &ld[3],
308 DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
309 DSM_MB_PER_SECOND_WRITE, &ld[5],
310 DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
312 DSM_BUSY_PCT, &ld[7],
314 DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
315 DSM_MB_PER_SECOND_FREE, &ld[9],
316 DSM_MS_PER_TRANSACTION_FREE, &ld[10],
318 DSM_TRANSFERS_PER_SECOND_OTHER, &ld[11],
319 DSM_MS_PER_TRANSACTION_OTHER, &ld[12],
321 DSM_KB_PER_TRANSFER_READ, &ld[13],
322 DSM_KB_PER_TRANSFER_WRITE, &ld[14],
323 DSM_KB_PER_TRANSFER_FREE, &ld[15],
327 if (flag_a && ld[7] < 0.1) {
332 PRINTMSG(" %4ju", (uintmax_t)u64);
333 PRINTMSG(" %6.0f", (double)ld[0]);
334 PRINTMSG(" %6.0f", (double)ld[1]);
336 PRINTMSG(" %6.0f", (double)ld[13]);
337 PRINTMSG(" %6.0f", (double)ld[2] * 1024);
339 PRINTMSG(" %6.0f", (double)ld[3]);
341 PRINTMSG(" %6.1f", (double)ld[3]);
342 PRINTMSG(" %6.0f", (double)ld[4]);
344 PRINTMSG(" %6.0f", (double)ld[14]);
345 PRINTMSG(" %6.0f", (double)ld[5] * 1024);
347 PRINTMSG(" %6.0f", (double)ld[6]);
349 PRINTMSG(" %6.1f", (double)ld[6]);
352 PRINTMSG(" %6.0f", (double)ld[8]);
354 PRINTMSG(" %6.0f", (double)ld[15]);
355 PRINTMSG(" %6.0f", (double)ld[9] * 1024);
357 PRINTMSG(" %6.0f", (double)ld[10]);
359 PRINTMSG(" %6.1f", (double)ld[10]);
363 PRINTMSG(" %6.0f", (double)ld[11]);
365 PRINTMSG(" %6.0f", (double)ld[12]);
367 PRINTMSG(" %6.1f", (double)ld[12]);
377 attron(COLOR_PAIR(i));
378 PRINTMSG(" %6.1lf", (double)ld[7]);
380 attroff(COLOR_PAIR(i));
386 } else if (gid->lg_what == ISPROVIDER) {
388 PRINTMSG(" %s", pp->lg_name);
389 } else if (gid->lg_what == ISCONSUMER) {
391 PRINTMSG(" %s/%s/%s",
392 cp->lg_geom->lg_class->lg_name,
393 cp->lg_geom->lg_name,
394 cp->lg_provider->lg_name);
401 geom_stats_snapshot_free(sp);
403 /* We loop extra to make sure we get the information. */
413 getyx(stdscr, cury, curx);
414 getmaxyx(stdscr, maxy, maxx);
416 if (maxy - 1 <= cury)
420 while((i = getch()) != ERR) {
437 line = el_gets(el, &line_len);
441 history(hist, &hist_ev, H_ENTER, line);
442 strlcpy(tmp_f_s, line, sizeof(f_s));
443 if ((p = strchr(tmp_f_s, '\n')) != NULL)
446 * Fix the terminal. We messed up
447 * curses idea of the screen by using
455 if (regcomp(&tmp_f_re, tmp_f_s, REG_EXTENDED)
458 printw("Invalid filter");
462 strlcpy(f_s, tmp_f_s, sizeof(f_s));
488 fprintf(stderr, "usage: gstat [-abBcdps] [-f filter] [-I interval]\n");