aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-30 08:01:04 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-30 08:01:04 +0000
commit42ee26d00cc9c9a6cf1f652a5351b30ac221fa34 (patch)
tree4cf26bdee66481e40afdefd5ebc804b7068e6efe /libbb
parentae7f7ebcb767714e4d294025e432638329f2948d (diff)
downloadbusybox-w32-42ee26d00cc9c9a6cf1f652a5351b30ac221fa34.tar.gz
busybox-w32-42ee26d00cc9c9a6cf1f652a5351b30ac221fa34.tar.bz2
busybox-w32-42ee26d00cc9c9a6cf1f652a5351b30ac221fa34.zip
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
Diffstat (limited to 'libbb')
-rw-r--r--libbb/procps.c38
1 files changed, 22 insertions, 16 deletions
diff --git a/libbb/procps.c b/libbb/procps.c
index 9f5a4c062..5924d60a8 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -109,9 +109,10 @@ void free_procps_scan(procps_status_t* sp)
109 109
110#if ENABLE_FEATURE_FAST_TOP 110#if ENABLE_FEATURE_FAST_TOP
111/* We cut a lot of corners here for speed */ 111/* We cut a lot of corners here for speed */
112static unsigned long fast_strtoul_10(char *str, char **endptr) 112static unsigned long fast_strtoul_10(char **endptr)
113{ 113{
114 char c; 114 char c;
115 char *str = *endptr;
115 unsigned long n = *str - '0'; 116 unsigned long n = *str - '0';
116 117
117 while ((c = *++str) != ' ') 118 while ((c = *++str) != ' ')
@@ -123,7 +124,9 @@ static unsigned long fast_strtoul_10(char *str, char **endptr)
123static char *skip_fields(char *str, int count) 124static char *skip_fields(char *str, int count)
124{ 125{
125 do { 126 do {
126 str = skip_non_whitespace(str); str++; 127 while (*str++ != ' ')
128 continue;
129 /* we found a space char, str points after it */
127 } while (--count); 130 } while (--count);
128 return str; 131 return str;
129} 132}
@@ -181,11 +184,11 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
181 } 184 }
182 185
183 if (flags & PSSCAN_STAT) { 186 if (flags & PSSCAN_STAT) {
184 char *cp; 187 char *cp, *comm1;
188 int tty;
185#if !ENABLE_FEATURE_FAST_TOP 189#if !ENABLE_FEATURE_FAST_TOP
186 unsigned long vsz, rss; 190 unsigned long vsz, rss;
187#endif 191#endif
188 int tty;
189 192
190 /* see proc(5) for some details on this */ 193 /* see proc(5) for some details on this */
191 strcpy(filename_tail, "/stat"); 194 strcpy(filename_tail, "/stat");
@@ -193,12 +196,14 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
193 if (n < 0) 196 if (n < 0)
194 break; 197 break;
195 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ 198 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
196 if (!cp || cp[1] != ' ') 199 /*if (!cp || cp[1] != ' ')
197 break; 200 break;*/
198 cp[0] = '\0'; 201 cp[0] = '\0';
199 if (sizeof(sp->comm) < 16) 202 if (sizeof(sp->comm) < 16)
200 BUG_comm_size(); 203 BUG_comm_size();
201 sscanf(buf, "%*s (%15c", sp->comm); 204 comm1 = strchr(buf, '(');
205 /*if (comm1)*/
206 safe_strncpy(sp->comm, comm1 + 1, sizeof(sp->comm));
202 207
203#if !ENABLE_FEATURE_FAST_TOP 208#if !ENABLE_FEATURE_FAST_TOP
204 n = sscanf(cp+2, 209 n = sscanf(cp+2,
@@ -231,20 +236,21 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
231/* This costs ~100 bytes more but makes top faster by 20% 236/* This costs ~100 bytes more but makes top faster by 20%
232 * If you run 10000 processes, this may be important for you */ 237 * If you run 10000 processes, this may be important for you */
233 sp->state[0] = cp[2]; 238 sp->state[0] = cp[2];
234 sp->ppid = fast_strtoul_10(cp + 4, &cp); 239 cp += 4;
235 sp->pgid = fast_strtoul_10(cp, &cp); 240 sp->ppid = fast_strtoul_10(&cp);
236 sp->sid = fast_strtoul_10(cp, &cp); 241 sp->pgid = fast_strtoul_10(&cp);
237 tty = fast_strtoul_10(cp, &cp); 242 sp->sid = fast_strtoul_10(&cp);
243 tty = fast_strtoul_10(&cp);
238 sp->tty_major = (tty >> 8) & 0xfff; 244 sp->tty_major = (tty >> 8) & 0xfff;
239 sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00); 245 sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
240 cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ 246 cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
241 sp->utime = fast_strtoul_10(cp, &cp); 247 sp->utime = fast_strtoul_10(&cp);
242 sp->stime = fast_strtoul_10(cp, &cp); 248 sp->stime = fast_strtoul_10(&cp);
243 cp = skip_fields(cp, 3); /* cutime, cstime, priority */ 249 cp = skip_fields(cp, 3); /* cutime, cstime, priority */
244 tasknice = fast_strtoul_10(cp, &cp); 250 tasknice = fast_strtoul_10(&cp);
245 cp = skip_fields(cp, 3); /* timeout, it_real_value, start_time */ 251 cp = skip_fields(cp, 3); /* timeout, it_real_value, start_time */
246 sp->vsz = fast_strtoul_10(cp, &cp) >> 10; /* vsize is in bytes and we want kb */ 252 sp->vsz = fast_strtoul_10(&cp) >> 10; /* vsize is in bytes and we want kb */
247 sp->rss = fast_strtoul_10(cp, &cp) >> 10; 253 sp->rss = fast_strtoul_10(&cp) >> 10;
248#endif 254#endif
249 255
250 if (sp->vsz == 0 && sp->state[0] != 'Z') 256 if (sp->vsz == 0 && sp->state[0] != 'Z')