machine.c revision 73164
1296853Sdes/* 257429Smarkm * top - a top users display for Unix 357429Smarkm * 457429Smarkm * SYNOPSIS: For FreeBSD-2.x and later 557429Smarkm * 657429Smarkm * DESCRIPTION: 765668Skris * Originally written for BSD4.4 system by Christos Zoulas. 865668Skris * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider 965668Skris * Order support hacked in from top-3.5beta6/machine/m_aix41.c 1065668Skris * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/) 1165668Skris * 1265668Skris * This is the machine-dependent module for FreeBSD 2.2 1365668Skris * Works for: 1465668Skris * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x 1592559Sdes * 1665668Skris * LIBS: -lkvm 1765668Skris * 1865668Skris * AUTHOR: Christos Zoulas <christos@ee.cornell.edu> 1965668Skris * Steven Wallace <swallace@freebsd.org> 2065668Skris * Wolfram Schneider <wosch@FreeBSD.org> 2165668Skris * Thomas Moestl <tmoestl@gmx.net> 2265668Skris * 2365668Skris * $FreeBSD: head/usr.bin/top/machine.c 73164 2001-02-27 17:11:19Z gallatin $ 2465668Skris */ 2565668Skris 2665668Skris 2765668Skris#include <sys/time.h> 2865668Skris#include <sys/types.h> 2965668Skris#include <sys/signal.h> 3065668Skris#include <sys/param.h> 3165668Skris 3265668Skris#include "os.h" 3365668Skris#include <stdio.h> 3465668Skris#include <nlist.h> 3565668Skris#include <math.h> 3657429Smarkm#include <kvm.h> 3757429Smarkm#include <pwd.h> 3857429Smarkm#include <sys/errno.h> 3957429Smarkm#include <sys/sysctl.h> 40162856Sdes#include <sys/dkstat.h> 41162856Sdes#include <sys/file.h> 42162856Sdes#include <sys/time.h> 4365668Skris#include <sys/proc.h> 44181111Sdes#include <sys/user.h> 4560573Skris#include <sys/vmmeter.h> 46295367Sdes#include <sys/resource.h> 47162856Sdes#include <sys/rtprio.h> 48162856Sdes 49162856Sdes/* Swap */ 50162856Sdes#include <stdlib.h> 51162856Sdes#include <sys/conf.h> 52162856Sdes 53162856Sdes#include <unistd.h> 54295367Sdes#include <osreldate.h> /* for changes in kernel structures */ 55162856Sdes 56162856Sdes#include "top.h" 5776262Sgreen#include "machine.h" 5857429Smarkm#include "screen.h" 5976262Sgreen#include "utils.h" 60295367Sdes 61295367Sdesstatic void getsysctl __P((char *, void *, size_t)); 6257429Smarkm 6360573Skris#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) 6476262Sgreen 6598684Sdesextern char* printable __P((char *)); 66295367Sdesint swapmode __P((int *retavail, int *retfree)); 67295367Sdesstatic int smpmode; 6857429Smarkmstatic int namelength; 6992559Sdesstatic int cmdlengthdelta; 7092559Sdes 7192559Sdes/* Prototypes for top internals */ 7292559Sdesvoid quit __P((int)); 7392559Sdes 74295367Sdes/* get_process_info passes back a handle. This is what it looks like: */ 7592559Sdes 7692559Sdesstruct handle 77221420Sdes{ 78221420Sdes struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 79221420Sdes int remaining; /* number of pointers remaining */ 80295367Sdes}; 81262566Sdes 82295367Sdes/* declarations for load_avg */ 8398684Sdes#include "loadavg.h" 84295367Sdes 8592559Sdes/* define what weighted cpu is. */ 8692559Sdes#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \ 8792559Sdes ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu)))) 88295367Sdes 89295367Sdes/* what we consider to be process size: */ 9098684Sdes#define PROCSIZE(pp) ((pp)->ki_size / 1024) 9198684Sdes 9292559Sdes/* definitions for indices in the nlist array */ 93113911Sdes 94113911Sdes/* 95113911Sdes * These definitions control the format of the per-process area 96296853Sdes */ 9776262Sgreen 9892559Sdesstatic char smp_header[] = 9976262Sgreen " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND"; 10076262Sgreen 10176262Sgreen#define smp_Proc_format \ 102264377Sdes "%5d %-*.*s %3d %4d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s" 103255767Sdes 10476262Sgreenstatic char up_header[] = 10576262Sgreen " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND"; 10676262Sgreen 10776262Sgreen#define up_Proc_format \ 10892559Sdes "%5d %-*.*s %3d %4d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s" 109295367Sdes 11057429Smarkm 111295367Sdes 112248619Sdes/* process state names for the "STATE" column of the display */ 113295367Sdes/* the extra nulls in the string "run" are for adding a slash and 11457429Smarkm the processor number when needed */ 115295367Sdes 116295367Sdeschar *state_abbrev[] = 11792559Sdes{ 11857429Smarkm "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "MUTEX" 119295367Sdes}; 12057429Smarkm 12192559Sdes 12292559Sdesstatic kvm_t *kd; 123295367Sdes 124295367Sdes/* values that we stash away in _init and use in later routines */ 12592559Sdes 126248619Sdesstatic double logcpu; 127248619Sdes 12892559Sdes/* these are retrieved from the kernel in _init */ 129248619Sdes 130248619Sdesstatic load_avg ccpu; 131248619Sdes 132248619Sdes/* these are used in the get_ functions */ 133295367Sdes 134295367Sdesstatic int lastpid; 135295367Sdes 136295367Sdes/* these are for calculating cpu state percentages */ 137248619Sdes 138295367Sdesstatic long cp_time[CPUSTATES]; 139295367Sdesstatic long cp_old[CPUSTATES]; 140295367Sdesstatic long cp_diff[CPUSTATES]; 141248619Sdes 142248619Sdes/* these are for detailing the process states */ 143248619Sdes 144295367Sdesint process_states[8]; 145248619Sdeschar *procstatenames[] = { 146248619Sdes "", " starting, ", " running, ", " sleeping, ", " stopped, ", 147248619Sdes " zombie, ", " waiting, ", " mutex, ", 148248619Sdes NULL 149295367Sdes}; 150295367Sdes 151248619Sdes/* these are for detailing the cpu states */ 152248619Sdes 153296853Sdesint cpu_states[CPUSTATES]; 154296853Sdeschar *cpustatenames[] = { 155248619Sdes "user", "nice", "system", "interrupt", "idle", NULL 156248619Sdes}; 157248619Sdes 15892559Sdes/* these are for detailing the memory statistics */ 15957429Smarkm 16057429Smarkmint memory_stats[7]; 16165668Skrischar *memorynames[] = { 16292559Sdes "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free", 163295367Sdes NULL 16457429Smarkm}; 16592559Sdes 16665668Skrisint swap_stats[7]; 167295367Sdeschar *swapnames[] = { 16892559Sdes/* 0 1 2 3 4 5 */ 169295367Sdes "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 170295367Sdes NULL 17165668Skris}; 17292559Sdes 17357429Smarkm 17457429Smarkm/* these are for keeping track of the proc array */ 17576262Sgreen 17692559Sdesstatic int nproc; 17792559Sdesstatic int onproc = -1; 17857429Smarkmstatic int pref_len; 17957429Smarkmstatic struct kinfo_proc *pbase; 18092559Sdesstatic struct kinfo_proc **pref; 181295367Sdes 18257429Smarkm/* these are for getting the memory statistics */ 183295367Sdes 18476262Sgreenstatic int pageshift; /* log base 2 of the pagesize */ 185240075Sdes 186295367Sdes/* define pagetok in terms of pageshift */ 187295367Sdes 18857429Smarkm#define pagetok(size) ((size) << pageshift) 189226046Sdes 190226046Sdes/* useful externals */ 191226046Sdeslong percentages(); 192226046Sdes 19365668Skris#ifdef ORDER 19492559Sdes/* sorting orders. first is default */ 19565668Skrischar *ordernames[] = { 196162856Sdes "cpu", "size", "res", "time", "pri", NULL 197162856Sdes}; 198162856Sdes#endif 199162856Sdes 200162856Sdesint 201226046Sdesmachine_init(statics) 202295367Sdes 203226046Sdesstruct statics *statics; 204226046Sdes 205226046Sdes{ 206226046Sdes register int pagesize; 207295367Sdes size_t modelen; 208295367Sdes struct passwd *pw; 209295367Sdes 210295367Sdes modelen = sizeof(smpmode); 211295367Sdes if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 && 212295367Sdes sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) || 213226046Sdes modelen != sizeof(smpmode)) 214226046Sdes smpmode = 0; 215226046Sdes 216162856Sdes while ((pw = getpwent()) != NULL) { 217162856Sdes if (strlen(pw->pw_name) > namelength) 21857429Smarkm namelength = strlen(pw->pw_name); 219296853Sdes } 220296853Sdes if (namelength < 8) 221295367Sdes namelength = 8; 222295367Sdes if (namelength > 15) 223295367Sdes namelength = 15; 224295367Sdes 225295367Sdes if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) 226295367Sdes return -1; 227296853Sdes 228296853Sdes GETSYSCTL("kern.ccpu", ccpu); 229295367Sdes 230295367Sdes /* this is used in calculating WCPU -- calculate it ahead of time */ 231295367Sdes logcpu = log(loaddouble(ccpu)); 232295367Sdes 233295367Sdes pbase = NULL; 23476262Sgreen pref = NULL; 23576262Sgreen nproc = 0; 23676262Sgreen onproc = -1; 237296853Sdes /* get the page size with "getpagesize" and calculate pageshift from it */ 238296853Sdes pagesize = getpagesize(); 23957429Smarkm pageshift = 0; 24092559Sdes while (pagesize > 1) 241295367Sdes { 242295367Sdes pageshift++; 243295367Sdes pagesize >>= 1; 244296853Sdes } 245295367Sdes 246295367Sdes /* we only need the amount of log(2)1024 for our conversion */ 247295367Sdes pageshift -= LOG1024; 248295367Sdes 249295367Sdes /* fill in the statics information */ 250295367Sdes statics->procstate_names = procstatenames; 25176262Sgreen statics->cpustate_names = cpustatenames; 252295367Sdes statics->memory_names = memorynames; 25392559Sdes statics->swap_names = swapnames; 25457429Smarkm#ifdef ORDER 25576262Sgreen statics->order_names = ordernames; 256124211Sdes#endif 257296853Sdes 258295367Sdes /* all done! */ 25957429Smarkm return(0); 26057429Smarkm} 261296853Sdes 262296853Sdeschar *format_header(uname_field) 263295367Sdes 26498684Sdesregister char *uname_field; 265295367Sdes 266295367Sdes{ 26776262Sgreen static char Header[128]; 26892559Sdes 26998684Sdes snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header, 270113911Sdes namelength, namelength, uname_field); 27198684Sdes 272126277Sdes cmdlengthdelta = strlen(Header) - 7; 273113911Sdes 274215116Sdes return Header; 27598684Sdes} 276295367Sdes 277295367Sdesstatic int swappgsin = -1; 27898684Sdesstatic int swappgsout = -1; 27992559Sdesextern struct timeval timeout; 280240075Sdes 281240075Sdesvoid 282240075Sdesget_system_info(si) 283204917Sdes 284204917Sdesstruct system_info *si; 285204917Sdes 286295367Sdes{ 287295367Sdes long total; 288295367Sdes struct loadavg sysload; 289295367Sdes int mib[2]; 290215116Sdes struct timeval boottime; 291295367Sdes size_t bt_size; 292215116Sdes 293295367Sdes /* get the cp_time array */ 294215116Sdes GETSYSCTL("kern.cp_time", cp_time); 295215116Sdes GETSYSCTL("vm.loadavg", sysload); 296295367Sdes GETSYSCTL("kern.lastpid", lastpid); 297215116Sdes 298215116Sdes /* convert load averages to doubles */ 299204917Sdes { 300215116Sdes register int i; 301295367Sdes register double *infoloadp; 302295367Sdes 303295367Sdes infoloadp = si->load_avg; 304215116Sdes for (i = 0; i < 3; i++) 305204917Sdes { 306295367Sdes#ifdef notyet 307295367Sdes *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale; 308295367Sdes#endif 309295367Sdes *infoloadp++ = loaddouble(sysload.ldavg[i]); 310295367Sdes } 311295367Sdes } 312204917Sdes 313295367Sdes /* convert cp_time counts to percentages */ 314295367Sdes total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 315295367Sdes 316295367Sdes /* sum memory & swap statistics */ 317295367Sdes { 318215116Sdes static unsigned int swap_delay = 0; 319215116Sdes static int swapavail = 0; 320215116Sdes static int swapfree = 0; 321215116Sdes static int bufspace = 0; 322215116Sdes static int nspgsin, nspgsout; 323215116Sdes 324215116Sdes GETSYSCTL("vfs.bufspace", bufspace); 325215116Sdes GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]); 326295367Sdes GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]); 327255767Sdes GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]); 328295367Sdes GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]); 32992559Sdes GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]); 33092559Sdes GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin); 33157429Smarkm GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout); 33257429Smarkm /* convert memory stats to Kbytes */ 33392559Sdes memory_stats[4] = bufspace / 1024; 334295367Sdes memory_stats[6] = -1; 33592559Sdes 336262566Sdes /* first interval */ 337295367Sdes if (swappgsin < 0) { 33898684Sdes swap_stats[4] = 0; 339262566Sdes swap_stats[5] = 0; 340262566Sdes } 341262566Sdes 342262566Sdes /* compute differences between old and new swap statistic */ 343262566Sdes else { 34498684Sdes swap_stats[4] = pagetok(((nspgsin - swappgsin))); 345295367Sdes swap_stats[5] = pagetok(((nspgsout - swappgsout))); 346295367Sdes } 34792559Sdes 34892559Sdes swappgsin = nspgsin; 349113911Sdes swappgsout = nspgsout; 35092559Sdes 351295367Sdes /* call CPU heavy swapmode() only for changes */ 352295367Sdes if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 353113911Sdes swap_stats[3] = swapmode(&swapavail, &swapfree); 35492559Sdes swap_stats[0] = swapavail; 355255767Sdes swap_stats[1] = swapavail - swapfree; 356113911Sdes swap_stats[2] = swapfree; 35792559Sdes } 35892559Sdes swap_delay = 1; 35992559Sdes swap_stats[6] = -1; 360295367Sdes } 36157429Smarkm 362295367Sdes /* set arrays and strings */ 363295367Sdes si->cpustates = cpu_states; 364295367Sdes si->memory = memory_stats; 365295367Sdes si->swap = swap_stats; 366295367Sdes 367295367Sdes 368295367Sdes if(lastpid > 0) { 369295367Sdes si->last_pid = lastpid; 370295367Sdes } else { 37157429Smarkm si->last_pid = -1; 372295367Sdes } 373295367Sdes 374295367Sdes /* 375295367Sdes * Print how long system has been up. 376295367Sdes * (Found by looking getting "boottime" from the kernel) 377295367Sdes */ 378295367Sdes mib[0] = CTL_KERN; 379295367Sdes mib[1] = KERN_BOOTTIME; 380295367Sdes bt_size = sizeof(boottime); 38165668Skris if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 && 38276262Sgreen boottime.tv_sec != 0) { 383295367Sdes si->boottime = boottime; 384295367Sdes } else { 385296853Sdes si->boottime.tv_sec = -1; 386295367Sdes } 387295367Sdes} 388295367Sdes 389295367Sdesstatic struct handle handle; 390255767Sdes 39157429Smarkmcaddr_t get_process_info(si, sel, compare) 392295367Sdes 393295367Sdesstruct system_info *si; 394295367Sdesstruct process_select *sel; 395295367Sdesint (*compare)(); 396295367Sdes 397295367Sdes{ 398295367Sdes register int i; 39957429Smarkm register int total_procs; 40057429Smarkm register int active_procs; 401295367Sdes register struct kinfo_proc **prefp; 40257429Smarkm register struct kinfo_proc *pp; 40392559Sdes 40457429Smarkm /* these are copied out of sel for speed */ 40592559Sdes int show_idle; 40692559Sdes int show_self; 40792559Sdes int show_system; 40857429Smarkm int show_uid; 40957429Smarkm int show_command; 41092559Sdes 411295367Sdes 41298684Sdes pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc); 41398684Sdes if (nproc > onproc) 414295367Sdes pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *) 41598684Sdes * (onproc = nproc)); 41698684Sdes if (pref == NULL || pbase == NULL) { 41798684Sdes (void) fprintf(stderr, "top: Out of memory.\n"); 41898684Sdes quit(23); 41998684Sdes } 42098684Sdes /* get a pointer to the states summary array */ 42198684Sdes si->procstates = process_states; 42298684Sdes 42398684Sdes /* set up flags which define what we are going to select */ 42498684Sdes show_idle = sel->idle; 425264377Sdes show_self = sel->self; 426255767Sdes show_system = sel->system; 42798684Sdes show_uid = sel->uid != -1; 428295367Sdes show_command = sel->command != NULL; 429295367Sdes 430295367Sdes /* count up process states and get pointers to interesting procs */ 431295367Sdes total_procs = 0; 432295367Sdes active_procs = 0; 433295367Sdes memset((char *)process_states, 0, sizeof(process_states)); 434295367Sdes prefp = pref; 435295367Sdes for (pp = pbase, i = 0; i < nproc; pp++, i++) 436295367Sdes { 437264377Sdes /* 438255767Sdes * Place pointers to each valid proc structure in pref[]. 439106130Sdes * Process slots that are actually in use have a non-zero 44098684Sdes * status field. Processes with P_SYSTEM set are system 44198684Sdes * processes---these get ignored unless show_sysprocs is set. 44298684Sdes */ 443295367Sdes if (pp->ki_stat != 0 && 44492559Sdes (show_self != pp->ki_pid) && 44592559Sdes (show_system || ((pp->ki_flag & P_SYSTEM) == 0))) 446295367Sdes { 44792559Sdes total_procs++; 44892559Sdes process_states[(unsigned char) pp->ki_stat]++; 449295367Sdes if ((pp->ki_stat != SZOMB) && 45092559Sdes (show_idle || (pp->ki_pctcpu != 0) || 45192559Sdes (pp->ki_stat == SRUN)) && 45292559Sdes (!show_uid || pp->ki_ruid == (uid_t)sel->uid)) 45392559Sdes { 45492559Sdes *prefp++ = pp; 45592559Sdes active_procs++; 45692559Sdes } 45792559Sdes } 458181111Sdes } 45992559Sdes 46092559Sdes /* if requested, sort the "interesting" processes */ 461295367Sdes if (compare != NULL) 46292559Sdes { 463240075Sdes qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare); 464240075Sdes } 465240075Sdes 46692559Sdes /* remember active and total counts */ 46792559Sdes si->p_total = total_procs; 46898684Sdes si->p_active = pref_len = active_procs; 469106130Sdes 470204917Sdes /* pass back a handle */ 471204917Sdes handle.next_proc = pref; 47292559Sdes handle.remaining = active_procs; 47392559Sdes return((caddr_t)&handle); 47457429Smarkm} 47557429Smarkm 47657429Smarkmchar fmt[128]; /* static area where result is built */ 47792559Sdes 47892559Sdeschar *format_next_process(handle, get_userid) 479295367Sdes 480204917Sdescaddr_t handle; 481295367Sdeschar *(*get_userid)(); 482295367Sdes 48357429Smarkm{ 484296853Sdes register struct kinfo_proc *pp; 485157019Sdes register long cputime; 486157019Sdes register double pct; 487157019Sdes struct handle *hp; 488124211Sdes char status[16]; 48998941Sdes int state; 49098941Sdes 491295367Sdes /* find and remember the next proc structure */ 492221420Sdes hp = (struct handle *)handle; 493295367Sdes pp = *(hp->next_proc++); 49457429Smarkm hp->remaining--; 495295367Sdes 496295367Sdes /* get the process's command name */ 497295367Sdes if ((pp->ki_sflag & PS_INMEM) == 0) { 498295367Sdes /* 499295367Sdes * Print swapped processes as <pname> 500295367Sdes */ 501295367Sdes char *comm = pp->ki_comm; 502295367Sdes#define COMSIZ sizeof(pp->ki_comm) 503295367Sdes char buf[COMSIZ]; 50492559Sdes (void) strncpy(buf, comm, COMSIZ); 505295367Sdes comm[0] = '<'; 506295367Sdes (void) strncpy(&comm[1], buf, COMSIZ - 2); 507295367Sdes comm[COMSIZ - 2] = '\0'; 50857429Smarkm (void) strncat(comm, ">", COMSIZ - 1); 509295367Sdes comm[COMSIZ - 1] = '\0'; 510295367Sdes } 51192559Sdes 512295367Sdes /* 513295367Sdes * Convert the process's runtime from microseconds to seconds. This 514295367Sdes * time includes the interrupt time although that is not wanted here. 515295367Sdes * ps(1) is similarly sloppy. 516295367Sdes */ 517240075Sdes cputime = (pp->ki_runtime + 500000) / 1000000; 518240075Sdes 519240075Sdes /* calculate the base for cpu percentages */ 52092559Sdes pct = pctdouble(pp->ki_pctcpu); 52192559Sdes 522295367Sdes /* generate "STATE" field */ 523295367Sdes switch (state = pp->ki_stat) { 524295367Sdes case SRUN: 525295367Sdes if (smpmode && pp->ki_oncpu != 0xff) 52698684Sdes sprintf(status, "CPU%d", pp->ki_oncpu); 52798684Sdes else 528295367Sdes strcpy(status, "RUN"); 529295367Sdes break; 530295367Sdes case SMTX: 531295367Sdes if (pp->ki_kiflag & KI_MTXBLOCK) { 532113911Sdes sprintf(status, "*%.6s", pp->ki_mtxname); 533113911Sdes break; 534113911Sdes } 53592559Sdes /* fall through */ 53657429Smarkm case SSLEEP: 53792559Sdes if (pp->ki_wmesg != NULL) { 53892559Sdes sprintf(status, "%.6s", pp->ki_wmesg); 539295367Sdes break; 540295367Sdes } 54192559Sdes /* fall through */ 542204917Sdes default: 54392559Sdes 54492559Sdes if (state >= 0 && 54592559Sdes state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 546204917Sdes sprintf(status, "%.6s", state_abbrev[(unsigned char) state]); 54792559Sdes else 54898684Sdes sprintf(status, "?%5d", state); 54998684Sdes break; 55098684Sdes } 55198684Sdes 55298684Sdes /* format this entry */ 55398684Sdes sprintf(fmt, 55498684Sdes smpmode ? smp_Proc_format : up_Proc_format, 55592559Sdes pp->ki_pid, 55692559Sdes namelength, namelength, 55792559Sdes (*get_userid)(pp->ki_ruid), 55892559Sdes pp->ki_pri.pri_level - PZERO, 55957429Smarkm 56057429Smarkm /* 561295367Sdes * normal time -> nice value -20 - +20 562295367Sdes * real time 0 - 31 -> nice value -52 - -21 563295367Sdes * idle time 0 - 31 -> nice value +21 - +52 564295367Sdes */ 565295367Sdes (pp->ki_pri.pri_class == PRI_TIMESHARE ? 566295367Sdes pp->ki_nice - NZERO : 567295367Sdes (PRI_IS_REALTIME(pp->ki_pri.pri_class) ? 568295367Sdes (PRIO_MIN - 1 - (PRI_MAX_REALTIME - pp->ki_pri.pri_level)) : 569295367Sdes (PRIO_MAX + 1 + pp->ki_pri.pri_level - PRI_MIN_IDLE))), 570295367Sdes format_k2(PROCSIZE(pp)), 571295367Sdes format_k2(pagetok(pp->ki_rssize)), 572295367Sdes status, 573295367Sdes smpmode ? pp->ki_lastcpu : 0, 574295367Sdes format_time(cputime), 575295367Sdes 100.0 * weighted_cpu(pct, pp), 576295367Sdes 100.0 * pct, 577295367Sdes screen_width > cmdlengthdelta ? 57892559Sdes screen_width - cmdlengthdelta : 57992559Sdes 0, 580204917Sdes printable(pp->ki_comm)); 581295367Sdes 58292559Sdes /* return the result */ 58392559Sdes return(fmt); 58492559Sdes} 58592559Sdes 586295367Sdesstatic void getsysctl (name, ptr, len) 58792559Sdes 58898684Sdeschar *name; 58998684Sdesvoid *ptr; 59092559Sdessize_t len; 59192559Sdes 59265668Skris{ 59365668Skris size_t nlen = len; 59492559Sdes if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 59592559Sdes fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 59657429Smarkm strerror(errno)); 59792559Sdes quit(23); 598147005Sdes } 59998684Sdes if (nlen != len) { 60092559Sdes fprintf(stderr, "top: sysctl(%s...) expected %d, got %d\n", name, 60198684Sdes len, nlen); 60298684Sdes quit(23); 603295367Sdes } 60492559Sdes} 60598684Sdes 60698684Sdes/* comparison routines for qsort */ 60792559Sdes 60898684Sdes/* 60998684Sdes * proc_compare - comparison function for "qsort" 61092559Sdes * Compares the resource consumption of two processes using five 611147005Sdes * distinct keys. The keys (in descending order of importance) are: 612295367Sdes * percent cpu, cpu ticks, state, resident set size, total virtual 613295367Sdes * memory usage. The process states are ordered as follows (from least 61492559Sdes * to most important): WAIT, zombie, sleep, stop, start, run. The 61592559Sdes * array declaration below maps a process state index into a number 61657429Smarkm * that reflects this ordering. 61776262Sgreen */ 61892559Sdes 61992559Sdesstatic unsigned char sorted_state[] = 620295367Sdes{ 62192559Sdes 0, /* not used */ 62257429Smarkm 3, /* sleep */ 623 1, /* ABANDONED (WAIT) */ 624 6, /* run */ 625 5, /* start */ 626 2, /* zombie */ 627 4 /* stop */ 628}; 629 630 631#define ORDERKEY_PCTCPU \ 632 if (lresult = (long) p2->ki_pctcpu - (long) p1->ki_pctcpu, \ 633 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0) 634 635#define ORDERKEY_CPTICKS \ 636 if ((result = p2->ki_runtime > p1->ki_runtime ? 1 : \ 637 p2->ki_runtime < p1->ki_runtime ? -1 : 0) == 0) 638 639#define ORDERKEY_STATE \ 640 if ((result = sorted_state[(unsigned char) p2->ki_stat] - \ 641 sorted_state[(unsigned char) p1->ki_stat]) == 0) 642 643#define ORDERKEY_PRIO \ 644 if ((result = p2->ki_pri.pri_level - p1->ki_pri.pri_level) == 0) 645 646#define ORDERKEY_RSSIZE \ 647 if ((result = p2->ki_rssize - p1->ki_rssize) == 0) 648 649#define ORDERKEY_MEM \ 650 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 ) 651 652/* compare_cpu - the comparison function for sorting by cpu percentage */ 653 654int 655#ifdef ORDER 656compare_cpu(pp1, pp2) 657#else 658proc_compare(pp1, pp2) 659#endif 660 661struct proc **pp1; 662struct proc **pp2; 663 664{ 665 register struct kinfo_proc *p1; 666 register struct kinfo_proc *p2; 667 register int result; 668 register pctcpu lresult; 669 670 /* remove one level of indirection */ 671 p1 = *(struct kinfo_proc **) pp1; 672 p2 = *(struct kinfo_proc **) pp2; 673 674 ORDERKEY_PCTCPU 675 ORDERKEY_CPTICKS 676 ORDERKEY_STATE 677 ORDERKEY_PRIO 678 ORDERKEY_RSSIZE 679 ORDERKEY_MEM 680 ; 681 682 return(result); 683} 684 685#ifdef ORDER 686/* compare routines */ 687int compare_size(), compare_res(), compare_time(), compare_prio(); 688 689int (*proc_compares[])() = { 690 compare_cpu, 691 compare_size, 692 compare_res, 693 compare_time, 694 compare_prio, 695 NULL 696}; 697 698/* compare_size - the comparison function for sorting by total memory usage */ 699 700int 701compare_size(pp1, pp2) 702 703struct proc **pp1; 704struct proc **pp2; 705 706{ 707 register struct kinfo_proc *p1; 708 register struct kinfo_proc *p2; 709 register int result; 710 register pctcpu lresult; 711 712 /* remove one level of indirection */ 713 p1 = *(struct kinfo_proc **) pp1; 714 p2 = *(struct kinfo_proc **) pp2; 715 716 ORDERKEY_MEM 717 ORDERKEY_RSSIZE 718 ORDERKEY_PCTCPU 719 ORDERKEY_CPTICKS 720 ORDERKEY_STATE 721 ORDERKEY_PRIO 722 ; 723 724 return(result); 725} 726 727/* compare_res - the comparison function for sorting by resident set size */ 728 729int 730compare_res(pp1, pp2) 731 732struct proc **pp1; 733struct proc **pp2; 734 735{ 736 register struct kinfo_proc *p1; 737 register struct kinfo_proc *p2; 738 register int result; 739 register pctcpu lresult; 740 741 /* remove one level of indirection */ 742 p1 = *(struct kinfo_proc **) pp1; 743 p2 = *(struct kinfo_proc **) pp2; 744 745 ORDERKEY_RSSIZE 746 ORDERKEY_MEM 747 ORDERKEY_PCTCPU 748 ORDERKEY_CPTICKS 749 ORDERKEY_STATE 750 ORDERKEY_PRIO 751 ; 752 753 return(result); 754} 755 756/* compare_time - the comparison function for sorting by total cpu time */ 757 758int 759compare_time(pp1, pp2) 760 761struct proc **pp1; 762struct proc **pp2; 763 764{ 765 register struct kinfo_proc *p1; 766 register struct kinfo_proc *p2; 767 register int result; 768 register pctcpu lresult; 769 770 /* remove one level of indirection */ 771 p1 = *(struct kinfo_proc **) pp1; 772 p2 = *(struct kinfo_proc **) pp2; 773 774 ORDERKEY_CPTICKS 775 ORDERKEY_PCTCPU 776 ORDERKEY_STATE 777 ORDERKEY_PRIO 778 ORDERKEY_RSSIZE 779 ORDERKEY_MEM 780 ; 781 782 return(result); 783 } 784 785/* compare_prio - the comparison function for sorting by cpu percentage */ 786 787int 788compare_prio(pp1, pp2) 789 790struct proc **pp1; 791struct proc **pp2; 792 793{ 794 register struct kinfo_proc *p1; 795 register struct kinfo_proc *p2; 796 register int result; 797 register pctcpu lresult; 798 799 /* remove one level of indirection */ 800 p1 = *(struct kinfo_proc **) pp1; 801 p2 = *(struct kinfo_proc **) pp2; 802 803 ORDERKEY_PRIO 804 ORDERKEY_CPTICKS 805 ORDERKEY_PCTCPU 806 ORDERKEY_STATE 807 ORDERKEY_RSSIZE 808 ORDERKEY_MEM 809 ; 810 811 return(result); 812} 813#endif 814 815/* 816 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 817 * the process does not exist. 818 * It is EXTREMLY IMPORTANT that this function work correctly. 819 * If top runs setuid root (as in SVR4), then this function 820 * is the only thing that stands in the way of a serious 821 * security problem. It validates requests for the "kill" 822 * and "renice" commands. 823 */ 824 825int proc_owner(pid) 826 827int pid; 828 829{ 830 register int cnt; 831 register struct kinfo_proc **prefp; 832 register struct kinfo_proc *pp; 833 834 prefp = pref; 835 cnt = pref_len; 836 while (--cnt >= 0) 837 { 838 pp = *prefp++; 839 if (pp->ki_pid == (pid_t)pid) 840 { 841 return((int)pp->ki_ruid); 842 } 843 } 844 return(-1); 845} 846 847int 848swapmode(retavail, retfree) 849 int *retavail; 850 int *retfree; 851{ 852 int n; 853 int pagesize = getpagesize(); 854 struct kvm_swap swapary[1]; 855 856 *retavail = 0; 857 *retfree = 0; 858 859#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 860 861 n = kvm_getswapinfo(kd, swapary, 1, 0); 862 if (n < 0 || swapary[0].ksw_total == 0) 863 return(0); 864 865 *retavail = CONVERT(swapary[0].ksw_total); 866 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 867 868 n = (int)((double)swapary[0].ksw_used * 100.0 / 869 (double)swapary[0].ksw_total); 870 return(n); 871} 872 873