2006-03-16 02:29:51 +01:00
|
|
|
|
2006-03-17 08:54:55 +01:00
|
|
|
/* Author: Ben Gras <beng@few.vu.nl> 17 march 2006 */
|
2010-09-14 23:30:23 +02:00
|
|
|
/* Modified for ProcFS by Alen Stojanov and David van Moolenbroek */
|
2006-03-17 08:54:55 +01:00
|
|
|
|
2006-03-16 02:29:51 +01:00
|
|
|
#include <stdio.h>
|
2011-07-21 16:29:08 +02:00
|
|
|
#include <unistd.h>
|
2006-03-16 02:29:51 +01:00
|
|
|
#include <pwd.h>
|
2006-03-29 01:42:55 +02:00
|
|
|
#include <curses.h>
|
2006-03-16 02:29:51 +01:00
|
|
|
#include <timers.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <termcap.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
2006-03-29 01:42:55 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2010-09-14 23:30:23 +02:00
|
|
|
#include <dirent.h>
|
2011-02-08 15:00:48 +01:00
|
|
|
#include <assert.h>
|
2006-03-16 02:29:51 +01:00
|
|
|
|
|
|
|
#include <sys/ioc_tty.h>
|
|
|
|
#include <sys/times.h>
|
|
|
|
#include <sys/types.h>
|
2006-03-29 01:42:55 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/select.h>
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2008-12-11 15:27:18 +01:00
|
|
|
#include <minix/com.h>
|
2006-03-16 02:29:51 +01:00
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/type.h>
|
2010-09-14 23:30:23 +02:00
|
|
|
#include <minix/endpoint.h>
|
2006-03-16 02:29:51 +01:00
|
|
|
#include <minix/const.h>
|
2010-02-10 16:38:27 +01:00
|
|
|
#include <minix/u64.h>
|
2010-11-06 21:40:15 +01:00
|
|
|
#include <paths.h>
|
2010-09-14 23:30:23 +02:00
|
|
|
#include <minix/procfs.h>
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
#define TIMECYCLEKEY 't'
|
2012-09-24 21:05:01 +02:00
|
|
|
#define ORDERKEY 'o'
|
2011-02-08 15:00:48 +01:00
|
|
|
|
2012-09-11 02:17:25 +02:00
|
|
|
#define ORDER_CPU 0
|
|
|
|
#define ORDER_MEMORY 1
|
|
|
|
#define ORDER_HIGHEST ORDER_MEMORY
|
|
|
|
int order = ORDER_CPU;
|
|
|
|
|
2008-12-11 15:27:18 +01:00
|
|
|
u32_t system_hz;
|
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
/* name of cpu cycle types, in the order they appear in /psinfo. */
|
Upgrading build system to new NetBSD revision
The tested targets are the followgin ones:
* tools
* distribution
* sets
* release
The remaining NetBSD targets have not been disabled nor tested
*at all*. Try them at your own risk, they may reboot the earth.
For all compliant Makefiles, objects and generated files are put in
MAKEOBJDIR, which means you can now keep objects between two branch
switching. Same for DESTDIR, please refer to build.sh options.
Regarding new or modifications of Makefiles a few things:
* Read share/mk/bsd.README
* If you add a subdirectory, add a Makefile in it, and have it called
by the parent through the SUBDIR variable.
* Do not add arbitrary inclusion which crosses to another branch of
the hierarchy; If you can't do without it, put a comment on why.
If possible, do not use inclusion at all.
* Use as much as possible the infrastructure, it is here to make
life easier, do not fight it.
Sets and package are now used to track files.
We have one set called "minix", composed of one package called "minix-sys"
2012-09-12 09:37:05 +02:00
|
|
|
const char *cputimenames[] = { "user", "ipc", "kernelcall" };
|
2011-02-08 15:00:48 +01:00
|
|
|
|
|
|
|
#define CPUTIMENAMES (sizeof(cputimenames)/sizeof(cputimenames[0]))
|
|
|
|
|
|
|
|
#define CPUTIME(m, i) (m & (1L << (i)))
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
unsigned int nr_procs, nr_tasks;
|
|
|
|
int nr_total;
|
|
|
|
|
|
|
|
#define SLOT_NR(e) (_ENDPOINT_P(e) + nr_tasks)
|
|
|
|
|
2006-03-16 02:29:51 +01:00
|
|
|
#define TC_BUFFER 1024 /* Size of termcap(3) buffer */
|
|
|
|
#define TC_STRINGS 200 /* Enough room for cm,cl,so,se */
|
|
|
|
|
2006-03-29 01:42:55 +02:00
|
|
|
char *Tclr_all;
|
|
|
|
|
2010-03-03 16:45:43 +01:00
|
|
|
int blockedverbose = 0;
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
#define USED 0x1
|
|
|
|
#define IS_TASK 0x2
|
|
|
|
#define IS_SYSTEM 0x4
|
|
|
|
#define BLOCKED 0x8
|
|
|
|
|
|
|
|
struct proc {
|
|
|
|
int p_flags;
|
|
|
|
endpoint_t p_endpoint;
|
|
|
|
pid_t p_pid;
|
2011-02-08 15:00:48 +01:00
|
|
|
u64_t p_cpucycles[CPUTIMENAMES];
|
2010-09-14 23:30:23 +02:00
|
|
|
int p_priority;
|
|
|
|
endpoint_t p_blocked;
|
|
|
|
time_t p_user_time;
|
|
|
|
vir_bytes p_memory;
|
|
|
|
uid_t p_effuid;
|
|
|
|
int p_nice;
|
|
|
|
char p_name[PROC_NAME_LEN+1];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct proc *proc = NULL, *prev_proc = NULL;
|
|
|
|
|
|
|
|
void parse_file(pid_t pid)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX], name[256], type, state;
|
|
|
|
int version, endpt, effuid;
|
|
|
|
unsigned long cycles_hi, cycles_lo;
|
|
|
|
FILE *fp;
|
|
|
|
struct proc *p;
|
2011-06-22 15:46:33 +02:00
|
|
|
int slot;
|
2011-02-08 15:00:48 +01:00
|
|
|
int i;
|
2010-09-14 23:30:23 +02:00
|
|
|
|
|
|
|
sprintf(path, "%d/psinfo", pid);
|
|
|
|
|
|
|
|
if ((fp = fopen(path, "r")) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (fscanf(fp, "%d", &version) != 1) {
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version != PSINFO_VERSION) {
|
|
|
|
fputs("procfs version mismatch!\n", stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fscanf(fp, " %c %d", &type, &endpt) != 2) {
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-22 15:46:33 +02:00
|
|
|
slot = SLOT_NR(endpt);
|
|
|
|
|
|
|
|
if(slot < 0 || slot >= nr_total) {
|
|
|
|
fprintf(stderr, "top: unreasonable endpoint number %d\n", endpt);
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = &proc[slot];
|
2010-09-14 23:30:23 +02:00
|
|
|
|
|
|
|
if (type == TYPE_TASK)
|
|
|
|
p->p_flags |= IS_TASK;
|
|
|
|
else if (type == TYPE_SYSTEM)
|
|
|
|
p->p_flags |= IS_SYSTEM;
|
|
|
|
|
|
|
|
p->p_endpoint = endpt;
|
|
|
|
p->p_pid = pid;
|
|
|
|
|
|
|
|
if (fscanf(fp, " %255s %c %d %d %lu %*u %lu %lu",
|
|
|
|
name, &state, &p->p_blocked, &p->p_priority,
|
|
|
|
&p->p_user_time, &cycles_hi, &cycles_lo) != 7) {
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(p->p_name, name, sizeof(p->p_name)-1);
|
|
|
|
p->p_name[sizeof(p->p_name)-1] = 0;
|
|
|
|
|
|
|
|
if (state != STATE_RUN)
|
|
|
|
p->p_flags |= BLOCKED;
|
2011-02-08 15:00:48 +01:00
|
|
|
p->p_cpucycles[0] = make64(cycles_lo, cycles_hi);
|
2010-09-14 23:30:23 +02:00
|
|
|
p->p_memory = 0L;
|
|
|
|
|
|
|
|
if (!(p->p_flags & IS_TASK)) {
|
Upgrading build system to new NetBSD revision
The tested targets are the followgin ones:
* tools
* distribution
* sets
* release
The remaining NetBSD targets have not been disabled nor tested
*at all*. Try them at your own risk, they may reboot the earth.
For all compliant Makefiles, objects and generated files are put in
MAKEOBJDIR, which means you can now keep objects between two branch
switching. Same for DESTDIR, please refer to build.sh options.
Regarding new or modifications of Makefiles a few things:
* Read share/mk/bsd.README
* If you add a subdirectory, add a Makefile in it, and have it called
by the parent through the SUBDIR variable.
* Do not add arbitrary inclusion which crosses to another branch of
the hierarchy; If you can't do without it, put a comment on why.
If possible, do not use inclusion at all.
* Use as much as possible the infrastructure, it is here to make
life easier, do not fight it.
Sets and package are now used to track files.
We have one set called "minix", composed of one package called "minix-sys"
2012-09-12 09:37:05 +02:00
|
|
|
int j;
|
|
|
|
if ((j=fscanf(fp, " %lu %*u %*u %*c %*d %*u %u %*u %d %*c %*d %*u",
|
2011-02-08 15:00:48 +01:00
|
|
|
&p->p_memory, &effuid, &p->p_nice)) != 3) {
|
2010-09-14 23:30:23 +02:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->p_effuid = effuid;
|
2012-09-19 19:38:19 +02:00
|
|
|
} else p->p_effuid = 0;
|
2010-09-14 23:30:23 +02:00
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
for(i = 1; i < CPUTIMENAMES; i++) {
|
|
|
|
if(fscanf(fp, " %lu %lu",
|
|
|
|
&cycles_hi, &cycles_lo) == 2) {
|
|
|
|
p->p_cpucycles[i] = make64(cycles_lo, cycles_hi);
|
|
|
|
} else {
|
2012-09-24 21:05:01 +02:00
|
|
|
p->p_cpucycles[i] = 0;
|
2011-02-08 15:00:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 22:19:22 +02:00
|
|
|
if ((p->p_flags & IS_TASK)) {
|
2012-09-19 13:07:18 +02:00
|
|
|
if(fscanf(fp, " %lu", &p->p_memory) != 1) {
|
|
|
|
p->p_memory = 0;
|
|
|
|
}
|
2012-09-18 22:19:22 +02:00
|
|
|
}
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
p->p_flags |= USED;
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_dir(void)
|
|
|
|
{
|
|
|
|
DIR *p_dir;
|
|
|
|
struct dirent *p_ent;
|
|
|
|
pid_t pid;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
if ((p_dir = opendir(".")) == NULL) {
|
|
|
|
perror("opendir on " _PATH_PROC);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p_ent = readdir(p_dir); p_ent != NULL; p_ent = readdir(p_dir)) {
|
|
|
|
pid = strtol(p_ent->d_name, &end, 10);
|
|
|
|
|
|
|
|
if (!end[0] && pid != 0)
|
|
|
|
parse_file(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(p_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_procs(void)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
p = prev_proc;
|
|
|
|
prev_proc = proc;
|
|
|
|
proc = p;
|
|
|
|
|
|
|
|
if (proc == NULL) {
|
|
|
|
proc = malloc(nr_total * sizeof(proc[0]));
|
|
|
|
|
|
|
|
if (proc == NULL) {
|
|
|
|
fprintf(stderr, "Out of memory!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nr_total; i++)
|
|
|
|
proc[i].p_flags = 0;
|
|
|
|
|
|
|
|
parse_dir();
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_memory(void)
|
2006-03-16 02:29:51 +01:00
|
|
|
{
|
2010-09-14 23:30:23 +02:00
|
|
|
FILE *fp;
|
2012-09-24 21:05:01 +02:00
|
|
|
unsigned int pagesize;
|
2010-09-14 23:30:23 +02:00
|
|
|
unsigned long total, free, largest, cached;
|
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
if ((fp = fopen("meminfo", "r")) == NULL)
|
2010-09-14 23:30:23 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free,
|
|
|
|
&largest, &cached) != 5) {
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2011-07-21 16:29:08 +02:00
|
|
|
printf("main memory: %ldK total, %ldK free, %ldK contig free, "
|
|
|
|
"%ldK cached\n",
|
2010-09-14 23:30:23 +02:00
|
|
|
(pagesize * total)/1024, (pagesize * free)/1024,
|
|
|
|
(pagesize * largest)/1024, (pagesize * cached)/1024);
|
2006-03-16 02:29:51 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_load(double *loads, int nloads)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
printf("load averages: ");
|
|
|
|
for(i = 0; i < nloads; i++)
|
|
|
|
printf("%s %.2f", (i > 0) ? "," : "", loads[i]);
|
|
|
|
printf("\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_proc_summary(struct proc *proc)
|
|
|
|
{
|
|
|
|
int p, alive, running, sleeping;
|
|
|
|
|
|
|
|
alive = running = sleeping = 0;
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
for(p = 0; p < nr_total; p++) {
|
|
|
|
if (proc[p].p_endpoint == IDLE)
|
2006-03-16 02:29:51 +01:00
|
|
|
continue;
|
2010-09-14 23:30:23 +02:00
|
|
|
if(!(proc[p].p_flags & USED))
|
2006-03-16 02:29:51 +01:00
|
|
|
continue;
|
|
|
|
alive++;
|
2010-09-14 23:30:23 +02:00
|
|
|
if(proc[p].p_flags & BLOCKED)
|
2006-03-16 02:29:51 +01:00
|
|
|
sleeping++;
|
|
|
|
else
|
|
|
|
running++;
|
|
|
|
}
|
|
|
|
printf("%d processes: %d running, %d sleeping\n",
|
|
|
|
alive, running, sleeping);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
struct tp {
|
2006-03-16 02:29:51 +01:00
|
|
|
struct proc *p;
|
2010-02-10 16:38:27 +01:00
|
|
|
u64_t ticks;
|
2010-03-03 16:45:43 +01:00
|
|
|
};
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2012-09-11 02:17:25 +02:00
|
|
|
int cmp_procs(const void *v1, const void *v2)
|
2006-03-16 02:29:51 +01:00
|
|
|
{
|
|
|
|
struct tp *p1 = (struct tp *) v1, *p2 = (struct tp *) v2;
|
2010-03-03 16:45:43 +01:00
|
|
|
int p1blocked, p2blocked;
|
|
|
|
|
2012-09-11 02:17:25 +02:00
|
|
|
if(order == ORDER_MEMORY) {
|
|
|
|
if(p1->p->p_memory < p2->p->p_memory) return 1;
|
|
|
|
if(p1->p->p_memory > p2->p->p_memory) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
p1blocked = !!(p1->p->p_flags & BLOCKED);
|
|
|
|
p2blocked = !!(p2->p->p_flags & BLOCKED);
|
2010-03-03 16:45:43 +01:00
|
|
|
|
|
|
|
/* Primarily order by used number of cpu cycles.
|
2012-09-24 21:05:01 +02:00
|
|
|
*
|
2010-03-03 16:45:43 +01:00
|
|
|
* Exception: if in blockedverbose mode, a blocked
|
|
|
|
* process is always printed after an unblocked
|
|
|
|
* process, and used cpu cycles don't matter.
|
|
|
|
*
|
|
|
|
* In both cases, process slot number is a tie breaker.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(blockedverbose && (p1blocked || p2blocked)) {
|
2012-09-11 15:39:00 +02:00
|
|
|
if(!p1blocked && p2blocked)
|
2010-02-10 16:38:27 +01:00
|
|
|
return -1;
|
2012-09-11 15:39:00 +02:00
|
|
|
if(!p2blocked && p1blocked)
|
2010-03-03 16:45:43 +01:00
|
|
|
return 1;
|
2012-09-24 21:05:01 +02:00
|
|
|
} else if(p1->ticks != p2->ticks) {
|
|
|
|
return (p2->ticks - p1->ticks);
|
|
|
|
}
|
2010-03-03 16:45:43 +01:00
|
|
|
|
|
|
|
/* Process slot number is a tie breaker. */
|
2010-09-14 23:30:23 +02:00
|
|
|
return (int) (p1->p - p2->p);
|
2010-03-03 16:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct tp *lookup(endpoint_t who, struct tp *tptab, int np)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
for(t = 0; t < np; t++)
|
|
|
|
if(who == tptab[t].p->p_endpoint)
|
|
|
|
return &tptab[t];
|
|
|
|
|
|
|
|
fprintf(stderr, "lookup: tp %d (0x%x) not found.\n", who, who);
|
|
|
|
abort();
|
|
|
|
|
|
|
|
return NULL;
|
2006-03-16 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
double ktotal = 0;
|
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
void print_proc(struct tp *tp, u64_t total_ticks)
|
2010-03-03 16:45:43 +01:00
|
|
|
{
|
|
|
|
int euid = 0;
|
|
|
|
static struct passwd *who = NULL;
|
|
|
|
static int last_who = -1;
|
|
|
|
char *name = "";
|
|
|
|
int ticks;
|
|
|
|
struct proc *pr = tp->p;
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
printf("%5d ", pr->p_pid);
|
|
|
|
euid = pr->p_effuid;
|
|
|
|
name = pr->p_name;
|
2010-03-03 16:45:43 +01:00
|
|
|
|
|
|
|
if(last_who != euid || !who) {
|
|
|
|
who = getpwuid(euid);
|
|
|
|
last_who = euid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(who && who->pw_name) printf("%-8s ", who->pw_name);
|
2010-09-14 23:30:23 +02:00
|
|
|
else if(!(pr->p_flags & IS_TASK)) printf("%8d ", pr->p_effuid);
|
2010-03-03 16:45:43 +01:00
|
|
|
else printf(" ");
|
|
|
|
|
|
|
|
printf(" %2d ", pr->p_priority);
|
2010-09-14 23:30:23 +02:00
|
|
|
if(!(pr->p_flags & IS_TASK)) {
|
|
|
|
printf(" %3d ", pr->p_nice);
|
2010-03-03 16:45:43 +01:00
|
|
|
} else printf(" ");
|
2011-07-15 14:15:15 +02:00
|
|
|
printf("%6ldK", (pr->p_memory + 512) / 1024);
|
2010-09-14 23:30:23 +02:00
|
|
|
printf("%6s", (pr->p_flags & BLOCKED) ? "" : "RUN");
|
2010-03-03 16:45:43 +01:00
|
|
|
ticks = pr->p_user_time;
|
2011-07-21 16:29:08 +02:00
|
|
|
printf(" %3u:%02u ", (ticks/system_hz/60), (ticks/system_hz)%60);
|
2010-03-03 16:45:43 +01:00
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
printf("%6.2f%% %s", 100.0 * tp->ticks / total_ticks, name);
|
2010-03-03 16:45:43 +01:00
|
|
|
}
|
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
char *cputimemodename(int cputimemode)
|
|
|
|
{
|
|
|
|
static char name[100];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
name[0] = '\0';
|
2012-09-24 21:05:01 +02:00
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
for(i = 0; i < CPUTIMENAMES; i++) {
|
|
|
|
if(CPUTIME(cputimemode, i)) {
|
|
|
|
assert(strlen(name) +
|
|
|
|
strlen(cputimenames[i]) < sizeof(name));
|
|
|
|
strcat(name, cputimenames[i]);
|
|
|
|
strcat(name, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64_t cputicks(struct proc *p1, struct proc *p2, int timemode)
|
|
|
|
{
|
|
|
|
int i;
|
2012-09-24 21:05:01 +02:00
|
|
|
u64_t t = 0;
|
2011-02-08 15:00:48 +01:00
|
|
|
for(i = 0; i < CPUTIMENAMES; i++) {
|
2012-09-24 21:05:01 +02:00
|
|
|
if(!CPUTIME(timemode, i))
|
2011-02-08 15:00:48 +01:00
|
|
|
continue;
|
|
|
|
if(p1->p_endpoint == p2->p_endpoint) {
|
2012-09-24 21:05:01 +02:00
|
|
|
t = t + p2->p_cpucycles[i] - p1->p_cpucycles[i];
|
2011-02-08 15:00:48 +01:00
|
|
|
} else {
|
2012-09-24 21:05:01 +02:00
|
|
|
t = t + p2->p_cpucycles[i];
|
2011-02-08 15:00:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
char *ordername(int orderno)
|
|
|
|
{
|
|
|
|
switch(orderno) {
|
|
|
|
case ORDER_CPU: return "cpu";
|
|
|
|
case ORDER_MEMORY: return "memory";
|
|
|
|
}
|
|
|
|
return "invalid order";
|
|
|
|
}
|
|
|
|
|
2006-03-16 02:29:51 +01:00
|
|
|
void print_procs(int maxlines,
|
2011-02-08 15:00:48 +01:00
|
|
|
struct proc *proc1, struct proc *proc2, int cputimemode)
|
2006-03-16 02:29:51 +01:00
|
|
|
{
|
2010-09-14 23:30:23 +02:00
|
|
|
int p, nprocs;
|
2012-09-24 21:05:01 +02:00
|
|
|
u64_t idleticks = 0;
|
|
|
|
u64_t kernelticks = 0;
|
|
|
|
u64_t systemticks = 0;
|
|
|
|
u64_t userticks = 0;
|
|
|
|
u64_t total_ticks = 0;
|
2010-03-03 16:45:43 +01:00
|
|
|
int blockedseen = 0;
|
2010-09-14 23:30:23 +02:00
|
|
|
static struct tp *tick_procs = NULL;
|
2006-03-29 01:44:10 +02:00
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
if (tick_procs == NULL) {
|
|
|
|
tick_procs = malloc(nr_total * sizeof(tick_procs[0]));
|
|
|
|
|
|
|
|
if (tick_procs == NULL) {
|
|
|
|
fprintf(stderr, "Out of memory!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(p = nprocs = 0; p < nr_total; p++) {
|
2011-02-08 15:00:48 +01:00
|
|
|
u64_t uticks;
|
2010-09-14 23:30:23 +02:00
|
|
|
if(!(proc2[p].p_flags & USED))
|
2006-03-16 02:29:51 +01:00
|
|
|
continue;
|
|
|
|
tick_procs[nprocs].p = proc2 + p;
|
2011-02-08 15:00:48 +01:00
|
|
|
tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
|
|
|
|
uticks = cputicks(&proc1[p], &proc2[p], 1);
|
2012-09-24 21:05:01 +02:00
|
|
|
total_ticks = total_ticks + uticks;
|
2006-03-16 02:29:51 +01:00
|
|
|
if(p-NR_TASKS == IDLE) {
|
2011-02-08 15:00:48 +01:00
|
|
|
idleticks = uticks;
|
2006-03-16 02:29:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-02-10 16:38:27 +01:00
|
|
|
if(p-NR_TASKS == KERNEL) {
|
2011-02-08 15:00:48 +01:00
|
|
|
kernelticks = uticks;
|
2010-02-10 16:38:27 +01:00
|
|
|
}
|
2010-09-14 23:30:23 +02:00
|
|
|
if(!(proc2[p].p_flags & IS_TASK)) {
|
|
|
|
if(proc2[p].p_flags & IS_SYSTEM)
|
2012-09-24 21:05:01 +02:00
|
|
|
systemticks = systemticks + tick_procs[nprocs].ticks;
|
2010-09-14 23:30:23 +02:00
|
|
|
else
|
2012-09-24 21:05:01 +02:00
|
|
|
userticks = userticks + tick_procs[nprocs].ticks;
|
2010-09-14 23:30:23 +02:00
|
|
|
}
|
2006-03-17 11:04:14 +01:00
|
|
|
|
2006-03-16 02:29:51 +01:00
|
|
|
nprocs++;
|
|
|
|
}
|
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
if (total_ticks == 0)
|
2010-02-10 16:38:27 +01:00
|
|
|
return;
|
|
|
|
|
2012-09-11 02:17:25 +02:00
|
|
|
qsort(tick_procs, nprocs, sizeof(tick_procs[0]), cmp_procs);
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks);
|
|
|
|
printf("%6.2f%% system, ", 100.0 * systemticks / total_ticks);
|
|
|
|
printf("%6.2f%% kernel, ", 100.0 * kernelticks/ total_ticks);
|
|
|
|
printf("%6.2f%% idle", 100.0 * idleticks / total_ticks);
|
2010-02-10 16:38:27 +01:00
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
#define NEWLINE do { printf("\n"); if(--maxlines <= 0) { return; } } while(0)
|
2010-03-03 16:45:43 +01:00
|
|
|
NEWLINE;
|
2011-02-08 15:00:48 +01:00
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
printf("CPU time displayed ('%c' to cycle): %s; ",
|
2011-02-08 15:00:48 +01:00
|
|
|
TIMECYCLEKEY, cputimemodename(cputimemode));
|
2012-09-24 21:05:01 +02:00
|
|
|
printf(" sort order ('%c' to cycle): %s", ORDERKEY, ordername(order));
|
2011-02-08 15:00:48 +01:00
|
|
|
NEWLINE;
|
|
|
|
|
2010-03-03 16:45:43 +01:00
|
|
|
NEWLINE;
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2011-07-15 14:15:15 +02:00
|
|
|
printf(" PID USERNAME PRI NICE SIZE STATE TIME CPU COMMAND");
|
2010-03-03 16:45:43 +01:00
|
|
|
NEWLINE;
|
2006-03-16 02:29:51 +01:00
|
|
|
for(p = 0; p < nprocs; p++) {
|
|
|
|
struct proc *pr;
|
2010-03-03 16:45:43 +01:00
|
|
|
int level = 0;
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
pr = tick_procs[p].p;
|
2010-03-03 16:45:43 +01:00
|
|
|
|
2012-09-18 22:19:22 +02:00
|
|
|
if((pr->p_flags & IS_TASK) && pr->p_pid != KERNEL) {
|
2010-02-10 16:38:27 +01:00
|
|
|
/* skip old kernel tasks as they don't run anymore */
|
|
|
|
continue;
|
2006-03-29 14:44:06 +02:00
|
|
|
}
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2010-03-03 16:45:43 +01:00
|
|
|
/* If we're in blocked verbose mode, indicate start of
|
|
|
|
* blocked processes.
|
|
|
|
*/
|
2010-09-14 23:30:23 +02:00
|
|
|
if(blockedverbose && (pr->p_flags & BLOCKED) && !blockedseen) {
|
2010-03-03 16:45:43 +01:00
|
|
|
NEWLINE;
|
|
|
|
printf("Blocked processes:");
|
|
|
|
NEWLINE;
|
|
|
|
blockedseen = 1;
|
|
|
|
}
|
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
print_proc(&tick_procs[p], total_ticks);
|
2010-03-03 16:45:43 +01:00
|
|
|
NEWLINE;
|
|
|
|
|
|
|
|
if(!blockedverbose)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Traverse dependency chain if blocked. */
|
2010-09-14 23:30:23 +02:00
|
|
|
while(pr->p_flags & BLOCKED) {
|
2010-03-03 16:45:43 +01:00
|
|
|
endpoint_t dep = NONE;
|
|
|
|
struct tp *tpdep;
|
|
|
|
level += 5;
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
if((dep = pr->p_blocked) == NONE) {
|
2010-03-03 16:45:43 +01:00
|
|
|
printf("not blocked on a process");
|
|
|
|
NEWLINE;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2010-03-03 16:45:43 +01:00
|
|
|
if(dep == ANY)
|
|
|
|
break;
|
2010-02-10 16:38:27 +01:00
|
|
|
|
2010-03-03 16:45:43 +01:00
|
|
|
tpdep = lookup(dep, tick_procs, nprocs);
|
|
|
|
pr = tpdep->p;
|
|
|
|
printf("%*s> ", level, "");
|
2012-09-24 21:05:01 +02:00
|
|
|
print_proc(tpdep, total_ticks);
|
2010-03-03 16:45:43 +01:00
|
|
|
NEWLINE;
|
|
|
|
}
|
2006-03-16 02:29:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
void showtop(int cputimemode, int r)
|
2006-03-16 02:29:51 +01:00
|
|
|
{
|
|
|
|
#define NLOADS 3
|
|
|
|
double loads[NLOADS];
|
2010-09-14 23:30:23 +02:00
|
|
|
int nloads, lines = 0;
|
2006-03-16 02:29:51 +01:00
|
|
|
struct winsize winsize;
|
|
|
|
|
|
|
|
if(ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize) != 0) {
|
|
|
|
perror("TIOCGWINSZ");
|
|
|
|
fprintf(stderr, "TIOCGWINSZ failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
get_procs();
|
|
|
|
if (prev_proc == NULL)
|
|
|
|
get_procs();
|
2006-03-16 02:29:51 +01:00
|
|
|
|
|
|
|
if((nloads = getloadavg(loads, NLOADS)) != NLOADS) {
|
|
|
|
fprintf(stderr, "getloadavg() failed - %d loads\n", nloads);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("%s", Tclr_all);
|
|
|
|
|
|
|
|
lines += print_load(loads, NLOADS);
|
|
|
|
lines += print_proc_summary(proc);
|
2010-09-14 23:30:23 +02:00
|
|
|
lines += print_memory();
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2006-03-22 16:32:44 +01:00
|
|
|
if(winsize.ws_row > 0) r = winsize.ws_row;
|
|
|
|
|
2011-02-08 15:00:48 +01:00
|
|
|
print_procs(r - lines - 2, prev_proc, proc, cputimemode);
|
2011-08-04 03:02:02 +02:00
|
|
|
fflush(NULL);
|
2006-03-16 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
2006-03-22 16:32:44 +01:00
|
|
|
void init(int *rows)
|
2006-03-16 02:29:51 +01:00
|
|
|
{
|
|
|
|
char *term;
|
|
|
|
static char buffer[TC_BUFFER], strings[TC_STRINGS];
|
2006-03-22 16:32:44 +01:00
|
|
|
char *s = strings, *v;
|
|
|
|
|
|
|
|
*rows = 0;
|
2006-03-16 02:29:51 +01:00
|
|
|
|
|
|
|
if(!(term = getenv("TERM"))) {
|
|
|
|
fprintf(stderr, "No TERM set\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( tgetent( buffer, term ) != 1 ) {
|
|
|
|
fprintf(stderr, "tgetent failed for term %s\n", term);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-08-04 03:02:02 +02:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
|
2006-03-16 02:29:51 +01:00
|
|
|
if ( (Tclr_all = tgetstr( "cl", &s )) == NULL )
|
|
|
|
Tclr_all = "\f";
|
2006-03-22 16:32:44 +01:00
|
|
|
|
|
|
|
if((v = tgetstr ("li", &s)) != NULL)
|
|
|
|
sscanf(v, "%d", rows);
|
|
|
|
if(*rows < 1) *rows = 24;
|
2006-03-16 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
2006-03-29 01:42:55 +02:00
|
|
|
void sigwinch(int sig) { }
|
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
void getkinfo(void)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen("kinfo", "r")) == NULL) {
|
2010-11-06 21:40:15 +01:00
|
|
|
fprintf(stderr, "opening " _PATH_PROC "kinfo failed\n");
|
2010-09-14 23:30:23 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) {
|
2010-11-06 21:40:15 +01:00
|
|
|
fprintf(stderr, "reading from " _PATH_PROC "kinfo failed\n");
|
2010-09-14 23:30:23 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
nr_total = (int) (nr_procs + nr_tasks);
|
|
|
|
}
|
|
|
|
|
2006-03-16 02:29:51 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2010-09-14 23:30:23 +02:00
|
|
|
int r, c, s = 0;
|
2011-02-08 15:00:48 +01:00
|
|
|
int cputimemode = 1; /* bitmap. */
|
2010-09-14 23:30:23 +02:00
|
|
|
|
|
|
|
if (chdir(_PATH_PROC) != 0) {
|
|
|
|
perror("chdir to " _PATH_PROC);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
system_hz = (u32_t) sysconf(_SC_CLK_TCK);
|
2006-03-29 01:42:55 +02:00
|
|
|
|
2010-09-14 23:30:23 +02:00
|
|
|
getkinfo();
|
2008-12-11 15:27:18 +01:00
|
|
|
|
2006-03-22 16:32:44 +01:00
|
|
|
init(&r);
|
2006-03-16 02:29:51 +01:00
|
|
|
|
2010-03-03 16:45:43 +01:00
|
|
|
while((c=getopt(argc, argv, "s:B")) != EOF) {
|
2006-03-29 01:42:55 +02:00
|
|
|
switch(c) {
|
|
|
|
case 's':
|
|
|
|
s = atoi(optarg);
|
|
|
|
break;
|
2010-03-03 16:45:43 +01:00
|
|
|
case 'B':
|
|
|
|
blockedverbose = 1;
|
|
|
|
break;
|
2006-03-29 01:42:55 +02:00
|
|
|
default:
|
|
|
|
fprintf(stderr,
|
2010-03-03 16:45:43 +01:00
|
|
|
"Usage: %s [-s<secdelay>] [-B]\n",
|
|
|
|
argv[0]);
|
2006-03-29 01:42:55 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 21:05:01 +02:00
|
|
|
if(s < 1)
|
2006-03-29 01:42:55 +02:00
|
|
|
s = 2;
|
|
|
|
|
2010-03-03 16:45:43 +01:00
|
|
|
/* Catch window size changes so display is updated properly
|
|
|
|
* right away.
|
|
|
|
*/
|
2006-03-29 01:42:55 +02:00
|
|
|
signal(SIGWINCH, sigwinch);
|
|
|
|
|
2006-03-16 02:29:51 +01:00
|
|
|
while(1) {
|
2006-03-29 01:42:55 +02:00
|
|
|
fd_set fds;
|
|
|
|
int ns;
|
|
|
|
struct timeval tv;
|
2011-02-08 15:00:48 +01:00
|
|
|
showtop(cputimemode, r);
|
2006-03-29 01:42:55 +02:00
|
|
|
tv.tv_sec = s;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(STDIN_FILENO, &fds);
|
2011-07-21 16:29:08 +02:00
|
|
|
|
2006-03-29 01:42:55 +02:00
|
|
|
if((ns=select(STDIN_FILENO+1, &fds, NULL, NULL, &tv)) < 0
|
|
|
|
&& errno != EINTR) {
|
|
|
|
perror("select");
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ns > 0 && FD_ISSET(STDIN_FILENO, &fds)) {
|
|
|
|
char c;
|
|
|
|
if(read(STDIN_FILENO, &c, 1) == 1) {
|
|
|
|
switch(c) {
|
|
|
|
case 'q':
|
2010-09-14 23:30:23 +02:00
|
|
|
putchar('\r');
|
2006-03-29 01:42:55 +02:00
|
|
|
return 0;
|
|
|
|
break;
|
2012-09-11 02:17:25 +02:00
|
|
|
case 'o':
|
|
|
|
order++;
|
|
|
|
if(order > ORDER_HIGHEST)
|
|
|
|
order = 0;
|
|
|
|
break;
|
2011-02-08 15:00:48 +01:00
|
|
|
case TIMECYCLEKEY:
|
|
|
|
cputimemode++;
|
|
|
|
if(cputimemode >= (1L << CPUTIMENAMES))
|
|
|
|
cputimemode = 1;
|
|
|
|
break;
|
2006-03-29 01:42:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-03-16 02:29:51 +01:00
|
|
|
}
|
2006-03-29 01:42:55 +02:00
|
|
|
|
|
|
|
return 0;
|
2006-03-16 02:29:51 +01:00
|
|
|
}
|
|
|
|
|