diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-30 14:47:41 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-30 14:47:41 +0000 |
commit | 98ebab8b768d4651d8db2f46a0cd6bd53012c8e6 (patch) | |
tree | 2f491e67f1f49a31b0cfeeaabb032b646b8d808c /procps | |
parent | 8b1409896d3224fec477bc372c4dceaebe6d6351 (diff) | |
download | busybox-w32-98ebab8b768d4651d8db2f46a0cd6bd53012c8e6.tar.gz busybox-w32-98ebab8b768d4651d8db2f46a0cd6bd53012c8e6.tar.bz2 busybox-w32-98ebab8b768d4651d8db2f46a0cd6bd53012c8e6.zip |
top,ps: improve /proc/PID/cmdinfo reading code
function old new delta
display_status - 1231 +1231
read_cmdline - 101 +101
parse_conf 1284 1303 +19
arith 2033 2042 +9
collect_blk 467 474 +7
fsck_main 1909 1911 +2
dhcprelay_main 1125 1122 -3
singlemount 4555 4547 -8
read_close 50 36 -14
get_lcm 123 105 -18
ed_main 3111 3084 -27
func_args 73 28 -45
procps_scan 732 658 -74
top_main 2187 899 -1288
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 4/8 up/down: 1369/-1477) Total: -108 bytes
text data bss dec hex filename
676048 2744 13968 692760 a9218 busybox_old
675940 2744 13968 692652 a91ac busybox_unstripped
Diffstat (limited to 'procps')
-rw-r--r-- | procps/ps.c | 63 | ||||
-rw-r--r-- | procps/top.c | 22 |
2 files changed, 34 insertions, 51 deletions
diff --git a/procps/ps.c b/procps/ps.c index 003e8eacd..c6bffc60d 100644 --- a/procps/ps.c +++ b/procps/ps.c | |||
@@ -27,11 +27,7 @@ static void func_comm(char *buf, int size, const procps_status_t *ps) | |||
27 | 27 | ||
28 | static void func_args(char *buf, int size, const procps_status_t *ps) | 28 | static void func_args(char *buf, int size, const procps_status_t *ps) |
29 | { | 29 | { |
30 | buf[0] = '\0'; | 30 | read_cmdline(buf, size, ps->pid, ps->comm); |
31 | if (ps->cmd) | ||
32 | safe_strncpy(buf, ps->cmd, size+1); | ||
33 | else if (size >= 2) | ||
34 | sprintf(buf, "[%.*s]", size-2, ps->comm); | ||
35 | } | 31 | } |
36 | 32 | ||
37 | static void func_pid(char *buf, int size, const procps_status_t *ps) | 33 | static void func_pid(char *buf, int size, const procps_status_t *ps) |
@@ -112,25 +108,25 @@ typedef struct { | |||
112 | 108 | ||
113 | static const ps_out_t out_spec[] = { | 109 | static const ps_out_t out_spec[] = { |
114 | // Mandated by POSIX: | 110 | // Mandated by POSIX: |
115 | { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID }, | 111 | { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID }, |
116 | { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM }, | 112 | { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM }, |
117 | { 256 , "args" ,"COMMAND",func_args ,PSSCAN_CMD|PSSCAN_COMM }, | 113 | { 256 , "args" ,"COMMAND",func_args ,PSSCAN_COMM }, |
118 | { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID }, | 114 | { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID }, |
119 | { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID }, | 115 | { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID }, |
120 | { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID }, | 116 | { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID }, |
121 | // { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_ }, | 117 | // { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_ }, |
122 | // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID }, | 118 | // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID }, |
123 | // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ }, | 119 | // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ }, |
124 | // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ }, | 120 | // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ }, |
125 | // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID }, | 121 | // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID }, |
126 | // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID }, | 122 | // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID }, |
127 | // { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ }, | 123 | // { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ }, |
128 | { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY }, | 124 | { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY }, |
129 | { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, | 125 | { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ }, |
130 | // Not mandated by POSIX, but useful: | 126 | // Not mandated by POSIX, but useful: |
131 | { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, | 127 | { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS }, |
132 | #if ENABLE_SELINUX | 128 | #if ENABLE_SELINUX |
133 | { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT }, | 129 | { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT }, |
134 | #endif | 130 | #endif |
135 | }; | 131 | }; |
136 | 132 | ||
@@ -386,10 +382,9 @@ int ps_main(int argc, char **argv) | |||
386 | | PSSCAN_UIDGID | 382 | | PSSCAN_UIDGID |
387 | | PSSCAN_STATE | 383 | | PSSCAN_STATE |
388 | | PSSCAN_VSZ | 384 | | PSSCAN_VSZ |
389 | | PSSCAN_CMD | 385 | | PSSCAN_COMM |
390 | | use_selinux | 386 | | use_selinux |
391 | ))) { | 387 | ))) { |
392 | char *namecmd = p->cmd; | ||
393 | #if ENABLE_SELINUX | 388 | #if ENABLE_SELINUX |
394 | if (use_selinux) { | 389 | if (use_selinux) { |
395 | len = printf("%5u %-32s %s ", | 390 | len = printf("%5u %-32s %s ", |
@@ -408,21 +403,11 @@ int ps_main(int argc, char **argv) | |||
408 | p->pid, user, p->vsz, p->state); | 403 | p->pid, user, p->vsz, p->state); |
409 | } | 404 | } |
410 | 405 | ||
411 | i = terminal_width-len; | 406 | { |
412 | 407 | char sz = terminal_width - len; | |
413 | if (namecmd && namecmd[0]) { | 408 | char buf[sz + 1]; |
414 | if (i < 0) | 409 | read_cmdline(buf, sz, p->pid, p->comm); |
415 | i = 0; | 410 | puts(buf); |
416 | if (strlen(namecmd) > (size_t)i) | ||
417 | namecmd[i] = '\0'; | ||
418 | puts(namecmd); | ||
419 | } else { | ||
420 | namecmd = p->comm; | ||
421 | if (i < 2) | ||
422 | i = 2; | ||
423 | if (strlen(namecmd) > ((size_t)i-2)) | ||
424 | namecmd[i-2] = '\0'; | ||
425 | printf("[%s]\n", namecmd); | ||
426 | } | 411 | } |
427 | } | 412 | } |
428 | if (ENABLE_FEATURE_CLEAN_UP) | 413 | if (ENABLE_FEATURE_CLEAN_UP) |
diff --git a/procps/top.c b/procps/top.c index 136f1404a..a37a0d07f 100644 --- a/procps/top.c +++ b/procps/top.c | |||
@@ -40,11 +40,10 @@ typedef struct top_status_t { | |||
40 | unsigned pid, ppid; | 40 | unsigned pid, ppid; |
41 | unsigned uid; | 41 | unsigned uid; |
42 | char state[4]; | 42 | char state[4]; |
43 | /* TODO: read /proc/$PID/cmdline only for processes which are displayed */ | 43 | char comm[COMM_LEN]; |
44 | char cmd[64]; | ||
45 | } top_status_t; | 44 | } top_status_t; |
46 | 45 | ||
47 | typedef struct jiffy_counts_t{ | 46 | typedef struct jiffy_counts_t { |
48 | unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal; | 47 | unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal; |
49 | unsigned long long total; | 48 | unsigned long long total; |
50 | unsigned long long busy; | 49 | unsigned long long busy; |
@@ -421,7 +420,7 @@ static void display_status(int count, int scr_width) | |||
421 | 420 | ||
422 | /* Ok, all prelim data is ready, go thru the list */ | 421 | /* Ok, all prelim data is ready, go thru the list */ |
423 | while (count-- > 0) { | 422 | while (count-- > 0) { |
424 | int col = scr_width+1; | 423 | int col = scr_width; |
425 | CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift); | 424 | CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift); |
426 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE | 425 | #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE |
427 | CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift); | 426 | CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift); |
@@ -444,8 +443,11 @@ static void display_status(int count, int scr_width) | |||
444 | , SHOW_STAT(pcpu) | 443 | , SHOW_STAT(pcpu) |
445 | #endif | 444 | #endif |
446 | ); | 445 | ); |
447 | if (col > 0) | 446 | if (col > 0) { |
448 | printf("%.*s", col, s->cmd); | 447 | char buf[col + 1]; |
448 | read_cmdline(buf, col, s->pid, s->comm); | ||
449 | fputs(buf, stdout); | ||
450 | } | ||
449 | /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, | 451 | /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, |
450 | jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ | 452 | jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ |
451 | s++; | 453 | s++; |
@@ -560,12 +562,11 @@ int top_main(int argc, char **argv) | |||
560 | | PSSCAN_UTIME | 562 | | PSSCAN_UTIME |
561 | | PSSCAN_STATE | 563 | | PSSCAN_STATE |
562 | | PSSCAN_COMM | 564 | | PSSCAN_COMM |
563 | | PSSCAN_CMD | ||
564 | | PSSCAN_SID | 565 | | PSSCAN_SID |
565 | | PSSCAN_UIDGID | 566 | | PSSCAN_UIDGID |
566 | ))) { | 567 | ))) { |
567 | int n = ntop; | 568 | int n = ntop; |
568 | top = xrealloc(top, (++ntop)*sizeof(top_status_t)); | 569 | top = xrealloc(top, (++ntop) * sizeof(*top)); |
569 | top[n].pid = p->pid; | 570 | top[n].pid = p->pid; |
570 | top[n].ppid = p->ppid; | 571 | top[n].ppid = p->ppid; |
571 | top[n].vsz = p->vsz; | 572 | top[n].vsz = p->vsz; |
@@ -574,10 +575,7 @@ int top_main(int argc, char **argv) | |||
574 | #endif | 575 | #endif |
575 | top[n].uid = p->uid; | 576 | top[n].uid = p->uid; |
576 | strcpy(top[n].state, p->state); | 577 | strcpy(top[n].state, p->state); |
577 | if (p->cmd) | 578 | strcpy(top[n].comm, p->comm); |
578 | safe_strncpy(top[n].cmd, p->cmd, sizeof(top[n].cmd)); | ||
579 | else /* mimic ps */ | ||
580 | sprintf(top[n].cmd, "[%s]", p->comm); | ||
581 | } | 579 | } |
582 | if (ntop == 0) { | 580 | if (ntop == 0) { |
583 | bb_error_msg_and_die("no process info in /proc"); | 581 | bb_error_msg_and_die("no process info in /proc"); |