aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-07-15 19:23:38 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-07-15 19:23:38 +0000
commitc1166c3a8c8cd4507a1dcd1ea3b3e1af9b24b319 (patch)
treedff839e17d8b7748c8ca9bfb004f5043a4e660b8
parent9109fbeaa115f8d8364e40d866914cc9e46d51e5 (diff)
downloadbusybox-w32-c1166c3a8c8cd4507a1dcd1ea3b3e1af9b24b319.tar.gz
busybox-w32-c1166c3a8c8cd4507a1dcd1ea3b3e1af9b24b319.tar.bz2
busybox-w32-c1166c3a8c8cd4507a1dcd1ea3b3e1af9b24b319.zip
top: simpler loadavg processing
display_status 1787 1726 -61 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-61) Total: -61 bytes text data bss dec hex filename 677244 3344 13936 694524 a98fc busybox_old 677180 3344 13936 694460 a98bc busybox_unstripped
-rw-r--r--procps/top.c24
1 files changed, 10 insertions, 14 deletions
diff --git a/procps/top.c b/procps/top.c
index cd55ea308..22ac49cc5 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -219,7 +219,6 @@ static unsigned long display_generic(int scr_width)
219 FILE *fp; 219 FILE *fp;
220 char buf[80]; 220 char buf[80];
221 char scrbuf[80]; 221 char scrbuf[80];
222 char *end;
223 unsigned long total, used, mfree, shared, buffers, cached; 222 unsigned long total, used, mfree, shared, buffers, cached;
224 223
225 /* read memory info */ 224 /* read memory info */
@@ -272,18 +271,15 @@ static unsigned long display_generic(int scr_width)
272 271
273 /* read load average as a string */ 272 /* read load average as a string */
274 buf[0] = '\0'; 273 buf[0] = '\0';
275 open_read_close("loadavg", buf, sizeof(buf)); 274 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
276 end = strchr(buf, ' '); 275 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
277 if (end) end = strchr(end+1, ' ');
278 if (end) end = strchr(end+1, ' ');
279 if (end) *end = '\0';
280 276
281 /* output memory info and load average */ 277 /* output memory info and load average */
282 /* clear screen & go to top */ 278 /* clear screen & go to top */
283 if (scr_width > sizeof(scrbuf)) 279 if (scr_width > sizeof(scrbuf))
284 scr_width = sizeof(scrbuf); 280 scr_width = sizeof(scrbuf);
285 snprintf(scrbuf, scr_width, 281 snprintf(scrbuf, scr_width,
286 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached", 282 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
287 used, mfree, shared, buffers, cached); 283 used, mfree, shared, buffers, cached);
288 284
289 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf); 285 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
@@ -292,18 +288,20 @@ static unsigned long display_generic(int scr_width)
292 /* 288 /*
293 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100% 289 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
294 */ 290 */
295 /* using (unsigned) cast to make multiplication cheaper: */ 291 /* using (unsigned) casts to make multiplication cheaper: */
292 unsigned total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
296#if ENABLE_FEATURE_TOP_DECIMALS 293#if ENABLE_FEATURE_TOP_DECIMALS
297/* Generated code is approx +0.5k */ 294/* Generated code is approx +0.5k */
298#define CALC_STAT(xxx) div_t xxx = div(1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff, 10) 295#define CALC_STAT(xxx) div_t xxx = div(1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff, 10)
299#define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem 296#define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem
297/* %3u in practice almost never displays "100"
298 * and thus has implicit leading space: " 99" */
300#define FMT "%3u.%c%%" 299#define FMT "%3u.%c%%"
301#else 300#else
302#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff 301#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
303#define SHOW_STAT(xxx) xxx 302#define SHOW_STAT(xxx) xxx
304#define FMT "%3u%%" 303#define FMT "%4u%%"
305#endif 304#endif
306 unsigned total_diff = (jif.total - prev_jif.total ? : 1);
307 CALC_STAT(usr); 305 CALC_STAT(usr);
308 CALC_STAT(sys); 306 CALC_STAT(sys);
309 CALC_STAT(nic); 307 CALC_STAT(nic);
@@ -314,9 +312,7 @@ static unsigned long display_generic(int scr_width)
314 //CALC_STAT(steal); 312 //CALC_STAT(steal);
315 313
316 snprintf(scrbuf, scr_width, 314 snprintf(scrbuf, scr_width,
317 /* Barely fits in 79 chars when in "decimals" mode. 315 /* Barely fits in 79 chars when in "decimals" mode. */
318 * %3u in practice almost never displays "100"
319 * and thus has implicit leading space: " 99" */
320 "CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" io"FMT" irq"FMT" softirq", 316 "CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" io"FMT" irq"FMT" softirq",
321 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle), 317 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
322 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq) 318 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
@@ -344,7 +340,7 @@ static unsigned long display_generic(int scr_width)
344#define UPSCALE 100 340#define UPSCALE 100
345#define CALC_STAT(name, val) unsigned name = (val) 341#define CALC_STAT(name, val) unsigned name = (val)
346#define SHOW_STAT(name) name 342#define SHOW_STAT(name) name
347#define FMT " %3u%%" 343#define FMT "%4u%%"
348#endif 344#endif
349/* display process statuses */ 345/* display process statuses */
350static void display_status(int count, int scr_width) 346static void display_status(int count, int scr_width)