2006-04-12 18:39:58 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-09-17 22:14:58 +00:00
|
|
|
/*
|
|
|
|
* A tiny 'top' utility.
|
|
|
|
*
|
2002-09-30 20:52:10 +00:00
|
|
|
* This is written specifically for the linux /proc/<PID>/stat(m)
|
|
|
|
* files format.
|
|
|
|
|
|
|
|
* This reads the PIDs of all processes and their status and shows
|
|
|
|
* the status of processes (first ones that fit to screen) at given
|
|
|
|
* intervals.
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2002-09-17 22:14:58 +00:00
|
|
|
* NOTES:
|
|
|
|
* - At startup this changes to /proc, all the reads are then
|
|
|
|
* relative to that.
|
2004-03-15 08:29:22 +00:00
|
|
|
*
|
2002-09-17 22:14:58 +00:00
|
|
|
* (C) Eero Tamminen <oak at welho dot com>
|
2002-09-30 20:52:10 +00:00
|
|
|
*
|
2004-04-14 17:51:38 +00:00
|
|
|
* Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru>
|
2002-09-30 20:52:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Original code Copyrights */
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1992 Branko Lankester
|
|
|
|
* Copyright (c) 1992 Roger Binns
|
|
|
|
* Copyright (C) 1994-1996 Charles L. Blake.
|
|
|
|
* Copyright (C) 1992-1998 Michael K. Johnson
|
|
|
|
* May be distributed under the conditions of the
|
|
|
|
* GNU Library General Public License
|
2002-09-17 22:14:58 +00:00
|
|
|
*/
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-06-02 20:56:16 +00:00
|
|
|
#include "busybox.h"
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
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;
|
2002-09-30 20:52:10 +00:00
|
|
|
static int ntop;
|
2006-11-05 00:43:51 +00:00
|
|
|
/* This structure stores some critical information from one frame to
|
|
|
|
the next. Used for finding deltas. */
|
|
|
|
struct save_hist {
|
|
|
|
unsigned long ticks;
|
|
|
|
unsigned pid;
|
|
|
|
};
|
2006-12-30 17:57:03 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-11-05 00:43:51 +00:00
|
|
|
static struct save_hist *prev_hist;
|
|
|
|
static int prev_hist_count;
|
|
|
|
/* static int hist_iterations; */
|
|
|
|
static unsigned total_pcpu;
|
|
|
|
/* static unsigned long total_rss; */
|
2006-12-30 17:57:03 +00:00
|
|
|
#endif
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2006-10-06 09:49:47 +00:00
|
|
|
#define OPT_BATCH_MODE (option_mask32 & 0x4)
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2006-11-05 00:43:51 +00:00
|
|
|
static int pid_sort(top_status_t *P, top_status_t *Q)
|
2002-09-30 20:52:10 +00:00
|
|
|
{
|
2006-11-05 00:43:51 +00:00
|
|
|
/* Buggy wrt pids with high bit set */
|
|
|
|
/* (linux pids are in [1..2^15-1]) */
|
2006-02-13 22:04:27 +00:00
|
|
|
return (Q->pid - P->pid);
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
2005-07-30 09:42:05 +00:00
|
|
|
#endif
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
static int mem_sort(top_status_t *P, top_status_t *Q)
|
2002-09-30 20:52:10 +00:00
|
|
|
{
|
2006-11-05 00:43:51 +00:00
|
|
|
/* 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 > */
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
|
|
|
|
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
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-11-05 00:38:51 +00:00
|
|
|
enum { SORT_DEPTH = 3 };
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
static cmp_funcp sort_function[SORT_DEPTH];
|
|
|
|
|
|
|
|
static int pcpu_sort(top_status_t *P, top_status_t *Q)
|
2002-09-30 20:52:10 +00:00
|
|
|
{
|
2006-11-05 00:43:51 +00:00
|
|
|
/* Buggy wrt ticks with high bit set */
|
|
|
|
/* Affects only processes for which ticks overflow */
|
|
|
|
return (int)Q->pcpu - (int)P->pcpu;
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
static int time_sort(top_status_t *P, top_status_t *Q)
|
2002-09-30 20:52:10 +00:00
|
|
|
{
|
2006-11-05 00:43:51 +00:00
|
|
|
/* 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 > */
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 19:39:00 +00:00
|
|
|
static int mult_lvl_cmp(void* a, void* b) {
|
2006-02-13 22:04:27 +00:00
|
|
|
int i, cmp_val;
|
|
|
|
|
2006-11-05 00:38:51 +00:00
|
|
|
for (i = 0; i < SORT_DEPTH; i++) {
|
2006-02-13 22:04:27 +00:00
|
|
|
cmp_val = (*sort_function[i])(a, b);
|
|
|
|
if (cmp_val != 0)
|
|
|
|
return cmp_val;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2006-02-13 22:04:27 +00:00
|
|
|
|
2006-11-05 00:38:51 +00:00
|
|
|
typedef struct {
|
2006-04-24 23:13:46 +00:00
|
|
|
unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
|
|
|
|
unsigned long long total;
|
|
|
|
unsigned long long busy;
|
2006-11-05 00:38:51 +00:00
|
|
|
} jiffy_counts_t;
|
|
|
|
static jiffy_counts_t jif, prev_jif;
|
2006-04-24 23:13:46 +00:00
|
|
|
static void get_jiffy_counts(void)
|
2006-02-13 22:04:27 +00:00
|
|
|
{
|
2006-08-03 15:41:12 +00:00
|
|
|
FILE* fp = xfopen("stat", "r");
|
2006-04-24 23:13:46 +00:00
|
|
|
prev_jif = jif;
|
|
|
|
if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
|
|
|
|
&jif.usr,&jif.nic,&jif.sys,&jif.idle,
|
|
|
|
&jif.iowait,&jif.irq,&jif.softirq,&jif.steal) < 4) {
|
2006-09-09 12:55:02 +00:00
|
|
|
bb_error_msg_and_die("failed to read /proc/stat");
|
2006-02-13 22:04:27 +00:00
|
|
|
}
|
2006-04-24 23:13:46 +00:00
|
|
|
fclose(fp);
|
|
|
|
jif.total = jif.usr + jif.nic + jif.sys + jif.idle
|
|
|
|
+ jif.iowait + jif.irq + jif.softirq + jif.steal;
|
|
|
|
/* procps 2.x does not count iowait as busy time */
|
|
|
|
jif.busy = jif.total - jif.idle - jif.iowait;
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2002-09-30 20:52:10 +00:00
|
|
|
static void do_stats(void)
|
|
|
|
{
|
2006-11-05 00:43:51 +00:00
|
|
|
top_status_t *cur;
|
2006-11-01 09:16:49 +00:00
|
|
|
pid_t pid;
|
2006-11-05 00:43:51 +00:00
|
|
|
int i, last_i, n;
|
2006-04-24 23:13:46 +00:00
|
|
|
struct save_hist *new_hist;
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-04-24 23:13:46 +00:00
|
|
|
get_jiffy_counts();
|
|
|
|
total_pcpu = 0;
|
|
|
|
/* total_rss = 0; */
|
|
|
|
new_hist = xmalloc(sizeof(struct save_hist)*ntop);
|
2006-02-13 22:04:27 +00:00
|
|
|
/*
|
|
|
|
* Make a pass through the data to get stats.
|
|
|
|
*/
|
2006-04-24 23:13:46 +00:00
|
|
|
/* hist_iterations = 0; */
|
|
|
|
i = 0;
|
|
|
|
for (n = 0; n < ntop; n++) {
|
2006-02-13 22:04:27 +00:00
|
|
|
cur = top + n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate time in cur process. Time is sum of user time
|
2006-04-24 23:13:46 +00:00
|
|
|
* and system time
|
2006-02-13 22:04:27 +00:00
|
|
|
*/
|
|
|
|
pid = cur->pid;
|
2006-11-05 00:43:51 +00:00
|
|
|
new_hist[n].ticks = cur->ticks;
|
2006-04-24 23:13:46 +00:00
|
|
|
new_hist[n].pid = pid;
|
2006-02-13 22:04:27 +00:00
|
|
|
|
|
|
|
/* find matching entry from previous pass */
|
2006-04-24 23:13:46 +00:00
|
|
|
cur->pcpu = 0;
|
|
|
|
/* do not start at index 0, continue at last used one
|
|
|
|
* (brought hist_iterations from ~14000 down to 172) */
|
|
|
|
last_i = i;
|
|
|
|
if (prev_hist_count) do {
|
|
|
|
if (prev_hist[i].pid == pid) {
|
2006-11-05 00:43:51 +00:00
|
|
|
cur->pcpu = cur->ticks - prev_hist[i].ticks;
|
|
|
|
total_pcpu += cur->pcpu;
|
2006-02-13 22:04:27 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-04-24 23:13:46 +00:00
|
|
|
i = (i+1) % prev_hist_count;
|
|
|
|
/* hist_iterations++; */
|
|
|
|
} while (i != last_i);
|
|
|
|
/* total_rss += cur->rss; */
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-02-13 22:04:27 +00:00
|
|
|
* Save cur frame's information.
|
2002-09-30 20:52:10 +00:00
|
|
|
*/
|
2006-04-24 23:13:46 +00:00
|
|
|
free(prev_hist);
|
|
|
|
prev_hist = new_hist;
|
|
|
|
prev_hist_count = ntop;
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2002-09-17 22:14:58 +00:00
|
|
|
/* display generic info (meminfo / loadavg) */
|
2006-04-24 23:13:46 +00:00
|
|
|
static unsigned long display_generic(int scr_width)
|
2002-09-17 22:14:58 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buf[80];
|
2006-04-24 23:13:46 +00:00
|
|
|
char scrbuf[80];
|
|
|
|
char *end;
|
2002-09-17 22:14:58 +00:00
|
|
|
unsigned long total, used, mfree, shared, buffers, cached;
|
2003-10-11 18:47:20 +00:00
|
|
|
unsigned int needs_conversion = 1;
|
2002-09-17 22:14:58 +00:00
|
|
|
|
|
|
|
/* read memory info */
|
2006-08-03 15:41:12 +00:00
|
|
|
fp = xfopen("meminfo", "r");
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2003-10-11 18:47:20 +00:00
|
|
|
/*
|
|
|
|
* Old kernels (such as 2.4.x) had a nice summary of memory info that
|
|
|
|
* we could parse, however this is gone entirely in 2.6. Try parsing
|
|
|
|
* the old way first, and if that fails, parse each field manually.
|
|
|
|
*
|
|
|
|
* First, we read in the first line. Old kernels will have bogus
|
|
|
|
* strings we don't care about, whereas new kernels will start right
|
|
|
|
* out with MemTotal:
|
2005-11-29 12:32:33 +00:00
|
|
|
* -- PFM.
|
2003-10-11 18:47:20 +00:00
|
|
|
*/
|
|
|
|
if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
|
2005-11-29 12:32:33 +00:00
|
|
|
fgets(buf, sizeof(buf), fp); /* skip first line */
|
2003-10-11 18:47:20 +00:00
|
|
|
|
|
|
|
fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
|
|
|
|
&total, &used, &mfree, &shared, &buffers, &cached);
|
|
|
|
} else {
|
2004-03-15 08:29:22 +00:00
|
|
|
/*
|
2003-10-11 18:47:20 +00:00
|
|
|
* Revert to manual parsing, which incidentally already has the
|
|
|
|
* sizes in kilobytes. This should be safe for both 2.4 and
|
|
|
|
* 2.6.
|
|
|
|
*/
|
|
|
|
needs_conversion = 0;
|
|
|
|
|
|
|
|
fscanf(fp, "MemFree: %lu %s\n", &mfree, buf);
|
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
/*
|
2003-10-11 18:47:20 +00:00
|
|
|
* MemShared: is no longer present in 2.6. Report this as 0,
|
|
|
|
* to maintain consistent behavior with normal procps.
|
|
|
|
*/
|
|
|
|
if (fscanf(fp, "MemShared: %lu %s\n", &shared, buf) != 2)
|
|
|
|
shared = 0;
|
|
|
|
|
|
|
|
fscanf(fp, "Buffers: %lu %s\n", &buffers, buf);
|
|
|
|
fscanf(fp, "Cached: %lu %s\n", &cached, buf);
|
|
|
|
|
|
|
|
used = total - mfree;
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
|
|
|
fclose(fp);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2006-04-24 23:13:46 +00:00
|
|
|
/* read load average as a string */
|
|
|
|
buf[0] = '\0';
|
2006-10-27 09:34:22 +00:00
|
|
|
open_read_close("loadavg", buf, sizeof(buf));
|
2006-04-24 23:13:46 +00:00
|
|
|
end = strchr(buf, ' ');
|
|
|
|
if (end) end = strchr(end+1, ' ');
|
|
|
|
if (end) end = strchr(end+1, ' ');
|
|
|
|
if (end) *end = '\0';
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2003-10-11 18:47:20 +00:00
|
|
|
if (needs_conversion) {
|
|
|
|
/* convert to kilobytes */
|
|
|
|
used /= 1024;
|
|
|
|
mfree /= 1024;
|
|
|
|
shared /= 1024;
|
|
|
|
buffers /= 1024;
|
|
|
|
cached /= 1024;
|
|
|
|
total /= 1024;
|
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2002-09-17 22:14:58 +00:00
|
|
|
/* output memory info and load average */
|
2002-09-30 20:52:10 +00:00
|
|
|
/* clear screen & go to top */
|
2006-04-24 23:13:46 +00:00
|
|
|
if (scr_width > sizeof(scrbuf))
|
|
|
|
scr_width = sizeof(scrbuf);
|
|
|
|
snprintf(scrbuf, scr_width,
|
|
|
|
"Mem: %ldK used, %ldK free, %ldK shrd, %ldK buff, %ldK cached",
|
2006-02-13 22:04:27 +00:00
|
|
|
used, mfree, shared, buffers, cached);
|
2006-09-29 17:16:39 +00:00
|
|
|
|
|
|
|
printf(OPT_BATCH_MODE ? "%s\n" : "\e[H\e[J%s\n", scrbuf);
|
2007-01-11 17:20:00 +00:00
|
|
|
|
2006-10-27 09:34:22 +00:00
|
|
|
snprintf(scrbuf, scr_width, "Load average: %s", buf);
|
2006-04-24 23:13:46 +00:00
|
|
|
printf("%s\n", scrbuf);
|
|
|
|
|
2003-10-11 18:47:20 +00:00
|
|
|
return total;
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* display process statuses */
|
2006-04-24 23:13:46 +00:00
|
|
|
static void display_status(int count, int scr_width)
|
2002-09-17 22:14:58 +00:00
|
|
|
{
|
2006-04-24 23:13:46 +00:00
|
|
|
enum {
|
|
|
|
bits_per_int = sizeof(int)*8
|
|
|
|
};
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
top_status_t *s = top;
|
2002-09-30 20:52:10 +00:00
|
|
|
char rss_str_buf[8];
|
2006-04-24 23:13:46 +00:00
|
|
|
unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
|
|
|
|
unsigned pmem_shift, pmem_scale;
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-24 23:13:46 +00:00
|
|
|
unsigned pcpu_shift, pcpu_scale;
|
2006-11-05 00:38:51 +00:00
|
|
|
unsigned busy_jifs;
|
2006-04-24 23:13:46 +00:00
|
|
|
|
2002-09-17 22:14:58 +00:00
|
|
|
/* what info of the processes is shown */
|
2006-09-29 17:16:39 +00:00
|
|
|
printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
|
2006-04-24 23:13:46 +00:00
|
|
|
" PID USER STATUS RSS PPID %CPU %MEM COMMAND");
|
|
|
|
#define MIN_WIDTH \
|
|
|
|
sizeof( " PID USER STATUS RSS PPID %CPU %MEM C")
|
2002-09-30 20:52:10 +00:00
|
|
|
#else
|
2006-09-29 17:16:39 +00:00
|
|
|
printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
|
2006-04-24 23:13:46 +00:00
|
|
|
" PID USER STATUS RSS PPID %MEM COMMAND");
|
|
|
|
#define MIN_WIDTH \
|
|
|
|
sizeof( " PID USER STATUS RSS PPID %MEM C")
|
2002-09-30 20:52:10 +00:00
|
|
|
#endif
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2006-04-24 23:13:46 +00:00
|
|
|
/*
|
|
|
|
* MEM% = s->rss/MemTotal
|
|
|
|
*/
|
|
|
|
pmem_shift = bits_per_int-11;
|
|
|
|
pmem_scale = 1000*(1U<<(bits_per_int-11)) / total_memory;
|
|
|
|
/* s->rss is in kb. we want (s->rss * pmem_scale) to never overflow */
|
|
|
|
while (pmem_scale >= 512) {
|
|
|
|
pmem_scale /= 4;
|
|
|
|
pmem_shift -= 2;
|
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
|
|
busy_jifs = jif.busy - prev_jif.busy;
|
|
|
|
/* This happens if there were lots of short-lived processes
|
|
|
|
* between two top updates (e.g. compilation) */
|
|
|
|
if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
|
|
|
|
|
2006-04-24 23:13:46 +00:00
|
|
|
/*
|
|
|
|
* CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
|
|
|
|
* (pcpu is delta of sys+user time between samples)
|
|
|
|
*/
|
|
|
|
/* (jif.xxx - prev_jif.xxx) and s->pcpu are
|
|
|
|
* in 0..~64000 range (HZ*update_interval).
|
|
|
|
* we assume that unsigned is at least 32-bit.
|
|
|
|
*/
|
|
|
|
pcpu_shift = 6;
|
2006-11-05 00:38:51 +00:00
|
|
|
pcpu_scale = (1000*64*(uint16_t)busy_jifs ? : 1);
|
2006-04-24 23:13:46 +00:00
|
|
|
while (pcpu_scale < (1U<<(bits_per_int-2))) {
|
|
|
|
pcpu_scale *= 4;
|
|
|
|
pcpu_shift += 2;
|
|
|
|
}
|
|
|
|
pcpu_scale /= ( (uint16_t)(jif.total-prev_jif.total)*total_pcpu ? : 1);
|
|
|
|
/* we want (s->pcpu * pcpu_scale) to never overflow */
|
|
|
|
while (pcpu_scale >= 1024) {
|
|
|
|
pcpu_scale /= 4;
|
|
|
|
pcpu_shift -= 2;
|
|
|
|
}
|
|
|
|
/* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
|
|
|
|
#endif
|
2006-09-29 17:16:39 +00:00
|
|
|
while (count-- > 0) {
|
|
|
|
div_t pmem = div((s->rss*pmem_scale) >> pmem_shift, 10);
|
2006-04-24 23:13:46 +00:00
|
|
|
int col = scr_width+1;
|
2006-05-19 12:48:56 +00:00
|
|
|
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2006-04-24 23:13:46 +00:00
|
|
|
if (s->rss >= 100*1024)
|
2002-09-30 20:52:10 +00:00
|
|
|
sprintf(rss_str_buf, "%6ldM", s->rss/1024);
|
2002-09-18 19:27:10 +00:00
|
|
|
else
|
2002-09-30 20:52:10 +00:00
|
|
|
sprintf(rss_str_buf, "%7ld", s->rss);
|
2006-11-05 00:43:51 +00:00
|
|
|
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(
|
|
|
|
pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);
|
|
|
|
)
|
|
|
|
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,
|
2006-05-04 19:51:22 +00:00
|
|
|
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
|
|
|
|
pmem.quot, '0'+pmem.rem);
|
2006-10-27 09:34:22 +00:00
|
|
|
if (col > 0)
|
2006-11-05 00:43:51 +00:00
|
|
|
printf("%.*s", col, s->comm);
|
2006-05-19 12:48:56 +00:00
|
|
|
/* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
|
2006-04-24 23:13:46 +00:00
|
|
|
jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
|
2002-09-30 20:52:10 +00:00
|
|
|
s++;
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
2006-04-24 23:13:46 +00:00
|
|
|
/* printf(" %d", hist_iterations); */
|
2006-09-29 17:16:39 +00:00
|
|
|
putchar(OPT_BATCH_MODE ? '\n' : '\r');
|
2006-04-24 23:13:46 +00:00
|
|
|
fflush(stdout);
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2002-10-22 12:21:15 +00:00
|
|
|
static void clearmems(void)
|
2002-09-17 22:14:58 +00:00
|
|
|
{
|
2006-11-05 00:43:51 +00:00
|
|
|
clear_username_cache();
|
2002-09-30 20:52:10 +00:00
|
|
|
free(top);
|
|
|
|
top = 0;
|
|
|
|
ntop = 0;
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2002-09-30 20:52:10 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <signal.h>
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2002-09-30 20:52:10 +00:00
|
|
|
static struct termios initial_settings;
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2002-09-30 20:52:10 +00:00
|
|
|
static void reset_term(void)
|
2002-09-17 22:14:58 +00:00
|
|
|
{
|
2002-09-30 20:52:10 +00:00
|
|
|
tcsetattr(0, TCSANOW, (void *) &initial_settings);
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_CLEAN_UP
|
2002-09-30 20:52:10 +00:00
|
|
|
clearmems();
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-24 23:13:46 +00:00
|
|
|
free(prev_hist);
|
2002-09-30 20:52:10 +00:00
|
|
|
#endif
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_CLEAN_UP */
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2006-02-13 22:04:27 +00:00
|
|
|
static void sig_catcher(int sig ATTRIBUTE_UNUSED)
|
2002-09-30 20:52:10 +00:00
|
|
|
{
|
|
|
|
reset_term();
|
2006-06-28 14:11:25 +00:00
|
|
|
exit(1);
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2002-09-17 22:14:58 +00:00
|
|
|
|
|
|
|
int top_main(int argc, char **argv)
|
|
|
|
{
|
2006-09-29 17:16:39 +00:00
|
|
|
int count, lines, col;
|
2006-10-08 12:49:22 +00:00
|
|
|
unsigned interval = 5; /* default update rate is 5 seconds */
|
|
|
|
unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */
|
2006-09-29 17:16:39 +00:00
|
|
|
char *sinterval, *siterations;
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2002-09-30 20:52:10 +00:00
|
|
|
struct termios new_settings;
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set readfds;
|
|
|
|
unsigned char c;
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2002-09-17 22:14:58 +00:00
|
|
|
/* do normal option parsing */
|
2006-09-09 12:55:02 +00:00
|
|
|
interval = 5;
|
2006-10-03 21:00:06 +00:00
|
|
|
opt_complementary = "-";
|
2006-11-05 00:38:51 +00:00
|
|
|
getopt32(argc, argv, "d:n:b", &sinterval, &siterations);
|
2006-10-08 12:49:22 +00:00
|
|
|
if (option_mask32 & 0x1) interval = xatou(sinterval); // -d
|
|
|
|
if (option_mask32 & 0x2) iterations = xatou(siterations); // -n
|
2006-10-06 09:49:47 +00:00
|
|
|
//if (option_mask32 & 0x4) // -b
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2002-10-22 12:21:15 +00:00
|
|
|
/* change to /proc */
|
2006-08-03 15:41:12 +00:00
|
|
|
xchdir("/proc");
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2002-09-30 20:52:10 +00:00
|
|
|
tcgetattr(0, (void *) &initial_settings);
|
|
|
|
memcpy(&new_settings, &initial_settings, sizeof(struct termios));
|
2006-09-09 12:55:02 +00:00
|
|
|
/* unbuffered input, turn off echo */
|
|
|
|
new_settings.c_lflag &= ~(ISIG | ICANON | ECHO | ECHONL);
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2006-04-24 23:13:46 +00:00
|
|
|
signal(SIGTERM, sig_catcher);
|
2006-06-28 14:11:25 +00:00
|
|
|
signal(SIGINT, sig_catcher);
|
2002-09-30 20:52:10 +00:00
|
|
|
tcsetattr(0, TCSANOW, (void *) &new_settings);
|
|
|
|
atexit(reset_term);
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2005-07-30 09:42:05 +00:00
|
|
|
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = pcpu_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
#else
|
|
|
|
sort_function = mem_sort;
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
2005-07-30 09:42:05 +00:00
|
|
|
|
2002-09-30 20:52:10 +00:00
|
|
|
while (1) {
|
2006-11-05 00:43:51 +00:00
|
|
|
procps_status_t *p = NULL;
|
2002-10-22 12:21:15 +00:00
|
|
|
|
2006-04-24 23:13:46 +00:00
|
|
|
/* Default to 25 lines - 5 lines for status */
|
|
|
|
lines = 24 - 3;
|
|
|
|
col = 79;
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2006-04-24 23:13:46 +00:00
|
|
|
get_terminal_width_height(0, &col, &lines);
|
|
|
|
if (lines < 5 || col < MIN_WIDTH) {
|
|
|
|
sleep(interval);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lines -= 3;
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2006-04-24 23:13:46 +00:00
|
|
|
|
|
|
|
/* read process IDs & status for all the processes */
|
2006-11-05 00:43:51 +00:00
|
|
|
while ((p = procps_scan(p, 0
|
|
|
|
| PSSCAN_PID
|
|
|
|
| PSSCAN_PPID
|
|
|
|
| PSSCAN_RSS
|
|
|
|
| PSSCAN_STIME
|
|
|
|
| PSSCAN_UTIME
|
|
|
|
| PSSCAN_STATE
|
|
|
|
| PSSCAN_COMM
|
|
|
|
| PSSCAN_SID
|
|
|
|
| PSSCAN_UIDGID
|
|
|
|
))) {
|
2002-10-22 12:21:15 +00:00
|
|
|
int n = ntop;
|
2006-11-05 00:43:51 +00:00
|
|
|
top = xrealloc(top, (++ntop)*sizeof(top_status_t));
|
|
|
|
top[n].pid = p->pid;
|
|
|
|
top[n].ppid = p->ppid;
|
|
|
|
top[n].rss = p->rss;
|
2006-12-30 17:57:03 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-11-05 00:43:51 +00:00
|
|
|
top[n].ticks = p->stime + p->utime;
|
2006-12-30 17:57:03 +00:00
|
|
|
#endif
|
2006-11-05 00:43:51 +00:00
|
|
|
top[n].uid = p->uid;
|
|
|
|
strcpy(top[n].state, p->state);
|
|
|
|
strcpy(top[n].comm, p->comm);
|
2002-10-22 12:21:15 +00:00
|
|
|
}
|
|
|
|
if (ntop == 0) {
|
2006-10-14 02:23:43 +00:00
|
|
|
bb_error_msg_and_die("can't find process info in /proc");
|
2006-02-13 22:04:27 +00:00
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-24 23:13:46 +00:00
|
|
|
if (!prev_hist_count) {
|
2002-09-30 20:52:10 +00:00
|
|
|
do_stats();
|
|
|
|
sleep(1);
|
|
|
|
clearmems();
|
|
|
|
continue;
|
2006-02-13 22:04:27 +00:00
|
|
|
}
|
2002-09-30 20:52:10 +00:00
|
|
|
do_stats();
|
2006-11-05 00:43:51 +00:00
|
|
|
qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
|
2002-09-30 20:52:10 +00:00
|
|
|
#else
|
2006-11-05 00:43:51 +00:00
|
|
|
qsort(top, ntop, sizeof(top_status_t), (void*)sort_function);
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
2006-09-29 17:16:39 +00:00
|
|
|
count = lines;
|
2006-10-27 09:34:22 +00:00
|
|
|
if (OPT_BATCH_MODE || count > ntop) {
|
2006-09-29 17:16:39 +00:00
|
|
|
count = ntop;
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
/* show status for each of the processes */
|
2006-09-29 17:16:39 +00:00
|
|
|
display_status(count, col);
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2002-09-30 20:52:10 +00:00
|
|
|
tv.tv_sec = interval;
|
|
|
|
tv.tv_usec = 0;
|
2006-04-24 23:13:46 +00:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(0, &readfds);
|
|
|
|
select(1, &readfds, NULL, NULL, &tv);
|
|
|
|
if (FD_ISSET(0, &readfds)) {
|
|
|
|
if (read(0, &c, 1) <= 0) { /* signal */
|
2005-07-30 09:42:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2006-04-24 23:13:46 +00:00
|
|
|
if (c == 'q' || c == initial_settings.c_cc[VINTR])
|
2006-02-15 17:15:56 +00:00
|
|
|
break;
|
2006-04-24 23:13:46 +00:00
|
|
|
if (c == 'M') {
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = mem_sort;
|
|
|
|
sort_function[1] = pcpu_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
#else
|
|
|
|
sort_function = mem_sort;
|
|
|
|
#endif
|
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-24 23:13:46 +00:00
|
|
|
if (c == 'P') {
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = pcpu_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
}
|
2006-04-24 23:13:46 +00:00
|
|
|
if (c == 'T') {
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = time_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = pcpu_sort;
|
|
|
|
}
|
|
|
|
#endif
|
2006-04-24 23:13:46 +00:00
|
|
|
if (c == 'N') {
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = pid_sort;
|
|
|
|
#else
|
|
|
|
sort_function = pid_sort;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2006-09-29 17:16:39 +00:00
|
|
|
if (!--iterations)
|
|
|
|
break;
|
2002-09-30 20:52:10 +00:00
|
|
|
#else
|
2002-09-17 22:14:58 +00:00
|
|
|
sleep(interval);
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2002-09-30 20:52:10 +00:00
|
|
|
clearmems();
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
2006-04-24 23:13:46 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
2006-02-15 17:15:56 +00:00
|
|
|
clearmems();
|
|
|
|
putchar('\n');
|
2002-09-17 22:14:58 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|