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_C, flag_d, flag_o, flag_p,
58 static int flag_I = 1000000;
60 #define HIGH_PCT_BUSY_THRESH 80
61 #define MEDIUM_PCT_BUSY_THRESH 50
62 #define PRINTMSG(...) do { \
63 if ((flag_b && !loop) || (flag_B)) \
64 printf(__VA_ARGS__); \
66 printw(__VA_ARGS__); \
69 static void usage(void);
79 main(int argc, char **argv)
82 int curx, cury, maxx, maxy, line_len, loop, max_flen, head_printed;
83 struct devstat *gsp, *gsq;
86 struct timespec tp, tq;
91 regex_t f_re, tmp_f_re;
94 char f_s[100], pf_s[100], tmp_f_s[100];
95 char ts[100], g_name[4096];
108 /* Turn on batch mode if output is not tty. */
109 if (!isatty(fileno(stdout)))
113 while ((i = getopt(argc, argv, "abBdcCf:I:ops")) != -1) {
130 /* csv out implies repeating batch mode */
139 if (strlen(optarg) > sizeof(f_s) - 1)
140 errx(EX_USAGE, "Filter string too long");
141 if (regcomp(&f_re, optarg, REG_EXTENDED) != 0)
143 "Invalid filter - see re_format(7)");
144 strlcpy(f_s, optarg, sizeof(f_s));
151 i = strtoul(optarg, &p, 0);
152 if (p == optarg || errno == EINVAL ||
154 errx(1, "Invalid argument to -I");
155 } else if (!strcmp(p, "s"))
157 else if (!strcmp(p, "ms"))
159 else if (!strcmp(p, "us"))
179 i = geom_gettree(&gmp);
181 err(1, "geom_gettree = %d", i);
182 error = geom_stats_open();
184 err(1, "geom_stats_open()");
186 sq = geom_stats_snapshot_get();
188 err(1, "geom_stats_snapshot()");
191 hist = history_init();
193 err(EX_SOFTWARE, "history_init()");
194 history(hist, &hist_ev, H_SETSIZE, 100);
195 el = el_init("gstat", stdin, stdout, stderr);
197 err(EX_SOFTWARE, "el_init");
198 el_set(el, EL_EDITOR, "emacs");
199 el_set(el, EL_SIGNAL, 1);
200 el_set(el, EL_HIST, history, hist);
201 el_set(el, EL_PROMPT, el_prompt);
203 history(hist, &hist_ev, H_ENTER, f_s);
207 use_default_colors();
208 pair_content(0, &cf, &cb);
209 init_pair(1, COLOR_GREEN, cb);
210 init_pair(2, COLOR_MAGENTA, cb);
211 init_pair(3, COLOR_RED, cb);
216 intrflush(stdscr, FALSE);
217 keypad(stdscr, TRUE);
219 geom_stats_snapshot_timestamp(sq, &tq);
220 for (quit = 0; !quit;) {
221 sp = geom_stats_snapshot_get();
223 err(1, "geom_stats_snapshot()");
224 geom_stats_snapshot_timestamp(sp, &tp);
225 dt = tp.tv_sec - tq.tv_sec;
226 dt += (tp.tv_nsec - tq.tv_nsec) * 1e-9;
228 if (flag_C) { /* set timestamp string */
229 (void)strftime(ts,sizeof(ts),
230 "%F %T",localtime(&tq.tv_sec));
231 (void)snprintf(ts,sizeof(ts),
232 "%s.%.9ld",ts,tq.tv_nsec);
235 geom_stats_snapshot_reset(sp);
236 geom_stats_snapshot_reset(sq);
240 PRINTMSG("dT: %5.3fs w: %.3fs", dt,
241 (float)flag_I / 1000000);
242 if (!flag_C && f_s[0] != '\0') {
243 PRINTMSG(" filter: ");
245 getyx(stdscr, cury, curx);
246 getmaxyx(stdscr, maxy, maxx);
248 strlcpy(pf_s, f_s, sizeof(pf_s));
249 max_flen = maxx - curx - 1;
250 if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
252 pf_s[max_flen - 3] = '.';
254 pf_s[max_flen - 2] = '.';
256 pf_s[max_flen - 1] = '.';
257 pf_s[max_flen] = '\0';
259 PRINTMSG("%s", pf_s);
263 PRINTMSG(" L(q) ops/s ");
265 PRINTMSG(" r/s kB kBps ms/r ");
266 PRINTMSG(" w/s kB kBps ms/w ");
269 PRINTMSG(" r/s kBps ms/r ");
270 PRINTMSG(" w/s kBps ms/w ");
274 PRINTMSG(" d/s kB kBps");
277 PRINTMSG(" d/s kBps ms/d ");
280 PRINTMSG(" o/s ms/o ");
281 PRINTMSG("%%busy Name\n");
282 } else if (flag_C && !head_printed) {
283 PRINTMSG("timestamp,name,q-depth,total_ops/s");
285 PRINTMSG(",read/s,read_sz-KiB");
286 PRINTMSG(",read-KiB/s,ms/read");
287 PRINTMSG(",write/s,write_sz-KiB");
288 PRINTMSG(",write-KiB/s,ms/write");
290 PRINTMSG(",read/s,read-KiB/s,ms/read");
291 PRINTMSG(",write/s,write-KiB/s,ms/write");
295 PRINTMSG(",delete/s,delete-sz-KiB");
296 PRINTMSG(",delete-KiB/s,ms/delete");
298 PRINTMSG(",delete/s,delete-KiB/s");
299 PRINTMSG(",ms/delete");
303 PRINTMSG(",other/s,ms/other");
304 PRINTMSG(",%%busy\n");
308 gsp = geom_stats_snapshot_next(sp);
309 gsq = geom_stats_snapshot_next(sq);
310 if (gsp == NULL || gsq == NULL)
314 gid = geom_lookupid(&gmp, gsp->id);
316 geom_deletetree(&gmp);
317 i = geom_gettree(&gmp);
319 err(1, "geom_gettree = %d", i);
320 gid = geom_lookupid(&gmp, gsp->id);
324 if (gid->lg_what == ISCONSUMER && !flag_c)
326 if (flag_p && gid->lg_what == ISPROVIDER &&
327 ((struct gprovider *)
328 (gid->lg_ptr))->lg_geom->lg_rank != 1)
330 /* Do not print past end of window */
332 getyx(stdscr, cury, curx);
336 if ((gid->lg_what == ISPROVIDER
337 || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
339 if ((regexec(&f_re, pp->lg_name, 0, NULL, 0)
343 if (gsp->sequence0 != gsp->sequence1) {
345 * it is ok to skip entire line silently
352 devstat_compute_statistics(gsp, gsq, dt,
353 DSM_QUEUE_LENGTH, &u64,
354 DSM_TRANSFERS_PER_SECOND, &ld[0],
356 DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
357 DSM_MB_PER_SECOND_READ, &ld[2],
358 DSM_MS_PER_TRANSACTION_READ, &ld[3],
360 DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
361 DSM_MB_PER_SECOND_WRITE, &ld[5],
362 DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
364 DSM_BUSY_PCT, &ld[7],
366 DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
367 DSM_MB_PER_SECOND_FREE, &ld[9],
368 DSM_MS_PER_TRANSACTION_FREE, &ld[10],
370 DSM_TRANSFERS_PER_SECOND_OTHER, &ld[11],
371 DSM_MS_PER_TRANSACTION_OTHER, &ld[12],
373 DSM_KB_PER_TRANSFER_READ, &ld[13],
374 DSM_KB_PER_TRANSFER_WRITE, &ld[14],
375 DSM_KB_PER_TRANSFER_FREE, &ld[15],
379 if (flag_a && ld[7] < 0.1) {
384 /* store name for geom device */
386 (void)snprintf(g_name, sizeof(g_name), "??");
387 } else if (gid->lg_what == ISPROVIDER) {
389 (void)snprintf(g_name, sizeof(g_name), "%s",
391 } else if (gid->lg_what == ISCONSUMER) {
393 (void)snprintf(g_name, sizeof(g_name),
395 cp->lg_geom->lg_class->lg_name,
396 cp->lg_geom->lg_name,
397 cp->lg_provider->lg_name);
401 PRINTMSG("%s", ts); /* timestamp */
402 PRINTMSG(",%s", g_name); /* print name */
403 PRINTMSG(",%ju", (uintmax_t)u64);
404 PRINTMSG(",%.0f", (double)ld[0]);
405 PRINTMSG(",%.0f", (double)ld[1]);
407 PRINTMSG(",%.0f", (double)ld[13]);
408 PRINTMSG(",%.0f", (double)ld[2] * 1024);
410 PRINTMSG(",%.0f", (double)ld[3]);
412 PRINTMSG(",%.1f", (double)ld[3]);
413 PRINTMSG(",%.0f", (double)ld[4]);
415 PRINTMSG(",%.0f", (double)ld[14]);
416 PRINTMSG(",%.0f", (double)ld[5] * 1024);
418 PRINTMSG(",%.0f", (double)ld[6]);
420 PRINTMSG(",%.1f", (double)ld[6]);
423 PRINTMSG(",%.0f", (double)ld[8]);
427 PRINTMSG(",%.0f", (double)ld[9] * 1024);
437 PRINTMSG(",%.0f", (double)ld[11]);
445 PRINTMSG(",%.1lf", (double)ld[7]);
447 PRINTMSG(" %4ju", (uintmax_t)u64);
448 PRINTMSG(" %6.0f", (double)ld[0]);
449 PRINTMSG(" %6.0f", (double)ld[1]);
451 PRINTMSG(" %6.0f", (double)ld[13]);
452 PRINTMSG(" %6.0f", (double)ld[2] * 1024);
454 PRINTMSG(" %6.0f", (double)ld[3]);
456 PRINTMSG(" %6.1f", (double)ld[3]);
457 PRINTMSG(" %6.0f", (double)ld[4]);
459 PRINTMSG(" %6.0f", (double)ld[14]);
460 PRINTMSG(" %6.0f", (double)ld[5] * 1024);
462 PRINTMSG(" %6.0f", (double)ld[6]);
464 PRINTMSG(" %6.1f", (double)ld[6]);
467 PRINTMSG(" %6.0f", (double)ld[8]);
472 (double)ld[9] * 1024);
482 PRINTMSG(" %6.0f", (double)ld[11]);
491 if (ld[7] > HIGH_PCT_BUSY_THRESH)
493 else if (ld[7] > MEDIUM_PCT_BUSY_THRESH)
498 attron(COLOR_PAIR(i));
499 PRINTMSG(" %6.1lf", (double)ld[7]);
501 attroff(COLOR_PAIR(i));
505 PRINTMSG(" %s", g_name);
512 geom_stats_snapshot_free(sp);
514 /* We loop extra to make sure we get the information. */
520 if (fflush(stdout) == EOF)
525 getyx(stdscr, cury, curx);
526 getmaxyx(stdscr, maxy, maxx);
528 if (maxy - 1 <= cury)
532 while((i = getch()) != ERR) {
549 line = el_gets(el, &line_len);
553 history(hist, &hist_ev, H_ENTER, line);
554 strlcpy(tmp_f_s, line, sizeof(f_s));
555 if ((p = strchr(tmp_f_s, '\n')) != NULL)
558 * Fix the terminal. We messed up
559 * curses idea of the screen by using
567 if (regcomp(&tmp_f_re, tmp_f_s, REG_EXTENDED)
570 printw("Invalid filter");
574 strlcpy(f_s, tmp_f_s, sizeof(f_s));
600 fprintf(stderr, "usage: gstat [-abBcCdps] [-f filter] [-I interval]\n");