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_c, flag_d;
55 static int flag_I = 1000000;
57 static void usage(void);
67 main(int argc, char **argv)
70 int curx, cury, maxx, maxy, line_len, max_flen;
71 struct devstat *gsp, *gsq;
74 struct timespec tp, tq;
79 regex_t f_re, tmp_f_re;
82 char f_s[100], pf_s[100], tmp_f_s[100];
91 while ((i = getopt(argc, argv, "adcf:I:")) != -1) {
103 if (strlen(optarg) > sizeof(f_s) - 1)
104 errx(EX_USAGE, "Filter string too long");
105 if (regcomp(&f_re, optarg, REG_EXTENDED) != 0)
107 "Invalid filter - see re_format(7)");
108 strncpy(f_s, optarg, sizeof(f_s));
112 i = strtoul(optarg, &p, 0);
113 if (p == optarg || errno == EINVAL ||
115 errx(1, "Invalid argument to -I");
116 } else if (!strcmp(p, "s"))
118 else if (!strcmp(p, "ms"))
120 else if (!strcmp(p, "us"))
134 i = geom_gettree(&gmp);
136 err(1, "geom_gettree = %d", i);
137 error = geom_stats_open();
139 err(1, "geom_stats_open()");
141 sq = geom_stats_snapshot_get();
143 err(1, "geom_stats_snapshot()");
147 use_default_colors();
148 pair_content(0, &cf, &cb);
149 init_pair(1, COLOR_GREEN, cb);
150 init_pair(2, COLOR_MAGENTA, cb);
151 init_pair(3, COLOR_RED, cb);
156 intrflush(stdscr, FALSE);
157 keypad(stdscr, TRUE);
159 hist = history_init();
161 err(EX_SOFTWARE, "history_init()");
162 history(hist, &hist_ev, H_SETSIZE, 100);
163 el = el_init("gstat", stdin, stdout, stderr);
165 err(EX_SOFTWARE, "el_init");
166 el_set(el, EL_EDITOR, "emacs");
167 el_set(el, EL_SIGNAL, 1);
168 el_set(el, EL_HIST, history, hist);
169 el_set(el, EL_PROMPT, el_prompt);
171 history(hist, &hist_ev, H_ENTER, f_s);
172 geom_stats_snapshot_timestamp(sq, &tq);
173 for (quit = 0; !quit;) {
174 sp = geom_stats_snapshot_get();
176 err(1, "geom_stats_snapshot()");
177 geom_stats_snapshot_timestamp(sp, &tp);
178 dt = tp.tv_sec - tq.tv_sec;
179 dt += (tp.tv_nsec - tq.tv_nsec) * 1e-9;
182 geom_stats_snapshot_reset(sp);
183 geom_stats_snapshot_reset(sq);
185 printw("dT: %5.3fs w: %.3fs",
186 dt, (float)flag_I / 1000000);
187 if (f_s[0] != '\0') {
189 getyx(stdscr, cury, curx);
190 getmaxyx(stdscr, maxy, maxx);
191 strncpy(pf_s, f_s, sizeof(pf_s));
192 max_flen = maxx - curx - 1;
193 if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
195 pf_s[max_flen - 3] = '.';
197 pf_s[max_flen - 2] = '.';
199 pf_s[max_flen - 1] = '.';
200 pf_s[max_flen] = '\0';
205 printw(" L(q) ops/s ");
206 printw(" r/s kBps ms/r ");
207 printw(" w/s kBps ms/w ");
209 printw(" d/s kBps ms/d ");
210 printw("%%busy Name\n");
212 gsp = geom_stats_snapshot_next(sp);
213 gsq = geom_stats_snapshot_next(sq);
214 if (gsp == NULL || gsq == NULL)
218 gid = geom_lookupid(&gmp, gsp->id);
220 geom_deletetree(&gmp);
221 i = geom_gettree(&gmp);
223 err(1, "geom_gettree = %d", i);
224 gid = geom_lookupid(&gmp, gsp->id);
228 if (gid->lg_what == ISCONSUMER && !flag_c)
230 /* Do not print past end of window */
231 getyx(stdscr, cury, curx);
234 if ((gid->lg_what == ISPROVIDER
235 || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
237 if ((regexec(&f_re, pp->lg_name, 0, NULL, 0)
241 if (gsp->sequence0 != gsp->sequence1) {
245 devstat_compute_statistics(gsp, gsq, dt,
246 DSM_QUEUE_LENGTH, &u64,
247 DSM_TRANSFERS_PER_SECOND, &ld[0],
249 DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
250 DSM_MB_PER_SECOND_READ, &ld[2],
251 DSM_MS_PER_TRANSACTION_READ, &ld[3],
253 DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
254 DSM_MB_PER_SECOND_WRITE, &ld[5],
255 DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
257 DSM_BUSY_PCT, &ld[7],
258 DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
259 DSM_MB_PER_SECOND_FREE, &ld[9],
260 DSM_MS_PER_TRANSACTION_FREE, &ld[10],
263 if (flag_a && ld[7] < 0.1) {
268 printw(" %4ju", (uintmax_t)u64);
269 printw(" %6.0f", (double)ld[0]);
270 printw(" %6.0f", (double)ld[1]);
271 printw(" %6.0f", (double)ld[2] * 1024);
272 printw(" %6.1f", (double)ld[3]);
273 printw(" %6.0f", (double)ld[4]);
274 printw(" %6.0f", (double)ld[5] * 1024);
275 printw(" %6.1f", (double)ld[6]);
278 printw(" %6.0f", (double)ld[8]);
279 printw(" %6.0f", (double)ld[9] * 1024);
280 printw(" %6.1f", (double)ld[10]);
289 attron(COLOR_PAIR(i));
290 printw(" %6.1lf", (double)ld[7]);
291 attroff(COLOR_PAIR(i));
295 } else if (gid->lg_what == ISPROVIDER) {
297 printw(" %s", pp->lg_name);
298 } else if (gid->lg_what == ISCONSUMER) {
301 cp->lg_geom->lg_class->lg_name,
302 cp->lg_geom->lg_name,
303 cp->lg_provider->lg_name);
309 geom_stats_snapshot_free(sp);
310 getyx(stdscr, cury, curx);
311 getmaxyx(stdscr, maxy, maxx);
313 if (maxy - 1 <= cury)
317 while((i = getch()) != ERR) {
334 line = el_gets(el, &line_len);
338 history(hist, &hist_ev, H_ENTER, line);
339 strncpy(tmp_f_s, line, sizeof(f_s));
340 if ((p = strchr(tmp_f_s, '\n')) != NULL)
343 * We have to clear since we messed up
344 * curses idea of the screen by using
349 if (regcomp(&tmp_f_re, tmp_f_s, REG_EXTENDED)
352 printw("Invalid filter");
356 strncpy(f_s, tmp_f_s, sizeof(f_s));
380 fprintf(stderr, "usage: gstat [-acd] [-f filter] [-I interval]\n");