svn commit: trunk/busybox/procps

vda at busybox.net vda at busybox.net
Sun Jul 15 19:23:39 UTC 2007


Author: vda
Date: 2007-07-15 12:23:38 -0700 (Sun, 15 Jul 2007)
New Revision: 19098

Log:
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



Modified:
   trunk/busybox/procps/top.c


Changeset:
Modified: trunk/busybox/procps/top.c
===================================================================
--- trunk/busybox/procps/top.c	2007-07-15 17:07:58 UTC (rev 19097)
+++ trunk/busybox/procps/top.c	2007-07-15 19:23:38 UTC (rev 19098)
@@ -219,7 +219,6 @@
 	FILE *fp;
 	char buf[80];
 	char scrbuf[80];
-	char *end;
 	unsigned long total, used, mfree, shared, buffers, cached;
 
 	/* read memory info */
@@ -272,18 +271,15 @@
 
 	/* read load average as a string */
 	buf[0] = '\0';
-	open_read_close("loadavg", buf, sizeof(buf));
-	end = strchr(buf, ' ');
-	if (end) end = strchr(end+1, ' ');
-	if (end) end = strchr(end+1, ' ');
-	if (end) *end = '\0';
+	open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
+	buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
 
 	/* output memory info and load average */
 	/* clear screen & go to top */
 	if (scr_width > sizeof(scrbuf))
 		scr_width = sizeof(scrbuf);
 	snprintf(scrbuf, scr_width,
-		"Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
+		"Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
 		used, mfree, shared, buffers, cached);
 
 	printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
@@ -292,18 +288,20 @@
 		/*
 		 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
 		 */
-		/* using (unsigned) cast to make multiplication cheaper: */
+		/* using (unsigned) casts to make multiplication cheaper: */
+		unsigned total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
 #if ENABLE_FEATURE_TOP_DECIMALS
 /* Generated code is approx +0.5k */
 #define CALC_STAT(xxx) div_t xxx = div(1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff, 10)
 #define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem
+/* %3u in practice almost never displays "100"
+ * and thus has implicit leading space:  " 99" */
 #define FMT "%3u.%c%%"
 #else
 #define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
 #define SHOW_STAT(xxx) xxx
-#define FMT "%3u%%"
+#define FMT "%4u%%"
 #endif
-		unsigned total_diff = (jif.total - prev_jif.total ? : 1);
 		CALC_STAT(usr);
 		CALC_STAT(sys);
 		CALC_STAT(nic);
@@ -314,9 +312,7 @@
 		//CALC_STAT(steal);
 
 		snprintf(scrbuf, scr_width,
-			/* Barely fits in 79 chars when in "decimals" mode.
-			 * %3u in practice almost never displays "100"
-			 * and thus has implicit leading space:  " 99" */
+			/* Barely fits in 79 chars when in "decimals" mode. */
 			"CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" io"FMT" irq"FMT" softirq",
 			SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
 			SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
@@ -344,7 +340,7 @@
 #define UPSCALE 100
 #define CALC_STAT(name, val) unsigned name = (val)
 #define SHOW_STAT(name) name
-#define FMT " %3u%%"
+#define FMT "%4u%%"
 #endif
 /* display process statuses */
 static void display_status(int count, int scr_width)




More information about the busybox-cvs mailing list