[PATCH] pstree: New applet
Denys Vlasenko
vda.linux at googlemail.com
Sat Nov 6 02:24:25 UTC 2010
On Thu, Nov 4, 2010 at 2:53 PM, Lauri Kasanen <curaga at operamail.com> wrote:
> Attaching a port of pstree to busybox.
>
> I did take a look at the procps_scan function, but I don't think that's easily usable for pstree.
>
> - Lauri
>
>
> function old new delta
> pstree_main - 851 +851
> dump_tree - 822 +822
> .rodata 1418 1671 +253
> add_proc - 221 +221
> out_char - 160 +160
> dump_by_user - 110 +110
> new_proc - 102 +102
> tree_equal - 82 +82
> out_newline - 65 +65
> sym - 48 +48
> xstat - 30 +30
> xgetpwnam - 29 +29
> out_string - 25 +25
> find_proc - 24 +24
> xuname2uid - 15 +15
> width - 8 +8
> static.stderr@@GLIBC_2 - 8 +8
> more - 8 +8
> list - 8 +8
> applet_main 16 24 +8
> applet_names 7 14 +7
> output_width - 4 +4
> dumped - 4 +4
> cur_x - 4 +4
> charlen - 4 +4
> capacity - 4 +4
> applet_nameofs 4 6 +2
> last_char - 1 +1
> ------------------------------------------------------------------------------
> (add/remove: 25/0 grow/shrink: 4/0 up/down: 2907/0) Total: 2907 bytes
> text data bss dec hex filename
> 18124 898 128 19150 4ace busybox_old
> 22018 986 168 23172 5a84 busybox_unstripped
You have a lot of static data:
+static struct {
+ const char *empty_2;
+ const char *branch_2;
+ const char *vert_2;
+ const char *last_2;
+ const char *single_3;
+ const char *first_3;
+} sym = {
+" ", "|-", "| ", "`-", "---", "-+-"};
+
+static PROC *list = NULL;
+
+/* The buffers will be dynamically increased in size as needed. */
+static int capacity = 0;
+static int *width = NULL;
+static int *more = NULL;
+
+static int output_width = 132;
+static int cur_x = 1;
+static char last_char = 0;
+static int dumped = 0; /* used by dump_by_user */
+static int charlen = 0; /* length of character */
Can you eliminate it? Most modules in busybox have zero
data and bss:
# size coreutils/*.o
text data bss dec hex filename
121 0 0 121 79 coreutils/basename.o
1810 0 0 1810 712 coreutils/cal.o
118 0 0 118 76 coreutils/cat.o
322 0 0 322 142 coreutils/catv.o
66 0 0 66 42 coreutils/chgrp.o
418 0 0 418 1a2 coreutils/chmod.o
440 0 0 440 1b8 coreutils/chown.o
89 0 0 89 59 coreutils/chroot.o
204 0 0 204 cc coreutils/cksum.o
371 0 0 371 173 coreutils/comm.o
582 0 0 582 246 coreutils/cp.o
1233 0 0 1233 4d1 coreutils/cut.o
1152 0 0 1152 480 coreutils/date.o
2237 0 0 2237 8bd coreutils/dd.o
1077 0 0 1077 435 coreutils/df.o
28 0 0 28 1c coreutils/dirname.o
448 0 0 448 1c0 coreutils/dos2unix.o
793 0 0 793 319 coreutils/du.o
293 0 0 293 125 coreutils/echo.o
254 0 0 254 fe coreutils/env.o
792 0 0 792 318 coreutils/expand.o
2477 0 0 2477 9ad coreutils/expr.o
6 0 0 6 6 coreutils/false.o
657 0 0 657 291 coreutils/fold.o
128 0 0 128 80 coreutils/fsync.o
469 0 0 469 1d5 coreutils/head.o
43 0 0 43 2b coreutils/hostid.o
789 0 0 789 315 coreutils/id.o
859 0 0 859 35b coreutils/install.o
54 0 0 54 36 coreutils/length.o
465 0 0 465 1d1 coreutils/ln.o
72 0 0 72 48 coreutils/logname.o
4032 0 0 4032 fc0 coreutils/ls.o
913 0 0 913 391 coreutils/md5_sha1_sum.o
199 0 0 199 c7 coreutils/mkdir.o
76 0 0 76 4c coreutils/mkfifo.o
193 0 0 193 c1 coreutils/mknod.o
678 0 0 678 2a6 coreutils/mv.o
177 0 0 177 b1 coreutils/nice.o
273 0 0 273 111 coreutils/nohup.o
6474 13 38 6525 197d coreutils/od.o
86 0 0 86 56 coreutils/printenv.o
1651 0 0 1651 673 coreutils/printf.o
41 0 0 41 29 coreutils/pwd.o
128 0 0 128 80 coreutils/readlink.o
79 0 0 79 4f coreutils/realpath.o
202 0 0 202 ca coreutils/rmdir.o
206 0 0 206 ce coreutils/rm.o
485 0 0 485 1e5 coreutils/seq.o
445 0 0 445 1bd coreutils/sleep.o
2210 0 5 2215 8a7 coreutils/sort.o
637 0 0 637 27d coreutils/split.o
3268 0 0 3268 cc4 coreutils/stat.o
5303 0 0 5303 14b7 coreutils/stty.o
498 0 0 498 1f2 coreutils/sum.o
20 0 0 20 14 coreutils/sync.o
335 0 0 335 14f coreutils/tac.o
1894 0 0 1894 766 coreutils/tail.o
324 0 0 324 144 coreutils/tee.o
2337 0 0 2337 921 coreutils/test.o
0 0 0 0 0 coreutils/test_ptr_hack.o
440 0 0 440 1b8 coreutils/touch.o
1378 0 0 1378 562 coreutils/tr.o
3 0 0 3 3 coreutils/true.o
96 0 0 96 60 coreutils/tty.o
404 0 0 404 194 coreutils/uname.o
435 0 0 435 1b3 coreutils/uniq.o
40 0 0 40 28 coreutils/usleep.o
875 0 0 875 36b coreutils/uudecode.o
385 0 0 385 181 coreutils/uuencode.o
668 0 0 668 29c coreutils/wc.o
37 0 0 37 25 coreutils/whoami.o
453 0 0 453 1c5 coreutils/who.o
120 0 0 120 78 coreutils/yes.o
Your new module usually is expected to do the same.
+ if (width != NULL) {
+ free(width);
+ width = NULL;
+ }
if () is redundant here - free(NULL) is allowed.
+ new = xmalloc(sizeof(PROC));
+
+ strcpy(new->comm, comm);
+ new->pid = pid;
+ new->uid = uid;
+ new->flags = 0;
+ new->children = NULL;
+ new->parent = NULL;
+ new->next = list;
+ return list = new;
use xzalloc - you don't need zero assignments after it.
+static int out_args(char *mystr)
+{
+ char *here;
+ int strcount=0;
+ char tmpstr[5];
+
+ for (here = mystr; *here; here++) {
+ if (*here == '\\') {
+ out_string("\\\\");
+ strcount += 2;
+ } else if (*here >= ' ' && *here <= '~') {
+ out_char(*here);
+ strcount++;
+ } else {
+ sprintf(tmpstr, "\\%03o", (unsigned char) *here);
+ out_string(tmpstr);
+ strcount += 4;
+ }
+ } /* for */
+ return strcount;
+}
Please use uniform indent. Everywhere.
+ out_string(lvl ==
+ level -
+ 1 ? last ? sym.last_2 :
sym.branch_2 : more[lvl +
+
1] ?
+ sym.vert_2 : sym.empty_2);
Can you write it more readably?
+ path = xmalloc(strlen(PROC_BASE) + strlen(de->d_name) + 10);
+ sprintf(path, "%s/%d/stat", PROC_BASE, pid);
^^^^^^^^^^^^^^^^^^
use xasprintf instead of xmalloc+sprintf
+ if ((file = fopen(path, "r")) != NULL) {
+ empty = 0;
+ sprintf(path, "%s/%d", PROC_BASE, pid);
+ xstat(path, &st);
--
vda
More information about the busybox
mailing list