xref: /freebsd/usr.bin/top/machine.c (revision 4657548d18877f64bd02be888406aa5b02bf9b06)
1 /*
2  * top - a top users display for Unix
3  *
4  * SYNOPSIS:  For FreeBSD-2.x and later
5  *
6  * DESCRIPTION:
7  * Originally written for BSD4.4 system by Christos Zoulas.
8  * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider
9  * Order support hacked in from top-3.5beta6/machine/m_aix41.c
10  *   by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/)
11  *
12  * This is the machine-dependent module for FreeBSD 2.2
13  * Works for:
14  *	FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x
15  *
16  * LIBS: -lkvm
17  *
18  * AUTHOR:  Christos Zoulas <christos@ee.cornell.edu>
19  *          Steven Wallace  <swallace@freebsd.org>
20  *          Wolfram Schneider <wosch@FreeBSD.org>
21  *          Thomas Moestl <tmoestl@gmx.net>
22  *
23  * $FreeBSD$
24  */
25 
26 #include <sys/param.h>
27 #include <sys/errno.h>
28 #include <sys/file.h>
29 #include <sys/proc.h>
30 #include <sys/resource.h>
31 #include <sys/rtprio.h>
32 #include <sys/signal.h>
33 #include <sys/sysctl.h>
34 #include <sys/time.h>
35 #include <sys/user.h>
36 #include <sys/vmmeter.h>
37 
38 #include <err.h>
39 #include <kvm.h>
40 #include <math.h>
41 #include <nlist.h>
42 #include <paths.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <unistd.h>
49 #include <vis.h>
50 
51 #include "top.h"
52 #include "machine.h"
53 #include "screen.h"
54 #include "utils.h"
55 #include "layout.h"
56 
57 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
58 #define	SMPUNAMELEN	13
59 #define	UPUNAMELEN	15
60 
61 extern struct process_select ps;
62 extern char* printable(char *);
63 static int smpmode;
64 enum displaymodes displaymode;
65 #ifdef TOP_USERNAME_LEN
66 static int namelength = TOP_USERNAME_LEN;
67 #else
68 static int namelength = 8;
69 #endif
70 /* TOP_JID_LEN based on max of 999999 */
71 #define TOP_JID_LEN 7
72 #define TOP_SWAP_LEN 6
73 static int jidlength;
74 static int swaplength;
75 static int cmdlengthdelta;
76 
77 /* Prototypes for top internals */
78 void quit(int);
79 
80 /* get_process_info passes back a handle.  This is what it looks like: */
81 
82 struct handle {
83 	struct kinfo_proc **next_proc;	/* points to next valid proc pointer */
84 	int remaining;			/* number of pointers remaining */
85 };
86 
87 /* declarations for load_avg */
88 #include "loadavg.h"
89 
90 /* define what weighted cpu is.  */
91 #define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \
92 			 ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu))))
93 
94 /* what we consider to be process size: */
95 #define PROCSIZE(pp) ((pp)->ki_size / 1024)
96 
97 #define RU(pp)	(&(pp)->ki_rusage)
98 #define RUTOT(pp) \
99 	(RU(pp)->ru_inblock + RU(pp)->ru_oublock + RU(pp)->ru_majflt)
100 
101 #define	PCTCPU(pp) (pcpu[pp - pbase])
102 
103 /* definitions for indices in the nlist array */
104 
105 /*
106  *  These definitions control the format of the per-process area
107  */
108 
109 static char io_header[] =
110     "  PID%*s %-*.*s   VCSW  IVCSW   READ  WRITE  FAULT  TOTAL PERCENT COMMAND";
111 
112 #define io_Proc_format \
113     "%5d%*s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s"
114 
115 static char smp_header_thr[] =
116     "  PID%*s %-*.*s  THR PRI NICE   SIZE    RES%*s STATE   C   TIME %7s COMMAND";
117 static char smp_header[] =
118     "  PID%*s %-*.*s "   "PRI NICE   SIZE    RES%*s STATE   C   TIME %7s COMMAND";
119 
120 #define smp_Proc_format \
121     "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s %2d%7s %6.2f%% %.*s"
122 
123 static char up_header_thr[] =
124     "  PID%*s %-*.*s  THR PRI NICE   SIZE    RES%*s STATE    TIME %7s COMMAND";
125 static char up_header[] =
126     "  PID%*s %-*.*s "   "PRI NICE   SIZE    RES%*s STATE    TIME %7s COMMAND";
127 
128 #define up_Proc_format \
129     "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s%.0d%7s %6.2f%% %.*s"
130 
131 
132 /* process state names for the "STATE" column of the display */
133 /* the extra nulls in the string "run" are for adding a slash and
134    the processor number when needed */
135 
136 char *state_abbrev[] = {
137 	"", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK"
138 };
139 
140 
141 static kvm_t *kd;
142 
143 /* values that we stash away in _init and use in later routines */
144 
145 static double logcpu;
146 
147 /* these are retrieved from the kernel in _init */
148 
149 static load_avg  ccpu;
150 
151 /* these are used in the get_ functions */
152 
153 static int lastpid;
154 
155 /* these are for calculating cpu state percentages */
156 
157 static long cp_time[CPUSTATES];
158 static long cp_old[CPUSTATES];
159 static long cp_diff[CPUSTATES];
160 
161 /* these are for detailing the process states */
162 
163 int process_states[8];
164 char *procstatenames[] = {
165 	"", " starting, ", " running, ", " sleeping, ", " stopped, ",
166 	" zombie, ", " waiting, ", " lock, ",
167 	NULL
168 };
169 
170 /* these are for detailing the cpu states */
171 
172 int cpu_states[CPUSTATES];
173 char *cpustatenames[] = {
174 	"user", "nice", "system", "interrupt", "idle", NULL
175 };
176 
177 /* these are for detailing the memory statistics */
178 
179 int memory_stats[7];
180 char *memorynames[] = {
181 	"K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ",
182 	"K Free", NULL
183 };
184 
185 int arc_stats[7];
186 char *arcnames[] = {
187 	"K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other",
188 	NULL
189 };
190 
191 int carc_stats[5];
192 char *carcnames[] = {
193 	"K Compressed, ", "K Uncompressed, ", ":1 Ratio, ", "K Overhead",
194 	NULL
195 };
196 
197 int swap_stats[7];
198 char *swapnames[] = {
199 	"K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
200 	NULL
201 };
202 
203 
204 /* these are for keeping track of the proc array */
205 
206 static int nproc;
207 static int onproc = -1;
208 static int pref_len;
209 static struct kinfo_proc *pbase;
210 static struct kinfo_proc **pref;
211 static struct kinfo_proc *previous_procs;
212 static struct kinfo_proc **previous_pref;
213 static int previous_proc_count = 0;
214 static int previous_proc_count_max = 0;
215 static int previous_thread;
216 
217 /* data used for recalculating pctcpu */
218 static double *pcpu;
219 static struct timespec proc_uptime;
220 static struct timeval proc_wall_time;
221 static struct timeval previous_wall_time;
222 static uint64_t previous_interval = 0;
223 
224 /* total number of io operations */
225 static long total_inblock;
226 static long total_oublock;
227 static long total_majflt;
228 
229 /* these are for getting the memory statistics */
230 
231 static int arc_enabled;
232 static int carc_enabled;
233 static int pageshift;		/* log base 2 of the pagesize */
234 
235 /* define pagetok in terms of pageshift */
236 
237 #define pagetok(size) ((size) << pageshift)
238 
239 /* swap usage */
240 #define ki_swap(kip) \
241     ((kip)->ki_swrss > (kip)->ki_rssize ? (kip)->ki_swrss - (kip)->ki_rssize : 0)
242 
243 /* useful externals */
244 long percentages();
245 
246 #ifdef ORDER
247 /*
248  * Sorting orders.  The first element is the default.
249  */
250 char *ordernames[] = {
251 	"cpu", "size", "res", "time", "pri", "threads",
252 	"total", "read", "write", "fault", "vcsw", "ivcsw",
253 	"jid", "swap", "pid", NULL
254 };
255 #endif
256 
257 /* Per-cpu time states */
258 static int maxcpu;
259 static int maxid;
260 static int ncpus;
261 static u_long cpumask;
262 static long *times;
263 static long *pcpu_cp_time;
264 static long *pcpu_cp_old;
265 static long *pcpu_cp_diff;
266 static int *pcpu_cpu_states;
267 
268 static int compare_swap(const void *a, const void *b);
269 static int compare_jid(const void *a, const void *b);
270 static int compare_pid(const void *a, const void *b);
271 static int compare_tid(const void *a, const void *b);
272 static const char *format_nice(const struct kinfo_proc *pp);
273 static void getsysctl(const char *name, void *ptr, size_t len);
274 static int swapmode(int *retavail, int *retfree);
275 static void update_layout(void);
276 
277 void
278 toggle_pcpustats(void)
279 {
280 
281 	if (ncpus == 1)
282 		return;
283 	update_layout();
284 }
285 
286 /* Adjust display based on ncpus and the ARC state. */
287 static void
288 update_layout(void)
289 {
290 
291 	y_mem = 3;
292 	y_arc = 4;
293 	y_carc = 5;
294 	y_swap = 4 + arc_enabled + carc_enabled;
295 	y_idlecursor = 5 + arc_enabled + carc_enabled;
296 	y_message = 5 + arc_enabled + carc_enabled;
297 	y_header = 6 + arc_enabled + carc_enabled;
298 	y_procs = 7 + arc_enabled + carc_enabled;
299 	Header_lines = 7 + arc_enabled + carc_enabled;
300 
301 	if (pcpu_stats) {
302 		y_mem += ncpus - 1;
303 		y_arc += ncpus - 1;
304 		y_carc += ncpus - 1;
305 		y_swap += ncpus - 1;
306 		y_idlecursor += ncpus - 1;
307 		y_message += ncpus - 1;
308 		y_header += ncpus - 1;
309 		y_procs += ncpus - 1;
310 		Header_lines += ncpus - 1;
311 	}
312 }
313 
314 int
315 machine_init(struct statics *statics, char do_unames)
316 {
317 	int i, j, empty, pagesize;
318 	uint64_t arc_size;
319 	size_t size;
320 	struct passwd *pw;
321 
322 	size = sizeof(smpmode);
323 	if ((sysctlbyname("machdep.smp_active", &smpmode, &size,
324 	    NULL, 0) != 0 &&
325 	    sysctlbyname("kern.smp.active", &smpmode, &size,
326 	    NULL, 0) != 0) ||
327 	    size != sizeof(smpmode))
328 		smpmode = 0;
329 
330 	size = sizeof(arc_size);
331 	if (sysctlbyname("vfs.zfs.compressed_arc_enabled", &arc_size, &size,
332 	    NULL, 0) == 0 && arc_size == 1)
333 		carc_enabled = 1;
334 	size = sizeof(arc_size);
335 	if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc_size, &size,
336 	    NULL, 0) == 0 && arc_size != 0)
337 		arc_enabled = 1;
338 
339 	if (do_unames) {
340 	    while ((pw = getpwent()) != NULL) {
341 		if (strlen(pw->pw_name) > namelength)
342 			namelength = strlen(pw->pw_name);
343 	    }
344 	}
345 	if (smpmode && namelength > SMPUNAMELEN)
346 		namelength = SMPUNAMELEN;
347 	else if (namelength > UPUNAMELEN)
348 		namelength = UPUNAMELEN;
349 
350 	kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
351 	if (kd == NULL)
352 		return (-1);
353 
354 	GETSYSCTL("kern.ccpu", ccpu);
355 
356 	/* this is used in calculating WCPU -- calculate it ahead of time */
357 	logcpu = log(loaddouble(ccpu));
358 
359 	pbase = NULL;
360 	pref = NULL;
361 	pcpu = NULL;
362 	nproc = 0;
363 	onproc = -1;
364 
365 	/* get the page size and calculate pageshift from it */
366 	pagesize = getpagesize();
367 	pageshift = 0;
368 	while (pagesize > 1) {
369 		pageshift++;
370 		pagesize >>= 1;
371 	}
372 
373 	/* we only need the amount of log(2)1024 for our conversion */
374 	pageshift -= LOG1024;
375 
376 	/* fill in the statics information */
377 	statics->procstate_names = procstatenames;
378 	statics->cpustate_names = cpustatenames;
379 	statics->memory_names = memorynames;
380 	if (arc_enabled)
381 		statics->arc_names = arcnames;
382 	else
383 		statics->arc_names = NULL;
384 	if (carc_enabled)
385 		statics->carc_names = carcnames;
386 	else
387 		statics->carc_names = NULL;
388 	statics->swap_names = swapnames;
389 #ifdef ORDER
390 	statics->order_names = ordernames;
391 #endif
392 
393 	/* Allocate state for per-CPU stats. */
394 	cpumask = 0;
395 	ncpus = 0;
396 	GETSYSCTL("kern.smp.maxcpus", maxcpu);
397 	size = sizeof(long) * maxcpu * CPUSTATES;
398 	times = malloc(size);
399 	if (times == NULL)
400 		err(1, "malloc %zu bytes", size);
401 	if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1)
402 		err(1, "sysctlbyname kern.cp_times");
403 	pcpu_cp_time = calloc(1, size);
404 	maxid = (size / CPUSTATES / sizeof(long)) - 1;
405 	for (i = 0; i <= maxid; i++) {
406 		empty = 1;
407 		for (j = 0; empty && j < CPUSTATES; j++) {
408 			if (times[i * CPUSTATES + j] != 0)
409 				empty = 0;
410 		}
411 		if (!empty) {
412 			cpumask |= (1ul << i);
413 			ncpus++;
414 		}
415 	}
416 	size = sizeof(long) * ncpus * CPUSTATES;
417 	pcpu_cp_old = calloc(1, size);
418 	pcpu_cp_diff = calloc(1, size);
419 	pcpu_cpu_states = calloc(1, size);
420 	statics->ncpus = ncpus;
421 
422 	update_layout();
423 
424 	/* all done! */
425 	return (0);
426 }
427 
428 char *
429 format_header(char *uname_field)
430 {
431 	static char Header[128];
432 	const char *prehead;
433 
434 	if (ps.jail)
435 		jidlength = TOP_JID_LEN + 1;	/* +1 for extra left space. */
436 	else
437 		jidlength = 0;
438 
439 	if (ps.swap)
440 		swaplength = TOP_SWAP_LEN + 1;  /* +1 for extra left space */
441 	else
442 		swaplength = 0;
443 
444 	switch (displaymode) {
445 	case DISP_CPU:
446 		/*
447 		 * The logic of picking the right header format seems reverse
448 		 * here because we only want to display a THR column when
449 		 * "thread mode" is off (and threads are not listed as
450 		 * separate lines).
451 		 */
452 		prehead = smpmode ?
453 		    (ps.thread ? smp_header : smp_header_thr) :
454 		    (ps.thread ? up_header : up_header_thr);
455 		snprintf(Header, sizeof(Header), prehead,
456 		    jidlength, ps.jail ? " JID" : "",
457 		    namelength, namelength, uname_field,
458 		    swaplength, ps.swap ? " SWAP" : "",
459 		    ps.wcpu ? "WCPU" : "CPU");
460 		break;
461 	case DISP_IO:
462 		prehead = io_header;
463 		snprintf(Header, sizeof(Header), prehead,
464 		    jidlength, ps.jail ? " JID" : "",
465 		    namelength, namelength, uname_field);
466 		break;
467 	}
468 	cmdlengthdelta = strlen(Header) - 7;
469 	return (Header);
470 }
471 
472 static int swappgsin = -1;
473 static int swappgsout = -1;
474 extern struct timeval timeout;
475 
476 
477 void
478 get_system_info(struct system_info *si)
479 {
480 	long total;
481 	struct loadavg sysload;
482 	int mib[2];
483 	struct timeval boottime;
484 	uint64_t arc_stat, arc_stat2;
485 	int i, j;
486 	size_t size;
487 
488 	/* get the CPU stats */
489 	size = (maxid + 1) * CPUSTATES * sizeof(long);
490 	if (sysctlbyname("kern.cp_times", pcpu_cp_time, &size, NULL, 0) == -1)
491 		err(1, "sysctlbyname kern.cp_times");
492 	GETSYSCTL("kern.cp_time", cp_time);
493 	GETSYSCTL("vm.loadavg", sysload);
494 	GETSYSCTL("kern.lastpid", lastpid);
495 
496 	/* convert load averages to doubles */
497 	for (i = 0; i < 3; i++)
498 		si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale;
499 
500 	/* convert cp_time counts to percentages */
501 	for (i = j = 0; i <= maxid; i++) {
502 		if ((cpumask & (1ul << i)) == 0)
503 			continue;
504 		percentages(CPUSTATES, &pcpu_cpu_states[j * CPUSTATES],
505 		    &pcpu_cp_time[j * CPUSTATES],
506 		    &pcpu_cp_old[j * CPUSTATES],
507 		    &pcpu_cp_diff[j * CPUSTATES]);
508 		j++;
509 	}
510 	percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
511 
512 	/* sum memory & swap statistics */
513 	{
514 		static unsigned int swap_delay = 0;
515 		static int swapavail = 0;
516 		static int swapfree = 0;
517 		static long bufspace = 0;
518 		static int nspgsin, nspgsout;
519 
520 		GETSYSCTL("vfs.bufspace", bufspace);
521 		GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]);
522 		GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]);
523 		GETSYSCTL("vm.stats.vm.v_laundry_count", memory_stats[2]);
524 		GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[3]);
525 		GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]);
526 		GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin);
527 		GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout);
528 		/* convert memory stats to Kbytes */
529 		memory_stats[0] = pagetok(memory_stats[0]);
530 		memory_stats[1] = pagetok(memory_stats[1]);
531 		memory_stats[2] = pagetok(memory_stats[2]);
532 		memory_stats[3] = pagetok(memory_stats[3]);
533 		memory_stats[4] = bufspace / 1024;
534 		memory_stats[5] = pagetok(memory_stats[5]);
535 		memory_stats[6] = -1;
536 
537 		/* first interval */
538 		if (swappgsin < 0) {
539 			swap_stats[4] = 0;
540 			swap_stats[5] = 0;
541 		}
542 
543 		/* compute differences between old and new swap statistic */
544 		else {
545 			swap_stats[4] = pagetok(((nspgsin - swappgsin)));
546 			swap_stats[5] = pagetok(((nspgsout - swappgsout)));
547 		}
548 
549 		swappgsin = nspgsin;
550 		swappgsout = nspgsout;
551 
552 		/* call CPU heavy swapmode() only for changes */
553 		if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
554 			swap_stats[3] = swapmode(&swapavail, &swapfree);
555 			swap_stats[0] = swapavail;
556 			swap_stats[1] = swapavail - swapfree;
557 			swap_stats[2] = swapfree;
558 		}
559 		swap_delay = 1;
560 		swap_stats[6] = -1;
561 	}
562 
563 	if (arc_enabled) {
564 		GETSYSCTL("kstat.zfs.misc.arcstats.size", arc_stat);
565 		arc_stats[0] = arc_stat >> 10;
566 		GETSYSCTL("vfs.zfs.mfu_size", arc_stat);
567 		arc_stats[1] = arc_stat >> 10;
568 		GETSYSCTL("vfs.zfs.mru_size", arc_stat);
569 		arc_stats[2] = arc_stat >> 10;
570 		GETSYSCTL("vfs.zfs.anon_size", arc_stat);
571 		arc_stats[3] = arc_stat >> 10;
572 		GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc_stat);
573 		GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc_stat2);
574 		arc_stats[4] = arc_stat + arc_stat2 >> 10;
575 		GETSYSCTL("kstat.zfs.misc.arcstats.other_size", arc_stat);
576 		arc_stats[5] = arc_stat >> 10;
577 		si->arc = arc_stats;
578 	}
579 	if (carc_enabled) {
580 		GETSYSCTL("kstat.zfs.misc.arcstats.compressed_size", arc_stat);
581 		carc_stats[0] = arc_stat >> 10;
582 		GETSYSCTL("kstat.zfs.misc.arcstats.uncompressed_size", arc_stat);
583 		carc_stats[1] = arc_stat >> 10;
584 		carc_stats[2] = arc_stats[0]; /* ARC Total */
585 		GETSYSCTL("kstat.zfs.misc.arcstats.overhead_size", arc_stat);
586 		carc_stats[3] = arc_stat >> 10;
587 		si->carc = carc_stats;
588 	}
589 
590 	/* set arrays and strings */
591 	if (pcpu_stats) {
592 		si->cpustates = pcpu_cpu_states;
593 		si->ncpus = ncpus;
594 	} else {
595 		si->cpustates = cpu_states;
596 		si->ncpus = 1;
597 	}
598 	si->memory = memory_stats;
599 	si->swap = swap_stats;
600 
601 
602 	if (lastpid > 0) {
603 		si->last_pid = lastpid;
604 	} else {
605 		si->last_pid = -1;
606 	}
607 
608 	/*
609 	 * Print how long system has been up.
610 	 * (Found by looking getting "boottime" from the kernel)
611 	 */
612 	mib[0] = CTL_KERN;
613 	mib[1] = KERN_BOOTTIME;
614 	size = sizeof(boottime);
615 	if (sysctl(mib, nitems(mib), &boottime, &size, NULL, 0) != -1 &&
616 	    boottime.tv_sec != 0) {
617 		si->boottime = boottime;
618 	} else {
619 		si->boottime.tv_sec = -1;
620 	}
621 }
622 
623 #define NOPROC	((void *)-1)
624 
625 /*
626  * We need to compare data from the old process entry with the new
627  * process entry.
628  * To facilitate doing this quickly we stash a pointer in the kinfo_proc
629  * structure to cache the mapping.  We also use a negative cache pointer
630  * of NOPROC to avoid duplicate lookups.
631  * XXX: this could be done when the actual processes are fetched, we do
632  * it here out of laziness.
633  */
634 const struct kinfo_proc *
635 get_old_proc(struct kinfo_proc *pp)
636 {
637 	struct kinfo_proc **oldpp, *oldp;
638 
639 	/*
640 	 * If this is the first fetch of the kinfo_procs then we don't have
641 	 * any previous entries.
642 	 */
643 	if (previous_proc_count == 0)
644 		return (NULL);
645 	/* negative cache? */
646 	if (pp->ki_udata == NOPROC)
647 		return (NULL);
648 	/* cached? */
649 	if (pp->ki_udata != NULL)
650 		return (pp->ki_udata);
651 	/*
652 	 * Not cached,
653 	 * 1) look up based on pid.
654 	 * 2) compare process start.
655 	 * If we fail here, then setup a negative cache entry, otherwise
656 	 * cache it.
657 	 */
658 	oldpp = bsearch(&pp, previous_pref, previous_proc_count,
659 	    sizeof(*previous_pref), ps.thread ? compare_tid : compare_pid);
660 	if (oldpp == NULL) {
661 		pp->ki_udata = NOPROC;
662 		return (NULL);
663 	}
664 	oldp = *oldpp;
665 	if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) {
666 		pp->ki_udata = NOPROC;
667 		return (NULL);
668 	}
669 	pp->ki_udata = oldp;
670 	return (oldp);
671 }
672 
673 /*
674  * Return the total amount of IO done in blocks in/out and faults.
675  * store the values individually in the pointers passed in.
676  */
677 long
678 get_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp,
679     long *vcsw, long *ivcsw)
680 {
681 	const struct kinfo_proc *oldp;
682 	static struct kinfo_proc dummy;
683 	long ret;
684 
685 	oldp = get_old_proc(pp);
686 	if (oldp == NULL) {
687 		bzero(&dummy, sizeof(dummy));
688 		oldp = &dummy;
689 	}
690 	*inp = RU(pp)->ru_inblock - RU(oldp)->ru_inblock;
691 	*oup = RU(pp)->ru_oublock - RU(oldp)->ru_oublock;
692 	*flp = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
693 	*vcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
694 	*ivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
695 	ret =
696 	    (RU(pp)->ru_inblock - RU(oldp)->ru_inblock) +
697 	    (RU(pp)->ru_oublock - RU(oldp)->ru_oublock) +
698 	    (RU(pp)->ru_majflt - RU(oldp)->ru_majflt);
699 	return (ret);
700 }
701 
702 /*
703  * If there was a previous update, use the delta in ki_runtime over
704  * the previous interval to calculate pctcpu.  Otherwise, fall back
705  * to using the kernel's ki_pctcpu.
706  */
707 static double
708 proc_calc_pctcpu(struct kinfo_proc *pp)
709 {
710 	const struct kinfo_proc *oldp;
711 
712 	if (previous_interval != 0) {
713 		oldp = get_old_proc(pp);
714 		if (oldp != NULL)
715 			return ((double)(pp->ki_runtime - oldp->ki_runtime)
716 			    / previous_interval);
717 
718 		/*
719 		 * If this process/thread was created during the previous
720 		 * interval, charge it's total runtime to the previous
721 		 * interval.
722 		 */
723 		else if (pp->ki_start.tv_sec > previous_wall_time.tv_sec ||
724 		    (pp->ki_start.tv_sec == previous_wall_time.tv_sec &&
725 		    pp->ki_start.tv_usec >= previous_wall_time.tv_usec))
726 			return ((double)pp->ki_runtime / previous_interval);
727 	}
728 	return (pctdouble(pp->ki_pctcpu));
729 }
730 
731 /*
732  * Return true if this process has used any CPU time since the
733  * previous update.
734  */
735 static int
736 proc_used_cpu(struct kinfo_proc *pp)
737 {
738 	const struct kinfo_proc *oldp;
739 
740 	oldp = get_old_proc(pp);
741 	if (oldp == NULL)
742 		return (PCTCPU(pp) != 0);
743 	return (pp->ki_runtime != oldp->ki_runtime ||
744 	    RU(pp)->ru_nvcsw != RU(oldp)->ru_nvcsw ||
745 	    RU(pp)->ru_nivcsw != RU(oldp)->ru_nivcsw);
746 }
747 
748 /*
749  * Return the total number of block in/out and faults by a process.
750  */
751 long
752 get_io_total(struct kinfo_proc *pp)
753 {
754 	long dummy;
755 
756 	return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy));
757 }
758 
759 static struct handle handle;
760 
761 caddr_t
762 get_process_info(struct system_info *si, struct process_select *sel,
763     int (*compare)(const void *, const void *))
764 {
765 	int i;
766 	int total_procs;
767 	long p_io;
768 	long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw;
769 	long nsec;
770 	int active_procs;
771 	struct kinfo_proc **prefp;
772 	struct kinfo_proc *pp;
773 	struct timespec previous_proc_uptime;
774 
775 	/* these are copied out of sel for speed */
776 	int show_idle;
777 	int show_jid;
778 	int show_self;
779 	int show_system;
780 	int show_uid;
781 	int show_command;
782 	int show_kidle;
783 
784 	/*
785 	 * If thread state was toggled, don't cache the previous processes.
786 	 */
787 	if (previous_thread != sel->thread)
788 		nproc = 0;
789 	previous_thread = sel->thread;
790 
791 	/*
792 	 * Save the previous process info.
793 	 */
794 	if (previous_proc_count_max < nproc) {
795 		free(previous_procs);
796 		previous_procs = malloc(nproc * sizeof(*previous_procs));
797 		free(previous_pref);
798 		previous_pref = malloc(nproc * sizeof(*previous_pref));
799 		if (previous_procs == NULL || previous_pref == NULL) {
800 			(void) fprintf(stderr, "top: Out of memory.\n");
801 			quit(23);
802 		}
803 		previous_proc_count_max = nproc;
804 	}
805 	if (nproc) {
806 		for (i = 0; i < nproc; i++)
807 			previous_pref[i] = &previous_procs[i];
808 		bcopy(pbase, previous_procs, nproc * sizeof(*previous_procs));
809 		qsort(previous_pref, nproc, sizeof(*previous_pref),
810 		    ps.thread ? compare_tid : compare_pid);
811 	}
812 	previous_proc_count = nproc;
813 	previous_proc_uptime = proc_uptime;
814 	previous_wall_time = proc_wall_time;
815 	previous_interval = 0;
816 
817 	pbase = kvm_getprocs(kd, sel->thread ? KERN_PROC_ALL : KERN_PROC_PROC,
818 	    0, &nproc);
819 	(void)gettimeofday(&proc_wall_time, NULL);
820 	if (clock_gettime(CLOCK_UPTIME, &proc_uptime) != 0)
821 		memset(&proc_uptime, 0, sizeof(proc_uptime));
822 	else if (previous_proc_uptime.tv_sec != 0 &&
823 	    previous_proc_uptime.tv_nsec != 0) {
824 		previous_interval = (proc_uptime.tv_sec -
825 		    previous_proc_uptime.tv_sec) * 1000000;
826 		nsec = proc_uptime.tv_nsec - previous_proc_uptime.tv_nsec;
827 		if (nsec < 0) {
828 			previous_interval -= 1000000;
829 			nsec += 1000000000;
830 		}
831 		previous_interval += nsec / 1000;
832 	}
833 	if (nproc > onproc) {
834 		pref = realloc(pref, sizeof(*pref) * nproc);
835 		pcpu = realloc(pcpu, sizeof(*pcpu) * nproc);
836 		onproc = nproc;
837 	}
838 	if (pref == NULL || pbase == NULL || pcpu == NULL) {
839 		(void) fprintf(stderr, "top: Out of memory.\n");
840 		quit(23);
841 	}
842 	/* get a pointer to the states summary array */
843 	si->procstates = process_states;
844 
845 	/* set up flags which define what we are going to select */
846 	show_idle = sel->idle;
847 	show_jid = sel->jid != -1;
848 	show_self = sel->self == -1;
849 	show_system = sel->system;
850 	show_uid = sel->uid != -1;
851 	show_command = sel->command != NULL;
852 	show_kidle = sel->kidle;
853 
854 	/* count up process states and get pointers to interesting procs */
855 	total_procs = 0;
856 	active_procs = 0;
857 	total_inblock = 0;
858 	total_oublock = 0;
859 	total_majflt = 0;
860 	memset((char *)process_states, 0, sizeof(process_states));
861 	prefp = pref;
862 	for (pp = pbase, i = 0; i < nproc; pp++, i++) {
863 
864 		if (pp->ki_stat == 0)
865 			/* not in use */
866 			continue;
867 
868 		if (!show_self && pp->ki_pid == sel->self)
869 			/* skip self */
870 			continue;
871 
872 		if (!show_system && (pp->ki_flag & P_SYSTEM))
873 			/* skip system process */
874 			continue;
875 
876 		p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt,
877 		    &p_vcsw, &p_ivcsw);
878 		total_inblock += p_inblock;
879 		total_oublock += p_oublock;
880 		total_majflt += p_majflt;
881 		total_procs++;
882 		process_states[pp->ki_stat]++;
883 
884 		if (pp->ki_stat == SZOMB)
885 			/* skip zombies */
886 			continue;
887 
888 		if (!show_kidle && pp->ki_tdflags & TDF_IDLETD)
889 			/* skip kernel idle process */
890 			continue;
891 
892 		PCTCPU(pp) = proc_calc_pctcpu(pp);
893 		if (sel->thread && PCTCPU(pp) > 1.0)
894 			PCTCPU(pp) = 1.0;
895 		if (displaymode == DISP_CPU && !show_idle &&
896 		    (!proc_used_cpu(pp) ||
897 		     pp->ki_stat == SSTOP || pp->ki_stat == SIDL))
898 			/* skip idle or non-running processes */
899 			continue;
900 
901 		if (displaymode == DISP_IO && !show_idle && p_io == 0)
902 			/* skip processes that aren't doing I/O */
903 			continue;
904 
905 		if (show_jid && pp->ki_jid != sel->jid)
906 			/* skip proc. that don't belong to the selected JID */
907 			continue;
908 
909 		if (show_uid && pp->ki_ruid != (uid_t)sel->uid)
910 			/* skip proc. that don't belong to the selected UID */
911 			continue;
912 
913 		*prefp++ = pp;
914 		active_procs++;
915 	}
916 
917 	/* if requested, sort the "interesting" processes */
918 	if (compare != NULL)
919 		qsort(pref, active_procs, sizeof(*pref), compare);
920 
921 	/* remember active and total counts */
922 	si->p_total = total_procs;
923 	si->p_active = pref_len = active_procs;
924 
925 	/* pass back a handle */
926 	handle.next_proc = pref;
927 	handle.remaining = active_procs;
928 	return ((caddr_t)&handle);
929 }
930 
931 static char fmt[512];	/* static area where result is built */
932 
933 char *
934 format_next_process(caddr_t handle, char *(*get_userid)(int), int flags)
935 {
936 	struct kinfo_proc *pp;
937 	const struct kinfo_proc *oldp;
938 	long cputime;
939 	double pct;
940 	struct handle *hp;
941 	char status[16];
942 	int cpu, state;
943 	struct rusage ru, *rup;
944 	long p_tot, s_tot;
945 	char *proc_fmt, thr_buf[6];
946 	char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1];
947 	char *cmdbuf = NULL;
948 	char **args;
949 	const int cmdlen = 128;
950 
951 	/* find and remember the next proc structure */
952 	hp = (struct handle *)handle;
953 	pp = *(hp->next_proc++);
954 	hp->remaining--;
955 
956 	/* get the process's command name */
957 	if ((pp->ki_flag & P_INMEM) == 0) {
958 		/*
959 		 * Print swapped processes as <pname>
960 		 */
961 		size_t len;
962 
963 		len = strlen(pp->ki_comm);
964 		if (len > sizeof(pp->ki_comm) - 3)
965 			len = sizeof(pp->ki_comm) - 3;
966 		memmove(pp->ki_comm + 1, pp->ki_comm, len);
967 		pp->ki_comm[0] = '<';
968 		pp->ki_comm[len + 1] = '>';
969 		pp->ki_comm[len + 2] = '\0';
970 	}
971 
972 	/*
973 	 * Convert the process's runtime from microseconds to seconds.  This
974 	 * time includes the interrupt time although that is not wanted here.
975 	 * ps(1) is similarly sloppy.
976 	 */
977 	cputime = (pp->ki_runtime + 500000) / 1000000;
978 
979 	/* calculate the base for cpu percentages */
980 	pct = PCTCPU(pp);
981 
982 	/* generate "STATE" field */
983 	switch (state = pp->ki_stat) {
984 	case SRUN:
985 		if (smpmode && pp->ki_oncpu != NOCPU)
986 			sprintf(status, "CPU%d", pp->ki_oncpu);
987 		else
988 			strcpy(status, "RUN");
989 		break;
990 	case SLOCK:
991 		if (pp->ki_kiflag & KI_LOCKBLOCK) {
992 			sprintf(status, "*%.6s", pp->ki_lockname);
993 			break;
994 		}
995 		/* fall through */
996 	case SSLEEP:
997 		if (pp->ki_wmesg != NULL) {
998 			sprintf(status, "%.6s", pp->ki_wmesg);
999 			break;
1000 		}
1001 		/* FALLTHROUGH */
1002 	default:
1003 
1004 		if (state >= 0 &&
1005 		    state < sizeof(state_abbrev) / sizeof(*state_abbrev))
1006 			sprintf(status, "%.6s", state_abbrev[state]);
1007 		else
1008 			sprintf(status, "?%5d", state);
1009 		break;
1010 	}
1011 
1012 	cmdbuf = (char *)malloc(cmdlen + 1);
1013 	if (cmdbuf == NULL) {
1014 		warn("malloc(%d)", cmdlen + 1);
1015 		return NULL;
1016 	}
1017 
1018 	if (!(flags & FMT_SHOWARGS)) {
1019 		if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1020 		    pp->ki_tdname[0]) {
1021 			snprintf(cmdbuf, cmdlen, "%s{%s%s}", pp->ki_comm,
1022 			    pp->ki_tdname, pp->ki_moretdname);
1023 		} else {
1024 			snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm);
1025 		}
1026 	} else {
1027 		if (pp->ki_flag & P_SYSTEM ||
1028 		    pp->ki_args == NULL ||
1029 		    (args = kvm_getargv(kd, pp, cmdlen)) == NULL ||
1030 		    !(*args)) {
1031 			if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1032 		    	    pp->ki_tdname[0]) {
1033 				snprintf(cmdbuf, cmdlen,
1034 				    "[%s{%s%s}]", pp->ki_comm, pp->ki_tdname,
1035 				    pp->ki_moretdname);
1036 			} else {
1037 				snprintf(cmdbuf, cmdlen,
1038 				    "[%s]", pp->ki_comm);
1039 			}
1040 		} else {
1041 			char *src, *dst, *argbuf;
1042 			char *cmd;
1043 			size_t argbuflen;
1044 			size_t len;
1045 
1046 			argbuflen = cmdlen * 4;
1047 			argbuf = (char *)malloc(argbuflen + 1);
1048 			if (argbuf == NULL) {
1049 				warn("malloc(%zu)", argbuflen + 1);
1050 				free(cmdbuf);
1051 				return NULL;
1052 			}
1053 
1054 			dst = argbuf;
1055 
1056 			/* Extract cmd name from argv */
1057 			cmd = strrchr(*args, '/');
1058 			if (cmd == NULL)
1059 				cmd = *args;
1060 			else
1061 				cmd++;
1062 
1063 			for (; (src = *args++) != NULL; ) {
1064 				if (*src == '\0')
1065 					continue;
1066 				len = (argbuflen - (dst - argbuf) - 1) / 4;
1067 				strvisx(dst, src,
1068 				    MIN(strlen(src), len),
1069 				    VIS_NL | VIS_CSTYLE);
1070 				while (*dst != '\0')
1071 					dst++;
1072 				if ((argbuflen - (dst - argbuf) - 1) / 4 > 0)
1073 					*dst++ = ' '; /* add delimiting space */
1074 			}
1075 			if (dst != argbuf && dst[-1] == ' ')
1076 				dst--;
1077 			*dst = '\0';
1078 
1079 			if (strcmp(cmd, pp->ki_comm) != 0) {
1080 				if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1081 				    pp->ki_tdname[0])
1082 					snprintf(cmdbuf, cmdlen,
1083 					    "%s (%s){%s%s}", argbuf,
1084 					    pp->ki_comm, pp->ki_tdname,
1085 					    pp->ki_moretdname);
1086 				else
1087 					snprintf(cmdbuf, cmdlen,
1088 					    "%s (%s)", argbuf, pp->ki_comm);
1089 			} else {
1090 				if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1091 				    pp->ki_tdname[0])
1092 					snprintf(cmdbuf, cmdlen,
1093 					    "%s{%s%s}", argbuf, pp->ki_tdname,
1094 					    pp->ki_moretdname);
1095 				else
1096 					strlcpy(cmdbuf, argbuf, cmdlen);
1097 			}
1098 			free(argbuf);
1099 		}
1100 	}
1101 
1102 	if (ps.jail == 0)
1103 		jid_buf[0] = '\0';
1104 	else
1105 		snprintf(jid_buf, sizeof(jid_buf), "%*d",
1106 		    jidlength - 1, pp->ki_jid);
1107 
1108 	if (ps.swap == 0)
1109 		swap_buf[0] = '\0';
1110 	else
1111 		snprintf(swap_buf, sizeof(swap_buf), "%*s",
1112 		    swaplength - 1,
1113 		    format_k2(pagetok(ki_swap(pp)))); /* XXX */
1114 
1115 	if (displaymode == DISP_IO) {
1116 		oldp = get_old_proc(pp);
1117 		if (oldp != NULL) {
1118 			ru.ru_inblock = RU(pp)->ru_inblock -
1119 			    RU(oldp)->ru_inblock;
1120 			ru.ru_oublock = RU(pp)->ru_oublock -
1121 			    RU(oldp)->ru_oublock;
1122 			ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
1123 			ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
1124 			ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
1125 			rup = &ru;
1126 		} else {
1127 			rup = RU(pp);
1128 		}
1129 		p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt;
1130 		s_tot = total_inblock + total_oublock + total_majflt;
1131 
1132 		snprintf(fmt, sizeof(fmt), io_Proc_format,
1133 		    pp->ki_pid,
1134 		    jidlength, jid_buf,
1135 		    namelength, namelength, (*get_userid)(pp->ki_ruid),
1136 		    rup->ru_nvcsw,
1137 		    rup->ru_nivcsw,
1138 		    rup->ru_inblock,
1139 		    rup->ru_oublock,
1140 		    rup->ru_majflt,
1141 		    p_tot,
1142 		    s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot),
1143 		    screen_width > cmdlengthdelta ?
1144 		    screen_width - cmdlengthdelta : 0,
1145 		    printable(cmdbuf));
1146 
1147 		free(cmdbuf);
1148 
1149 		return (fmt);
1150 	}
1151 
1152 	/* format this entry */
1153 	if (smpmode) {
1154 		if (state == SRUN && pp->ki_oncpu != NOCPU)
1155 			cpu = pp->ki_oncpu;
1156 		else
1157 			cpu = pp->ki_lastcpu;
1158 	} else
1159 		cpu = 0;
1160 	proc_fmt = smpmode ? smp_Proc_format : up_Proc_format;
1161 	if (ps.thread != 0)
1162 		thr_buf[0] = '\0';
1163 	else
1164 		snprintf(thr_buf, sizeof(thr_buf), "%*d ",
1165 		    (int)(sizeof(thr_buf) - 2), pp->ki_numthreads);
1166 
1167 	snprintf(fmt, sizeof(fmt), proc_fmt,
1168 	    pp->ki_pid,
1169 	    jidlength, jid_buf,
1170 	    namelength, namelength, (*get_userid)(pp->ki_ruid),
1171 	    thr_buf,
1172 	    pp->ki_pri.pri_level - PZERO,
1173 	    format_nice(pp),
1174 	    format_k2(PROCSIZE(pp)),
1175 	    format_k2(pagetok(pp->ki_rssize)),
1176 	    swaplength, swaplength, swap_buf,
1177 	    status,
1178 	    cpu,
1179 	    format_time(cputime),
1180 	    ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct,
1181 	    screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0,
1182 	    printable(cmdbuf));
1183 
1184 	free(cmdbuf);
1185 
1186 	/* return the result */
1187 	return (fmt);
1188 }
1189 
1190 static void
1191 getsysctl(const char *name, void *ptr, size_t len)
1192 {
1193 	size_t nlen = len;
1194 
1195 	if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
1196 		fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name,
1197 		    strerror(errno));
1198 		quit(23);
1199 	}
1200 	if (nlen != len) {
1201 		fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n",
1202 		    name, (unsigned long)len, (unsigned long)nlen);
1203 		quit(23);
1204 	}
1205 }
1206 
1207 static const char *
1208 format_nice(const struct kinfo_proc *pp)
1209 {
1210 	const char *fifo, *kproc;
1211 	int rtpri;
1212 	static char nicebuf[4 + 1];
1213 
1214 	fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F";
1215 	kproc = (pp->ki_flag & P_KPROC) ? "k" : "";
1216 	switch (PRI_BASE(pp->ki_pri.pri_class)) {
1217 	case PRI_ITHD:
1218 		return ("-");
1219 	case PRI_REALTIME:
1220 		/*
1221 		 * XXX: the kernel doesn't tell us the original rtprio and
1222 		 * doesn't really know what it was, so to recover it we
1223 		 * must be more chummy with the implementation than the
1224 		 * implementation is with itself.  pri_user gives a
1225 		 * constant "base" priority, but is only initialized
1226 		 * properly for user threads.  pri_native gives what the
1227 		 * kernel calls the "base" priority, but it isn't constant
1228 		 * since it is changed by priority propagation.  pri_native
1229 		 * also isn't properly initialized for all threads, but it
1230 		 * is properly initialized for kernel realtime and idletime
1231 		 * threads.  Thus we use pri_user for the base priority of
1232 		 * user threads (it is always correct) and pri_native for
1233 		 * the base priority of kernel realtime and idletime threads
1234 		 * (there is nothing better, and it is usually correct).
1235 		 *
1236 		 * The field width and thus the buffer are too small for
1237 		 * values like "kr31F", but such values shouldn't occur,
1238 		 * and if they do then the tailing "F" is not displayed.
1239 		 */
1240 		rtpri = ((pp->ki_flag & P_KPROC) ? pp->ki_pri.pri_native :
1241 		    pp->ki_pri.pri_user) - PRI_MIN_REALTIME;
1242 		snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s",
1243 		    kproc, rtpri, fifo);
1244 		break;
1245 	case PRI_TIMESHARE:
1246 		if (pp->ki_flag & P_KPROC)
1247 			return ("-");
1248 		snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO);
1249 		break;
1250 	case PRI_IDLE:
1251 		/* XXX: as above. */
1252 		rtpri = ((pp->ki_flag & P_KPROC) ? pp->ki_pri.pri_native :
1253 		    pp->ki_pri.pri_user) - PRI_MIN_IDLE;
1254 		snprintf(nicebuf, sizeof(nicebuf), "%si%d%s",
1255 		    kproc, rtpri, fifo);
1256 		break;
1257 	default:
1258 		return ("?");
1259 	}
1260 	return (nicebuf);
1261 }
1262 
1263 /* comparison routines for qsort */
1264 
1265 static int
1266 compare_pid(const void *p1, const void *p2)
1267 {
1268 	const struct kinfo_proc * const *pp1 = p1;
1269 	const struct kinfo_proc * const *pp2 = p2;
1270 
1271 	if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0)
1272 		abort();
1273 
1274 	return ((*pp1)->ki_pid - (*pp2)->ki_pid);
1275 }
1276 
1277 static int
1278 compare_tid(const void *p1, const void *p2)
1279 {
1280 	const struct kinfo_proc * const *pp1 = p1;
1281 	const struct kinfo_proc * const *pp2 = p2;
1282 
1283 	if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0)
1284 		abort();
1285 
1286 	return ((*pp1)->ki_tid - (*pp2)->ki_tid);
1287 }
1288 
1289 /*
1290  *  proc_compare - comparison function for "qsort"
1291  *	Compares the resource consumption of two processes using five
1292  *	distinct keys.  The keys (in descending order of importance) are:
1293  *	percent cpu, cpu ticks, state, resident set size, total virtual
1294  *	memory usage.  The process states are ordered as follows (from least
1295  *	to most important):  WAIT, zombie, sleep, stop, start, run.  The
1296  *	array declaration below maps a process state index into a number
1297  *	that reflects this ordering.
1298  */
1299 
1300 static int sorted_state[] = {
1301 	0,	/* not used		*/
1302 	3,	/* sleep		*/
1303 	1,	/* ABANDONED (WAIT)	*/
1304 	6,	/* run			*/
1305 	5,	/* start		*/
1306 	2,	/* zombie		*/
1307 	4	/* stop			*/
1308 };
1309 
1310 
1311 #define ORDERKEY_PCTCPU(a, b) do { \
1312 	double diff; \
1313 	if (ps.wcpu) \
1314 		diff = weighted_cpu(PCTCPU((b)), (b)) - \
1315 		    weighted_cpu(PCTCPU((a)), (a)); \
1316 	else \
1317 		diff = PCTCPU((b)) - PCTCPU((a)); \
1318 	if (diff != 0) \
1319 		return (diff > 0 ? 1 : -1); \
1320 } while (0)
1321 
1322 #define ORDERKEY_CPTICKS(a, b) do { \
1323 	int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \
1324 	if (diff != 0) \
1325 		return (diff > 0 ? 1 : -1); \
1326 } while (0)
1327 
1328 #define ORDERKEY_STATE(a, b) do { \
1329 	int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \
1330 	if (diff != 0) \
1331 		return (diff > 0 ? 1 : -1); \
1332 } while (0)
1333 
1334 #define ORDERKEY_PRIO(a, b) do { \
1335 	int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \
1336 	if (diff != 0) \
1337 		return (diff > 0 ? 1 : -1); \
1338 } while (0)
1339 
1340 #define	ORDERKEY_THREADS(a, b) do { \
1341 	int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \
1342 	if (diff != 0) \
1343 		return (diff > 0 ? 1 : -1); \
1344 } while (0)
1345 
1346 #define ORDERKEY_RSSIZE(a, b) do { \
1347 	long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \
1348 	if (diff != 0) \
1349 		return (diff > 0 ? 1 : -1); \
1350 } while (0)
1351 
1352 #define ORDERKEY_MEM(a, b) do { \
1353 	long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \
1354 	if (diff != 0) \
1355 		return (diff > 0 ? 1 : -1); \
1356 } while (0)
1357 
1358 #define ORDERKEY_JID(a, b) do { \
1359 	int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \
1360 	if (diff != 0) \
1361 		return (diff > 0 ? 1 : -1); \
1362 } while (0)
1363 
1364 #define ORDERKEY_SWAP(a, b) do { \
1365 	int diff = (int)ki_swap(b) - (int)ki_swap(a); \
1366 	if (diff != 0) \
1367 		return (diff > 0 ? 1 : -1); \
1368 } while (0)
1369 
1370 /* compare_cpu - the comparison function for sorting by cpu percentage */
1371 
1372 int
1373 #ifdef ORDER
1374 compare_cpu(void *arg1, void *arg2)
1375 #else
1376 proc_compare(void *arg1, void *arg2)
1377 #endif
1378 {
1379 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1380 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1381 
1382 	ORDERKEY_PCTCPU(p1, p2);
1383 	ORDERKEY_CPTICKS(p1, p2);
1384 	ORDERKEY_STATE(p1, p2);
1385 	ORDERKEY_PRIO(p1, p2);
1386 	ORDERKEY_RSSIZE(p1, p2);
1387 	ORDERKEY_MEM(p1, p2);
1388 
1389 	return (0);
1390 }
1391 
1392 #ifdef ORDER
1393 /* "cpu" compare routines */
1394 int compare_size(), compare_res(), compare_time(), compare_prio(),
1395     compare_threads();
1396 
1397 /*
1398  * "io" compare routines.  Context switches aren't i/o, but are displayed
1399  * on the "io" display.
1400  */
1401 int compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(),
1402     compare_vcsw(), compare_ivcsw();
1403 
1404 int (*compares[])() = {
1405 	compare_cpu,
1406 	compare_size,
1407 	compare_res,
1408 	compare_time,
1409 	compare_prio,
1410 	compare_threads,
1411 	compare_iototal,
1412 	compare_ioread,
1413 	compare_iowrite,
1414 	compare_iofault,
1415 	compare_vcsw,
1416 	compare_ivcsw,
1417 	compare_jid,
1418 	compare_swap,
1419 	NULL
1420 };
1421 
1422 /* compare_size - the comparison function for sorting by total memory usage */
1423 
1424 int
1425 compare_size(void *arg1, void *arg2)
1426 {
1427 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1428 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1429 
1430 	ORDERKEY_MEM(p1, p2);
1431 	ORDERKEY_RSSIZE(p1, p2);
1432 	ORDERKEY_PCTCPU(p1, p2);
1433 	ORDERKEY_CPTICKS(p1, p2);
1434 	ORDERKEY_STATE(p1, p2);
1435 	ORDERKEY_PRIO(p1, p2);
1436 
1437 	return (0);
1438 }
1439 
1440 /* compare_res - the comparison function for sorting by resident set size */
1441 
1442 int
1443 compare_res(void *arg1, void *arg2)
1444 {
1445 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1446 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1447 
1448 	ORDERKEY_RSSIZE(p1, p2);
1449 	ORDERKEY_MEM(p1, p2);
1450 	ORDERKEY_PCTCPU(p1, p2);
1451 	ORDERKEY_CPTICKS(p1, p2);
1452 	ORDERKEY_STATE(p1, p2);
1453 	ORDERKEY_PRIO(p1, p2);
1454 
1455 	return (0);
1456 }
1457 
1458 /* compare_time - the comparison function for sorting by total cpu time */
1459 
1460 int
1461 compare_time(void *arg1, void *arg2)
1462 {
1463 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1464 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1465 
1466 	ORDERKEY_CPTICKS(p1, p2);
1467 	ORDERKEY_PCTCPU(p1, p2);
1468 	ORDERKEY_STATE(p1, p2);
1469 	ORDERKEY_PRIO(p1, p2);
1470 	ORDERKEY_RSSIZE(p1, p2);
1471 	ORDERKEY_MEM(p1, p2);
1472 
1473 	return (0);
1474 }
1475 
1476 /* compare_prio - the comparison function for sorting by priority */
1477 
1478 int
1479 compare_prio(void *arg1, void *arg2)
1480 {
1481 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1482 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1483 
1484 	ORDERKEY_PRIO(p1, p2);
1485 	ORDERKEY_CPTICKS(p1, p2);
1486 	ORDERKEY_PCTCPU(p1, p2);
1487 	ORDERKEY_STATE(p1, p2);
1488 	ORDERKEY_RSSIZE(p1, p2);
1489 	ORDERKEY_MEM(p1, p2);
1490 
1491 	return (0);
1492 }
1493 
1494 /* compare_threads - the comparison function for sorting by threads */
1495 int
1496 compare_threads(void *arg1, void *arg2)
1497 {
1498 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1499 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1500 
1501 	ORDERKEY_THREADS(p1, p2);
1502 	ORDERKEY_PCTCPU(p1, p2);
1503 	ORDERKEY_CPTICKS(p1, p2);
1504 	ORDERKEY_STATE(p1, p2);
1505 	ORDERKEY_PRIO(p1, p2);
1506 	ORDERKEY_RSSIZE(p1, p2);
1507 	ORDERKEY_MEM(p1, p2);
1508 
1509 	return (0);
1510 }
1511 
1512 /* compare_jid - the comparison function for sorting by jid */
1513 static int
1514 compare_jid(const void *arg1, const void *arg2)
1515 {
1516 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1517 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1518 
1519 	ORDERKEY_JID(p1, p2);
1520 	ORDERKEY_PCTCPU(p1, p2);
1521 	ORDERKEY_CPTICKS(p1, p2);
1522 	ORDERKEY_STATE(p1, p2);
1523 	ORDERKEY_PRIO(p1, p2);
1524 	ORDERKEY_RSSIZE(p1, p2);
1525 	ORDERKEY_MEM(p1, p2);
1526 
1527 	return (0);
1528 }
1529 
1530 /* compare_swap - the comparison function for sorting by swap */
1531 static int
1532 compare_swap(const void *arg1, const void *arg2)
1533 {
1534 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1535 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1536 
1537 	ORDERKEY_SWAP(p1, p2);
1538 	ORDERKEY_PCTCPU(p1, p2);
1539 	ORDERKEY_CPTICKS(p1, p2);
1540 	ORDERKEY_STATE(p1, p2);
1541 	ORDERKEY_PRIO(p1, p2);
1542 	ORDERKEY_RSSIZE(p1, p2);
1543 	ORDERKEY_MEM(p1, p2);
1544 
1545 	return (0);
1546 }
1547 #endif /* ORDER */
1548 
1549 /* assorted comparison functions for sorting by i/o */
1550 
1551 int
1552 #ifdef ORDER
1553 compare_iototal(void *arg1, void *arg2)
1554 #else
1555 io_compare(void *arg1, void *arg2)
1556 #endif
1557 {
1558 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1559 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1560 
1561 	return (get_io_total(p2) - get_io_total(p1));
1562 }
1563 
1564 #ifdef ORDER
1565 int
1566 compare_ioread(void *arg1, void *arg2)
1567 {
1568 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1569 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1570 	long dummy, inp1, inp2;
1571 
1572 	(void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy);
1573 	(void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy);
1574 
1575 	return (inp2 - inp1);
1576 }
1577 
1578 int
1579 compare_iowrite(void *arg1, void *arg2)
1580 {
1581 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1582 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1583 	long dummy, oup1, oup2;
1584 
1585 	(void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy);
1586 	(void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy);
1587 
1588 	return (oup2 - oup1);
1589 }
1590 
1591 int
1592 compare_iofault(void *arg1, void *arg2)
1593 {
1594 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1595 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1596 	long dummy, flp1, flp2;
1597 
1598 	(void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy);
1599 	(void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy);
1600 
1601 	return (flp2 - flp1);
1602 }
1603 
1604 int
1605 compare_vcsw(void *arg1, void *arg2)
1606 {
1607 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1608 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1609 	long dummy, flp1, flp2;
1610 
1611 	(void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy);
1612 	(void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy);
1613 
1614 	return (flp2 - flp1);
1615 }
1616 
1617 int
1618 compare_ivcsw(void *arg1, void *arg2)
1619 {
1620 	struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1621 	struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1622 	long dummy, flp1, flp2;
1623 
1624 	(void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1);
1625 	(void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2);
1626 
1627 	return (flp2 - flp1);
1628 }
1629 #endif /* ORDER */
1630 
1631 /*
1632  * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
1633  *		the process does not exist.
1634  *		It is EXTREMELY IMPORTANT that this function work correctly.
1635  *		If top runs setuid root (as in SVR4), then this function
1636  *		is the only thing that stands in the way of a serious
1637  *		security problem.  It validates requests for the "kill"
1638  *		and "renice" commands.
1639  */
1640 
1641 int
1642 proc_owner(int pid)
1643 {
1644 	int cnt;
1645 	struct kinfo_proc **prefp;
1646 	struct kinfo_proc *pp;
1647 
1648 	prefp = pref;
1649 	cnt = pref_len;
1650 	while (--cnt >= 0) {
1651 		pp = *prefp++;
1652 		if (pp->ki_pid == (pid_t)pid)
1653 			return ((int)pp->ki_ruid);
1654 	}
1655 	return (-1);
1656 }
1657 
1658 static int
1659 swapmode(int *retavail, int *retfree)
1660 {
1661 	int n;
1662 	int pagesize = getpagesize();
1663 	struct kvm_swap swapary[1];
1664 
1665 	*retavail = 0;
1666 	*retfree = 0;
1667 
1668 #define CONVERT(v)	((quad_t)(v) * pagesize / 1024)
1669 
1670 	n = kvm_getswapinfo(kd, swapary, 1, 0);
1671 	if (n < 0 || swapary[0].ksw_total == 0)
1672 		return (0);
1673 
1674 	*retavail = CONVERT(swapary[0].ksw_total);
1675 	*retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
1676 
1677 	n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total);
1678 	return (n);
1679 }
1680