top,ps: 'stringify' tty only when needed. -60 bytes.
authorDenis Vlasenko <vda.linux@googlemail.com>
Tue, 19 Jun 2007 14:43:17 +0000 (14:43 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Tue, 19 Jun 2007 14:43:17 +0000 (14:43 -0000)
include/libbb.h
libbb/procps.c
procps/ps.c

index 999caae0c4797dfb4f00c1fd4d1577949d56b4a0..9ca5653488cd3f28ea154f227a31f8071b774cdf 100644 (file)
@@ -847,10 +847,9 @@ typedef struct {
        unsigned sid;
        unsigned uid;
        unsigned gid;
+       unsigned tty_major,tty_minor;
        char state[4];
-       char tty_str[8]; /* "maj,min" or "?" */
-       /* basename of executable in exec(2), read from /proc/N/stat, */
-       /* size from sizeof(task_struct.comm) in /usr/include/linux/sched.h */
+       /* basename of executable in exec(2), read from /proc/N/stat */
        char comm[COMM_LEN];
        /* user/group? - use passwd/group parsing functions */
 } procps_status_t;
index e04ff34ff20a95cda224899c20377c8be80dede3..10ddabf9469da8cc8b3e57a1f058edba9b9f4f40 100644 (file)
@@ -225,30 +225,18 @@ procps_status_t* procps_scan(procps_status_t* sp, int flags)
                                break;
                        sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
                        sp->rss = rss >> 10;
-
-                       sp->tty_str[0] = '?';
-                       /* sp->tty_str[1] = '\0'; - done by memset */
-                       if (tty) /* tty field of "0" means "no tty" */
-                               snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
-                                       (tty >> 8) & 0xfff, /* major */
-                                       (tty & 0xff) | ((tty >> 12) & 0xfff00));
+                       sp->tty_major = (tty >> 8) & 0xfff;
+                       sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00));
 #else
 /* This costs ~100 bytes more but makes top faster by 20%
  * If you run 10000 processes, this may be important for you */
-                       cp += 2;
-                       sp->state[0] = *cp++; cp++;
-                       sp->ppid = fast_strtoul_10(cp, &cp);
+                       sp->state[0] = cp[2];
+                       sp->ppid = fast_strtoul_10(cp + 4, &cp);
                        sp->pgid = fast_strtoul_10(cp, &cp);
                        sp->sid = fast_strtoul_10(cp, &cp);
-                       sp->tty_str[0] = '?';
-                       /* sp->tty_str[1] = '\0'; - done by memset */
                        tty = fast_strtoul_10(cp, &cp);
-                       if (tty && (flags & PSSCAN_TTY)) {
-                               /* tty field of "0" means "no tty" */
-                               snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
-                                       (tty >> 8) & 0xfff, /* major */
-                                       (tty & 0xff) | ((tty >> 12) & 0xfff00));
-                       }
+                       sp->tty_major = (tty >> 8) & 0xfff;
+                       sp->tty_minor = (tty & 0xff) | ((tty >> 12) & 0xfff00);
                        cp = skip_fields(cp, 6); /* tpgid, flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
                        sp->utime = fast_strtoul_10(cp, &cp);
                        sp->stime = fast_strtoul_10(cp, &cp);
index c3023cf5e97ca70e97bbcd3235948e1d9fb24e3b..fd53eca5b90297c4e4af3f140042dfdd2885be5e 100644 (file)
@@ -68,7 +68,10 @@ static void func_rss(char *buf, int size, const procps_status_t *ps)
 
 static void func_tty(char *buf, int size, const procps_status_t *ps)
 {
-       safe_strncpy(buf, ps->tty_str, size+1);
+       buf[0] = '?';
+       buf[1] = '\0';
+       if (ps->tty_major) /* tty field of "0" means "no tty" */
+               snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
 }
 
 #if ENABLE_SELINUX