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$ 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[] = 110251630Sjhb " PID%s %-*.*s THR PRI NICE SIZE RES STATE C TIME %7s COMMAND"; 11127340Speterstatic char smp_header[] = 112251630Sjhb " PID%s %-*.*s " "PRI NICE SIZE RES STATE C TIME %7s COMMAND"; 11324143Sjoerg 11427340Speter#define smp_Proc_format \ 115251630Sjhb "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s %2d%7s %6.2f%% %.*s" 11624143Sjoerg 117145073Skeramidastatic char up_header_thr[] = 118251630Sjhb " PID%s %-*.*s THR PRI NICE SIZE RES STATE TIME %7s COMMAND"; 11927340Speterstatic char up_header[] = 120251630Sjhb " PID%s %-*.*s " "PRI NICE SIZE RES STATE TIME %7s COMMAND"; 12124143Sjoerg 12227340Speter#define up_Proc_format \ 123251630Sjhb "%5d%s %-*.*s %s%3d %4s%7s %6s %-6.6s%.0d%7s %6.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[] = { 181241730Sjhb "K Total, ", "K MFU, ", "K MRU, ", "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", 228242887Srpaulo "jid", "pid", 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 789243262Srpaulostatic char fmt[512]; /* 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]; 800248167Sjhb int cpu, 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; 806243262Srpaulo const int cmdlen = 128; 80724143Sjoerg 808131623Sdes /* find and remember the next proc structure */ 809131623Sdes hp = (struct handle *)handle; 810131623Sdes pp = *(hp->next_proc++); 811131623Sdes hp->remaining--; 812131620Sdes 813131623Sdes /* get the process's command name */ 814172207Sjeff if ((pp->ki_flag & P_INMEM) == 0) { 815131623Sdes /* 816131623Sdes * Print swapped processes as <pname> 817131623Sdes */ 818158280Sbde size_t len; 819158280Sbde 820158280Sbde len = strlen(pp->ki_comm); 821131626Sdes if (len > sizeof(pp->ki_comm) - 3) 822131626Sdes len = sizeof(pp->ki_comm) - 3; 823131626Sdes memmove(pp->ki_comm + 1, pp->ki_comm, len); 824131626Sdes pp->ki_comm[0] = '<'; 825131626Sdes pp->ki_comm[len + 1] = '>'; 826131626Sdes pp->ki_comm[len + 2] = '\0'; 827131623Sdes } 828131623Sdes 82924143Sjoerg /* 830131623Sdes * Convert the process's runtime from microseconds to seconds. This 831131623Sdes * time includes the interrupt time although that is not wanted here. 832131623Sdes * ps(1) is similarly sloppy. 83324143Sjoerg */ 834131623Sdes cputime = (pp->ki_runtime + 500000) / 1000000; 83524143Sjoerg 836131623Sdes /* calculate the base for cpu percentages */ 837131623Sdes pct = pctdouble(pp->ki_pctcpu); 83824143Sjoerg 839131623Sdes /* generate "STATE" field */ 840131623Sdes switch (state = pp->ki_stat) { 84124143Sjoerg case SRUN: 842131623Sdes if (smpmode && pp->ki_oncpu != 0xff) 843131623Sdes sprintf(status, "CPU%d", pp->ki_oncpu); 844131623Sdes else 845131623Sdes strcpy(status, "RUN"); 846131623Sdes break; 847104388Sjhb case SLOCK: 848131623Sdes if (pp->ki_kiflag & KI_LOCKBLOCK) { 849131623Sdes sprintf(status, "*%.6s", pp->ki_lockname); 850131623Sdes break; 851131623Sdes } 852131623Sdes /* fall through */ 85324143Sjoerg case SSLEEP: 854131623Sdes if (pp->ki_wmesg != NULL) { 855131623Sdes sprintf(status, "%.6s", pp->ki_wmesg); 856131623Sdes break; 857131623Sdes } 858131623Sdes /* FALLTHROUGH */ 85924143Sjoerg default: 86043720Sfenner 861131623Sdes if (state >= 0 && 862131623Sdes state < sizeof(state_abbrev) / sizeof(*state_abbrev)) 863131626Sdes sprintf(status, "%.6s", state_abbrev[state]); 864131623Sdes else 865131623Sdes sprintf(status, "?%5d", state); 866131623Sdes break; 867131623Sdes } 86824143Sjoerg 869243262Srpaulo cmdbuf = (char *)malloc(cmdlen + 1); 870168710Sstas if (cmdbuf == NULL) { 871243262Srpaulo warn("malloc(%d)", cmdlen + 1); 872168710Sstas return NULL; 873168710Sstas } 874168710Sstas 875168710Sstas if (!(flags & FMT_SHOWARGS)) { 876173004Sjulian if (ps.thread && pp->ki_flag & P_HADTHREADS && 877224199Sbz pp->ki_tdname[0]) { 878243262Srpaulo snprintf(cmdbuf, cmdlen, "%s{%s}", pp->ki_comm, 879224202Sbz pp->ki_tdname); 880173004Sjulian } else { 881243262Srpaulo snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm); 882168710Sstas } 883173004Sjulian } else { 884173004Sjulian if (pp->ki_flag & P_SYSTEM || 885173004Sjulian pp->ki_args == NULL || 886243262Srpaulo (args = kvm_getargv(kd, pp, cmdlen)) == NULL || 887173004Sjulian !(*args)) { 888173004Sjulian if (ps.thread && pp->ki_flag & P_HADTHREADS && 889224199Sbz pp->ki_tdname[0]) { 890243262Srpaulo snprintf(cmdbuf, cmdlen, 891224202Sbz "[%s{%s}]", pp->ki_comm, pp->ki_tdname); 892173004Sjulian } else { 893243262Srpaulo snprintf(cmdbuf, cmdlen, 894173004Sjulian "[%s]", pp->ki_comm); 895173004Sjulian } 896173004Sjulian } else { 897173004Sjulian char *src, *dst, *argbuf; 898173004Sjulian char *cmd; 899173004Sjulian size_t argbuflen; 900173004Sjulian size_t len; 901168710Sstas 902243262Srpaulo argbuflen = cmdlen * 4; 903173004Sjulian argbuf = (char *)malloc(argbuflen + 1); 904173004Sjulian if (argbuf == NULL) { 905173004Sjulian warn("malloc(%d)", argbuflen + 1); 906173004Sjulian free(cmdbuf); 907173004Sjulian return NULL; 908173004Sjulian } 909168710Sstas 910173004Sjulian dst = argbuf; 911168710Sstas 912173004Sjulian /* Extract cmd name from argv */ 913173004Sjulian cmd = strrchr(*args, '/'); 914173004Sjulian if (cmd == NULL) 915173004Sjulian cmd = *args; 916173004Sjulian else 917173004Sjulian cmd++; 918168710Sstas 919173004Sjulian for (; (src = *args++) != NULL; ) { 920173004Sjulian if (*src == '\0') 921173004Sjulian continue; 922173004Sjulian len = (argbuflen - (dst - argbuf) - 1) / 4; 923173004Sjulian strvisx(dst, src, 924173004Sjulian strlen(src) < len ? strlen(src) : len, 925173004Sjulian VIS_NL | VIS_CSTYLE); 926173004Sjulian while (*dst != '\0') 927173004Sjulian dst++; 928173004Sjulian if ((argbuflen - (dst - argbuf) - 1) / 4 > 0) 929173004Sjulian *dst++ = ' '; /* add delimiting space */ 930173004Sjulian } 931173004Sjulian if (dst != argbuf && dst[-1] == ' ') 932173004Sjulian dst--; 933173004Sjulian *dst = '\0'; 934168710Sstas 935243262Srpaulo if (strcmp(cmd, pp->ki_comm) != 0) { 936224202Sbz if (ps.thread && pp->ki_flag & P_HADTHREADS && 937224202Sbz pp->ki_tdname[0]) 938243262Srpaulo snprintf(cmdbuf, cmdlen, 939224202Sbz "%s (%s){%s}", argbuf, pp->ki_comm, 940224202Sbz pp->ki_tdname); 941224202Sbz else 942243262Srpaulo snprintf(cmdbuf, cmdlen, 943224202Sbz "%s (%s)", argbuf, pp->ki_comm); 944224202Sbz } else { 945224202Sbz if (ps.thread && pp->ki_flag & P_HADTHREADS && 946224202Sbz pp->ki_tdname[0]) 947243262Srpaulo snprintf(cmdbuf, cmdlen, 948224202Sbz "%s{%s}", argbuf, pp->ki_tdname); 949224202Sbz else 950243262Srpaulo strlcpy(cmdbuf, argbuf, cmdlen); 951224202Sbz } 952173004Sjulian free(argbuf); 953173004Sjulian } 954168710Sstas } 955168710Sstas 956168799Srafan if (ps.jail == 0) 957168799Srafan jid_buf[0] = '\0'; 958168799Srafan else 959168799Srafan snprintf(jid_buf, sizeof(jid_buf), " %*d", 960168799Srafan sizeof(jid_buf) - 3, pp->ki_jid); 961168799Srafan 962131623Sdes if (displaymode == DISP_IO) { 963131623Sdes oldp = get_old_proc(pp); 964131623Sdes if (oldp != NULL) { 965158280Sbde ru.ru_inblock = RU(pp)->ru_inblock - 966158280Sbde RU(oldp)->ru_inblock; 967158280Sbde ru.ru_oublock = RU(pp)->ru_oublock - 968158280Sbde RU(oldp)->ru_oublock; 969131623Sdes ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt; 970133817Salfred ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw; 971133817Salfred ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw; 972131623Sdes rup = &ru; 973131623Sdes } else { 974131623Sdes rup = RU(pp); 975131623Sdes } 976131623Sdes p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt; 977131623Sdes s_tot = total_inblock + total_oublock + total_majflt; 978131402Salfred 979234416Skib snprintf(fmt, sizeof(fmt), io_Proc_format, 980131623Sdes pp->ki_pid, 981168799Srafan jid_buf, 982158280Sbde namelength, namelength, (*get_userid)(pp->ki_ruid), 983133817Salfred rup->ru_nvcsw, 984133817Salfred rup->ru_nivcsw, 985131626Sdes rup->ru_inblock, 986131626Sdes rup->ru_oublock, 987131626Sdes rup->ru_majflt, 988131626Sdes p_tot, 989131626Sdes s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot), 990131623Sdes screen_width > cmdlengthdelta ? 991131623Sdes screen_width - cmdlengthdelta : 0, 992168710Sstas printable(cmdbuf)); 993168710Sstas 994168710Sstas free(cmdbuf); 995168710Sstas 996131623Sdes return (fmt); 997131623Sdes } 998145073Skeramida 999131623Sdes /* format this entry */ 1000248167Sjhb if (smpmode) { 1001248167Sjhb if (state == SRUN && pp->ki_oncpu != 0xff) 1002248167Sjhb cpu = pp->ki_oncpu; 1003248167Sjhb else 1004248167Sjhb cpu = pp->ki_lastcpu; 1005248167Sjhb } else 1006248167Sjhb cpu = 0; 1007145073Skeramida proc_fmt = smpmode ? smp_Proc_format : up_Proc_format; 1008145073Skeramida if (ps.thread != 0) 1009145073Skeramida thr_buf[0] = '\0'; 1010145073Skeramida else 1011145073Skeramida snprintf(thr_buf, sizeof(thr_buf), "%*d ", 1012145073Skeramida sizeof(thr_buf) - 2, pp->ki_numthreads); 1013145073Skeramida 1014234416Skib snprintf(fmt, sizeof(fmt), proc_fmt, 101569896Smckusick pp->ki_pid, 1016168799Srafan jid_buf, 1017158280Sbde namelength, namelength, (*get_userid)(pp->ki_ruid), 1018145073Skeramida thr_buf, 101972377Sjake pp->ki_pri.pri_level - PZERO, 1020158282Sbde format_nice(pp), 102137100Sdt format_k2(PROCSIZE(pp)), 102269896Smckusick format_k2(pagetok(pp->ki_rssize)), 102324143Sjoerg status, 1024248167Sjhb cpu, 102524143Sjoerg format_time(cputime), 1026146342Skeramida ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct, 1027158280Sbde screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0, 1028168710Sstas printable(cmdbuf)); 102924143Sjoerg 1030168710Sstas free(cmdbuf); 1031168710Sstas 1032131623Sdes /* return the result */ 1033131623Sdes return (fmt); 103424143Sjoerg} 103524143Sjoerg 1036131310Salfredstatic void 1037158282Sbdegetsysctl(const char *name, void *ptr, size_t len) 103824143Sjoerg{ 1039131623Sdes size_t nlen = len; 1040131310Salfred 1041131623Sdes if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { 1042131623Sdes fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name, 1043131623Sdes strerror(errno)); 1044131623Sdes quit(23); 1045131623Sdes } 1046131623Sdes if (nlen != len) { 1047158280Sbde fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", 1048158280Sbde name, (unsigned long)len, (unsigned long)nlen); 1049131623Sdes quit(23); 1050131623Sdes } 105124143Sjoerg} 105272951Srwatson 1053164058Sbdestatic const char * 1054164058Sbdeformat_nice(const struct kinfo_proc *pp) 1055158282Sbde{ 1056164058Sbde const char *fifo, *kthread; 1057164058Sbde int rtpri; 1058164058Sbde static char nicebuf[4 + 1]; 1059158282Sbde 1060164058Sbde fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F"; 1061164058Sbde kthread = (pp->ki_flag & P_KTHREAD) ? "k" : ""; 1062164058Sbde switch (PRI_BASE(pp->ki_pri.pri_class)) { 1063164058Sbde case PRI_ITHD: 1064164058Sbde return ("-"); 1065164058Sbde case PRI_REALTIME: 1066170774Sbde /* 1067170774Sbde * XXX: the kernel doesn't tell us the original rtprio and 1068170774Sbde * doesn't really know what it was, so to recover it we 1069170774Sbde * must be more chummy with the implementation than the 1070170774Sbde * implementation is with itself. pri_user gives a 1071170774Sbde * constant "base" priority, but is only initialized 1072170774Sbde * properly for user threads. pri_native gives what the 1073170774Sbde * kernel calls the "base" priority, but it isn't constant 1074170774Sbde * since it is changed by priority propagation. pri_native 1075170774Sbde * also isn't properly initialized for all threads, but it 1076170774Sbde * is properly initialized for kernel realtime and idletime 1077170774Sbde * threads. Thus we use pri_user for the base priority of 1078170774Sbde * user threads (it is always correct) and pri_native for 1079170774Sbde * the base priority of kernel realtime and idletime threads 1080170774Sbde * (there is nothing better, and it is usually correct). 1081170774Sbde * 1082170774Sbde * The field width and thus the buffer are too small for 1083170774Sbde * values like "kr31F", but such values shouldn't occur, 1084170774Sbde * and if they do then the tailing "F" is not displayed. 1085170774Sbde */ 1086170774Sbde rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1087170774Sbde pp->ki_pri.pri_user) - PRI_MIN_REALTIME; 1088164058Sbde snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s", 1089164058Sbde kthread, rtpri, fifo); 1090164058Sbde break; 1091164058Sbde case PRI_TIMESHARE: 1092164058Sbde if (pp->ki_flag & P_KTHREAD) 1093164058Sbde return ("-"); 1094164058Sbde snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO); 1095164058Sbde break; 1096164058Sbde case PRI_IDLE: 1097170774Sbde /* XXX: as above. */ 1098170774Sbde rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native : 1099170774Sbde pp->ki_pri.pri_user) - PRI_MIN_IDLE; 1100164058Sbde snprintf(nicebuf, sizeof(nicebuf), "%si%d%s", 1101164058Sbde kthread, rtpri, fifo); 1102164058Sbde break; 1103164058Sbde default: 1104164058Sbde return ("?"); 1105164058Sbde } 1106158282Sbde return (nicebuf); 1107158282Sbde} 1108158282Sbde 110938278Swosch/* comparison routines for qsort */ 111024143Sjoerg 1111158282Sbdestatic int 1112131622Sdescompare_pid(const void *p1, const void *p2) 1113131402Salfred{ 1114131623Sdes const struct kinfo_proc * const *pp1 = p1; 1115131623Sdes const struct kinfo_proc * const *pp2 = p2; 1116131402Salfred 1117131623Sdes if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0) 1118131623Sdes abort(); 1119131402Salfred 1120131623Sdes return ((*pp1)->ki_pid - (*pp2)->ki_pid); 1121131402Salfred} 1122131402Salfred 1123224188Sjhbstatic int 1124224188Sjhbcompare_tid(const void *p1, const void *p2) 1125224188Sjhb{ 1126224188Sjhb const struct kinfo_proc * const *pp1 = p1; 1127224188Sjhb const struct kinfo_proc * const *pp2 = p2; 1128224188Sjhb 1129224188Sjhb if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0) 1130224188Sjhb abort(); 1131224188Sjhb 1132224188Sjhb return ((*pp1)->ki_tid - (*pp2)->ki_tid); 1133224188Sjhb} 1134224188Sjhb 113524143Sjoerg/* 113624143Sjoerg * proc_compare - comparison function for "qsort" 113724143Sjoerg * Compares the resource consumption of two processes using five 1138131620Sdes * distinct keys. The keys (in descending order of importance) are: 1139131620Sdes * percent cpu, cpu ticks, state, resident set size, total virtual 1140131620Sdes * memory usage. The process states are ordered as follows (from least 1141131620Sdes * to most important): WAIT, zombie, sleep, stop, start, run. The 1142131620Sdes * array declaration below maps a process state index into a number 1143131620Sdes * that reflects this ordering. 114424143Sjoerg */ 114524143Sjoerg 1146158280Sbdestatic int sorted_state[] = { 1147131623Sdes 0, /* not used */ 1148131623Sdes 3, /* sleep */ 1149131623Sdes 1, /* ABANDONED (WAIT) */ 1150131623Sdes 6, /* run */ 1151131623Sdes 5, /* start */ 1152131623Sdes 2, /* zombie */ 1153131623Sdes 4 /* stop */ 115424143Sjoerg}; 115538278Swosch 1156131620Sdes 1157146343Skeramida#define ORDERKEY_PCTCPU(a, b) do { \ 1158146343Skeramida long diff; \ 1159146343Skeramida if (ps.wcpu) \ 1160158280Sbde diff = floor(1.0E6 * weighted_cpu(pctdouble((b)->ki_pctcpu), \ 1161158280Sbde (b))) - \ 1162158280Sbde floor(1.0E6 * weighted_cpu(pctdouble((a)->ki_pctcpu), \ 1163158280Sbde (a))); \ 1164146343Skeramida else \ 1165146343Skeramida diff = (long)(b)->ki_pctcpu - (long)(a)->ki_pctcpu; \ 1166146343Skeramida if (diff != 0) \ 1167146343Skeramida return (diff > 0 ? 1 : -1); \ 1168131626Sdes} while (0) 116938278Swosch 1170131626Sdes#define ORDERKEY_CPTICKS(a, b) do { \ 1171131628Sdes int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \ 1172131626Sdes if (diff != 0) \ 1173131626Sdes return (diff > 0 ? 1 : -1); \ 1174131626Sdes} while (0) 117538278Swosch 1176131626Sdes#define ORDERKEY_STATE(a, b) do { \ 1177131626Sdes int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \ 1178131626Sdes if (diff != 0) \ 1179131626Sdes return (diff > 0 ? 1 : -1); \ 1180131626Sdes} while (0) 118138278Swosch 1182131626Sdes#define ORDERKEY_PRIO(a, b) do { \ 1183131628Sdes int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \ 1184131626Sdes if (diff != 0) \ 1185131626Sdes return (diff > 0 ? 1 : -1); \ 1186131626Sdes} while (0) 118738278Swosch 1188145073Skeramida#define ORDERKEY_THREADS(a, b) do { \ 1189145073Skeramida int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \ 1190145073Skeramida if (diff != 0) \ 1191145073Skeramida return (diff > 0 ? 1 : -1); \ 1192145073Skeramida} while (0) 1193145073Skeramida 1194131626Sdes#define ORDERKEY_RSSIZE(a, b) do { \ 1195131628Sdes long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \ 1196131626Sdes if (diff != 0) \ 1197131626Sdes return (diff > 0 ? 1 : -1); \ 1198131626Sdes} while (0) 119938278Swosch 1200131626Sdes#define ORDERKEY_MEM(a, b) do { \ 1201131628Sdes long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \ 1202131626Sdes if (diff != 0) \ 1203131626Sdes return (diff > 0 ? 1 : -1); \ 1204131626Sdes} while (0) 120538278Swosch 1206168799Srafan#define ORDERKEY_JID(a, b) do { \ 1207168799Srafan int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \ 1208168799Srafan if (diff != 0) \ 1209168799Srafan return (diff > 0 ? 1 : -1); \ 1210168799Srafan} while (0) 1211168799Srafan 121238278Swosch/* compare_cpu - the comparison function for sorting by cpu percentage */ 121338278Swosch 121424143Sjoergint 121538278Swosch#ifdef ORDER 1216131626Sdescompare_cpu(void *arg1, void *arg2) 121738278Swosch#else 1218131626Sdesproc_compare(void *arg1, void *arg2) 121938278Swosch#endif 122024143Sjoerg{ 1221131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1222131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 122324143Sjoerg 1224131626Sdes ORDERKEY_PCTCPU(p1, p2); 1225131626Sdes ORDERKEY_CPTICKS(p1, p2); 1226131626Sdes ORDERKEY_STATE(p1, p2); 1227131626Sdes ORDERKEY_PRIO(p1, p2); 1228131626Sdes ORDERKEY_RSSIZE(p1, p2); 1229131626Sdes ORDERKEY_MEM(p1, p2); 123024143Sjoerg 1231131626Sdes return (0); 123224143Sjoerg} 123324143Sjoerg 123438278Swosch#ifdef ORDER 1235158280Sbde/* "cpu" compare routines */ 1236158280Sbdeint compare_size(), compare_res(), compare_time(), compare_prio(), 1237158280Sbde compare_threads(); 123824143Sjoerg 1239158280Sbde/* 1240158280Sbde * "io" compare routines. Context switches aren't i/o, but are displayed 1241158280Sbde * on the "io" display. 1242158280Sbde */ 1243158280Sbdeint compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(), 1244158280Sbde compare_vcsw(), compare_ivcsw(); 1245158280Sbde 1246133817Salfredint (*compares[])() = { 1247131623Sdes compare_cpu, 1248131623Sdes compare_size, 1249131623Sdes compare_res, 1250131623Sdes compare_time, 1251131623Sdes compare_prio, 1252145073Skeramida compare_threads, 1253133817Salfred compare_iototal, 1254133817Salfred compare_ioread, 1255133817Salfred compare_iowrite, 1256133817Salfred compare_iofault, 1257133817Salfred compare_vcsw, 1258133817Salfred compare_ivcsw, 1259168799Srafan compare_jid, 1260131623Sdes NULL 126138278Swosch}; 126238278Swosch 126338278Swosch/* compare_size - the comparison function for sorting by total memory usage */ 126438278Swosch 126538278Swoschint 1266131626Sdescompare_size(void *arg1, void *arg2) 126738278Swosch{ 1268131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1269131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 127038278Swosch 1271131626Sdes ORDERKEY_MEM(p1, p2); 1272131626Sdes ORDERKEY_RSSIZE(p1, p2); 1273131626Sdes ORDERKEY_PCTCPU(p1, p2); 1274131626Sdes ORDERKEY_CPTICKS(p1, p2); 1275131626Sdes ORDERKEY_STATE(p1, p2); 1276131626Sdes ORDERKEY_PRIO(p1, p2); 127738278Swosch 1278131626Sdes return (0); 127938278Swosch} 128038278Swosch 128138278Swosch/* compare_res - the comparison function for sorting by resident set size */ 128238278Swosch 128338278Swoschint 1284131626Sdescompare_res(void *arg1, void *arg2) 128538278Swosch{ 1286131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1287131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 128838278Swosch 1289131626Sdes ORDERKEY_RSSIZE(p1, p2); 1290131626Sdes ORDERKEY_MEM(p1, p2); 1291131626Sdes ORDERKEY_PCTCPU(p1, p2); 1292131626Sdes ORDERKEY_CPTICKS(p1, p2); 1293131626Sdes ORDERKEY_STATE(p1, p2); 1294131626Sdes ORDERKEY_PRIO(p1, p2); 129538278Swosch 1296131626Sdes return (0); 129738278Swosch} 129838278Swosch 129938278Swosch/* compare_time - the comparison function for sorting by total cpu time */ 130038278Swosch 130138278Swoschint 1302131626Sdescompare_time(void *arg1, void *arg2) 1303131310Salfred{ 1304131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1305131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 130638278Swosch 1307131626Sdes ORDERKEY_CPTICKS(p1, p2); 1308131626Sdes ORDERKEY_PCTCPU(p1, p2); 1309131626Sdes ORDERKEY_STATE(p1, p2); 1310131626Sdes ORDERKEY_PRIO(p1, p2); 1311131626Sdes ORDERKEY_RSSIZE(p1, p2); 1312131626Sdes ORDERKEY_MEM(p1, p2); 131338278Swosch 1314131626Sdes return (0); 1315131623Sdes} 1316131310Salfred 1317131626Sdes/* compare_prio - the comparison function for sorting by priority */ 131838278Swosch 131938278Swoschint 1320131626Sdescompare_prio(void *arg1, void *arg2) 132138278Swosch{ 1322131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1323131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 132438278Swosch 1325131626Sdes ORDERKEY_PRIO(p1, p2); 1326131626Sdes ORDERKEY_CPTICKS(p1, p2); 1327131626Sdes ORDERKEY_PCTCPU(p1, p2); 1328131626Sdes ORDERKEY_STATE(p1, p2); 1329131626Sdes ORDERKEY_RSSIZE(p1, p2); 1330131626Sdes ORDERKEY_MEM(p1, p2); 133138278Swosch 1332131626Sdes return (0); 133338278Swosch} 1334145073Skeramida 1335145073Skeramida/* compare_threads - the comparison function for sorting by threads */ 1336145073Skeramidaint 1337145073Skeramidacompare_threads(void *arg1, void *arg2) 1338145073Skeramida{ 1339145073Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1340145073Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1341145073Skeramida 1342145073Skeramida ORDERKEY_THREADS(p1, p2); 1343145073Skeramida ORDERKEY_PCTCPU(p1, p2); 1344145073Skeramida ORDERKEY_CPTICKS(p1, p2); 1345145073Skeramida ORDERKEY_STATE(p1, p2); 1346145073Skeramida ORDERKEY_PRIO(p1, p2); 1347145073Skeramida ORDERKEY_RSSIZE(p1, p2); 1348145073Skeramida ORDERKEY_MEM(p1, p2); 1349145073Skeramida 1350145073Skeramida return (0); 1351145073Skeramida} 1352168799Srafan 1353168799Srafan/* compare_jid - the comparison function for sorting by jid */ 1354168799Srafanstatic int 1355168799Srafancompare_jid(const void *arg1, const void *arg2) 1356168799Srafan{ 1357168799Srafan struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1358168799Srafan struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1359168799Srafan 1360168799Srafan ORDERKEY_JID(p1, p2); 1361168799Srafan ORDERKEY_PCTCPU(p1, p2); 1362168799Srafan ORDERKEY_CPTICKS(p1, p2); 1363168799Srafan ORDERKEY_STATE(p1, p2); 1364168799Srafan ORDERKEY_PRIO(p1, p2); 1365168799Srafan ORDERKEY_RSSIZE(p1, p2); 1366168799Srafan ORDERKEY_MEM(p1, p2); 1367168799Srafan 1368168799Srafan return (0); 1369168799Srafan} 1370158280Sbde#endif /* ORDER */ 137138278Swosch 1372158280Sbde/* assorted comparison functions for sorting by i/o */ 1373131829Skeramida 1374131402Salfredint 1375131829Skeramida#ifdef ORDER 1376131829Skeramidacompare_iototal(void *arg1, void *arg2) 1377131829Skeramida#else 1378131626Sdesio_compare(void *arg1, void *arg2) 1379131829Skeramida#endif 1380131402Salfred{ 1381131626Sdes struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1382131626Sdes struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1383131402Salfred 1384131626Sdes return (get_io_total(p2) - get_io_total(p1)); 1385131402Salfred} 1386131829Skeramida 1387131829Skeramida#ifdef ORDER 1388131829Skeramidaint 1389131829Skeramidacompare_ioread(void *arg1, void *arg2) 1390131829Skeramida{ 1391131829Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1392131829Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1393131829Skeramida long dummy, inp1, inp2; 1394131829Skeramida 1395133817Salfred (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy); 1396133817Salfred (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy); 1397131829Skeramida 1398131829Skeramida return (inp2 - inp1); 1399131829Skeramida} 1400131829Skeramida 1401131829Skeramidaint 1402131829Skeramidacompare_iowrite(void *arg1, void *arg2) 1403131829Skeramida{ 1404131829Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1405131829Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1406131829Skeramida long dummy, oup1, oup2; 1407131829Skeramida 1408133817Salfred (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy); 1409133817Salfred (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy); 1410131829Skeramida 1411131829Skeramida return (oup2 - oup1); 1412131829Skeramida} 1413131829Skeramida 1414131829Skeramidaint 1415131829Skeramidacompare_iofault(void *arg1, void *arg2) 1416131829Skeramida{ 1417131829Skeramida struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1418131829Skeramida struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1419131829Skeramida long dummy, flp1, flp2; 1420131829Skeramida 1421133817Salfred (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy); 1422133817Salfred (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy); 1423131829Skeramida 1424131829Skeramida return (flp2 - flp1); 1425131829Skeramida} 1426131829Skeramida 1427133817Salfredint 1428133817Salfredcompare_vcsw(void *arg1, void *arg2) 1429133817Salfred{ 1430133817Salfred struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1431133817Salfred struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1432133817Salfred long dummy, flp1, flp2; 1433133817Salfred 1434133817Salfred (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy); 1435133817Salfred (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy); 1436133817Salfred 1437133817Salfred return (flp2 - flp1); 1438133817Salfred} 1439133817Salfred 1440133817Salfredint 1441133817Salfredcompare_ivcsw(void *arg1, void *arg2) 1442133817Salfred{ 1443133817Salfred struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1; 1444133817Salfred struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2; 1445133817Salfred long dummy, flp1, flp2; 1446133817Salfred 1447133817Salfred (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1); 1448133817Salfred (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2); 1449133817Salfred 1450133817Salfred return (flp2 - flp1); 1451133817Salfred} 1452131829Skeramida#endif /* ORDER */ 1453131829Skeramida 145424143Sjoerg/* 145524143Sjoerg * proc_owner(pid) - returns the uid that owns process "pid", or -1 if 145624143Sjoerg * the process does not exist. 1457228992Suqs * It is EXTREMELY IMPORTANT that this function work correctly. 145824143Sjoerg * If top runs setuid root (as in SVR4), then this function 145924143Sjoerg * is the only thing that stands in the way of a serious 146024143Sjoerg * security problem. It validates requests for the "kill" 146124143Sjoerg * and "renice" commands. 146224143Sjoerg */ 146324143Sjoerg 1464131310Salfredint 1465131622Sdesproc_owner(int pid) 146624143Sjoerg{ 1467131623Sdes int cnt; 1468131623Sdes struct kinfo_proc **prefp; 1469131623Sdes struct kinfo_proc *pp; 147024143Sjoerg 1471131623Sdes prefp = pref; 1472131623Sdes cnt = pref_len; 1473131626Sdes while (--cnt >= 0) { 1474131623Sdes pp = *prefp++; 1475131623Sdes if (pp->ki_pid == (pid_t)pid) 1476131623Sdes return ((int)pp->ki_ruid); 147724143Sjoerg } 1478131623Sdes return (-1); 147924143Sjoerg} 148024143Sjoerg 1481158282Sbdestatic int 1482131622Sdesswapmode(int *retavail, int *retfree) 148324143Sjoerg{ 148443053Sdillon int n; 148543053Sdillon int pagesize = getpagesize(); 148643053Sdillon struct kvm_swap swapary[1]; 148724143Sjoerg 148843053Sdillon *retavail = 0; 148943053Sdillon *retfree = 0; 149024143Sjoerg 149143053Sdillon#define CONVERT(v) ((quad_t)(v) * pagesize / 1024) 149224143Sjoerg 149343053Sdillon n = kvm_getswapinfo(kd, swapary, 1, 0); 149443697Sdillon if (n < 0 || swapary[0].ksw_total == 0) 1495131623Sdes return (0); 149624143Sjoerg 149743053Sdillon *retavail = CONVERT(swapary[0].ksw_total); 149843053Sdillon *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); 149924143Sjoerg 1500131626Sdes n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total); 1501131623Sdes return (n); 150243053Sdillon} 1503