procps: shrink /proc scanning code a bit

function                                             old     new   delta
skip_fields                                           16      15      -1
.rodata                                           144266  144258      -8
procps_scan                                          739     692     -47
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-56)             Total: -56 bytes
   text    data     bss     dec     hex filename
 734845    3028   14400  752273   b7a91 busybox_old
 734789    3028   14400  752217   b7a59 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2007-06-30 08:01:04 +00:00
parent ae7f7ebcb7
commit 42ee26d00c

View File

@ -109,9 +109,10 @@ void free_procps_scan(procps_status_t* sp)
#if ENABLE_FEATURE_FAST_TOP #if ENABLE_FEATURE_FAST_TOP
/* We cut a lot of corners here for speed */ /* We cut a lot of corners here for speed */
static unsigned long fast_strtoul_10(char *str, char **endptr) static unsigned long fast_strtoul_10(char **endptr)
{ {
char c; char c;
char *str = *endptr;
unsigned long n = *str - '0'; unsigned long n = *str - '0';
while ((c = *++str) != ' ') while ((c = *++str) != ' ')
@ -123,7 +124,9 @@ static unsigned long fast_strtoul_10(char *str, char **endptr)
static char *skip_fields(char *str, int count) static char *skip_fields(char *str, int count)
{ {
do { do {
str = skip_non_whitespace(str); str++; while (*str++ != ' ')
continue;
/* we found a space char, str points after it */
} while (--count); } while (--count);
return str; return str;
} }
@ -181,11 +184,11 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
} }
if (flags & PSSCAN_STAT) { if (flags & PSSCAN_STAT) {
char *cp; char *cp, *comm1;
int tty;
#if !ENABLE_FEATURE_FAST_TOP #if !ENABLE_FEATURE_FAST_TOP
unsigned long vsz, rss; unsigned long vsz, rss;
#endif #endif
int tty;
/* see proc(5) for some details on this */ /* see proc(5) for some details on this */
strcpy(filename_tail, "/stat"); strcpy(filename_tail, "/stat");
@ -193,12 +196,14 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
if (n < 0) if (n < 0)
break; break;
cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
if (!cp || cp[1] != ' ') /*if (!cp || cp[1] != ' ')
break; break;*/
cp[0] = '\0'; cp[0] = '\0';
if (sizeof(sp->comm) < 16) if (sizeof(sp->comm) < 16)
BUG_comm_size(); BUG_comm_size();
sscanf(buf, "%*s (%15c", sp->comm); comm1 = strchr(buf, '(');
/*if (comm1)*/
safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
#if !ENABLE_FEATURE_FAST_TOP #if !ENABLE_FEATURE_FAST_TOP
n = sscanf(cp+2, n = sscanf(cp+2,
@ -231,20 +236,21 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
/* This costs ~100 bytes more but makes top faster by 20% /* This costs ~100 bytes more but makes top faster by 20%
* If you run 10000 processes, this may be important for you */ * If you run 10000 processes, this may be important for you */
sp->state[0] = cp[2]; sp->state[0] = cp[2];
sp->ppid = fast_strtoul_10(cp + 4, &cp); cp += 4;
sp->pgid = fast_strtoul_10(cp, &cp); sp->ppid = fast_strtoul_10(&cp);
sp->sid = fast_strtoul_10(cp, &cp); sp->pgid = fast_strtoul_10(&cp);
tty = fast_strtoul_10(cp, &cp); sp->sid = fast_strtoul_10(&cp);
tty = fast_strtoul_10(&cp);
sp->tty_major = (tty >> 8) & 0xfff; sp->tty_major = (tty >> 8) & 0xfff;
sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00); sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
sp->utime = fast_strtoul_10(cp, &cp); sp->utime = fast_strtoul_10(&cp);
sp->stime = fast_strtoul_10(cp, &cp); sp->stime = fast_strtoul_10(&cp);
cp = skip_fields(cp, 3); /* cutime, cstime, priority */ cp = skip_fields(cp, 3); /* cutime, cstime, priority */
tasknice = fast_strtoul_10(cp, &cp); tasknice = fast_strtoul_10(&cp);
cp = skip_fields(cp, 3); /* timeout, it_real_value, start_time */ cp = skip_fields(cp, 3); /* timeout, it_real_value, start_time */
sp->vsz = fast_strtoul_10(cp, &cp) >> 10; /* vsize is in bytes and we want kb */ sp->vsz = fast_strtoul_10(&cp) >> 10; /* vsize is in bytes and we want kb */
sp->rss = fast_strtoul_10(cp, &cp) >> 10; sp->rss = fast_strtoul_10(&cp) >> 10;
#endif #endif
if (sp->vsz == 0 && sp->state[0] != 'Z') if (sp->vsz == 0 && sp->state[0] != 'Z')