2006-04-13 00:09:58 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-09-18 03:44:58 +05:30
|
|
|
/*
|
|
|
|
* A tiny 'top' utility.
|
|
|
|
*
|
2002-10-01 02:22:10 +05:30
|
|
|
* 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 13:59:22 +05:30
|
|
|
*
|
2002-09-18 03:44:58 +05:30
|
|
|
* NOTES:
|
|
|
|
* - At startup this changes to /proc, all the reads are then
|
|
|
|
* relative to that.
|
2004-03-15 13:59:22 +05:30
|
|
|
*
|
2002-09-18 03:44:58 +05:30
|
|
|
* (C) Eero Tamminen <oak at welho dot com>
|
2002-10-01 02:22:10 +05:30
|
|
|
*
|
2004-04-14 23:21:38 +05:30
|
|
|
* Rewritten by Vladimir Oleynik (C) 2002 <dzo@simtreas.ru>
|
2002-10-01 02:22:10 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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-18 03:44:58 +05:30
|
|
|
*/
|
2002-10-01 02:22:10 +05:30
|
|
|
|
|
|
|
#include <sys/types.h>
|
2002-09-18 03:44:58 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include "busybox.h"
|
|
|
|
|
2005-09-16 18:25:29 +05:30
|
|
|
//#define CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE /* + 2k */
|
2002-10-01 02:22:10 +05:30
|
|
|
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-01 02:22:10 +05:30
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h> /* htons */
|
|
|
|
#endif
|
|
|
|
|
2002-09-18 03:44:58 +05:30
|
|
|
|
2002-10-22 17:51:15 +05:30
|
|
|
typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q);
|
2002-10-01 02:22:10 +05:30
|
|
|
|
2002-10-22 17:51:15 +05:30
|
|
|
static procps_status_t *top; /* Hehe */
|
2002-10-01 02:22:10 +05:30
|
|
|
static int ntop;
|
|
|
|
|
2005-07-30 15:12:05 +05:30
|
|
|
#ifdef CONFIG_FEATURE_USE_TERMIOS
|
2006-04-25 04:43:46 +05:30
|
|
|
static int pid_sort(procps_status_t *P, procps_status_t *Q)
|
2002-10-01 02:22:10 +05:30
|
|
|
{
|
2006-02-14 03:34:27 +05:30
|
|
|
return (Q->pid - P->pid);
|
2002-10-01 02:22:10 +05:30
|
|
|
}
|
2005-07-30 15:12:05 +05:30
|
|
|
#endif
|
2002-10-01 02:22:10 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
static int mem_sort(procps_status_t *P, procps_status_t *Q)
|
2002-10-01 02:22:10 +05:30
|
|
|
{
|
2006-02-14 03:34:27 +05:30
|
|
|
return (int)(Q->rss - P->rss);
|
2002-10-01 02:22:10 +05:30
|
|
|
}
|
|
|
|
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-01 02:22:10 +05:30
|
|
|
|
|
|
|
#define sort_depth 3
|
|
|
|
static cmp_t sort_function[sort_depth];
|
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
static int pcpu_sort(procps_status_t *P, procps_status_t *Q)
|
2002-10-01 02:22:10 +05:30
|
|
|
{
|
2006-02-14 03:34:27 +05:30
|
|
|
return (Q->pcpu - P->pcpu);
|
2002-10-01 02:22:10 +05:30
|
|
|
}
|
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
static int time_sort(procps_status_t *P, procps_status_t *Q)
|
2002-10-01 02:22:10 +05:30
|
|
|
{
|
2006-02-14 03:34:27 +05:30
|
|
|
return (int)((Q->stime + Q->utime) - (P->stime + P->utime));
|
2002-10-01 02:22:10 +05:30
|
|
|
}
|
|
|
|
|
2005-04-17 01:09:00 +05:30
|
|
|
static int mult_lvl_cmp(void* a, void* b) {
|
2006-02-14 03:34:27 +05:30
|
|
|
int i, cmp_val;
|
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
for (i = 0; i < sort_depth; i++) {
|
2006-02-14 03:34:27 +05:30
|
|
|
cmp_val = (*sort_function[i])(a, b);
|
|
|
|
if (cmp_val != 0)
|
|
|
|
return cmp_val;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-10-01 02:22:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* This structure stores some critical information from one frame to
|
|
|
|
the next. mostly used for sorting. Added cumulative and resident fields. */
|
|
|
|
struct save_hist {
|
2006-02-14 03:34:27 +05:30
|
|
|
int ticks;
|
|
|
|
int pid;
|
2002-10-01 02:22:10 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculates percent cpu usage for each task.
|
|
|
|
*/
|
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
static struct save_hist *prev_hist;
|
|
|
|
static int prev_hist_count;
|
|
|
|
/* static int hist_iterations; */
|
2002-10-01 02:22:10 +05:30
|
|
|
|
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
static unsigned total_pcpu;
|
|
|
|
/* static unsigned long total_rss; */
|
2006-02-14 03:34:27 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
struct jiffy_counts {
|
|
|
|
unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal;
|
|
|
|
unsigned long long total;
|
|
|
|
unsigned long long busy;
|
|
|
|
};
|
|
|
|
static struct jiffy_counts jif, prev_jif;
|
2006-02-14 03:34:27 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
static void get_jiffy_counts(void)
|
2006-02-14 03:34:27 +05:30
|
|
|
{
|
2006-04-25 04:43:46 +05:30
|
|
|
FILE* fp = bb_xfopen("stat", "r");
|
|
|
|
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) {
|
|
|
|
bb_error_msg_and_die("failed to read 'stat'");
|
2006-02-14 03:34:27 +05:30
|
|
|
}
|
2006-04-25 04:43:46 +05:30
|
|
|
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-10-01 02:22:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void do_stats(void)
|
|
|
|
{
|
2006-02-14 03:34:27 +05:30
|
|
|
procps_status_t *cur;
|
2006-04-25 04:43:46 +05:30
|
|
|
int pid, total_time, i, last_i, n;
|
|
|
|
struct save_hist *new_hist;
|
2002-10-01 02:22:10 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
get_jiffy_counts();
|
|
|
|
total_pcpu = 0;
|
|
|
|
/* total_rss = 0; */
|
|
|
|
new_hist = xmalloc(sizeof(struct save_hist)*ntop);
|
2006-02-14 03:34:27 +05:30
|
|
|
/*
|
|
|
|
* Make a pass through the data to get stats.
|
|
|
|
*/
|
2006-04-25 04:43:46 +05:30
|
|
|
/* hist_iterations = 0; */
|
|
|
|
i = 0;
|
|
|
|
for (n = 0; n < ntop; n++) {
|
2006-02-14 03:34:27 +05:30
|
|
|
cur = top + n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate time in cur process. Time is sum of user time
|
2006-04-25 04:43:46 +05:30
|
|
|
* and system time
|
2006-02-14 03:34:27 +05:30
|
|
|
*/
|
|
|
|
pid = cur->pid;
|
2006-04-25 04:43:46 +05:30
|
|
|
total_time = cur->stime + cur->utime;
|
|
|
|
new_hist[n].ticks = total_time;
|
|
|
|
new_hist[n].pid = pid;
|
2006-02-14 03:34:27 +05:30
|
|
|
|
|
|
|
/* find matching entry from previous pass */
|
2006-04-25 04:43:46 +05:30
|
|
|
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) {
|
|
|
|
cur->pcpu = total_time - prev_hist[i].ticks;
|
2006-02-14 03:34:27 +05:30
|
|
|
break;
|
|
|
|
}
|
2006-04-25 04:43:46 +05:30
|
|
|
i = (i+1) % prev_hist_count;
|
|
|
|
/* hist_iterations++; */
|
|
|
|
} while (i != last_i);
|
|
|
|
total_pcpu += cur->pcpu;
|
|
|
|
/* total_rss += cur->rss; */
|
2002-10-01 02:22:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-02-14 03:34:27 +05:30
|
|
|
* Save cur frame's information.
|
2002-10-01 02:22:10 +05:30
|
|
|
*/
|
2006-04-25 04:43:46 +05:30
|
|
|
free(prev_hist);
|
|
|
|
prev_hist = new_hist;
|
|
|
|
prev_hist_count = ntop;
|
2002-10-01 02:22:10 +05:30
|
|
|
}
|
|
|
|
#else
|
|
|
|
static cmp_t sort_function;
|
2005-09-16 18:25:29 +05:30
|
|
|
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
2002-10-01 02:22:10 +05:30
|
|
|
|
2002-09-18 03:44:58 +05:30
|
|
|
/* display generic info (meminfo / loadavg) */
|
2006-04-25 04:43:46 +05:30
|
|
|
static unsigned long display_generic(int scr_width)
|
2002-09-18 03:44:58 +05:30
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char buf[80];
|
2006-04-25 04:43:46 +05:30
|
|
|
char scrbuf[80];
|
|
|
|
char *end;
|
2002-09-18 03:44:58 +05:30
|
|
|
unsigned long total, used, mfree, shared, buffers, cached;
|
2003-10-12 00:17:20 +05:30
|
|
|
unsigned int needs_conversion = 1;
|
2002-09-18 03:44:58 +05:30
|
|
|
|
|
|
|
/* read memory info */
|
2003-03-19 14:43:01 +05:30
|
|
|
fp = bb_xfopen("meminfo", "r");
|
2002-09-18 03:44:58 +05:30
|
|
|
|
2003-10-12 00:17:20 +05:30
|
|
|
/*
|
|
|
|
* 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 18:02:33 +05:30
|
|
|
* -- PFM.
|
2003-10-12 00:17:20 +05:30
|
|
|
*/
|
|
|
|
if (fscanf(fp, "MemTotal: %lu %s\n", &total, buf) != 2) {
|
2005-11-29 18:02:33 +05:30
|
|
|
fgets(buf, sizeof(buf), fp); /* skip first line */
|
2003-10-12 00:17:20 +05:30
|
|
|
|
|
|
|
fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
|
|
|
|
&total, &used, &mfree, &shared, &buffers, &cached);
|
|
|
|
} else {
|
2004-03-15 13:59:22 +05:30
|
|
|
/*
|
2003-10-12 00:17:20 +05:30
|
|
|
* 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 13:59:22 +05:30
|
|
|
/*
|
2003-10-12 00:17:20 +05:30
|
|
|
* 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-18 03:44:58 +05:30
|
|
|
}
|
|
|
|
fclose(fp);
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
/* read load average as a string */
|
2003-03-19 14:43:01 +05:30
|
|
|
fp = bb_xfopen("loadavg", "r");
|
2006-04-25 04:43:46 +05:30
|
|
|
buf[0] = '\0';
|
|
|
|
fgets(buf, sizeof(buf), fp);
|
|
|
|
end = strchr(buf, ' ');
|
|
|
|
if (end) end = strchr(end+1, ' ');
|
|
|
|
if (end) end = strchr(end+1, ' ');
|
|
|
|
if (end) *end = '\0';
|
2002-09-18 03:44:58 +05:30
|
|
|
fclose(fp);
|
|
|
|
|
2003-10-12 00:17:20 +05:30
|
|
|
if (needs_conversion) {
|
|
|
|
/* convert to kilobytes */
|
|
|
|
used /= 1024;
|
|
|
|
mfree /= 1024;
|
|
|
|
shared /= 1024;
|
|
|
|
buffers /= 1024;
|
|
|
|
cached /= 1024;
|
|
|
|
total /= 1024;
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-09-18 03:44:58 +05:30
|
|
|
/* output memory info and load average */
|
2002-10-01 02:22:10 +05:30
|
|
|
/* clear screen & go to top */
|
2006-04-25 04:43:46 +05:30
|
|
|
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-14 03:34:27 +05:30
|
|
|
used, mfree, shared, buffers, cached);
|
2006-04-25 04:43:46 +05:30
|
|
|
printf("\e[H\e[J%s\n", scrbuf);
|
|
|
|
snprintf(scrbuf, scr_width,
|
|
|
|
"Load average: %s (Status: S=sleeping R=running, W=waiting)", buf);
|
|
|
|
printf("%s\n", scrbuf);
|
|
|
|
|
2003-10-12 00:17:20 +05:30
|
|
|
return total;
|
2002-09-18 03:44:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* display process statuses */
|
2006-04-25 04:43:46 +05:30
|
|
|
static void display_status(int count, int scr_width)
|
2002-09-18 03:44:58 +05:30
|
|
|
{
|
2006-04-25 04:43:46 +05:30
|
|
|
enum {
|
|
|
|
bits_per_int = sizeof(int)*8
|
|
|
|
};
|
|
|
|
|
2002-10-22 17:51:15 +05:30
|
|
|
procps_status_t *s = top;
|
2002-10-01 02:22:10 +05:30
|
|
|
char rss_str_buf[8];
|
2006-04-25 04:43:46 +05:30
|
|
|
unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */
|
|
|
|
unsigned pmem_shift, pmem_scale;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-25 04:43:46 +05:30
|
|
|
unsigned pcpu_shift, pcpu_scale;
|
|
|
|
|
2002-09-18 03:44:58 +05:30
|
|
|
/* what info of the processes is shown */
|
2006-04-25 04:43:46 +05:30
|
|
|
printf("\e[7m%.*s\e[0m", scr_width,
|
|
|
|
" PID USER STATUS RSS PPID %CPU %MEM COMMAND");
|
|
|
|
#define MIN_WIDTH \
|
|
|
|
sizeof( " PID USER STATUS RSS PPID %CPU %MEM C")
|
2002-10-01 02:22:10 +05:30
|
|
|
#else
|
2006-04-25 04:43:46 +05:30
|
|
|
printf("\e[7m%.*s\e[0m", scr_width,
|
|
|
|
" PID USER STATUS RSS PPID %MEM COMMAND");
|
|
|
|
#define MIN_WIDTH \
|
|
|
|
sizeof( " PID USER STATUS RSS PPID %MEM C")
|
2002-10-01 02:22:10 +05:30
|
|
|
#endif
|
2002-09-18 03:44:58 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1);
|
|
|
|
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
|
2002-09-18 03:44:58 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
while (count--) {
|
|
|
|
div_t pmem = div( (s->rss*pmem_scale) >> pmem_shift, 10);
|
|
|
|
int col = scr_width+1;
|
2006-05-19 18:18:56 +05:30
|
|
|
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(div_t pcpu;)
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
if (s->rss >= 100*1024)
|
2002-10-01 02:22:10 +05:30
|
|
|
sprintf(rss_str_buf, "%6ldM", s->rss/1024);
|
2002-09-19 00:57:10 +05:30
|
|
|
else
|
2002-10-01 02:22:10 +05:30
|
|
|
sprintf(rss_str_buf, "%7ld", s->rss);
|
2006-05-19 18:18:56 +05:30
|
|
|
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);)
|
2006-05-05 01:21:22 +05:30
|
|
|
col -= printf("\n%5d %-8s %s %s%6d%3u.%c" \
|
2006-05-19 18:18:56 +05:30
|
|
|
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ",
|
2006-04-25 04:43:46 +05:30
|
|
|
s->pid, s->user, s->state, rss_str_buf, s->ppid,
|
2006-05-05 01:21:22 +05:30
|
|
|
USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,)
|
|
|
|
pmem.quot, '0'+pmem.rem);
|
2006-04-25 04:43:46 +05:30
|
|
|
if (col>0)
|
|
|
|
printf("%.*s", col, s->short_cmd);
|
2006-05-19 18:18:56 +05:30
|
|
|
/* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu,
|
2006-04-25 04:43:46 +05:30
|
|
|
jif.busy - prev_jif.busy, jif.total - prev_jif.total); */
|
2002-10-01 02:22:10 +05:30
|
|
|
s++;
|
2002-10-22 17:51:15 +05:30
|
|
|
}
|
2006-04-25 04:43:46 +05:30
|
|
|
/* printf(" %d", hist_iterations); */
|
|
|
|
putchar('\r');
|
|
|
|
fflush(stdout);
|
2002-09-18 03:44:58 +05:30
|
|
|
}
|
|
|
|
|
2002-10-22 17:51:15 +05:30
|
|
|
static void clearmems(void)
|
2002-09-18 03:44:58 +05:30
|
|
|
{
|
2002-10-01 02:22:10 +05:30
|
|
|
free(top);
|
|
|
|
top = 0;
|
|
|
|
ntop = 0;
|
2002-09-18 03:44:58 +05:30
|
|
|
}
|
|
|
|
|
2005-07-30 15:12:05 +05:30
|
|
|
#ifdef CONFIG_FEATURE_USE_TERMIOS
|
2002-10-01 02:22:10 +05:30
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <signal.h>
|
2002-09-18 03:44:58 +05:30
|
|
|
|
|
|
|
|
2002-10-01 02:22:10 +05:30
|
|
|
static struct termios initial_settings;
|
2002-09-18 03:44:58 +05:30
|
|
|
|
2002-10-01 02:22:10 +05:30
|
|
|
static void reset_term(void)
|
2002-09-18 03:44:58 +05:30
|
|
|
{
|
2002-10-01 02:22:10 +05:30
|
|
|
tcsetattr(0, TCSANOW, (void *) &initial_settings);
|
|
|
|
#ifdef CONFIG_FEATURE_CLEAN_UP
|
|
|
|
clearmems();
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-25 04:43:46 +05:30
|
|
|
free(prev_hist);
|
2002-10-01 02:22:10 +05:30
|
|
|
#endif
|
|
|
|
#endif /* CONFIG_FEATURE_CLEAN_UP */
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2006-02-14 03:34:27 +05:30
|
|
|
static void sig_catcher(int sig ATTRIBUTE_UNUSED)
|
2002-10-01 02:22:10 +05:30
|
|
|
{
|
|
|
|
reset_term();
|
2002-09-18 03:44:58 +05:30
|
|
|
}
|
2002-10-01 02:22:10 +05:30
|
|
|
#endif /* CONFIG_FEATURE_USE_TERMIOS */
|
|
|
|
|
2002-09-18 03:44:58 +05:30
|
|
|
|
|
|
|
int top_main(int argc, char **argv)
|
|
|
|
{
|
2002-10-01 02:22:10 +05:30
|
|
|
int opt, interval, lines, col;
|
2005-11-29 18:02:33 +05:30
|
|
|
char *sinterval;
|
2005-07-30 15:12:05 +05:30
|
|
|
#ifdef CONFIG_FEATURE_USE_TERMIOS
|
2002-10-01 02:22:10 +05:30
|
|
|
struct termios new_settings;
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set readfds;
|
|
|
|
unsigned char c;
|
|
|
|
struct sigaction sa;
|
|
|
|
#endif /* CONFIG_FEATURE_USE_TERMIOS */
|
|
|
|
|
2002-09-18 03:44:58 +05:30
|
|
|
/* do normal option parsing */
|
2005-11-29 18:02:33 +05:30
|
|
|
opt = bb_getopt_ulflags(argc, argv, "d:", &sinterval);
|
2006-04-25 04:43:46 +05:30
|
|
|
if ((opt & 1)) {
|
2005-11-29 18:02:33 +05:30
|
|
|
interval = atoi(sinterval);
|
|
|
|
} else {
|
|
|
|
/* Default update rate is 5 seconds */
|
|
|
|
interval = 5;
|
2002-09-18 03:44:58 +05:30
|
|
|
}
|
|
|
|
|
2002-10-22 17:51:15 +05:30
|
|
|
/* change to /proc */
|
2006-04-13 00:09:58 +05:30
|
|
|
bb_xchdir("/proc");
|
2005-07-30 15:12:05 +05:30
|
|
|
#ifdef CONFIG_FEATURE_USE_TERMIOS
|
2002-10-01 02:22:10 +05:30
|
|
|
tcgetattr(0, (void *) &initial_settings);
|
|
|
|
memcpy(&new_settings, &initial_settings, sizeof(struct termios));
|
|
|
|
new_settings.c_lflag &= ~(ISIG | ICANON); /* unbuffered input */
|
|
|
|
/* Turn off echoing */
|
|
|
|
new_settings.c_lflag &= ~(ECHO | ECHONL);
|
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
signal(SIGTERM, sig_catcher);
|
|
|
|
sigaction(SIGTERM, (struct sigaction *) 0, &sa);
|
2002-10-01 02:22:10 +05:30
|
|
|
sa.sa_flags |= SA_RESTART;
|
|
|
|
sa.sa_flags &= ~SA_INTERRUPT;
|
2006-04-25 04:43:46 +05:30
|
|
|
sigaction(SIGTERM, &sa, (struct sigaction *) 0);
|
|
|
|
sigaction(SIGINT, &sa, (struct sigaction *) 0);
|
2002-10-01 02:22:10 +05:30
|
|
|
tcsetattr(0, TCSANOW, (void *) &new_settings);
|
|
|
|
atexit(reset_term);
|
|
|
|
#endif /* CONFIG_FEATURE_USE_TERMIOS */
|
2005-07-30 15:12:05 +05:30
|
|
|
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-01 02:22:10 +05:30
|
|
|
sort_function[0] = pcpu_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
#else
|
|
|
|
sort_function = mem_sort;
|
2005-09-16 18:25:29 +05:30
|
|
|
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
2005-07-30 15:12:05 +05:30
|
|
|
|
2002-10-01 02:22:10 +05:30
|
|
|
while (1) {
|
2006-04-25 04:43:46 +05:30
|
|
|
procps_status_t *p;
|
2002-10-22 17:51:15 +05:30
|
|
|
|
2006-04-25 04:43:46 +05:30
|
|
|
/* Default to 25 lines - 5 lines for status */
|
|
|
|
lines = 24 - 3;
|
|
|
|
col = 79;
|
|
|
|
#ifdef CONFIG_FEATURE_USE_TERMIOS
|
|
|
|
get_terminal_width_height(0, &col, &lines);
|
|
|
|
if (lines < 5 || col < MIN_WIDTH) {
|
|
|
|
sleep(interval);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lines -= 3;
|
|
|
|
#endif /* CONFIG_FEATURE_USE_TERMIOS */
|
|
|
|
|
|
|
|
/* read process IDs & status for all the processes */
|
2002-10-22 17:51:15 +05:30
|
|
|
while ((p = procps_scan(0)) != 0) {
|
|
|
|
int n = ntop;
|
|
|
|
|
|
|
|
top = xrealloc(top, (++ntop)*sizeof(procps_status_t));
|
|
|
|
memcpy(top + n, p, sizeof(procps_status_t));
|
|
|
|
}
|
|
|
|
if (ntop == 0) {
|
2006-02-14 03:34:27 +05:30
|
|
|
bb_error_msg_and_die("Can't find process info in /proc");
|
|
|
|
}
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-25 04:43:46 +05:30
|
|
|
if (!prev_hist_count) {
|
2002-10-01 02:22:10 +05:30
|
|
|
do_stats();
|
|
|
|
sleep(1);
|
|
|
|
clearmems();
|
|
|
|
continue;
|
2006-02-14 03:34:27 +05:30
|
|
|
}
|
2002-10-01 02:22:10 +05:30
|
|
|
do_stats();
|
2006-04-25 04:43:46 +05:30
|
|
|
qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp);
|
2002-10-01 02:22:10 +05:30
|
|
|
#else
|
2002-10-22 17:51:15 +05:30
|
|
|
qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function);
|
2005-09-16 18:25:29 +05:30
|
|
|
#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */
|
2002-10-01 02:22:10 +05:30
|
|
|
opt = lines;
|
|
|
|
if (opt > ntop) {
|
|
|
|
opt = ntop;
|
|
|
|
}
|
|
|
|
/* show status for each of the processes */
|
|
|
|
display_status(opt, col);
|
2005-07-30 15:12:05 +05:30
|
|
|
#ifdef CONFIG_FEATURE_USE_TERMIOS
|
2002-10-01 02:22:10 +05:30
|
|
|
tv.tv_sec = interval;
|
|
|
|
tv.tv_usec = 0;
|
2006-04-25 04:43:46 +05:30
|
|
|
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 15:12:05 +05:30
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2006-04-25 04:43:46 +05:30
|
|
|
if (c == 'q' || c == initial_settings.c_cc[VINTR])
|
2006-02-15 22:45:56 +05:30
|
|
|
break;
|
2006-04-25 04:43:46 +05:30
|
|
|
if (c == 'M') {
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-01 02:22:10 +05:30
|
|
|
sort_function[0] = mem_sort;
|
|
|
|
sort_function[1] = pcpu_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
#else
|
|
|
|
sort_function = mem_sort;
|
|
|
|
#endif
|
|
|
|
}
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2006-04-25 04:43:46 +05:30
|
|
|
if (c == 'P') {
|
2002-10-01 02:22:10 +05:30
|
|
|
sort_function[0] = pcpu_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = time_sort;
|
|
|
|
}
|
2006-04-25 04:43:46 +05:30
|
|
|
if (c == 'T') {
|
2002-10-01 02:22:10 +05:30
|
|
|
sort_function[0] = time_sort;
|
|
|
|
sort_function[1] = mem_sort;
|
|
|
|
sort_function[2] = pcpu_sort;
|
|
|
|
}
|
|
|
|
#endif
|
2006-04-25 04:43:46 +05:30
|
|
|
if (c == 'N') {
|
2005-09-16 18:25:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
|
2002-10-01 02:22:10 +05:30
|
|
|
sort_function[0] = pid_sort;
|
|
|
|
#else
|
|
|
|
sort_function = pid_sort;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2002-09-18 03:44:58 +05:30
|
|
|
sleep(interval);
|
2005-07-30 15:12:05 +05:30
|
|
|
#endif /* CONFIG_FEATURE_USE_TERMIOS */
|
2002-10-01 02:22:10 +05:30
|
|
|
clearmems();
|
2002-09-18 03:44:58 +05:30
|
|
|
}
|
2006-04-25 04:43:46 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
2006-02-15 22:45:56 +05:30
|
|
|
clearmems();
|
|
|
|
putchar('\n');
|
2002-09-18 03:44:58 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|