top: don't wait before final bailout (try top -b -n1).
top: make code a bit more readable.
This commit is contained in:
parent
fb48f6c982
commit
8bdba4d011
55
procps/top.c
55
procps/top.c
@ -67,7 +67,7 @@ struct globals {
|
|||||||
struct termios initial_settings;
|
struct termios initial_settings;
|
||||||
#endif
|
#endif
|
||||||
#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
||||||
cmp_funcp sort_function;
|
cmp_funcp sort_function[1];
|
||||||
#else
|
#else
|
||||||
cmp_funcp sort_function[SORT_DEPTH];
|
cmp_funcp sort_function[SORT_DEPTH];
|
||||||
struct save_hist *prev_hist;
|
struct save_hist *prev_hist;
|
||||||
@ -81,17 +81,14 @@ struct globals {
|
|||||||
#define G (*(struct globals*)&bb_common_bufsiz1)
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
||||||
#define top (G.top )
|
#define top (G.top )
|
||||||
#define ntop (G.ntop )
|
#define ntop (G.ntop )
|
||||||
#if ENABLE_FEATURE_USE_TERMIOS
|
|
||||||
#define initial_settings (G. initial_settings )
|
#define initial_settings (G. initial_settings )
|
||||||
#endif
|
|
||||||
#define sort_function (G.sort_function )
|
#define sort_function (G.sort_function )
|
||||||
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
||||||
#define prev_hist (G.prev_hist )
|
#define prev_hist (G.prev_hist )
|
||||||
#define prev_hist_count (G.prev_hist_count )
|
#define prev_hist_count (G.prev_hist_count )
|
||||||
#define jif (G.jif )
|
#define jif (G.jif )
|
||||||
#define prev_jif (G.prev_jif )
|
#define prev_jif (G.prev_jif )
|
||||||
#define total_pcpu (G.total_pcpu )
|
#define total_pcpu (G.total_pcpu )
|
||||||
#endif
|
|
||||||
|
|
||||||
#define OPT_BATCH_MODE (option_mask32 & 0x4)
|
#define OPT_BATCH_MODE (option_mask32 & 0x4)
|
||||||
|
|
||||||
@ -356,8 +353,7 @@ static unsigned long display_generic(int scr_width)
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* display process statuses */
|
static void display_process_list(int count, int scr_width)
|
||||||
static void display_status(int count, int scr_width)
|
|
||||||
{
|
{
|
||||||
enum {
|
enum {
|
||||||
BITS_PER_INT = sizeof(int)*8
|
BITS_PER_INT = sizeof(int)*8
|
||||||
@ -482,7 +478,6 @@ static void display_status(int count, int scr_width)
|
|||||||
#undef CALC_STAT
|
#undef CALC_STAT
|
||||||
#undef FMT
|
#undef FMT
|
||||||
|
|
||||||
|
|
||||||
static void clearmems(void)
|
static void clearmems(void)
|
||||||
{
|
{
|
||||||
clear_username_cache();
|
clear_username_cache();
|
||||||
@ -491,7 +486,6 @@ static void clearmems(void)
|
|||||||
ntop = 0;
|
ntop = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if ENABLE_FEATURE_USE_TERMIOS
|
#if ENABLE_FEATURE_USE_TERMIOS
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
@ -499,12 +493,12 @@ static void clearmems(void)
|
|||||||
static void reset_term(void)
|
static void reset_term(void)
|
||||||
{
|
{
|
||||||
tcsetattr(0, TCSANOW, (void *) &initial_settings);
|
tcsetattr(0, TCSANOW, (void *) &initial_settings);
|
||||||
#if ENABLE_FEATURE_CLEAN_UP
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||||
clearmems();
|
clearmems();
|
||||||
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
||||||
free(prev_hist);
|
free(prev_hist);
|
||||||
#endif
|
#endif
|
||||||
#endif /* FEATURE_CLEAN_UP */
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sig_catcher(int sig ATTRIBUTE_UNUSED)
|
static void sig_catcher(int sig ATTRIBUTE_UNUSED)
|
||||||
@ -548,7 +542,7 @@ int top_main(int argc, char **argv)
|
|||||||
xchdir("/proc");
|
xchdir("/proc");
|
||||||
#if ENABLE_FEATURE_USE_TERMIOS
|
#if ENABLE_FEATURE_USE_TERMIOS
|
||||||
tcgetattr(0, (void *) &initial_settings);
|
tcgetattr(0, (void *) &initial_settings);
|
||||||
memcpy(&new_settings, &initial_settings, sizeof(struct termios));
|
memcpy(&new_settings, &initial_settings, sizeof(new_settings));
|
||||||
/* unbuffered input, turn off echo */
|
/* unbuffered input, turn off echo */
|
||||||
new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
|
new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
|
||||||
|
|
||||||
@ -563,17 +557,18 @@ int top_main(int argc, char **argv)
|
|||||||
sort_function[1] = mem_sort;
|
sort_function[1] = mem_sort;
|
||||||
sort_function[2] = time_sort;
|
sort_function[2] = time_sort;
|
||||||
#else
|
#else
|
||||||
sort_function = mem_sort;
|
sort_function[0] = mem_sort;
|
||||||
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
procps_status_t *p = NULL;
|
procps_status_t *p = NULL;
|
||||||
|
|
||||||
/* Default to 25 lines - 5 lines for status */
|
/* Default */
|
||||||
lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1);
|
lines = 24 - 3 USE_FEATURE_TOP_CPU_GLOBAL_PERCENTS( - 1);
|
||||||
col = 79;
|
col = 79;
|
||||||
#if ENABLE_FEATURE_USE_TERMIOS
|
#if ENABLE_FEATURE_USE_TERMIOS
|
||||||
get_terminal_width_height(0, &col, &lines);
|
get_terminal_width_height(0, &col, &lines);
|
||||||
|
/* We wrap horribly if width is too narrow (TODO) */
|
||||||
if (lines < 5 || col < MIN_WIDTH) {
|
if (lines < 5 || col < MIN_WIDTH) {
|
||||||
sleep(interval);
|
sleep(interval);
|
||||||
continue;
|
continue;
|
||||||
@ -618,27 +613,31 @@ int top_main(int argc, char **argv)
|
|||||||
/* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
|
/* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
|
||||||
qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
|
qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
|
||||||
#else
|
#else
|
||||||
qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);
|
qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
|
||||||
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
||||||
count = lines;
|
count = lines;
|
||||||
if (OPT_BATCH_MODE || count > ntop) {
|
if (OPT_BATCH_MODE || count > ntop) {
|
||||||
count = ntop;
|
count = ntop;
|
||||||
}
|
}
|
||||||
/* show status for each of the processes */
|
display_process_list(count, col);
|
||||||
display_status(count, col);
|
clearmems();
|
||||||
#if ENABLE_FEATURE_USE_TERMIOS
|
if (iterations >= 0 && !--iterations)
|
||||||
|
break;
|
||||||
|
#if !ENABLE_FEATURE_USE_TERMIOS
|
||||||
|
sleep(interval);
|
||||||
|
#else
|
||||||
if (poll(pfd, 1, interval * 1000) != 0) {
|
if (poll(pfd, 1, interval * 1000) != 0) {
|
||||||
if (read(0, &c, 1) != 1) /* signal */
|
if (read(0, &c, 1) != 1) /* signal */
|
||||||
break;
|
break;
|
||||||
if (c == 'q' || c == initial_settings.c_cc[VINTR])
|
if (c == 'q' || c == initial_settings.c_cc[VINTR])
|
||||||
break;
|
break;
|
||||||
|
if (c == 'N')
|
||||||
|
sort_function[0] = pid_sort;
|
||||||
if (c == 'M') {
|
if (c == 'M') {
|
||||||
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
||||||
sort_function[0] = mem_sort;
|
sort_function[0] = mem_sort;
|
||||||
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
||||||
sort_function[1] = pcpu_sort;
|
sort_function[1] = pcpu_sort;
|
||||||
sort_function[2] = time_sort;
|
sort_function[2] = time_sort;
|
||||||
#else
|
|
||||||
sort_function = mem_sort;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
||||||
@ -652,24 +651,10 @@ int top_main(int argc, char **argv)
|
|||||||
sort_function[1] = mem_sort;
|
sort_function[1] = mem_sort;
|
||||||
sort_function[2] = pcpu_sort;
|
sort_function[2] = pcpu_sort;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
if (c == 'N') {
|
|
||||||
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
||||||
sort_function[0] = pid_sort;
|
|
||||||
#else
|
|
||||||
sort_function = pid_sort;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (iterations >= 0 && !--iterations)
|
|
||||||
break;
|
|
||||||
#else
|
|
||||||
sleep(interval);
|
|
||||||
#endif /* FEATURE_USE_TERMIOS */
|
#endif /* FEATURE_USE_TERMIOS */
|
||||||
clearmems();
|
|
||||||
}
|
}
|
||||||
if (ENABLE_FEATURE_CLEAN_UP)
|
|
||||||
clearmems();
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user