svn commit: r190316 - stable/7/usr.sbin/gstat

Maxim Konovalov maxim at FreeBSD.org
Mon Mar 23 02:00:34 PDT 2009


Author: maxim
Date: Mon Mar 23 09:00:33 2009
New Revision: 190316
URL: http://svn.freebsd.org/changeset/base/190316

Log:
  MFC r175118: when the ms/req fields exceed 1 second, drop the fractions
  to fit more digits.
  MFC r183665: batch mode.
  MFC r189738: fix man page sinopsys; remove BUG section.
  MFC r189739: turn the batch mode on if stdout is not tty.
  > Description of fields to fill in above:                     76 columns --|
  > PR:            If a GNATS PR is affected by the change.
  > Submitted by:  If someone else sent in the change.
  > Reviewed by:   If someone else reviewed your modification.
  > Approved by:   If you needed approval for this commit.
  > Obtained from: If the change is from a third party.
  > MFC after:     N [day[s]|week[s]|month[s]].  Request a reminder email.
  > Security:      Vulnerability reference (one per line) or description.
  > Empty fields above will be automatically removed.
  
  _M   gstat
  M    gstat/gstat.c
  M    gstat/gstat.8

Modified:
  stable/7/usr.sbin/gstat/   (props changed)
  stable/7/usr.sbin/gstat/gstat.8
  stable/7/usr.sbin/gstat/gstat.c

Modified: stable/7/usr.sbin/gstat/gstat.8
==============================================================================
--- stable/7/usr.sbin/gstat/gstat.8	Mon Mar 23 08:33:19 2009	(r190315)
+++ stable/7/usr.sbin/gstat/gstat.8	Mon Mar 23 09:00:33 2009	(r190316)
@@ -24,7 +24,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd May 20, 2006
+.Dd March 12, 2009
 .Dt GSTAT 8
 .Os
 .Sh NAME
@@ -32,7 +32,7 @@
 .Nd print statistics about GEOM disks
 .Sh SYNOPSIS
 .Nm
-.Op Fl acd
+.Op Fl abcd
 .Op Fl f Ar filter
 .Op Fl I Ar interval
 .Sh DESCRIPTION
@@ -46,6 +46,10 @@ The options are as follows:
 .Bl -tag -width indent
 .It Fl a
 Only display providers that are at least 0.1% busy.
+.It Fl b
+Batch mode.
+Collect numbers, print and exit.
+Default if stdout is not a tty.
 .It Fl c
 Enable display of
 .Xr geom 4
@@ -90,15 +94,3 @@ A
 .Nm
 utility appeared in
 .Fx 5.0 .
-.Sh BUGS
-The
-.Nm
-utility only works interactively.
-It should probably detect when it is run non-interactively and produce
-simple
-.Tn ASCII
-text output.
-Otherwise, this utility should probably be folded into
-.Xr systat 1
-which only works interactively and is the obvious utility to run for
-various other forms of system statistics.

Modified: stable/7/usr.sbin/gstat/gstat.c
==============================================================================
--- stable/7/usr.sbin/gstat/gstat.c	Mon Mar 23 08:33:19 2009	(r190315)
+++ stable/7/usr.sbin/gstat/gstat.c	Mon Mar 23 09:00:33 2009	(r190316)
@@ -51,9 +51,16 @@
 #include <sysexits.h>
 #include <unistd.h>
 
-static int flag_a, flag_c, flag_d;
+static int flag_a, flag_b, flag_c, flag_d;
 static int flag_I = 1000000;
 
+#define PRINTMSG(...) do {						\
+		if (flag_b && !loop)					\
+			printf(__VA_ARGS__);				\
+		else if (!flag_b)					\
+			printw(__VA_ARGS__);				\
+	} while(0)
+
 static void usage(void);
 
 static const char*
