Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

v29i122: top-3.4 - top process display, V3.4, Part21/22

1 view
Skip to first unread message

William Lefebvre

unread,
Sep 1, 1996, 3:00:00 AM9/1/96
to

Submitted-By: w...@groupsys.com (William Lefebvre)
Posting-Number: Volume 29, Issue 122
Archive-Name: top-3.4/part21

#! /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 21 (of 22)."
# Contents: top-3.4/machine/m_next32.c
# Wrapped by lefebvre@acapulco on Fri Aug 30 12:35:52 1996
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'top-3.4/machine/m_next32.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'top-3.4/machine/m_next32.c'\"
else
echo shar: Extracting \"'top-3.4/machine/m_next32.c'\" \(28829 characters\)
sed "s/^X//" >'top-3.4/machine/m_next32.c' <<'END_OF_FILE'
X/*
X * top - a top users display for Unix
X * NEXTSTEP v.0.3 2/14/1996 tpugh
X *
X * SYNOPSIS: any m68k or intel NEXTSTEP v3.x system
X *
X * DESCRIPTION:
X * This is the machine-dependent module for NEXTSTEP v3.x
X * Reported to work for NEXTSTEP v3.0, v3.2, and v3.3 Mach OS:
X * NEXTSTEP v3.0 on Motorola machines.
X * NEXTSTEP v3.2 on Intel and Motorola machines.
X * NEXTSTEP v3.3 on Intel and Motorola machines.
X * Problem with command column for (Choose next40 for fix):
X * NEXTSTEP v3.2 on HP machines.
X * NEXTSTEP v3.3 on HP and Sparc machines.
X * Has not been tested for NEXTSTEP v2.x machines, although it should work.
X * Has not been tested for NEXTSTEP v3.1 machines, although it should work.
X * Install "top" with the permissions 4755.
X * hostname# chmod 4755 top
X * hostname# ls -lg top
X * -rwsr-xr-x 1 root kmem 121408 Sep 1 10:14 top*
X * With the kmem group sticky bit set, we can read kernal memory without problems,
X * but to communicate with the Mach kernal for task and thread info, it requires
X * root privileges. Therefore, "top" must be setuid 4755 with the owner as root.
X *
X * LIBS:
X *
X * Need the compiler flag, "-DSHOW_UTT", to see the user task and thread task
X * data structures to report process info.
X *
X * CFLAGS: -DSHOW_UTT
X *
X *
X * AUTHORS: Tim Pugh <tp...@oce.orst.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 <sys/file.h>
X#include <sys/time.h>
X#import <mach/mach.h>
X#include <sys/vmmeter.h>
X#import <mach/vm_statistics.h>
X
X#import "machine/m_next_task.h"
X
X/* Problems on NS/HPPA machines. Also, not currently used by source code.
X *#define DOSWAP
X */
X
X#include "top.h"
X#include "machine.h"
X#include "utils.h"
X
Xextern int errno, sys_nerr;
Xextern char *sys_errlist[];
X#define strerror(e) (((e) >= 0 && (e) < sys_nerr) ? sys_errlist[(e)] : "Unknown error")
X
X#define VMUNIX "/mach"
X#define KMEM "/dev/kmem"
X#define MEM "/dev/mem"
X#ifdef DOSWAP
X#define SWAP "/dev/drum"
X#endif
X
X/* NeXT BSD process structure does not contain locations to hold info such as
X * cpu usage, memory usage, resident core memory, or cpu time data. So I've made
X * a new process structure which composites the NeXT structure and the missing
X * system info.
X */
Xstruct proc_unix {
X struct proc *p_self; /* Each p_self points to a element in pbase. */
X int p_pctcpu; /* Scaled percent cpu usage. */
X int p_vsize; /* Total VM memory usage. */
X int p_rsize; /* Resident core memory usage. */
X int p_cptime; /* scaled CPU Time */
X int run_state; /* Task run state. */
X int flags; /* Task state flags. */
X int nthreads; /* Number of threads per Task. */
X int cur_priority; /* Current main thread priority */
X};
X
X/* Contains the list of processes. */
Xstruct handle
X{
X struct proc_unix *list; /* points to list of valid proc pointer */
X int count; /* number of pointers */
X int current; /* Index of the current process formatting */
X};
X
X/* declarations for load_avg */
X#include "loadavg.h"
X#define LSCALE 1000 /* scaling for "fixed point" arithmetic - <sys/kernel.h> */
X
X/* define what weighted cpu is. */
X/*
X *#define weighted_cpu(pct, pp) ((pp)->p_time == 0 ? 0.0 : \
X * ((pct) / (1.0 - exp((pp)->p_time * logcpu))))
X */
X
X/* The following three variables are not defined in NeXT's process structure.
X * So they are zeroed until other ways of obtaining the info are found.
X */
X/* what we consider to be process size: */
X/* #define PROCSIZE(pp) ((pp)->p_tsize + (pp)->p_dsize + (pp)->p_ssize) */
X#define PROCSIZE(pp) (0)
X
X/* #define P_RSSIZE(pp) ((pp)->p_rssize) */
X#define P_RSSIZE(pp) (0)
X
X/* #define P_CPTICKS(pp) ((pp)->p_cpticks) */
X#define P_CPTICKS(pp) (0)
X
X
Xextern int thread_stats(int p_pid, struct thread_basic_info *info, int *count);
Xextern int mach_load_avg(void);
Xextern kern_return_t task_stats(int p_pid, struct task_basic_info *info);
X
X/* definitions for indices in the nlist array */
X#define X_AVENRUN 0
X#define X_CCPU 1
X#define X_NPROC 2
X#define X_PROC 3
X#define X_TOTAL 4
X#define X_CP_TIME 5
X#define X_MPID 6
X#define X_HZ 7
X
Xstatic struct nlist nlst[] = {
X { "_avenrun" }, /* 0 */
X { "_cpu_clk" }, /* 1 */
X { "_max_proc" }, /* 2 */
X { "_allproc" }, /* 3 */
X { "_total" }, /* 4 */
X { "_cp_time" }, /* 5 */
X { "_mpid" }, /* 6 */
X { "_hz" }, /* 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 STATE PRI NICE THR VSIZE RSIZE %MEM %CPU TIME COMMAND";
X/* static char header[] =
X * " PID X STATE PRI NICE THR VSIZE RSIZE %MEM %WCPU %CPU TIME COMMAND";
X */
X
X/* 0123456 -- field to fill in starts at header+6 */
X#define UNAME_START 6
X
X#define Proc_format \
X "%5d %-8.8s %-5s %3d %4d %4d %5s %5s %6.2f %6.2f %6s %.14s"
X/* #define Proc_format \
X * "%5d %-8.8s %-5s %3d %4d %4d %5s %5s %6.2f %6.2f %6.2f %6s %.14s"
X */
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 */
Xchar *state_abbrev[] =
X{
X "", "sleep", "WAIT", "run\0\0\0", "start", "zomb", "stop"
X};
Xchar *mach_state[] =
X{
X "", "R", "T", "S", "U", "H"
X};
Xchar *flags_state[] =
X{
X "", "W", "I"
X};
X
X/* these are for detailing the process states */
Xint process_states[7];
X/* char *procstatenames[] = {
X * "", " sleeping, ", " ABANDONED, ", " running, ", " starting, ",
X * " zombie, ", " stopped, ",
X * NULL
X *};
X */
Xchar *procstatenames[] = {
X "", " running, ", " stopped, ", " sleeping, ", " uninterruptable, ",
X " halted, ", " zombie ", NULL
X};
X
X
Xstatic int kmem, mem;
X#ifdef DOSWAP
Xstatic int swap;
X#endif
X
X/* values that we stash away in _init and use in later routines */
X
X/* static double logcpu; */
X
X/* these are retrieved from the kernel in _init */
X
Xstatic unsigned long proc;
Xstatic int nproc;
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 avenrun_offset;
Xstatic unsigned long mpid_offset;
Xstatic unsigned long total_offset;
Xstatic unsigned long cp_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];
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 */
Xint memory_stats[7];
X/* char *memorynames[] = {
X * "Real: ", "K/", "K act/tot ", "Virtual: ", "K/",
X * "K act/tot ", "Free: ", "K", NULL
X * };
X */
Xchar *memorynames[] = {
X "K Tot, ", "K Act, ", "K Inact, ", "K Wired, ", "K Free, ", "K in, ", "K out ", NULL
X};
X
X/* these are for keeping track of the proc array */
Xstatic int bytes;
Xstatic int pref_count;
Xstatic struct proc *pbase;
Xstatic struct proc_unix *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#define pagetok(size) ((size) << pageshift)
X
X/* useful externals */
Xextern int errno;
Xextern char *sys_errlist[];
X
Xlong lseek();
Xlong time();
X
Xmachine_init(struct statics *statics)
X{
X register int i = 0;
X register int pagesize;
X
X if ((kmem = open(KMEM, O_RDONLY)) == -1) {
X perror(KMEM);
X return(-1);
X }
X if ((mem = open(MEM, O_RDONLY)) == -1) {
X perror(MEM);
X return(-1);
X }
X
X#ifdef DOSWAP
X if ((swap = open(SWAP, O_RDONLY)) == -1) {
X perror(SWAP);
X return(-1);
X }
X#endif
X
X /* get the list of symbols we want to access in the kernel */
X (void) nlist(VMUNIX, 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_PROC].n_value, (int *)(&proc), sizeof(proc),
X nlst[X_PROC].n_un.n_name);
X (void) getkval(nlst[X_NPROC].n_value, &nproc, sizeof(nproc),
X nlst[X_NPROC].n_un.n_name);
X (void) getkval(nlst[X_HZ].n_value, (int *)(&hz), sizeof(hz),
X nlst[X_HZ].n_un.n_name);
X/* (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu),
X * nlst[X_CCPU].n_un.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
X
X /* this is used in calculating WCPU -- calculate it ahead of time */
X/* ccpu = mach_load_avg();
X * logcpu = log((double)(ccpu)/LOAD_SCALE);
X */
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_unix *)malloc((nproc+1) * sizeof(struct proc_unix *));
X
X /* Just in case ... */
X if (pbase == (struct proc *)NULL || pref == (struct proc_unix *)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 = ceil(log(pagesize)/log(2.0));
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(register char *uname_field)
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
Xstatic int swappgsin = -1;
Xstatic int swappgsout = -1;
Xstatic vm_statistics_data_t vm_stats;
Xstatic host_basic_info_data_t host_stats;
X
Xget_system_info(struct system_info *si)
X{
X long avenrun[3];
X long 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 /* 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 for(i=0; i<3; i++)
X si->load_avg[i] = ((double)avenrun[i])/LSCALE;
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 /* get total -- systemwide main memory usage structure */
X /* Does not work on NeXT system. Use vm_statistics() for paging info. */
X /* struct vmtotal total;
X * (void) getkval(total_offset, (int *)(&total), sizeof(total),
X * "_total");
X */
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 kern_return_t status;
X unsigned int count=HOST_BASIC_INFO_COUNT;
X status = vm_statistics(task_self(), &vm_stats);
X#ifdef DEBUG
X if(status != KERN_SUCCESS)
X mach_error("An error calling vm_statistics()!", status);
X#endif
X status = host_info(host_self(), HOST_BASIC_INFO, (host_info_t)&host_stats, &count);
X#ifdef DEBUG
X if(status != KERN_SUCCESS)
X mach_error("An error calling host_info()!", status);
X#endif
X /* convert memory stats to Kbytes */
X memory_stats[0] = pagetok(host_stats.memory_size / vm_stats.pagesize);
X memory_stats[1] = pagetok(vm_stats.active_count);
X memory_stats[2] = pagetok(vm_stats.inactive_count);
X memory_stats[3] = pagetok(vm_stats.wire_count);
X memory_stats[4] = pagetok(vm_stats.free_count);
X if (swappgsin < 0)
X {
X memory_stats[5] = 1;
X memory_stats[6] = 1;
X } else {
X memory_stats[5] = pagetok(((vm_stats.pageins - swappgsin)));
X memory_stats[6] = pagetok(((vm_stats.pageouts - swappgsout)));
X }
X swappgsin = vm_stats.pageins;
X swappgsout = vm_stats.pageouts;
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(struct system_info *si,
X struct process_select *sel,
X int (*compare)())
X{
X int i, j;
X int total_procs;
X int active_procs;
X struct proc *pp;
X struct task_basic_info taskInfo;
X struct thread_basic_info threadInfo;
X kern_return_t thread_status;
X kern_return_t task_status;
X int threadCount;
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 /* 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 (void) getkval(nlst[X_PROC].n_value, (int *)(&proc), sizeof(proc),
X nlst[X_PROC].n_un.n_name);
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 i = 0;
X j = 0;
X do {
X if(i == 0) {
X /* read first proc structure */
X (void) getkval(proc, (int *)&pbase[i], sizeof(struct proc), "first proc");
X } else {
X (void) getkval(pp->p_nxt, (int *)&pbase[i], sizeof(struct proc), "nxt proc");
X }
X pp = &pbase[i];
X
X thread_status = thread_stats(pp->p_pid, &threadInfo, &threadCount);
X task_status = task_stats(pp->p_pid, &taskInfo);
X /*
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/* Using thread info for process states. */
X/* process_states[pp->p_stat]++; */
X if(thread_status==KERN_SUCCESS)
X process_states[threadInfo.run_state]++;
X if ((pp->p_stat != SZOMB) &&
X (show_idle || (pp->p_stat == SRUN)) &&
X (!show_uid || pp->p_uid == (uid_t)sel->uid))
X {
X pref[j].p_self = pp;
X if(thread_status==KERN_SUCCESS)
X {
X pref[j].run_state = threadInfo.run_state;
X pref[j].flags = threadInfo.flags;
X pref[j].p_pctcpu = threadInfo.cpu_usage;
X pref[j].p_cptime = threadInfo.user_time.seconds +
X threadInfo.system_time.seconds;
X pref[j].cur_priority = threadInfo.cur_priority;
X pref[j].nthreads = threadCount;
X } else {
X pref[j].run_state = 0;
X pref[j].flags = 0;
X pref[j].p_pctcpu = 0;
X pref[j].p_cptime = 0;
X }
X /* Get processes memory usage and cputime */
X if(task_status==KERN_SUCCESS)
X {
X pref[j].p_rsize = taskInfo.resident_size/1024;
X pref[j].p_vsize = taskInfo.virtual_size/1024;
X } else {
X pref[j].p_rsize = 0;
X pref[j].p_vsize = 0;
X }
X active_procs++;
X j++;
X }
X }
X i++;
X } while(pp->p_nxt != 0);
X pref[j].p_self = NULL; /* End list of processes with NULL */
X
X /* if requested, sort the "interesting" processes */
X if (compare != NULL)
X {
X qsort((char *)pref, active_procs, sizeof(struct proc_unix), compare);
X }
X
X /* remember active and total counts */
X si->p_total = total_procs;
X si->p_active = pref_count = active_procs;
X
X /* pass back a handle */
X handle.list = pref;
X handle.count = active_procs;
X handle.current = 0;
X return((caddr_t)&handle);
X}
X
Xchar fmt[MAX_COLS]; /* static area where result is built */
X
Xchar *format_next_process(caddr_t handle, char *(*get_userid)())
X{
X register struct proc *pp;
X register long cputime;
X register double pct, wcpu, pctmem;
X int where;
X struct user u;
X struct handle *hp;
X register int p_pctcpu;
X register int rm_size;
X register int vm_size;
X register int run_state;
X register int flags;
X register int nthreads;
X register int cur_priority;
X char state_str[10];
X
X /* find and remember the next proc structure */
X hp = (struct handle *)handle;
X pp = hp->list[hp->current].p_self;
X p_pctcpu = hp->list[hp->current].p_pctcpu;
X cputime = hp->list[hp->current].p_cptime;
X rm_size = hp->list[hp->current].p_rsize;
X vm_size = hp->list[hp->current].p_vsize;
X run_state = hp->list[hp->current].run_state;
X flags = hp->list[hp->current].flags;
X nthreads = hp->list[hp->current].nthreads;
X cur_priority = hp->list[hp->current].cur_priority;
X hp->current++;
X hp->count--;
X
X /* get the process's user struct and set cputime */
X where = getu(pp, &u);
X if (where == -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 if (where == 1) {
X /*
X * Print swapped processes as <pname>
X */
X char buf[sizeof(u.u_comm)];
X (void) strncpy(buf, u.u_comm, sizeof(u.u_comm));
X u.u_comm[0] = '<';
X (void) strncpy(&u.u_comm[1], buf, sizeof(u.u_comm) - 2);
X u.u_comm[sizeof(u.u_comm) - 2] = '\0';
X (void) strncat(u.u_comm, ">", sizeof(u.u_comm) - 1);
X u.u_comm[sizeof(u.u_comm) - 1] = '\0';
X }
X/* User structure does not work. Use Thread Info to get cputime for process. */
X/* cputime = u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec; */
X }
X
X
X /* calculate the base for cpu percentages */
X pct = (double)(p_pctcpu)/TH_USAGE_SCALE;
X/* wcpu = weighted_cpu(pct, pp);
X */
X pctmem = (double)(rm_size*1024.) / (double)(host_stats.memory_size);
X
X /* Get process state description */
X if(run_state)
X {
X strcpy(state_str, mach_state[run_state]);
X strcat(state_str, flags_state[flags]);
X } else {
X strcpy(state_str, state_abbrev[pp->p_stat]);
X }
X
X /* format this entry */
X sprintf(fmt,
X Proc_format,
X pp->p_pid,
X (*get_userid)(pp->p_uid),
X state_str,
X cur_priority,
X/* pp->p_pri - PZERO, */
X pp->p_nice - NZERO,
X nthreads,
X format_k(vm_size),
X format_k(rm_size),
X 100.0 * pctmem,
X/* 100.0 * wcpu, */
X 100.0 * pct,
X format_time(cputime),
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(register struct proc *p, struct user *u)
X{
X register int nbytes, n;
X struct task task;
X struct utask utask;
X struct uthread thread;
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 /* NEXTSTEP proc.h
X * One structure allocated per active
X * process. It contains all data needed
X * about the process while the
X * process may be swapped out.
X * Other per process data (user.h)
X * is swapped with the process.
X */
X
X if ((p->p_flag & SLOAD) == 0) {
X/* User info is always in core.
X * #ifdef DOSWAP
X * if (lseek(swap, (long)dtob(p->p_swaddr), 0) == -1) {
X * perror("lseek(swap)");
X * return(-1);
X * }
X * if (read(swap, (char *) u, sizeof(struct user)) != sizeof(struct user)) {
X * perror("read(swap)");
X * return(-1);
X * }
X * return (1);
X * #else
X */
X return(-1);
X/*#endif
X */
X }
X
X /*
X * Process is currently in memory, we hope!
X */
X if(!getkval(p->task, (int *)&task, sizeof(struct task), "task")) {
X#ifdef DEBUG
X perror("getkval(p->task)");
X#endif
X /* we can't seem to get to it, so pretend it's swapped out */
X return(-1);
X }
X
X if(!getkval(task.u_address, (int *)&utask, sizeof(struct utask), "task.u_address")) {
X#ifdef DEBUG
X perror("getkval(task->utask)");
X#endif
X /* we can't seem to get to it, so pretend it's swapped out */
X return(-1);
X }
X
X /* Copy utask and uthread info into struct user *u */
X /* This is incomplete. Only copied info needed. */
X u->u_procp = utask.uu_procp;
X u->u_ar0 = utask.uu_ar0;
X u->u_ru = utask.uu_ru;
X strcpy(u->u_comm, utask.uu_comm);
X nbytes = strlen(u->u_comm);
X for(n=nbytes; n<MAXCOMLEN; n++)
X u->u_comm[n] = ' ';
X u->u_comm[MAXCOMLEN] = '\0';
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(register struct nlist *nlst)
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_un.n_name != NULL)
X {
X if (nlst->n_type == 0 && nlst->n_value == 0)
X {
X /* this one wasn't found */
X fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_un.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(unsigned long offset, int *ptr, int size, char *refstr)
X{
X if (lseek(kmem, (long)offset, L_SET) == -1) {
X if (*refstr == '!')
X refstr++;
X (void) fprintf(stderr, "%s: lseek to %s: %s\n", KMEM,
X refstr, strerror(errno));
X quit(23);
X }
X if (read(kmem, (char *) ptr, size) == -1) {
X if (*refstr == '!')
X return(0);
X else {
X (void) fprintf(stderr, "%s: reading %s: %s\n", KMEM,
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(struct proc_unix *pp1, struct proc_unix *pp2)
X{
X register struct proc *p1 = pp1->p_self;
X register struct proc *p2 = pp2->p_self;
X register int result;
X register pctcpu lresult;
X
X /* compare percent cpu (pctcpu) */
X if ((lresult = pp2->p_pctcpu - pp1->p_pctcpu) == 0)
X {
X /* use cpticks to break the tie */
X if ((result = P_CPTICKS(p2) - P_CPTICKS(p1)) == 0)
X {
X /* use process state to break the tie */
X if ((result = sorted_state[p2->p_stat] - 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 = pp2->p_rsize - pp1->p_rsize) == 0)
X {
X /* use total memory to break the tie */
X result = pp2->p_vsize - pp1->p_vsize;
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(int pid)
X{
X register int cnt;
X register struct proc *pp;
X
X cnt = pref_count;
X while (--cnt >= 0)
X {
X pp = pref[cnt].p_self;
X if( pp->p_pid == pid ) /* Modified (pid_t)pid to pid, compiler error. */
X {
X return((int)pp->p_uid);
X }
X }
X return(-1);
X}
X
Xint thread_stats(int pid, struct thread_basic_info *info, int *thread_count)
X{
X int i;
X kern_return_t status;
X kern_return_t status_dealloc;
X task_t p_task;
X thread_array_t thread_list, list;
X struct thread_basic_info threadInfo;
X unsigned int info_count = THREAD_BASIC_INFO_COUNT;
X
X /* Get the task pointer for the process. */
X status = task_by_unix_pid( task_self(), pid, &p_task);
X if (status!=KERN_SUCCESS)
X {
X#ifdef DEBUG
X printf("pid = %i\n", pid);
X mach_error("Error calling task_by_unix_pid()", status);
X#endif
X return status;
X }
X
X /* Get the list of threads for the task. */
X status = task_threads(p_task, &thread_list, thread_count);
X if (status!=KERN_SUCCESS)
X {
X#ifdef DEBUG
X mach_error("Error calling task_threads()", status);
X#endif
X return status;
X }
X
X /* Get the pctcpu value for each thread and sum the values */
X info->user_time.seconds = 0;
X info->user_time.microseconds = 0;
X info->system_time.seconds = 0;
X info->system_time.microseconds = 0;
X info->cpu_usage = 0;
X info->sleep_time = 0;
X
X for(i=0; i<*thread_count; i++)
X {
X status = thread_info(thread_list[i], THREAD_BASIC_INFO,
X (thread_info_t)&threadInfo, &info_count);
X if (status!=KERN_SUCCESS)
X {
X#ifdef DEBUG
X mach_error("Error calling thread_info()", status);
X#endif
X break;
X } else {
X if(i==0)
X {
X info->base_priority = threadInfo.base_priority;
X info->cur_priority = threadInfo.cur_priority;
X info->run_state = threadInfo.run_state;
X info->flags = threadInfo.flags;
X info->suspend_count = threadInfo.suspend_count;
X info->sleep_time += threadInfo.sleep_time;
X }
X info->user_time.seconds += threadInfo.user_time.seconds;
X info->user_time.microseconds += threadInfo.user_time.microseconds;
X info->system_time.seconds += threadInfo.system_time.seconds;
X info->system_time.microseconds += threadInfo.system_time.microseconds;
X info->cpu_usage += threadInfo.cpu_usage;
X }
X }
X
X /* Deallocate the list of threads. */
X status_dealloc = vm_deallocate(task_self(), (vm_address_t)thread_list,
X sizeof(thread_list)*(*thread_count));
X if (status_dealloc != KERN_SUCCESS)
X {
X#ifdef DEBUG
X mach_error("Trouble freeing thread_list", status_dealloc);
X#endif
X status = status_dealloc;
X }
X return status;
X}
X
Xint mach_load_avg(void)
X{
X kern_return_t status;
X host_t host;
X unsigned int info_count;
X struct processor_set_basic_info info;
X processor_set_t default_set;
X
X status=processor_set_default(host_self(), &default_set);
X if (status!=KERN_SUCCESS){
X mach_error("Error calling processor_set_default", status);
X exit(1);
X }
X
X info_count=PROCESSOR_SET_BASIC_INFO_COUNT;
X status=processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO,
X &host, (processor_set_info_t)&info, &info_count);
X#ifdef DEBUG
X if (status != KERN_SUCCESS)
X mach_error("Error calling processor_set_info", status);
X#endif
X return info.load_average;
X}
X
Xkern_return_t task_stats(int pid, struct task_basic_info *info)
X{
X kern_return_t status;
X task_t p_task;
X unsigned int info_count=TASK_BASIC_INFO_COUNT;
X
X /* Get the task pointer for the process. */
X status = task_by_unix_pid( task_self(), pid, &p_task);
X if (status!=KERN_SUCCESS) {
X#ifdef DEBUG
X printf("pid = %i\n", pid);
X mach_error("Error calling task_by_unix_pid()", status);
X#endif
X return(status);
X }
X
X status=task_info(p_task, TASK_BASIC_INFO, (task_info_t)info, &info_count);
X if (status!=KERN_SUCCESS) {
X#ifdef DEBUG
X mach_error("Error calling task_info()", status);
X#endif
X return(status);
X }
X return(KERN_SUCCESS);
X}
END_OF_FILE
if test 28829 -ne `wc -c <'top-3.4/machine/m_next32.c'`; then
echo shar: \"'top-3.4/machine/m_next32.c'\" unpacked with wrong size!
fi
# end of 'top-3.4/machine/m_next32.c'
fi
echo shar: End of archive 21 \(of 22\).
cp /dev/null ark21isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ; do
if test ! -f ark${I}isdone ; then
MISSING="${MISSING} ${I}"
fi
done
if test "${MISSING}" = "" ; then
echo You have unpacked all 22 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


0 new messages