diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-29 18:18:08 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-08-29 18:18:08 +0000 |
| commit | 8bdba4d011887ce77cd6de93859cacf7adea391e (patch) | |
| tree | b67863867ad867ee15f6fb0695b90c615b513e8a | |
| parent | fb48f6c982479f21457ef652f8a9ffc14b0a6b75 (diff) | |
| download | busybox-w32-8bdba4d011887ce77cd6de93859cacf7adea391e.tar.gz busybox-w32-8bdba4d011887ce77cd6de93859cacf7adea391e.tar.bz2 busybox-w32-8bdba4d011887ce77cd6de93859cacf7adea391e.zip | |
top: don't wait before final bailout (try top -b -n1).
top: make code a bit more readable.
| -rw-r--r-- | procps/top.c | 59 |
1 files changed, 22 insertions, 37 deletions
diff --git a/procps/top.c b/procps/top.c index abc7a4363..1b4736454 100644 --- a/procps/top.c +++ b/procps/top.c | |||
| @@ -67,7 +67,7 @@ struct globals { | |||
| 67 | struct termios initial_settings; | 67 | struct termios initial_settings; |
| 68 | #endif | 68 | #endif |
| 69 | #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | 69 | #if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 70 | cmp_funcp sort_function; | 70 | cmp_funcp sort_function[1]; |
| 71 | #else | 71 | #else |
| 72 | cmp_funcp sort_function[SORT_DEPTH]; | 72 | cmp_funcp sort_function[SORT_DEPTH]; |
| 73 | struct save_hist *prev_hist; | 73 | struct save_hist *prev_hist; |
| @@ -81,17 +81,14 @@ struct globals { | |||
| 81 | #define G (*(struct globals*)&bb_common_bufsiz1) | 81 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 82 | #define top (G.top ) | 82 | #define top (G.top ) |
| 83 | #define ntop (G.ntop ) | 83 | #define ntop (G.ntop ) |
| 84 | #if ENABLE_FEATURE_USE_TERMIOS | ||
| 85 | #define initial_settings (G. initial_settings ) | 84 | #define initial_settings (G. initial_settings ) |
| 86 | #endif | ||
| 87 | #define sort_function (G.sort_function ) | 85 | #define sort_function (G.sort_function ) |
| 88 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | ||
| 89 | #define prev_hist (G.prev_hist ) | 86 | #define prev_hist (G.prev_hist ) |
| 90 | #define prev_hist_count (G.prev_hist_count ) | 87 | #define prev_hist_count (G.prev_hist_count ) |
| 91 | #define jif (G.jif ) | 88 | #define jif (G.jif ) |
| 92 | #define prev_jif (G.prev_jif ) | 89 | #define prev_jif (G.prev_jif ) |
| 93 | #define total_pcpu (G.total_pcpu ) | 90 | #define total_pcpu (G.total_pcpu ) |
| 94 | #endif | 91 | |
| 95 | 92 | ||
| 96 | #define OPT_BATCH_MODE (option_mask32 & 0x4) | 93 | #define OPT_BATCH_MODE (option_mask32 & 0x4) |
| 97 | 94 | ||
| @@ -356,8 +353,7 @@ static unsigned long display_generic(int scr_width) | |||
| 356 | return total; | 353 | return total; |
| 357 | } | 354 | } |
| 358 | 355 | ||
| 359 | /* display process statuses */ | 356 | static void display_process_list(int count, int scr_width) |
| 360 | static void display_status(int count, int scr_width) | ||
| 361 | { | 357 | { |
| 362 | enum { | 358 | enum { |
| 363 | BITS_PER_INT = sizeof(int)*8 | 359 | BITS_PER_INT = sizeof(int)*8 |
| @@ -482,7 +478,6 @@ static void display_status(int count, int scr_width) | |||
| 482 | #undef CALC_STAT | 478 | #undef CALC_STAT |
| 483 | #undef FMT | 479 | #undef FMT |
| 484 | 480 | ||
| 485 | |||
| 486 | static void clearmems(void) | 481 | static void clearmems(void) |
| 487 | { | 482 | { |
| 488 | clear_username_cache(); | 483 | clear_username_cache(); |
| @@ -491,7 +486,6 @@ static void clearmems(void) | |||
| 491 | ntop = 0; | 486 | ntop = 0; |
| 492 | } | 487 | } |
| 493 | 488 | ||
| 494 | |||
| 495 | #if ENABLE_FEATURE_USE_TERMIOS | 489 | #if ENABLE_FEATURE_USE_TERMIOS |
| 496 | #include <termios.h> | 490 | #include <termios.h> |
| 497 | #include <signal.h> | 491 | #include <signal.h> |
| @@ -499,12 +493,12 @@ static void clearmems(void) | |||
| 499 | static void reset_term(void) | 493 | static void reset_term(void) |
| 500 | { | 494 | { |
| 501 | tcsetattr(0, TCSANOW, (void *) &initial_settings); | 495 | tcsetattr(0, TCSANOW, (void *) &initial_settings); |
| 502 | #if ENABLE_FEATURE_CLEAN_UP | 496 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 503 | clearmems(); | 497 | clearmems(); |
| 504 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | 498 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| 505 | free(prev_hist); | 499 | free(prev_hist); |
| 506 | #endif | 500 | #endif |
| 507 | #endif /* FEATURE_CLEAN_UP */ | 501 | } |
| 508 | } | 502 | } |
| 509 | 503 | ||
| 510 | static void sig_catcher(int sig ATTRIBUTE_UNUSED) | 504 | static void sig_catcher(int sig ATTRIBUTE_UNUSED) |
| @@ -548,7 +542,7 @@ int top_main(int argc, char **argv) | |||
| 548 | xchdir("/proc"); | 542 | xchdir("/proc"); |
| 549 | #if ENABLE_FEATURE_USE_TERMIOS | 543 | #if ENABLE_FEATURE_USE_TERMIOS |
| 550 | tcgetattr(0, (void *) &initial_settings); | 544 | tcgetattr(0, (void *) &initial_settings); |
| 551 | memcpy(&new_settings, &initial_settings, sizeof(struct termios)); | 545 | memcpy(&new_settings, &initial_settings, sizeof(new_settings)); |
| 552 | /* unbuffered input, turn off echo */ | 546 | /* unbuffered input, turn off echo */ |
| 553 | new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); | 547 | new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL); |
| 554 | 548 | ||
| @@ -563,17 +557,18 @@ int top_main(int argc, char **argv) | |||
| 563 | sort_function[1] = mem_sort; | 557 | sort_function[1] = mem_sort; |
| 564 | sort_function[2] = time_sort; | 558 | sort_function[2] = time_sort; |
| 565 | #else | 559 | #else |
| 566 | sort_function = mem_sort; | 560 | sort_function[0] = mem_sort; |
| 567 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ | 561 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
| 568 | 562 | ||
| 569 | while (1) { | 563 | while (1) { |
| 570 | procps_status_t *p = NULL; | 564 | procps_status_t *p = NULL; |
| 571 | 565 | ||
| 572 | /* Default to 25 lines - 5 lines for status */ | 566 | /* Default */ |
| 573 | lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1); | 567 | lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1); |
| 574 | col = 79; | 568 | col = 79; |
| 575 | #if ENABLE_FEATURE_USE_TERMIOS | 569 | #if ENABLE_FEATURE_USE_TERMIOS |
| 576 | get_terminal_width_height(0, &col, &lines); | 570 | get_terminal_width_height(0, &col, &lines); |
| 571 | /* We wrap horribly if width is too narrow (TODO) */ | ||
| 577 | if (lines < 5 || col < MIN_WIDTH) { | 572 | if (lines < 5 || col < MIN_WIDTH) { |
| 578 | sleep(interval); | 573 | sleep(interval); |
| 579 | continue; | 574 | continue; |
| @@ -618,27 +613,31 @@ int top_main(int argc, char **argv) | |||
| 618 | /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */ | 613 | /* TODO: we don't need to sort all 10000 processes, we need to find top 24! */ |
| 619 | qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp); | 614 | qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp); |
| 620 | #else | 615 | #else |
| 621 | qsort(top, ntop, sizeof(top_status_t), (void*)sort_function); | 616 | qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0])); |
| 622 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ | 617 | #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ |
| 623 | count = lines; | 618 | count = lines; |
| 624 | if (OPT_BATCH_MODE || count > ntop) { | 619 | if (OPT_BATCH_MODE || count > ntop) { |
| 625 | count = ntop; | 620 | count = ntop; |
| 626 | } | 621 | } |
| 627 | /* show status for each of the processes */ | 622 | display_process_list(count, col); |
| 628 | display_status(count, col); | 623 | clearmems(); |
| 629 | #if ENABLE_FEATURE_USE_TERMIOS | 624 | if (iterations >= 0 && !--iterations) |
| 625 | break; | ||
| 626 | #if !ENABLE_FEATURE_USE_TERMIOS | ||
| 627 | sleep(interval); | ||
| 628 | #else | ||
| 630 | if (poll(pfd, 1, interval * 1000) != 0) { | 629 | if (poll(pfd, 1, interval * 1000) != 0) { |
| 631 | if (read(0, &c, 1) != 1) /* signal */ | 630 | if (read(0, &c, 1) != 1) /* signal */ |
| 632 | break; | 631 | break; |
| 633 | if (c == 'q' || c == initial_settings.c_cc[VINTR]) | 632 | if (c == 'q' || c == initial_settings.c_cc[VINTR]) |
| 634 | break; | 633 | break; |
| 634 | if (c == 'N') | ||
| 635 | sort_function[0] = pid_sort; | ||
| 635 | if (c == 'M') { | 636 | if (c == 'M') { |
| 636 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | ||
| 637 | sort_function[0] = mem_sort; | 637 | sort_function[0] = mem_sort; |
| 638 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | ||
| 638 | sort_function[1] = pcpu_sort; | 639 | sort_function[1] = pcpu_sort; |
| 639 | sort_function[2] = time_sort; | 640 | sort_function[2] = time_sort; |
| 640 | #else | ||
| 641 | sort_function = mem_sort; | ||
| 642 | #endif | 641 | #endif |
| 643 | } | 642 | } |
| 644 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | 643 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
| @@ -653,23 +652,9 @@ int top_main(int argc, char **argv) | |||
| 653 | sort_function[2] = pcpu_sort; | 652 | sort_function[2] = pcpu_sort; |
| 654 | } | 653 | } |
| 655 | #endif | 654 | #endif |
| 656 | if (c == 'N') { | ||
| 657 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | ||
| 658 | sort_function[0] = pid_sort; | ||
| 659 | #else | ||
| 660 | sort_function = pid_sort; | ||
| 661 | #endif | ||
| 662 | } | ||
| 663 | } | 655 | } |
| 664 | if (iterations >= 0 && !--iterations) | ||
| 665 | break; | ||
| 666 | #else | ||
| 667 | sleep(interval); | ||
| 668 | #endif /* FEATURE_USE_TERMIOS */ | 656 | #endif /* FEATURE_USE_TERMIOS */ |
| 669 | clearmems(); | ||
| 670 | } | 657 | } |
| 671 | if (ENABLE_FEATURE_CLEAN_UP) | ||
| 672 | clearmems(); | ||
| 673 | putchar('\n'); | 658 | putchar('\n'); |
| 674 | return EXIT_SUCCESS; | 659 | return EXIT_SUCCESS; |
| 675 | } | 660 | } |
