char *arg;
pid_t pid;
int signo = SIGTERM, errors = 0, quiet = 0;
- const int killall = (ENABLE_KILLALL && applet_name[4]=='a'
- && (!ENABLE_KILLALL5 || applet_name[7]!='5'));
- const int killall5 = (ENABLE_KILLALL5 && applet_name[4]=='a'
- && (!ENABLE_KILLALL || applet_name[7]=='5'));
+ const int killall = (ENABLE_KILLALL && applet_name[4] == 'a'
+ && (!ENABLE_KILLALL5 || applet_name[7] != '5'));
+ const int killall5 = (ENABLE_KILLALL5 && applet_name[4] == 'a'
+ && (!ENABLE_KILLALL || applet_name[7] == '5'));
/* Parse any options */
argc--;
arg = *++argv;
- if (argc<1 || arg[0]!='-') {
+ if (argc < 1 || arg[0] != '-') {
goto do_it_now;
}
/* The -l option, which prints out signal names. */
- if (arg[1]=='l' && arg[2]=='\0') {
+ if (arg[1] == 'l' && arg[2] == '\0') {
const char *name;
- if (argc==1) {
+ if (argc == 1) {
/* Print the whole signal list */
int col = 0;
- for (signo = 1; signo<32; signo++) {
+ for (signo = 1; signo < 32; signo++) {
name = get_signame(signo);
if (isdigit(name[0])) continue;
if (col > 66) {
}
puts("");
} else { /* -l <sig list> */
- while ((arg = *++argv)!=NULL) {
+ while ((arg = *++argv)) {
if (isdigit(arg[0])) {
signo = xatoi_u(arg);
name = get_signame(signo);
} else {
signo = get_signum(arg);
- if (signo<0)
+ if (signo < 0)
bb_error_msg_and_die("unknown signal '%s'", arg);
name = get_signame(signo);
}
}
/* The -q quiet option */
- if (killall && arg[1]=='q' && arg[2]=='\0') {
+ if (killall && arg[1] == 'q' && arg[2] == '\0') {
quiet = 1;
arg = *++argv;
argc--;
- if (argc<1) bb_show_usage();
- if (arg[0]!='-') goto do_it_now;
+ if (argc < 1) bb_show_usage();
+ if (arg[0] != '-') goto do_it_now;
}
/* -SIG */
signo = get_signum(&arg[1]);
- if (signo<0)
+ if (signo < 0)
bb_error_msg_and_die("bad signal name '%s'", &arg[1]);
arg = *++argv;
argc--;
static int ntop;
#define OPT_BATCH_MODE (option_mask32 & 0x4)
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
static int pid_sort(procps_status_t *P, procps_status_t *Q)
{
return (Q->pid - P->pid);
return (int)(Q->rss - P->rss);
}
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-#define sort_depth 3
-static cmp_t sort_function[sort_depth];
+enum { SORT_DEPTH = 3 };
+static cmp_t sort_function[SORT_DEPTH];
static int pcpu_sort(procps_status_t *P, procps_status_t *Q)
{
static int mult_lvl_cmp(void* a, void* b) {
int i, cmp_val;
- for (i = 0; i < sort_depth; i++) {
+ for (i = 0; i < SORT_DEPTH; i++) {
cmp_val = (*sort_function[i])(a, b);
if (cmp_val != 0)
return cmp_val;
static unsigned total_pcpu;
/* static unsigned long total_rss; */
-struct jiffy_counts {
+typedef struct {
unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
unsigned long long total;
unsigned long long busy;
-};
-static struct jiffy_counts jif, prev_jif;
-
+} jiffy_counts_t;
+static jiffy_counts_t jif, prev_jif;
static void get_jiffy_counts(void)
{
FILE* fp = xfopen("stat", "r");
}
#else
static cmp_t sort_function;
-#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
+#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
/* display generic info (meminfo / loadavg) */
static unsigned long display_generic(int scr_width)
unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
unsigned pmem_shift, pmem_scale;
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
unsigned pcpu_shift, pcpu_scale;
+ unsigned busy_jifs;
/* what info of the processes is shown */
printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
pmem_scale /= 4;
pmem_shift -= 2;
}
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+ busy_jifs = jif.busy - prev_jif.busy;
+ /* This happens if there were lots of short-lived processes
+ * between two top updates (e.g. compilation) */
+ if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
+
/*
* CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
* (pcpu is delta of sys+user time between samples)
* we assume that unsigned is at least 32-bit.
*/
pcpu_shift = 6;
- pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1);
+ pcpu_scale = (1000*64*(uint16_t)busy_jifs ? : 1);
while (pcpu_scale < (1U<<(bits_per_int-2))) {
pcpu_scale *= 4;
pcpu_shift += 2;
ntop = 0;
}
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
#include <termios.h>
#include <signal.h>
static void reset_term(void)
{
tcsetattr(0, TCSANOW, (void *) &initial_settings);
-#ifdef CONFIG_FEATURE_CLEAN_UP
+#if ENABLE_FEATURE_CLEAN_UP
clearmems();
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
free(prev_hist);
#endif
-#endif /* CONFIG_FEATURE_CLEAN_UP */
+#endif /* FEATURE_CLEAN_UP */
}
static void sig_catcher(int sig ATTRIBUTE_UNUSED)
reset_term();
exit(1);
}
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
int top_main(int argc, char **argv)
unsigned interval = 5; /* default update rate is 5 seconds */
unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
char *sinterval, *siterations;
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
struct termios new_settings;
struct timeval tv;
fd_set readfds;
unsigned char c;
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
/* do normal option parsing */
interval = 5;
opt_complementary = "-";
- getopt32(argc, argv, "d:n:b",
- &sinterval, &siterations);
+ getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
//if (option_mask32 & 0x4) // -b
/* change to /proc */
xchdir("/proc");
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
tcgetattr(0, (void *) &initial_settings);
memcpy(&new_settings, &initial_settings, sizeof(struct termios));
/* unbuffered input, turn off echo */
signal(SIGINT, sig_catcher);
tcsetattr(0, TCSANOW, (void *) &new_settings);
atexit(reset_term);
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
sort_function[0] = pcpu_sort;
sort_function[1] = mem_sort;
sort_function[2] = time_sort;
#else
sort_function = mem_sort;
-#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
+#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
while (1) {
procps_status_t *p;
/* Default to 25 lines - 5 lines for status */
lines = 24 - 3;
col = 79;
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
get_terminal_width_height(0, &col, &lines);
if (lines < 5 || col < MIN_WIDTH) {
sleep(interval);
continue;
}
lines -= 3;
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
/* read process IDs & status for all the processes */
while ((p = procps_scan(0)) != 0) {
if (ntop == 0) {
bb_error_msg_and_die("can't find process info in /proc");
}
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
if (!prev_hist_count) {
do_stats();
sleep(1);
qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp);
#else
qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
-#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
+#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
count = lines;
if (OPT_BATCH_MODE || count > ntop) {
count = ntop;
}
/* show status for each of the processes */
display_status(count, col);
-#ifdef CONFIG_FEATURE_USE_TERMIOS
+#if ENABLE_FEATURE_USE_TERMIOS
tv.tv_sec = interval;
tv.tv_usec = 0;
FD_ZERO(&readfds);
if (c == 'q' || c == initial_settings.c_cc[VINTR])
break;
if (c == 'M') {
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
sort_function[0] = mem_sort;
sort_function[1] = pcpu_sort;
sort_function[2] = time_sort;
sort_function = mem_sort;
#endif
}
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
if (c == 'P') {
sort_function[0] = pcpu_sort;
sort_function[1] = mem_sort;
}
#endif
if (c == 'N') {
-#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
sort_function[0] = pid_sort;
#else
sort_function = pid_sort;
break;
#else
sleep(interval);
-#endif /* CONFIG_FEATURE_USE_TERMIOS */
+#endif /* FEATURE_USE_TERMIOS */
clearmems();
}
if (ENABLE_FEATURE_CLEAN_UP)