svn commit: trunk/busybox: include libbb procps

vda at busybox.net vda at busybox.net
Thu Apr 19 14:47:12 UTC 2007


Author: vda
Date: 2007-04-19 07:47:11 -0700 (Thu, 19 Apr 2007)
New Revision: 18497

Log:
procps: remove all global variables
   text    data     bss     dec     hex filename
   1462      14      24    1500     5dc busybox.t2/procps/ps.o
   1484       0       0    1484     5cc busybox.t3/procps/ps.o
   3122       0     252    3374     d2e busybox.t1/procps/top.o
   3117       0       0    3117     c2d busybox.t3/procps/top.o


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


Changeset:
Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2007-04-19 14:46:14 UTC (rev 18496)
+++ trunk/busybox/include/libbb.h	2007-04-19 14:47:11 UTC (rev 18497)
@@ -931,8 +931,8 @@
 #ifndef BUFSIZ
 #define BUFSIZ 4096
 #endif
-// TODO: provide hard guarantees on minimum size of bb_common_bufsiz1
-extern char bb_common_bufsiz1[BUFSIZ+1];
+/* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
+extern char bb_common_bufsiz1[(BUFSIZ > 256*sizeof(void*) ? BUFSIZ : 256*sizeof(void*)) + 1];
 /* This struct is deliberately not defined. */
 /* See docs/keep_data_small.txt */
 struct globals;

Modified: trunk/busybox/libbb/messages.c
===================================================================
--- trunk/busybox/libbb/messages.c	2007-04-19 14:46:14 UTC (rev 18496)
+++ trunk/busybox/libbb/messages.c	2007-04-19 14:47:11 UTC (rev 18497)
@@ -54,7 +54,7 @@
 # error unknown path to wtmp file
 #endif
 
-char bb_common_bufsiz1[BUFSIZ+1];
+char bb_common_bufsiz1[(BUFSIZ > 256*sizeof(void*) ? BUFSIZ : 256*sizeof(void*)) + 1];
 
 struct globals;
 /* Make it reside in R/W memory: */

Modified: trunk/busybox/libbb/procps.c
===================================================================
--- trunk/busybox/libbb/procps.c	2007-04-19 14:46:14 UTC (rev 18496)
+++ trunk/busybox/libbb/procps.c	2007-04-19 14:47:11 UTC (rev 18497)
@@ -184,7 +184,7 @@
 
 			sp->tty_str[0] = '?';
 			/* sp->tty_str[1] = '\0'; - done by memset */
-			if (tty >= 0) /* tty field of "-1" means "no tty" */
+			if (tty) /* tty field of "0" means "no tty" */
 				snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
 					(tty >> 8) & 0xfff, /* major */
 					(tty & 0xff) | ((tty >> 12) & 0xfff00));

Modified: trunk/busybox/procps/ps.c
===================================================================
--- trunk/busybox/procps/ps.c	2007-04-19 14:46:14 UTC (rev 18496)
+++ trunk/busybox/procps/ps.c	2007-04-19 14:47:11 UTC (rev 18497)
@@ -109,7 +109,7 @@
 //	{ sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID          },
 //	{ sizeof("RUSER"  )-1, "ruser" ,"RUSER"  ,func_ruser ,PSSCAN_UIDGID          },
 //	{ sizeof("TIME"   )-1, "time"  ,"TIME"   ,func_time  ,PSSCAN_                },
-	{ sizeof("TT"     )-1, "tty"   ,"TT"     ,func_tty   ,PSSCAN_TTY             },
+	{ 6                  , "tty"   ,"TT"     ,func_tty   ,PSSCAN_TTY             },
 	{ 4                  , "vsz"   ,"VSZ"    ,func_vsz   ,PSSCAN_VSZ             },
 // Not mandated by POSIX, but useful:
 	{ 4                  , "rss"   ,"RSS"    ,func_rss   ,PSSCAN_RSS             },
@@ -117,13 +117,25 @@
 
 #define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
 
-static ps_out_t* out;
-static int out_cnt;
-static int print_header;
-static int ps_flags;
-static char *buffer;
-static unsigned terminal_width;
+#define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args"
 
