diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-07-15 19:25:01 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-07-15 19:25:01 +0000 |
commit | 24c5fbaf42158d54ec9c334174c267e9c1c84f4c (patch) | |
tree | b662d6957d529f31e83c23840b7f86e36b7b4642 /procps | |
parent | c1166c3a8c8cd4507a1dcd1ea3b3e1af9b24b319 (diff) | |
download | busybox-w32-24c5fbaf42158d54ec9c334174c267e9c1c84f4c.tar.gz busybox-w32-24c5fbaf42158d54ec9c334174c267e9c1c84f4c.tar.bz2 busybox-w32-24c5fbaf42158d54ec9c334174c267e9c1c84f4c.zip |
top: make "100%" case less ugly, and shrink code while at it
fmt_100percent_8 - 101 +101
.rodata 129185 129153 -32
display_status 1726 1489 -237
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/2 up/down: 101/-269) Total: -168 bytes
text data bss dec hex filename
677180 3344 13936 694460 a98bc busybox_old
677020 3344 13936 694300 a981c busybox_unstripped
Diffstat (limited to 'procps')
-rw-r--r-- | procps/top.c | 62 |
1 files changed, 42 insertions, 20 deletions
diff --git a/procps/top.c b/procps/top.c index 22ac49cc5..70e479a80 100644 --- a/procps/top.c +++ b/procps/top.c | |||
@@ -212,7 +212,30 @@ static void do_stats(void) | |||
212 | } | 212 | } |
213 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ | 213 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
214 | 214 | ||
215 | 215 | #if ENABLE_FEATURE_TOP_DECIMALS | |
216 | /* formats 7 char string (8 with terminating NUL) */ | ||
217 | static char *fmt_100percent_8(char buf[8], unsigned value_10) | ||
218 | { | ||
219 | unsigned t; | ||
220 | if (value_10 >= 1000) { | ||
221 | strcpy(buf, " 100% "); | ||
222 | return buf; | ||
223 | } | ||
224 | /* else generate " [N/space]N.N% " string */ | ||
225 | t = value_10 / 100; | ||
226 | value_10 = value_10 % 100; | ||
227 | buf[0] = ' '; | ||
228 | buf[1] = t ? t + '0' : ' '; | ||
229 | buf[2] = '0' + (value_10 / 10); | ||
230 | buf[3] = '.'; | ||
231 | buf[4] = '0' + (value_10 % 10); | ||
232 | buf[5] = '%'; | ||
233 | buf[6] = ' '; | ||
234 | buf[7] = '\0'; | ||
235 | return buf; | ||
236 | } | ||
237 | #endif | ||
238 | |||
216 | /* display generic info (meminfo / loadavg) */ | 239 | /* display generic info (meminfo / loadavg) */ |
217 | static unsigned long display_generic(int scr_width) | 240 | static unsigned long display_generic(int scr_width) |
218 | { | 241 | { |
@@ -288,19 +311,17 @@ static unsigned long display_generic(int scr_width) | |||
288 | /* | 311 | /* |
289 | * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100% | 312 | * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100% |
290 | */ | 313 | */ |
291 | /* using (unsigned) casts to make multiplication cheaper: */ | 314 | /* using (unsigned) casts to make operations cheaper cheaper */ |
292 | unsigned total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1); | 315 | unsigned total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1); |
293 | #if ENABLE_FEATURE_TOP_DECIMALS | 316 | #if ENABLE_FEATURE_TOP_DECIMALS |
294 | /* Generated code is approx +0.5k */ | 317 | /* Generated code is approx +0.5k */ |
295 | #define CALC_STAT(xxx) div_t xxx = div(1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff, 10) | 318 | #define CALC_STAT(xxx) char xxx[8] |
296 | #define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem | 319 | #define SHOW_STAT(xxx) fmt_100percent_8(xxx, 1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff) |
297 | /* %3u in practice almost never displays "100" | 320 | #define FMT "%s" |
298 | * and thus has implicit leading space: " 99" */ | ||
299 | #define FMT "%3u.%c%%" | ||
300 | #else | 321 | #else |
301 | #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff | 322 | #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff |
302 | #define SHOW_STAT(xxx) xxx | 323 | #define SHOW_STAT(xxx) xxx |
303 | #define FMT "%4u%%" | 324 | #define FMT "%4u%% " |
304 | #endif | 325 | #endif |
305 | CALC_STAT(usr); | 326 | CALC_STAT(usr); |
306 | CALC_STAT(sys); | 327 | CALC_STAT(sys); |
@@ -313,7 +334,7 @@ static unsigned long display_generic(int scr_width) | |||
313 | 334 | ||
314 | snprintf(scrbuf, scr_width, | 335 | snprintf(scrbuf, scr_width, |
315 | /* Barely fits in 79 chars when in "decimals" mode. */ | 336 | /* Barely fits in 79 chars when in "decimals" mode. */ |
316 | "CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" io"FMT" irq"FMT" softirq", | 337 | "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq", |
317 | SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle), | 338 | SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle), |
318 | SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq) | 339 | SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq) |
319 | //, SHOW_STAT(steal) - what is this 'steal' thing? | 340 | //, SHOW_STAT(steal) - what is this 'steal' thing? |
@@ -331,17 +352,6 @@ static unsigned long display_generic(int scr_width) | |||
331 | return total; | 352 | return total; |
332 | } | 353 | } |
333 | 354 | ||
334 | #if ENABLE_FEATURE_TOP_DECIMALS | ||
335 | #define UPSCALE 1000 | ||
336 | #define CALC_STAT(name, val) div_t name = div((val), 10) | ||
337 | #define SHOW_STAT(name) name.quot, '0'+name.rem | ||
338 | #define FMT "%3u.%c" | ||
339 | #else | ||
340 | #define UPSCALE 100 | ||
341 | #define CALC_STAT(name, val) unsigned name = (val) | ||
342 | #define SHOW_STAT(name) name | ||
343 | #define FMT "%4u%%" | ||
344 | #endif | ||
345 | /* display process statuses */ | 355 | /* display process statuses */ |
346 | static void display_status(int count, int scr_width) | 356 | static void display_status(int count, int scr_width) |
347 | { | 357 | { |
@@ -373,6 +383,17 @@ static void display_status(int count, int scr_width) | |||
373 | sizeof( " PID PPID USER STAT VSZ %MEM C") | 383 | sizeof( " PID PPID USER STAT VSZ %MEM C") |
374 | #endif | 384 | #endif |
375 | 385 | ||
386 | #if ENABLE_FEATURE_TOP_DECIMALS | ||
387 | #define UPSCALE 1000 | ||
388 | #define CALC_STAT(name, val) div_t name = div((val), 10) | ||
389 | #define SHOW_STAT(name) name.quot, '0'+name.rem | ||
390 | #define FMT "%3u.%c" | ||
391 | #else | ||
392 | #define UPSCALE 100 | ||
393 | #define CALC_STAT(name, val) unsigned name = (val) | ||
394 | #define SHOW_STAT(name) name | ||
395 | #define FMT "%4u%%" | ||
396 | #endif | ||
376 | /* | 397 | /* |
377 | * MEM% = s->vsz/MemTotal | 398 | * MEM% = s->vsz/MemTotal |
378 | */ | 399 | */ |
@@ -452,6 +473,7 @@ static void display_status(int count, int scr_width) | |||
452 | putchar(OPT_BATCH_MODE ? '\n' : '\r'); | 473 | putchar(OPT_BATCH_MODE ? '\n' : '\r'); |
453 | fflush(stdout); | 474 | fflush(stdout); |
454 | } | 475 | } |
476 | #undef UPSCALE | ||
455 | #undef SHOW_STAT | 477 | #undef SHOW_STAT |
456 | #undef CALC_STAT | 478 | #undef CALC_STAT |
457 | #undef FMT | 479 | #undef FMT |