aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
Diffstat (limited to 'procps')
-rw-r--r--procps/ps.c4
-rw-r--r--procps/top.c47
2 files changed, 30 insertions, 21 deletions
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 */