replace /proc scanning code by more versatile one.

Use it where appropriate.
Stop scanning /etc/passwd *for every process*!!! (uid->username)
top: reduce memory usage - we won't save unneeded fields
from /proc info anymore. Downside: ~+250 bytes of code
This commit is contained in:
Denis Vlasenko 2006-11-05 00:43:51 +00:00
parent fa07680091
commit 459e4d6cf7
6 changed files with 329 additions and 172 deletions

View File

@ -501,23 +501,47 @@ enum { COMM_LEN = 16 };
#endif #endif
#endif #endif
typedef struct { typedef struct {
int pid, ppid; DIR *dir;
char user[9]; /* Fields are set to 0/NULL if failed to determine (or not requested) */
char state[4];
unsigned long rss;
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
unsigned pcpu;
unsigned long stime, utime;
#endif
char *cmd; char *cmd;
unsigned long rss;
/* basename of executable file in call to exec(2), unsigned long stime, utime;
size from kernel headers */ unsigned pid;
char short_cmd[COMM_LEN]; unsigned ppid;
unsigned pgid;
unsigned sid;
unsigned uid;
unsigned gid;
/* basename of executable file in call to exec(2), size from */
/* sizeof(task_struct.comm) in /usr/include/linux/sched.h */
char state[4];
char comm[COMM_LEN];
// user/group? - use passwd/group parsing functions
} procps_status_t; } procps_status_t;
procps_status_t* procps_scan(int save_user_arg0); enum {
PSSCAN_PID = 1 << 0,
PSSCAN_PPID = 1 << 1,
PSSCAN_PGID = 1 << 2,
PSSCAN_SID = 1 << 3,
PSSCAN_UIDGID = 1 << 4,
PSSCAN_COMM = 1 << 5,
PSSCAN_CMD = 1 << 6,
PSSCAN_STATE = 1 << 7,
PSSCAN_RSS = 1 << 8,
PSSCAN_STIME = 1 << 9,
PSSCAN_UTIME = 1 << 10,
/* These are all retrieved from proc/NN/stat in one go: */
PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
| PSSCAN_COMM | PSSCAN_STATE
| PSSCAN_RSS | PSSCAN_STIME | PSSCAN_UTIME,
};
procps_status_t* alloc_procps_scan(int flags);
void free_procps_scan(procps_status_t* sp);
procps_status_t* procps_scan(procps_status_t* sp, int flags);
pid_t *find_pid_by_name(const char* procName); pid_t *find_pid_by_name(const char* procName);
pid_t *pidlist_reverse(pid_t *pidList); pid_t *pidlist_reverse(pid_t *pidList);
void clear_username_cache(void);
const char* get_cached_username(uid_t uid);
extern const char bb_uuenc_tbl_base64[]; extern const char bb_uuenc_tbl_base64[];

View File

