2 * Copyright (c) 2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * 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_d, flag_o, flag_p, flag_s;
55 static int flag_I = 1000000;
57 #define PRINTMSG(...) do { \
58 if ((flag_b && !loop) || (flag_B)) \
59 printf(__VA_ARGS__); \
61 printw(__VA_ARGS__); \
64 static void usage(void);
74 main(int argc, char **argv)
77 int curx, cury, maxx, maxy, line_len, loop, max_flen;
78 struct devstat *gsp, *gsq;
81 struct timespec tp, tq;
86 regex_t f_re, tmp_f_re;
89 char f_s[100], pf_s[100], tmp_f_s[100];
102 /* Turn on batch mode if output is not tty. */
103 if (!isatty(fileno(stdout)))
107 while ((i = getopt(argc, argv, "abBdcf:I:ops")) != -1) {
126 if (strlen(optarg) > sizeof(f_s) - 1)
127 errx(EX_USAGE, "Filter string too long");
128 if (regcomp(&f_re, optarg, REG_EXTENDED) != 0)
130 "Invalid filter - see re_format(7)");
131 strlcpy(f_s, optarg, sizeof(f_s));
138 i = strtoul(optarg, &p, 0);
139 if (p == optarg || errno == EINVAL ||
141 errx(1, "Invalid argument to -I");
142 } else if (!strcmp(p, "s"))
144 else if (!strcmp(p, "ms"))
146 else if (!strcmp(p, "us"))
166 i = geom_gettree(&gmp);
168 err(1, "geom_gettree = %d", i);
169 error = geom_stats_open();
171 err(1, "geom_stats_open()");
173 sq = geom_stats_snapshot_get();
175 err(1, "geom_stats_snapshot()");
178 hist = history_init();
180 err(EX_SOFTWARE, "history_init()");
181 history(hist, &hist_ev, H_SETSIZE, 100);
182 el = el_init("gstat", stdin, stdout, stderr);
184 err(EX_SOFTWARE, "el_init");
185 el_set(el, EL_EDITOR, "emacs");
186 el_set(el, EL_SIGNAL, 1);
187 el_set(el, EL_HIST, history, hist);
188 el_set(el, EL_PROMPT, el_prompt);
190 history(hist, &hist_ev, H_ENTER, f_s);
194 use_default_colors();
195 pair_content(0, &cf, &cb);
196 init_pair(1, COLOR_GREEN, cb);
197 init_pair(2, COLOR_MAGENTA, cb);
198 init_pair(3, COLOR_RED, cb);
203 intrflush(stdscr, FALSE);
204 keypad(stdscr, TRUE);
206 geom_stats_snapshot_timestamp(sq, &tq);
207 for (quit = 0; !quit;) {
208 sp = geom_stats_snapshot_get();
210 err(1, "geom_stats_snapshot()");
211 geom_stats_snapshot_timestamp(sp, &tp);
212 dt = tp.tv_sec - tq.tv_sec;
213 dt += (tp.tv_nsec - tq.tv_nsec) * 1e-9;
216 geom_stats_snapshot_reset(sp);
217 geom_stats_snapshot_reset(sq);
220 PRINTMSG("dT: %5.3fs w: %.3fs", dt, (float)flag_I / 1000000);
221 if (f_s[0] != '\0') {
222 PRINTMSG(" filter: ");
224 getyx(stdscr, cury, curx);
225 getmaxyx(stdscr, maxy, maxx);
227 strlcpy(pf_s, f_s, sizeof(pf_s));
228 max_flen = maxx - curx - 1;
229 if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
231 pf_s[max_flen - 3] = '.';
233 pf_s[max_flen - 2] = '.';
235 pf_s[max_flen - 1] = '.';
236 pf_s[max_flen] = '\0';
238 PRINTMSG("%s", pf_s);
241 PRINTMSG(" L(q) ops/s ");
243 PRINTMSG(" r/s kB kBps ms/r ");
244 PRINTMSG(" w/s kB kBps ms/w ");
247 PRINTMSG(" r/s kBps ms/r ");
248 PRINTMSG(" w/s kBps ms/w ");
252 PRINTMSG(" d/s kB kBps ms/d ");
254 PRINTMSG(" d/s kBps ms/d ");
257 PRINTMSG(" o/s ms/o ");
258 PRINTMSG("%%busy Name\n");
260 gsp = geom_stats_snapshot_next(sp);
261 gsq = geom_stats_snapshot_next(sq);
262 if (gsp == NULL || gsq == NULL)
266 gid = geom_lookupid(&gmp, gsp->id);
268 geom_deletetree(&gmp);
269 i = geom_gettree(&gmp);
271 err(1, "geom_gettree = %d", i);
272 gid = geom_lookupid(&gmp, gsp->id);
276 if (gid->lg_what == ISCONSUMER && !flag_c)
278 if (flag_p && gid->lg_what == ISPROVIDER &&
279 ((struct gprovider *)(gid->lg_ptr))->lg_geom->lg_rank != 1)
281 /* Do not print past end of window */
283 getyx(stdscr, cury, curx);
287 if ((gid->lg_what == ISPROVIDER
288 || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
290 if ((regexec(&f_re, pp->lg_name, 0, NULL, 0)
294 if (gsp->sequence0 != gsp->sequence1) {
298 devstat_compute_statistics(gsp, gsq, dt,
299 DSM_QUEUE_LENGTH, &u64,
300 DSM_TRANSFERS_PER_SECOND, &ld[0],
302 DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
303 DSM_MB_PER_SECOND_READ, &ld[2],
304 DSM_MS_PER_TRANSACTION_READ, &ld[3],
306 DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
307 DSM_MB_PER_SECOND_WRITE, &ld[5],
308 DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
310 DSM_BUSY_PCT, &ld[7],
312 DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
313 DSM_MB_PER_SECOND_FREE, &ld[9],
314 DSM_MS_PER_TRANSACTION_FREE, &ld[10],
316 DSM_TRANSFERS_PER_SECOND_OTHER, &ld[11],
317 DSM_MS_PER_TRANSACTION_OTHER, &ld[12],
319 DSM_KB_PER_TRANSFER_READ, &ld[13],
320 DSM_KB_PER_TRANSFER_WRITE, &ld[14],
321 DSM_KB_PER_TRANSFER_FREE, &ld[15],
325 if (flag_a && ld[7] < 0.1) {
330 PRINTMSG(" %4ju", (uintmax_t)u64);
331 PRINTMSG(" %6.0f", (double)ld[0]);
332 PRINTMSG(" %6.0f", (double)ld[1]);
334 PRINTMSG(" %6.0f", (double)ld[13]);
335 PRINTMSG(" %6.0f", (double)ld[2] * 1024);
337 PRINTMSG(" %6.0f", (double)ld[3]);
339 PRINTMSG(" %6.1f", (double)ld[3]);
340 PRINTMSG(" %6.0f", (double)ld[4]);
342 PRINTMSG(" %6.0f", (double)ld[14]);
343 PRINTMSG(" %6.0f", (double)ld[5] * 1024);
345 PRINTMSG(" %6.0f", (double)ld[6]);
347 PRINTMSG(" %6.1f", (double)ld[6]);
350 PRINTMSG(" %6.0f", (double)ld[8]);
352 PRINTMSG(" %6.0f", (double)ld[15]);
353 PRINTMSG(" %6.0f", (double)ld[9] * 1024);
355 PRINTMSG(" %6.0f", (double)ld[10]);
357 PRINTMSG(" %6.1f", (double)ld[10]);
361 PRINTMSG(" %6.0f", (double)ld[11]);
363 PRINTMSG(" %6.0f", (double)ld[12]);
365 PRINTMSG(" %6.1f", (double)ld[12]);
375 attron(COLOR_PAIR(i));
376 PRINTMSG(" %6.1lf", (double)ld[7]);
378 attroff(COLOR_PAIR(i));
384 } else if (gid->lg_what == ISPROVIDER) {
386 PRINTMSG(" %s", pp->lg_name);
387 } else if (gid->lg_what == ISCONSUMER) {
389 PRINTMSG(" %s/%s/%s",
390 cp->lg_geom->lg_class->lg_name,
391 cp->lg_geom->lg_name,
392 cp->lg_provider->lg_name);
399 geom_stats_snapshot_free(sp);
401 /* We loop extra to make sure we get the information. */
411 getyx(stdscr, cury, curx);
412 getmaxyx(stdscr, maxy, maxx);
414 if (maxy - 1 <= cury)
418 while((i = getch()) != ERR) {
435 line = el_gets(el, &line_len);
439 history(hist, &hist_ev, H_ENTER, line);
440 strlcpy(tmp_f_s, line, sizeof(f_s));
441 if ((p = strchr(tmp_f_s, '\n')) != NULL)
444 * Fix the terminal. We messed up
445 * curses idea of the screen by using
453 if (regcomp(&tmp_f_re, tmp_f_s, REG_EXTENDED)
456 printw("Invalid filter");
460 strlcpy(f_s, tmp_f_s, sizeof(f_s));
486 fprintf(stderr, "usage: gstat [-abBcdps] [-f filter] [-I interval]\n");