summaryrefslogtreecommitdiff
path: root/procps/top.c
diff options
context:
space:
mode:
authorDenis Vlasenko2007-06-10 18:04:54 +0000
committerDenis Vlasenko2007-06-10 18:04:54 +0000
commitb1e5addfedf9de4bf5dfa910756747b2b4c1f2bd (patch)
tree7ae41a8cc5b53a8d3e771b71e00a6ae64ce5127d /procps/top.c
parent5a65447e3090908b4bad645c84e37298ca7a7449 (diff)
downloadbusybox-b1e5addfedf9de4bf5dfa910756747b2b4c1f2bd.zip
busybox-b1e5addfedf9de4bf5dfa910756747b2b4c1f2bd.tar.gz
top: improve global CPU percentage (smaller & faster code)
Diffstat (limited to 'procps/top.c')
-rw-r--r--procps/top.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/procps/top.c b/procps/top.c
index 6d8dfd8..564e943 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -292,8 +292,15 @@ static unsigned long display_generic(int scr_width)
/*
* xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
*/
-#define CALC_STAT(xxx) div_t xxx = div(1000 * (jif.xxx - prev_jif.xxx) / total_diff, 10)
-#define SHOW_STAT(xxx) xxx.quot, '0'+xxx.rem
+ /* using (unsigned) cast to make multiplication cheaper: */
+#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
+#define SHOW_STAT(xxx) xxx
+#define FMT "%3u%%"
+// We can display fractional percents, but at least in glibc div() is a _function_
+// and generated code is really awful and big (+0.5k more code):
+//#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
+//#define FMT "%3u.%c%%"
unsigned total_diff = (jif.total - prev_jif.total ? : 1);
CALC_STAT(usr);
CALC_STAT(sys);
@@ -305,10 +312,10 @@ static unsigned long display_generic(int scr_width)
//CALC_STAT(steal);
snprintf(scrbuf, scr_width,
- /* %3u.%c in practice almost never displays "100.0"
- * and thus has implicit leading space: " 99.6" */
- "CPU:%3u.%c%% us%3u.%c%% sy%3u.%c%% ni%3u.%c%% id%3u.%c%% wa%3u.%c%% hi%3u.%c%% si",
- // %3u.%c%%st", - what is this 'steal' thing?
+ /* %3u in practice almost never displays "100"
+ * and thus has implicit leading space: " 99" */
+ "CPU:"FMT" usr"FMT" sys"FMT" nice"FMT" idle"FMT" wait"FMT" irq"FMT" softirq",
+ // FMT" steal", - what is this 'steal' thing?
// I doubt anyone needs to know it
SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
@@ -317,6 +324,7 @@ static unsigned long display_generic(int scr_width)
puts(scrbuf);
#undef SHOW_STAT
#undef CALC_STAT
+#undef FMT
}
snprintf(scrbuf, scr_width, "Load average: %s", buf);