aboutsummaryrefslogtreecommitdiff
path: root/procps/top.c
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-05 00:38:51 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-05 00:38:51 +0000
commitd2c185589694b83e492144a6a5d0f79e16dd9b74 (patch)
tree12930c47ad7f06dc86fc82279a575a6338c4aa7b /procps/top.c
parent35674e54f092dfac129d992a80833d5ff740fd1d (diff)
downloadbusybox-w32-d2c185589694b83e492144a6a5d0f79e16dd9b74.tar.gz
busybox-w32-d2c185589694b83e492144a6a5d0f79e16dd9b74.tar.bz2
busybox-w32-d2c185589694b83e492144a6a5d0f79e16dd9b74.zip
top: improve CPU% calculation
style fixes git-svn-id: svn://busybox.net/trunk/busybox@16508 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'procps/top.c')
-rw-r--r--procps/top.c74
1 files changed, 39 insertions, 35 deletions
diff --git a/procps/top.c b/procps/top.c
index 3ff61dfa7..c76fdc312 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -36,7 +36,7 @@ static procps_status_t *top; /* Hehe */
36static int ntop; 36static int ntop;
37#define OPT_BATCH_MODE (option_mask32 & 0x4) 37#define OPT_BATCH_MODE (option_mask32 & 0x4)
38 38
39#ifdef CONFIG_FEATURE_USE_TERMIOS 39#if ENABLE_FEATURE_USE_TERMIOS
40static int pid_sort(procps_status_t *P, procps_status_t *Q) 40static int pid_sort(procps_status_t *P, procps_status_t *Q)
41{ 41{
42 return (Q->pid - P->pid); 42 return (Q->pid - P->pid);
@@ -48,10 +48,10 @@ static int mem_sort(procps_status_t *P, procps_status_t *Q)
48 return (int)(Q->rss - P->rss); 48 return (int)(Q->rss - P->rss);
49} 49}
50 50
51#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 51#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
52 52
53#define sort_depth 3 53enum { SORT_DEPTH = 3 };
54static cmp_t sort_function[sort_depth]; 54static cmp_t sort_function[SORT_DEPTH];
55 55
56static int pcpu_sort(procps_status_t *P, procps_status_t *Q) 56static int pcpu_sort(procps_status_t *P, procps_status_t *Q)
57{ 57{
@@ -66,7 +66,7 @@ static int time_sort(procps_status_t *P, procps_status_t *Q)
66static int mult_lvl_cmp(void* a, void* b) { 66static int mult_lvl_cmp(void* a, void* b) {
67 int i, cmp_val; 67 int i, cmp_val;
68 68
69 for (i = 0; i < sort_depth; i++) { 69 for (i = 0; i < SORT_DEPTH; i++) {
70 cmp_val = (*sort_function[i])(a, b); 70 cmp_val = (*sort_function[i])(a, b);
71 if (cmp_val != 0) 71 if (cmp_val != 0)
72 return cmp_val; 72 return cmp_val;
@@ -93,13 +93,12 @@ static int prev_hist_count;
93static unsigned total_pcpu; 93static unsigned total_pcpu;
94/* static unsigned long total_rss; */ 94/* static unsigned long total_rss; */
95 95
96struct jiffy_counts { 96typedef struct {
97 unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal; 97 unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
98 unsigned long long total; 98 unsigned long long total;
99 unsigned long long busy; 99 unsigned long long busy;
100}; 100} jiffy_counts_t;
101static struct jiffy_counts jif, prev_jif; 101static jiffy_counts_t jif, prev_jif;
102
103static void get_jiffy_counts(void) 102static void get_jiffy_counts(void)
104{ 103{
105 FILE* fp = xfopen("stat", "r"); 104 FILE* fp = xfopen("stat", "r");
@@ -170,7 +169,7 @@ static void do_stats(void)
170} 169}
171#else 170#else
172static cmp_t sort_function; 171static cmp_t sort_function;
173#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ 172#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
174 173
175/* display generic info (meminfo / loadavg) */ 174/* display generic info (meminfo / loadavg) */
176static unsigned long display_generic(int scr_width) 175static unsigned long display_generic(int scr_width)
@@ -271,8 +270,9 @@ static void display_status(int count, int scr_width)
271 unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */ 270 unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
272 unsigned pmem_shift, pmem_scale; 271 unsigned pmem_shift, pmem_scale;
273 272
274#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 273#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
275 unsigned pcpu_shift, pcpu_scale; 274 unsigned pcpu_shift, pcpu_scale;
275 unsigned busy_jifs;
276 276
277 /* what info of the processes is shown */ 277 /* what info of the processes is shown */
278 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, 278 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
@@ -296,7 +296,12 @@ static void display_status(int count, int scr_width)
296 pmem_scale /= 4; 296 pmem_scale /= 4;
297 pmem_shift -= 2; 297 pmem_shift -= 2;
298 } 298 }
299#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 299#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
300 busy_jifs = jif.busy - prev_jif.busy;
301 /* This happens if there were lots of short-lived processes
302 * between two top updates (e.g. compilation) */
303 if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
304
300 /* 305 /*
301 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks 306 * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
302 * (pcpu is delta of sys+user time between samples) 307 * (pcpu is delta of sys+user time between samples)
@@ -306,7 +311,7 @@ static void display_status(int count, int scr_width)
306 * we assume that unsigned is at least 32-bit. 311 * we assume that unsigned is at least 32-bit.
307 */ 312 */
308 pcpu_shift = 6; 313 pcpu_shift = 6;
309 pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1); 314 pcpu_scale = (1000*64*(uint16_t)busy_jifs ? : 1);
310 while (pcpu_scale < (1U<<(bits_per_int-2))) { 315 while (pcpu_scale < (1U<<(bits_per_int-2))) {
311 pcpu_scale *= 4; 316 pcpu_scale *= 4;
312 pcpu_shift += 2; 317 pcpu_shift += 2;
@@ -352,7 +357,7 @@ static void clearmems(void)
352 ntop = 0; 357 ntop = 0;
353} 358}
354 359
355#ifdef CONFIG_FEATURE_USE_TERMIOS 360#if ENABLE_FEATURE_USE_TERMIOS
356#include <termios.h> 361#include <termios.h>
357#include <signal.h> 362#include <signal.h>
358 363
@@ -362,12 +367,12 @@ static struct termios initial_settings;
362static void reset_term(void) 367static void reset_term(void)
363{ 368{
364 tcsetattr(0, TCSANOW, (void *) &initial_settings); 369 tcsetattr(0, TCSANOW, (void *) &initial_settings);
365#ifdef CONFIG_FEATURE_CLEAN_UP 370#if ENABLE_FEATURE_CLEAN_UP
366 clearmems(); 371 clearmems();
367#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 372#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
368 free(prev_hist); 373 free(prev_hist);
369#endif 374#endif
370#endif /* CONFIG_FEATURE_CLEAN_UP */ 375#endif /* FEATURE_CLEAN_UP */
371} 376}
372 377
373static void sig_catcher(int sig ATTRIBUTE_UNUSED) 378static void sig_catcher(int sig ATTRIBUTE_UNUSED)
@@ -375,7 +380,7 @@ static void sig_catcher(int sig ATTRIBUTE_UNUSED)
375 reset_term(); 380 reset_term();
376 exit(1); 381 exit(1);
377} 382}
378#endif /* CONFIG_FEATURE_USE_TERMIOS */ 383#endif /* FEATURE_USE_TERMIOS */
379 384
380 385
381int top_main(int argc, char **argv) 386int top_main(int argc, char **argv)
@@ -384,25 +389,24 @@ int top_main(int argc, char **argv)
384 unsigned interval = 5; /* default update rate is 5 seconds */ 389 unsigned interval = 5; /* default update rate is 5 seconds */
385 unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */ 390 unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
386 char *sinterval, *siterations; 391 char *sinterval, *siterations;
387#ifdef CONFIG_FEATURE_USE_TERMIOS 392#if ENABLE_FEATURE_USE_TERMIOS
388 struct termios new_settings; 393 struct termios new_settings;
389 struct timeval tv; 394 struct timeval tv;
390 fd_set readfds; 395 fd_set readfds;
391 unsigned char c; 396 unsigned char c;
392#endif /* CONFIG_FEATURE_USE_TERMIOS */ 397#endif /* FEATURE_USE_TERMIOS */
393 398
394 /* do normal option parsing */ 399 /* do normal option parsing */
395 interval = 5; 400 interval = 5;
396 opt_complementary = "-"; 401 opt_complementary = "-";
397 getopt32(argc, argv, "d:n:b", 402 getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
398 &sinterval, &siterations);
399 if (option_mask32 & 0x1) interval = xatou(sinterval); // -d 403 if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
400 if (option_mask32 & 0x2) iterations = xatou(siterations); // -n 404 if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
401 //if (option_mask32 & 0x4) // -b 405 //if (option_mask32 & 0x4) // -b
402 406
403 /* change to /proc */ 407 /* change to /proc */
404 xchdir("/proc"); 408 xchdir("/proc");
405#ifdef CONFIG_FEATURE_USE_TERMIOS 409#if ENABLE_FEATURE_USE_TERMIOS
406 tcgetattr(0, (void *) &initial_settings); 410 tcgetattr(0, (void *) &initial_settings);
407 memcpy(&new_settings, &initial_settings, sizeof(struct termios)); 411 memcpy(&new_settings, &initial_settings, sizeof(struct termios));
408 /* unbuffered input, turn off echo */ 412 /* unbuffered input, turn off echo */
@@ -412,15 +416,15 @@ int top_main(int argc, char **argv)
412 signal(SIGINT, sig_catcher); 416 signal(SIGINT, sig_catcher);
413 tcsetattr(0, TCSANOW, (void *) &new_settings); 417 tcsetattr(0, TCSANOW, (void *) &new_settings);
414 atexit(reset_term); 418 atexit(reset_term);
415#endif /* CONFIG_FEATURE_USE_TERMIOS */ 419#endif /* FEATURE_USE_TERMIOS */
416 420
417#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 421#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
418 sort_function[0] = pcpu_sort; 422 sort_function[0] = pcpu_sort;
419 sort_function[1] = mem_sort; 423 sort_function[1] = mem_sort;
420 sort_function[2] = time_sort; 424 sort_function[2] = time_sort;
421#else 425#else
422 sort_function = mem_sort; 426 sort_function = mem_sort;
423#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ 427#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
424 428
425 while (1) { 429 while (1) {
426 procps_status_t *p; 430 procps_status_t *p;
@@ -428,14 +432,14 @@ int top_main(int argc, char **argv)
428 /* Default to 25 lines - 5 lines for status */ 432 /* Default to 25 lines - 5 lines for status */
429 lines = 24 - 3; 433 lines = 24 - 3;
430 col = 79; 434 col = 79;
431#ifdef CONFIG_FEATURE_USE_TERMIOS 435#if ENABLE_FEATURE_USE_TERMIOS
432 get_terminal_width_height(0, &col, &lines); 436 get_terminal_width_height(0, &col, &lines);
433 if (lines < 5 || col < MIN_WIDTH) { 437 if (lines < 5 || col < MIN_WIDTH) {
434 sleep(interval); 438 sleep(interval);
435 continue; 439 continue;
436 } 440 }
437 lines -= 3; 441 lines -= 3;
438#endif /* CONFIG_FEATURE_USE_TERMIOS */ 442#endif /* FEATURE_USE_TERMIOS */
439 443
440 /* read process IDs & status for all the processes */ 444 /* read process IDs & status for all the processes */
441 while ((p = procps_scan(0)) != 0) { 445 while ((p = procps_scan(0)) != 0) {
@@ -447,7 +451,7 @@ int top_main(int argc, char **argv)
447 if (ntop == 0) { 451 if (ntop == 0) {
448 bb_error_msg_and_die("can't find process info in /proc"); 452 bb_error_msg_and_die("can't find process info in /proc");
449 } 453 }
450#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 454#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
451 if (!prev_hist_count) { 455 if (!prev_hist_count) {
452 do_stats(); 456 do_stats();
453 sleep(1); 457 sleep(1);
@@ -458,14 +462,14 @@ int top_main(int argc, char **argv)
458 qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp); 462 qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp);
459#else 463#else
460 qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); 464 qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
461#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ 465#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
462 count = lines; 466 count = lines;
463 if (OPT_BATCH_MODE || count > ntop) { 467 if (OPT_BATCH_MODE || count > ntop) {
464 count = ntop; 468 count = ntop;
465 } 469 }
466 /* show status for each of the processes */ 470 /* show status for each of the processes */
467 display_status(count, col); 471 display_status(count, col);
468#ifdef CONFIG_FEATURE_USE_TERMIOS 472#if ENABLE_FEATURE_USE_TERMIOS
469 tv.tv_sec = interval; 473 tv.tv_sec = interval;
470 tv.tv_usec = 0; 474 tv.tv_usec = 0;
471 FD_ZERO(&readfds); 475 FD_ZERO(&readfds);
@@ -478,7 +482,7 @@ int top_main(int argc, char **argv)
478 if (c == 'q' || c == initial_settings.c_cc[VINTR]) 482 if (c == 'q' || c == initial_settings.c_cc[VINTR])
479 break; 483 break;
480 if (c == 'M') { 484 if (c == 'M') {
481#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 485#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
482 sort_function[0] = mem_sort; 486 sort_function[0] = mem_sort;
483 sort_function[1] = pcpu_sort; 487 sort_function[1] = pcpu_sort;
484 sort_function[2] = time_sort; 488 sort_function[2] = time_sort;
@@ -486,7 +490,7 @@ int top_main(int argc, char **argv)
486 sort_function = mem_sort; 490 sort_function = mem_sort;
487#endif 491#endif
488 } 492 }
489#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 493#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
490 if (c == 'P') { 494 if (c == 'P') {
491 sort_function[0] = pcpu_sort; 495 sort_function[0] = pcpu_sort;
492 sort_function[1] = mem_sort; 496 sort_function[1] = mem_sort;
@@ -499,7 +503,7 @@ int top_main(int argc, char **argv)
499 } 503 }
500#endif 504#endif
501 if (c == 'N') { 505 if (c == 'N') {
502#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE 506#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
503 sort_function[0] = pid_sort; 507 sort_function[0] = pid_sort;
504#else 508#else
505 sort_function = pid_sort; 509 sort_function = pid_sort;
@@ -510,7 +514,7 @@ int top_main(int argc, char **argv)
510 break; 514 break;
511#else 515#else
512 sleep(interval); 516 sleep(interval);
513#endif /* CONFIG_FEATURE_USE_TERMIOS */ 517#endif /* FEATURE_USE_TERMIOS */
514 clearmems(); 518 clearmems();
515 } 519 }
516 if (ENABLE_FEATURE_CLEAN_UP) 520 if (ENABLE_FEATURE_CLEAN_UP)