+struct globals {
+	ps_out_t* out;
+	int out_cnt;
+	int print_header;
+	int need_flags;
+	char *buffer;
+	unsigned terminal_width;
+	char default_o[sizeof(DEFAULT_O_STR)];
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define out            (G.out           )
+#define out_cnt        (G.out_cnt       )
+#define print_header   (G.print_header  )
+#define need_flags     (G.need_flags    )
+#define buffer         (G.buffer        )
+#define terminal_width (G.terminal_width)
+#define default_o      (G.default_o     )
 
 static ps_out_t* new_out_t(void)
 {
@@ -186,7 +198,7 @@
 	int i;
 	int width = 0;
 	for (i = 0; i < out_cnt; i++) {
-		ps_flags |= out[i].ps_flags;
+		need_flags |= out[i].ps_flags;
 		if (out[i].header[0]) {
 			print_header = 1;
 		}
@@ -241,15 +253,15 @@
 	printf("%.*s\n", terminal_width, buffer);
 }
 
-/* Cannot be const: parse_o() will choke */
-static char default_o[] = "pid,user" /* TODO: ,vsz,stat */ ",args";
-
 int ps_main(int argc, char **argv);
 int ps_main(int argc, char **argv)
 {
 	procps_status_t *p;
 	llist_t* opt_o = NULL;
 
+	/* Cannot be const: parse_o() will choke */
+	strcpy(default_o, DEFAULT_O_STR);
+
 	// POSIX:
 	// -a  Write information for all processes associated with terminals
 	//     Implementations may omit session leaders from this list
@@ -282,7 +294,7 @@
 	format_header();
 
 	p = NULL;
-	while ((p = procps_scan(p, ps_flags))) {
+	while ((p = procps_scan(p, need_flags))) {
 		format_process(p);
 	}
 

Modified: trunk/busybox/procps/top.c
===================================================================
--- trunk/busybox/procps/top.c	2007-04-19 14:46:14 UTC (rev 18496)
+++ trunk/busybox/procps/top.c	2007-04-19 14:47:11 UTC (rev 18497)
@@ -31,7 +31,7 @@
 #include "busybox.h"
 
 
-typedef struct {
+typedef struct top_status_t {
 	unsigned long vsz;
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 	unsigned long ticks;
@@ -42,24 +42,60 @@
 	char state[4];
 	char comm[COMM_LEN];
 } top_status_t;
-static top_status_t *top;
-static int ntop;
+
+typedef struct jiffy_counts_t{
+	unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
+	unsigned long long total;
+	unsigned long long busy;
+} jiffy_counts_t;
+
 /* This structure stores some critical information from one frame to
    the next. Used for finding deltas. */
-struct save_hist {
+typedef struct save_hist {
 	unsigned long ticks;
 	unsigned pid;
+} save_hist;
+
+typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
+
+enum { SORT_DEPTH = 3 };
+
+struct globals {
+	top_status_t *top;
+	int ntop;
+#if ENABLE_FEATURE_USE_TERMIOS
+	struct termios initial_settings;
+#endif
+#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+	cmp_funcp sort_function;
+#else
+	cmp_funcp sort_function[SORT_DEPTH];
+	struct save_hist *prev_hist;
+	int prev_hist_count;
+	jiffy_counts_t jif, prev_jif;
+	/* int hist_iterations; */
+	unsigned total_pcpu;
+	/* unsigned long total_vsz; */
+#endif
 };
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define top              (G.top               )
+#define ntop             (G.ntop              )
+#if ENABLE_FEATURE_USE_TERMIOS
+#define initial_settings (G. initial_settings )
+#endif
+#define sort_function    (G.sort_function     )
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-static struct save_hist *prev_hist;
-static int prev_hist_count;
-/* static int hist_iterations; */
-static unsigned total_pcpu;
-/* static unsigned long total_vsz; */
+#define prev_hist        (G.prev_hist         )
+#define prev_hist_count  (G.prev_hist_count   )
+#define jif              (G.jif               )
+#define prev_jif         (G.prev_jif          )
+#define total_pcpu       (G.total_pcpu        )
 #endif
 
 #define OPT_BATCH_MODE (option_mask32 & 0x4)
 
+
 #if ENABLE_FEATURE_USE_TERMIOS
 static int pid_sort(top_status_t *P, top_status_t *Q)
 {
@@ -77,18 +113,8 @@
 }
 
 
-typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
 
-#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-
-static cmp_funcp sort_function;
-
-#else
-
-enum { SORT_DEPTH = 3 };
-
-static cmp_funcp sort_function[SORT_DEPTH];
-
 static int pcpu_sort(top_status_t *P, top_status_t *Q)
 {
 	/* Buggy wrt ticks with high bit set */
@@ -116,12 +142,6 @@
 }
 
 
-typedef struct {
-	unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
-	unsigned long long total;
-	unsigned long long busy;
-} jiffy_counts_t;
-static jiffy_counts_t jif, prev_jif;
 static void get_jiffy_counts(void)
 {
 	FILE* fp = xfopen("stat", "r");
@@ -391,8 +411,6 @@
 #include <termios.h>
 #include <signal.h>
 
-static struct termios initial_settings;
-
 static void reset_term(void)
 {
 	tcsetattr(0, TCSANOW, (void *) &initial_settings);
@@ -426,8 +444,9 @@
 	unsigned char c;
 #endif /* FEATURE_USE_TERMIOS */
 
+	interval = 5;
+
 	/* do normal option parsing */
-	interval = 5;
 	opt_complementary = "-";
 	getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
 	if (option_mask32 & 0x1) interval = xatou(sinterval); // -d




More information about the busybox-cvs mailing list