aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-29 17:16:39 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-29 17:16:39 +0000
commit266bc17690cd8e98bf30514a19f95891b6ccd1d3 (patch)
tree75afe0e05044880298a5ad38a04ad3213f0d2fd3
parentb1726785f947c35c11d2148196b8fa8b30445798 (diff)
downloadbusybox-w32-266bc17690cd8e98bf30514a19f95891b6ccd1d3.tar.gz
busybox-w32-266bc17690cd8e98bf30514a19f95891b6ccd1d3.tar.bz2
busybox-w32-266bc17690cd8e98bf30514a19f95891b6ccd1d3.zip
top: add support for -b, -n <iterations>
-rw-r--r--include/usage.h10
-rw-r--r--libbb/getopt_ulflags.c8
-rw-r--r--procps/ps.c4
-rw-r--r--procps/top.c47
4 files changed, 34 insertions, 35 deletions
diff --git a/include/usage.h b/include/usage.h
index 3c9744623..bb814f1f8 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2938,13 +2938,11 @@ USE_FEATURE_START_STOP_DAEMON_FANCY( \
2938 "\t-v\tDisplays verbose resource usage information" 2938 "\t-v\tDisplays verbose resource usage information"
2939 2939
2940#define top_trivial_usage \ 2940#define top_trivial_usage \
2941 "[-d <seconds>]" 2941 "[-b] [-n count] [-d seconds]"
2942#define top_full_usage \ 2942#define top_full_usage \
2943 "top provides an view of processor activity in real time.\n" \ 2943 "top provides a view of process activity in real time.\n" \
2944 "This utility reads the status for all processes in /proc each <seconds>\n" \ 2944 "It reads the status of all processes from /proc each <seconds>\n" \
2945 "and shows the status for however many processes will fit on the screen.\n" \ 2945 "and shows the status for however many processes will fit on the screen."
2946 "This utility will not show processes that are started after program startup,\n" \
2947 "but it will show the EXIT status for and PIDs that exit while it is running."
2948 2946
2949#define touch_trivial_usage \ 2947#define touch_trivial_usage \
2950 "[-c] FILE [FILE ...]" 2948 "[-c] FILE [FILE ...]"
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c
index 988f3f9f5..9d27c1f11 100644
--- a/libbb/getopt_ulflags.c
+++ b/libbb/getopt_ulflags.c
@@ -319,9 +319,7 @@ bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
319 const struct option *l_o; 319 const struct option *l_o;
320#endif 320#endif
321 unsigned long trigger; 321 unsigned long trigger;
322#ifdef CONFIG_PS
323 char **pargv = NULL; 322 char **pargv = NULL;
324#endif
325 int min_arg = 0; 323 int min_arg = 0;
326 int max_arg = -1; 324 int max_arg = -1;
327 325
@@ -461,9 +459,7 @@ bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
461#else 459#else
462 while ((c = getopt(argc, argv, applet_opts)) >= 0) { 460 while ((c = getopt(argc, argv, applet_opts)) >= 0) {
463#endif /* ENABLE_GETOPT_LONG */ 461#endif /* ENABLE_GETOPT_LONG */
464#if ENABLE_PS
465loop_arg_is_opt: 462loop_arg_is_opt:
466#endif
467 for (on_off = complementally; on_off->opt != c; on_off++) { 463 for (on_off = complementally; on_off->opt != c; on_off++) {
468 /* c==0 if long opt have non NULL flag */ 464 /* c==0 if long opt have non NULL flag */
469 if (on_off->opt == 0 && c != 0) 465 if (on_off->opt == 0 && c != 0)
@@ -485,13 +481,10 @@ loop_arg_is_opt:
485 } else if (on_off->optarg) { 481 } else if (on_off->optarg) {
486 *(char **)(on_off->optarg) = optarg; 482 *(char **)(on_off->optarg) = optarg;
487 } 483 }
488#if ENABLE_PS
489 if (pargv != NULL) 484 if (pargv != NULL)
490 break; 485 break;
491#endif
492 } 486 }
493 487
494#if ENABLE_PS
495 if (spec_flgs & ALL_ARGV_IS_OPTS) { 488 if (spec_flgs & ALL_ARGV_IS_OPTS) {
496 /* process argv is option, for example "ps" applet */ 489 /* process argv is option, for example "ps" applet */
497 if (pargv == NULL) 490 if (pargv == NULL)
@@ -506,7 +499,6 @@ loop_arg_is_opt:
506 } 499 }
507 } 500 }
508 } 501 }
509#endif
510 502
511#if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP 503#if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP
512 if (spec_flgs & FREE_FIRST_ARGV_IS_OPT) 504 if (spec_flgs & FREE_FIRST_ARGV_IS_OPT)
diff --git a/procps/ps.c b/procps/ps.c
index d88fe4af1..95502b284 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -16,14 +16,14 @@ int ps_main(int argc, char **argv)
16 16
17#if ENABLE_SELINUX 17#if ENABLE_SELINUX
18 int use_selinux = 0; 18 int use_selinux = 0;
19 security_context_t sid=NULL; 19 security_context_t sid = NULL;
20#endif 20#endif
21 21
22#if ENABLE_FEATURE_PS_WIDE 22#if ENABLE_FEATURE_PS_WIDE
23 int terminal_width; 23 int terminal_width;
24 int w_count = 0; 24 int w_count = 0;
25 25
26 bb_opt_complementally="-:ww"; 26 bb_opt_complementally = "-:ww";
27#else 27#else
28# define terminal_width 79 28# define terminal_width 79
29#endif 29#endif
diff --git a/procps/top.c b/procps/top.c
index 7043d3e6a..be8ac195b 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -30,12 +30,12 @@
30 30
31#include "busybox.h" 31#include "busybox.h"
32 32
33//#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE /* + 2k */
34
35typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q); 33typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
36 34
37static procps_status_t *top; /* Hehe */ 35static procps_status_t *top; /* Hehe */
38static int ntop; 36static int ntop;
37static unsigned option_mask;
38#define OPT_BATCH_MODE (option_mask & 0x4)
39 39
40#ifdef CONFIG_FEATURE_USE_TERMIOS 40#ifdef CONFIG_FEATURE_USE_TERMIOS
41static int pid_sort(procps_status_t *P, procps_status_t *Q) 41static int pid_sort(procps_status_t *P, procps_status_t *Q)
@@ -251,9 +251,11 @@ static unsigned long display_generic(int scr_width)
251 snprintf(scrbuf, scr_width, 251 snprintf(scrbuf, scr_width,
252 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached", 252 "Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
253 used, mfree, shared, buffers, cached); 253 used, mfree, shared, buffers, cached);
254 printf("\e[H\e[J%s\n", scrbuf); 254
255 printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
256
255 snprintf(scrbuf, scr_width, 257 snprintf(scrbuf, scr_width,
256 "Load average: %s (Status: S=sleeping R=running, W=waiting)", buf); 258 "Load average: %s", buf);
257 printf("%s\n", scrbuf); 259 printf("%s\n", scrbuf);
258 260
259 return total; 261 return total;
@@ -276,12 +278,12 @@ static void display_status(int count, int scr_width)
276 unsigned pcpu_shift, pcpu_scale; 278 unsigned pcpu_shift, pcpu_scale;
277 279
278 /* what info of the processes is shown */ 280 /* what info of the processes is shown */
279 printf("\e[7m%.*s\e[0m", scr_width, 281 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
280 " PID USER STATUS RSS PPID %CPU %MEM COMMAND"); 282 " PID USER STATUS RSS PPID %CPU %MEM COMMAND");
281#define MIN_WIDTH \ 283#define MIN_WIDTH \
282 sizeof( " PID USER STATUS RSS PPID %CPU %MEM C") 284 sizeof( " PID USER STATUS RSS PPID %CPU %MEM C")
283#else 285#else
284 printf("\e[7m%.*s\e[0m", scr_width, 286 printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
285 " PID USER STATUS RSS PPID %MEM COMMAND"); 287 " PID USER STATUS RSS PPID %MEM COMMAND");
286#define MIN_WIDTH \ 288#define MIN_WIDTH \
287 sizeof( " PID USER STATUS RSS PPID %MEM C") 289 sizeof( " PID USER STATUS RSS PPID %MEM C")
@@ -320,9 +322,9 @@ static void display_status(int count, int scr_width)
320 } 322 }
321 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */ 323 /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
322#endif 324#endif
323 325 if (OPT_BATCH_MODE) count--;
324 while (count--) { 326 while (count-- > 0) {
325 div_t pmem = div( (s->rss*pmem_scale) >> pmem_shift, 10); 327 div_t pmem = div((s->rss*pmem_scale) >> pmem_shift, 10);
326 int col = scr_width+1; 328 int col = scr_width+1;
327 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;) 329 USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
328 330
@@ -343,7 +345,7 @@ static void display_status(int count, int scr_width)
343 s++; 345 s++;
344 } 346 }
345 /* printf(" %d", hist_iterations); */ 347 /* printf(" %d", hist_iterations); */
346 putchar('\r'); 348 putchar(OPT_BATCH_MODE ? '\n' : '\r');
347 fflush(stdout); 349 fflush(stdout);
348} 350}
349 351
@@ -382,8 +384,10 @@ static void sig_catcher(int sig ATTRIBUTE_UNUSED)
382 384
383int top_main(int argc, char **argv) 385int top_main(int argc, char **argv)
384{ 386{
385 int opt, interval, lines, col; 387 int count, lines, col;
386 char *sinterval; 388 int interval = 5; /* default update rate is 5 seconds */
389 int iterations = -1; /* 2^32 iterations by default :) */
390 char *sinterval, *siterations;
387#ifdef CONFIG_FEATURE_USE_TERMIOS 391#ifdef CONFIG_FEATURE_USE_TERMIOS
388 struct termios new_settings; 392 struct termios new_settings;
389 struct timeval tv; 393 struct timeval tv;
@@ -393,9 +397,12 @@ int top_main(int argc, char **argv)
393 397
394 /* do normal option parsing */ 398 /* do normal option parsing */
395 interval = 5; 399 interval = 5;
396 opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval); 400 bb_opt_complementally = "-";
397 if (opt & 1) 401 option_mask = bb_getopt_ulflags(argc, argv, "d:n:b",
398 interval = atoi(sinterval); 402 &sinterval, &siterations);
403 if (option_mask & 0x1) interval = atoi(sinterval); // -d
404 if (option_mask & 0x2) iterations = atoi(siterations); // -n
405 //if (option_mask & 0x4) // -b
399 406
400 /* change to /proc */ 407 /* change to /proc */
401 xchdir("/proc"); 408 xchdir("/proc");
@@ -456,12 +463,12 @@ int top_main(int argc, char **argv)
456#else 463#else
457 qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); 464 qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
458#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ 465#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
459 opt = lines; 466 count = lines;
460 if (opt > ntop) { 467 if (count > ntop) {
461 opt = ntop; 468 count = ntop;
462 } 469 }
463 /* show status for each of the processes */ 470 /* show status for each of the processes */
464 display_status(opt, col); 471 display_status(count, col);
465#ifdef CONFIG_FEATURE_USE_TERMIOS 472#ifdef CONFIG_FEATURE_USE_TERMIOS
466 tv.tv_sec = interval; 473 tv.tv_sec = interval;
467 tv.tv_usec = 0; 474 tv.tv_usec = 0;
@@ -503,6 +510,8 @@ int top_main(int argc, char **argv)
503#endif 510#endif
504 } 511 }
505 } 512 }
513 if (!--iterations)
514 break;
506#else 515#else
507 sleep(interval); 516 sleep(interval);
508#endif /* CONFIG_FEATURE_USE_TERMIOS */ 517#endif /* CONFIG_FEATURE_USE_TERMIOS */