@@ -67,7 +74,7 @@ int
 main(int argc, char **argv)
 {
 	int error, i, quit;
-	int curx, cury, maxx, maxy, line_len, max_flen;
+	int curx, cury, maxx, maxy, line_len, loop, max_flen;
 	struct devstat *gsp, *gsq;
 	void *sp, *sq;
 	double dt;
@@ -87,12 +94,24 @@ main(int argc, char **argv)
 	History *hist;
 	HistEvent hist_ev;
 
+	hist = NULL;
+	el = NULL;
+	maxx = -1;
+	curx = -1;
+	loop = 1;
+	/* Turn on batch mode if output is not tty. */
+	if (!isatty(fileno(stdout)))
+		flag_b = 1;
+
 	f_s[0] = '\0';
-	while ((i = getopt(argc, argv, "adcf:I:")) != -1) {
+	while ((i = getopt(argc, argv, "adcf:I:b")) != -1) {
 		switch (i) {
 		case 'a':
 			flag_a = 1;
 			break;
+		case 'b':
+			flag_b = 1;
+			break;
 		case 'c':
 			flag_c = 1;
 			break;
@@ -141,34 +160,36 @@ main(int argc, char **argv)
 	sq = geom_stats_snapshot_get();
 	if (sq == NULL)
 		err(1, "geom_stats_snapshot()");
-	/* Setup curses */
-	initscr();
-	start_color();
-	use_default_colors();
-	pair_content(0, &cf, &cb);
-	init_pair(1, COLOR_GREEN, cb);
-	init_pair(2, COLOR_MAGENTA, cb);
-	init_pair(3, COLOR_RED, cb);
-	cbreak();
-	noecho();
-	nonl();
-	nodelay(stdscr, 1);
-	intrflush(stdscr, FALSE);
-	keypad(stdscr, TRUE);
-	/* Setup libedit */
-	hist = history_init();
-	if (hist == NULL)
-		err(EX_SOFTWARE, "history_init()");
-	history(hist, &hist_ev, H_SETSIZE, 100);
-	el = el_init("gstat", stdin, stdout, stderr);
-	if (el == NULL)
-		err(EX_SOFTWARE, "el_init");
-	el_set(el, EL_EDITOR, "emacs");
-	el_set(el, EL_SIGNAL, 1);
-	el_set(el, EL_HIST, history, hist);
-	el_set(el, EL_PROMPT, el_prompt);
-	if (f_s[0] != '\0')
-		history(hist, &hist_ev, H_ENTER, f_s);
+	if (!flag_b) {
+		/* Setup curses */
+		initscr();
+		start_color();
+		use_default_colors();
+		pair_content(0, &cf, &cb);
+		init_pair(1, COLOR_GREEN, cb);
+		init_pair(2, COLOR_MAGENTA, cb);
+		init_pair(3, COLOR_RED, cb);
+		cbreak();
+		noecho();
+		nonl();
+		nodelay(stdscr, 1);
+		intrflush(stdscr, FALSE);
+		keypad(stdscr, TRUE);
+		/* Setup libedit */
+		hist = history_init();
+		if (hist == NULL)
+			err(EX_SOFTWARE, "history_init()");
+		history(hist, &hist_ev, H_SETSIZE, 100);
+		el = el_init("gstat", stdin, stdout, stderr);
+		if (el == NULL)
+			err(EX_SOFTWARE, "el_init");
+		el_set(el, EL_EDITOR, "emacs");
+		el_set(el, EL_SIGNAL, 1);
+		el_set(el, EL_HIST, history, hist);
+		el_set(el, EL_PROMPT, el_prompt);
+		if (f_s[0] != '\0')
+			history(hist, &hist_ev, H_ENTER, f_s);
+	}
 	geom_stats_snapshot_timestamp(sq, &tq);
 	for (quit = 0; !quit;) {
 		sp = geom_stats_snapshot_get();
@@ -182,12 +203,13 @@ main(int argc, char **argv)
 		geom_stats_snapshot_reset(sp);
 		geom_stats_snapshot_reset(sq);
 		move(0,0);
-		printw("dT: %5.3fs  w: %.3fs",
-		    dt, (float)flag_I / 1000000);
+		PRINTMSG("dT: %5.3fs  w: %.3fs", dt, (float)flag_I / 1000000);
 		if (f_s[0] != '\0') {
-			printw("  filter: ");
-			getyx(stdscr, cury, curx);
-			getmaxyx(stdscr, maxy, maxx);
+			PRINTMSG("  filter: ");
+			if (!flag_b) {
+				getyx(stdscr, cury, curx);
+				getmaxyx(stdscr, maxy, maxx);
+			}
 			strncpy(pf_s, f_s, sizeof(pf_s));
 			max_flen = maxx - curx - 1;
 			if ((int)strlen(f_s) > max_flen && max_flen >= 0) {
@@ -199,15 +221,15 @@ main(int argc, char **argv)
 					pf_s[max_flen - 1] = '.';
 				pf_s[max_flen] = '\0';
 			}
-			printw("%s", pf_s);
+			PRINTMSG("%s", pf_s);
 		}
-		printw("\n");
-		printw(" L(q)  ops/s   ");
-		printw(" r/s   kBps   ms/r   ");
-		printw(" w/s   kBps   ms/w   ");
+		PRINTMSG("\n");
+		PRINTMSG(" L(q)  ops/s   ");
+		PRINTMSG(" r/s   kBps   ms/r   ");
+		PRINTMSG(" w/s   kBps   ms/w   ");
 		if (flag_d)
-			printw(" d/s   kBps   ms/d   ");
-		printw("%%busy Name\n");
+			PRINTMSG(" d/s   kBps   ms/d   ");
+		PRINTMSG("%%busy Name\n");
 		for (;;) {
 			gsp = geom_stats_snapshot_next(sp);
 			gsq = geom_stats_snapshot_next(sq);
@@ -228,9 +250,11 @@ main(int argc, char **argv)
 			if (gid->lg_what == ISCONSUMER && !flag_c)
 				continue;
 			/* Do not print past end of window */
-			getyx(stdscr, cury, curx);
-			if (curx > 0)
-				continue;
+			if (!flag_b) {
+				getyx(stdscr, cury, curx);
+				if (curx > 0)
+					continue;
+			}
 			if ((gid->lg_what == ISPROVIDER
 			    || gid->lg_what == ISCONSUMER) && f_s[0] != '\0') {
 				pp = gid->lg_ptr;
@@ -239,7 +263,7 @@ main(int argc, char **argv)
 				  continue;
 			}
 			if (gsp->sequence0 != gsp->sequence1) {
-				printw("*\n");
+				PRINTMSG("*\n");
 				continue;
 			}
 			devstat_compute_statistics(gsp, gsq, dt, 
@@ -265,19 +289,28 @@ main(int argc, char **argv)
 				continue;
 			}
 
-			printw(" %4ju", (uintmax_t)u64);
-			printw(" %6.0f", (double)ld[0]);
-			printw(" %6.0f", (double)ld[1]);
-			printw(" %6.0f", (double)ld[2] * 1024);
-			printw(" %6.1f", (double)ld[3]);
-			printw(" %6.0f", (double)ld[4]);
-			printw(" %6.0f", (double)ld[5] * 1024);
-			printw(" %6.1f", (double)ld[6]);
+			PRINTMSG(" %4ju", (uintmax_t)u64);
+			PRINTMSG(" %6.0f", (double)ld[0]);
+			PRINTMSG(" %6.0f", (double)ld[1]);
+			PRINTMSG(" %6.0f", (double)ld[2] * 1024);
+			if (ld[3] > 1e3) 
+				PRINTMSG(" %6.0f", (double)ld[3]);
+			else
+				PRINTMSG(" %6.1f", (double)ld[3]);
+			PRINTMSG(" %6.0f", (double)ld[4]);
+			PRINTMSG(" %6.0f", (double)ld[5] * 1024);
+			if (ld[6] > 1e3) 
+				PRINTMSG(" %6.0f", (double)ld[6]);
+			else
+				PRINTMSG(" %6.1f", (double)ld[6]);
 
 			if (flag_d) {
-				printw(" %6.0f", (double)ld[8]);
-				printw(" %6.0f", (double)ld[9] * 1024);
-				printw(" %6.1f", (double)ld[10]);
+				PRINTMSG(" %6.0f", (double)ld[8]);
+				PRINTMSG(" %6.0f", (double)ld[9] * 1024);
+				if (ld[10] > 1e3) 
+					PRINTMSG(" %6.0f", (double)ld[10]);
+				else
+					PRINTMSG(" %6.1f", (double)ld[10]);
 			}
 
 			if (ld[7] > 80)
@@ -286,27 +319,40 @@ main(int argc, char **argv)
 				i = 2;
 			else 
 				i = 1;
-			attron(COLOR_PAIR(i));
-			printw(" %6.1lf", (double)ld[7]);
-			attroff(COLOR_PAIR(i));
-			printw("|");
+			if (!flag_b)
+				attron(COLOR_PAIR(i));
+			PRINTMSG(" %6.1lf", (double)ld[7]);
+			if (!flag_b) {
+				attroff(COLOR_PAIR(i));
+				PRINTMSG("|");
+			} else
+				PRINTMSG(" ");
 			if (gid == NULL) {
-				printw(" ??");
+				PRINTMSG(" ??");
 			} else if (gid->lg_what == ISPROVIDER) {
 				pp = gid->lg_ptr;
-				printw(" %s", pp->lg_name);
+				PRINTMSG(" %s", pp->lg_name);
 			} else if (gid->lg_what == ISCONSUMER) {
 				cp = gid->lg_ptr;
-				printw(" %s/%s/%s",
+				PRINTMSG(" %s/%s/%s",
 				    cp->lg_geom->lg_class->lg_name,
 				    cp->lg_geom->lg_name,
 				    cp->lg_provider->lg_name);
 			}
-			clrtoeol();
-			printw("\n");
+			if (!flag_b)
+				clrtoeol();
+			PRINTMSG("\n");
 			*gsq = *gsp;
 		}
 		geom_stats_snapshot_free(sp);
+		if (flag_b) {
+			/* We loop extra to make sure we get the information. */
+			if (!loop)
+				break;
+			loop = 0;
+			usleep(flag_I);
+			continue;
+		}
 		getyx(stdscr, cury, curx);
 		getmaxyx(stdscr, maxy, maxx);
 		clrtobot();
@@ -369,15 +415,17 @@ main(int argc, char **argv)
 		}
 	}
 
-	endwin();
-	el_end(el);
+	if (!flag_b) {
+		endwin();
+		el_end(el);
+	}
 	exit(EX_OK);
 }
 
 static void
 usage(void)
 {
-	fprintf(stderr, "usage: gstat [-acd] [-f filter] [-I interval]\n");
+	fprintf(stderr, "usage: gstat [-abcd] [-f filter] [-I interval]\n");
 	exit(EX_USAGE);
         /* NOTREACHED */
 }


More information about the svn-src-stable-7 mailing list