PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.sbin/gstat/gstat.c

https://bitbucket.org/freebsd/freebsd-base
C | 602 lines | 523 code | 30 blank | 49 comment | 153 complexity | 79ca329cea98f71ebc5bef6364616417 MD5 | raw file
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2003 Poul-Henning Kamp
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  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
  17. * permission.
  18. *
  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
  29. * SUCH DAMAGE.
  30. *
  31. * $FreeBSD$
  32. */
  33. #include <sys/devicestat.h>
  34. #include <sys/mman.h>
  35. #include <sys/resource.h>
  36. #include <sys/time.h>
  37. #include <curses.h>
  38. #include <devstat.h>
  39. #include <err.h>
  40. #include <errno.h>
  41. #include <fcntl.h>
  42. #include <histedit.h>
  43. #include <libgeom.h>
  44. #include <paths.h>
  45. #include <regex.h>
  46. #include <stdint.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <sysexits.h>
  51. #include <unistd.h>
  52. static int flag_a, flag_b, flag_B, flag_c, flag_C, flag_d, flag_o, flag_p,
  53. flag_s;
  54. static int flag_I = 1000000;
  55. #define HIGH_PCT_BUSY_THRESH 80
  56. #define MEDIUM_PCT_BUSY_THRESH 50
  57. #define PRINTMSG(...) do { \
  58. if ((flag_b && !loop) || (flag_B)) \
  59. printf(__VA_ARGS__); \
  60. else if (!flag_b) \
  61. printw(__VA_ARGS__); \
  62. } while(0)
  63. static void usage(void);
  64. static const char*
  65. el_prompt(void)
  66. {
  67. return ("Filter: ");
  68. }
  69. int
  70. main(int argc, char **argv)
  71. {
  72. int error, i, quit;
  73. int curx, cury, maxx, maxy, line_len, loop, max_flen, head_printed;
  74. struct devstat *gsp, *gsq;
  75. void *sp, *sq;
  76. double dt;
  77. struct timespec tp, tq;
  78. struct gmesh gmp;
  79. struct gprovider *pp;
  80. struct gconsumer *cp;
  81. struct gident *gid;
  82. regex_t f_re, tmp_f_re;
  83. short cf, cb;
  84. char *p;
  85. char f_s[100], pf_s[100], tmp_f_s[100];
  86. char ts[100], g_name[4096];
  87. const char *line;
  88. long double ld[16];
  89. uint64_t u64;
  90. EditLine *el;
  91. History *hist;
  92. HistEvent hist_ev;
  93. hist = NULL;
  94. el = NULL;
  95. maxx = -1;
  96. curx = -1;
  97. loop = 1;
  98. /* Turn on batch mode if output is not tty. */
  99. if (!isatty(fileno(stdout)))
  100. flag_b = 1;
  101. f_s[0] = '\0';
  102. while ((i = getopt(argc, argv, "abBdcCf:I:ops")) != -1) {
  103. switch (i) {
  104. case 'a':
  105. flag_a = 1;
  106. break;
  107. case 'b':
  108. flag_b = 1;
  109. break;
  110. case 'B':
  111. flag_B = 1;
  112. flag_b = 1;
  113. break;
  114. case 'c':
  115. flag_c = 1;
  116. break;
  117. case 'C':
  118. flag_C = 1;
  119. /* csv out implies repeating batch mode */
  120. flag_b = 1;
  121. flag_B = 1;
  122. head_printed = 0;
  123. break;
  124. case 'd':
  125. flag_d = 1;
  126. break;
  127. case 'f':
  128. if (strlen(optarg) > sizeof(f_s) - 1)
  129. errx(EX_USAGE, "Filter string too long");
  130. if (regcomp(&f_re, optarg, REG_EXTENDED) != 0)
  131. errx(EX_USAGE,
  132. "Invalid filter - see re_format(7)");
  133. strlcpy(f_s, optarg, sizeof(f_s));
  134. break;
  135. case 'o':
  136. flag_o = 1;
  137. break;
  138. case 'I':
  139. p = NULL;
  140. i = strtoul(optarg, &p, 0);
  141. if (p == optarg || errno == EINVAL ||
  142. errno == ERANGE) {
  143. errx(1, "Invalid argument to -I");
  144. } else if (!strcmp(p, "s"))
  145. i *= 1000000;
  146. else if (!strcmp(p, "ms"))
  147. i *= 1000;
  148. else if (!strcmp(p, "us"))
  149. i *= 1;
  150. flag_I = i;
  151. break;
  152. case 'p':
  153. flag_p = 1;
  154. break;
  155. case 's':
  156. flag_s = 1;
  157. break;
  158. case '?':
  159. default:
  160. usage();
  161. }
  162. }
  163. argc -= optind;
  164. argv += optind;
  165. if (argc != 0)
  166. usage();
  167. i = geom_gettree(&gmp);
  168. if (i != 0)
  169. err(1, "geom_gettree = %d", i);
  170. error = geom_stats_open();
  171. if (error)
  172. err(1, "geom_stats_open()");
  173. sq = NULL;
  174. sq = geom_stats_snapshot_get();
  175. if (sq == NULL)
  176. err(1, "geom_stats_snapshot()");
  177. if (!flag_b) {
  178. /* Setup libedit */
  179. hist = history_init();
  180. if (hist == NULL)
  181. err(EX_SOFTWARE, "history_init()");
  182. history(hist, &hist_ev, H_SETSIZE, 100);
  183. el = el_init("gstat", stdin, stdout, stderr);
  184. if (el == NULL)
  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);
  190. if (f_s[0] != '\0')
  191. history(hist, &hist_ev, H_ENTER, f_s);
  192. /* Setup curses */
  193. initscr();
  194. start_color();
  195. use_default_colors();
  196. pair_content(0, &cf, &cb);
  197. init_pair(1, COLOR_GREEN, cb);
  198. init_pair(2, COLOR_MAGENTA, cb);
  199. init_pair(3, COLOR_RED, cb);
  200. cbreak();
  201. noecho();
  202. nonl();
  203. nodelay(stdscr, 1);
  204. intrflush(stdscr, FALSE);
  205. keypad(stdscr, TRUE);
  206. }
  207. geom_stats_snapshot_timestamp(sq, &tq);
  208. for (quit = 0; !quit;) {
  209. sp = geom_stats_snapshot_get();
  210. if (sp == NULL)
  211. err(1, "geom_stats_snapshot()");
  212. geom_stats_snapshot_timestamp(sp, &tp);
  213. dt = tp.tv_sec - tq.tv_sec;
  214. dt += (tp.tv_nsec - tq.tv_nsec) * 1e-9;
  215. tq = tp;
  216. if (flag_C) { /* set timestamp string */
  217. (void)strftime(ts,sizeof(ts),
  218. "%F %T",localtime(&tq.tv_sec));
  219. (void)snprintf(ts,sizeof(ts),
  220. "%s.%.9ld",ts,tq.tv_nsec);
  221. }
  222. geom_stats_snapshot_reset(sp);
  223. geom_stats_snapshot_reset(sq);
  224. if (!flag_b)
  225. move(0,0);
  226. if (!flag_C)
  227. PRINTMSG("dT: %5.3fs w: %.3fs", dt,
  228. (float)flag_I / 1000000);
  229. if (!flag_C && f_s[0] != '\0') {
  230. PRINTMSG(" filter: ");
  231. if (!flag_b) {
  232. getyx(stdscr, cury, curx);
  233. getmaxyx(stdscr, maxy, maxx);
  234. }
  235. strlcpy(pf_s, f_s, sizeof(pf_s));
  236. max_flen = maxx - curx - 1;
  237. if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
  238. if (max_flen > 3)
  239. pf_s[max_flen - 3] = '.';
  240. if (max_flen > 2)
  241. pf_s[max_flen - 2] = '.';
  242. if (max_flen > 1)
  243. pf_s[max_flen - 1] = '.';
  244. pf_s[max_flen] = '\0';
  245. }
  246. PRINTMSG("%s", pf_s);
  247. }
  248. if (!flag_C) {
  249. PRINTMSG("\n");
  250. PRINTMSG(" L(q) ops/s ");
  251. if (flag_s) {
  252. PRINTMSG(" r/s kB kBps ms/r ");
  253. PRINTMSG(" w/s kB kBps ms/w ");
  254. }
  255. else {
  256. PRINTMSG(" r/s kBps ms/r ");
  257. PRINTMSG(" w/s kBps ms/w ");
  258. }
  259. if (flag_d) {
  260. if (flag_s) {
  261. PRINTMSG(" d/s kB kBps");
  262. PRINTMSG(" ms/d ");
  263. } else
  264. PRINTMSG(" d/s kBps ms/d ");
  265. }
  266. if (flag_o)
  267. PRINTMSG(" o/s ms/o ");
  268. PRINTMSG("%%busy Name\n");
  269. } else if (flag_C && !head_printed) {
  270. PRINTMSG("timestamp,name,q-depth,total_ops/s");
  271. if (flag_s) {
  272. PRINTMSG(",read/s,read_sz-KiB");
  273. PRINTMSG(",read-KiB/s,ms/read");
  274. PRINTMSG(",write/s,write_sz-KiB");
  275. PRINTMSG(",write-KiB/s,ms/write");
  276. } else {
  277. PRINTMSG(",read/s,read-KiB/s,ms/read");
  278. PRINTMSG(",write/s,write-KiB/s,ms/write");
  279. }
  280. if (flag_d) {
  281. if (flag_s) {
  282. PRINTMSG(",delete/s,delete-sz-KiB");
  283. PRINTMSG(",delete-KiB/s,ms/delete");
  284. } else {
  285. PRINTMSG(",delete/s,delete-KiB/s");
  286. PRINTMSG(",ms/delete");
  287. }
  288. }
  289. if (flag_o)
  290. PRINTMSG(",other/s,ms/other");
  291. PRINTMSG(",%%busy\n");
  292. head_printed = 1;
  293. }
  294. for (;;) {
  295. gsp = geom_stats_snapshot_next(sp);
  296. gsq = geom_stats_snapshot_next(sq);
  297. if (gsp == NULL || gsq == NULL)
  298. break;
  299. if (gsp->id == NULL)
  300. continue;
  301. gid = geom_lookupid(&gmp, gsp->id);
  302. if (gid == NULL) {
  303. geom_deletetree(&gmp);
  304. i = geom_gettree(&gmp);
  305. if (i != 0)
  306. err(1, "geom_gettree = %d", i);
  307. gid = geom_lookupid(&gmp, gsp->id);
  308. }
  309. if (gid == NULL)
  310. continue;
  311. if (gid->lg_what == ISCONSUMER && !flag_c)
  312. continue;
  313. if (flag_p && gid->lg_what == ISPROVIDER &&
  314. ((struct gprovider *)
  315. (gid->lg_ptr))->lg_geom->lg_rank != 1)
  316. continue;
  317. /* Do not print past end of window */
  318. if (!flag_b) {
  319. getyx(stdscr, cury, curx);
  320. if (curx > 0)
  321. continue;
  322. }
  323. if ((gid->lg_what == ISPROVIDER
  324. || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
  325. pp = gid->lg_ptr;
  326. if ((regexec(&f_re, pp->lg_name, 0, NULL, 0)
  327. != 0))
  328. continue;
  329. }
  330. if (gsp->sequence0 != gsp->sequence1) {
  331. /*
  332. * it is ok to skip entire line silently
  333. * for CSV output
  334. */
  335. if (!flag_C)
  336. PRINTMSG("*\n");
  337. continue;
  338. }
  339. devstat_compute_statistics(gsp, gsq, dt,
  340. DSM_QUEUE_LENGTH, &u64,
  341. DSM_TRANSFERS_PER_SECOND, &ld[0],
  342. DSM_TRANSFERS_PER_SECOND_READ, &ld[1],
  343. DSM_MB_PER_SECOND_READ, &ld[2],
  344. DSM_MS_PER_TRANSACTION_READ, &ld[3],
  345. DSM_TRANSFERS_PER_SECOND_WRITE, &ld[4],
  346. DSM_MB_PER_SECOND_WRITE, &ld[5],
  347. DSM_MS_PER_TRANSACTION_WRITE, &ld[6],
  348. DSM_BUSY_PCT, &ld[7],
  349. DSM_TRANSFERS_PER_SECOND_FREE, &ld[8],
  350. DSM_MB_PER_SECOND_FREE, &ld[9],
  351. DSM_MS_PER_TRANSACTION_FREE, &ld[10],
  352. DSM_TRANSFERS_PER_SECOND_OTHER, &ld[11],
  353. DSM_MS_PER_TRANSACTION_OTHER, &ld[12],
  354. DSM_KB_PER_TRANSFER_READ, &ld[13],
  355. DSM_KB_PER_TRANSFER_WRITE, &ld[14],
  356. DSM_KB_PER_TRANSFER_FREE, &ld[15],
  357. DSM_NONE);
  358. if (flag_a && ld[7] < 0.1) {
  359. *gsq = *gsp;
  360. continue;
  361. }
  362. /* store name for geom device */
  363. if (gid == NULL) {
  364. (void)snprintf(g_name, sizeof(g_name), "??");
  365. } else if (gid->lg_what == ISPROVIDER) {
  366. pp = gid->lg_ptr;
  367. (void)snprintf(g_name, sizeof(g_name), "%s",
  368. pp->lg_name);
  369. } else if (gid->lg_what == ISCONSUMER) {
  370. cp = gid->lg_ptr;
  371. (void)snprintf(g_name, sizeof(g_name),
  372. "%s/%s/%s",
  373. cp->lg_geom->lg_class->lg_name,
  374. cp->lg_geom->lg_name,
  375. cp->lg_provider->lg_name);
  376. }
  377. if (flag_C) {
  378. PRINTMSG("%s", ts); /* timestamp */
  379. PRINTMSG(",%s", g_name); /* print name */
  380. PRINTMSG(",%ju", (uintmax_t)u64);
  381. PRINTMSG(",%.0f", (double)ld[0]);
  382. PRINTMSG(",%.0f", (double)ld[1]);
  383. if (flag_s)
  384. PRINTMSG(",%.0f", (double)ld[13]);
  385. PRINTMSG(",%.0f", (double)ld[2] * 1024);
  386. if (ld[3] > 1e3)
  387. PRINTMSG(",%.0f", (double)ld[3]);
  388. else
  389. PRINTMSG(",%.1f", (double)ld[3]);
  390. PRINTMSG(",%.0f", (double)ld[4]);
  391. if (flag_s)
  392. PRINTMSG(",%.0f", (double)ld[14]);
  393. PRINTMSG(",%.0f", (double)ld[5] * 1024);
  394. if (ld[6] > 1e3)
  395. PRINTMSG(",%.0f", (double)ld[6]);
  396. else
  397. PRINTMSG(",%.1f", (double)ld[6]);
  398. if (flag_d) {
  399. PRINTMSG(",%.0f", (double)ld[8]);
  400. if (flag_s)
  401. PRINTMSG(",%.0f",
  402. (double)ld[15]);
  403. PRINTMSG(",%.0f", (double)ld[9] * 1024);
  404. if (ld[10] > 1e3)
  405. PRINTMSG(",%.0f",
  406. (double)ld[10]);
  407. else
  408. PRINTMSG(",%.1f",
  409. (double)ld[10]);
  410. }
  411. if (flag_o) {
  412. PRINTMSG(",%.0f", (double)ld[11]);
  413. if (ld[12] > 1e3)
  414. PRINTMSG(",%.0f",
  415. (double)ld[12]);
  416. else
  417. PRINTMSG(",%.1f",
  418. (double)ld[12]);
  419. }
  420. PRINTMSG(",%.1lf", (double)ld[7]);
  421. } else {
  422. PRINTMSG(" %4ju", (uintmax_t)u64);
  423. PRINTMSG(" %6.0f", (double)ld[0]);
  424. PRINTMSG(" %6.0f", (double)ld[1]);
  425. if (flag_s)
  426. PRINTMSG(" %6.0f", (double)ld[13]);
  427. PRINTMSG(" %6.0f", (double)ld[2] * 1024);
  428. if (ld[3] > 1e3)
  429. PRINTMSG(" %6.0f", (double)ld[3]);
  430. else
  431. PRINTMSG(" %6.1f", (double)ld[3]);
  432. PRINTMSG(" %6.0f", (double)ld[4]);
  433. if (flag_s)
  434. PRINTMSG(" %6.0f", (double)ld[14]);
  435. PRINTMSG(" %6.0f", (double)ld[5] * 1024);
  436. if (ld[6] > 1e3)
  437. PRINTMSG(" %6.0f", (double)ld[6]);
  438. else
  439. PRINTMSG(" %6.1f", (double)ld[6]);
  440. if (flag_d) {
  441. PRINTMSG(" %6.0f", (double)ld[8]);
  442. if (flag_s)
  443. PRINTMSG(" %6.0f",
  444. (double)ld[15]);
  445. PRINTMSG(" %6.0f",
  446. (double)ld[9] * 1024);
  447. if (ld[10] > 1e3)
  448. PRINTMSG(" %6.0f",
  449. (double)ld[10]);
  450. else
  451. PRINTMSG(" %6.1f",
  452. (double)ld[10]);
  453. }
  454. if (flag_o) {
  455. PRINTMSG(" %6.0f", (double)ld[11]);
  456. if (ld[12] > 1e3)
  457. PRINTMSG(" %6.0f",
  458. (double)ld[12]);
  459. else
  460. PRINTMSG(" %6.1f",
  461. (double)ld[12]);
  462. }
  463. if (ld[7] > HIGH_PCT_BUSY_THRESH)
  464. i = 3;
  465. else if (ld[7] > MEDIUM_PCT_BUSY_THRESH)
  466. i = 2;
  467. else
  468. i = 1;
  469. if (!flag_b)
  470. attron(COLOR_PAIR(i));
  471. PRINTMSG(" %6.1lf", (double)ld[7]);
  472. if (!flag_b) {
  473. attroff(COLOR_PAIR(i));
  474. PRINTMSG("|");
  475. } else
  476. PRINTMSG(" ");
  477. PRINTMSG(" %s", g_name);
  478. if (!flag_b)
  479. clrtoeol();
  480. }
  481. PRINTMSG("\n");
  482. *gsq = *gsp;
  483. }
  484. geom_stats_snapshot_free(sp);
  485. if (flag_b) {
  486. /* We loop extra to make sure we get the information. */
  487. if (!loop)
  488. break;
  489. if (!flag_B)
  490. loop = 0;
  491. else
  492. fflush(stdout);
  493. usleep(flag_I);
  494. continue;
  495. }
  496. getyx(stdscr, cury, curx);
  497. getmaxyx(stdscr, maxy, maxx);
  498. clrtobot();
  499. if (maxy - 1 <= cury)
  500. move(maxy - 1, 0);
  501. refresh();
  502. usleep(flag_I);
  503. while((i = getch()) != ERR) {
  504. switch (i) {
  505. case '>':
  506. flag_I *= 2;
  507. break;
  508. case '<':
  509. flag_I /= 2;
  510. if (flag_I < 1000)
  511. flag_I = 1000;
  512. break;
  513. case 'c':
  514. flag_c = !flag_c;
  515. break;
  516. case 'f':
  517. move(0,0);
  518. clrtoeol();
  519. refresh();
  520. line = el_gets(el, &line_len);
  521. if (line == NULL)
  522. err(1, "el_gets");
  523. if (line_len > 1)
  524. history(hist, &hist_ev, H_ENTER, line);
  525. strlcpy(tmp_f_s, line, sizeof(f_s));
  526. if ((p = strchr(tmp_f_s, '\n')) != NULL)
  527. *p = '\0';
  528. /*
  529. * Fix the terminal. We messed up
  530. * curses idea of the screen by using
  531. * libedit.
  532. */
  533. clear();
  534. refresh();
  535. cbreak();
  536. noecho();
  537. nonl();
  538. if (regcomp(&tmp_f_re, tmp_f_s, REG_EXTENDED)
  539. != 0) {
  540. move(0, 0);
  541. printw("Invalid filter");
  542. refresh();
  543. sleep(1);
  544. } else {
  545. strlcpy(f_s, tmp_f_s, sizeof(f_s));
  546. f_re = tmp_f_re;
  547. }
  548. break;
  549. case 'F':
  550. f_s[0] = '\0';
  551. break;
  552. case 'q':
  553. quit = 1;
  554. break;
  555. default:
  556. break;
  557. }
  558. }
  559. }
  560. if (!flag_b) {
  561. el_end(el);
  562. endwin();
  563. }
  564. exit(EX_OK);
  565. }
  566. static void
  567. usage(void)
  568. {
  569. fprintf(stderr, "usage: gstat [-abBcCdps] [-f filter] [-I interval]\n");
  570. exit(EX_USAGE);
  571. /* NOTREACHED */
  572. }