top: add support for -b, -n <iterations>

This commit is contained in:
Denis Vlasenko 2006-09-29 17:16:39 +00:00
parent b1726785f9
commit 266bc17690
4 changed files with 34 additions and 35 deletions

View File

@ -2938,13 +2938,11 @@ USE_FEATURE_START_STOP_DAEMON_FANCY( \
"\t-v\tDisplays verbose resource usage information"
#define top_trivial_usage \
"[-d <seconds>]"
"[-b] [-n count] [-d seconds]"
#define top_full_usage \
"top provides an view of processor activity in real time.\n" \
"This utility reads the status for all processes in /proc each <seconds>\n" \
"and shows the status for however many processes will fit on the screen.\n" \
"This utility will not show processes that are started after program startup,\n" \
"but it will show the EXIT status for and PIDs that exit while it is running."
"top provides a view of process activity in real time.\n" \
"It reads the status of all processes from /proc each <seconds>\n" \
"and shows the status for however many processes will fit on the screen."
#define touch_trivial_usage \
"[-c] FILE [FILE ...]"

View File

@ -319,9 +319,7 @@ bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
const struct option *l_o;
#endif
unsigned long trigger;
#ifdef CONFIG_PS
char **pargv = NULL;
#endif
int min_arg = 0;
int max_arg = -1;
@ -461,9 +459,7 @@ bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
#else
while ((c = getopt(argc, argv, applet_opts)) >= 0) {
#endif /* ENABLE_GETOPT_LONG */
#if ENABLE_PS
loop_arg_is_opt:
#endif
for (on_off = complementally; on_off->opt != c; on_off++) {
/* c==0 if long opt have non NULL flag */
if (on_off->opt == 0 && c != 0)
@ -485,13 +481,10 @@ loop_arg_is_opt:
} else if (on_off->optarg) {
*(char **)(on_off->optarg) = optarg;
}
#if ENABLE_PS
if (pargv != NULL)
break;
#endif
}
#if ENABLE_PS
if (spec_flgs & ALL_ARGV_IS_OPTS) {
/* process argv is option, for example "ps" applet */
if (pargv == NULL)
@ -506,7 +499,6 @@ loop_arg_is_opt:
}
}
}
#endif
#if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP
if (spec_flgs & FREE_FIRST_ARGV_IS_OPT)

View File

@ -16,14 +16,14 @@ int ps_main(int argc, char **argv)
#if ENABLE_SELINUX
int use_selinux = 0;
security_context_t sid=NULL;
security_context_t sid = NULL;
#endif
#if ENABLE_FEATURE_PS_WIDE
int terminal_width;
int w_count = 0;
bb_opt_complementally="-:ww";
bb_opt_complementally = "-:ww";
#else
# define terminal_width 79
#endif

View File

@ -30,12 +30,12 @@
#include "busybox.h"
//#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE /* + 2k */
typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
static procps_status_t *top; /* Hehe */
static int ntop;
static unsigned option_mask;
#define OPT_BATCH_MODE (option_mask & 0x4)
#ifdef CONFIG_FEATURE_USE_TERMIOS
static int pid_sort(procps_status_t *P, procps_status_t *Q)
@ -251,9 +251,11 @@ static unsigned long display_generic(int scr_width)
snprintf(scrbuf, scr_width,
"Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
used, mfree, shared, buffers, cached);
printf("\e[H\e[J%s\n", scrbuf);
printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
snprintf(scrbuf, scr_width,
"Load average: %s (Status: S=sleeping R=running, W=waiting)", buf);
"Load average: %s", buf);
printf("%s\n", scrbuf);
return total;
@ -276,12 +278,12 @@ static void display_status(int count, int scr_width)
unsigned pcpu_shift, pcpu_scale;
/* what info of the processes is shown */
printf("\e[7m%.*s\e[0m", scr_width,
printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
" PID USER STATUS RSS PPID %CPU %MEM COMMAND");
#define MIN_WIDTH \
sizeof( " PID USER STATUS RSS PPID %CPU %MEM C")
#else
printf("\e[7m%.*s\e[0m", scr_width,
printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
" PID USER STATUS RSS PPID %MEM COMMAND");
#define MIN_WIDTH \
sizeof( " PID USER STATUS RSS PPID %MEM C")
@ -320,9 +322,9 @@ static void display_status(int count, int scr_width)
}
/* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
#endif
while (count--) {
div_t pmem = div( (s->rss*pmem_scale) >> pmem_shift, 10);
if (OPT_BATCH_MODE) count--;
while (count-- > 0) {
div_t pmem = div((s->rss*pmem_scale) >> pmem_shift, 10);
int col = scr_width+1;
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
@ -343,7 +345,7 @@ static void display_status(int count, int scr_width)
s++;
}
/* printf(" %d", hist_iterations); */
putchar('\r');
putchar(OPT_BATCH_MODE ? '\n' : '\r');
fflush(stdout);
}
@ -382,8 +384,10 @@ static void sig_catcher(int sig ATTRIBUTE_UNUSED)
int top_main(int argc, char **argv)
{
int opt, interval, lines, col;
char *sinterval;
int count, lines, col;
int interval = 5; /* default update rate is 5 seconds */
int iterations = -1; /* 2^32 iterations by default :) */
char *sinterval, *siterations;
#ifdef CONFIG_FEATURE_USE_TERMIOS
struct termios new_settings;
struct timeval tv;
@ -393,9 +397,12 @@ int top_main(int argc, char **argv)
/* do normal option parsing */
interval = 5;
opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval);
if (opt & 1)
interval = atoi(sinterval);
bb_opt_complementally = "-";
option_mask = bb_getopt_ulflags(argc, argv, "d:n:b",
&sinterval, &siterations);
if (option_mask & 0x1) interval = atoi(sinterval); // -d
if (option_mask & 0x2) iterations = atoi(siterations); // -n
//if (option_mask & 0x4) // -b
/* change to /proc */
xchdir("/proc");
@ -456,12 +463,12 @@ int top_main(int argc, char **argv)
#else
qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
opt = lines;
if (opt > ntop) {
opt = ntop;
count = lines;
if (count > ntop) {
count = ntop;
}
/* show status for each of the processes */
display_status(opt, col);
display_status(count, col);
#ifdef CONFIG_FEATURE_USE_TERMIOS
tv.tv_sec = interval;
tv.tv_usec = 0;
@ -503,6 +510,8 @@ int top_main(int argc, char **argv)
#endif
}
}
if (!--iterations)
break;
#else
sleep(interval);
#endif /* CONFIG_FEATURE_USE_TERMIOS */