From 8581863a1b536e2db20036e680b58cc07c54ad55 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 19 Apr 2007 14:47:11 +0000 Subject: [PATCH] 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 --- include/libbb.h | 4 +-- libbb/messages.c | 2 +- libbb/procps.c | 2 +- procps/ps.c | 36 ++++++++++++++-------- procps/top.c | 77 ++++++++++++++++++++++++++++++------------------ 5 files changed, 76 insertions(+), 45 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 31ded7e9c..fce10f310 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -931,8 +931,8 @@ extern const int const_int_1; #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; diff --git a/libbb/messages.c b/libbb/messages.c index 12a165ad7..3febe7645 100644 --- a/libbb/messages.c +++ b/libbb/messages.c @@ -54,7 +54,7 @@ WTMP_FILE; # 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: */ diff --git a/libbb/procps.c b/libbb/procps.c index 053f7d225..946f569f5 100644 --- a/libbb/procps.c +++ b/libbb/procps.c @@ -184,7 +184,7 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags) 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)); diff --git a/procps/ps.c b/procps/ps.c index 0c9b71e09..5128c3d59 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -109,7 +109,7 @@ static const ps_out_t out_spec[] = { // { 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 @@ static const ps_out_t out_spec[] = { #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 @@ static void post_process(void) 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 @@ static void format_process(const procps_status_t *ps) 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 @@ int ps_main(int argc, char **argv) format_header(); p = NULL; - while ((p = procps_scan(p, ps_flags))) { + while ((p = procps_scan(p, need_flags))) { format_process(p); } diff --git a/procps/top.c b/procps/top.c index 7d30936a8..580c30050 100644 --- a/procps/top.c +++ b/procps/top.c @@ -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 @@ typedef struct { 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,17 +113,7 @@ static int mem_sort(top_status_t *P, top_status_t *Q) } -typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q); - -#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE - -static cmp_funcp sort_function; - -#else - -enum { SORT_DEPTH = 3 }; - -static cmp_funcp sort_function[SORT_DEPTH]; +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE static int pcpu_sort(top_status_t *P, top_status_t *Q) { @@ -116,12 +142,6 @@ static int mult_lvl_cmp(void* a, void* b) } -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 @@ static void clearmems(void) #include #include -static struct termios initial_settings; - static void reset_term(void) { tcsetattr(0, TCSANOW, (void *) &initial_settings); @@ -426,8 +444,9 @@ int top_main(int argc, char **argv) unsigned char c; #endif /* FEATURE_USE_TERMIOS */ - /* do normal option parsing */ interval = 5; + + /* do normal option parsing */ opt_complementary = "-"; getopt32(argc, argv, "d:n:b", &sinterval, &siterations); if (option_mask32 & 0x1) interval = xatou(sinterval); // -d -- 2.25.1