home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-08-08 | 54.6 KB | 2,214 lines |
- Newsgroups: comp.sources.unix
- From: phil@eecs.nwu.edu (William LeFebvre)
- Subject: v27i006: top - a top process display, version 3.2, Part06/13
- References: <1.744843136.4744@gw.home.vix.com>
- Sender: unix-sources-moderator@gw.home.vix.com
- Approved: vixie@gw.home.vix.com
-
- Submitted-By: phil@eecs.nwu.edu (William LeFebvre)
- Posting-Number: Volume 27, Issue 6
- Archive-Name: top-3.2/part06
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 6 (of 13)."
- # Contents: machine/m_386bsd.c machine/m_bsd386.c machine/m_dynix.c
- # Wrapped by phil@pex on Wed Aug 4 14:22:43 1993
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'machine/m_386bsd.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'machine/m_386bsd.c'\"
- else
- echo shar: Extracting \"'machine/m_386bsd.c'\" \(17022 characters\)
- sed "s/^X//" >'machine/m_386bsd.c' <<'END_OF_FILE'
- X/*
- X * top - a top users display for Unix
- X *
- X * SYNOPSIS: For a 386BSD system
- X * Note memory statistisc and process sizes could be wrong,
- X * by ps gets them wrong too...
- X *
- X * DESCRIPTION:
- X * This is the machine-dependent module for BSD4.3 NET/2 386bsd
- X * Works for:
- X * i386 PC
- X *
- X * LIBS: -lutil
- X *
- X * AUTHOR: Steve Hocking (adapted from Christos Zoulas <christos@ee.cornell.edu>)
- X * Updated by Andrew Herbert <andrew@werple.apana.org.au>
- X */
- X
- X#include <sys/types.h>
- X#include <sys/signal.h>
- X#include <sys/param.h>
- X
- X#include <stdio.h>
- X#include <nlist.h>
- X#include <math.h>
- X#include <kvm.h>
- X#include <sys/errno.h>
- X#include <sys/kinfo.h>
- X#include <sys/kinfo_proc.h>
- X#ifdef notyet
- X#define time __time
- X#define hz __hz
- X#include <sys/kernel.h>
- X#undef time
- X#undef hz
- X#endif
- X#include <sys/dir.h>
- X#include <sys/dkstat.h>
- X#include <sys/file.h>
- X#include <sys/time.h>
- X#include <sys/vmmeter.h>
- X
- X
- X/* #define PATCHED_KVM /* YOU PROBABLY DON'T NEED THIS DEFINED! */
- X /* define this if you have a kvm.c */
- X /* that has been patched not to freshly */
- X /* alloc an array of procs each time */
- X /* kvm_getprocs is called, but to do a */
- X /* realloc when the no. of processes inc. */
- X
- X/* #define TOTAL_WORKING */ /* Uncomment when the total structure in */
- X /* the kernel actually works */
- X#define DOSWAP
- X
- X#include "top.h"
- X#include "machine.h"
- X
- X#define VMUNIX "/386bsd"
- X#define KMEM "/dev/kmem"
- X#define MEM "/dev/mem"
- X#ifdef DOSWAP
- X#define SWAP "/dev/drum"
- X#endif
- X
- Xtypedef struct _kinfo {
- X struct proc ki_p; /* proc structure */
- X struct eproc ki_e; /* extra stuff */
- X} KINFO;
- X
- X/* get_process_info passes back a handle. This is what it looks like: */
- X
- Xstruct handle
- X{
- X KINFO **next_proc; /* points to next valid proc pointer */
- X int remaining; /* number of pointers remaining */
- X};
- X
- X/* declarations for load_avg */
- X#include "loadavg.h"
- X
- X#define PP(pp, field) ((pp)->ki_p . field)
- X#define EP(pp, field) ((pp)->ki_e . field)
- X#define VP(pp, field) ((pp)->ki_e.e_vm . field)
- X
- X/* define what weighted cpu is. */
- X#define weighted_cpu(pct, pp) (PP((pp), p_time) == 0 ? 0.0 : \
- X ((pct) / (1.0 - exp(PP((pp), p_time) * logcpu))))
- X
- X/* what we consider to be process size: */
- X#define PROCSIZE(pp) (VP((pp), vm_tsize) + VP((pp), vm_dsize) + VP((pp), vm_ssize))
- X
- X/* definitions for indices in the nlist array */
- X#define X_CCPU 0
- X#define X_CP_TIME 1
- X#define X_HZ 2
- X#define X_AVENRUN 3
- X#define X_TOTAL 4
- X#define X_PG_FREE 5
- X#define X_PG_ACTIVE 6
- X#define X_PG_INACTIVE 7
- X#define X_PG_WIRED 8
- X
- Xstatic struct nlist nlst[] = {
- X { "_ccpu" }, /* 0 */
- X { "_cp_time" }, /* 1 */
- X { "_hz" }, /* 2 */
- X { "_averunnable" }, /* 3 */
- X { "_total"}, /* 4 */
- X { "_vm_page_free_count" }, /* 5 */
- X { "_vm_page_active_count" }, /* 6 */
- X { "_vm_page_inactive_count" },/* 7 */
- X { "_vm_page_wire_count" }, /* 8 */
- X { 0 }
- X};
- X
- X/*
- X * These definitions control the format of the per-process area
- X */
- X
- Xstatic char header[] =
- X " PID X PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
- X/* 0123456 -- field to fill in starts at header+6 */
- X#define UNAME_START 6
- X
- X#define Proc_format \
- X "%5d %-8.8s %3d %4d%6dK %4dK %-5s%4d:%02d %5.2f%% %5.2f%% %.14s"
- X
- X
- X/* process state names for the "STATE" column of the display */
- X/* the extra nulls in the string "run" are for adding a slash and
- X the processor number when needed */
- X
- Xchar *state_abbrev[] =
- X{
- X "", "sleep", "WAIT", "run\0\0\0", "start", "zomb", "stop"
- X};
- X
- X
- X/* values that we stash away in _init and use in later routines */
- X
- Xstatic double logcpu;
- X
- X/* these are retrieved from the kernel in _init */
- X
- Xstatic long hz;
- Xstatic load_avg ccpu;
- Xstatic int ncpu = 0;
- X
- X/* these are offsets obtained via nlist and used in the get_ functions */
- X
- Xstatic unsigned long cp_time_offset;
- Xstatic unsigned long avenrun_offset;
- X
- X/* these are for calculating cpu state percentages */
- X
- Xstatic long cp_time[CPUSTATES];
- Xstatic long cp_old[CPUSTATES];
- Xstatic long cp_diff[CPUSTATES];
- X
- X/* these are for detailing the process states */
- X
- Xint process_states[7];
- Xchar *procstatenames[] = {
- X "", " sleeping, ", " ABANDONED, ", " running, ", " starting, ",
- X " zombie, ", " stopped, ",
- X NULL
- X};
- X
- X/* these are for detailing the cpu states */
- X
- Xint cpu_states[4];
- Xchar *cpustatenames[] = {
- X "user", "nice", "system", "idle", NULL
- X};
- X
- X/* these are for detailing the memory statistics */
- X
- Xint memory_stats[8];
- Xchar *memorynames[] = {
- X#ifdef TOTAL_WORKING
- X "Real: ", "K/", "K ", "Virt: ", "K/",
- X "K ", "Free: ", "K", NULL
- X#else
- X " Free: ", "K ", " Active: ", "K ", " Inactive: ",
- X "K ", " Wired: ", "K ", NULL
- X#endif
- X};
- X
- X/* these are for keeping track of the proc array */
- X
- Xstatic int bytes;
- Xstatic int nproc;
- Xstatic int onproc = -1;
- Xstatic int pref_len;
- Xstatic KINFO *pbase;
- Xstatic KINFO **pref;
- X
- X/* these are for getting the memory statistics */
- X
- Xstatic int pageshift; /* log base 2 of the pagesize */
- X
- X/* define pagetok in terms of pageshift */
- X
- X#define pagetok(size) ((size) << pageshift)
- X
- X/* useful externals */
- Xlong percentages();
- X
- Xmachine_init(statics)
- X
- Xstruct statics *statics;
- X
- X{
- X register int i = 0;
- X register int pagesize;
- X char buf[1024];
- X
- X#if 0 /* some funny stuff going on here */
- X if (kvm_openfiles(NULL, NULL, NULL) == -1);
- X return -1;
- X#else
- X kvm_openfiles(VMUNIX, NULL, NULL);
- X#endif
- X
- X /* get the list of symbols we want to access in the kernel */
- X (void) kvm_nlist(nlst);
- X if (nlst[0].n_type == 0)
- X {
- X fprintf(stderr, "top: nlist failed\n");
- X return(-1);
- X }
- X
- X /* make sure they were all found */
- X if (i > 0 && check_nlist(nlst) > 0)
- X {
- X return(-1);
- X }
- X
- X /* get the symbol values out of kmem */
- X (void) getkval(nlst[X_HZ].n_value, (int *)(&hz), sizeof(hz),
- X nlst[X_HZ].n_name);
- X (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu),
- X nlst[X_CCPU].n_name);
- X
- X /* stash away certain offsets for later use */
- X cp_time_offset = nlst[X_CP_TIME].n_value;
- X avenrun_offset = nlst[X_AVENRUN].n_value;
- X
- X /* this is used in calculating WCPU -- calculate it ahead of time */
- X logcpu = log(loaddouble(ccpu));
- X
- X pbase = NULL;
- X pref = NULL;
- X nproc = 0;
- X onproc = -1;
- X /* get the page size with "getpagesize" and calculate pageshift from it */
- X pagesize = getpagesize();
- X pageshift = 0;
- X while (pagesize > 1)
- X {
- X pageshift++;
- X pagesize >>= 1;
- X }
- X
- X /* we only need the amount of log(2)1024 for our conversion */
- X pageshift -= LOG1024;
- X
- X /* fill in the statics information */
- X statics->procstate_names = procstatenames;
- X statics->cpustate_names = cpustatenames;
- X statics->memory_names = memorynames;
- X
- X /* all done! */
- X return(0);
- X}
- X
- Xchar *format_header(uname_field)
- X
- Xregister char *uname_field;
- X
- X{
- X register char *ptr;
- X
- X ptr = header + UNAME_START;
- X while (*uname_field != '\0')
- X {
- X *ptr++ = *uname_field++;
- X }
- X
- X return(header);
- X}
- X
- Xget_system_info(si)
- X
- Xstruct system_info *si;
- X
- X{
- X long total;
- X load_avg avenrun[3];
- X
- X /* get the cp_time array */
- X (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
- X "_cp_time");
- X (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
- X "_avenrun");
- X
- X /* convert load averages to doubles */
- X {
- X register int i;
- X register double *infoloadp;
- X load_avg *avenrunp;
- X
- X#ifdef KINFO_LOADAVG
- X struct loadavg sysload;
- X int size;
- X getkerninfo(KINFO_LOADAVG, &sysload, &size, 0);
- X#endif
- X
- X infoloadp = si->load_avg;
- X avenrunp = avenrun;
- X for (i = 0; i < 3; i++)
- X {
- X#ifdef KINFO_LOADAVG
- X *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
- X#endif
- X *infoloadp++ = loaddouble(*avenrunp++);
- X }
- X }
- X
- X /* convert cp_time counts to percentages */
- X total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
- X
- X /* sum memory statistics */
- X {
- X#ifdef TOTAL_WORKING
- X static struct vmtotal total;
- X int size;
- X
- X /* get total -- systemwide main memory usage structure */
- X#ifdef KINFO_METER
- X getkerninfo(KINFO_METER, &total, &size, 0);
- X#else
- X (void) getkval(nlst[X_TOTAL].n_value, (int *)(&total), sizeof(total),
- X nlst[X_TOTAL].n_name);
- X#endif
- X /* convert memory stats to Kbytes */
- X memory_stats[0] = -1;
- X memory_stats[1] = pagetok(total.t_arm);
- X memory_stats[2] = pagetok(total.t_rm);
- X memory_stats[3] = -1;
- X memory_stats[4] = pagetok(total.t_avm);
- X memory_stats[5] = pagetok(total.t_vm);
- X memory_stats[6] = -1;
- X memory_stats[7] = pagetok(total.t_free);
- X#else
- X static int free, active, inactive, wired;
- X
- X (void) getkval(nlst[X_PG_FREE].n_value, (int *)(&free), sizeof(free),
- X nlst[X_PG_FREE].n_name);
- X (void) getkval(nlst[X_PG_ACTIVE].n_value, (int *)(&active), sizeof(active),
- X nlst[X_PG_ACTIVE].n_name);
- X (void) getkval(nlst[X_PG_INACTIVE].n_value, (int *)(&inactive), sizeof(inactive),
- X nlst[X_PG_INACTIVE].n_name);
- X (void) getkval(nlst[X_PG_WIRED].n_value, (int *)(&wired), sizeof(wired),
- X nlst[X_PG_WIRED].n_name);
- X memory_stats[0] = -1;
- X memory_stats[1] = pagetok(free);
- X memory_stats[2] = -1;
- X memory_stats[3] = pagetok(active);
- X memory_stats[4] = -1;
- X memory_stats[5] = pagetok(inactive);
- X memory_stats[6] = -1;
- X memory_stats[7] = pagetok(wired);
- X#endif
- X }
- X
- X /* set arrays and strings */
- X si->cpustates = cpu_states;
- X si->memory = memory_stats;
- X si->last_pid = -1;
- X}
- X
- Xstatic struct handle handle;
- X
- Xcaddr_t get_process_info(si, sel, compare)
- X
- Xstruct system_info *si;
- Xstruct process_select *sel;
- Xint (*compare)();
- X
- X{
- X register int i;
- X register int total_procs;
- X register int active_procs;
- X register KINFO **prefp;
- X KINFO *pp;
- X struct proc *pr;
- X struct eproc *epr;
- X
- X /* these are copied out of sel for speed */
- X int show_idle;
- X int show_system;
- X int show_uid;
- X int show_command;
- X
- X
- X nproc = kvm_getprocs(KINFO_PROC_ALL, 0);
- X if (nproc > onproc)
- X {
- X pref = (KINFO **) realloc(pref, sizeof(KINFO *)
- X * nproc);
- X pbase = (KINFO *) realloc(pbase, sizeof(KINFO)
- X * (nproc + 2));
- X onproc = nproc;
- X }
- X if (pref == NULL || pbase == NULL) {
- X (void) fprintf(stderr, "top: Out of memory.\n");
- X quit(23);
- X }
- X /* get a pointer to the states summary array */
- X si->procstates = process_states;
- X
- X /* set up flags which define what we are going to select */
- X show_idle = sel->idle;
- X show_system = sel->system;
- X show_uid = sel->uid != -1;
- X show_command = sel->command != NULL;
- X
- X /* count up process states and get pointers to interesting procs */
- X total_procs = 0;
- X active_procs = 0;
- X memset((char *)process_states, 0, sizeof(process_states));
- X prefp = pref;
- X for (pp = pbase, i = 0; pr = kvm_nextproc(); pp++, i++)
- X {
- X /*
- X * Place pointers to each valid proc structure in pref[].
- X * Process slots that are actually in use have a non-zero
- X * status field. Processes with SSYS set are system
- X * processes---these get ignored unless show_sysprocs is set.
- X */
- X epr = kvm_geteproc(pr);
- X pp->ki_p = *pr;
- X pp->ki_e = *epr;
- X if (PP(pp, p_stat) != 0 &&
- X (show_system || ((PP(pp, p_flag) & SSYS) == 0)))
- X {
- X total_procs++;
- X process_states[PP(pp, p_stat)]++;
- X if ((PP(pp, p_stat) != SZOMB) &&
- X (show_idle || (PP(pp, p_pctcpu) != 0) ||
- X (PP(pp, p_stat) == SRUN)) &&
- X (!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t)sel->uid))
- X {
- X *prefp++ = pp;
- X active_procs++;
- X }
- X }
- X }
- X
- X /* if requested, sort the "interesting" processes */
- X if (compare != NULL)
- X {
- X qsort((char *)pref, active_procs, sizeof(KINFO *), compare);
- X }
- X
- X /* remember active and total counts */
- X si->p_total = total_procs;
- X si->p_active = pref_len = active_procs;
- X
- X /* pass back a handle */
- X handle.next_proc = pref;
- X handle.remaining = active_procs;
- X#ifndef PATCHED_KVM
- X kvm_freeprocs();
- X#endif
- X return((caddr_t)&handle);
- X}
- X
- Xchar fmt[128]; /* static area where result is built */
- X
- Xchar *format_next_process(handle, get_userid)
- X
- Xcaddr_t handle;
- Xchar *(*get_userid)();
- X
- X{
- X register KINFO *pp;
- X register long cputime;
- X register double pct;
- X int where;
- X struct handle *hp;
- X
- X /* find and remember the next proc structure */
- X hp = (struct handle *)handle;
- X pp = *(hp->next_proc++);
- X hp->remaining--;
- X
- X
- X /* get the process's user struct and set cputime */
- X cputime = PP(pp, p_utime.tv_sec) + PP(pp, p_stime.tv_sec);
- X
- X /* calculate the base for cpu percentages */
- X pct = pctdouble(PP(pp, p_pctcpu));
- X
- X /* format this entry */
- X sprintf(fmt,
- X Proc_format,
- X PP(pp, p_pid),
- X (*get_userid)(EP(pp, e_pcred.p_ruid)),
- X PP(pp, p_pri) - PZERO,
- X PP(pp, p_nice) - NZERO,
- X pagetok(PROCSIZE(pp)),
- X#ifdef notyet
- X pagetok(VP(pp, vm_rssize)),
- X#else
- X pagetok(pp->ki_e.e_vm.vm_pmap.pm_stats.resident_count),
- X#endif
- X state_abbrev[PP(pp, p_stat)],
- X cputime / 60l,
- X cputime % 60l,
- X 100.0 * weighted_cpu(pct, pp),
- X 100.0 * pct,
- X printable(PP(pp, p_comm)));
- X
- X /* return the result */
- X return(fmt);
- X}
- X
- X
- X/*
- X * check_nlist(nlst) - checks the nlist to see if any symbols were not
- X * found. For every symbol that was not found, a one-line
- X * message is printed to stderr. The routine returns the
- X * number of symbols NOT found.
- X */
- X
- Xint check_nlist(nlst)
- X
- Xregister struct nlist *nlst;
- X
- X{
- X register int i;
- X
- X /* check to see if we got ALL the symbols we requested */
- X /* this will write one line to stderr for every symbol not found */
- X
- X i = 0;
- X while (nlst->n_name != NULL)
- X {
- X if (nlst->n_type == 0)
- X {
- X /* this one wasn't found */
- X fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_name);
- X i = 1;
- X }
- X nlst++;
- X }
- X
- X return(i);
- X}
- X
- X
- X/*
- X * getkval(offset, ptr, size, refstr) - get a value out of the kernel.
- X * "offset" is the byte offset into the kernel for the desired value,
- X * "ptr" points to a buffer into which the value is retrieved,
- X * "size" is the size of the buffer (and the object to retrieve),
- X * "refstr" is a reference string used when printing error meessages,
- X * if "refstr" starts with a '!', then a failure on read will not
- X * be fatal (this may seem like a silly way to do things, but I
- X * really didn't want the overhead of another argument).
- X *
- X */
- X
- Xgetkval(offset, ptr, size, refstr)
- X
- Xunsigned long offset;
- Xint *ptr;
- Xint size;
- Xchar *refstr;
- X
- X{
- X if (kvm_read((void *) offset, (void *) ptr, size) != size)
- X {
- X if (*refstr == '!')
- X {
- X return(0);
- X }
- X else
- X {
- X fprintf(stderr, "top: kvm_read for %s: %s\n",
- X refstr, strerror(errno));
- X quit(23);
- X }
- X }
- X return(1);
- X}
- X
- X/* comparison routine for qsort */
- X
- X/*
- X * proc_compare - comparison function for "qsort"
- X * Compares the resource consumption of two processes using five
- X * distinct keys. The keys (in descending order of importance) are:
- X * percent cpu, cpu ticks, state, resident set size, total virtual
- X * memory usage. The process states are ordered as follows (from least
- X * to most important): WAIT, zombie, sleep, stop, start, run. The
- X * array declaration below maps a process state index into a number
- X * that reflects this ordering.
- X */
- X
- Xstatic unsigned char sorted_state[] =
- X{
- X 0, /* not used */
- X 3, /* sleep */
- X 1, /* ABANDONED (WAIT) */
- X 6, /* run */
- X 5, /* start */
- X 2, /* zombie */
- X 4 /* stop */
- X};
- X
- Xproc_compare(pp1, pp2)
- X
- XKINFO **pp1;
- XKINFO **pp2;
- X
- X{
- X register KINFO *p1;
- X register KINFO *p2;
- X register int result;
- X register pctcpu lresult;
- X
- X /* remove one level of indirection */
- X p1 = *pp1;
- X p2 = *pp2;
- X
- X /* compare percent cpu (pctcpu) */
- X if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0)
- X {
- X /* use cpticks to break the tie */
- X if ((result = PP(p2, p_cpticks) - PP(p1, p_cpticks)) == 0)
- X {
- X /* use process state to break the tie */
- X if ((result = sorted_state[PP(p2, p_stat)] -
- X sorted_state[PP(p1, p_stat)]) == 0)
- X {
- X /* use priority to break the tie */
- X if ((result = PP(p2, p_pri) - PP(p1, p_pri)) == 0)
- X {
- X /* use resident set size (rssize) to break the tie */
- X if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
- X {
- X /* use total memory to break the tie */
- X result = PROCSIZE(p2) - PROCSIZE(p1);
- X }
- X }
- X }
- X }
- X }
- X else
- X {
- X result = lresult < 0 ? -1 : 1;
- X }
- X
- X return(result);
- X}
- X
- X/*
- X * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
- X * the process does not exist.
- X * It is EXTREMLY IMPORTANT that this function work correctly.
- X * If top runs setuid root (as in SVR4), then this function
- X * is the only thing that stands in the way of a serious
- X * security problem. It validates requests for the "kill"
- X * and "renice" commands.
- X */
- X
- Xint proc_owner(pid)
- X
- Xint pid;
- X
- X{
- X register int cnt;
- X register KINFO **prefp;
- X register KINFO *pp;
- X
- X prefp = pref;
- X cnt = pref_len;
- X while (--cnt >= 0)
- X {
- X pp = *prefp++;
- X if (PP(pp, p_pid) == (pid_t)pid)
- X {
- X return((int)EP(pp, e_pcred.p_ruid));
- X }
- X }
- X return(-1);
- X}
- END_OF_FILE
- if test 17022 -ne `wc -c <'machine/m_386bsd.c'`; then
- echo shar: \"'machine/m_386bsd.c'\" unpacked with wrong size!
- fi
- # end of 'machine/m_386bsd.c'
- fi
- if test -f 'machine/m_bsd386.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'machine/m_bsd386.c'\"
- else
- echo shar: Extracting \"'machine/m_bsd386.c'\" \(16548 characters\)
- sed "s/^X//" >'machine/m_bsd386.c' <<'END_OF_FILE'
- X/*
- X * top - a top users display for Unix
- X *
- X * SYNOPSIS: For a BSD/386 system
- X * Note memory statistic and process sizes could be wrong,
- X * but ps gets them wrong too...
- X *
- X * DESCRIPTION:
- X * This is the machine-dependent module for BSD/386
- X * Works for:
- X * hp300
- X * i386
- X *
- X * LIBS: -lkvm
- X *
- X * AUTHOR: Christos Zoulas <christos@ee.cornell.edu>
- X */
- X
- X#include <sys/types.h>
- X#include <sys/signal.h>
- X#include <sys/param.h>
- X
- X#include <stdio.h>
- X#include <nlist.h>
- X#include <math.h>
- X#ifdef __bsdi__
- X#include <sys/time.h>
- X#include <sys/proc.h>
- X#include <sys/vmmeter.h>
- X#endif
- X#include <kvm.h>
- X#include <sys/errno.h>
- X#include <sys/kinfo.h>
- X#include <sys/kinfo_proc.h>
- X#ifdef notyet
- X#define time __time
- X#define hz __hz
- X#include <sys/kernel.h>
- X#undef time
- X#undef hz
- X#endif
- X#include <sys/dir.h>
- X#ifdef __bsdi__
- X#include <sys/cpustats.h>
- X#include <sys/sysinfo.h>
- X#else
- X#include <sys/dkstat.h>
- X#endif
- X#include <sys/file.h>
- X#include <sys/time.h>
- X
- X
- X#define DOSWAP
- X
- X#include "top.h"
- X#include "machine.h"
- X
- X#ifdef __bsdi__
- X#define VMUNIX "/bsd"
- X#else
- X#define VMUNIX "/vmunix"
- X#endif
- X#define KMEM "/dev/kmem"
- X#define MEM "/dev/mem"
- X#ifdef DOSWAP
- X#define SWAP "/dev/drum"
- X#endif
- X
- X/* get_process_info passes back a handle. This is what it looks like: */
- X
- Xstruct handle
- X{
- X struct kinfo_proc **next_proc; /* points to next valid proc pointer */
- X int remaining; /* number of pointers remaining */
- X};
- X
- X/* declarations for load_avg */
- X#include "loadavg.h"
- X
- X#define PP(pp, field) ((pp)->kp_proc . field)
- X#define EP(pp, field) ((pp)->kp_eproc . field)
- X#define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
- X
- X/* define what weighted cpu is. */
- X#define weighted_cpu(pct, pp) (PP((pp), p_time) == 0 ? 0.0 : \
- X ((pct) / (1.0 - exp(PP((pp), p_time) * logcpu))))
- X
- X/* what we consider to be process size: */
- X#define PROCSIZE(pp) (VP((pp), vm_tsize) \
- X + VP((pp), vm_dsize) \
- X + VP((pp), vm_ssize))
- X
- X/* definitions for indices in the nlist array */
- X#define X_CCPU 0
- X#ifdef __bsdi__
- X#define X_TOTAL 1
- X#else
- X#define X_CP_TIME 1
- X#endif
- X#define X_HZ 2
- X#define X_AVENRUN 3
- X
- Xstatic struct nlist nlst[] = {
- X { "_ccpu" }, /* 0 */
- X#ifdef __bsdi__
- X { "_total" }, /* 1 */
- X#else
- X { "_cp_time" }, /* 1 */
- X#endif
- X { "_hz" }, /* 2 */
- X { "_averunnable" }, /* 3 */
- X { 0 }
- X};
- X
- X/*
- X * These definitions control the format of the per-process area
- X */
- X
- Xstatic char header[] =
- X " PID X PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
- X/* 0123456 -- field to fill in starts at header+6 */
- X#define UNAME_START 6
- X
- X#define Proc_format \
- X "%5d %-8.8s %3d %4d%6dK %4dK %-5s%4d:%02d %5.2f%% %5.2f%% %.14s"
- X
- X
- X/* process state names for the "STATE" column of the display */
- X/* the extra nulls in the string "run" are for adding a slash and
- X the processor number when needed */
- X
- Xstatic char *state_abbrev[] =
- X{
- X "", "sleep", "WAIT", "run\0\0\0", "start", "zomb", "stop"
- X};
- X
- X
- Xstatic kvm_t *kd;
- X
- X/* values that we stash away in _init and use in later routines */
- X
- Xstatic double logcpu;
- X
- X/* these are retrieved from the kernel in _init */
- X
- Xstatic long hz;
- Xstatic load_avg ccpu;
- Xstatic int ncpu = 0;
- X
- X/* these are offsets obtained via nlist and used in the get_ functions */
- X
- X#ifdef __bsdi__
- Xstatic unsigned long total_offset;
- X#else
- Xstatic unsigned long cp_time_offset;
- X#endif
- Xstatic unsigned long avenrun_offset;
- X
- X#ifndef __bsdi__
- X/* these are for calculating cpu state percentages */
- X
- Xstatic u_long cp_time[CPUSTATES];
- Xstatic u_long cp_old[CPUSTATES];
- Xstatic u_long cp_diff[CPUSTATES];
- X#endif
- X
- X/* these are for detailing the process states */
- X
- Xstatic int process_states[7];
- Xstatic char *procstatenames[] = {
- X "", " sleeping, ", " ABANDONED, ", " running, ", " starting, ",
- X " zombie, ", " stopped, ",
- X NULL
- X};
- X
- X/* these are for detailing the cpu states */
- X
- Xstatic int cpu_states[CPUSTATES];
- Xstatic char *cpustatenames[CPUSTATES+1] = {
- X "user", "nice", "system", "idle", NULL
- X};
- X
- X/* these are for detailing the memory statistics */
- X
- Xstatic int memory_stats[8];
- Xstatic char *memorynames[] = {
- X "Real: ", "K/", "K ", "Virt: ", "K/",
- X "K ", "Free: ", "K", NULL
- X};
- X
- X/* these are for keeping track of the proc array */
- X
- Xstatic int bytes;
- Xstatic int nproc;
- Xstatic int onproc = -1;
- Xstatic int pref_len;
- Xstatic struct kinfo_proc *pbase;
- Xstatic struct kinfo_proc **pref;
- X
- X/* these are for getting the memory statistics */
- X
- Xstatic int pageshift; /* log base 2 of the pagesize */
- X
- X/* define pagetok in terms of pageshift */
- X
- X#define pagetok(size) ((size) << pageshift)
- X
- X/* useful externals */
- Xlong percentages();
- X
- Xmachine_init(statics)
- X
- Xstruct statics *statics;
- X
- X{
- X register int i = 0;
- X register int pagesize;
- X
- X if ((kd = kvm_open(VMUNIX, MEM, SWAP, O_RDONLY, "kvm_open")) == NULL)
- X return -1;
- X
- X
- X /* get the list of symbols we want to access in the kernel */
- X (void) kvm_nlist(kd, nlst);
- X if (nlst[0].n_type == 0)
- X {
- X fprintf(stderr, "top: nlist failed\n");
- X return(-1);
- X }
- X
- X /* make sure they were all found */
- X if (i > 0 && check_nlist(nlst) > 0)
- X {
- X return(-1);
- X }
- X
- X /* get the symbol values out of kmem */
- X (void) getkval(nlst[X_HZ].n_value, (int *)(&hz), sizeof(hz),
- X nlst[X_HZ].n_name);
- X (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu),
- X nlst[X_CCPU].n_name);
- X
- X /* stash away certain offsets for later use */
- X#ifdef __bsdi__
- X total_offset = nlst[X_TOTAL].n_value;
- X#else
- X cp_time_offset = nlst[X_CP_TIME].n_value;
- X#endif
- X avenrun_offset = nlst[X_AVENRUN].n_value;
- X
- X /* this is used in calculating WCPU -- calculate it ahead of time */
- X logcpu = log(loaddouble(ccpu));
- X
- X pbase = NULL;
- X pref = NULL;
- X nproc = 0;
- X onproc = -1;
- X /* get the page size with "getpagesize" and calculate pageshift from it */
- X pagesize = getpagesize();
- X pageshift = 0;
- X while (pagesize > 1)
- X {
- X pageshift++;
- X pagesize >>= 1;
- X }
- X
- X /* we only need the amount of log(2)1024 for our conversion */
- X pageshift -= LOG1024;
- X
- X /* fill in the statics information */
- X statics->procstate_names = procstatenames;
- X statics->cpustate_names = cpustatenames;
- X statics->memory_names = memorynames;
- X
- X /* all done! */
- X return(0);
- X}
- X
- Xchar *format_header(uname_field)
- X
- Xregister char *uname_field;
- X
- X{
- X register char *ptr;
- X
- X ptr = header + UNAME_START;
- X while (*uname_field != '\0')
- X {
- X *ptr++ = *uname_field++;
- X }
- X
- X return(header);
- X}
- X
- Xget_system_info(si)
- X
- Xstruct system_info *si;
- X
- X{
- X register u_long total;
- X load_avg avenrun[3];
- X#ifdef __bsdi__
- X struct cpustats cpu;
- X struct sysinfo sys;
- X int size;
- X#else
- X load_avg *avenrunp = avenrun;
- X#endif
- X
- X /* get the various high-level data structures */
- X#ifdef __bsdi__
- X size = sizeof(struct cpustats);
- X if (getkerninfo(KINFO_CPU, &cpu, &size, 0) < 0) {
- X perror("getkerninfo#1");
- X abort();
- X }
- X#ifdef notyet
- X size = sizeof(struct sysinfo);
- X if (getkerninfo(KINFO_SYSINFO, &sys, &size, 0) < 0) {
- X perror("getkerninfo#2");
- X abort();
- X }
- X#endif /*notyet*/
- X#else
- X (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
- X "_cp_time");
- X (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
- X "_avenrun");
- X#endif
- X
- X /* convert load averages to doubles */
- X {
- X register int i;
- X register double *infoloadp = si->load_avg;
- X
- X for (i = 0; i < CPUSTATES; i++)
- X {
- X#ifdef __bsdi__
- X *infoloadp++ = ((double) cpu.cp_averunnable[i]) / FSCALE;
- X#else
- X *infoloadp++ = loaddouble(*avenrunp++);
- X#endif
- X }
- X }
- X
- X /* convert cp_time counts to percentages */
- X#ifdef __bsdi__
- X {
- X register int i;
- X register double total, pct;
- X
- X total = 0.0;
- X for (i = 0; i < CPUSTATES; i++)
- X total += (double) cpu.cp_time[i];
- X if (total == 0)
- X pct = 0;
- X else
- X pct = 100 / total;
- X for (i = 0; i < CPUSTATES; i++)
- X cpu_states[i] = 10.0 * ((double)cpu.cp_time[i]) * pct;
- X }
- X#else
- X total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
- X#endif
- X
- X /* sum memory statistics */
- X {
- X struct vmtotal total;
- X int size;
- X
- X#ifdef __bsdi__
- X (void) getkval(total_offset, (int*)&total, sizeof(total),
- X "_total");
- X#else
- X /* get total -- systemwide main memory usage structure */
- X size = sizeof(struct vmtotal);
- X getkerninfo(KINFO_METER, &total, &size, 0);
- X#endif
- X /* convert memory stats to Kbytes */
- X memory_stats[0] = -1;
- X memory_stats[1] = pagetok(total.t_arm);
- X memory_stats[2] = pagetok(total.t_rm);
- X memory_stats[3] = -1;
- X memory_stats[4] = pagetok(total.t_avm);
- X memory_stats[5] = pagetok(total.t_vm);
- X memory_stats[6] = -1;
- X memory_stats[7] = pagetok(total.t_free);
- X }
- X
- X /* set arrays and strings */
- X si->cpustates = cpu_states;
- X si->memory = memory_stats;
- X si->last_pid = -1;
- X}
- X
- Xstatic struct handle handle;
- X
- Xcaddr_t get_process_info(si, sel, compare)
- X
- Xstruct system_info *si;
- Xstruct process_select *sel;
- Xint (*compare)();
- X
- X{
- X register int i;
- X register int total_procs;
- X register int active_procs;
- X register struct kinfo_proc **prefp;
- X register struct kinfo_proc *pp;
- X
- X /* these are copied out of sel for speed */
- X int show_idle;
- X int show_system;
- X int show_uid;
- X int show_command;
- X
- X
- X pbase = kvm_getprocs(kd, KINFO_PROC_ALL, 0, &nproc);
- X if (nproc > onproc)
- X pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
- X * (onproc = nproc));
- X if (pref == NULL || pbase == NULL) {
- X (void) fprintf(stderr, "top: Out of memory.\n");
- X quit(23);
- X }
- X /* get a pointer to the states summary array */
- X si->procstates = process_states;
- X
- X /* set up flags which define what we are going to select */
- X show_idle = sel->idle;
- X show_system = sel->system;
- X show_uid = sel->uid != -1;
- X show_command = sel->command != NULL;
- X
- X /* count up process states and get pointers to interesting procs */
- X total_procs = 0;
- X active_procs = 0;
- X memset((char *)process_states, 0, sizeof(process_states));
- X prefp = pref;
- X for (pp = pbase, i = 0; i < nproc; pp++, i++)
- X {
- X /*
- X * Place pointers to each valid proc structure in pref[].
- X * Process slots that are actually in use have a non-zero
- X * status field. Processes with SSYS set are system
- X * processes---these get ignored unless show_sysprocs is set.
- X */
- X if (PP(pp, p_stat) != 0 &&
- X (show_system || ((PP(pp, p_flag) & SSYS) == 0)))
- X {
- X int p_stat = PP(pp, p_stat);
- X
- X total_procs++;
- X if (p_stat < 1 || p_stat > 6)
- X abort();
- X process_states[p_stat]++;
- X if ((PP(pp, p_stat) != SZOMB) &&
- X (show_idle || (PP(pp, p_pctcpu) != 0) ||
- X (PP(pp, p_stat) == SRUN)) &&
- X (!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t)sel->uid))
- X {
- X *prefp++ = pp;
- X active_procs++;
- X }
- X }
- X }
- X
- X /* if requested, sort the "interesting" processes */
- X if (compare != NULL)
- X {
- X qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
- X }
- X
- X /* remember active and total counts */
- X si->p_total = total_procs;
- X si->p_active = pref_len = active_procs;
- X
- X /* pass back a handle */
- X handle.next_proc = pref;
- X handle.remaining = active_procs;
- X return((caddr_t)&handle);
- X}
- X
- Xchar fmt[128]; /* static area where result is built */
- X
- Xchar *format_next_process(handle, get_userid)
- X
- Xcaddr_t handle;
- Xchar *(*get_userid)();
- X
- X{
- X register struct kinfo_proc *pp;
- X register long cputime;
- X register double pct;
- X int where;
- X struct handle *hp;
- X
- X /* find and remember the next proc structure */
- X hp = (struct handle *)handle;
- X pp = *(hp->next_proc++);
- X hp->remaining--;
- X
- X
- X /* get the process's user struct and set cputime */
- X if ((PP(pp, p_flag) & SLOAD) == 0) {
- X /*
- X * Print swapped processes as <pname>
- X */
- X char *comm = PP(pp, p_comm);
- X#define COMSIZ sizeof(PP(pp, p_comm))
- X char buf[COMSIZ];
- X (void) strncpy(buf, comm, COMSIZ);
- X comm[0] = '<';
- X (void) strncpy(&comm[1], buf, COMSIZ - 2);
- X comm[COMSIZ - 2] = '\0';
- X (void) strncat(comm, ">", COMSIZ - 1);
- X comm[COMSIZ - 1] = '\0';
- X }
- X
- X cputime = PP(pp, p_utime.tv_sec) + PP(pp, p_stime.tv_sec);
- X
- X /* calculate the base for cpu percentages */
- X pct = pctdouble(PP(pp, p_pctcpu));
- X
- X /* format this entry */
- X sprintf(fmt,
- X Proc_format,
- X PP(pp, p_pid),
- X (*get_userid)(EP(pp, e_pcred.p_ruid)),
- X PP(pp, p_pri) - PZERO,
- X PP(pp, p_nice) - NZERO,
- X pagetok(PROCSIZE(pp)),
- X pagetok(VP(pp, vm_rssize)),
- X state_abbrev[PP(pp, p_stat)],
- X cputime / 60l,
- X cputime % 60l,
- X 100.0 * weighted_cpu(pct, pp),
- X 100.0 * pct,
- X printable(PP(pp, p_comm)));
- X
- X /* return the result */
- X return(fmt);
- X}
- X
- X
- X/*
- X * check_nlist(nlst) - checks the nlist to see if any symbols were not
- X * found. For every symbol that was not found, a one-line
- X * message is printed to stderr. The routine returns the
- X * number of symbols NOT found.
- X */
- X
- Xint check_nlist(nlst)
- X
- Xregister struct nlist *nlst;
- X
- X{
- X register int i;
- X
- X /* check to see if we got ALL the symbols we requested */
- X /* this will write one line to stderr for every symbol not found */
- X
- X i = 0;
- X while (nlst->n_name != NULL)
- X {
- X if (nlst->n_type == 0)
- X {
- X /* this one wasn't found */
- X fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_name);
- X i = 1;
- X }
- X nlst++;
- X }
- X
- X return(i);
- X}
- X
- X
- X/*
- X * getkval(offset, ptr, size, refstr) - get a value out of the kernel.
- X * "offset" is the byte offset into the kernel for the desired value,
- X * "ptr" points to a buffer into which the value is retrieved,
- X * "size" is the size of the buffer (and the object to retrieve),
- X * "refstr" is a reference string used when printing error meessages,
- X * if "refstr" starts with a '!', then a failure on read will not
- X * be fatal (this may seem like a silly way to do things, but I
- X * really didn't want the overhead of another argument).
- X *
- X */
- X
- Xgetkval(offset, ptr, size, refstr)
- X
- Xunsigned long offset;
- Xint *ptr;
- Xint size;
- Xchar *refstr;
- X
- X{
- X if (kvm_read(kd, offset, (char *) ptr, size) != size)
- X {
- X if (*refstr == '!')
- X {
- X return(0);
- X }
- X else
- X {
- X fprintf(stderr, "top: kvm_read for %s: %s\n",
- X refstr, strerror(errno));
- X quit(23);
- X }
- X }
- X return(1);
- X}
- X
- X/* comparison routine for qsort */
- X
- X/*
- X * proc_compare - comparison function for "qsort"
- X * Compares the resource consumption of two processes using five
- X * distinct keys. The keys (in descending order of importance) are:
- X * percent cpu, cpu ticks, state, resident set size, total virtual
- X * memory usage. The process states are ordered as follows (from least
- X * to most important): WAIT, zombie, sleep, stop, start, run. The
- X * array declaration below maps a process state index into a number
- X * that reflects this ordering.
- X */
- X
- Xstatic unsigned char sorted_state[] =
- X{
- X 0, /* not used */
- X 3, /* sleep */
- X 1, /* ABANDONED (WAIT) */
- X 6, /* run */
- X 5, /* start */
- X 2, /* zombie */
- X 4 /* stop */
- X};
- X
- Xproc_compare(pp1, pp2)
- X
- Xstruct kinfo_proc **pp1;
- Xstruct kinfo_proc **pp2;
- X
- X{
- X register struct kinfo_proc *p1;
- X register struct kinfo_proc *p2;
- X register int result;
- X register pctcpu lresult;
- X
- X /* remove one level of indirection */
- X p1 = *pp1;
- X p2 = *pp2;
- X
- X /* compare percent cpu (pctcpu) */
- X if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0)
- X {
- X /* use cpticks to break the tie */
- X if ((result = PP(p2, p_cpticks) - PP(p1, p_cpticks)) == 0)
- X {
- X /* use process state to break the tie */
- X if ((result = sorted_state[PP(p2, p_stat)] -
- X sorted_state[PP(p1, p_stat)]) == 0)
- X {
- X /* use priority to break the tie */
- X if ((result = PP(p2, p_pri) - PP(p1, p_pri)) == 0)
- X {
- X /* use resident set size (rssize) to break the tie */
- X if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
- X {
- X /* use total memory to break the tie */
- X result = PROCSIZE(p2) - PROCSIZE(p1);
- X }
- X }
- X }
- X }
- X }
- X else
- X {
- X result = lresult < 0 ? -1 : 1;
- X }
- X
- X return(result);
- X}
- X
- X/*
- X * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
- X * the process does not exist.
- X * It is EXTREMLY IMPORTANT that this function work correctly.
- X * If top runs setuid root (as in SVR4), then this function
- X * is the only thing that stands in the way of a serious
- X * security problem. It validates requests for the "kill"
- X * and "renice" commands.
- X */
- X
- Xint proc_owner(pid)
- X
- Xint pid;
- X
- X{
- X register int cnt;
- X register struct kinfo_proc **prefp;
- X register struct kinfo_proc *pp;
- X
- X prefp = pref;
- X cnt = pref_len;
- X while (--cnt >= 0)
- X {
- X pp = *prefp++;
- X if (PP(pp, p_pid) == (pid_t)pid)
- X {
- X return((int)EP(pp, e_pcred.p_ruid));
- X }
- X }
- X return(-1);
- X}
- END_OF_FILE
- if test 16548 -ne `wc -c <'machine/m_bsd386.c'`; then
- echo shar: \"'machine/m_bsd386.c'\" unpacked with wrong size!
- fi
- # end of 'machine/m_bsd386.c'
- fi
- if test -f 'machine/m_dynix.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'machine/m_dynix.c'\"
- else
- echo shar: Extracting \"'machine/m_dynix.c'\" \(17507 characters\)
- sed "s/^X//" >'machine/m_dynix.c' <<'END_OF_FILE'
- X/*
- X * top - a top users display for Unix
- X *
- X * SYNOPSIS: any Sequent Running Dynix 3.0.x
- X *
- X * DESCRIPTION:
- X * This is the machine-dependent module for Sequent Dynix 3
- X * This makes top work on the following systems:
- X * Sequent Semmetry, Dynix 3.0.12
- X * Sequent Balance, Dynix 3.0.4
- X *
- X * AUTHOR: Daniel Trinkle <trinkle@cs.purdue.edu>
- X */
- X
- X#include <sys/types.h>
- X#include <sys/signal.h>
- X#include <sys/param.h>
- X
- X#include <stdio.h>
- X#include <nlist.h>
- X#include <math.h>
- X#include <sys/dir.h>
- X#include <sys/user.h>
- X#include <sys/proc.h>
- X#include <sys/dk.h>
- X#include <sys/vm.h>
- X#include <machine/pte.h>
- X#include <machine/plocal.h>
- X#include <machine/engine.h>
- X#include <sys/file.h>
- X
- X#include "top.h"
- X#include "machine.h"
- X
- X#ifndef uid_t
- X/* some early versions of DYNIX don't have uid_t */
- X#define uid_t int
- X#endif
- X
- Xstruct engine *engine;
- Xstruct engine *pengine;
- Xstruct plocal **pplocal;
- Xint Nengine;
- X
- X/* get_process_info passes back a handle. This is what it looks like: */
- X
- Xstruct handle
- X{
- X struct proc **next_proc; /* points to next valid proc pointer */
- X int remaining; /* number of pointers remaining */
- X};
- X
- X/* declarations for load_avg */
- Xtypedef long load_avg;
- Xtypedef long pctcpu;
- X#define loaddouble(la) ((double)(la) / FSCALE)
- X#define intload(i) ((int)((i) * FSCALE))
- X#define pctdouble(p) ((double)(p) / FSCALE)
- X
- X/* define what weighted cpu is. */
- X#define weighted_cpu(pct, pp) ((pp)->p_time == 0 ? 0.0 : \
- X ((pct) / (1.0 - exp((pp)->p_time * logcpu))))
- X
- X/* what we consider to be process size: */
- X#define PROCSIZE(pp) ((pp)->p_dsize + (pp)->p_ssize)
- X
- X/* definitions for indices in the nlist array */
- X#define X_AVENRUN 0
- X#define X_CCPU 1
- X#define X_MPID 2
- X#define X_NPROC 3
- X#define X_PROC 4
- X#define X_TOTAL 5
- X#define X_ENGINE 6
- X#define X_NENGINE 7
- X
- Xstatic struct nlist nlst[] = {
- X { "_avenrun" }, /* 0 */
- X { "_ccpu" }, /* 1 */
- X { "_mpid" }, /* 2 */
- X { "_nproc" }, /* 3 */
- X { "_proc" }, /* 4 */
- X { "_total" }, /* 5 */
- X { "_engine" }, /* 6 */
- X { "_Nengine" }, /* 7 */
- X { 0 }
- X};
- X
- X/*
- X * These definitions control the format of the per-process area
- X */
- X
- Xstatic char header[] =
- X " PID X PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
- X/* 0123456 -- field to fill in starts at header+6 */
- X#define UNAME_START 6
- X
- X#define Proc_format \
- X "%5d %-8.8s %3d %4d%6dK %4dK %-5s%4d:%02d %5.2f%% %5.2f%% %.14s"
- X
- X
- X/* process state names for the "STATE" column of the display */
- X/* the extra nulls in the string "run" are for adding a slash and
- X the processor number when needed */
- X
- Xchar *state_abbrev[] =
- X{
- X "", "sleep", "WAIT", "run", "start", "zomb", "stop", "RUN"
- X};
- X
- X/* values that we stash away in _init and use in later routines */
- X
- Xstatic double logcpu;
- X
- X#define VMUNIX "/dynix"
- X#define KMEM "/dev/kmem"
- X#define MEM "/dev/mem"
- X
- Xstatic int kmem = -1;
- Xstatic int mem = -1;
- X
- Xstruct vmtotal total;
- X
- X/* these are retrieved from the kernel in _init */
- X
- Xstatic unsigned long proc;
- Xstatic int nproc;
- Xstatic load_avg ccpu;
- X
- X/* these are offsets obtained via nlist and used in the get_ functions */
- X
- Xstatic unsigned long mpid_offset;
- Xstatic unsigned long avenrun_offset;
- Xstatic unsigned long total_offset;
- X
- X/* these are for calculating cpu state percentages */
- X
- Xstatic long cp_time[CPUSTATES];
- Xstatic long cp_old[CPUSTATES];
- Xstatic long cp_diff[CPUSTATES];
- X
- X/* these are for detailing the process states */
- X
- Xint process_states[8];
- Xchar *procstatenames[] = {
- X "", " sleeping, ", " ABANDONED, ", " runable, ", " starting, ",
- X " zombie, ", " stopped, ", " running, ",
- X NULL
- X};
- X
- X/* these are for detailing the cpu states */
- X
- Xint cpu_states[CPUSTATES];
- Xchar *cpustatenames[] = {
- X "user", "nice", "system", "idle",
- X NULL
- X};
- X
- X/* these are for detailing the memory statistics */
- X
- Xint memory_stats[5];
- Xchar *memorynames[] = {
- X "K (", "K) real, ", "K (", "K) virtual, ", "K free", NULL
- X};
- X
- X/* these are for keeping track of the proc array */
- X
- Xstatic int bytes;
- Xstatic int pref_len;
- Xstatic struct proc *pbase;
- Xstatic struct proc **pref;
- X
- X#define pagetok(size) ((size) << (PGSHIFT - LOG1024))
- X
- X/* useful externals */
- Xextern int errno;
- Xextern char *sys_errlist[];
- X
- Xlong lseek();
- Xlong percentages();
- X
- Xmachine_init(statics)
- X
- Xstruct statics *statics;
- X
- X{
- X register int i;
- X
- X /* open kernel memory */
- X if ((kmem = open(KMEM, 0)) < 0)
- X {
- X perror(KMEM);
- X exit(20);
- X }
- X if ((mem = open(MEM, 0)) < 0)
- X {
- X perror(MEM);
- X exit(21);
- X }
- X
- X /* get the list of symbols we want to access in the kernel */
- X if ((i = nlist(VMUNIX, nlst)) < 0)
- X {
- X fprintf(stderr, "top: nlist failed\n");
- X return(-1);
- X }
- X
- X /* make sure they were all found */
- X if (i > 0 && check_nlist(nlst) > 0)
- X {
- X return(-1);
- X }
- X
- X /* get the symbol values out of kmem */
- X (void) getkval(nlst[X_PROC].n_value, (int *)(&proc), sizeof(proc),
- X nlst[X_PROC].n_name);
- X (void) getkval(nlst[X_NPROC].n_value, &nproc, sizeof(nproc),
- X nlst[X_NPROC].n_name);
- X (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu),
- X nlst[X_CCPU].n_name);
- X (void) getkval(nlst[X_NENGINE].n_value, &Nengine, sizeof(int),
- X nlst[X_NENGINE].n_name);
- X (void) getkval(nlst[X_ENGINE].n_value, &pengine, sizeof(struct engine *),
- X nlst[X_ENGINE].n_name);
- X
- X engine = (struct engine *)calloc(Nengine, sizeof(struct engine));
- X if (engine == NULL)
- X {
- X fprintf(stderr, "Cannot allocate memory for engine structure\n");
- X exit(2);
- X }
- X (void) getkval(pengine, &engine[0], Nengine * sizeof(struct engine),
- X "engine array");
- X pplocal = (struct plocal **)calloc(Nengine, sizeof(struct plocal *));
- X if (pplocal == NULL)
- X {
- X fprintf(stderr, "Cannot allocate memory for plocal structures\n");
- X exit(2);
- X }
- X for (i = 0; i < Nengine; i++) {
- X pplocal[i] = (struct plocal *)&engine[i].e_local->pp_local[0][0];
- X }
- X
- X /* stash away certain offsets for later use */
- X mpid_offset = nlst[X_MPID].n_value;
- X avenrun_offset = nlst[X_AVENRUN].n_value;
- X total_offset = nlst[X_TOTAL].n_value;
- X
- X /* this is used in calculating WCPU -- calculate it ahead of time */
- X logcpu = log(loaddouble(ccpu));
- X
- X /* allocate space for proc structure array and array of pointers */
- X bytes = nproc * sizeof(struct proc);
- X pbase = (struct proc *)malloc(bytes);
- X pref = (struct proc **)malloc(nproc * sizeof(struct proc *));
- X
- X /* Just in case ... */
- X if (pbase == (struct proc *)NULL || pref == (struct proc **)NULL)
- X {
- X fprintf(stderr, "top: can't allocate sufficient memory\n");
- X return(-1);
- X }
- X
- X /* fill in the statics information */
- X statics->procstate_names = procstatenames;
- X statics->cpustate_names = cpustatenames;
- X statics->memory_names = memorynames;
- X
- X /* all done! */
- X return(0);
- X}
- X
- Xchar *format_header(uname_field)
- X
- Xregister char *uname_field;
- X
- X{
- X register char *ptr;
- X
- X ptr = header + UNAME_START;
- X while (*uname_field != '\0')
- X {
- X *ptr++ = *uname_field++;
- X }
- X
- X return(header);
- X}
- X
- Xget_system_info(si)
- X
- Xstruct system_info *si;
- X
- X{
- X load_avg avenrun[3];
- X struct plocal plocal;
- X register int i, j;
- X
- X /* get the cp_time array */
- X for (j = 0; j < CPUSTATES; j++)
- X cp_time[j] = 0L;
- X for (i = 0; i < Nengine; i++) {
- X (void) getkval(pplocal[i], &plocal, sizeof(struct plocal), "plocal array");
- X for (j = 0; j < CPUSTATES; j++)
- X cp_time[j] += (long)plocal.cnt.v_time[j];
- X }
- X
- X /* get load average array */
- X (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
- X "_avenrun");
- X
- X /* get mpid -- process id of last process */
- X (void) getkval(mpid_offset, &(si->last_pid), sizeof(si->last_pid),
- X "_mpid");
- X
- X /* convert load averages to doubles */
- X {
- X register int i;
- X register double *infoloadp;
- X register load_avg *sysloadp;
- X
- X infoloadp = si->load_avg;
- X sysloadp = avenrun;
- X for (i = 0; i < 3; i++)
- X {
- X *infoloadp++ = loaddouble(*sysloadp++);
- X }
- X }
- X
- X /* convert cp_time counts to percentages */
- X (void) percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
- X
- X /* get total -- systemwide main memory usage structure */
- X (void) getkval(total_offset, (int *)(&total), sizeof(total),
- X "_total");
- X /* convert memory stats to Kbytes */
- X memory_stats[0] = pagetok(total.t_rm);
- X memory_stats[1] = pagetok(total.t_arm);
- X memory_stats[2] = pagetok(total.t_vm);
- X memory_stats[3] = pagetok(total.t_avm);
- X memory_stats[4] = pagetok(total.t_free);
- X
- X /* set arrays and strings */
- X si->cpustates = cpu_states;
- X si->memory = memory_stats;
- X}
- X
- Xstatic struct handle handle;
- X
- Xcaddr_t get_process_info(si, sel, compare)
- X
- Xstruct system_info *si;
- Xstruct process_select *sel;
- Xint (*compare)();
- X
- X{
- X register int i;
- X register int total_procs;
- X register int active_procs;
- X register struct proc **prefp;
- X register struct proc *pp;
- X
- X /* these are copied out of sel for speed */
- X int show_idle;
- X int show_system;
- X int show_uid;
- X
- X /* read all the proc structures in one fell swoop */
- X (void) getkval(proc, (int *)pbase, bytes, "proc array");
- X
- X /* get a pointer to the states summary array */
- X si->procstates = process_states;
- X
- X /* set up flags which define what we are going to select */
- X show_idle = sel->idle;
- X show_system = sel->system;
- X show_uid = sel->uid != -1;
- X
- X /* count up process states and get pointers to interesting procs */
- X total_procs = 0;
- X active_procs = 0;
- X bzero((char *)process_states, sizeof(process_states));
- X prefp = pref;
- X for (pp = pbase, i = 0; i < nproc; pp++, i++)
- X {
- X /*
- X * Place pointers to each valid proc structure in pref[].
- X * Process slots that are actually in use have a non-zero
- X * status field. Processes with SSYS set are system
- X * processes---these get ignored unless show_sysprocs is set.
- X */
- X if (pp->p_stat != 0 &&
- X (show_system || ((pp->p_flag & SSYS) == 0)))
- X {
- X total_procs++;
- X process_states[pp->p_stat]++;
- X if ((pp->p_stat != SZOMB) &&
- X (show_idle || (pp->p_pctcpu != 0) || (pp->p_stat == SRUN)) &&
- X (!show_uid || pp->p_uid == (uid_t)sel->uid))
- X {
- X *prefp++ = pp;
- X active_procs++;
- X }
- X }
- X }
- X
- X /* if requested, sort the "interesting" processes */
- X if (compare != NULL)
- X {
- X qsort((char *)pref, active_procs, sizeof(struct proc *), compare);
- X }
- X
- X /* remember active and total counts */
- X si->p_total = total_procs;
- X si->p_active = pref_len = active_procs;
- X
- X /* pass back a handle */
- X handle.next_proc = pref;
- X handle.remaining = active_procs;
- X return((caddr_t)&handle);
- X}
- X
- Xchar fmt[128]; /* static area where result is built */
- X
- Xchar *format_next_process(handle, get_userid)
- X
- Xcaddr_t handle;
- Xchar *(*get_userid)();
- X
- X{
- X register struct proc *pp;
- X register long cputime;
- X register double pct;
- X struct user u;
- X struct handle *hp;
- X
- X /* find and remember the next proc structure */
- X hp = (struct handle *)handle;
- X pp = *(hp->next_proc++);
- X hp->remaining--;
- X
- X
- X /* get the process's user struct and set cputime */
- X if (getu(pp, &u) == -1)
- X {
- X (void) strcpy(u.u_comm, "<swapped>");
- X cputime = 0;
- X }
- X else
- X {
- X /* set u_comm for system processes */
- X if (u.u_comm[0] == '\0')
- X {
- X if (pp->p_pid == 0)
- X {
- X (void) strcpy(u.u_comm, "Swapper");
- X }
- X else if (pp->p_pid == 2)
- X {
- X (void) strcpy(u.u_comm, "Pager");
- X }
- X }
- X
- X cputime = u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec;
- X }
- X
- X /* calculate the base for cpu percentages */
- X pct = pctdouble(pp->p_pctcpu);
- X
- X /* format this entry */
- X sprintf(fmt,
- X Proc_format,
- X pp->p_pid,
- X (*get_userid)(pp->p_uid),
- X pp->p_pri - PZERO,
- X pp->p_nice - NZERO,
- X pagetok(PROCSIZE(pp)),
- X pagetok(pp->p_rssize),
- X state_abbrev[pp->p_stat],
- X cputime / 60l,
- X cputime % 60l,
- X 100.0 * weighted_cpu(pct, pp),
- X 100.0 * pct,
- X printable(u.u_comm));
- X
- X /* return the result */
- X return(fmt);
- X}
- X
- X/*
- X * getu(p, u) - get the user structure for the process whose proc structure
- X * is pointed to by p. The user structure is put in the buffer pointed
- X * to by u. Return 0 if successful, -1 on failure (such as the process
- X * being swapped out).
- X */
- X
- Xgetu(p, u)
- X
- Xregister struct proc *p;
- Xstruct user *u;
- X
- X{
- X struct pte uptes[UPAGES];
- X register caddr_t upage;
- X register struct pte *pte;
- X register nbytes, n;
- X
- X /*
- X * Check if the process is currently loaded or swapped out. The way we
- X * get the u area is totally different for the two cases. For this
- X * application, we just don't bother if the process is swapped out.
- X */
- X if ((p->p_flag & SLOAD) == 0)
- X {
- X return(-1);
- X }
- X
- X /*
- X * Process is currently in memory, we hope!
- X */
- X#ifdef ns32000
- X if (!getkval(p->p_upte, uptes, sizeof(uptes), "!p->p_upte"))
- X#else
- X if (!getkval(p->p_pttop, uptes, sizeof(uptes), "!p->p_upte"))
- X#endif
- X {
- X /* we can't seem to get to it, so pretend it's swapped out */
- X return(-1);
- X }
- X upage = (caddr_t)u;
- X pte = uptes;
- X for (nbytes = sizeof(struct user); nbytes > 0; nbytes -= NBPG)
- X {
- X (void) lseek(mem, (long)(pte++->pg_pfnum * NBPG), 0);
- X n = MIN(nbytes, NBPG);
- X if (read(mem, upage, n) != n)
- X {
- X /* we can't seem to get to it, so pretend it's swapped out */
- X return(-1);
- X }
- X upage += n;
- X }
- X return(0);
- X}
- X
- X/*
- X * check_nlist(nlst) - checks the nlist to see if any symbols were not
- X * found. For every symbol that was not found, a one-line
- X * message is printed to stderr. The routine returns the
- X * number of symbols NOT found.
- X */
- X
- Xint check_nlist(nlst)
- X
- Xregister struct nlist *nlst;
- X
- X{
- X register int i;
- X
- X /* check to see if we got ALL the symbols we requested */
- X /* this will write one line to stderr for every symbol not found */
- X
- X i = 0;
- X while (nlst->n_name != NULL)
- X {
- X if (nlst->n_type == 0)
- X {
- X /* this one wasn't found */
- X fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_name);
- X i = 1;
- X }
- X nlst++;
- X }
- X
- X return(i);
- X}
- X
- X
- X/*
- X * getkval(offset, ptr, size, refstr) - get a value out of the kernel.
- X * "offset" is the byte offset into the kernel for the desired value,
- X * "ptr" points to a buffer into which the value is retrieved,
- X * "size" is the size of the buffer (and the object to retrieve),
- X * "refstr" is a reference string used when printing error meessages,
- X * if "refstr" starts with a '!', then a failure on read will not
- X * be fatal (this may seem like a silly way to do things, but I
- X * really didn't want the overhead of another argument).
- X *
- X */
- X
- Xgetkval(offset, ptr, size, refstr)
- X
- Xunsigned long offset;
- Xint *ptr;
- Xint size;
- Xchar *refstr;
- X
- X{
- X if (lseek(kmem, (long)offset, 0) == -1)
- X {
- X if (*refstr == '!')
- X {
- X refstr++;
- X }
- X fprintf(stderr, "%s: lseek to %s: %s\n",
- X KMEM, refstr, sys_errlist[errno]);
- X quit(22);
- X }
- X if (read(kmem, (char *)ptr, size) == -1)
- X {
- X if (*refstr == '!')
- X {
- X /* we lost the race with the kernel, process isn't in memory */
- X return(0);
- X }
- X else
- X {
- X fprintf(stderr, "%s: reading %s: %s\n",
- X KMEM, refstr, sys_errlist[errno]);
- X quit(23);
- X }
- X }
- X return(1);
- X}
- X
- X/* comparison routine for qsort */
- X
- X/*
- X * proc_compare - comparison function for "qsort"
- X * Compares the resource consumption of two processes using five
- X * distinct keys. The keys (in descending order of importance) are:
- X * percent cpu, cpu ticks, state, resident set size, total virtual
- X * memory usage. The process states are ordered as follows (from least
- X * to most important): WAIT, zombie, sleep, stop, start, run. The
- X * array declaration below maps a process state index into a number
- X * that reflects this ordering.
- X */
- X
- Xstatic unsigned char sorted_state[] =
- X{
- X 0, /* not used */
- X 3, /* sleep */
- X 1, /* ABANDONED (WAIT) */
- X 6, /* run */
- X 5, /* start */
- X 2, /* zombie */
- X 4, /* stop */
- X 7 /* RUN */
- X};
- X
- Xproc_compare(pp1, pp2)
- X
- Xstruct proc **pp1;
- Xstruct proc **pp2;
- X
- X{
- X register struct proc *p1;
- X register struct proc *p2;
- X register int result;
- X register pctcpu lresult;
- X
- X /* remove one level of indirection */
- X p1 = *pp1;
- X p2 = *pp2;
- X
- X /* compare percent cpu (pctcpu) */
- X if ((lresult = p2->p_pctcpu - p1->p_pctcpu) == 0)
- X {
- X /* use cpticks to break the tie */
- X if ((result = p2->p_cpticks - p1->p_cpticks) == 0)
- X {
- X /* use process state to break the tie */
- X if ((result = sorted_state[p2->p_stat] -
- X sorted_state[p1->p_stat]) == 0)
- X {
- X /* use priority to break the tie */
- X if ((result = p2->p_pri - p1->p_pri) == 0)
- X {
- X /* use resident set size (rssize) to break the tie */
- X if ((result = p2->p_rssize - p1->p_rssize) == 0)
- X {
- X /* use total memory to break the tie */
- X result = PROCSIZE(p2) - PROCSIZE(p1);
- X }
- X }
- X }
- X }
- X }
- X else
- X {
- X result = lresult < 0 ? -1 : 1;
- X }
- X
- X return(result);
- X}
- X
- X
- X/*
- X * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
- X * the process does not exist.
- X * It is EXTREMLY IMPORTANT that this function work correctly.
- X * If top runs setuid root (as in SVR4), then this function
- X * is the only thing that stands in the way of a serious
- X * security problem. It validates requests for the "kill"
- X * and "renice" commands.
- X */
- X
- Xint proc_owner(pid)
- X
- Xint pid;
- X
- X{
- X register int cnt;
- X register struct proc **prefp;
- X register struct proc *pp;
- X
- X prefp = pref;
- X cnt = pref_len;
- X while (--cnt >= 0)
- X {
- X if ((pp = *prefp++)->p_pid == (pid_t)pid)
- X {
- X return((int)pp->p_uid);
- X }
- X }
- X return(-1);
- X}
- END_OF_FILE
- if test 17507 -ne `wc -c <'machine/m_dynix.c'`; then
- echo shar: \"'machine/m_dynix.c'\" unpacked with wrong size!
- fi
- # end of 'machine/m_dynix.c'
- fi
- echo shar: End of archive 6 \(of 13\).
- cp /dev/null ark6isdone
- MISSING=""
- for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked all 13 archives.
- echo "Now read README and INSTALL, then run Configure"
- rm -f ark[1-9]isdone ark[1-9][0-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-