@ -23,11 +23,11 @@ pid_t* find_pid_by_name(const char* procName)
{ {
pid_t* pidList; pid_t* pidList;
int i = 0; int i = 0;
procps_status_t* p; procps_status_t* p = NULL;
pidList = xmalloc(sizeof(*pidList)); pidList = xmalloc(sizeof(*pidList));
while ((p = procps_scan(0)) != 0) { while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM))) {
if (strncmp(p->short_cmd, procName, COMM_LEN-1) == 0) { if (strncmp(p->comm, procName, sizeof(p->comm)-1) == 0) {
pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
pidList[i++] = p->pid; pidList[i++] = p->pid;
} }

View File

@ -11,6 +11,35 @@
#include "libbb.h" #include "libbb.h"
typedef struct {
uid_t uid;
char username[12];
} user_map_t;
static user_map_t *username_cache;
static int username_cache_size;
void clear_username_cache(void)
{
free(username_cache);
username_cache = NULL;
username_cache_size = 0;
}
const char* get_cached_username(uid_t uid)
{
int i;
for (i = 0; i < username_cache_size; i++)
if (username_cache[i].uid == uid)
return username_cache[i].username;
i = username_cache_size++;
username_cache = xrealloc(username_cache, username_cache_size * sizeof(*username_cache));
username_cache[i].uid = uid;
bb_getpwuid(username_cache[i].username, uid, sizeof(username_cache[i].username));
return username_cache[i].username;
}
#define PROCPS_BUFSIZE 1024 #define PROCPS_BUFSIZE 1024
static int read_to_buf(const char *filename, void *buf) static int read_to_buf(const char *filename, void *buf)
@ -21,119 +50,171 @@ static int read_to_buf(const char *filename, void *buf)
return ret; return ret;
} }
procps_status_t* alloc_procps_scan(int flags)
procps_status_t * procps_scan(int save_user_arg0)
{ {
static DIR *dir; procps_status_t* sp = xzalloc(sizeof(procps_status_t));
static procps_status_t ret_status; sp->dir = xopendir("/proc");
return sp;
}
void free_procps_scan(procps_status_t* sp)
{
closedir(sp->dir);
free(sp->cmd);
free(sp);
}
void BUG_comm_size(void);
procps_status_t* procps_scan(procps_status_t* sp, int flags)
{
struct dirent *entry; struct dirent *entry;
char *name;
char buf[PROCPS_BUFSIZE]; char buf[PROCPS_BUFSIZE];
char status[sizeof("/proc//cmdline") + sizeof(int)*3]; char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
char *status_tail; char *filename_tail;
procps_status_t curstatus;
long tasknice; long tasknice;
int pid; unsigned pid;
int n; int n;
struct stat sb; struct stat sb;
if (!dir) { if (!sp)
dir = xopendir("/proc"); sp = alloc_procps_scan(flags);
}
for (;;) { for (;;) {
entry = readdir(dir); entry = readdir(sp->dir);
if (entry == NULL) { if (entry == NULL) {
closedir(dir); free_procps_scan(sp);
dir = 0; return NULL;
return 0;
} }
name = entry->d_name; if (safe_strtou(entry->d_name, &pid))
if (!(*name >= '0' && *name <= '9'))
continue; continue;
memset(&curstatus, 0, sizeof(procps_status_t)); /* After this point we have to break, not continue
pid = atoi(name); * ("continue" would mean that current /proc/NNN
curstatus.pid = pid; * is not a valid process info) */
status_tail = status + sprintf(status, "/proc/%d", pid); memset(&sp->rss, 0, sizeof(*sp) - offsetof(procps_status_t, rss));
if (stat(status, &sb))
continue;
bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
sp->pid = pid;
if (!(flags & ~PSSCAN_PID)) break;
filename_tail = filename + sprintf(filename, "/proc/%d", pid);
if (flags & PSSCAN_UIDGID) {
if (stat(filename, &sb))
break;
/* Need comment - is this effective or read UID/GID? */
sp->uid = sb.st_uid;
sp->gid = sb.st_gid;
}
if (flags & PSSCAN_STAT) {
char *cp;
/* see proc(5) for some details on this */ /* see proc(5) for some details on this */
strcpy(status_tail, "/stat"); strcpy(filename_tail, "/stat");
n = read_to_buf(status, buf); n = read_to_buf(filename, buf);
if (n < 0) if (n < 0)
continue; break;
name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
if (name == 0 || name[1] != ' ') if (!cp || cp[1] != ' ')
continue; break;
*name = 0; cp[0] = '\0';
sscanf(buf, "%*s (%15c", curstatus.short_cmd); if (sizeof(sp->comm) < 16)
n = sscanf(name+2, BUG_comm_size();
"%c %d " sscanf(buf, "%*s (%15c", sp->comm);
"%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ n = sscanf(cp+2,
"%c %u " /* state, ppid */
"%u %u %*s %*s " /* pgid, sid, tty, tpgid */
"%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
"%lu %lu " /* utime, stime */ "%lu %lu " /* utime, stime */
#else
"%*s %*s " /* utime, stime */
#endif
"%*s %*s %*s " /* cutime, cstime, priority */ "%*s %*s %*s " /* cutime, cstime, priority */
"%ld " /* nice */ "%ld " /* nice */
"%*s %*s %*s " /* timeout, it_real_value, start_time */ "%*s %*s %*s " /* timeout, it_real_value, start_time */
"%*s " /* vsize */ "%*s " /* vsize */
"%ld", /* rss */ "%lu", /* rss */
curstatus.state, &curstatus.ppid, sp->state, &sp->ppid,
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE &sp->pgid, &sp->sid,
&curstatus.utime, &curstatus.stime, &sp->utime, &sp->stime,
#endif
&tasknice, &tasknice,
&curstatus.rss); &sp->rss);
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE if (n != 8)
if (n != 6) break;
#else
if (n != 4)
#endif
continue;
if (curstatus.rss == 0 && curstatus.state[0] != 'Z') if (sp->rss == 0 && sp->state[0] != 'Z')
curstatus.state[1] = 'W'; sp->state[1] = 'W';
else else
curstatus.state[1] = ' '; sp->state[1] = ' ';
if (tasknice < 0) if (tasknice < 0)
curstatus.state[2] = '<'; sp->state[2] = '<';
else if (tasknice > 0) else if (tasknice > 0)
curstatus.state[2] = 'N'; sp->state[2] = 'N';
else else
curstatus.state[2] = ' '; sp->state[2] = ' ';
#ifdef PAGE_SHIFT #ifdef PAGE_SHIFT
curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ sp->rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
#else #else
curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */ sp->rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
#endif #endif
}
if (save_user_arg0) { if (flags & PSSCAN_CMD) {
strcpy(status_tail, "/cmdline"); free(sp->cmd);
n = read_to_buf(status, buf); sp->cmd = NULL;
if (n > 0) { strcpy(filename_tail, "/cmdline");
if (buf[n-1]=='\n') n = read_to_buf(filename, buf);
buf[--n] = 0; if (n <= 0)
name = buf; break;
while (n) { if (buf[n-1] == '\n') {
if (((unsigned char)*name) < ' ') if (!--n)
*name = ' '; break;
name++; buf[n] = '\0';
}
do {
n--; n--;
if ((unsigned char)(buf[n]) < ' ')
buf[n] = ' ';
} while (n);
sp->cmd = strdup(buf);
} }
*name = 0; break;
if (buf[0])
curstatus.cmd = strdup(buf);
/* if NULL it work true also */
}
}
return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
} }
return sp;
} }
/* from kernel:
// pid comm S ppid pgid sid tty_nr tty_pgrp flg
sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
task->pid,
tcomm,
state,
ppid,
pgid,
sid,
tty_nr,
tty_pgrp,
task->flags,
min_flt,
cmin_flt,
maj_flt,
cmaj_flt,
cputime_to_clock_t(utime),
cputime_to_clock_t(stime),
cputime_to_clock_t(cutime),
cputime_to_clock_t(cstime),
priority,
nice,
num_threads,
// 0,
start_time,
vsize,
mm ? get_mm_rss(mm) : 0,
rsslim,
mm ? mm->start_code : 0,
mm ? mm->end_code : 0,
mm ? mm->start_stack : 0,
esp,
eip,
the rest is some obsolete cruft
*/

