svn commit: trunk/busybox: include libbb procps

vda at busybox.net vda at busybox.net
Fri Sep 29 17:16:40 UTC 2006


Author: vda
Date: 2006-09-29 10:16:39 -0700 (Fri, 29 Sep 2006)
New Revision: 16260

Log:
top: add support for -b, -n <iterations>


Modified:
   trunk/busybox/include/usage.h
   trunk/busybox/libbb/getopt_ulflags.c
   trunk/busybox/procps/ps.c
   trunk/busybox/procps/top.c


Changeset:
Modified: trunk/busybox/include/usage.h
===================================================================
--- trunk/busybox/include/usage.h	2006-09-29 14:43:20 UTC (rev 16259)
+++ trunk/busybox/include/usage.h	2006-09-29 17:16:39 UTC (rev 16260)
@@ -2938,13 +2938,11 @@
 	"\t-v\tDisplays verbose resource usage information"
 
 #define top_trivial_usage \
-	"[-d <seconds>]"
+	"[-b] [-n count] [-d seconds]"
 #define top_full_usage \
-	"top provides an view of processor activity in real time.\n" \
-	"This utility reads the status for all processes in /proc each <seconds>\n" \
-	"and shows the status for however many processes will fit on the screen.\n" \
-	"This utility will not show processes that are started after program startup,\n" \
-	"but it will show the EXIT status for and PIDs that exit while it is running."
+	"top provides a view of process activity in real time.\n" \
+	"It reads the status of all processes from /proc each <seconds>\n" \
+	"and shows the status for however many processes will fit on the screen."
 
 #define touch_trivial_usage \
 	"[-c] FILE [FILE ...]"

Modified: trunk/busybox/libbb/getopt_ulflags.c
===================================================================
--- trunk/busybox/libbb/getopt_ulflags.c	2006-09-29 14:43:20 UTC (rev 16259)
+++ trunk/busybox/libbb/getopt_ulflags.c	2006-09-29 17:16:39 UTC (rev 16260)
@@ -319,9 +319,7 @@
 	const struct option *l_o;
 #endif
 	unsigned long trigger;
-#ifdef CONFIG_PS
 	char **pargv = NULL;
-#endif
 	int min_arg = 0;
 	int max_arg = -1;
 
@@ -461,9 +459,7 @@
 #else
 	while ((c = getopt(argc, argv, applet_opts)) >= 0) {
 #endif /* ENABLE_GETOPT_LONG */
-#if ENABLE_PS
 loop_arg_is_opt:
-#endif
 		for (on_off = complementally; on_off->opt != c; on_off++) {
 			/* c==0 if long opt have non NULL flag */
 			if (on_off->opt == 0 && c != 0)
@@ -485,13 +481,10 @@
 		} else if (on_off->optarg) {
 			*(char **)(on_off->optarg) = optarg;
 		}
-#if ENABLE_PS
 		if (pargv != NULL)
 			break;
-#endif
 	}
 
-#if ENABLE_PS
 	if (spec_flgs & ALL_ARGV_IS_OPTS) {
 		/* process argv is option, for example "ps" applet */
 		if (pargv == NULL)
@@ -506,7 +499,6 @@
 			}
 		}
 	}
-#endif
 
 #if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP
 	if (spec_flgs & FREE_FIRST_ARGV_IS_OPT)

Modified: trunk/busybox/procps/ps.c
===================================================================
--- trunk/busybox/procps/ps.c	2006-09-29 14:43:20 UTC (rev 16259)
+++ trunk/busybox/procps/ps.c	2006-09-29 17:16:39 UTC (rev 16260)
@@ -16,14 +16,14 @@
 
 #if ENABLE_SELINUX
 	int use_selinux = 0;
-	security_context_t sid=NULL;
+	security_context_t sid = NULL;
 #endif
 
 #if ENABLE_FEATURE_PS_WIDE
 	int terminal_width;
 	int w_count = 0;
 
-	bb_opt_complementally="-:ww";
+	bb_opt_complementally = "-:ww";
 #else
 # define terminal_width 79
 #endif

Modified: trunk/busybox/procps/top.c
===================================================================
--- trunk/busybox/procps/top.c	2006-09-29 14:43:20 UTC (rev 16259)
+++ trunk/busybox/procps/top.c	2006-09-29 17:16:39 UTC (rev 16260)
@@ -30,12 +30,12 @@
 
 #include "busybox.h"
 
