machine.c revision 238020
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 238020 2012-07-02 20:08:11Z jhb $ 2424143Sjoerg */ 2524143Sjoerg 2624143Sjoerg#include <sys/param.h> 2724143Sjoerg#include <sys/errno.h> 28131621Sdes#include <sys/file.h> 29131621Sdes#include <sys/proc.h> 30131621Sdes#include <sys/resource.h> 31131621Sdes#include <sys/rtprio.h> 32131621Sdes#include <sys/signal.h> 3324143Sjoerg#include <sys/sysctl.h> 3424143Sjoerg#include <sys/time.h> 3524143Sjoerg#include <sys/user.h> 3624143Sjoerg#include <sys/vmmeter.h> 3724143Sjoerg 38222136Spluknet#include <err.h> 39131621Sdes#include <kvm.h> 40131621Sdes#include <math.h> 41131621Sdes#include <nlist.h> 42131626Sdes#include <paths.h> 43131621Sdes#include <pwd.h> 44131621Sdes#include <stdio.h> 4524143Sjoerg#include <stdlib.h> 46144636Sstefanf#include <string.h> 47144636Sstefanf#include <strings.h> 4872951Srwatson#include <unistd.h> 49168710Sstas#include <vis.h> 5024143Sjoerg 5124143Sjoerg#include "top.h" 5224143Sjoerg#include "machine.h" 5369375Sjhb#include "screen.h" 5472951Srwatson#include "utils.h" 55175420Speter#include "layout.h" 5624143Sjoerg 5772951Srwatson#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) 58146291Sobrien#define SMPUNAMELEN 13 59146291Sobrien#define UPUNAMELEN 15 6072951Srwatson 61145073Skeramidaextern struct process_select ps; 6292922Simpextern char* printable(char *); 6327340Speterstatic int smpmode; 64131402Salfredenum displaymodes displaymode; 65175195Sobrien#ifdef TOP_USERNAME_LEN 66175195Sobrienstatic int namelength = TOP_USERNAME_LEN; 67175195Sobrien#else 68146291Sobrienstatic int namelength = 8; 69175195Sobrien#endif 7069375Sjhbstatic int cmdlengthdelta; 7124143Sjoerg 7272951Srwatson/* Prototypes for top internals */ 7392922Simpvoid quit(int); 7424143Sjoerg 7524143Sjoerg/* get_process_info passes back a handle. This is what it looks like: */ 7624143Sjoerg 77158280Sbdestruct handle { 78131623Sdes struct kinfo_proc **next_proc; /* points to next valid proc pointer */ 79131623Sdes int remaining; /* number of pointers remaining */ 8024143Sjoerg}; 8124143Sjoerg 8224143Sjoerg/* declarations for load_avg */ 8324143Sjoerg#include "loadavg.h" 8424143Sjoerg 8524143Sjoerg/* define what weighted cpu is. */ 8669896Smckusick#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \ 8769896Smckusick ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu)))) 8824143Sjoerg 8924143Sjoerg/* what we consider to be process size: */ 9069896Smckusick#define PROCSIZE(pp) ((pp)->ki_size / 1024) 9124143Sjoerg 92131402Salfred#define RU(pp) (&(pp)->ki_rusage) 93131402Salfred#define RUTOT(pp) \ 94131402Salfred (RU(pp)->ru_inblock + RU(pp)->ru_oublock + RU(pp)->ru_majflt) 95131402Salfred 96131402Salfred 9724143Sjoerg/* definitions for indices in the nlist array */ 9824143Sjoerg 9924143Sjoerg/* 10024143Sjoerg * These definitions control the format of the per-process area 10124143Sjoerg */ 10224143Sjoerg 103131402Salfredstatic char io_header[] = 104168799Srafan " PID%s %-*.*s VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND"; 105131402Salfred 106131402Salfred#define io_Proc_format \ 107168799Srafan "%5d%s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s" 108131402Salfred 109145073Skeramidastatic char smp_header_thr[] = 110186499Srpaulo " PID%s %-*.*s THR PRI NICE SIZE RES STATE C TIME %6s COMMAND"; 11127340Speterstatic char smp_header[] = 112186499Srpaulo " PID%s %-*.*s " "PRI NICE SIZE RES STATE C TIME %6s COMMAND"; 11324143Sjoerg 11427340Speter#define smp_Proc_format \ 115183526Sjhb "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s %2d%7s %5.2f%% %.*s" 11624143Sjoerg 117145073Skeramidastatic char up_header_thr[] = 118168799Srafan " PID%s %-*.*s THR PRI NICE SIZE RES STATE TIME %6s COMMAND"; 11927340Speterstatic char up_header[] = 120168799Srafan " PID%s %-*.*s " "PRI NICE SIZE RES STATE TIME %6s COMMAND"; 12124143Sjoerg 12227340Speter#define up_Proc_format \ 123168799Srafan "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s%.0d%7s %5.2f%% %.*s" 12424143Sjoerg 12524143Sjoerg 12624143Sjoerg/* process state names for the "STATE" column of the display */ 12724143Sjoerg/* the extra nulls in the string "run" are for adding a slash and 12824143Sjoerg the processor number when needed */ 12924143Sjoerg 130158280Sbdechar *state_abbrev[] = { 131131623Sdes "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK" 13224143Sjoerg}; 13324143Sjoerg 13424143Sjoerg 13524143Sjoergstatic kvm_t *kd; 13624143Sjoerg 13724143Sjoerg/* values that we stash away in _init and use in later routines */ 13824143Sjoerg 13924143Sjoergstatic double logcpu; 14024143Sjoerg 14124143Sjoerg/* these are retrieved from the kernel in _init */ 14224143Sjoerg 14324143Sjoergstatic load_avg ccpu; 14424143Sjoerg 14572951Srwatson/* these are used in the get_ functions */ 14624143Sjoerg 14772951Srwatsonstatic int lastpid; 14824143Sjoerg 14924143Sjoerg/* these are for calculating cpu state percentages */ 15024143Sjoerg 15124143Sjoergstatic long cp_time[CPUSTATES]; 15224143Sjoergstatic long cp_old[CPUSTATES]; 15324143Sjoergstatic long cp_diff[CPUSTATES]; 15424143Sjoerg 15524143Sjoerg/* these are for detailing the process states */ 15624143Sjoerg 15765557Sjasoneint process_states[8]; 15824143Sjoergchar *procstatenames[] = { 159131623Sdes "", " starting, ", " running, ", " sleeping, ", " stopped, ", 160131623Sdes " zombie, ", " waiting, ", " lock, ", 161131623Sdes NULL 16224143Sjoerg}; 16324143Sjoerg 16424143Sjoerg/* these are for detailing the cpu states */ 16524143Sjoerg 16624143Sjoergint cpu_states[CPUSTATES]; 16724143Sjoergchar *cpustatenames[] = { 168131623Sdes "user", "nice", "system", "interrupt", "idle", NULL 16924143Sjoerg}; 17024143Sjoerg 17124143Sjoerg/* these are for detailing the memory statistics */ 17224143Sjoerg 17324143Sjoergint memory_stats[7]; 17424143Sjoergchar *memorynames[] = { 175158280Sbde "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", 176158280Sbde "K Free", NULL 17724143Sjoerg}; 17824143Sjoerg 179237656Sjhbint arc_stats[7]; 180237656Sjhbchar *arcnames[] = { 181237656Sjhb "K Total, ", "K MRU, ", "K MFU, ", "K Anon, ", "K Header, ", "K Other", 182237656Sjhb NULL 183237656Sjhb}; 184237656Sjhb 18524143Sjoergint swap_stats[7]; 18624143Sjoergchar *swapnames[] = { 187131623Sdes "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out", 188131623Sdes NULL 18924143Sjoerg}; 19024143Sjoerg 19124143Sjoerg 19224143Sjoerg/* these are for keeping track of the proc array */ 19324143Sjoerg 19424143Sjoergstatic int nproc; 19524143Sjoergstatic int onproc = -1; 19624143Sjoergstatic int pref_len; 19724143Sjoergstatic struct kinfo_proc *pbase; 19824143Sjoergstatic struct kinfo_proc **pref; 199131402Salfredstatic struct kinfo_proc *previous_procs; 200131402Salfredstatic struct kinfo_proc **previous_pref; 201131402Salfredstatic int previous_proc_count = 0; 202131402Salfredstatic int previous_proc_count_max = 0; 203237656Sjhbstatic int arc_enabled; 20424143Sjoerg 205131412Salfred/* total number of io operations */ 206131412Salfredstatic long total_inblock; 207131412Salfredstatic long total_oublock; 208131412Salfredstatic long total_majflt; 209131412Salfred 21024143Sjoerg/* these are for getting the memory statistics */ 21124143Sjoerg 21224143Sjoergstatic int pageshift; /* log base 2 of the pagesize */ 21324143Sjoerg 21424143Sjoerg/* define pagetok in terms of pageshift */ 21524143Sjoerg 21624143Sjoerg#define pagetok(size) ((size) << pageshift) 21724143Sjoerg 21824143Sjoerg/* useful externals */ 21924143Sjoerglong percentages(); 22024143Sjoerg 22138278Swosch#ifdef ORDER 222131829Skeramida/* 223145073Skeramida * Sorting orders. The first element is the default. 224131829Skeramida */ 225133817Salfredchar *ordernames[] = { 226145073Skeramida "cpu", "size", "res", "time", "pri", "threads", 227168799Srafan "total", "read", "write", "fault", "vcsw", "ivcsw", 228168799Srafan "jid", NULL 22938278Swosch}; 23038278Swosch#endif 23138278Swosch 232175420Speter/* Per-cpu time states */ 233175420Speterstatic int maxcpu; 234175420Speterstatic int maxid; 235175420Speterstatic int ncpus; 236175420Speterstatic u_long cpumask; 237175420Speterstatic long *times; 238175420Speterstatic long *pcpu_cp_time; 239175420Speterstatic long *pcpu_cp_old; 240175420Speterstatic long *pcpu_cp_diff; 241175420Speterstatic int *pcpu_cpu_states; 242175420Speter 243168799Srafanstatic int compare_jid(const void *a, const void *b); 244158282Sbdestatic int compare_pid(const void *a, const void *b); 245224188Sjhbstatic int compare_tid(const void *a, const void *b); 246158282Sbdestatic const char *format_nice(const struct kinfo_proc *pp); 247158282Sbdestatic void getsysctl(const char *name, void *ptr, size_t len); 248158282Sbdestatic int swapmode(int *retavail, int *retfree); 249237656Sjhbstatic void update_layout(void); 250158282Sbde 251223936Sjhbvoid 252224205Sjhbtoggle_pcpustats(void) 253223936Sjhb{ 254223936Sjhb 255223936Sjhb if (ncpus == 1) 256223936Sjhb return; 257237656Sjhb update_layout(); 258237656Sjhb} 259223936Sjhb 260237656Sjhb/* Adjust display based on ncpus and the ARC state. */ 261237656Sjhbstatic void 262237656Sjhbupdate_layout(void) 263237656Sjhb{ 264237656Sjhb 265237656Sjhb y_mem = 3; 266238020Sjhb y_arc = 4; 267237656Sjhb y_swap = 4 + arc_enabled; 268237656Sjhb y_idlecursor = 5 + arc_enabled; 269237656Sjhb y_message = 5 + arc_enabled; 270237656Sjhb y_header = 6 + arc_enabled; 271237656Sjhb y_procs = 7 + arc_enabled; 272237656Sjhb Header_lines = 7 + arc_enabled; 273237656Sjhb 274223936Sjhb if (pcpu_stats) { 275238020Sjhb y_mem += ncpus - 1; 276238020Sjhb y_arc += ncpus - 1; 277237656Sjhb y_swap += ncpus - 1; 278237656Sjhb y_idlecursor += ncpus - 1; 279237656Sjhb y_message += ncpus - 1; 280237656Sjhb y_header += ncpus - 1; 281237656Sjhb y_procs += ncpus - 1; 282237656Sjhb Header_lines += ncpus - 1; 283223936Sjhb } 284223936Sjhb} 285223936Sjhb 28624143Sjoergint 287175195Sobrienmachine_init(struct statics *statics, char do_unames) 28824143Sjoerg{ 289223936Sjhb int i, j, empty, pagesize; 290237656Sjhb uint64_t arc_size; 291223936Sjhb size_t size; 292131623Sdes struct passwd *pw; 29324143Sjoerg 294223936Sjhb size = sizeof(smpmode); 295223936Sjhb if ((sysctlbyname("machdep.smp_active", &smpmode, &size, 296158280Sbde NULL, 0) != 0 && 297223936Sjhb sysctlbyname("kern.smp.active", &smpmode, &size, 298158280Sbde NULL, 0) != 0) || 299223936Sjhb size != sizeof(smpmode)) 300131623Sdes smpmode = 0; 30127340Speter 302237656Sjhb size = sizeof(arc_size); 303237656Sjhb if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc_size, &size, 304237656Sjhb NULL, 0) == 0 && arc_size != 0) 305237656Sjhb arc_enabled = 1; 306237656Sjhb 307175195Sobrien if (do_unames) { 308175195Sobrien while ((pw = getpwent()) != NULL) { 309131623Sdes if (strlen(pw->pw_name) > namelength) 310131623Sdes namelength = strlen(pw->pw_name); 311175195Sobrien } 312131623Sdes } 313146291Sobrien if (smpmode && namelength > SMPUNAMELEN) 314146291Sobrien namelength = SMPUNAMELEN; 315146291Sobrien else if (namelength > UPUNAMELEN) 316146291Sobrien namelength = UPUNAMELEN; 31727390Speter 318131626Sdes kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); 319131626Sdes if (kd == NULL) 320131626Sdes return (-1); 32124143Sjoerg 322131623Sdes GETSYSCTL("kern.ccpu", ccpu); 32324143Sjoerg 324131623Sdes /* this is used in calculating WCPU -- calculate it ahead of time */ 325131623Sdes logcpu = log(loaddouble(ccpu)); 32624143Sjoerg 327131623Sdes pbase = NULL; 328131623Sdes pref = NULL; 329131623Sdes nproc = 0; 330131623Sdes onproc = -1; 331158280Sbde 332158280Sbde /* get the page size and calculate pageshift from it */ 333131623Sdes pagesize = getpagesize(); 334131623Sdes pageshift = 0; 335131626Sdes while (pagesize > 1) { 336131623Sdes pageshift++; 337131623Sdes pagesize >>= 1; 338131623Sdes } 33924143Sjoerg 340131623Sdes /* we only need the amount of log(2)1024 for our conversion */ 341131623Sdes pageshift -= LOG1024; 34224143Sjoerg 343131623Sdes /* fill in the statics information */ 344131623Sdes statics->procstate_names = procstatenames; 345131623Sdes statics->cpustate_names = cpustatenames; 346131623Sdes statics->memory_names = memorynames; 347237656Sjhb if (arc_enabled) 348237656Sjhb statics->arc_names = arcnames; 349237656Sjhb else 350237656Sjhb statics->arc_names = NULL; 351131623Sdes statics->swap_names = swapnames; 35238278Swosch#ifdef ORDER 353133817Salfred statics->order_names = ordernames; 35438278Swosch#endif 35524143Sjoerg 356223936Sjhb /* Allocate state for per-CPU stats. */ 357223936Sjhb cpumask = 0; 358223936Sjhb ncpus = 0; 359223936Sjhb GETSYSCTL("kern.smp.maxcpus", maxcpu); 360223936Sjhb size = sizeof(long) * maxcpu * CPUSTATES; 361223936Sjhb times = malloc(size); 362223936Sjhb if (times == NULL) 363223936Sjhb err(1, "malloc %zd bytes", size); 364223936Sjhb if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1) 365223936Sjhb err(1, "sysctlbyname kern.cp_times"); 366223936Sjhb pcpu_cp_time = calloc(1, size); 367223936Sjhb maxid = (size / CPUSTATES / sizeof(long)) - 1; 368223936Sjhb for (i = 0; i <= maxid; i++) { 369223936Sjhb empty = 1; 370223936Sjhb for (j = 0; empty && j < CPUSTATES; j++) { 371223936Sjhb if (times[i * CPUSTATES + j] != 0) 372223936Sjhb empty = 0; 373175420Speter } 374223936Sjhb if (!empty) { 375223936Sjhb cpumask |= (1ul << i); 376223936Sjhb ncpus++; 377175420Speter } 378175420Speter } 379223936Sjhb size = sizeof(long) * ncpus * CPUSTATES; 380223936Sjhb pcpu_cp_old = calloc(1, size); 381223936Sjhb pcpu_cp_diff = calloc(1, size); 382223936Sjhb pcpu_cpu_states = calloc(1, size); 383224205Sjhb statics->ncpus = ncpus; 384175420Speter 385237656Sjhb update_layout(); 386223936Sjhb 387131623Sdes /* all done! */ 388131623Sdes return (0); 38924143Sjoerg} 39024143Sjoerg 391131310Salfredchar * 392131622Sdesformat_header(char *uname_field) 39324143Sjoerg{ 394131623Sdes static char Header[128]; 395131623Sdes const char *prehead; 39624143Sjoerg 397131623Sdes switch (displaymode) { 398131623Sdes case DISP_CPU: 399145073Skeramida /* 400145073Skeramida * The logic of picking the right header format seems reverse 401145073Skeramida * here because we only want to display a THR column when 402145073Skeramida * "thread mode" is off (and threads are not listed as 403145073Skeramida * separate lines). 404145073Skeramida */ 405145073Skeramida prehead = smpmode ? 406145073Skeramida (ps.thread ? smp_header : smp_header_thr) : 407145073Skeramida (ps.thread ? up_header : up_header_thr); 408146342Skeramida snprintf(Header, sizeof(Header), prehead, 409168799Srafan ps.jail ? " JID" : "", 410146342Skeramida namelength, namelength, uname_field, 411146342Skeramida ps.wcpu ? "WCPU" : "CPU"); 412131623Sdes break; 413131623Sdes case DISP_IO: 414131623Sdes prehead = io_header; 415146342Skeramida snprintf(Header, sizeof(Header), prehead, 416168799Srafan ps.jail ? " JID" : "", 417146342Skeramida namelength, namelength, uname_field); 418131623Sdes break; 419131623Sdes } 420131623Sdes cmdlengthdelta = strlen(Header) - 7; 421131623Sdes return (Header); 42224143Sjoerg} 42324143Sjoerg 42424143Sjoergstatic int swappgsin = -1; 42524143Sjoergstatic int swappgsout = -1; 42624143Sjoergextern struct timeval timeout; 42724143Sjoerg 428175420Speter 42924143Sjoergvoid 430131622Sdesget_system_info(struct system_info *si) 43124143Sjoerg{ 432131623Sdes long total; 433131623Sdes struct loadavg sysload; 434131623Sdes int mib[2]; 435131623Sdes struct timeval boottime; 436237656Sjhb uint64_t arc_stat, arc_stat2; 437175420Speter int i, j; 438175420Speter size_t size; 43924143Sjoerg 440223936Sjhb /* get the CPU stats */ 441223936Sjhb size = (maxid + 1) * CPUSTATES * sizeof(long); 442223936Sjhb if (sysctlbyname("kern.cp_times", pcpu_cp_time, &size, NULL, 0) == -1) 443223936Sjhb err(1, "sysctlbyname kern.cp_times"); 444223936Sjhb GETSYSCTL("kern.cp_time", cp_time); 445131623Sdes GETSYSCTL("vm.loadavg", sysload); 446131623Sdes GETSYSCTL("kern.lastpid", lastpid); 44724143Sjoerg 448131623Sdes /* convert load averages to doubles */ 449131626Sdes for (i = 0; i < 3; i++) 450131626Sdes si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale; 45124143Sjoerg 452223936Sjhb /* convert cp_time counts to percentages */ 453223936Sjhb for (i = j = 0; i <= maxid; i++) { 454223936Sjhb if ((cpumask & (1ul << i)) == 0) 455223936Sjhb continue; 456223936Sjhb percentages(CPUSTATES, &pcpu_cpu_states[j * CPUSTATES], 457223936Sjhb &pcpu_cp_time[j * CPUSTATES], 458223936Sjhb &pcpu_cp_old[j * CPUSTATES], 459223936Sjhb &pcpu_cp_diff[j * CPUSTATES]); 460223936Sjhb j++; 461175420Speter } 462223936Sjhb percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff); 46324143Sjoerg 464131623Sdes /* sum memory & swap statistics */ 465131623Sdes { 466131623Sdes static unsigned int swap_delay = 0; 467131623Sdes static int swapavail = 0; 468131623Sdes static int swapfree = 0; 469189626Sjhb static long bufspace = 0; 470131623Sdes static int nspgsin, nspgsout; 47124143Sjoerg 472131623Sdes GETSYSCTL("vfs.bufspace", bufspace); 473131623Sdes GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]); 474131623Sdes GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]); 475131623Sdes GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]); 476131623Sdes GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]); 477131623Sdes GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]); 478131623Sdes GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin); 479131623Sdes GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout); 480131623Sdes /* convert memory stats to Kbytes */ 481131623Sdes memory_stats[0] = pagetok(memory_stats[0]); 482131623Sdes memory_stats[1] = pagetok(memory_stats[1]); 483131623Sdes memory_stats[2] = pagetok(memory_stats[2]); 484131623Sdes memory_stats[3] = pagetok(memory_stats[3]); 485131623Sdes memory_stats[4] = bufspace / 1024; 486131623Sdes memory_stats[5] = pagetok(memory_stats[5]); 487131623Sdes memory_stats[6] = -1; 48824143Sjoerg 489131623Sdes /* first interval */ 490131623Sdes if (swappgsin < 0) { 491131623Sdes swap_stats[4] = 0; 492131623Sdes swap_stats[5] = 0; 493131623Sdes } 49424143Sjoerg 495131623Sdes /* compute differences between old and new swap statistic */ 496131623Sdes else { 497131623Sdes swap_stats[4] = pagetok(((nspgsin - swappgsin))); 498131623Sdes swap_stats[5] = pagetok(((nspgsout - swappgsout))); 499131623Sdes } 50024143Sjoerg 501131623Sdes swappgsin = nspgsin; 502131623Sdes swappgsout = nspgsout; 50324143Sjoerg 504131623Sdes /* call CPU heavy swapmode() only for changes */ 505131623Sdes if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) { 506131623Sdes swap_stats[3] = swapmode(&swapavail, &swapfree); 507131623Sdes swap_stats[0] = swapavail; 508131623Sdes swap_stats[1] = swapavail - swapfree; 509131623Sdes swap_stats[2] = swapfree; 510131623Sdes } 511131623Sdes swap_delay = 1; 512131623Sdes swap_stats[6] = -1; 51324143Sjoerg } 51424143Sjoerg 515237656Sjhb if (arc_enabled) { 516237656Sjhb GETSYSCTL("kstat.zfs.misc.arcstats.size", arc_stat); 517237656Sjhb arc_stats[0] = arc_stat >> 10; 518237656Sjhb GETSYSCTL("vfs.zfs.mfu_size", arc_stat); 519237656Sjhb arc_stats[1] = arc_stat >> 10; 520237656Sjhb GETSYSCTL("vfs.zfs.mru_size", arc_stat); 521237656Sjhb arc_stats[2] = arc_stat >> 10; 522237656Sjhb GETSYSCTL("vfs.zfs.anon_size", arc_stat); 523237656Sjhb arc_stats[3] = arc_stat >> 10; 524237656Sjhb GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc_stat); 525237656Sjhb GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc_stat2); 526237656Sjhb arc_stats[4] = arc_stat + arc_stat2 >> 10; 527237656Sjhb GETSYSCTL("kstat.zfs.misc.arcstats.other_size", arc_stat); 528237656Sjhb arc_stats[5] = arc_stat >> 10; 529237656Sjhb si->arc = arc_stats; 530237656Sjhb } 531237656Sjhb 532131623Sdes /* set arrays and strings */ 533175420Speter if (pcpu_stats) { 534175420Speter si->cpustates = pcpu_cpu_states; 535175420Speter si->ncpus = ncpus; 536175420Speter } else { 537175420Speter si->cpustates = cpu_states; 538175420Speter si->ncpus = 1; 539175420Speter } 540131623Sdes si->memory = memory_stats; 541131623Sdes si->swap = swap_stats; 54224143Sjoerg 54324143Sjoerg 544131623Sdes if (lastpid > 0) { 545131623Sdes si->last_pid = lastpid; 546131623Sdes } else { 547131623Sdes si->last_pid = -1; 548131623Sdes } 54942447Sobrien 550131623Sdes /* 551131623Sdes * Print how long system has been up. 552131623Sdes * (Found by looking getting "boottime" from the kernel) 553131623Sdes */ 554131623Sdes mib[0] = CTL_KERN; 555131623Sdes mib[1] = KERN_BOOTTIME; 556237656Sjhb size = sizeof(boottime); 557237656Sjhb if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && 558131623Sdes boottime.tv_sec != 0) { 559131623Sdes si->boottime = boottime; 560131623Sdes } else { 561131623Sdes si->boottime.tv_sec = -1; 562131623Sdes } 56324143Sjoerg} 56424143Sjoerg 565132015Salfred#define NOPROC ((void *)-1) 566132015Salfred 567132955Salfred/* 568132955Salfred * We need to compare data from the old process entry with the new 569132955Salfred * process entry. 570132955Salfred * To facilitate doing this quickly we stash a pointer in the kinfo_proc 571132955Salfred * structure to cache the mapping. We also use a negative cache pointer 572132955Salfred * of NOPROC to avoid duplicate lookups. 573132955Salfred * XXX: this could be done when the actual processes are fetched, we do 574132955Salfred * it here out of laziness. 575132955Salfred */ 576131402Salfredconst struct kinfo_proc * 577131402Salfredget_old_proc(struct kinfo_proc *pp) 578131402Salfred{ 579131402Salfred struct kinfo_proc **oldpp, *oldp; 580131402Salfred 581132955Salfred /* 582132955Salfred * If this is the first fetch of the kinfo_procs then we don't have 583132955Salfred * any previous entries. 584132955Salfred */ 585131402Salfred if (previous_proc_count == 0) 586131402Salfred return (NULL); 587132955Salfred /* negative cache? */ 588132015Salfred if (pp->ki_udata == NOPROC) 589132015Salfred return (NULL); 590132955Salfred /* cached? */ 591132015Salfred if (pp->ki_udata != NULL) 592132015Salfred return (pp->ki_udata); 593132955Salfred /* 594132955Salfred * Not cached, 595132955Salfred * 1) look up based on pid. 596132955Salfred * 2) compare process start. 597132955Salfred * If we fail here, then setup a negative cache entry, otherwise 598132955Salfred * cache it. 599132955Salfred */ 600131402Salfred oldpp = bsearch(&pp, previous_pref, previous_proc_count, 601224188Sjhb sizeof(*previous_pref), ps.thread ? compare_tid : compare_pid); 602132015Salfred if (oldpp == NULL) { 603132015Salfred pp->ki_udata = NOPROC; 604131402Salfred return (NULL); 605132015Salfred } 606131402Salfred oldp = *oldpp; 607132015Salfred if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) { 608132015Salfred pp->ki_udata = NOPROC; 609131402Salfred return (NULL); 610132015Salfred } 611132015Salfred pp->ki_udata = oldp; 612131402Salfred return (oldp); 613131402Salfred} 614131402Salfred 615132955Salfred/* 616132955Salfred * Return the total amount of IO done in blocks in/out and faults. 617132955Salfred * store the values individually in the pointers passed in. 618132955Salfred */ 619131402Salfredlong 620158280Sbdeget_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp, 621158280Sbde long *vcsw, long *ivcsw) 622131402Salfred{ 623131402Salfred const struct kinfo_proc *oldp; 624131402Salfred static struct kinfo_proc dummy; 625131402Salfred long ret; 626131402Salfred 627131402Salfred oldp = get_old_proc(pp); 628131402Salfred if (oldp == NULL) { 629131402Salfred bzero(&dummy, sizeof(dummy)); 630131402Salfred oldp = &dummy; 631131402Salfred } 632131412Salfred *inp = RU(pp)->ru_inblock - RU(oldp)->ru_inblock; 633131412Salfred *oup = RU(pp)->ru_oublock - RU(oldp)->ru_oublock; 634131412Salfred *flp = RU(pp)->ru_majflt - RU(oldp)->ru_majflt; 635133817Salfred *vcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw; 636133817Salfred *ivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw; 637131402Salfred ret = 638131402Salfred (RU(pp)->ru_inblock - RU(oldp)->ru_inblock) + 639131402Salfred (RU(pp)->ru_oublock - RU(oldp)->ru_oublock) + 640131402Salfred (RU(pp)->ru_majflt - RU(oldp)->ru_majflt); 641131402Salfred return (ret); 642131402Salfred} 643131402Salfred 644132955Salfred/* 645132955Salfred * Return the total number of block in/out and faults by a process. 646132955Salfred */ 647131412Salfredlong 648131412Salfredget_io_total(struct kinfo_proc *pp) 649131412Salfred{ 650131412Salfred long dummy; 651131412Salfred 652133817Salfred return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy)); 653131412Salfred} 654131412Salfred 65524143Sjoergstatic struct handle handle; 65624143Sjoerg 657131310Salfredcaddr_t 658131622Sdesget_process_info(struct system_info *si, struct process_select *sel, 659131626Sdes int (*compare)(const void *, const void *)) 66024143Sjoerg{ 661131623Sdes int i; 662131623Sdes int total_procs; 663131623Sdes long p_io; 664133817Salfred long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw; 665131623Sdes int active_procs; 666131623Sdes struct kinfo_proc **prefp; 667131623Sdes struct kinfo_proc *pp; 66824143Sjoerg 669131623Sdes /* these are copied out of sel for speed */ 670131623Sdes int show_idle; 671131623Sdes int show_self; 672131623Sdes int show_system; 673131623Sdes int show_uid; 674131623Sdes int show_command; 675222530Sjhb int show_kidle; 67624143Sjoerg 677131623Sdes /* 678131623Sdes * Save the previous process info. 679131623Sdes */ 680131623Sdes if (previous_proc_count_max < nproc) { 681131623Sdes free(previous_procs); 682131626Sdes previous_procs = malloc(nproc * sizeof(*previous_procs)); 683131623Sdes free(previous_pref); 684131626Sdes previous_pref = malloc(nproc * sizeof(*previous_pref)); 685131623Sdes if (previous_procs == NULL || previous_pref == NULL) { 686131623Sdes (void) fprintf(stderr, "top: Out of memory.\n"); 687131623Sdes quit(23); 688131623Sdes } 689131623Sdes previous_proc_count_max = nproc; 690131623Sdes } 691131623Sdes if (nproc) { 692131623Sdes for (i = 0; i < nproc; i++) 693131623Sdes previous_pref[i] = &previous_procs[i]; 694131626Sdes bcopy(pbase, previous_procs, nproc * sizeof(*previous_procs)); 695158280Sbde qsort(previous_pref, nproc, sizeof(*previous_pref), 696224188Sjhb ps.thread ? compare_tid : compare_pid); 697131623Sdes } 698131623Sdes previous_proc_count = nproc; 699131402Salfred 700224062Sjhb pbase = kvm_getprocs(kd, sel->thread ? KERN_PROC_ALL : KERN_PROC_PROC, 701224062Sjhb 0, &nproc); 702131623Sdes if (nproc > onproc) 703131626Sdes pref = realloc(pref, sizeof(*pref) * (onproc = nproc)); 704131623Sdes if (pref == NULL || pbase == NULL) { 705131623Sdes (void) fprintf(stderr, "top: Out of memory.\n"); 706131623Sdes quit(23); 707131623Sdes } 708131623Sdes /* get a pointer to the states summary array */ 709131623Sdes si->procstates = process_states; 71024143Sjoerg 711131623Sdes /* set up flags which define what we are going to select */ 712131623Sdes show_idle = sel->idle; 713132024Sdes show_self = sel->self == -1; 714131623Sdes show_system = sel->system; 715131623Sdes show_uid = sel->uid != -1; 716131623Sdes show_command = sel->command != NULL; 717222530Sjhb show_kidle = sel->kidle; 71824143Sjoerg 719131623Sdes /* count up process states and get pointers to interesting procs */ 720131623Sdes total_procs = 0; 721131623Sdes active_procs = 0; 722131623Sdes total_inblock = 0; 723131623Sdes total_oublock = 0; 724131623Sdes total_majflt = 0; 725131623Sdes memset((char *)process_states, 0, sizeof(process_states)); 726131623Sdes prefp = pref; 727131626Sdes for (pp = pbase, i = 0; i < nproc; pp++, i++) { 728131619Sdes 729131623Sdes if (pp->ki_stat == 0) 730132024Sdes /* not in use */ 731132024Sdes continue; 732131619Sdes 733131623Sdes if (!show_self && pp->ki_pid == sel->self) 734131623Sdes /* skip self */ 735131623Sdes continue; 736131619Sdes 737131623Sdes if (!show_system && (pp->ki_flag & P_SYSTEM)) 738131623Sdes /* skip system process */ 739131623Sdes continue; 740131619Sdes 741158280Sbde p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt, 742158280Sbde &p_vcsw, &p_ivcsw); 743131623Sdes total_inblock += p_inblock; 744131623Sdes total_oublock += p_oublock; 745131623Sdes total_majflt += p_majflt; 746131623Sdes total_procs++; 747131626Sdes process_states[pp->ki_stat]++; 748131619Sdes 749131623Sdes if (pp->ki_stat == SZOMB) 750131623Sdes /* skip zombies */ 751131623Sdes continue; 752131619Sdes 753223841Sjhb if (!show_kidle && pp->ki_tdflags & TDF_IDLETD) 754223841Sjhb /* skip kernel idle process */ 755223841Sjhb continue; 756223841Sjhb 757131623Sdes if (displaymode == DISP_CPU && !show_idle && 758159520Sse (pp->ki_pctcpu == 0 || 759159520Sse pp->ki_stat == SSTOP || pp->ki_stat == SIDL)) 760131623Sdes /* skip idle or non-running processes */ 761131623Sdes continue; 762131619Sdes 763131623Sdes if (displaymode == DISP_IO && !show_idle && p_io == 0) 764131623Sdes /* skip processes that aren't doing I/O */ 765131623Sdes continue; 766131619Sdes 767131623Sdes if (show_uid && pp->ki_ruid != (uid_t)sel->uid) 768158280Sbde /* skip proc. that don't belong to the selected UID */ 769131623Sdes continue; 770131619Sdes 771224062Sjhb *prefp++ = pp; 772224062Sjhb active_procs++; 773131623Sdes } 774131623Sdes 775131623Sdes /* if requested, sort the "interesting" processes */ 776131623Sdes if (compare != NULL) 777131626Sdes qsort(pref, active_procs, sizeof(*pref), compare); 77824143Sjoerg 779131623Sdes /* remember active and total counts */ 780131623Sdes si->p_total = total_procs; 781131623Sdes si->p_active = pref_len = active_procs; 78224143Sjoerg 783131623Sdes /* pass back a handle */ 784131623Sdes handle.next_proc = pref; 785131623Sdes handle.remaining = active_procs; 786131623Sdes return ((caddr_t)&handle); 78724143Sjoerg} 78824143Sjoerg 789131626Sdesstatic char fmt[128]; /* static area where result is built */ 79024143Sjoerg 791131310Salfredchar * 792168710Sstasformat_next_process(caddr_t handle, char *(*get_userid)(int), int flags) 79324143Sjoerg{ 794131623Sdes struct kinfo_proc *pp; 795131623Sdes const struct kinfo_proc *oldp; 796131623Sdes long cputime; 797131623Sdes double pct; 798131623Sdes struct handle *hp; 799131623Sdes char status[16]; 800131623Sdes int state; 801131623Sdes struct rusage ru, *rup; 802131623Sdes long p_tot, s_tot; 803168799Srafan char *proc_fmt, thr_buf[6], jid_buf[6]; 804168710Sstas char *cmdbuf = NULL; 805168710Sstas char **args; 80624143Sjoerg 807131623Sdes /* find and remember the next proc structure */ 808131623Sdes hp = (struct handle *)handle; 809131623Sdes pp = *(hp->next_proc++); 810131623Sdes hp->remaining--; 811131620Sdes 812131623Sdes /* get the process's command name */ 813172207Sjeff if ((pp->ki_flag & P_INMEM) == 0) { 814131623Sdes /* 815131623Sdes * Print swapped processes as <pname> 816131623Sdes */ 817158280Sbde size_t len; 818158280Sbde 819158280Sbde len = strlen(pp->ki_comm); 820131626Sdes if (len > sizeof(pp->ki_comm) - 3) 821131626Sdes len = sizeof(pp->ki_comm) - 3; 822131626Sdes memmove(pp->ki_comm + 1, pp->ki_comm, len); 823131626Sdes pp->ki_comm[0] = '<'; 824131626Sdes pp->ki_comm[len + 1] = '>'; 825131626Sdes pp->ki_comm[len + 2] = '\0'; 826131623Sdes } 827131623Sdes 82824143Sjoerg /* 829131623Sdes * Convert the process's runtime from microseconds to seconds. This 830131623Sdes * time includes the interrupt time although that is not wanted here. 831131623Sdes * ps(1) is similarly sloppy. 83224143Sjoerg */ 833131623Sdes cputime = (pp->ki_runtime + 500000) / 1000000; 83424143Sjoerg 835131623Sdes /* calculate the base for cpu percentages */ 836131623Sdes pct = pctdouble(pp->ki_pctcpu); 83724143Sjoerg 838131623Sdes /* generate "STATE" field */ 839131623Sdes switch (state = pp->ki_stat) { 84024143Sjoerg case SRUN: 841131623Sdes if (smpmode && pp->ki_oncpu != 0xff) 842131623Sdes sprintf(status, "CPU%d", pp->ki_oncpu); 843131623Sdes else 844131623Sdes strcpy(status, "RUN"); 845131623Sdes break; 846104388Sjhb case SLOCK: 847131623Sdes if (pp->ki_kiflag & KI_LOCKBLOCK) { 848131623Sdes sprintf(status, "*%.6s", pp->ki_lockname); 849131623Sdes break; 850131623Sdes } 851131623Sdes /* fall through */ 85224143Sjoerg case SSLEEP: 853131623Sdes if (pp->ki_wmesg != NULL) { 854131623Sdes sprintf(status, "%.6s", pp->ki_wmesg); 855131623Sdes break; 856131623Sdes } 857131623Sdes /* FALLTHROUGH */ 85824143Sjoerg default: 85943720Sfenner 860131623Sdes if (state >= 0 && 861131623Sdes state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 862131626Sdes sprintf(status, "%.6s", state_abbrev[state]); 863131623Sdes else 864131623Sdes sprintf(status, "?%5d", state); 865131623Sdes break; 866131623Sdes } 86724143Sjoerg 868168710Sstas cmdbuf = (char *)malloc(cmdlengthdelta + 1); 869168710Sstas if (cmdbuf == NULL) { 870168710Sstas warn("malloc(%d)", cmdlengthdelta + 1); 871168710Sstas return NULL; 872168710Sstas } 873168710Sstas 874168710Sstas if (!(flags & FMT_SHOWARGS)) { 875173004Sjulian if (ps.thread && pp->ki_flag & P_HADTHREADS && 876224199Sbz pp->ki_tdname[0]) { 877224202Sbz snprintf(cmdbuf, cmdlengthdelta, "%s{%s}", pp->ki_comm, 878224202Sbz pp->ki_tdname); 879173004Sjulian } else { 880173004Sjulian snprintf(cmdbuf, cmdlengthdelta, "%s", pp->ki_comm); 881168710Sstas } 882173004Sjulian } else { 883173004Sjulian if (pp->ki_flag & P_SYSTEM || 884173004Sjulian pp->ki_args == NULL || 885173004Sjulian (args = kvm_getargv(kd, pp, cmdlengthdelta)) == NULL || 886173004Sjulian !(*args)) { 887173004Sjulian if (ps.thread && pp->ki_flag & P_HADTHREADS && 888224199Sbz pp->ki_tdname[0]) { 889173004Sjulian snprintf(cmdbuf, cmdlengthdelta, 890224202Sbz "[%s{%s}]", pp->ki_comm, pp->ki_tdname); 891173004Sjulian } else { 892173004Sjulian snprintf(cmdbuf, cmdlengthdelta, 893173004Sjulian "[%s]", pp->ki_comm); 894173004Sjulian } 895173004Sjulian } else { 896173004Sjulian char *src, *dst, *argbuf; 897173004Sjulian char *cmd; 898173004Sjulian size_t argbuflen; 899173004Sjulian size_t len; 900168710Sstas 901173004Sjulian argbuflen = cmdlengthdelta * 4; 902173004Sjulian argbuf = (char *)malloc(argbuflen + 1); 903173004Sjulian if (argbuf == NULL) { 904173004Sjulian warn("malloc(%d)", argbuflen + 1); 905173004Sjulian free(cmdbuf); 906173004Sjulian return NULL; 907173004Sjulian } 908168710Sstas 909173004Sjulian dst = argbuf; 910168710Sstas 911173004Sjulian /* Extract cmd name from argv */ 912173004Sjulian cmd = strrchr(*args, '/'); 913173004Sjulian if (cmd == NULL) 914173004Sjulian cmd = *args; 915173004Sjulian else 916173004Sjulian cmd++; 917168710Sstas 918173004Sjulian for (; (src = *args++) != NULL; ) { 919173004Sjulian if (*src == '\0') 920173004Sjulian continue; 921173004Sjulian len = (argbuflen - (dst - argbuf) - 1) / 4; 922173004Sjulian strvisx(dst, src, 923173004Sjulian strlen(src) < len ? strlen(src) : len, 924173004Sjulian VIS_NL | VIS_CSTYLE); 925173004Sjulian while (*dst != '\0') 926173004Sjulian dst++; 927173004Sjulian if ((argbuflen - (dst - argbuf) - 1) / 4 > 0) 928173004Sjulian *dst++ = ' '; /* add delimiting space */ 929173004Sjulian } 930173004Sjulian if (dst != argbuf && dst[-1] == ' ') 931173004Sjulian dst--; 932173004Sjulian *dst = '\0'; 933168710Sstas 934224202Sbz if (strcmp(cmd, pp->ki_comm) != 0 ) { 935224202Sbz if (ps.thread && pp->ki_flag & P_HADTHREADS && 936224202Sbz pp->ki_tdname[0]) 937224202Sbz snprintf(cmdbuf, cmdlengthdelta, 938224202Sbz "%s (%s){%s}", argbuf, pp->ki_comm, 939224202Sbz pp->ki_tdname); 940224202Sbz else 941224202Sbz snprintf(cmdbuf, cmdlengthdelta, 942224202Sbz "%s (%s)", argbuf, pp->ki_comm); 943224202Sbz } else { 944224202Sbz if (ps.thread && pp->ki_flag & P_HADTHREADS && 945224202Sbz pp->ki_tdname[0]) 946224202Sbz snprintf(cmdbuf, cmdlengthdelta, 947224202Sbz "%s{%s}", argbuf, pp->ki_tdname); 948224202Sbz else 949224202Sbz strlcpy(cmdbuf, argbuf, cmdlengthdelta); 950224202Sbz } 951173004Sjulian free(argbuf); 952173004Sjulian } 953168710Sstas } 954168710Sstas 955168799Srafan if (ps.jail == 0) 956168799Srafan jid_buf[0] = '\0'; 957168799Srafan else 958168799Srafan snprintf(jid_buf, sizeof(jid_buf), " %*d", 959168799Srafan sizeof(jid_buf) - 3, pp->ki_jid); 960168799Srafan 961131623Sdes if (displaymode == DISP_IO) { 962131623Sdes oldp = get_old_proc(pp); 963131623Sdes if (oldp != NULL) { 964158280Sbde ru.ru_inblock = RU(pp)->ru_inblock - 965158280Sbde RU(oldp)->ru_inblock; 966158280Sbde ru.ru_oublock = RU(pp)->ru_oublock - 967158280Sbde RU(oldp)->ru_oublock; 968131623Sdes ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt; 969133817Salfred ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw; 970133817Salfred ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw; 971131623Sdes rup = &ru; 972131623Sdes } else { 973131623Sdes rup = RU(pp); 974131623Sdes } 975131623Sdes p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt; 976131623Sdes s_tot = total_inblock + total_oublock + total_majflt; 977131402Salfred 978234416Skib snprintf(fmt, sizeof(fmt), io_Proc_format, 979131623Sdes pp->ki_pid, 980168799Srafan jid_buf, 981158280Sbde namelength, namelength, (*get_userid)(pp->ki_ruid), 982133817Salfred rup->ru_nvcsw, 983133817Salfred rup->ru_nivcsw, 984131626Sdes rup->ru_inblock, 985131626Sdes rup->ru_oublock, 986131626Sdes rup->ru_majflt, 987131626Sdes p_tot, 988131626Sdes s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot), 989131623Sdes screen_width > cmdlengthdelta ? 990131623Sdes screen_width - cmdlengthdelta : 0, 991168710Sstas printable(cmdbuf)); 992168710Sstas 993168710Sstas free(cmdbuf); 994168710Sstas 995131623Sdes return (fmt); 996131623Sdes } 997145073Skeramida 998131623Sdes /* format this entry */ 999145073Skeramida proc_fmt = smpmode ? smp_Proc_format : up_Proc_format; 1000145073Skeramida if (ps.thread != 0) 1001145073Skeramida thr_buf[0] = '\0'; 1002145073Skeramida else 1003145073Skeramida snprintf(thr_buf, sizeof(thr_buf), "%*d ", 1004145073Skeramida sizeof(thr_buf) - 2, pp->ki_numthreads); 1005145073Skeramida 1006234416Skib snprintf(fmt, sizeof(fmt), proc_fmt, 100769896Smckusick pp->ki_pid, 1008168799Srafan jid_buf, 1009158280Sbde namelength, namelength, (*get_userid)(pp->ki_ruid), 1010145073Skeramida thr_buf, 101172377Sjake pp->ki_pri.pri_level - PZERO, 1012158282Sbde format_nice(pp), 101337100Sdt format_k2(PROCSIZE(pp)), 101469896Smckusick format_k2(pagetok(pp->ki_rssize)), 101524143Sjoerg status, 101669896Smckusick smpmode ? pp->ki_lastcpu : 0, 101724143Sjoerg format_time(cputime), 1018146342Skeramida ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct, 1019158280Sbde screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0, 1020168710Sstas printable(cmdbuf)); 102124143Sjoerg 1022168710Sstas free(cmdbuf); 1023168710Sstas 1024131623Sdes /* return the result */ 1025131623Sdes return (fmt); 102624143Sjoerg} 102724143Sjoerg 1028131310Salfredstatic void 1029158282Sbdegetsysctl(const char *name, void *ptr, size_t len) 103024143Sjoerg{ 1031131623Sdes size_t nlen = len; 1032131310Salfred 1033131623Sdes if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 1034131623Sdes fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 1035131623Sdes strerror(errno)); 1036131623Sdes quit(23); 1037131623Sdes } 1038131623Sdes if (nlen != len) { 1039158280Sbde fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", 1040158280Sbde name, (unsigned long)len, (unsigned long)nlen); 1041131623Sdes quit(23); 1042131623Sdes } 104324143Sjoerg} 104472951Srwatson 1045164058Sbdestatic const char * 1046164058Sbdeformat_nice(const struct kinfo_proc *pp) 1047158282Sbde{ 1048164058Sbde const char *fifo, *kthread; 1049164058Sbde int rtpri; 1050164058Sbde static char nicebuf[4 + 1]; 1051158282Sbde 1052164058Sbde fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F"; 1053164058Sbde kthread = (pp->ki_flag & P_KTHREAD) ? "k" : ""; 1054164058Sbde switch (PRI_BASE(pp->ki_pri.pri_class)) { 1055164058Sbde case PRI_ITHD: 1056164058Sbde return ("-"); 1057164058Sbde case PRI_REALTIME: 1058170774Sbde /* 1059170774Sbde * XXX: the kernel doesn't tell us the original rtprio and 1060170774Sbde * doesn't really know what it was, so to recover it we 1061170774Sbde * must be more chummy with the implementation than the 1062170774Sbde * implementation is with itself. pri_user gives a 1063170774Sbde * constant "base" priority, but is only initialized 1064170774Sbde * properly for user threads. pri_native gives what the 1065170774Sbde * kernel calls the "base" priority, but it isn't constant 1066170774Sbde * since it is changed by priority propagation. pri_native 1067170774Sbde * also isn't properly initialized for all threads, but it 1068170774Sbde * is properly initialized for kernel realtime and idletime 1069170774Sbde * threads. Thus we use pri_user for the base priority of 1070170774Sbde * user threads (it is always correct) and pri_native for 1071170774Sbde * the base priority of kernel realtime and idletime threads 1072170774Sbde * (there is nothing better, and it is usually correct). 1073170774Sbde * 1074170774Sbde * The field width and thus the buffer are too small for 1075170774Sbde * values like "kr31F", but such values shouldn't occur, 1076170774Sbde * and if they do then the tailing "F" is not displayed. 1077170774Sbde */ 1078170774Sbde rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1079170774Sbde pp->ki_pri.pri_user) - PRI_MIN_REALTIME; 1080164058Sbde snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s", 1081164058Sbde kthread, rtpri, fifo); 1082164058Sbde break; 1083164058Sbde case PRI_TIMESHARE: 1084164058Sbde if (pp->ki_flag & P_KTHREAD) 1085164058Sbde return ("-"); 1086164058Sbde snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO); 1087164058Sbde break; 1088164058Sbde case PRI_IDLE: 1089170774Sbde /* XXX: as above. */ 1090170774Sbde rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1091170774Sbde pp->ki_pri.pri_user) - PRI_MIN_IDLE; 1092164058Sbde snprintf(nicebuf, sizeof(nicebuf), "%si%d%s", 1093164058Sbde kthread, rtpri, fifo); 1094164058Sbde break; 1095164058Sbde default: 1096164058Sbde return ("?"); 1097164058Sbde } 1098158282Sbde return (nicebuf); 1099158282Sbde} 1100158282Sbde 110138278Swosch/* comparison routines for qsort */ 110224143Sjoerg 1103158282Sbdestatic int 1104131622Sdescompare_pid(const void *p1, const void *p2) 1105131402Salfred{ 1106131623Sdes const struct kinfo_proc * const *pp1 = p1; 1107131623Sdes const struct kinfo_proc * const *pp2 = p2; 1108131402Salfred 1109131623Sdes if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0) 1110131623Sdes abort(); 1111131402Salfred 1112131623Sdes return ((*pp1)->ki_pid - (*pp2)->ki_pid); 1113131402Salfred} 1114131402Salfred 1115224188Sjhbstatic int 1116224188Sjhbcompare_tid(const void *p1, const void *p2) 1117224188Sjhb{ 1118224188Sjhb const struct kinfo_proc * const *pp1 = p1; 1119224188Sjhb const struct kinfo_proc * const *pp2 = p2; 1120224188Sjhb 1121224188Sjhb if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0) 1122224188Sjhb abort(); 1123224188Sjhb 1124224188Sjhb return ((*pp1)->ki_tid - (*pp2)->ki_tid); 1125224188Sjhb} 1126224188Sjhb 112724143Sjoerg/* 112824143Sjoerg * proc_compare - comparison function for "qsort" 112924143Sjoerg * Compares the resource consumption of two processes using five 1130131620Sdes * distinct keys. The keys (in descending order of importance) are: 1131131620Sdes * percent cpu, cpu ticks, state, resident set size, total virtual 1132131620Sdes * memory usage. The process states are ordered as follows (from least 1133131620Sdes * to most important): WAIT, zombie, sleep, stop, start, run. The 1134131620Sdes * array declaration below maps a process state index into a number 1135131620Sdes * that reflects this ordering. 113624143Sjoerg */ 113724143Sjoerg 1138158280Sbdestatic int sorted_state[] = { 1139131623Sdes 0, /* not used */ 1140131623Sdes 3, /* sleep */ 1141131623Sdes 1, /* ABANDONED (WAIT) */ 1142131623Sdes 6, /* run */ 1143131623Sdes 5, /* start */ 1144131623Sdes 2, /* zombie */ 1145131623Sdes 4 /* stop */ 114624143Sjoerg}; 114738278Swosch 1148131620Sdes 1149146343Skeramida#define ORDERKEY_PCTCPU(a, b) do { \ 1150146343Skeramida long diff; \ 1151146343Skeramida if (ps.wcpu) \ 1152158280Sbde diff = floor(1.0E6 * weighted_cpu(pctdouble((b)->ki_pctcpu), \ 1153158280Sbde (b))) - \ 1154158280Sbde floor(1.0E6 * weighted_cpu(pctdouble((a)->ki_pctcpu), \ 1155158280Sbde (a))); \ 1156146343Skeramida else \ 1157146343Skeramida diff = (long)(b)->ki_pctcpu - (long)(a)->ki_pctcpu; \ 1158146343Skeramida if (diff != 0) \ 1159146343Skeramida return (diff > 0 ? 1 : -1); \ 1160131626Sdes} while (0) 116138278Swosch 1162131626Sdes#define ORDERKEY_CPTICKS(a, b) do { \ 1163131628Sdes int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \ 1164131626Sdes if (diff != 0) \ 1165131626Sdes return (diff > 0 ? 1 : -1); \ 1166131626Sdes} while (0) 116738278Swosch 1168131626Sdes#define ORDERKEY_STATE(a, b) do { \ 1169131626Sdes int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \ 1170131626Sdes if (diff != 0) \ 1171131626Sdes return (diff > 0 ? 1 : -1); \ 1172131626Sdes} while (0) 117338278Swosch 1174131626Sdes#define ORDERKEY_PRIO(a, b) do { \ 1175131628Sdes int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \ 1176131626Sdes if (diff != 0) \ 1177131626Sdes return (diff > 0 ? 1 : -1); \ 1178131626Sdes} while (0) 117938278Swosch 1180145073Skeramida#define ORDERKEY_THREADS(a, b) do { \ 1181145073Skeramida int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \ 1182145073Skeramida if (diff != 0) \ 1183145073Skeramida return (diff > 0 ? 1 : -1); \ 1184145073Skeramida} while (0) 1185145073Skeramida 1186131626Sdes#define ORDERKEY_RSSIZE(a, b) do { \ 1187131628Sdes long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \ 1188131626Sdes if (diff != 0) \ 1189131626Sdes return (diff > 0 ? 1 : -1); \ 1190131626Sdes} while (0) 119138278Swosch 1192131626Sdes#define ORDERKEY_MEM(a, b) do { \ 1193131628Sdes long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \ 1194131626Sdes if (diff != 0) \ 1195131626Sdes return (diff > 0 ? 1 : -1); \ 1196131626Sdes} while (0) 119738278Swosch 1198168799Srafan#define ORDERKEY_JID(a, b) do { \ 1199168799Srafan int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \ 1200168799Srafan if (diff != 0) \ 1201168799Srafan return (diff > 0 ? 1 : -1); \ 1202168799Srafan} while (0) 1203168799Srafan 120438278Swosch/* compare_cpu - the comparison function for sorting by cpu percentage */ 120538278Swosch 120624143Sjoergint 120738278Swosch#ifdef ORDER 1208131626Sdescompare_cpu(void *arg1, void *arg2) 120938278Swosch#else 1210131626Sdesproc_compare(void *arg1, void *arg2) 121138278Swosch#endif 121224143Sjoerg{ 1213131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1214131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 121524143Sjoerg 1216131626Sdes ORDERKEY_PCTCPU(p1, p2); 1217131626Sdes ORDERKEY_CPTICKS(p1, p2); 1218131626Sdes ORDERKEY_STATE(p1, p2); 1219131626Sdes ORDERKEY_PRIO(p1, p2); 1220131626Sdes ORDERKEY_RSSIZE(p1, p2); 1221131626Sdes ORDERKEY_MEM(p1, p2); 122224143Sjoerg 1223131626Sdes return (0); 122424143Sjoerg} 122524143Sjoerg 122638278Swosch#ifdef ORDER 1227158280Sbde/* "cpu" compare routines */ 1228158280Sbdeint compare_size(), compare_res(), compare_time(), compare_prio(), 1229158280Sbde compare_threads(); 123024143Sjoerg 1231158280Sbde/* 1232158280Sbde * "io" compare routines. Context switches aren't i/o, but are displayed 1233158280Sbde * on the "io" display. 1234158280Sbde */ 1235158280Sbdeint compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(), 1236158280Sbde compare_vcsw(), compare_ivcsw(); 1237158280Sbde 1238133817Salfredint (*compares[])() = { 1239131623Sdes compare_cpu, 1240131623Sdes compare_size, 1241131623Sdes compare_res, 1242131623Sdes compare_time, 1243131623Sdes compare_prio, 1244145073Skeramida compare_threads, 1245133817Salfred compare_iototal, 1246133817Salfred compare_ioread, 1247133817Salfred compare_iowrite, 1248133817Salfred compare_iofault, 1249133817Salfred compare_vcsw, 1250133817Salfred compare_ivcsw, 1251168799Srafan compare_jid, 1252131623Sdes NULL 125338278Swosch}; 125438278Swosch 125538278Swosch/* compare_size - the comparison function for sorting by total memory usage */ 125638278Swosch 125738278Swoschint 1258131626Sdescompare_size(void *arg1, void *arg2) 125938278Swosch{ 1260131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1261131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 126238278Swosch 1263131626Sdes ORDERKEY_MEM(p1, p2); 1264131626Sdes ORDERKEY_RSSIZE(p1, p2); 1265131626Sdes ORDERKEY_PCTCPU(p1, p2); 1266131626Sdes ORDERKEY_CPTICKS(p1, p2); 1267131626Sdes ORDERKEY_STATE(p1, p2); 1268131626Sdes ORDERKEY_PRIO(p1, p2); 126938278Swosch 1270131626Sdes return (0); 127138278Swosch} 127238278Swosch 127338278Swosch/* compare_res - the comparison function for sorting by resident set size */ 127438278Swosch 127538278Swoschint 1276131626Sdescompare_res(void *arg1, void *arg2) 127738278Swosch{ 1278131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1279131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 128038278Swosch 1281131626Sdes ORDERKEY_RSSIZE(p1, p2); 1282131626Sdes ORDERKEY_MEM(p1, p2); 1283131626Sdes ORDERKEY_PCTCPU(p1, p2); 1284131626Sdes ORDERKEY_CPTICKS(p1, p2); 1285131626Sdes ORDERKEY_STATE(p1, p2); 1286131626Sdes ORDERKEY_PRIO(p1, p2); 128738278Swosch 1288131626Sdes return (0); 128938278Swosch} 129038278Swosch 129138278Swosch/* compare_time - the comparison function for sorting by total cpu time */ 129238278Swosch 129338278Swoschint 1294131626Sdescompare_time(void *arg1, void *arg2) 1295131310Salfred{ 1296131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1297131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 129838278Swosch 1299131626Sdes ORDERKEY_CPTICKS(p1, p2); 1300131626Sdes ORDERKEY_PCTCPU(p1, p2); 1301131626Sdes ORDERKEY_STATE(p1, p2); 1302131626Sdes ORDERKEY_PRIO(p1, p2); 1303131626Sdes ORDERKEY_RSSIZE(p1, p2); 1304131626Sdes ORDERKEY_MEM(p1, p2); 130538278Swosch 1306131626Sdes return (0); 1307131623Sdes} 1308131310Salfred 1309131626Sdes/* compare_prio - the comparison function for sorting by priority */ 131038278Swosch 131138278Swoschint 1312131626Sdescompare_prio(void *arg1, void *arg2) 131338278Swosch{ 1314131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1315131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 131638278Swosch 1317131626Sdes ORDERKEY_PRIO(p1, p2); 1318131626Sdes ORDERKEY_CPTICKS(p1, p2); 1319131626Sdes ORDERKEY_PCTCPU(p1, p2); 1320131626Sdes ORDERKEY_STATE(p1, p2); 1321131626Sdes ORDERKEY_RSSIZE(p1, p2); 1322131626Sdes ORDERKEY_MEM(p1, p2); 132338278Swosch 1324131626Sdes return (0); 132538278Swosch} 1326145073Skeramida 1327145073Skeramida/* compare_threads - the comparison function for sorting by threads */ 1328145073Skeramidaint 1329145073Skeramidacompare_threads(void *arg1, void *arg2) 1330145073Skeramida{ 1331145073Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1332145073Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1333145073Skeramida 1334145073Skeramida ORDERKEY_THREADS(p1, p2); 1335145073Skeramida ORDERKEY_PCTCPU(p1, p2); 1336145073Skeramida ORDERKEY_CPTICKS(p1, p2); 1337145073Skeramida ORDERKEY_STATE(p1, p2); 1338145073Skeramida ORDERKEY_PRIO(p1, p2); 1339145073Skeramida ORDERKEY_RSSIZE(p1, p2); 1340145073Skeramida ORDERKEY_MEM(p1, p2); 1341145073Skeramida 1342145073Skeramida return (0); 1343145073Skeramida} 1344168799Srafan 1345168799Srafan/* compare_jid - the comparison function for sorting by jid */ 1346168799Srafanstatic int 1347168799Srafancompare_jid(const void *arg1, const void *arg2) 1348168799Srafan{ 1349168799Srafan struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1350168799Srafan struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1351168799Srafan 1352168799Srafan ORDERKEY_JID(p1, p2); 1353168799Srafan ORDERKEY_PCTCPU(p1, p2); 1354168799Srafan ORDERKEY_CPTICKS(p1, p2); 1355168799Srafan ORDERKEY_STATE(p1, p2); 1356168799Srafan ORDERKEY_PRIO(p1, p2); 1357168799Srafan ORDERKEY_RSSIZE(p1, p2); 1358168799Srafan ORDERKEY_MEM(p1, p2); 1359168799Srafan 1360168799Srafan return (0); 1361168799Srafan} 1362158280Sbde#endif /* ORDER */ 136338278Swosch 1364158280Sbde/* assorted comparison functions for sorting by i/o */ 1365131829Skeramida 1366131402Salfredint 1367131829Skeramida#ifdef ORDER 1368131829Skeramidacompare_iototal(void *arg1, void *arg2) 1369131829Skeramida#else 1370131626Sdesio_compare(void *arg1, void *arg2) 1371131829Skeramida#endif 1372131402Salfred{ 1373131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1374131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1375131402Salfred 1376131626Sdes return (get_io_total(p2) - get_io_total(p1)); 1377131402Salfred} 1378131829Skeramida 1379131829Skeramida#ifdef ORDER 1380131829Skeramidaint 1381131829Skeramidacompare_ioread(void *arg1, void *arg2) 1382131829Skeramida{ 1383131829Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1384131829Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1385131829Skeramida long dummy, inp1, inp2; 1386131829Skeramida 1387133817Salfred (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy); 1388133817Salfred (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy); 1389131829Skeramida 1390131829Skeramida return (inp2 - inp1); 1391131829Skeramida} 1392131829Skeramida 1393131829Skeramidaint 1394131829Skeramidacompare_iowrite(void *arg1, void *arg2) 1395131829Skeramida{ 1396131829Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1397131829Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1398131829Skeramida long dummy, oup1, oup2; 1399131829Skeramida 1400133817Salfred (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy); 1401133817Salfred (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy); 1402131829Skeramida 1403131829Skeramida return (oup2 - oup1); 1404131829Skeramida} 1405131829Skeramida 1406131829Skeramidaint 1407131829Skeramidacompare_iofault(void *arg1, void *arg2) 1408131829Skeramida{ 1409131829Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1410131829Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1411131829Skeramida long dummy, flp1, flp2; 1412131829Skeramida 1413133817Salfred (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy); 1414133817Salfred (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy); 1415131829Skeramida 1416131829Skeramida return (flp2 - flp1); 1417131829Skeramida} 1418131829Skeramida 1419133817Salfredint 1420133817Salfredcompare_vcsw(void *arg1, void *arg2) 1421133817Salfred{ 1422133817Salfred struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1423133817Salfred struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1424133817Salfred long dummy, flp1, flp2; 1425133817Salfred 1426133817Salfred (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy); 1427133817Salfred (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy); 1428133817Salfred 1429133817Salfred return (flp2 - flp1); 1430133817Salfred} 1431133817Salfred 1432133817Salfredint 1433133817Salfredcompare_ivcsw(void *arg1, void *arg2) 1434133817Salfred{ 1435133817Salfred struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1436133817Salfred struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1437133817Salfred long dummy, flp1, flp2; 1438133817Salfred 1439133817Salfred (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1); 1440133817Salfred (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2); 1441133817Salfred 1442133817Salfred return (flp2 - flp1); 1443133817Salfred} 1444131829Skeramida#endif /* ORDER */ 1445131829Skeramida 144624143Sjoerg/* 144724143Sjoerg * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 144824143Sjoerg * the process does not exist. 1449228992Suqs * It is EXTREMELY IMPORTANT that this function work correctly. 145024143Sjoerg * If top runs setuid root (as in SVR4), then this function 145124143Sjoerg * is the only thing that stands in the way of a serious 145224143Sjoerg * security problem. It validates requests for the "kill" 145324143Sjoerg * and "renice" commands. 145424143Sjoerg */ 145524143Sjoerg 1456131310Salfredint 1457131622Sdesproc_owner(int pid) 145824143Sjoerg{ 1459131623Sdes int cnt; 1460131623Sdes struct kinfo_proc **prefp; 1461131623Sdes struct kinfo_proc *pp; 146224143Sjoerg 1463131623Sdes prefp = pref; 1464131623Sdes cnt = pref_len; 1465131626Sdes while (--cnt >= 0) { 1466131623Sdes pp = *prefp++; 1467131623Sdes if (pp->ki_pid == (pid_t)pid) 1468131623Sdes return ((int)pp->ki_ruid); 146924143Sjoerg } 1470131623Sdes return (-1); 147124143Sjoerg} 147224143Sjoerg 1473158282Sbdestatic int 1474131622Sdesswapmode(int *retavail, int *retfree) 147524143Sjoerg{ 147643053Sdillon int n; 147743053Sdillon int pagesize = getpagesize(); 147843053Sdillon struct kvm_swap swapary[1]; 147924143Sjoerg 148043053Sdillon *retavail = 0; 148143053Sdillon *retfree = 0; 148224143Sjoerg 148343053Sdillon#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 148424143Sjoerg 148543053Sdillon n = kvm_getswapinfo(kd, swapary, 1, 0); 148643697Sdillon if (n < 0 || swapary[0].ksw_total == 0) 1487131623Sdes return (0); 148824143Sjoerg 148943053Sdillon *retavail = CONVERT(swapary[0].ksw_total); 149043053Sdillon *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 149124143Sjoerg 1492131626Sdes n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total); 1493131623Sdes return (n); 149443053Sdillon} 1495