aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-07-15 19:27:48 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-07-15 19:27:48 +0000
commit110967a6f7b214eb8a3b78f9a3e09a0de6412a88 (patch)
tree29587da99dcc10c5a851e7299a5fa6df43da1d1b /procps
parent24c5fbaf42158d54ec9c334174c267e9c1c84f4c (diff)
downloadbusybox-w32-110967a6f7b214eb8a3b78f9a3e09a0de6412a88.tar.gz
busybox-w32-110967a6f7b214eb8a3b78f9a3e09a0de6412a88.tar.bz2
busybox-w32-110967a6f7b214eb8a3b78f9a3e09a0de6412a88.zip
top: nested function allows us to reuse some code
(not everyone likes them - but code does get smaller). display_generic - 761 +761 static.fmt_100percent_8 - 111 +111 fmt_100percent_8 101 - -101 display_status 1489 581 -908 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 0/1 up/down: 872/-1009) Total: -137 bytes text data bss dec hex filename 677020 3344 13936 694300 a981c busybox_old 676876 3344 13936 694156 a978c busybox_unstripped
Diffstat (limited to 'procps')
-rw-r--r--procps/Config.in2
-rw-r--r--procps/top.c110
2 files changed, 58 insertions, 54 deletions
diff --git a/procps/Config.in b/procps/Config.in
index b834fbf96..f041b5d72 100644
--- a/procps/Config.in
+++ b/procps/Config.in
@@ -122,7 +122,7 @@ config FEATURE_TOP_CPU_GLOBAL_PERCENTS
122 Makes top display "CPU: NN% usr NN% sys..." line. 122 Makes top display "CPU: NN% usr NN% sys..." line.
123 123
124config FEATURE_TOP_DECIMALS 124config FEATURE_TOP_DECIMALS
125 bool "Show 1/10th of a percent in CPU/mem statistics (adds 0.5k bytes)" 125 bool "Show 1/10th of a percent in CPU/mem statistics (adds 0.3k bytes)"
126 default n 126 default n
127 depends on FEATURE_TOP_CPU_USAGE_PERCENTAGE 127 depends on FEATURE_TOP_CPU_USAGE_PERCENTAGE
128 help 128 help
diff --git a/procps/top.c b/procps/top.c
index 70e479a80..e9ccac0cd 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -212,30 +212,6 @@ static void do_stats(void)
212} 212}
213#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ 213#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
214 214
215#if ENABLE_FEATURE_TOP_DECIMALS
216/* formats 7 char string (8 with terminating NUL) */
217static 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
239/* display generic info (meminfo / loadavg) */ 215/* display generic info (meminfo / loadavg) */
240static unsigned long display_generic(int scr_width) 216static unsigned long display_generic(int scr_width)
241{ 217{
@@ -243,6 +219,34 @@ static unsigned long display_generic(int scr_width)
243 char buf[80]; 219 char buf[80];
244 char scrbuf[80]; 220 char scrbuf[80];
245 unsigned long total, used, mfree, shared, buffers, cached; 221 unsigned long total, used, mfree, shared, buffers, cached;
222 unsigned total_diff;
223
224#if ENABLE_FEATURE_TOP_DECIMALS
225 /* formats 7 char string (8 with terminating NUL) */
226 /* using GCCism (nested function) - we need to access total_diff */
227 /* This produces more than 100 bytes smaller code */
228 char *fmt_100percent_8(char pbuf[8], unsigned value)
229 {
230 unsigned t;
231 if (value >= total_diff) { /* 100% ? */
232 strcpy(pbuf, " 100% ");
233 return pbuf;
234 }
235 /* else generate " [N/space]N.N% " string */
236 value = 1000 * value / total_diff;
237 t = value / 100;
238 value = value % 100;
239 pbuf[0] = ' ';
240 pbuf[1] = t ? t + '0' : ' ';
241 pbuf[2] = '0' + (value / 10);
242 pbuf[3] = '.';
243 pbuf[4] = '0' + (value % 10);
244 pbuf[5] = '%';
245 pbuf[6] = ' ';
246 pbuf[7] = '\0';
247 return pbuf;
248 }
249#endif
246 250
247 /* read memory info */ 251 /* read memory info */
248 fp = xfopen("meminfo", "r"); 252 fp = xfopen("meminfo", "r");
@@ -292,60 +296,60 @@ static unsigned long display_generic(int scr_width)
292 } 296 }
293 fclose(fp); 297 fclose(fp);
294 298
295 /* read load average as a string */ 299 /* output memory info */
296 buf[0] = '\0';
297 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
298 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
299
300 /* output memory info and load average */
301 /* clear screen & go to top */
302 if (scr_width > sizeof(scrbuf)) 300 if (scr_width > sizeof(scrbuf))
303 scr_width = sizeof(scrbuf); 301 scr_width = sizeof(scrbuf);
304 snprintf(scrbuf, scr_width, 302 snprintf(scrbuf, scr_width,
305 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached", 303 "Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
306 used, mfree, shared, buffers, cached); 304 used, mfree, shared, buffers, cached);
307 305 /* clear screen & go to top */
308 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf); 306 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
309 307
310 if (ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS) { 308 if (ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS) {
311 /* 309 /*
312 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100% 310 * xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
313 */ 311 */
314 /* using (unsigned) casts to make operations cheaper cheaper */ 312 /* using (unsigned) casts to make operations cheaper */
315 unsigned total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1); 313 total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
316#if ENABLE_FEATURE_TOP_DECIMALS 314#if ENABLE_FEATURE_TOP_DECIMALS
317/* Generated code is approx +0.5k */ 315/* Generated code is approx +0.3k */
318#define CALC_STAT(xxx) char xxx[8] 316#define CALC_STAT(xxx) char xxx[8]
319#define SHOW_STAT(xxx) fmt_100percent_8(xxx, 1000 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff) 317#define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx))
320#define FMT "%s" 318#define FMT "%s"
321#else 319#else
322#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff 320#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
323#define SHOW_STAT(xxx) xxx 321#define SHOW_STAT(xxx) xxx
324#define FMT "%4u%% " 322#define FMT "%4u%% "
325#endif 323#endif
326 CALC_STAT(usr); 324 { /* need block: CALC_STAT are declarations */
327 CALC_STAT(sys); 325 CALC_STAT(usr);
328 CALC_STAT(nic); 326 CALC_STAT(sys);
329 CALC_STAT(idle); 327 CALC_STAT(nic);
330 CALC_STAT(iowait); 328 CALC_STAT(idle);
331 CALC_STAT(irq); 329 CALC_STAT(iowait);
332 CALC_STAT(softirq); 330 CALC_STAT(irq);
333 //CALC_STAT(steal); 331 CALC_STAT(softirq);
334 332 //CALC_STAT(steal);
335 snprintf(scrbuf, scr_width, 333
336 /* Barely fits in 79 chars when in "decimals" mode. */ 334 snprintf(scrbuf, scr_width,
337 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq", 335 /* Barely fits in 79 chars when in "decimals" mode. */
338 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle), 336 "CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
339 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq) 337 SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
340 //, SHOW_STAT(steal) - what is this 'steal' thing? 338 SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
341 // I doubt anyone wants to know it 339 //, SHOW_STAT(steal) - what is this 'steal' thing?
342 ); 340 // I doubt anyone wants to know it
341 );
342 }
343 puts(scrbuf); 343 puts(scrbuf);
344#undef SHOW_STAT 344#undef SHOW_STAT
345#undef CALC_STAT 345#undef CALC_STAT
346#undef FMT 346#undef FMT
347 } 347 }
348 348
349 /* read load average as a string */
350 buf[0] = '\0';
351 open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
352 buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
349 snprintf(scrbuf, scr_width, "Load average: %s", buf); 353 snprintf(scrbuf, scr_width, "Load average: %s", buf);
350 puts(scrbuf); 354 puts(scrbuf);
351 355