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