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
This commit is contained in:
Denis Vlasenko 2007-04-19 14:47:11 +00:00
parent 516a0ca2dc
commit 8581863a1b
5 changed files with 76 additions and 45 deletions

View File

@ -931,8 +931,8 @@ extern const int const_int_1;
#ifndef BUFSIZ #ifndef BUFSIZ
#define BUFSIZ 4096 #define BUFSIZ 4096
#endif #endif
// TODO: provide hard guarantees on minimum size of bb_common_bufsiz1 /* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */
extern char bb_common_bufsiz1[BUFSIZ+1]; extern char bb_common_bufsiz1[(BUFSIZ > 256*sizeof(void*) ? BUFSIZ : 256*sizeof(void*)) + 1];
/* This struct is deliberately not defined. */ /* This struct is deliberately not defined. */
/* See docs/keep_data_small.txt */ /* See docs/keep_data_small.txt */
struct globals; struct globals;

View File

@ -54,7 +54,7 @@ WTMP_FILE;
# error unknown path to wtmp file # error unknown path to wtmp file
#endif #endif
char bb_common_bufsiz1[BUFSIZ+1]; char bb_common_bufsiz1[(BUFSIZ > 256*sizeof(void*) ? BUFSIZ : 256*sizeof(void*)) + 1];
struct globals; struct globals;
/* Make it reside in R/W memory: */ /* Make it reside in R/W memory: */

View File

@ -184,7 +184,7 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
sp->tty_str[0] = '?'; sp->tty_str[0] = '?';
/* sp->tty_str[1] = '\0'; - done by memset */ /* 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", snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
(tty >> 8) & 0xfff, /* major */ (tty >> 8) & 0xfff, /* major */
(tty & 0xff) | ((tty >> 12) & 0xfff00)); (tty & 0xff) | ((tty >> 12) & 0xfff00));

View File

@ -109,7 +109,7 @@ static const ps_out_t out_spec[] = {
// { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID }, // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
// { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID }, // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
// { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ }, // { 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 }, { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
// Not mandated by POSIX, but useful: // Not mandated by POSIX, but useful:
{ 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, { 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]) ) #define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
static ps_out_t* out; #define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args"
static int out_cnt;
static int print_header;
static int ps_flags;
static char *buffer;
static unsigned terminal_width;
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) static ps_out_t* new_out_t(void)
{ {
@ -186,7 +198,7 @@ static void post_process(void)
int i; int i;
int width = 0; int width = 0;
for (i = 0; i < out_cnt; i++) { for (i = 0; i < out_cnt; i++) {
ps_flags |= out[i].ps_flags; need_flags |= out[i].ps_flags;
if (out[i].header[0]) { if (out[i].header[0]) {
print_header = 1; print_header = 1;
} }
@ -241,15 +253,15 @@ static void format_process(const procps_status_t *ps)
printf("%.*s\n", terminal_width, buffer); 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);
int ps_main(int argc, char **argv) int ps_main(int argc, char **argv)
{ {
procps_status_t *p; procps_status_t *p;
llist_t* opt_o = NULL; llist_t* opt_o = NULL;
/* Cannot be const: parse_o() will choke */
strcpy(default_o, DEFAULT_O_STR);
// POSIX: // POSIX:
// -a Write information for all processes associated with terminals // -a Write information for all processes associated with terminals
// Implementations may omit session leaders from this list // Implementations may omit session leaders from this list
@ -282,7 +294,7 @@ int ps_main(int argc, char **argv)
format_header(); format_header();
p = NULL; p = NULL;
while ((p = procps_scan(p, ps_flags))) { while ((p = procps_scan(p, need_flags))) {
format_process(p); format_process(p);
} }

View File

@ -31,7 +31,7 @@
#include "busybox.h" #include "busybox.h"
typedef struct { typedef struct top_status_t {
unsigned long vsz; unsigned long vsz;
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
unsigned long ticks; unsigned long ticks;
@ -42,24 +42,60 @@ typedef struct {
char state[4]; char state[4];
char comm[COMM_LEN]; char comm[COMM_LEN];
} top_status_t; } 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 /* This structure stores some critical information from one frame to
the next. Used for finding deltas. */ the next. Used for finding deltas. */
struct save_hist { typedef struct save_hist {
unsigned long ticks; unsigned long ticks;
unsigned pid; 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 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
static struct save_hist *prev_hist; #define prev_hist (G.prev_hist )
static int prev_hist_count; #define prev_hist_count (G.prev_hist_count )
/* static int hist_iterations; */ #define jif (G.jif )
static unsigned total_pcpu; #define prev_jif (G.prev_jif )
/* static unsigned long total_vsz; */ #define total_pcpu (G.total_pcpu )
#endif #endif
#define OPT_BATCH_MODE (option_mask32 & 0x4) #define OPT_BATCH_MODE (option_mask32 & 0x4)
#if ENABLE_FEATURE_USE_TERMIOS #if ENABLE_FEATURE_USE_TERMIOS
static int pid_sort(top_status_t *P, top_status_t *Q) 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
#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) 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) static void get_jiffy_counts(void)
{ {
FILE* fp = xfopen("stat", "r"); FILE* fp = xfopen("stat", "r");
@ -391,8 +411,6 @@ static void clearmems(void)
#include <termios.h> #include <termios.h>
#include <signal.h> #include <signal.h>
static struct termios initial_settings;
static void reset_term(void) static void reset_term(void)
{ {
tcsetattr(0, TCSANOW, (void *) &initial_settings); tcsetattr(0, TCSANOW, (void *) &initial_settings);
@ -426,8 +444,9 @@ int top_main(int argc, char **argv)
unsigned char c; unsigned char c;
#endif /* FEATURE_USE_TERMIOS */ #endif /* FEATURE_USE_TERMIOS */
/* do normal option parsing */
interval = 5; interval = 5;
/* do normal option parsing */
opt_complementary = "-"; opt_complementary = "-";
getopt32(argc, argv, "d:n:b", &sinterval, &siterations); getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
if (option_mask32 & 0x1) interval = xatou(sinterval); // -d if (option_mask32 & 0x1) interval = xatou(sinterval); // -d