home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-08-08 | 43.3 KB | 1,749 lines |
- Newsgroups: comp.sources.unix
- From: phil@eecs.nwu.edu (William LeFebvre)
- Subject: v27i012: top - a top process display, version 3.2, Part12/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 12
- Archive-Name: top-3.2/part12
-
- #! /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 12 (of 13)."
- # Contents: machine/m_sunos4.c machine/m_sunos4mp.c
- # Wrapped by phil@pex on Wed Aug 4 14:22:46 1993
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'machine/m_sunos4.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'machine/m_sunos4.c'\"
- else
- echo shar: Extracting \"'machine/m_sunos4.c'\" \(20456 characters\)
- sed "s/^X//" >'machine/m_sunos4.c' <<'END_OF_FILE'
- X/*
- X * top - a top users display for Unix
- X *
- X * SYNOPSIS: any Sun running SunOS version 4.x
- X *
- X * DESCRIPTION:
- X * This is the machine-dependent module for SunOS 4.x.
- X * This makes top work on the following systems:
- X * SunOS 4.0
- X * SunOS 4.0.1
- X * SunOS 4.0.2 (including 386i architecture)
- X * SunOS 4.0.3
- X * SunOS 4.1
- X * SunOS 4.1.1
- X * SunOS 4.1.2 (including MP architectures)
- X * SunOS 4.1.3 (including MP architectures)
- X * Solbourne OS/MP PRIOR to 4.1A
- X *
- X * LIBS: -lkvm
- X *
- X * AUTHOR: William LeFebvre <phil@eecs.nwu.edu>
- X * Solbourne support by David MacKenzie <djm@eng.umd.edu>
- X */
- X
- X/*
- X * #ifdef MULTIPROCESSOR means Sun MP.
- X * #ifdef solbourne is for Solbourne.
- X */
- X
- X#include <sys/types.h>
- X#include <sys/signal.h>
- X
- X/* make sure param.h gets loaded with KERNEL defined to get PZERO & NZERO */
- X#define KERNEL
- X#include <sys/param.h>
- X#undef KERNEL
- X
- X#include <stdio.h>
- X#include <kvm.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 <sys/file.h>
- X#include <sys/time.h>
- X#include <vm/page.h>
- X
- X#ifdef solbourne
- X#include <sys/syscall.h>
- X#endif
- X
- X#include "top.h"
- X#include "machine.h"
- X
- X/* declarations for load_avg */
- X#include "loadavg.h"
- 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/* 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_tsize + (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_CP_TIME 6
- X#define X_PAGES 7
- X#define X_EPAGES 8
- X
- Xstatic struct nlist nlst[] = {
- X#ifdef i386
- X { "avenrun" }, /* 0 */
- X { "ccpu" }, /* 1 */
- X { "mpid" }, /* 2 */
- X { "nproc" }, /* 3 */
- X { "proc" }, /* 4 */
- X { "total" }, /* 5 */
- X { "cp_time" }, /* 6 */
- X { "pages" }, /* 7 */
- X { "epages" }, /* 8 */
- X#else
- X { "_avenrun" }, /* 0 */
- X { "_ccpu" }, /* 1 */
- X { "_mpid" }, /* 2 */
- X { "_nproc" }, /* 3 */
- X { "_proc" }, /* 4 */
- X { "_total" }, /* 5 */
- X { "_cp_time" }, /* 6 */
- X { "_pages" }, /* 7 */
- X { "_epages" }, /* 8 */
- X#ifdef MULTIPROCESSOR
- X { "_ncpu" },
- X#define X_NCPU 9
- X { "_xp_time" },
- X#define X_XP_TIME 10
- X#endif
- X#endif
- 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/* values that we stash away in _init and use in later routines */
- X
- Xstatic double logcpu;
- Xkvm_t *kd;
- X
- X/* these are retrieved from the kernel in _init */
- X
- Xstatic unsigned long proc;
- Xstatic int nproc;
- Xstatic load_avg ccpu;
- Xstatic unsigned long pages;
- Xstatic unsigned long epages;
- Xstatic int ncpu = 0;
- 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;
- Xstatic unsigned long cp_time_offset;
- X#ifdef MULTIPROCESSOR
- Xstatic unsigned long xp_time_offset;
- X#endif
- 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#ifdef MULTIPROCESSOR
- Xstatic long xp_time[NCPU][XPSTATES];
- X/* for now we only accumulate spin time, but extending this to pick up
- X other stuff in xp_time is trivial. */
- Xstatic long xp_old[NCPU];
- X#endif
- 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[5];
- Xchar *cpustatenames[] = {
- X "user", "nice", "system", "idle",
- X#ifdef MULTIPROCESSOR
- X "spin",
- X#define XCP_SPIN 4
- X#endif
- X NULL
- X};
- X
- X/* these are for detailing the memory statistics */
- X
- Xint memory_stats[4];
- Xchar *memorynames[] = {
- X "K available, ", "K in use, ", "K free, ", "K locked", 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/* these are for getting the memory statistics */
- X
- Xstatic struct page *physpage;
- Xstatic int bytesize;
- Xstatic int count;
- 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 */
- Xextern int errno;
- Xextern char *sys_errlist[];
- X
- Xlong lseek();
- Xlong time();
- Xlong percentages();
- X
- Xmachine_init(statics)
- X
- Xstruct statics *statics;
- X
- X{
- X register int i;
- X register int pagesize;
- X
- X /* initialize the kernel interface */
- X if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "top")) == NULL)
- X {
- X perror("kvm_open");
- X return(-1);
- X }
- X
- X /* get the list of symbols we want to access in the kernel */
- X if ((i = kvm_nlist(kd, nlst)) < 0)
- X {
- X fprintf(stderr, "top: nlist failed\n");
- X return(-1);
- X }
- X
- X#ifdef MULTIPROCESSOR
- X /* were ncpu and xp_time not found in the nlist? */
- X if (i > 0 && nlst[X_NCPU].n_type == 0 && nlst[X_XP_TIME].n_type == 0)
- X {
- X /* we were compiled on an MP system but we are not running on one */
- X /* so we will pretend this didn't happen and set ncpu = 1 */
- X i -= 2;
- X ncpu = 1;
- X }
- X#endif
- X
- X#ifdef solbourne
- X {
- X unsigned int status, type;
- X
- X /* Get the number of CPUs on this system. */
- X syscall(SYS_getcpustatus, &status, &ncpu, &type);
- X }
- X#endif
- 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_PAGES].n_value, (int *)(&pages), sizeof(pages),
- X nlst[X_PAGES].n_name);
- X (void) getkval(nlst[X_EPAGES].n_value, (int *)(&epages), sizeof(epages),
- X nlst[X_EPAGES].n_name);
- X#ifdef MULTIPROCESSOR
- X if (ncpu == 0)
- X {
- X /* if ncpu > 0 then we are not really on an MP system */
- X (void) getkval(nlst[X_NCPU].n_value, (int *)(&ncpu), sizeof(ncpu),
- X nlst[X_NCPU].n_name);
- X }
- X#endif
- 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 cp_time_offset = nlst[X_CP_TIME].n_value;
- X#ifdef MULTIPROCESSOR
- X xp_time_offset = nlst[X_XP_TIME].n_value;
- X#endif
- 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 /* allocate a table to hold all the page structs */
- X bytesize = epages - pages;
- X count = bytesize / sizeof(struct page);
- X physpage = (struct page *)malloc(epages - pages);
- X if (physpage == NULL)
- X {
- X fprintf(stderr, "top: can't allocate sufficient memory\n");
- X return(-1);
- X }
- X
- 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#if defined(MULTIPROCESSOR) || defined(solbourne)
- X /* add a slash to the "run" state abbreviation */
- X if (ncpu > 1)
- X {
- X state_abbrev[SRUN][3] = '/';
- X }
- X#endif
- 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 long total;
- X#ifdef MULTIPROCESSOR
- X long half_total;
- X#endif
- X
- X /* get the cp_time array */
- X (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
- X "_cp_time");
- X
- X#ifdef MULTIPROCESSOR
- X /* get the xp_time array as well */
- X if (ncpu > 1)
- X {
- X (void) getkval(xp_time_offset, (int *)xp_time, sizeof(xp_time),
- X "_xp_time");
- X }
- X#endif
- 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 /* get the array of physpage descriptors */
- X (void) getkval(pages, (int *)physpage, bytesize, "array _page");
- 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 total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
- X
- X#ifdef MULTIPROCESSOR
- X /* calculate spin time from all processors */
- X if (ncpu > 1)
- X {
- X register int c;
- X register int i;
- X register long sum;
- X register long change;
- X
- X /* collect differences for each processor and add them */
- X sum = 0;
- X for (i = 0; i < ncpu; i++)
- X {
- X c = xp_time[i][XP_SPIN];
- X change = c - xp_old[i];
- X if (change < 0)
- X {
- X /* counter wrapped */
- X change = (long)((unsigned long)c -
- X (unsigned long)xp_old[i]);
- X }
- X sum += change;
- X xp_old[i] = c;
- X }
- X
- X /*
- X * NOTE: I am assuming that the ticks found in xp_time are
- X * already included in the ticks accumulated in cp_time. To
- X * get an accurate reflection, therefore, we have to subtract
- X * the spin time from the system time and recompute those two
- X * percentages.
- X */
- X half_total = total / 2l;
- X cp_diff[CP_SYS] -= sum;
- X cpu_states[CP_SYS] = (int)((cp_diff[CP_SYS] * 1000 + half_total) /
- X total);
- X cpu_states[XCP_SPIN] = (int)((sum * 1000 + half_total) / total);
- X }
- X#endif
- X
- X /* sum memory statistics */
- X {
- X register struct page *pp;
- X register int cnt;
- X register int inuse;
- X register int free;
- X register int locked;
- X
- X /* bop thru the array counting page types */
- X pp = physpage;
- X inuse = free = locked = 0;
- X for (cnt = count; --cnt >= 0; pp++)
- X {
- X if (pp->p_free)
- X free++;
- X else if (pp->p_lock || pp->p_keepcnt > 0)
- X locked++;
- X else
- X inuse++;
- X }
- X
- X /* convert memory stats to Kbytes */
- X memory_stats[0] = pagetok(inuse + free);
- X memory_stats[1] = pagetok(inuse);
- X memory_stats[2] = pagetok(free);
- X memory_stats[3] = pagetok(locked);
- X }
- 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 int show_command;
- 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 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 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 /* 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#ifdef MULTIPROCESSOR
- X /*
- X * If there is more than one cpu then add the processor number to
- X * the "run/" string. Note that this will only show up if the
- X * process is in the run state. Also note: when they
- X * start making Suns with more than 9 processors this will break
- X * since the string will then be more than 5 characters.
- X */
- X if (ncpu > 1)
- X {
- X state_abbrev[SRUN][4] = (pp->p_cpuid & 0xf) + '0';
- X }
- X#endif
- X#ifdef solbourne
- X if (ncpu > 1)
- X {
- X state_abbrev[SRUN][4] = (pp->p_lastcpu) + '0';
- X }
- X#endif
- 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 register struct user *lu;
- X
- X lu = kvm_getu(kd, p);
- X if (lu == NULL)
- X {
- X return(-1);
- X }
- X else
- X {
- X *u = *lu;
- X return(0);
- 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#ifdef i386
- X if (nlst->n_value == 0)
- X#else
- X if (nlst->n_type == 0)
- X#endif
- 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, 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, sys_errlist[errno]);
- X quit(23);
- X /*NOTREACHED*/
- 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 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 * 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 20456 -ne `wc -c <'machine/m_sunos4.c'`; then
- echo shar: \"'machine/m_sunos4.c'\" unpacked with wrong size!
- fi
- # end of 'machine/m_sunos4.c'
- fi
- if test -f 'machine/m_sunos4mp.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'machine/m_sunos4mp.c'\"
- else
- echo shar: Extracting \"'machine/m_sunos4mp.c'\" \(19935 characters\)
- sed "s/^X//" >'machine/m_sunos4mp.c' <<'END_OF_FILE'
- X/*
- X * top - a top users display for Unix
- X *
- X * SYNOPSIS: any multi-processor Sun running SunOS versions 4.1.2 or 4.1.3
- X *
- X * DESCRIPTION:
- X * This is the machine-dependent module for SunOS 4.x with multi-processor
- X * support. This module always compiles code for multiprocessors and
- X * assumes that it is being compiled on a multiprocessor architecture
- X * such as sun4m). This makes top work on the following systems:
- X * SunOS 4.1.2 (MP architectures only)
- X * SunOS 4.1.3 (MP architectures only)
- X * Solbourne running os/mp 4.1b or later only
- X *
- X * LIBS: -lkvm
- X *
- X * AUTHOR: William LeFebvre <phil@eecs.nwu.edu>
- X * Solbourne support by David MacKenzie <djm@eng.umd.edu>
- X */
- X
- X/*
- X * #ifdef MULTIPROCESSOR means Sun MP or newer Solbourne
- X */
- X
- X#include <sys/types.h>
- X#include <sys/signal.h>
- X
- X/*
- X * When including files, we need to have MULTIPROCESSOR on so that a version
- X * compiled on a non-MP system will work on an MP system. We must take
- X * great care, then in pur interpretation of certain preprocessor constants,
- X * such as NCPU, XPSTATES, XP_*.
- X */
- X#ifndef MULTIPROCESSOR
- X#define MULTIPROCESSOR
- X#endif
- X
- X/* make sure param.h gets loaded with KERNEL defined to get PZERO & NZERO */
- X#define KERNEL
- X#include <sys/param.h>
- X#undef KERNEL
- X
- X#include <stdio.h>
- X#include <kvm.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 <sys/file.h>
- X#include <sys/time.h>
- X#include <vm/page.h>
- X
- X#include "top.h"
- X#include "machine.h"
- X
- X/* declarations for load_avg */
- X#include "loadavg.h"
- 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/* 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_tsize + (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_CP_TIME 6
- X#define X_PAGES 7
- X#define X_EPAGES 8
- X
- Xstatic struct nlist nlst[] = {
- X#ifdef i386
- X { "avenrun" }, /* 0 */
- X { "ccpu" }, /* 1 */
- X { "mpid" }, /* 2 */
- X { "nproc" }, /* 3 */
- X { "proc" }, /* 4 */
- X { "total" }, /* 5 */
- X { "cp_time" }, /* 6 */
- X { "pages" }, /* 7 */
- X { "epages" }, /* 8 */
- X#else
- X { "_avenrun" }, /* 0 */
- X { "_ccpu" }, /* 1 */
- X { "_mpid" }, /* 2 */
- X { "_nproc" }, /* 3 */
- X { "_proc" }, /* 4 */
- X { "_total" }, /* 5 */
- X { "_cp_time" }, /* 6 */
- X { "_pages" }, /* 7 */
- X { "_epages" }, /* 8 */
- X#define NLST_REQUIRED 9
- X { "_ncpu" },
- X#define X_NCPU 9
- X { "_xp_time" },
- X#define X_XP_TIME 10
- X#endif
- 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/* values that we stash away in _init and use in later routines */
- X
- Xstatic double logcpu;
- Xkvm_t *kd;
- X
- X/* these are retrieved from the kernel in _init */
- X
- Xstatic unsigned long proc;
- Xstatic int nproc;
- Xstatic load_avg ccpu;
- Xstatic unsigned long pages;
- Xstatic unsigned long epages;
- Xstatic int ncpu = 0;
- X#define IS_MP (ncpu > 1)
- 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;
- Xstatic unsigned long cp_time_offset;
- Xstatic unsigned long xp_time_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];
- Xstatic long xp_time[NCPU][XPSTATES];
- X/* for now we only accumulate spin time, but extending this to pick up
- X other stuff in xp_time is trivial. */
- Xstatic long xp_old[NCPU];
- 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[5];
- Xchar *cpustatenames[] = {
- X "user", "nice", "system", "idle",
- X NULL, /* set to "spin" on MP machines */
- X NULL
- X};
- X#define XCP_SPIN 4
- X
- X/* these are for detailing the memory statistics */
- X
- Xint memory_stats[4];
- Xchar *memorynames[] = {
- X "K available, ", "K in use, ", "K free, ", "K locked", 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/* these are for getting the memory statistics */
- X
- Xstatic struct page *physpage;
- Xstatic int bytesize;
- Xstatic int count;
- 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 */
- Xextern int errno;
- Xextern char *sys_errlist[];
- X
- Xlong lseek();
- Xlong time();
- Xlong percentages();
- X
- Xmachine_init(statics)
- X
- Xstruct statics *statics;
- X
- X{
- X register int i;
- X register int pagesize;
- X
- X /* initialize the kernel interface */
- X if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "top")) == NULL)
- X {
- X perror("kvm_open");
- X return(-1);
- X }
- X
- X /* get the list of symbols we want to access in the kernel */
- X if ((i = kvm_nlist(kd, nlst)) < 0)
- X {
- X fprintf(stderr, "top: nlist failed\n");
- X return(-1);
- X }
- X
- X /* were ncpu and xp_time not found in the nlist? */
- X if (i > 0 && nlst[X_NCPU].n_type == 0 && nlst[X_XP_TIME].n_type == 0)
- X {
- X /* we are only running with one cpu */
- X /* so we will pretend this didn't happen and set ncpu = 1 */
- X i -= 2;
- X ncpu = 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_PAGES].n_value, (int *)(&pages), sizeof(pages),
- X nlst[X_PAGES].n_name);
- X (void) getkval(nlst[X_EPAGES].n_value, (int *)(&epages), sizeof(epages),
- X nlst[X_EPAGES].n_name);
- X if (ncpu == 0)
- X {
- X /* we have not yet determined the number of processors, so
- X do that now */
- X /* assert: nlst[X_NCPU].n_type != 0 => nlst[X_NCPU].n_value != 0 */
- X (void) getkval(nlst[X_NCPU].n_value, (int *)(&ncpu), sizeof(ncpu),
- X nlst[X_NCPU].n_name);
- 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 cp_time_offset = nlst[X_CP_TIME].n_value;
- X xp_time_offset = nlst[X_XP_TIME].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 /* allocate a table to hold all the page structs */
- X bytesize = epages - pages;
- X count = bytesize / sizeof(struct page);
- X physpage = (struct page *)malloc(epages - pages);
- X if (physpage == NULL)
- X {
- X fprintf(stderr, "top: can't allocate sufficient memory\n");
- X return(-1);
- X }
- X
- 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 /* add a slash to the "run" state abbreviation */
- X if (IS_MP)
- X {
- X state_abbrev[SRUN][3] = '/';
- X cpustatenames[XCP_SPIN] = "spin";
- 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 long total;
- X long half_total;
- X
- X /* get the cp_time array */
- X (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
- X "_cp_time");
- X
- X if (IS_MP)
- X {
- X /* get the xp_time array as well */
- X (void) getkval(xp_time_offset, (int *)xp_time, sizeof(xp_time),
- X "_xp_time");
- 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 /* get the array of physpage descriptors */
- X (void) getkval(pages, (int *)physpage, bytesize, "array _page");
- 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 total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
- X
- X /* calculate spin time from all processors */
- X if (IS_MP)
- X {
- X register int c;
- X register int i;
- X register long sum;
- X register long change;
- X
- X /* collect differences for each processor and add them */
- X sum = 0;
- X for (i = 0; i < ncpu; i++)
- X {
- X c = xp_time[i][XP_SPIN];
- X change = c - xp_old[i];
- X if (change < 0)
- X {
- X /* counter wrapped */
- X change = (long)((unsigned long)c -
- X (unsigned long)xp_old[i]);
- X }
- X sum += change;
- X xp_old[i] = c;
- X }
- X
- X /*
- X * NOTE: I am assuming that the ticks found in xp_time are
- X * already included in the ticks accumulated in cp_time. To
- X * get an accurate reflection, therefore, we have to subtract
- X * the spin time from the system time and recompute those two
- X * percentages.
- X */
- X half_total = total / 2l;
- X cp_diff[CP_SYS] -= sum;
- X cpu_states[CP_SYS] = (int)((cp_diff[CP_SYS] * 1000 + half_total) /
- X total);
- X cpu_states[XCP_SPIN] = (int)((sum * 1000 + half_total) / total);
- X }
- X
- X /* sum memory statistics */
- X {
- X register struct page *pp;
- X register int cnt;
- X register int inuse;
- X register int free;
- X register int locked;
- X
- X /* bop thru the array counting page types */
- X pp = physpage;
- X inuse = free = locked = 0;
- X for (cnt = count; --cnt >= 0; pp++)
- X {
- X if (pp->p_free)
- X free++;
- X else if (pp->p_lock || pp->p_keepcnt > 0)
- X locked++;
- X else
- X inuse++;
- X }
- X
- X /* convert memory stats to Kbytes */
- X memory_stats[0] = pagetok(inuse + free);
- X memory_stats[1] = pagetok(inuse);
- X memory_stats[2] = pagetok(free);
- X memory_stats[3] = pagetok(locked);
- X }
- 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 int show_command;
- 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 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 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 /* 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 /*
- X * If there is more than one cpu then add the processor number to
- X * the "run/" string. Note that this will only show up if the
- X * process is in the run state. Also note: when they
- X * start making Suns with more than 9 processors this will break
- X * since the string will then be more than 5 characters.
- X */
- X if (IS_MP)
- X {
- X state_abbrev[SRUN][4] = (pp->p_cpuid & 0xf) + '0';
- X }
- 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 register struct user *lu;
- X
- X lu = kvm_getu(kd, p);
- X if (lu == NULL)
- X {
- X return(-1);
- X }
- X else
- X {
- X *u = *lu;
- X return(0);
- 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#ifdef i386
- X if (nlst->n_value == 0)
- X#else
- X if (nlst->n_type == 0)
- X#endif
- 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, 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, sys_errlist[errno]);
- X quit(23);
- X /*NOTREACHED*/
- 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 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
- Xint proc_owner(pid)
- X
- Xint pid;
- X
- X{
- X register int ac;
- X register struct proc **prefp;
- X register struct proc *pp;
- X
- X prefp = pref;
- X ac = pref_len;
- X while (--ac >= 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 19935 -ne `wc -c <'machine/m_sunos4mp.c'`; then
- echo shar: \"'machine/m_sunos4mp.c'\" unpacked with wrong size!
- fi
- # end of 'machine/m_sunos4mp.c'
- fi
- echo shar: End of archive 12 \(of 13\).
- cp /dev/null ark12isdone
- 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
-