View File

@ -82,7 +82,7 @@ do_it_now:
if (killall5) { if (killall5) {
pid_t sid; pid_t sid;
procps_status_t* p; procps_status_t* p = NULL;
// Cannot happen anyway? We don't TERM ourself, we STOP // Cannot happen anyway? We don't TERM ourself, we STOP
// /* kill(-1, sig) on Linux (at least 2.1.x) // /* kill(-1, sig) on Linux (at least 2.1.x)
@ -94,8 +94,8 @@ do_it_now:
pid = getpid(); pid = getpid();
sid = getsid(pid); sid = getsid(pid);
/* Now kill all processes except our session */ /* Now kill all processes except our session */
while ((p = procps_scan(0))!=0) { while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
if (getsid(p->pid)!=sid && p->pid!=pid && p->pid!=1) if (p->sid != sid && p->pid != pid && p->pid != 1)
kill(p->pid, signo); kill(p->pid, signo);
} }
/* And let them continue */ /* And let them continue */

View File

@ -11,7 +11,7 @@
int ps_main(int argc, char **argv) int ps_main(int argc, char **argv)
{ {
procps_status_t * p; procps_status_t *p = NULL;
int i, len; int i, len;
SKIP_SELINUX(const) int use_selinux = 0; SKIP_SELINUX(const) int use_selinux = 0;
USE_SELINUX(security_context_t sid = NULL;) USE_SELINUX(security_context_t sid = NULL;)
@ -50,7 +50,13 @@ int ps_main(int argc, char **argv)
else else
puts(" PID Uid VmSize Stat Command"); puts(" PID Uid VmSize Stat Command");
while ((p = procps_scan(1)) != 0) { while ((p = procps_scan(p, 0
| PSSCAN_PID
| PSSCAN_UIDGID
| PSSCAN_STATE
| PSSCAN_RSS
| PSSCAN_CMD
))) {
char *namecmd = p->cmd; char *namecmd = p->cmd;
#if ENABLE_SELINUX #if ENABLE_SELINUX
if (use_selinux) { if (use_selinux) {
@ -71,13 +77,18 @@ int ps_main(int argc, char **argv)
} else { } else {
safe_strncpy(sbuf, "unknown", 7); safe_strncpy(sbuf, "unknown", 7);
} }
len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state); len = printf("%5u %-32s %s ", p->pid, sbuf, p->state);
} else } else
#endif #endif
{
const char *user = get_cached_username(p->uid);
if (p->rss == 0) if (p->rss == 0)
len = printf("%5u %-8s %s ", (unsigned)p->pid, p->user, p->state); len = printf("%5u %-8s %s ",
p->pid, user, p->state);
else else
len = printf("%5u %-8s %6ld %s ", (unsigned)p->pid, p->user, p->rss, p->state); len = printf("%5u %-8s %6ld %s ",
p->pid, user, p->rss, p->state);
}
i = terminal_width-len; i = terminal_width-len;
@ -88,16 +99,15 @@ int ps_main(int argc, char **argv)
namecmd[i] = 0; namecmd[i] = 0;
puts(namecmd); puts(namecmd);
} else { } else {
namecmd = p->short_cmd; namecmd = p->comm;
if (i < 2) if (i < 2)
i = 2; i = 2;
if (strlen(namecmd) > ((size_t)i-2)) if (strlen(namecmd) > ((size_t)i-2))
namecmd[i-2] = 0; namecmd[i-2] = 0;
printf("[%s]\n", namecmd); printf("[%s]\n", namecmd);
} }
/* no check needed, but to make valgrind happy.. */
if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
free(p->cmd);
} }
if (ENABLE_FEATURE_CLEAN_UP)
clear_username_cache();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -30,37 +30,76 @@
#include "busybox.h" #include "busybox.h"
typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
static procps_status_t *top; /* Hehe */ typedef struct {
unsigned long rss;
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
unsigned long ticks;
unsigned pcpu; /* delta of ticks */
#endif
unsigned pid, ppid;
unsigned uid;
char state[4];
char comm[COMM_LEN];
} top_status_t;
static top_status_t *top;
static int ntop; static int ntop;
/* This structure stores some critical information from one frame to
the next. Used for finding deltas. */
struct save_hist {
unsigned long ticks;
unsigned pid;
};
static struct save_hist *prev_hist;
static int prev_hist_count;
/* static int hist_iterations; */
static unsigned total_pcpu;
/* static unsigned long total_rss; */
#define OPT_BATCH_MODE (option_mask32 & 0x4) #define OPT_BATCH_MODE (option_mask32 & 0x4)
#if ENABLE_FEATURE_USE_TERMIOS #if ENABLE_FEATURE_USE_TERMIOS
static int pid_sort(procps_status_t *P, procps_status_t *Q) static int pid_sort(top_status_t *P, top_status_t *Q)
{ {
/* Buggy wrt pids with high bit set */
/* (linux pids are in [1..2^15-1]) */
return (Q->pid - P->pid); return (Q->pid - P->pid);
} }
#endif #endif
static int mem_sort(procps_status_t *P, procps_status_t *Q) static int mem_sort(top_status_t *P, top_status_t *Q)
{ {
return (int)(Q->rss - P->rss); /* We want to avoid unsigned->signed and truncation errors */
if (Q->rss < P->rss) return -1;
return Q->rss != P->rss; /* 0 if ==, 1 if > */
} }
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
static cmp_funcp sort_function;
#else
enum { SORT_DEPTH = 3 }; enum { SORT_DEPTH = 3 };
static cmp_t sort_function[SORT_DEPTH];
static int pcpu_sort(procps_status_t *P, procps_status_t *Q) static cmp_funcp sort_function[SORT_DEPTH];
static int pcpu_sort(top_status_t *P, top_status_t *Q)
{ {
return (Q->pcpu - P->pcpu); /* Buggy wrt ticks with high bit set */
/* Affects only processes for which ticks overflow */
return (int)Q->pcpu - (int)P->pcpu;
} }
static int time_sort(procps_status_t *P, procps_status_t *Q) static int time_sort(top_status_t *P, top_status_t *Q)
{ {
return (int)((Q->stime + Q->utime) - (P->stime + P->utime)); /* We want to avoid unsigned->signed and truncation errors */
if (Q->ticks < P->ticks) return -1;
return Q->ticks != P->ticks; /* 0 if ==, 1 if > */
} }
static int mult_lvl_cmp(void* a, void* b) { static int mult_lvl_cmp(void* a, void* b) {
@ -74,24 +113,6 @@ static int mult_lvl_cmp(void* a, void* b) {
return 0; return 0;
} }
/* This structure stores some critical information from one frame to
the next. Mostly used for sorting. */
struct save_hist {
int ticks;
pid_t pid;
};
/*
* Calculates percent cpu usage for each task.
*/
static struct save_hist *prev_hist;
static int prev_hist_count;
/* static int hist_iterations; */
static unsigned total_pcpu;
/* static unsigned long total_rss; */
typedef struct { typedef struct {
unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal; unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
@ -115,11 +136,12 @@ static void get_jiffy_counts(void)
jif.busy = jif.total - jif.idle - jif.iowait; jif.busy = jif.total - jif.idle - jif.iowait;
} }
static void do_stats(void) static void do_stats(void)
{ {
procps_status_t *cur; top_status_t *cur;
pid_t pid; pid_t pid;
int total_time, i, last_i, n; int i, last_i, n;
struct save_hist *new_hist; struct save_hist *new_hist;
get_jiffy_counts(); get_jiffy_counts();
@ -139,8 +161,7 @@ static void do_stats(void)
* and system time * and system time
*/ */
pid = cur->pid; pid = cur->pid;
total_time = cur->stime + cur->utime; new_hist[n].ticks = cur->ticks;
new_hist[n].ticks = total_time;
new_hist[n].pid = pid; new_hist[n].pid = pid;
/* find matching entry from previous pass */ /* find matching entry from previous pass */
@ -150,13 +171,13 @@ static void do_stats(void)
last_i = i; last_i = i;
if (prev_hist_count) do { if (prev_hist_count) do {
if (prev_hist[i].pid == pid) { if (prev_hist[i].pid == pid) {
cur->pcpu = total_time - prev_hist[i].ticks; cur->pcpu = cur->ticks - prev_hist[i].ticks;
total_pcpu += cur->pcpu;
break; break;
} }
i = (i+1) % prev_hist_count; i = (i+1) % prev_hist_count;
/* hist_iterations++; */ /* hist_iterations++; */
} while (i != last_i); } while (i != last_i);
total_pcpu += cur->pcpu;
/* total_rss += cur->rss; */ /* total_rss += cur->rss; */
} }
@ -167,10 +188,9 @@ static void do_stats(void)
prev_hist = new_hist; prev_hist = new_hist;
prev_hist_count = ntop; prev_hist_count = ntop;
} }
#else
static cmp_t sort_function;
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
/* display generic info (meminfo / loadavg) */ /* display generic info (meminfo / loadavg) */
static unsigned long display_generic(int scr_width) static unsigned long display_generic(int scr_width)
{ {
@ -265,7 +285,7 @@ static void display_status(int count, int scr_width)
bits_per_int = sizeof(int)*8 bits_per_int = sizeof(int)*8
}; };
procps_status_t *s = top; top_status_t *s = top;
char rss_str_buf[8]; char rss_str_buf[8];
unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */ unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
unsigned pmem_shift, pmem_scale; unsigned pmem_shift, pmem_scale;
@ -333,14 +353,19 @@ static void display_status(int count, int scr_width)
sprintf(rss_str_buf, "%6ldM", s->rss/1024); sprintf(rss_str_buf, "%6ldM", s->rss/1024);
else else
sprintf(rss_str_buf, "%7ld", s->rss); sprintf(rss_str_buf, "%7ld", s->rss);
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);) USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(
col -= printf("\n%5u %-8s %s %s%6u%3u.%c" \ pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ", )
(unsigned)s->pid, s->user, s->state, rss_str_buf, (unsigned)s->ppid, col -= printf("\n%5u %-8s %s "
"%s%6u"
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c")
"%3u.%c ",
s->pid, get_cached_username(s->uid), s->state,
rss_str_buf, s->ppid,
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,) USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
pmem.quot, '0'+pmem.rem); pmem.quot, '0'+pmem.rem);
if (col > 0) if (col > 0)
printf("%.*s", col, s->short_cmd); printf("%.*s", col, s->comm);
/* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
s++; s++;
@ -350,18 +375,20 @@ static void display_status(int count, int scr_width)
fflush(stdout); fflush(stdout);
} }
static void clearmems(void) static void clearmems(void)
{ {
clear_username_cache();
free(top); free(top);
top = 0; top = 0;
ntop = 0; ntop = 0;
} }
#if ENABLE_FEATURE_USE_TERMIOS #if ENABLE_FEATURE_USE_TERMIOS
#include <termios.h> #include <termios.h>
#include <signal.h> #include <signal.h>
static struct termios initial_settings; static struct termios initial_settings;
static void reset_term(void) static void reset_term(void)
@ -427,7 +454,7 @@ int top_main(int argc, char **argv)
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
while (1) { while (1) {
procps_status_t *p; procps_status_t *p = NULL;
/* Default to 25 lines - 5 lines for status */ /* Default to 25 lines - 5 lines for status */
lines = 24 - 3; lines = 24 - 3;
@ -442,11 +469,26 @@ int top_main(int argc, char **argv)
#endif /* FEATURE_USE_TERMIOS */ #endif /* FEATURE_USE_TERMIOS */
/* read process IDs & status for all the processes */ /* read process IDs & status for all the processes */
while ((p = procps_scan(0)) != 0) { while ((p = procps_scan(p, 0
| PSSCAN_PID
| PSSCAN_PPID
| PSSCAN_RSS
| PSSCAN_STIME
| PSSCAN_UTIME
| PSSCAN_STATE
| PSSCAN_COMM
| PSSCAN_SID
| PSSCAN_UIDGID
))) {
int n = ntop; int n = ntop;
top = xrealloc(top, (++ntop)*sizeof(top_status_t));
top = xrealloc(top, (++ntop)*sizeof(procps_status_t)); top[n].pid = p->pid;
memcpy(top + n, p, sizeof(procps_status_t)); top[n].ppid = p->ppid;
top[n].rss = p->rss;
top[n].ticks = p->stime + p->utime;
top[n].uid = p->uid;
strcpy(top[n].state, p->state);
strcpy(top[n].comm, p->comm);
} }
if (ntop == 0) { if (ntop == 0) {
bb_error_msg_and_die("can't find process info in /proc"); bb_error_msg_and_die("can't find process info in /proc");
@ -459,9 +501,9 @@ int top_main(int argc, char **argv)
continue; continue;
} }
do_stats(); do_stats();
qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp); qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
#else #else
qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
count = lines; count = lines;
if (OPT_BATCH_MODE || count > ntop) { if (OPT_BATCH_MODE || count > ntop) {