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_c, flag_d;
55 static int flag_I = 1000000;
57 #define PRINTMSG(...) do { \
58 if (flag_b && !loop) \
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, "adcf:I:b")) != -1) {
122 if (strlen(optarg) > sizeof(f_s) - 1)
123 errx(EX_USAGE, "Filter string too long");
124 if (regcomp(&f_re, optarg, REG_EXTENDED) != 0)
126 "Invalid filter - see re_format(7)");
127 strlcpy(f_s, optarg, sizeof(f_s));
131 i = strtoul(optarg, &p, 0);
132 if (p == optarg || errno == EINVAL ||
134 errx(1, "Invalid argument to -I");
135 } else if (!strcmp(p, "s"))
137 else if (!strcmp(p, "ms"))
139 else if (!strcmp(p, "us"))
153 i = geom_gettree(&gmp);
155 err(1, "geom_gettree = %d", i);
156 error = geom_stats_open();
158 err(1, "geom_stats_open()");
160 sq = geom_stats_snapshot_get();
162 err(1, "geom_stats_snapshot()");
167 use_default_colors();
168 pair_content(0, &cf, &cb);
169 init_pair(1, COLOR_GREEN, cb);
170 init_pair(2, COLOR_MAGENTA, cb);
171 init_pair(3, COLOR_RED, cb);
176 intrflush(stdscr, FALSE);
177 keypad(stdscr, TRUE);
179 hist = history_init();
181 err(EX_SOFTWARE, "history_init()");
182 history(hist, &hist_ev, H_SETSIZE, 100);
183 el = el_init("gstat", stdin, stdout, stderr);
185 err(EX_SOFTWARE, "el_init");
186 el_set(el, EL_EDITOR, "emacs");
187 el_set(el, EL_SIGNAL, 1);
188 el_set(el, EL_HIST, history, hist);
189 el_set(el, EL_PROMPT, el_prompt);
191 history(hist, &hist_ev, H_ENTER, f_s);
193 geom_stats_snapshot_timestamp(sq, &tq);
194 for (quit = 0; !quit;) {
195 sp = geom_stats_snapshot_get();
197 err(1, "geom_stats_snapshot()");
198 geom_stats_snapshot_timestamp(sp, &tp);
199 dt = tp.tv_sec - tq.tv_sec;
200 dt += (tp.tv_nsec - tq.tv_nsec) * 1e-9;
203 geom_stats_snapshot_reset(sp);
204 geom_stats_snapshot_reset(sq);
206 PRINTMSG("dT: %5.3fs w: %.3fs", dt, (float)flag_I / 1000000);
207 if (f_s[0] != '\0') {
208 PRINTMSG(" filter: ");
210 getyx(stdscr, cury, curx);
211 getmaxyx(stdscr, maxy, maxx);
213 strlcpy(pf_s, f_s, sizeof(pf_s));
214 max_flen = maxx - curx - 1;
215 if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
217 pf_s[max_flen - 3] = '.';
219 pf_s[max_flen - 2] = '.';
221 pf_s[max_flen - 1] = '.';
222 pf_s[max_flen] = '\0';
224 PRINTMSG("%s", pf_s);
227 PRINTMSG(" L(q) ops/s ");
228 PRINTMSG(" r/s kBps ms/r ");
229 PRINTMSG(" w/s kBps ms/w ");
231 PRINTMSG(" d/s kBps ms/d ");
232 PRINTMSG("%%busy Name\n");
234 gsp = geom_stats_snapshot_next(sp);
235 gsq = geom_stats_snapshot_next(sq);
236 if (gsp == NULL || gsq == NULL)
240 gid = geom_lookupid(&gmp, gsp->id);
242 geom_deletetree(&gmp);
243 i = geom_gettree(&gmp);
245 err(1, "geom_gettree = %d", i);
246 gid = geom_lookupid(&gmp, gsp->id);
250 if (gid->lg_what == ISCONSUMER && !flag_c)
252 /* Do not print past end of window */
254 getyx(stdscr, cury, curx);
258 if ((gid->lg_what == ISPROVIDER
259 || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
261 if ((regexec(&f_re, pp->lg_name, 0, NULL, 0)
265 if (gsp->sequence0 != gsp->sequence1) {
269 devstat_compute_statistics(gsp, gsq, dt,
270 DSM_QUEUE_LENGTH, &u64,
271 DSM_TRANSFERS_PER_SECOND, &ld[0],
273 DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
274 DSM_MB_PER_SECOND_READ, &ld[2],
275 DSM_MS_PER_TRANSACTION_READ, &ld[3],
277 DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
278 DSM_MB_PER_SECOND_WRITE, &ld[5],
279 DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
281 DSM_BUSY_PCT, &ld[7],
282 DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
283 DSM_MB_PER_SECOND_FREE, &ld[9],
284 DSM_MS_PER_TRANSACTION_FREE, &ld[10],
287 if (flag_a && ld[7] < 0.1) {
292 PRINTMSG(" %4ju", (uintmax_t)u64);
293 PRINTMSG(" %6.0f", (double)ld[0]);
294 PRINTMSG(" %6.0f", (double)ld[1]);
295 PRINTMSG(" %6.0f", (double)ld[2] * 1024);
297 PRINTMSG(" %6.0f", (double)ld[3]);
299 PRINTMSG(" %6.1f", (double)ld[3]);
300 PRINTMSG(" %6.0f", (double)ld[4]);
301 PRINTMSG(" %6.0f", (double)ld[5] * 1024);
303 PRINTMSG(" %6.0f", (double)ld[6]);
305 PRINTMSG(" %6.1f", (double)ld[6]);
308 PRINTMSG(" %6.0f", (double)ld[8]);
309 PRINTMSG(" %6.0f", (double)ld[9] * 1024);
311 PRINTMSG(" %6.0f", (double)ld[10]);
313 PRINTMSG(" %6.1f", (double)ld[10]);
323 attron(COLOR_PAIR(i));
324 PRINTMSG(" %6.1lf", (double)ld[7]);
326 attroff(COLOR_PAIR(i));
332 } else if (gid->lg_what == ISPROVIDER) {
334 PRINTMSG(" %s", pp->lg_name);
335 } else if (gid->lg_what == ISCONSUMER) {
337 PRINTMSG(" %s/%s/%s",
338 cp->lg_geom->lg_class->lg_name,
339 cp->lg_geom->lg_name,
340 cp->lg_provider->lg_name);
347 geom_stats_snapshot_free(sp);
349 /* We loop extra to make sure we get the information. */
356 getyx(stdscr, cury, curx);
357 getmaxyx(stdscr, maxy, maxx);
359 if (maxy - 1 <= cury)
363 while((i = getch()) != ERR) {
380 line = el_gets(el, &line_len);
384 history(hist, &hist_ev, H_ENTER, line);
385 strlcpy(tmp_f_s, line, sizeof(f_s));
386 if ((p = strchr(tmp_f_s, '\n')) != NULL)
389 * We have to clear since we messed up
390 * curses idea of the screen by using
395 if (regcomp(&tmp_f_re, tmp_f_s, REG_EXTENDED)
398 printw("Invalid filter");
402 strlcpy(f_s, tmp_f_s, sizeof(f_s));
428 fprintf(stderr, "usage: gstat [-abcd] [-f filter] [-I interval]\n");