2 * SPDX-License-Identifier: BSD-3-Clause
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
33 #include <sys/devicestat.h>
35 #include <sys/resource.h>
54 static int flag_a, flag_b, flag_B, flag_c, flag_C, flag_d, flag_o, flag_p,
56 static int flag_I = 1000000;
58 #define HIGH_PCT_BUSY_THRESH 80
59 #define MEDIUM_PCT_BUSY_THRESH 50
60 #define PRINTMSG(...) do { \
61 if ((flag_b && !loop) || (flag_B)) \
62 printf(__VA_ARGS__); \
64 printw(__VA_ARGS__); \
67 static void usage(void) __dead2;
77 main(int argc, char **argv)
80 int curx, cury, maxx, maxy, line_len, loop, max_flen, head_printed;
81 struct devstat *gsp, *gsq;
84 struct timespec tp, tq;
89 regex_t f_re, tmp_f_re;
92 char f_s[100], pf_s[100], tmp_f_s[100];
93 char ts[100], g_name[4096];
106 /* Turn on batch mode if output is not tty. */
107 if (!isatty(fileno(stdout)))
111 while ((i = getopt(argc, argv, "abBdcCf:I:ops")) != -1) {
128 /* csv out implies repeating batch mode */
137 if (strlen(optarg) > sizeof(f_s) - 1)
138 errx(EX_USAGE, "Filter string too long");
139 if (regcomp(&f_re, optarg, REG_EXTENDED) != 0)
141 "Invalid filter - see re_format(7)");
142 strlcpy(f_s, optarg, sizeof(f_s));
149 i = strtoul(optarg, &p, 0);
150 if (p == optarg || errno == EINVAL ||
152 errx(1, "Invalid argument to -I");
153 } else if (!strcmp(p, "s"))
155 else if (!strcmp(p, "ms"))
157 else if (!strcmp(p, "us"))
177 i = geom_gettree(&gmp);
179 err(1, "geom_gettree = %d", i);
180 error = geom_stats_open();
182 err(1, "geom_stats_open()");
184 sq = geom_stats_snapshot_get();
186 err(1, "geom_stats_snapshot()");
189 hist = history_init();
191 err(EX_SOFTWARE, "history_init()");
192 history(hist, &hist_ev, H_SETSIZE, 100);
193 el = el_init("gstat", stdin, stdout, stderr);
195 err(EX_SOFTWARE, "el_init");
196 el_set(el, EL_EDITOR, "emacs");
197 el_set(el, EL_SIGNAL, 1);
198 el_set(el, EL_HIST, history, hist);
199 el_set(el, EL_PROMPT, el_prompt);
201 history(hist, &hist_ev, H_ENTER, f_s);
205 use_default_colors();
206 pair_content(0, &cf, &cb);
207 init_pair(1, COLOR_GREEN, cb);
208 init_pair(2, COLOR_MAGENTA, cb);
209 init_pair(3, COLOR_RED, cb);
214 intrflush(stdscr, FALSE);
215 keypad(stdscr, TRUE);
217 geom_stats_snapshot_timestamp(sq, &tq);
218 for (quit = 0; !quit;) {
219 sp = geom_stats_snapshot_get();
221 err(1, "geom_stats_snapshot()");
222 geom_stats_snapshot_timestamp(sp, &tp);
223 dt = tp.tv_sec - tq.tv_sec;
224 dt += (tp.tv_nsec - tq.tv_nsec) * 1e-9;
226 if (flag_C) { /* set timestamp string */
227 (void)strftime(ts,sizeof(ts),
228 "%F %T",localtime(&tq.tv_sec));
229 (void)snprintf(ts,sizeof(ts),
230 "%s.%.9ld",ts,tq.tv_nsec);
233 geom_stats_snapshot_reset(sp);
234 geom_stats_snapshot_reset(sq);
238 PRINTMSG("dT: %5.3fs w: %.3fs", dt,
239 (float)flag_I / 1000000);
240 if (!flag_C && f_s[0] != '\0') {
241 PRINTMSG(" filter: ");
243 getyx(stdscr, cury, curx);
244 getmaxyx(stdscr, maxy, maxx);
246 strlcpy(pf_s, f_s, sizeof(pf_s));
247 max_flen = maxx - curx - 1;
248 if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
250 pf_s[max_flen - 3] = '.';
252 pf_s[max_flen - 2] = '.';
254 pf_s[max_flen - 1] = '.';
255 pf_s[max_flen] = '\0';
257 PRINTMSG("%s", pf_s);
261 PRINTMSG(" L(q) ops/s ");
263 PRINTMSG(" r/s kB kBps ms/r ");
264 PRINTMSG(" w/s kB kBps ms/w ");
267 PRINTMSG(" r/s kBps ms/r ");
268 PRINTMSG(" w/s kBps ms/w ");
272 PRINTMSG(" d/s kB kBps");
275 PRINTMSG(" d/s kBps ms/d ");
278 PRINTMSG(" o/s ms/o ");
279 PRINTMSG("%%busy Name\n");
280 } else if (flag_C && !head_printed) {
281 PRINTMSG("timestamp,name,q-depth,total_ops/s");
283 PRINTMSG(",read/s,read_sz-KiB");
284 PRINTMSG(",read-KiB/s,ms/read");
285 PRINTMSG(",write/s,write_sz-KiB");
286 PRINTMSG(",write-KiB/s,ms/write");
288 PRINTMSG(",read/s,read-KiB/s,ms/read");
289 PRINTMSG(",write/s,write-KiB/s,ms/write");
293 PRINTMSG(",delete/s,delete-sz-KiB");
294 PRINTMSG(",delete-KiB/s,ms/delete");
296 PRINTMSG(",delete/s,delete-KiB/s");
297 PRINTMSG(",ms/delete");
301 PRINTMSG(",other/s,ms/other");
302 PRINTMSG(",%%busy\n");
306 gsp = geom_stats_snapshot_next(sp);
307 gsq = geom_stats_snapshot_next(sq);
308 if (gsp == NULL || gsq == NULL)
312 gid = geom_lookupid(&gmp, gsp->id);
314 geom_deletetree(&gmp);
315 i = geom_gettree(&gmp);
317 err(1, "geom_gettree = %d", i);
318 gid = geom_lookupid(&gmp, gsp->id);
322 if (gid->lg_what == ISCONSUMER && !flag_c)
324 if (flag_p && gid->lg_what == ISPROVIDER &&
325 ((struct gprovider *)
326 (gid->lg_ptr))->lg_geom->lg_rank != 1)
328 /* Do not print past end of window */
330 getyx(stdscr, cury, curx);
334 if ((gid->lg_what == ISPROVIDER
335 || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
337 if ((regexec(&f_re, pp->lg_name, 0, NULL, 0)
341 if (gsp->sequence0 != gsp->sequence1) {
343 * it is ok to skip entire line silently
350 devstat_compute_statistics(gsp, gsq, dt,
351 DSM_QUEUE_LENGTH, &u64,
352 DSM_TRANSFERS_PER_SECOND, &ld[0],
354 DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
355 DSM_MB_PER_SECOND_READ, &ld[2],
356 DSM_MS_PER_TRANSACTION_READ, &ld[3],
358 DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
359 DSM_MB_PER_SECOND_WRITE, &ld[5],
360 DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
362 DSM_BUSY_PCT, &ld[7],
364 DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
365 DSM_MB_PER_SECOND_FREE, &ld[9],
366 DSM_MS_PER_TRANSACTION_FREE, &ld[10],
368 DSM_TRANSFERS_PER_SECOND_OTHER, &ld[11],
369 DSM_MS_PER_TRANSACTION_OTHER, &ld[12],
371 DSM_KB_PER_TRANSFER_READ, &ld[13],
372 DSM_KB_PER_TRANSFER_WRITE, &ld[14],
373 DSM_KB_PER_TRANSFER_FREE, &ld[15],
377 if (flag_a && ld[7] < 0.1) {
382 /* store name for geom device */
384 (void)snprintf(g_name, sizeof(g_name), "??");
385 } else if (gid->lg_what == ISPROVIDER) {
387 (void)snprintf(g_name, sizeof(g_name), "%s",
389 } else if (gid->lg_what == ISCONSUMER) {
391 (void)snprintf(g_name, sizeof(g_name),
393 cp->lg_geom->lg_class->lg_name,
394 cp->lg_geom->lg_name,
395 cp->lg_provider->lg_name);
399 PRINTMSG("%s", ts); /* timestamp */
400 PRINTMSG(",%s", g_name); /* print name */
401 PRINTMSG(",%ju", (uintmax_t)u64);
402 PRINTMSG(",%.0f", (double)ld[0]);
403 PRINTMSG(",%.0f", (double)ld[1]);
405 PRINTMSG(",%.0f", (double)ld[13]);
406 PRINTMSG(",%.0f", (double)ld[2] * 1024);
408 PRINTMSG(",%.0f", (double)ld[3]);
410 PRINTMSG(",%.1f", (double)ld[3]);
411 PRINTMSG(",%.0f", (double)ld[4]);
413 PRINTMSG(",%.0f", (double)ld[14]);
414 PRINTMSG(",%.0f", (double)ld[5] * 1024);
416 PRINTMSG(",%.0f", (double)ld[6]);
418 PRINTMSG(",%.1f", (double)ld[6]);
421 PRINTMSG(",%.0f", (double)ld[8]);
425 PRINTMSG(",%.0f", (double)ld[9] * 1024);
435 PRINTMSG(",%.0f", (double)ld[11]);
443 PRINTMSG(",%.1lf", (double)ld[7]);
445 PRINTMSG(" %4ju", (uintmax_t)u64);
446 PRINTMSG(" %6.0f", (double)ld[0]);
447 PRINTMSG(" %6.0f", (double)ld[1]);
449 PRINTMSG(" %6.0f", (double)ld[13]);
450 PRINTMSG(" %6.0f", (double)ld[2] * 1024);
452 PRINTMSG(" %6.0f", (double)ld[3]);
454 PRINTMSG(" %6.1f", (double)ld[3]);
455 PRINTMSG(" %6.0f", (double)ld[4]);
457 PRINTMSG(" %6.0f", (double)ld[14]);
458 PRINTMSG(" %6.0f", (double)ld[5] * 1024);
460 PRINTMSG(" %6.0f", (double)ld[6]);
462 PRINTMSG(" %6.1f", (double)ld[6]);
465 PRINTMSG(" %6.0f", (double)ld[8]);
470 (double)ld[9] * 1024);
480 PRINTMSG(" %6.0f", (double)ld[11]);
489 if (ld[7] > HIGH_PCT_BUSY_THRESH)
491 else if (ld[7] > MEDIUM_PCT_BUSY_THRESH)
496 attron(COLOR_PAIR(i));
497 PRINTMSG(" %6.1lf", (double)ld[7]);
499 attroff(COLOR_PAIR(i));
503 PRINTMSG(" %s", g_name);
510 geom_stats_snapshot_free(sp);
512 /* We loop extra to make sure we get the information. */
518 if (fflush(stdout) == EOF)
523 getyx(stdscr, cury, curx);
524 getmaxyx(stdscr, maxy, maxx);
526 if (maxy - 1 <= cury)
530 while((i = getch()) != ERR) {
547 line = el_gets(el, &line_len);
551 history(hist, &hist_ev, H_ENTER, line);
552 strlcpy(tmp_f_s, line, sizeof(f_s));
553 if ((p = strchr(tmp_f_s, '\n')) != NULL)
556 * Fix the terminal. We messed up
557 * curses idea of the screen by using
565 if (regcomp(&tmp_f_re, tmp_f_s, REG_EXTENDED)
568 printw("Invalid filter");
572 strlcpy(f_s, tmp_f_s, sizeof(f_s));
598 fprintf(stderr, "usage: gstat [-abBcCdps] [-f filter] [-I interval]\n");