-//#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE  /* + 2k */
-
 typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
 
 static procps_status_t *top;   /* Hehe */
 static int ntop;
+static unsigned option_mask;
+#define OPT_BATCH_MODE (option_mask & 0x4)
 
 #ifdef CONFIG_FEATURE_USE_TERMIOS
 static int pid_sort(procps_status_t *P, procps_status_t *Q)
@@ -251,9 +251,11 @@
 	snprintf(scrbuf, scr_width,
 		"Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
 		used, mfree, shared, buffers, cached);
-	printf("\e[H\e[J%s\n", scrbuf);
+
+	printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
+    
 	snprintf(scrbuf, scr_width,
-		"Load average: %s  (Status: S=sleeping R=running, W=waiting)", buf);
+		"Load average: %s", buf);
 	printf("%s\n", scrbuf);
 
 	return total;
@@ -276,12 +278,12 @@
 	unsigned pcpu_shift, pcpu_scale;
 
 	/* what info of the processes is shown */
-	printf("\e[7m%.*s\e[0m", scr_width,
+	printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
 		"  PID USER     STATUS   RSS  PPID %CPU %MEM COMMAND");
 #define MIN_WIDTH \
 	sizeof( "  PID USER     STATUS   RSS  PPID %CPU %MEM C")
 #else
-	printf("\e[7m%.*s\e[0m", scr_width,
+	printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
 		"  PID USER     STATUS   RSS  PPID %MEM COMMAND");
 #define MIN_WIDTH \
 	sizeof( "  PID USER     STATUS   RSS  PPID %MEM C")
@@ -320,9 +322,9 @@
 	}
 	/* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
 #endif
-
-	while (count--) {
-		div_t pmem = div( (s->rss*pmem_scale) >> pmem_shift, 10);
+	if (OPT_BATCH_MODE) count--;
+	while (count-- > 0) {
+		div_t pmem = div((s->rss*pmem_scale) >> pmem_shift, 10);
 		int col = scr_width+1;
 		USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
 
@@ -343,7 +345,7 @@
 		s++;
 	}
 	/* printf(" %d", hist_iterations); */
-	putchar('\r');
+	putchar(OPT_BATCH_MODE ? '\n' : '\r');
 	fflush(stdout);
 }
 
@@ -382,8 +384,10 @@
 
 int top_main(int argc, char **argv)
 {
-	int opt, interval, lines, col;
-	char *sinterval;
+	int count, lines, col;
+	int interval = 5; /* default update rate is 5 seconds */
+	int iterations = -1; /* 2^32 iterations by default :) */
+	char *sinterval, *siterations;
 #ifdef CONFIG_FEATURE_USE_TERMIOS
 	struct termios new_settings;
 	struct timeval tv;
@@ -393,9 +397,12 @@
 
 	/* do normal option parsing */
 	interval = 5;
-	opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval);
-	if (opt & 1)
-		interval = atoi(sinterval);
+	bb_opt_complementally = "-";
+	option_mask = bb_getopt_ulflags(argc, argv, "d:n:b", 
+		&sinterval, &siterations);
+	if (option_mask & 0x1) interval = atoi(sinterval); // -d
+	if (option_mask & 0x2) iterations = atoi(siterations); // -n
+	//if (option_mask & 0x4) // -b
 
 	/* change to /proc */
 	xchdir("/proc");
@@ -456,12 +463,12 @@
 #else
 		qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
 #endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
-		opt = lines;
-		if (opt > ntop) {
-			opt = ntop;
+		count = lines;
+		if (count > ntop) {
+			count = ntop;
 		}
 		/* show status for each of the processes */
-		display_status(opt, col);
+		display_status(count, col);
 #ifdef CONFIG_FEATURE_USE_TERMIOS
 		tv.tv_sec = interval;
 		tv.tv_usec = 0;
@@ -503,6 +510,8 @@
 #endif
 			}
 		}
+		if (!--iterations)
+			break;
 #else
 		sleep(interval);
 #endif /* CONFIG_FEATURE_USE_TERMIOS */




More information about the busybox-cvs mailing list