machine.c revision 104388
124143Sjoerg/* 224143Sjoerg * top - a top users display for Unix 324143Sjoerg * 447901Sn_hibma * SYNOPSIS: For FreeBSD-2.x and later 524143Sjoerg * 624143Sjoerg * DESCRIPTION: 724143Sjoerg * Originally written for BSD4.4 system by Christos Zoulas. 824143Sjoerg * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider 938278Swosch * Order support hacked in from top-3.5beta6/machine/m_aix41.c 1038278Swosch * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/) 1124143Sjoerg * 1224143Sjoerg * This is the machine-dependent module for FreeBSD 2.2 1324143Sjoerg * Works for: 1447901Sn_hibma * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x 1524143Sjoerg * 1624143Sjoerg * LIBS: -lkvm 1724143Sjoerg * 1824143Sjoerg * AUTHOR: Christos Zoulas <christos@ee.cornell.edu> 1924143Sjoerg * Steven Wallace <swallace@freebsd.org> 2024143Sjoerg * Wolfram Schneider <wosch@FreeBSD.org> 2172951Srwatson * Thomas Moestl <tmoestl@gmx.net> 2224143Sjoerg * 2350477Speter * $FreeBSD: head/usr.bin/top/machine.c 104388 2002-10-02 20:33:52Z jhb $ 2424143Sjoerg */ 2524143Sjoerg 2624143Sjoerg 2742447Sobrien#include <sys/time.h> 2824143Sjoerg#include <sys/types.h> 2924143Sjoerg#include <sys/signal.h> 3024143Sjoerg#include <sys/param.h> 3124143Sjoerg 3224143Sjoerg#include "os.h" 3324143Sjoerg#include <stdio.h> 3424143Sjoerg#include <nlist.h> 3524143Sjoerg#include <math.h> 3624143Sjoerg#include <kvm.h> 3727390Speter#include <pwd.h> 3824143Sjoerg#include <sys/errno.h> 3924143Sjoerg#include <sys/sysctl.h> 4024143Sjoerg#include <sys/dkstat.h> 4124143Sjoerg#include <sys/file.h> 4224143Sjoerg#include <sys/time.h> 4324143Sjoerg#include <sys/proc.h> 4424143Sjoerg#include <sys/user.h> 4524143Sjoerg#include <sys/vmmeter.h> 4629904Speter#include <sys/resource.h> 4729904Speter#include <sys/rtprio.h> 4824143Sjoerg 4924143Sjoerg/* Swap */ 5024143Sjoerg#include <stdlib.h> 5124143Sjoerg 5272951Srwatson#include <unistd.h> 5324143Sjoerg#include <osreldate.h> /* for changes in kernel structures */ 5424143Sjoerg 5524143Sjoerg#include "top.h" 5624143Sjoerg#include "machine.h" 5769375Sjhb#include "screen.h" 5872951Srwatson#include "utils.h" 5924143Sjoerg 6092922Simpstatic void getsysctl(char *, void *, size_t); 6172951Srwatson 6272951Srwatson#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) 6372951Srwatson 6492922Simpextern char* printable(char *); 6592922Simpint swapmode(int *retavail, int *retfree); 6627340Speterstatic int smpmode; 6727390Speterstatic int namelength; 6869375Sjhbstatic int cmdlengthdelta; 6924143Sjoerg 7072951Srwatson/* Prototypes for top internals */ 7192922Simpvoid quit(int); 7224143Sjoerg 7324143Sjoerg/* get_process_info passes back a handle. This is what it looks like: */ 7424143Sjoerg 7524143Sjoergstruct handle 7624143Sjoerg{ 7724143Sjoerg struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 7824143Sjoerg int remaining; /* number of pointers remaining */ 7924143Sjoerg}; 8024143Sjoerg 8124143Sjoerg/* declarations for load_avg */ 8224143Sjoerg#include "loadavg.h" 8324143Sjoerg 8424143Sjoerg/* define what weighted cpu is. */ 8569896Smckusick#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \ 8669896Smckusick ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu)))) 8724143Sjoerg 8824143Sjoerg/* what we consider to be process size: */ 8969896Smckusick#define PROCSIZE(pp) ((pp)->ki_size / 1024) 9024143Sjoerg 9124143Sjoerg/* definitions for indices in the nlist array */ 9224143Sjoerg 9324143Sjoerg/* 9424143Sjoerg * These definitions control the format of the per-process area 9524143Sjoerg */ 9624143Sjoerg 9727340Speterstatic char smp_header[] = 9872647Smarkm " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND"; 9924143Sjoerg 10027340Speter#define smp_Proc_format \ 10172647Smarkm "%5d %-*.*s %3d %4d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s" 10224143Sjoerg 10327340Speterstatic char up_header[] = 10472647Smarkm " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND"; 10524143Sjoerg 10627340Speter#define up_Proc_format \ 10772647Smarkm "%5d %-*.*s %3d %4d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s" 10824143Sjoerg 10924143Sjoerg 11024143Sjoerg 11124143Sjoerg/* process state names for the "STATE" column of the display */ 11224143Sjoerg/* the extra nulls in the string "run" are for adding a slash and 11324143Sjoerg the processor number when needed */ 11424143Sjoerg 11524143Sjoergchar *state_abbrev[] = 11624143Sjoerg{ 117104388Sjhb "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK" 11824143Sjoerg}; 11924143Sjoerg 12024143Sjoerg 12124143Sjoergstatic kvm_t *kd; 12224143Sjoerg 12324143Sjoerg/* values that we stash away in _init and use in later routines */ 12424143Sjoerg 12524143Sjoergstatic double logcpu; 12624143Sjoerg 12724143Sjoerg/* these are retrieved from the kernel in _init */ 12824143Sjoerg 12924143Sjoergstatic load_avg ccpu; 13024143Sjoerg 13172951Srwatson/* these are used in the get_ functions */ 13224143Sjoerg 13372951Srwatsonstatic int lastpid; 13424143Sjoerg 13524143Sjoerg/* these are for calculating cpu state percentages */ 13624143Sjoerg 13724143Sjoergstatic long cp_time[CPUSTATES]; 13824143Sjoergstatic long cp_old[CPUSTATES]; 13924143Sjoergstatic long cp_diff[CPUSTATES]; 14024143Sjoerg 14124143Sjoerg/* these are for detailing the process states */ 14224143Sjoerg 14365557Sjasoneint process_states[8]; 14424143Sjoergchar *procstatenames[] = { 14524143Sjoerg "", " starting, ", " running, ", " sleeping, ", " stopped, ", 146104388Sjhb " zombie, ", " waiting, ", " lock, ", 14724143Sjoerg NULL 14824143Sjoerg}; 14924143Sjoerg 15024143Sjoerg/* these are for detailing the cpu states */ 15124143Sjoerg 15224143Sjoergint cpu_states[CPUSTATES]; 15324143Sjoergchar *cpustatenames[] = { 15424143Sjoerg "user", "nice", "system", "interrupt", "idle", NULL 15524143Sjoerg}; 15624143Sjoerg 15724143Sjoerg/* these are for detailing the memory statistics */ 15824143Sjoerg 15924143Sjoergint memory_stats[7]; 16024143Sjoergchar *memorynames[] = { 16124143Sjoerg "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free", 16224143Sjoerg NULL 16324143Sjoerg}; 16424143Sjoerg 16524143Sjoergint swap_stats[7]; 16624143Sjoergchar *swapnames[] = { 16724143Sjoerg/* 0 1 2 3 4 5 */ 16824143Sjoerg "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 16924143Sjoerg NULL 17024143Sjoerg}; 17124143Sjoerg 17224143Sjoerg 17324143Sjoerg/* these are for keeping track of the proc array */ 17424143Sjoerg 17524143Sjoergstatic int nproc; 17624143Sjoergstatic int onproc = -1; 17724143Sjoergstatic int pref_len; 17824143Sjoergstatic struct kinfo_proc *pbase; 17924143Sjoergstatic struct kinfo_proc **pref; 18024143Sjoerg 18124143Sjoerg/* these are for getting the memory statistics */ 18224143Sjoerg 18324143Sjoergstatic int pageshift; /* log base 2 of the pagesize */ 18424143Sjoerg 18524143Sjoerg/* define pagetok in terms of pageshift */ 18624143Sjoerg 18724143Sjoerg#define pagetok(size) ((size) << pageshift) 18824143Sjoerg 18924143Sjoerg/* useful externals */ 19024143Sjoerglong percentages(); 19124143Sjoerg 19238278Swosch#ifdef ORDER 19338278Swosch/* sorting orders. first is default */ 19438278Swoschchar *ordernames[] = { 19538278Swosch "cpu", "size", "res", "time", "pri", NULL 19638278Swosch}; 19738278Swosch#endif 19838278Swosch 19924143Sjoergint 20024143Sjoergmachine_init(statics) 20124143Sjoerg 20224143Sjoergstruct statics *statics; 20324143Sjoerg 20424143Sjoerg{ 20524143Sjoerg register int pagesize; 20673164Sgallatin size_t modelen; 20727390Speter struct passwd *pw; 20824143Sjoerg 20927340Speter modelen = sizeof(smpmode); 21028819Speter if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 && 21176079Sjhb sysctlbyname("kern.smp.active", &smpmode, &modelen, NULL, 0) < 0) || 21227340Speter modelen != sizeof(smpmode)) 21327340Speter smpmode = 0; 21427340Speter 21527390Speter while ((pw = getpwent()) != NULL) { 21627390Speter if (strlen(pw->pw_name) > namelength) 21727390Speter namelength = strlen(pw->pw_name); 21827390Speter } 21927390Speter if (namelength < 8) 22027390Speter namelength = 8; 22174142Swill if (smpmode && namelength > 13) 22274142Swill namelength = 13; 22374142Swill else if (namelength > 15) 22453281Sdavidn namelength = 15; 22527390Speter 22672951Srwatson if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) 22724143Sjoerg return -1; 22824143Sjoerg 22972951Srwatson GETSYSCTL("kern.ccpu", ccpu); 23024143Sjoerg 23124143Sjoerg /* this is used in calculating WCPU -- calculate it ahead of time */ 23224143Sjoerg logcpu = log(loaddouble(ccpu)); 23324143Sjoerg 23424143Sjoerg pbase = NULL; 23524143Sjoerg pref = NULL; 23624143Sjoerg nproc = 0; 23724143Sjoerg onproc = -1; 23824143Sjoerg /* get the page size with "getpagesize" and calculate pageshift from it */ 23924143Sjoerg pagesize = getpagesize(); 24024143Sjoerg pageshift = 0; 24124143Sjoerg while (pagesize > 1) 24224143Sjoerg { 24324143Sjoerg pageshift++; 24424143Sjoerg pagesize >>= 1; 24524143Sjoerg } 24624143Sjoerg 24724143Sjoerg /* we only need the amount of log(2)1024 for our conversion */ 24824143Sjoerg pageshift -= LOG1024; 24924143Sjoerg 25024143Sjoerg /* fill in the statics information */ 25124143Sjoerg statics->procstate_names = procstatenames; 25224143Sjoerg statics->cpustate_names = cpustatenames; 25324143Sjoerg statics->memory_names = memorynames; 25424143Sjoerg statics->swap_names = swapnames; 25538278Swosch#ifdef ORDER 25638278Swosch statics->order_names = ordernames; 25738278Swosch#endif 25824143Sjoerg 25924143Sjoerg /* all done! */ 26024143Sjoerg return(0); 26124143Sjoerg} 26224143Sjoerg 26324143Sjoergchar *format_header(uname_field) 26424143Sjoerg 26524143Sjoergregister char *uname_field; 26624143Sjoerg 26724143Sjoerg{ 26827390Speter static char Header[128]; 26924143Sjoerg 27027390Speter snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header, 27127390Speter namelength, namelength, uname_field); 27227340Speter 27369375Sjhb cmdlengthdelta = strlen(Header) - 7; 27424143Sjoerg 27527390Speter return Header; 27624143Sjoerg} 27724143Sjoerg 27824143Sjoergstatic int swappgsin = -1; 27924143Sjoergstatic int swappgsout = -1; 28024143Sjoergextern struct timeval timeout; 28124143Sjoerg 28224143Sjoergvoid 28324143Sjoergget_system_info(si) 28424143Sjoerg 28524143Sjoergstruct system_info *si; 28624143Sjoerg 28724143Sjoerg{ 28824143Sjoerg long total; 28973164Sgallatin struct loadavg sysload; 29042447Sobrien int mib[2]; 29142447Sobrien struct timeval boottime; 29242447Sobrien size_t bt_size; 29324143Sjoerg 29424143Sjoerg /* get the cp_time array */ 29572951Srwatson GETSYSCTL("kern.cp_time", cp_time); 29673164Sgallatin GETSYSCTL("vm.loadavg", sysload); 29772951Srwatson GETSYSCTL("kern.lastpid", lastpid); 29824143Sjoerg 29924143Sjoerg /* convert load averages to doubles */ 30024143Sjoerg { 30124143Sjoerg register int i; 30224143Sjoerg register double *infoloadp; 30324143Sjoerg 30424143Sjoerg infoloadp = si->load_avg; 30524143Sjoerg for (i = 0; i < 3; i++) 30624143Sjoerg { 30724143Sjoerg#ifdef notyet 30824143Sjoerg *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale; 30924143Sjoerg#endif 31073164Sgallatin *infoloadp++ = loaddouble(sysload.ldavg[i]); 31124143Sjoerg } 31224143Sjoerg } 31324143Sjoerg 31424143Sjoerg /* convert cp_time counts to percentages */ 31524143Sjoerg total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 31624143Sjoerg 31724143Sjoerg /* sum memory & swap statistics */ 31824143Sjoerg { 31924143Sjoerg static unsigned int swap_delay = 0; 32024143Sjoerg static int swapavail = 0; 32124143Sjoerg static int swapfree = 0; 32224143Sjoerg static int bufspace = 0; 32372951Srwatson static int nspgsin, nspgsout; 32424143Sjoerg 32572951Srwatson GETSYSCTL("vfs.bufspace", bufspace); 32672951Srwatson GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]); 32772951Srwatson GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]); 32872951Srwatson GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]); 32972951Srwatson GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]); 33072951Srwatson GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]); 33172951Srwatson GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin); 33272951Srwatson GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout); 33324143Sjoerg /* convert memory stats to Kbytes */ 33474524Stmm memory_stats[0] = pagetok(memory_stats[0]); 33574524Stmm memory_stats[1] = pagetok(memory_stats[1]); 33674524Stmm memory_stats[2] = pagetok(memory_stats[2]); 33774524Stmm memory_stats[3] = pagetok(memory_stats[3]); 33824143Sjoerg memory_stats[4] = bufspace / 1024; 33974524Stmm memory_stats[5] = pagetok(memory_stats[5]); 34024143Sjoerg memory_stats[6] = -1; 34124143Sjoerg 34224143Sjoerg /* first interval */ 34324143Sjoerg if (swappgsin < 0) { 34424143Sjoerg swap_stats[4] = 0; 34524143Sjoerg swap_stats[5] = 0; 34624143Sjoerg } 34724143Sjoerg 34824143Sjoerg /* compute differences between old and new swap statistic */ 34924143Sjoerg else { 35072951Srwatson swap_stats[4] = pagetok(((nspgsin - swappgsin))); 35172951Srwatson swap_stats[5] = pagetok(((nspgsout - swappgsout))); 35224143Sjoerg } 35324143Sjoerg 35472951Srwatson swappgsin = nspgsin; 35572951Srwatson swappgsout = nspgsout; 35624143Sjoerg 35724143Sjoerg /* call CPU heavy swapmode() only for changes */ 35824143Sjoerg if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 35924143Sjoerg swap_stats[3] = swapmode(&swapavail, &swapfree); 36024143Sjoerg swap_stats[0] = swapavail; 36124143Sjoerg swap_stats[1] = swapavail - swapfree; 36224143Sjoerg swap_stats[2] = swapfree; 36324143Sjoerg } 36424143Sjoerg swap_delay = 1; 36524143Sjoerg swap_stats[6] = -1; 36624143Sjoerg } 36724143Sjoerg 36824143Sjoerg /* set arrays and strings */ 36924143Sjoerg si->cpustates = cpu_states; 37024143Sjoerg si->memory = memory_stats; 37124143Sjoerg si->swap = swap_stats; 37224143Sjoerg 37324143Sjoerg 37424143Sjoerg if(lastpid > 0) { 37524143Sjoerg si->last_pid = lastpid; 37624143Sjoerg } else { 37724143Sjoerg si->last_pid = -1; 37824143Sjoerg } 37942447Sobrien 38042447Sobrien /* 38142447Sobrien * Print how long system has been up. 38242447Sobrien * (Found by looking getting "boottime" from the kernel) 38342447Sobrien */ 38442447Sobrien mib[0] = CTL_KERN; 38542447Sobrien mib[1] = KERN_BOOTTIME; 38642447Sobrien bt_size = sizeof(boottime); 38742447Sobrien if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 38842447Sobrien boottime.tv_sec != 0) { 38942447Sobrien si->boottime = boottime; 39042447Sobrien } else { 39142447Sobrien si->boottime.tv_sec = -1; 39242447Sobrien } 39324143Sjoerg} 39424143Sjoerg 39524143Sjoergstatic struct handle handle; 39624143Sjoerg 39724143Sjoergcaddr_t get_process_info(si, sel, compare) 39824143Sjoerg 39924143Sjoergstruct system_info *si; 40024143Sjoergstruct process_select *sel; 40124143Sjoergint (*compare)(); 40224143Sjoerg 40324143Sjoerg{ 40424143Sjoerg register int i; 40524143Sjoerg register int total_procs; 40624143Sjoerg register int active_procs; 40724143Sjoerg register struct kinfo_proc **prefp; 40824143Sjoerg register struct kinfo_proc *pp; 40924143Sjoerg 41024143Sjoerg /* these are copied out of sel for speed */ 41124143Sjoerg int show_idle; 41238090Sdes int show_self; 41324143Sjoerg int show_system; 41424143Sjoerg int show_uid; 41524143Sjoerg int show_command; 41624143Sjoerg 41724143Sjoerg 41824143Sjoerg pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 41924143Sjoerg if (nproc > onproc) 42024143Sjoerg pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *) 42124143Sjoerg * (onproc = nproc)); 42224143Sjoerg if (pref == NULL || pbase == NULL) { 42324143Sjoerg (void) fprintf(stderr, "top: Out of memory.\n"); 42424143Sjoerg quit(23); 42524143Sjoerg } 42624143Sjoerg /* get a pointer to the states summary array */ 42724143Sjoerg si->procstates = process_states; 42824143Sjoerg 42924143Sjoerg /* set up flags which define what we are going to select */ 43024143Sjoerg show_idle = sel->idle; 43138090Sdes show_self = sel->self; 43224143Sjoerg show_system = sel->system; 43324143Sjoerg show_uid = sel->uid != -1; 43424143Sjoerg show_command = sel->command != NULL; 43524143Sjoerg 43624143Sjoerg /* count up process states and get pointers to interesting procs */ 43724143Sjoerg total_procs = 0; 43824143Sjoerg active_procs = 0; 43924143Sjoerg memset((char *)process_states, 0, sizeof(process_states)); 44024143Sjoerg prefp = pref; 44124143Sjoerg for (pp = pbase, i = 0; i < nproc; pp++, i++) 44224143Sjoerg { 44324143Sjoerg /* 44424143Sjoerg * Place pointers to each valid proc structure in pref[]. 44524143Sjoerg * Process slots that are actually in use have a non-zero 44624143Sjoerg * status field. Processes with P_SYSTEM set are system 44724143Sjoerg * processes---these get ignored unless show_sysprocs is set. 44824143Sjoerg */ 44969896Smckusick if (pp->ki_stat != 0 && 45069896Smckusick (show_self != pp->ki_pid) && 45169896Smckusick (show_system || ((pp->ki_flag & P_SYSTEM) == 0))) 45224143Sjoerg { 45324143Sjoerg total_procs++; 45469896Smckusick process_states[(unsigned char) pp->ki_stat]++; 45569896Smckusick if ((pp->ki_stat != SZOMB) && 45669896Smckusick (show_idle || (pp->ki_pctcpu != 0) || 45769896Smckusick (pp->ki_stat == SRUN)) && 45869896Smckusick (!show_uid || pp->ki_ruid == (uid_t)sel->uid)) 45924143Sjoerg { 46024143Sjoerg *prefp++ = pp; 46124143Sjoerg active_procs++; 46224143Sjoerg } 46324143Sjoerg } 46424143Sjoerg } 46524143Sjoerg 46624143Sjoerg /* if requested, sort the "interesting" processes */ 46724143Sjoerg if (compare != NULL) 46824143Sjoerg { 46924143Sjoerg qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare); 47024143Sjoerg } 47124143Sjoerg 47224143Sjoerg /* remember active and total counts */ 47324143Sjoerg si->p_total = total_procs; 47424143Sjoerg si->p_active = pref_len = active_procs; 47524143Sjoerg 47624143Sjoerg /* pass back a handle */ 47724143Sjoerg handle.next_proc = pref; 47824143Sjoerg handle.remaining = active_procs; 47924143Sjoerg return((caddr_t)&handle); 48024143Sjoerg} 48124143Sjoerg 48224143Sjoergchar fmt[128]; /* static area where result is built */ 48324143Sjoerg 48424143Sjoergchar *format_next_process(handle, get_userid) 48524143Sjoerg 48624143Sjoergcaddr_t handle; 48724143Sjoergchar *(*get_userid)(); 48824143Sjoerg 48924143Sjoerg{ 49024143Sjoerg register struct kinfo_proc *pp; 49124143Sjoerg register long cputime; 49224143Sjoerg register double pct; 49324143Sjoerg struct handle *hp; 49424143Sjoerg char status[16]; 49543720Sfenner int state; 49624143Sjoerg 49724143Sjoerg /* find and remember the next proc structure */ 49824143Sjoerg hp = (struct handle *)handle; 49924143Sjoerg pp = *(hp->next_proc++); 50024143Sjoerg hp->remaining--; 50124143Sjoerg 50253255Sbde /* get the process's command name */ 50371589Sjhb if ((pp->ki_sflag & PS_INMEM) == 0) { 50424143Sjoerg /* 50524143Sjoerg * Print swapped processes as <pname> 50624143Sjoerg */ 50769896Smckusick char *comm = pp->ki_comm; 50869896Smckusick#define COMSIZ sizeof(pp->ki_comm) 50924143Sjoerg char buf[COMSIZ]; 51024143Sjoerg (void) strncpy(buf, comm, COMSIZ); 51124143Sjoerg comm[0] = '<'; 51224143Sjoerg (void) strncpy(&comm[1], buf, COMSIZ - 2); 51324143Sjoerg comm[COMSIZ - 2] = '\0'; 51424143Sjoerg (void) strncat(comm, ">", COMSIZ - 1); 51524143Sjoerg comm[COMSIZ - 1] = '\0'; 51624143Sjoerg } 51724143Sjoerg 51853255Sbde /* 51953255Sbde * Convert the process's runtime from microseconds to seconds. This 52053255Sbde * time includes the interrupt time although that is not wanted here. 52153255Sbde * ps(1) is similarly sloppy. 52253255Sbde */ 52369896Smckusick cputime = (pp->ki_runtime + 500000) / 1000000; 52424143Sjoerg 52524143Sjoerg /* calculate the base for cpu percentages */ 52669896Smckusick pct = pctdouble(pp->ki_pctcpu); 52724143Sjoerg 52824143Sjoerg /* generate "STATE" field */ 52969896Smckusick switch (state = pp->ki_stat) { 53024143Sjoerg case SRUN: 53169896Smckusick if (smpmode && pp->ki_oncpu != 0xff) 53269896Smckusick sprintf(status, "CPU%d", pp->ki_oncpu); 53324143Sjoerg else 53424143Sjoerg strcpy(status, "RUN"); 53524143Sjoerg break; 536104388Sjhb case SLOCK: 537104388Sjhb if (pp->ki_kiflag & KI_LOCKBLOCK) { 538104388Sjhb sprintf(status, "*%.6s", pp->ki_lockname); 53969370Sjhb break; 54069370Sjhb } 54169370Sjhb /* fall through */ 54224143Sjoerg case SSLEEP: 54369896Smckusick if (pp->ki_wmesg != NULL) { 54469896Smckusick sprintf(status, "%.6s", pp->ki_wmesg); 54524143Sjoerg break; 54624143Sjoerg } 547102412Scharnier /* FALLTHROUGH */ 54824143Sjoerg default: 54943720Sfenner 55043720Sfenner if (state >= 0 && 55143720Sfenner state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 55243720Sfenner sprintf(status, "%.6s", state_abbrev[(unsigned char) state]); 55343720Sfenner else 55443720Sfenner sprintf(status, "?%5d", state); 55524143Sjoerg break; 55624143Sjoerg } 55724143Sjoerg 55824143Sjoerg /* format this entry */ 55924143Sjoerg sprintf(fmt, 56027340Speter smpmode ? smp_Proc_format : up_Proc_format, 56169896Smckusick pp->ki_pid, 56227390Speter namelength, namelength, 56369896Smckusick (*get_userid)(pp->ki_ruid), 56472377Sjake pp->ki_pri.pri_level - PZERO, 56529904Speter 56629904Speter /* 56729904Speter * normal time -> nice value -20 - +20 56829904Speter * real time 0 - 31 -> nice value -52 - -21 56929904Speter * idle time 0 - 31 -> nice value +21 - +52 57029904Speter */ 57172377Sjake (pp->ki_pri.pri_class == PRI_TIMESHARE ? 57269896Smckusick pp->ki_nice - NZERO : 57372377Sjake (PRI_IS_REALTIME(pp->ki_pri.pri_class) ? 57472377Sjake (PRIO_MIN - 1 - (PRI_MAX_REALTIME - pp->ki_pri.pri_level)) : 57572377Sjake (PRIO_MAX + 1 + pp->ki_pri.pri_level - PRI_MIN_IDLE))), 57637100Sdt format_k2(PROCSIZE(pp)), 57769896Smckusick format_k2(pagetok(pp->ki_rssize)), 57824143Sjoerg status, 57969896Smckusick smpmode ? pp->ki_lastcpu : 0, 58024143Sjoerg format_time(cputime), 58141325Sdfr 100.0 * weighted_cpu(pct, pp), 58241325Sdfr 100.0 * pct, 58369375Sjhb screen_width > cmdlengthdelta ? 58469375Sjhb screen_width - cmdlengthdelta : 58569375Sjhb 0, 58669896Smckusick printable(pp->ki_comm)); 58724143Sjoerg 58824143Sjoerg /* return the result */ 58924143Sjoerg return(fmt); 59024143Sjoerg} 59124143Sjoerg 59272951Srwatsonstatic void getsysctl (name, ptr, len) 59324143Sjoerg 59472951Srwatsonchar *name; 59572951Srwatsonvoid *ptr; 59673164Sgallatinsize_t len; 59724143Sjoerg 59824143Sjoerg{ 59973164Sgallatin size_t nlen = len; 60072951Srwatson if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 60172951Srwatson fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 60272951Srwatson strerror(errno)); 60372951Srwatson quit(23); 60424143Sjoerg } 60572951Srwatson if (nlen != len) { 60677553Stmm fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", name, 60777553Stmm (unsigned long)len, (unsigned long)nlen); 60824143Sjoerg quit(23); 60924143Sjoerg } 61024143Sjoerg} 61172951Srwatson 61238278Swosch/* comparison routines for qsort */ 61324143Sjoerg 61424143Sjoerg/* 61524143Sjoerg * proc_compare - comparison function for "qsort" 61624143Sjoerg * Compares the resource consumption of two processes using five 61724143Sjoerg * distinct keys. The keys (in descending order of importance) are: 61824143Sjoerg * percent cpu, cpu ticks, state, resident set size, total virtual 61924143Sjoerg * memory usage. The process states are ordered as follows (from least 62024143Sjoerg * to most important): WAIT, zombie, sleep, stop, start, run. The 62124143Sjoerg * array declaration below maps a process state index into a number 62224143Sjoerg * that reflects this ordering. 62324143Sjoerg */ 62424143Sjoerg 62524143Sjoergstatic unsigned char sorted_state[] = 62624143Sjoerg{ 62724143Sjoerg 0, /* not used */ 62824143Sjoerg 3, /* sleep */ 62924143Sjoerg 1, /* ABANDONED (WAIT) */ 63024143Sjoerg 6, /* run */ 63124143Sjoerg 5, /* start */ 63224143Sjoerg 2, /* zombie */ 63324143Sjoerg 4 /* stop */ 63424143Sjoerg}; 63524143Sjoerg 63638278Swosch 63738278Swosch#define ORDERKEY_PCTCPU \ 63869896Smckusick if (lresult = (long) p2->ki_pctcpu - (long) p1->ki_pctcpu, \ 63938278Swosch (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) 64038278Swosch 64138278Swosch#define ORDERKEY_CPTICKS \ 64269896Smckusick if ((result = p2->ki_runtime > p1->ki_runtime ? 1 : \ 64369896Smckusick p2->ki_runtime < p1->ki_runtime ? -1 : 0) == 0) 64438278Swosch 64538278Swosch#define ORDERKEY_STATE \ 64669896Smckusick if ((result = sorted_state[(unsigned char) p2->ki_stat] - \ 64769896Smckusick sorted_state[(unsigned char) p1->ki_stat]) == 0) 64838278Swosch 64938278Swosch#define ORDERKEY_PRIO \ 65072377Sjake if ((result = p2->ki_pri.pri_level - p1->ki_pri.pri_level) == 0) 65138278Swosch 65238278Swosch#define ORDERKEY_RSSIZE \ 65369896Smckusick if ((result = p2->ki_rssize - p1->ki_rssize) == 0) 65438278Swosch 65538278Swosch#define ORDERKEY_MEM \ 65638278Swosch if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) 65738278Swosch 65838278Swosch/* compare_cpu - the comparison function for sorting by cpu percentage */ 65938278Swosch 66024143Sjoergint 66138278Swosch#ifdef ORDER 66238278Swoschcompare_cpu(pp1, pp2) 66338278Swosch#else 66424143Sjoergproc_compare(pp1, pp2) 66538278Swosch#endif 66624143Sjoerg 66724143Sjoergstruct proc **pp1; 66824143Sjoergstruct proc **pp2; 66924143Sjoerg 67024143Sjoerg{ 67124143Sjoerg register struct kinfo_proc *p1; 67224143Sjoerg register struct kinfo_proc *p2; 67324143Sjoerg register int result; 67424143Sjoerg register pctcpu lresult; 67524143Sjoerg 67624143Sjoerg /* remove one level of indirection */ 67724143Sjoerg p1 = *(struct kinfo_proc **) pp1; 67824143Sjoerg p2 = *(struct kinfo_proc **) pp2; 67924143Sjoerg 68038278Swosch ORDERKEY_PCTCPU 68138278Swosch ORDERKEY_CPTICKS 68238278Swosch ORDERKEY_STATE 68338278Swosch ORDERKEY_PRIO 68438278Swosch ORDERKEY_RSSIZE 68538278Swosch ORDERKEY_MEM 68638278Swosch ; 68724143Sjoerg 68824143Sjoerg return(result); 68924143Sjoerg} 69024143Sjoerg 69138278Swosch#ifdef ORDER 69238278Swosch/* compare routines */ 69338278Swoschint compare_size(), compare_res(), compare_time(), compare_prio(); 69424143Sjoerg 69538278Swoschint (*proc_compares[])() = { 69638278Swosch compare_cpu, 69738278Swosch compare_size, 69838278Swosch compare_res, 69938278Swosch compare_time, 70038278Swosch compare_prio, 70138278Swosch NULL 70238278Swosch}; 70338278Swosch 70438278Swosch/* compare_size - the comparison function for sorting by total memory usage */ 70538278Swosch 70638278Swoschint 70738278Swoschcompare_size(pp1, pp2) 70838278Swosch 70938278Swoschstruct proc **pp1; 71038278Swoschstruct proc **pp2; 71138278Swosch 71238278Swosch{ 71338278Swosch register struct kinfo_proc *p1; 71438278Swosch register struct kinfo_proc *p2; 71538278Swosch register int result; 71638278Swosch register pctcpu lresult; 71738278Swosch 71838278Swosch /* remove one level of indirection */ 71938278Swosch p1 = *(struct kinfo_proc **) pp1; 72038278Swosch p2 = *(struct kinfo_proc **) pp2; 72138278Swosch 72238278Swosch ORDERKEY_MEM 72338278Swosch ORDERKEY_RSSIZE 72438278Swosch ORDERKEY_PCTCPU 72538278Swosch ORDERKEY_CPTICKS 72638278Swosch ORDERKEY_STATE 72738278Swosch ORDERKEY_PRIO 72838278Swosch ; 72938278Swosch 73038278Swosch return(result); 73138278Swosch} 73238278Swosch 73338278Swosch/* compare_res - the comparison function for sorting by resident set size */ 73438278Swosch 73538278Swoschint 73638278Swoschcompare_res(pp1, pp2) 73738278Swosch 73838278Swoschstruct proc **pp1; 73938278Swoschstruct proc **pp2; 74038278Swosch 74138278Swosch{ 74238278Swosch register struct kinfo_proc *p1; 74338278Swosch register struct kinfo_proc *p2; 74438278Swosch register int result; 74538278Swosch register pctcpu lresult; 74638278Swosch 74738278Swosch /* remove one level of indirection */ 74838278Swosch p1 = *(struct kinfo_proc **) pp1; 74938278Swosch p2 = *(struct kinfo_proc **) pp2; 75038278Swosch 75138278Swosch ORDERKEY_RSSIZE 75238278Swosch ORDERKEY_MEM 75338278Swosch ORDERKEY_PCTCPU 75438278Swosch ORDERKEY_CPTICKS 75538278Swosch ORDERKEY_STATE 75638278Swosch ORDERKEY_PRIO 75738278Swosch ; 75838278Swosch 75938278Swosch return(result); 76038278Swosch} 76138278Swosch 76238278Swosch/* compare_time - the comparison function for sorting by total cpu time */ 76338278Swosch 76438278Swoschint 76538278Swoschcompare_time(pp1, pp2) 76638278Swosch 76738278Swoschstruct proc **pp1; 76838278Swoschstruct proc **pp2; 76938278Swosch 77038278Swosch{ 77138278Swosch register struct kinfo_proc *p1; 77238278Swosch register struct kinfo_proc *p2; 77338278Swosch register int result; 77438278Swosch register pctcpu lresult; 77538278Swosch 77638278Swosch /* remove one level of indirection */ 77738278Swosch p1 = *(struct kinfo_proc **) pp1; 77838278Swosch p2 = *(struct kinfo_proc **) pp2; 77938278Swosch 78038278Swosch ORDERKEY_CPTICKS 78138278Swosch ORDERKEY_PCTCPU 78238278Swosch ORDERKEY_STATE 78338278Swosch ORDERKEY_PRIO 78438278Swosch ORDERKEY_RSSIZE 78538278Swosch ORDERKEY_MEM 78638278Swosch ; 78738278Swosch 78838278Swosch return(result); 78938278Swosch } 79038278Swosch 79138278Swosch/* compare_prio - the comparison function for sorting by cpu percentage */ 79238278Swosch 79338278Swoschint 79438278Swoschcompare_prio(pp1, pp2) 79538278Swosch 79638278Swoschstruct proc **pp1; 79738278Swoschstruct proc **pp2; 79838278Swosch 79938278Swosch{ 80038278Swosch register struct kinfo_proc *p1; 80138278Swosch register struct kinfo_proc *p2; 80238278Swosch register int result; 80338278Swosch register pctcpu lresult; 80438278Swosch 80538278Swosch /* remove one level of indirection */ 80638278Swosch p1 = *(struct kinfo_proc **) pp1; 80738278Swosch p2 = *(struct kinfo_proc **) pp2; 80838278Swosch 80938278Swosch ORDERKEY_PRIO 81038278Swosch ORDERKEY_CPTICKS 81138278Swosch ORDERKEY_PCTCPU 81238278Swosch ORDERKEY_STATE 81338278Swosch ORDERKEY_RSSIZE 81438278Swosch ORDERKEY_MEM 81538278Swosch ; 81638278Swosch 81738278Swosch return(result); 81838278Swosch} 81938278Swosch#endif 82038278Swosch 82124143Sjoerg/* 82224143Sjoerg * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 82324143Sjoerg * the process does not exist. 82424143Sjoerg * It is EXTREMLY IMPORTANT that this function work correctly. 82524143Sjoerg * If top runs setuid root (as in SVR4), then this function 82624143Sjoerg * is the only thing that stands in the way of a serious 82724143Sjoerg * security problem. It validates requests for the "kill" 82824143Sjoerg * and "renice" commands. 82924143Sjoerg */ 83024143Sjoerg 83124143Sjoergint proc_owner(pid) 83224143Sjoerg 83324143Sjoergint pid; 83424143Sjoerg 83524143Sjoerg{ 83624143Sjoerg register int cnt; 83724143Sjoerg register struct kinfo_proc **prefp; 83824143Sjoerg register struct kinfo_proc *pp; 83924143Sjoerg 84024143Sjoerg prefp = pref; 84124143Sjoerg cnt = pref_len; 84224143Sjoerg while (--cnt >= 0) 84324143Sjoerg { 84424143Sjoerg pp = *prefp++; 84569896Smckusick if (pp->ki_pid == (pid_t)pid) 84624143Sjoerg { 84769896Smckusick return((int)pp->ki_ruid); 84824143Sjoerg } 84924143Sjoerg } 85024143Sjoerg return(-1); 85124143Sjoerg} 85224143Sjoerg 85324143Sjoergint 85424143Sjoergswapmode(retavail, retfree) 85524143Sjoerg int *retavail; 85624143Sjoerg int *retfree; 85724143Sjoerg{ 85843053Sdillon int n; 85943053Sdillon int pagesize = getpagesize(); 86043053Sdillon struct kvm_swap swapary[1]; 86124143Sjoerg 86243053Sdillon *retavail = 0; 86343053Sdillon *retfree = 0; 86424143Sjoerg 86543053Sdillon#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 86624143Sjoerg 86743053Sdillon n = kvm_getswapinfo(kd, swapary, 1, 0); 86843697Sdillon if (n < 0 || swapary[0].ksw_total == 0) 86924143Sjoerg return(0); 87024143Sjoerg 87143053Sdillon *retavail = CONVERT(swapary[0].ksw_total); 87243053Sdillon *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 87324143Sjoerg 87443053Sdillon n = (int)((double)swapary[0].ksw_used * 100.0 / 87543053Sdillon (double)swapary[0].ksw_total); 87643053Sdillon return(n); 87743053Sdillon} 87824143Sjoerg 879