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