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
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2007-04-19 14:47:11 +00:00
|
|
|
typedef struct top_status_t {
|
2007-02-08 08:21:58 +00:00
|
|
|
unsigned long vsz;
|
2006-11-05 00:43:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
|
|
unsigned long ticks;
|
|
|
|
unsigned pcpu; /* delta of ticks */
|
|
|
|
#endif
|
|
|
|
unsigned pid, ppid;
|
|
|
|
unsigned uid;
|
|
|
|
char state[4];
|
2007-06-30 14:47:41 +00:00
|
|
|
char comm[COMM_LEN];
|
2006-11-05 00:43:51 +00:00
|
|
|
} top_status_t;
|
2007-04-19 14:47:11 +00:00
|
|
|
|
2007-06-30 14:47:41 +00:00
|
|
|
typedef struct jiffy_counts_t {
|
2007-04-19 14:47:11 +00:00
|
|
|
unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
|
|
|
|
unsigned long long total;
|
|
|
|
unsigned long long busy;
|
|
|
|
} jiffy_counts_t;
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
/* This structure stores some critical information from one frame to
|
|
|
|
the next. Used for finding deltas. */
|
2007-04-19 14:47:11 +00:00
|
|
|
typedef struct save_hist {
|
2006-11-05 00:43:51 +00:00
|
|
|
unsigned long ticks;
|
2008-05-15 21:30:45 +00:00
|
|
|
pid_t pid;
|
2007-04-19 14:47:11 +00:00
|
|
|
} save_hist;
|
|
|
|
|
|
|
|
typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q);
|
|
|
|
|
2007-09-08 16:51:19 +00:00
|
|
|
|
2007-04-19 14:47:11 +00:00
|
|
|
enum { SORT_DEPTH = 3 };
|
|
|
|
|
2007-09-08 16:51:19 +00:00
|
|
|
|
2007-04-19 14:47:11 +00:00
|
|
|
struct globals {
|
|
|
|
top_status_t *top;
|
|
|
|
int ntop;
|
2007-09-08 16:51:19 +00:00
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
|
|
|
smallint sort_field;
|
|
|
|
smallint inverted;
|
|
|
|
#endif
|
2007-04-19 14:47:11 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
|
|
|
struct termios initial_settings;
|
|
|
|
#endif
|
|
|
|
#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2007-08-29 18:18:08 +00:00
|
|
|
cmp_funcp sort_function[1];
|
2007-04-19 14:47:11 +00:00
|
|
|
#else
|
|
|
|
cmp_funcp sort_function[SORT_DEPTH];
|
|
|
|
struct save_hist *prev_hist;
|
|
|
|
int prev_hist_count;
|
|
|
|
jiffy_counts_t jif, prev_jif;
|
|
|
|
/* int hist_iterations; */
|
|
|
|
unsigned total_pcpu;
|
|
|
|
/* unsigned long total_vsz; */
|
|
|
|
#endif
|
2007-10-16 22:53:05 +00:00
|
|
|
char line_buf[80];
|
2006-11-05 00:43:51 +00:00
|
|
|
};
|
2007-10-16 22:53:05 +00:00
|
|
|
|
2007-09-08 17:34:05 +00:00
|
|
|
enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) };
|
2007-10-16 22:53:05 +00:00
|
|
|
|
2007-04-19 14:47:11 +00:00
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
2008-06-25 09:53:17 +00:00
|
|
|
#define INIT_G() do { \
|
|
|
|
struct G_sizecheck { \
|
|
|
|
char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
|
|
|
|
}; \
|
|
|
|
} while (0)
|
2007-04-19 14:47:11 +00:00
|
|
|
#define top (G.top )
|
|
|
|
#define ntop (G.ntop )
|
2007-09-08 16:51:19 +00:00
|
|
|
#define sort_field (G.sort_field )
|
|
|
|
#define inverted (G.inverted )
|
|
|
|
#define initial_settings (G.initial_settings )
|
2007-04-19 14:47:11 +00:00
|
|
|
#define sort_function (G.sort_function )
|
|
|
|
#define prev_hist (G.prev_hist )
|
|
|
|
#define prev_hist_count (G.prev_hist_count )
|
|
|
|
#define jif (G.jif )
|
|
|
|
#define prev_jif (G.prev_jif )
|
|
|
|
#define total_pcpu (G.total_pcpu )
|
2007-09-08 17:34:05 +00:00
|
|
|
#define line_buf (G.line_buf )
|
2007-08-29 18:18:08 +00:00
|
|
|
|
2008-03-19 19:38:46 +00:00
|
|
|
enum {
|
|
|
|
OPT_d = (1 << 0),
|
|
|
|
OPT_n = (1 << 1),
|
|
|
|
OPT_b = (1 << 2),
|
|
|
|
OPT_EOF = (1 << 3), /* pseudo: "we saw EOF in stdin" */
|
|
|
|
};
|
|
|
|
#define OPT_BATCH_MODE (option_mask32 & OPT_b)
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2007-04-19 14:47:11 +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 */
|
2007-02-08 08:21:58 +00:00
|
|
|
if (Q->vsz < P->vsz) return -1;
|
|
|
|
return Q->vsz != P->vsz; /* 0 if ==, 1 if > */
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
|
2007-04-19 14:47:11 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-11-05 00:43:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-04-16 22:32:04 +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-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) {
|
2008-03-19 19:38:46 +00:00
|
|
|
bb_error_msg_and_die("can't 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;
|
2007-02-08 08:21:58 +00:00
|
|
|
/* total_vsz = 0; */
|
2006-04-24 23:13:46 +00:00
|
|
|
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);
|
2007-02-08 08:21:58 +00:00
|
|
|
/* total_vsz += cur->vsz; */
|
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
|
|
|
|
2007-08-23 10:52:52 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && ENABLE_FEATURE_TOP_DECIMALS
|
|
|
|
/* formats 7 char string (8 with terminating NUL) */
|
|
|
|
static char *fmt_100percent_8(char pbuf[8], unsigned value, unsigned total)
|
|
|
|
{
|
|
|
|
unsigned t;
|
|
|
|
if (value >= total) { /* 100% ? */
|
|
|
|
strcpy(pbuf, " 100% ");
|
|
|
|
return pbuf;
|
|
|
|
}
|
|
|
|
/* else generate " [N/space]N.N% " string */
|
|
|
|
value = 1000 * value / total;
|
|
|
|
t = value / 100;
|
|
|
|
value = value % 100;
|
|
|
|
pbuf[0] = ' ';
|
|
|
|
pbuf[1] = t ? t + '0' : ' ';
|
|
|
|
pbuf[2] = '0' + (value / 10);
|
|
|
|
pbuf[3] = '.';
|
|
|
|
pbuf[4] = '0' + (value % 10);
|
|
|
|
pbuf[5] = '%';
|
|
|
|
pbuf[6] = ' ';
|
|
|
|
pbuf[7] = '\0';
|
|
|
|
return pbuf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-29 18:34:26 +00:00
|
|
|
static unsigned long display_header(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];
|
2002-09-17 22:14:58 +00:00
|
|
|
unsigned long total, used, mfree, shared, buffers, cached;
|
2007-08-23 10:52:52 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
|
2007-07-15 19:27:48 +00:00
|
|
|
unsigned total_diff;
|
2007-08-17 08:29:48 +00:00
|
|
|
#endif
|
2007-07-15 19:27:48 +00:00
|
|
|
|
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",
|
2007-06-10 17:11:59 +00:00
|
|
|
&total, &used, &mfree, &shared, &buffers, &cached);
|
|
|
|
/* convert to kilobytes */
|
|
|
|
used /= 1024;
|
|
|
|
mfree /= 1024;
|
|
|
|
shared /= 1024;
|
|
|
|
buffers /= 1024;
|
|
|
|
cached /= 1024;
|
|
|
|
total /= 1024;
|
2003-10-11 18:47:20 +00:00
|
|
|
} 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-07-15 19:27:48 +00:00
|
|
|
/* output memory info */
|
2008-05-15 21:30:45 +00:00
|
|
|
if (scr_width > (int)sizeof(scrbuf))
|
2006-04-24 23:13:46 +00:00
|
|
|
scr_width = sizeof(scrbuf);
|
|
|
|
snprintf(scrbuf, scr_width,
|
2007-07-15 19:23:38 +00:00
|
|
|
"Mem: %luK used, %luK free, %luK shrd, %luK buff, %luK cached",
|
2006-02-13 22:04:27 +00:00
|
|
|
used, mfree, shared, buffers, cached);
|
2007-07-15 19:27:48 +00:00
|
|
|
/* clear screen & go to top */
|
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
|
|
|
|
2007-08-17 08:29:48 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS
|
|
|
|
/*
|
|
|
|
* xxx% = (jif.xxx - prev_jif.xxx) / (jif.total - prev_jif.total) * 100%
|
|
|
|
*/
|
|
|
|
/* using (unsigned) casts to make operations cheaper */
|
|
|
|
total_diff = ((unsigned)(jif.total - prev_jif.total) ? : 1);
|
2007-06-11 16:31:55 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_DECIMALS
|
2007-07-15 19:27:48 +00:00
|
|
|
/* Generated code is approx +0.3k */
|
2007-07-15 19:25:01 +00:00
|
|
|
#define CALC_STAT(xxx) char xxx[8]
|
2007-08-23 10:52:52 +00:00
|
|
|
#define SHOW_STAT(xxx) fmt_100percent_8(xxx, (unsigned)(jif.xxx - prev_jif.xxx), total_diff)
|
2007-07-15 19:25:01 +00:00
|
|
|
#define FMT "%s"
|
2007-06-11 16:31:55 +00:00
|
|
|
#else
|
2007-06-10 18:04:54 +00:00
|
|
|
#define CALC_STAT(xxx) unsigned xxx = 100 * (unsigned)(jif.xxx - prev_jif.xxx) / total_diff
|
|
|
|
#define SHOW_STAT(xxx) xxx
|
2007-07-15 19:25:01 +00:00
|
|
|
#define FMT "%4u%% "
|
2007-06-11 16:31:55 +00:00
|
|
|
#endif
|
2007-08-17 08:29:48 +00:00
|
|
|
{ /* need block: CALC_STAT are declarations */
|
|
|
|
CALC_STAT(usr);
|
|
|
|
CALC_STAT(sys);
|
|
|
|
CALC_STAT(nic);
|
|
|
|
CALC_STAT(idle);
|
|
|
|
CALC_STAT(iowait);
|
|
|
|
CALC_STAT(irq);
|
|
|
|
CALC_STAT(softirq);
|
|
|
|
//CALC_STAT(steal);
|
|
|
|
|
|
|
|
snprintf(scrbuf, scr_width,
|
|
|
|
/* Barely fits in 79 chars when in "decimals" mode. */
|
|
|
|
"CPU:"FMT"usr"FMT"sys"FMT"nice"FMT"idle"FMT"io"FMT"irq"FMT"softirq",
|
|
|
|
SHOW_STAT(usr), SHOW_STAT(sys), SHOW_STAT(nic), SHOW_STAT(idle),
|
|
|
|
SHOW_STAT(iowait), SHOW_STAT(irq), SHOW_STAT(softirq)
|
|
|
|
//, SHOW_STAT(steal) - what is this 'steal' thing?
|
|
|
|
// I doubt anyone wants to know it
|
|
|
|
);
|
|
|
|
}
|
|
|
|
puts(scrbuf);
|
2007-06-10 17:11:59 +00:00
|
|
|
#undef SHOW_STAT
|
|
|
|
#undef CALC_STAT
|
2007-06-10 18:04:54 +00:00
|
|
|
#undef FMT
|
2007-08-17 08:29:48 +00:00
|
|
|
#endif
|
2007-06-10 17:11:59 +00:00
|
|
|
|
2007-07-15 19:27:48 +00:00
|
|
|
/* read load average as a string */
|
|
|
|
buf[0] = '\0';
|
|
|
|
open_read_close("loadavg", buf, sizeof("N.NN N.NN N.NN")-1);
|
|
|
|
buf[sizeof("N.NN N.NN N.NN")-1] = '\0';
|
2006-10-27 09:34:22 +00:00
|
|
|
snprintf(scrbuf, scr_width, "Load average: %s", buf);
|
2007-06-10 17:11:59 +00:00
|
|
|
puts(scrbuf);
|
2006-04-24 23:13:46 +00:00
|
|
|
|
2003-10-11 18:47:20 +00:00
|
|
|
return total;
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
|
|
|
|
2007-09-08 17:34:05 +00:00
|
|
|
static NOINLINE void display_process_list(int count, int scr_width)
|
2002-09-17 22:14:58 +00:00
|
|
|
{
|
2006-04-24 23:13:46 +00:00
|
|
|
enum {
|
2007-06-11 16:31:55 +00:00
|
|
|
BITS_PER_INT = sizeof(int)*8
|
2006-04-24 23:13:46 +00:00
|
|
|
};
|
|
|
|
|
2006-11-05 00:43:51 +00:00
|
|
|
top_status_t *s = top;
|
2007-02-08 08:21:58 +00:00
|
|
|
char vsz_str_buf[8];
|
2007-08-29 18:34:26 +00:00
|
|
|
unsigned long total_memory = display_header(scr_width); /* or use total_vsz? */
|
2007-06-11 16:31:55 +00:00
|
|
|
/* xxx_shift and xxx_scale variables allow us to replace
|
|
|
|
* expensive divides with multiply and shift */
|
|
|
|
unsigned pmem_shift, pmem_scale, pmem_half;
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2007-06-11 16:31:55 +00:00
|
|
|
unsigned pcpu_shift, pcpu_scale, pcpu_half;
|
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,
|
2007-06-11 16:31:55 +00:00
|
|
|
" PID PPID USER STAT VSZ %MEM %CPU COMMAND");
|
2002-09-30 20:52:10 +00:00
|
|
|
#else
|
2007-06-11 16:31:55 +00:00
|
|
|
|
|
|
|
/* !CPU_USAGE_PERCENTAGE */
|
2006-09-29 17:16:39 +00:00
|
|
|
printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width,
|
2007-06-11 16:31:55 +00:00
|
|
|
" PID PPID USER STAT VSZ %MEM COMMAND");
|
2002-09-30 20:52:10 +00:00
|
|
|
#endif
|
2002-09-17 22:14:58 +00:00
|
|
|
|
2007-07-15 19:25:01 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_DECIMALS
|
|
|
|
#define UPSCALE 1000
|
|
|
|
#define CALC_STAT(name, val) div_t name = div((val), 10)
|
|
|
|
#define SHOW_STAT(name) name.quot, '0'+name.rem
|
|
|
|
#define FMT "%3u.%c"
|
|
|
|
#else
|
|
|
|
#define UPSCALE 100
|
|
|
|
#define CALC_STAT(name, val) unsigned name = (val)
|
|
|
|
#define SHOW_STAT(name) name
|
|
|
|
#define FMT "%4u%%"
|
|
|
|
#endif
|
2006-04-24 23:13:46 +00:00
|
|
|
/*
|
2007-02-08 08:21:58 +00:00
|
|
|
* MEM% = s->vsz/MemTotal
|
2006-04-24 23:13:46 +00:00
|
|
|
*/
|
2007-06-11 16:31:55 +00:00
|
|
|
pmem_shift = BITS_PER_INT-11;
|
|
|
|
pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
|
2007-02-08 08:21:58 +00:00
|
|
|
/* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
|
2006-04-24 23:13:46 +00:00
|
|
|
while (pmem_scale >= 512) {
|
|
|
|
pmem_scale /= 4;
|
|
|
|
pmem_shift -= 2;
|
|
|
|
}
|
2007-06-11 16:31:55 +00:00
|
|
|
pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 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;
|
2007-06-11 16:31:55 +00:00
|
|
|
pcpu_scale = (UPSCALE*64*(uint16_t)busy_jifs ? : 1);
|
|
|
|
while (pcpu_scale < (1U<<(BITS_PER_INT-2))) {
|
2006-04-24 23:13:46 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-06-11 16:31:55 +00:00
|
|
|
pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS? 20 : 2);
|
2006-04-24 23:13:46 +00:00
|
|
|
/* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
|
|
|
|
#endif
|
2007-06-11 16:31:55 +00:00
|
|
|
|
2007-09-08 16:51:19 +00:00
|
|
|
scr_width += 2; /* account for leading '\n' and trailing NUL */
|
2008-02-11 11:44:38 +00:00
|
|
|
/* Ok, all preliminary data is ready, go through the list */
|
2006-09-29 17:16:39 +00:00
|
|
|
while (count-- > 0) {
|
2007-09-08 16:51:19 +00:00
|
|
|
unsigned col;
|
2007-06-11 16:31:55 +00:00
|
|
|
CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
|
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
|
|
CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
|
|
|
|
#endif
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2007-09-08 16:51:19 +00:00
|
|
|
if (s->vsz >= 100000)
|
2007-08-28 19:35:34 +00:00
|
|
|
sprintf(vsz_str_buf, "%6ldm", s->vsz/1024);
|
2002-09-18 19:27:10 +00:00
|
|
|
else
|
2007-02-08 08:21:58 +00:00
|
|
|
sprintf(vsz_str_buf, "%7ld", s->vsz);
|
2007-06-11 16:31:55 +00:00
|
|
|
// PID PPID USER STAT VSZ %MEM [%CPU] COMMAND
|
2007-09-08 17:34:05 +00:00
|
|
|
col = snprintf(line_buf, scr_width,
|
2007-09-08 16:51:19 +00:00
|
|
|
"\n" "%5u%6u %-8.8s %s%s" FMT
|
2007-06-11 16:31:55 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
|
|
FMT
|
|
|
|
#endif
|
|
|
|
" ",
|
|
|
|
s->pid, s->ppid, get_cached_username(s->uid),
|
|
|
|
s->state, vsz_str_buf,
|
|
|
|
SHOW_STAT(pmem)
|
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
|
|
, SHOW_STAT(pcpu)
|
|
|
|
#endif
|
|
|
|
);
|
2008-05-15 21:30:45 +00:00
|
|
|
if ((int)(col + 1) < scr_width)
|
2007-09-08 17:42:00 +00:00
|
|
|
read_cmdline(line_buf + col, scr_width - col - 1, s->pid, s->comm);
|
2007-09-08 17:34:05 +00:00
|
|
|
fputs(line_buf, stdout);
|
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); */
|
2007-09-27 10:20:47 +00:00
|
|
|
bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
|
2006-04-24 23:13:46 +00:00
|
|
|
fflush(stdout);
|
2002-09-17 22:14:58 +00:00
|
|
|
}
|
2007-07-15 19:25:01 +00:00
|
|
|
#undef UPSCALE
|
2007-06-11 16:31:55 +00:00
|
|
|
#undef SHOW_STAT
|
|
|
|
#undef CALC_STAT
|
|
|
|
#undef FMT
|
2002-09-17 22:14:58 +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);
|
2007-09-08 16:51:19 +00:00
|
|
|
top = NULL;
|
2002-09-30 20:52:10 +00:00
|
|
|
ntop = 0;
|
2002-09-17 22:14:58 +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 void reset_term(void)
|
2002-09-17 22:14:58 +00:00
|
|
|
{
|
2008-03-19 19:38:46 +00:00
|
|
|
tcsetattr(0, TCSANOW, &initial_settings);
|
2007-08-29 18:18:08 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
clearmems();
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2007-08-29 18:18:08 +00:00
|
|
|
free(prev_hist);
|
2002-09-30 20:52:10 +00:00
|
|
|
#endif
|
2007-08-29 18:18:08 +00:00
|
|
|
}
|
2002-09-30 20:52:10 +00:00
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2008-07-05 09:18:54 +00:00
|
|
|
static void sig_catcher(int sig UNUSED_PARAM)
|
2002-09-30 20:52:10 +00:00
|
|
|
{
|
|
|
|
reset_term();
|
2008-05-19 09:29:47 +00:00
|
|
|
exit(EXIT_FAILURE);
|
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
|
|
|
|
2007-09-08 17:21:01 +00:00
|
|
|
/*
|
|
|
|
* TOPMEM support
|
|
|
|
*/
|
2007-09-08 16:51:19 +00:00
|
|
|
|
|
|
|
typedef unsigned long mem_t;
|
|
|
|
|
|
|
|
typedef struct topmem_status_t {
|
|
|
|
unsigned pid;
|
|
|
|
char comm[COMM_LEN];
|
|
|
|
/* vsz doesn't count /dev/xxx mappings except /dev/zero */
|
|
|
|
mem_t vsz ;
|
|
|
|
mem_t vszrw ;
|
|
|
|
mem_t rss ;
|
|
|
|
mem_t rss_sh ;
|
|
|
|
mem_t dirty ;
|
|
|
|
mem_t dirty_sh;
|
|
|
|
mem_t stack ;
|
|
|
|
} topmem_status_t;
|
|
|
|
|
|
|
|
enum { NUM_SORT_FIELD = 7 };
|
|
|
|
|
|
|
|
#define topmem ((topmem_status_t*)top)
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
|
|
|
static int topmem_sort(char *a, char *b)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
mem_t l, r;
|
|
|
|
|
|
|
|
n = offsetof(topmem_status_t, vsz) + (sort_field * sizeof(mem_t));
|
|
|
|
l = *(mem_t*)(a + n);
|
|
|
|
r = *(mem_t*)(b + n);
|
|
|
|
// if (l == r) {
|
|
|
|
// l = a->mapped_rw;
|
|
|
|
// r = b->mapped_rw;
|
|
|
|
// }
|
|
|
|
/* We want to avoid unsigned->signed and truncation errors */
|
|
|
|
/* l>r: -1, l=r: 0, l<r: 1 */
|
|
|
|
n = (l > r) ? -1 : (l != r);
|
|
|
|
return inverted ? -n : n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cut "NNNN " out of " NNNN kb" */
|
|
|
|
static char *grab_number(char *str, const char *match, unsigned sz)
|
|
|
|
{
|
|
|
|
if (strncmp(str, match, sz) == 0) {
|
|
|
|
str = skip_whitespace(str + sz);
|
|
|
|
(skip_non_whitespace(str))[1] = '\0';
|
|
|
|
return xstrdup(str);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* display header info (meminfo / loadavg) */
|
|
|
|
static void display_topmem_header(int scr_width)
|
|
|
|
{
|
|
|
|
char linebuf[128];
|
2008-05-15 21:30:45 +00:00
|
|
|
unsigned i;
|
2007-09-08 16:51:19 +00:00
|
|
|
FILE *fp;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
/* 1 */ char *total;
|
|
|
|
/* 2 */ char *mfree;
|
|
|
|
/* 3 */ char *buf;
|
|
|
|
/* 4 */ char *cache;
|
|
|
|
/* 5 */ char *swaptotal;
|
|
|
|
/* 6 */ char *swapfree;
|
|
|
|
/* 7 */ char *dirty;
|
|
|
|
/* 8 */ char *mwrite;
|
|
|
|
/* 9 */ char *anon;
|
|
|
|
/* 10 */ char *map;
|
|
|
|
/* 11 */ char *slab;
|
2008-01-29 10:33:34 +00:00
|
|
|
} u;
|
2007-09-08 16:51:19 +00:00
|
|
|
char *str[11];
|
|
|
|
} Z;
|
2008-01-29 10:33:34 +00:00
|
|
|
#define total Z.u.total
|
|
|
|
#define mfree Z.u.mfree
|
|
|
|
#define buf Z.u.buf
|
|
|
|
#define cache Z.u.cache
|
|
|
|
#define swaptotal Z.u.swaptotal
|
|
|
|
#define swapfree Z.u.swapfree
|
|
|
|
#define dirty Z.u.dirty
|
|
|
|
#define mwrite Z.u.mwrite
|
|
|
|
#define anon Z.u.anon
|
|
|
|
#define map Z.u.map
|
|
|
|
#define slab Z.u.slab
|
2007-09-08 16:51:19 +00:00
|
|
|
#define str Z.str
|
|
|
|
|
|
|
|
memset(&Z, 0, sizeof(Z));
|
|
|
|
|
|
|
|
/* read memory info */
|
|
|
|
fp = xfopen("meminfo", "r");
|
|
|
|
while (fgets(linebuf, sizeof(linebuf), fp)) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
#define SCAN(match, name) \
|
|
|
|
p = grab_number(linebuf, match, sizeof(match)-1); \
|
|
|
|
if (p) { name = p; continue; }
|
|
|
|
|
|
|
|
SCAN("MemTotal:", total);
|
|
|
|
SCAN("MemFree:", mfree);
|
|
|
|
SCAN("Buffers:", buf);
|
|
|
|
SCAN("Cached:", cache);
|
|
|
|
SCAN("SwapTotal:", swaptotal);
|
|
|
|
SCAN("SwapFree:", swapfree);
|
|
|
|
SCAN("Dirty:", dirty);
|
|
|
|
SCAN("Writeback:", mwrite);
|
|
|
|
SCAN("AnonPages:", anon);
|
|
|
|
SCAN("Mapped:", map);
|
|
|
|
SCAN("Slab:", slab);
|
|
|
|
#undef SCAN
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
2007-12-24 12:16:24 +00:00
|
|
|
#define S(s) (s ? s : "0 ")
|
2007-09-08 16:51:19 +00:00
|
|
|
snprintf(linebuf, sizeof(linebuf),
|
|
|
|
"Mem %stotal %sanon %smap %sfree",
|
|
|
|
S(total), S(anon), S(map), S(mfree));
|
|
|
|
printf(OPT_BATCH_MODE ? "%.*s\n" : "\e[H\e[J%.*s\n", scr_width, linebuf);
|
|
|
|
|
|
|
|
snprintf(linebuf, sizeof(linebuf),
|
|
|
|
" %sslab %sbuf %scache %sdirty %swrite",
|
|
|
|
S(slab), S(buf), S(cache), S(dirty), S(mwrite));
|
|
|
|
printf("%.*s\n", scr_width, linebuf);
|
|
|
|
|
|
|
|
snprintf(linebuf, sizeof(linebuf),
|
|
|
|
"Swap %stotal %sfree", // TODO: % used?
|
|
|
|
S(swaptotal), S(swapfree));
|
|
|
|
printf("%.*s\n", scr_width, linebuf);
|
|
|
|
#undef S
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(str); i++)
|
|
|
|
free(str[i]);
|
|
|
|
#undef total
|
|
|
|
#undef free
|
|
|
|
#undef buf
|
|
|
|
#undef cache
|
|
|
|
#undef swaptotal
|
|
|
|
#undef swapfree
|
|
|
|
#undef dirty
|
|
|
|
#undef write
|
|
|
|
#undef anon
|
|
|
|
#undef map
|
|
|
|
#undef slab
|
|
|
|
#undef str
|
|
|
|
}
|
|
|
|
|
2008-01-06 03:26:53 +00:00
|
|
|
static void ulltoa6_and_space(unsigned long long ul, char buf[6])
|
2007-09-08 16:51:19 +00:00
|
|
|
{
|
2008-01-06 03:26:53 +00:00
|
|
|
/* see http://en.wikipedia.org/wiki/Tera */
|
|
|
|
smart_ulltoa5(ul, buf, " mgtpezy");
|
2007-09-08 16:51:19 +00:00
|
|
|
buf[5] = ' ';
|
|
|
|
}
|
|
|
|
|
2007-09-08 17:34:05 +00:00
|
|
|
static NOINLINE void display_topmem_process_list(int count, int scr_width)
|
2007-09-08 16:51:19 +00:00
|
|
|
{
|
|
|
|
#define HDR_STR " PID VSZ VSZRW RSS (SHR) DIRTY (SHR) STACK"
|
|
|
|
#define MIN_WIDTH sizeof(HDR_STR)
|
|
|
|
const topmem_status_t *s = topmem;
|
|
|
|
|
|
|
|
display_topmem_header(scr_width);
|
2007-09-08 17:34:05 +00:00
|
|
|
strcpy(line_buf, HDR_STR " COMMAND");
|
|
|
|
line_buf[5 + sort_field * 6] = '*';
|
|
|
|
printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, line_buf);
|
2007-09-08 16:51:19 +00:00
|
|
|
|
|
|
|
while (--count >= 0) {
|
|
|
|
// PID VSZ VSZRW RSS (SHR) DIRTY (SHR) COMMAND
|
2008-01-06 03:26:53 +00:00
|
|
|
ulltoa6_and_space(s->pid , &line_buf[0*6]);
|
|
|
|
ulltoa6_and_space(s->vsz , &line_buf[1*6]);
|
|
|
|
ulltoa6_and_space(s->vszrw , &line_buf[2*6]);
|
|
|
|
ulltoa6_and_space(s->rss , &line_buf[3*6]);
|
|
|
|
ulltoa6_and_space(s->rss_sh , &line_buf[4*6]);
|
|
|
|
ulltoa6_and_space(s->dirty , &line_buf[5*6]);
|
|
|
|
ulltoa6_and_space(s->dirty_sh, &line_buf[6*6]);
|
|
|
|
ulltoa6_and_space(s->stack , &line_buf[7*6]);
|
2007-09-08 17:34:05 +00:00
|
|
|
line_buf[8*6] = '\0';
|
2008-05-15 21:30:45 +00:00
|
|
|
if (scr_width > (int)MIN_WIDTH) {
|
2007-09-08 17:34:05 +00:00
|
|
|
read_cmdline(&line_buf[8*6], scr_width - MIN_WIDTH, s->pid, s->comm);
|
2007-09-08 16:51:19 +00:00
|
|
|
}
|
2007-09-08 17:34:05 +00:00
|
|
|
printf("\n""%.*s", scr_width, line_buf);
|
2007-09-08 16:51:19 +00:00
|
|
|
s++;
|
|
|
|
}
|
2007-09-27 10:20:47 +00:00
|
|
|
bb_putchar(OPT_BATCH_MODE ? '\n' : '\r');
|
2007-09-08 16:51:19 +00:00
|
|
|
fflush(stdout);
|
|
|
|
#undef HDR_STR
|
|
|
|
#undef MIN_WIDTH
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void display_topmem_process_list(int count, int scr_width);
|
|
|
|
int topmem_sort(char *a, char *b);
|
|
|
|
#endif /* TOPMEM */
|
|
|
|
|
2007-09-08 17:21:01 +00:00
|
|
|
/*
|
|
|
|
* end TOPMEM support
|
|
|
|
*/
|
2007-09-08 16:51:19 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
TOP_MASK = 0
|
|
|
|
| PSSCAN_PID
|
|
|
|
| PSSCAN_PPID
|
|
|
|
| PSSCAN_VSZ
|
|
|
|
| PSSCAN_STIME
|
|
|
|
| PSSCAN_UTIME
|
|
|
|
| PSSCAN_STATE
|
|
|
|
| PSSCAN_COMM
|
|
|
|
| PSSCAN_UIDGID,
|
|
|
|
TOPMEM_MASK = 0
|
|
|
|
| PSSCAN_PID
|
|
|
|
| PSSCAN_SMAPS
|
|
|
|
| PSSCAN_COMM,
|
|
|
|
};
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int top_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int top_main(int argc UNUSED_PARAM, char **argv)
|
2002-09-17 22:14:58 +00:00
|
|
|
{
|
2008-05-18 22:28:26 +00:00
|
|
|
int count;
|
2007-09-08 17:21:01 +00:00
|
|
|
int iterations;
|
2008-05-18 22:28:26 +00:00
|
|
|
unsigned lines, col;
|
|
|
|
unsigned interval;
|
2008-02-10 19:44:20 +00:00
|
|
|
char *sinterval;
|
2007-09-08 16:51:19 +00:00
|
|
|
SKIP_FEATURE_TOPMEM(const) unsigned scan_mask = TOP_MASK;
|
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;
|
2007-08-28 19:35:34 +00:00
|
|
|
struct pollfd pfd[1];
|
2002-09-30 20:52:10 +00:00
|
|
|
unsigned char c;
|
2007-08-28 19:35:34 +00:00
|
|
|
|
|
|
|
pfd[0].fd = 0;
|
|
|
|
pfd[0].events = POLLIN;
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2002-09-30 20:52:10 +00:00
|
|
|
|
2007-09-08 17:21:01 +00:00
|
|
|
INIT_G();
|
|
|
|
|
2008-03-19 19:38:46 +00:00
|
|
|
interval = 5; /* default update interval is 5 seconds */
|
2007-09-08 17:21:01 +00:00
|
|
|
iterations = 0; /* infinite */
|
2007-04-19 14:47:11 +00:00
|
|
|
|
2008-03-19 19:38:46 +00:00
|
|
|
/* all args are options; -n NUM */
|
2008-02-10 19:44:20 +00:00
|
|
|
opt_complementary = "-:n+";
|
|
|
|
getopt32(argv, "d:n:b", &sinterval, &iterations);
|
2008-03-19 19:38:46 +00:00
|
|
|
if (option_mask32 & OPT_d) {
|
2007-08-28 19:35:34 +00:00
|
|
|
/* Need to limit it to not overflow poll timeout */
|
|
|
|
interval = xatou16(sinterval); // -d
|
|
|
|
}
|
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);
|
2007-08-29 18:18:08 +00:00
|
|
|
memcpy(&new_settings, &initial_settings, sizeof(new_settings));
|
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
|
|
|
|
2008-03-19 19:38:46 +00:00
|
|
|
bb_signals(BB_FATAL_SIGS, sig_catcher);
|
2002-09-30 20:52:10 +00:00
|
|
|
tcsetattr(0, TCSANOW, (void *) &new_settings);
|
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
|
2007-08-29 18:18:08 +00:00
|
|
|
sort_function[0] = 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
|
|
|
|
2007-09-08 17:21:01 +00:00
|
|
|
lines = 24; /* default */
|
2006-04-24 23:13:46 +00:00
|
|
|
col = 79;
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2008-03-19 19:38:46 +00:00
|
|
|
/* We output to stdout, we need size of stdout (not stdin)! */
|
|
|
|
get_terminal_width_height(STDOUT_FILENO, &col, &lines);
|
2007-09-08 16:51:19 +00:00
|
|
|
if (lines < 5 || col < 10) {
|
2006-04-24 23:13:46 +00:00
|
|
|
sleep(interval);
|
|
|
|
continue;
|
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2007-09-08 17:34:05 +00:00
|
|
|
if (col > LINE_BUF_SIZE-2) /* +2 bytes for '\n', NUL, */
|
|
|
|
col = LINE_BUF_SIZE-2;
|
2007-09-08 16:51:19 +00:00
|
|
|
if (!ENABLE_FEATURE_TOP_CPU_GLOBAL_PERCENTS && scan_mask == TOP_MASK)
|
|
|
|
lines -= 3;
|
|
|
|
else
|
|
|
|
lines -= 4;
|
2006-04-24 23:13:46 +00:00
|
|
|
|
|
|
|
/* read process IDs & status for all the processes */
|
2007-09-08 16:51:19 +00:00
|
|
|
while ((p = procps_scan(p, scan_mask)) != NULL) {
|
|
|
|
int n;
|
|
|
|
if (scan_mask == TOP_MASK) {
|
|
|
|
n = ntop;
|
2008-07-08 05:14:36 +00:00
|
|
|
top = xrealloc_vector(top, 2, ntop++);
|
2007-09-08 16:51:19 +00:00
|
|
|
top[n].pid = p->pid;
|
|
|
|
top[n].ppid = p->ppid;
|
|
|
|
top[n].vsz = p->vsz;
|
2006-12-30 17:57:03 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2007-09-08 16:51:19 +00:00
|
|
|
top[n].ticks = p->stime + p->utime;
|
2006-12-30 17:57:03 +00:00
|
|
|
#endif
|
2007-09-08 16:51:19 +00:00
|
|
|
top[n].uid = p->uid;
|
|
|
|
strcpy(top[n].state, p->state);
|
|
|
|
strcpy(top[n].comm, p->comm);
|
|
|
|
} else { /* TOPMEM */
|
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
|
|
|
if (!(p->mapped_ro | p->mapped_rw))
|
|
|
|
continue; /* kernel threads are ignored */
|
|
|
|
n = ntop;
|
2008-07-08 05:14:36 +00:00
|
|
|
/* No bug here - top and topmem are the same */
|
|
|
|
top = xrealloc_vector(topmem, 2, ntop++);
|
2007-09-08 16:51:19 +00:00
|
|
|
strcpy(topmem[n].comm, p->comm);
|
|
|
|
topmem[n].pid = p->pid;
|
|
|
|
topmem[n].vsz = p->mapped_rw + p->mapped_ro;
|
|
|
|
topmem[n].vszrw = p->mapped_rw;
|
|
|
|
topmem[n].rss_sh = p->shared_clean + p->shared_dirty;
|
|
|
|
topmem[n].rss = p->private_clean + p->private_dirty + topmem[n].rss_sh;
|
|
|
|
topmem[n].dirty = p->private_dirty + p->shared_dirty;
|
|
|
|
topmem[n].dirty_sh = p->shared_dirty;
|
|
|
|
topmem[n].stack = p->stack;
|
|
|
|
#endif
|
|
|
|
}
|
2008-03-19 19:38:46 +00:00
|
|
|
} /* end of "while we read /proc" */
|
2002-10-22 12:21:15 +00:00
|
|
|
if (ntop == 0) {
|
2008-03-19 19:38:46 +00:00
|
|
|
bb_error_msg("no process info in /proc");
|
|
|
|
break;
|
2006-02-13 22:04:27 +00:00
|
|
|
}
|
2007-09-08 16:51:19 +00:00
|
|
|
|
|
|
|
if (scan_mask == TOP_MASK) {
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2007-09-08 16:51:19 +00:00
|
|
|
if (!prev_hist_count) {
|
|
|
|
do_stats();
|
|
|
|
usleep(100000);
|
|
|
|
clearmems();
|
|
|
|
continue;
|
|
|
|
}
|
2002-09-30 20:52:10 +00:00
|
|
|
do_stats();
|
2007-06-30 08:03:26 +00:00
|
|
|
/* TODO: we don't need to sort all 10000 processes, we need to find top 24! */
|
2007-09-08 16:51:19 +00:00
|
|
|
qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp);
|
2002-09-30 20:52:10 +00:00
|
|
|
#else
|
2007-09-08 16:51:19 +00:00
|
|
|
qsort(top, ntop, sizeof(top_status_t), (void*)(sort_function[0]));
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
2008-04-17 18:04:38 +00:00
|
|
|
}
|
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
|
|
|
else { /* TOPMEM */
|
2007-09-08 16:51:19 +00:00
|
|
|
qsort(topmem, ntop, sizeof(topmem_status_t), (void*)topmem_sort);
|
|
|
|
}
|
2008-04-17 18:04:38 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
2007-09-08 16:51:19 +00:00
|
|
|
if (scan_mask == TOP_MASK)
|
|
|
|
display_process_list(count, col);
|
2008-04-17 18:04:38 +00:00
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
2007-09-08 16:51:19 +00:00
|
|
|
else
|
|
|
|
display_topmem_process_list(count, col);
|
2008-04-17 18:04:38 +00:00
|
|
|
#endif
|
2007-08-29 18:18:08 +00:00
|
|
|
clearmems();
|
|
|
|
if (iterations >= 0 && !--iterations)
|
|
|
|
break;
|
|
|
|
#if !ENABLE_FEATURE_USE_TERMIOS
|
|
|
|
sleep(interval);
|
|
|
|
#else
|
2008-03-19 19:38:46 +00:00
|
|
|
if (option_mask32 & (OPT_b|OPT_EOF))
|
|
|
|
/* batch mode, or EOF on stdin ("top </dev/null") */
|
|
|
|
sleep(interval);
|
|
|
|
else if (safe_poll(pfd, 1, interval * 1000) > 0) {
|
2008-05-19 09:48:17 +00:00
|
|
|
if (safe_read(STDIN_FILENO, &c, 1) != 1) { /* error/EOF? */
|
2008-03-19 19:38:46 +00:00
|
|
|
option_mask32 |= OPT_EOF;
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-08 16:51:19 +00:00
|
|
|
if (c == initial_settings.c_cc[VINTR])
|
|
|
|
break;
|
|
|
|
c |= 0x20; /* lowercase */
|
|
|
|
if (c == 'q')
|
2006-02-15 17:15:56 +00:00
|
|
|
break;
|
2007-09-08 16:51:19 +00:00
|
|
|
if (c == 'n') {
|
|
|
|
USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
|
2007-08-29 18:18:08 +00:00
|
|
|
sort_function[0] = pid_sort;
|
2007-09-08 16:51:19 +00:00
|
|
|
}
|
|
|
|
if (c == 'm') {
|
|
|
|
USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = mem_sort;
|
2007-08-29 18:18:08 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[1] = pcpu_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
#endif
|
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2007-09-08 16:51:19 +00:00
|
|
|
if (c == 'p') {
|
|
|
|
USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = pcpu_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
}
|
2007-09-08 16:51:19 +00:00
|
|
|
if (c == 't') {
|
|
|
|
USE_FEATURE_TOPMEM(scan_mask = TOP_MASK;)
|
2002-09-30 20:52:10 +00:00
|
|
|
sort_function[0] = time_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = pcpu_sort;
|
|
|
|
}
|
2007-09-08 16:51:19 +00:00
|
|
|
#if ENABLE_FEATURE_TOPMEM
|
|
|
|
if (c == 's') {
|
|
|
|
scan_mask = TOPMEM_MASK;
|
|
|
|
free(prev_hist);
|
|
|
|
prev_hist = NULL;
|
|
|
|
prev_hist_count = 0;
|
|
|
|
sort_field = (sort_field + 1) % NUM_SORT_FIELD;
|
|
|
|
}
|
|
|
|
if (c == 'r')
|
|
|
|
inverted ^= 1;
|
|
|
|
#endif
|
2002-09-30 20:52:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
2006-11-05 00:38:51 +00:00
|
|
|
#endif /* FEATURE_USE_TERMIOS */
|
2008-03-19 19:38:46 +00:00
|
|
|
} /* end of "while (1)" */
|
|
|
|
|
2007-09-27 10:20:47 +00:00
|
|
|
bb_putchar('\n');
|
2008-03-20 16:05:02 +00:00
|
|
|
#if ENABLE_FEATURE_USE_TERMIOS
|
2008-03-19 19:38:46 +00:00
|
|
|
reset_term();
|
2008-03-20 16:05:02 +00:00
|
|
|
#endif
|
2002-09-17 22:14:58 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|