kernel - Add Proportional RSS (PRES)
[dragonfly.git] / usr.bin / top / m_dragonfly.c
blobf9f1619fba1e035b7fc52b7ffc3c9c825bde88e8
1 /*
2 * top - a top users display for Unix
4 * SYNOPSIS: For DragonFly 2.x and later
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/)
12 * This is the machine-dependent module for DragonFly 2.5.1
13 * Should work for:
14 * DragonFly 2.x and above
16 * LIBS: -lkvm
18 * AUTHOR: Jan Lentfer <Jan.Lentfer@web.de>
19 * This module has been put together from different sources and is based on the
20 * work of many other people, e.g. Matthew Dillon, Simon Schubert, Jordan Gordeev.
22 * $FreeBSD: src/usr.bin/top/machine.c,v 1.29.2.2 2001/07/31 20:27:05 tmm Exp $
23 * $DragonFly: src/usr.bin/top/machine.c,v 1.26 2008/10/16 01:52:33 swildner Exp $
26 #include <sys/user.h>
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <sys/signal.h>
30 #include <sys/param.h>
32 #include "os.h"
33 #include <err.h>
34 #include <kvm.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <math.h>
38 #include <pwd.h>
39 #include <sys/errno.h>
40 #include <sys/sysctl.h>
41 #include <sys/file.h>
42 #include <sys/vmmeter.h>
43 #include <sys/resource.h>
44 #include <sys/rtprio.h>
46 /* Swap */
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <sys/conf.h>
51 #include <osreldate.h> /* for changes in kernel structures */
53 #include <sys/kinfo.h>
54 #include <kinfo.h>
55 #include "top.h"
56 #include "display.h"
57 #include "machine.h"
58 #include "screen.h"
59 #include "utils.h"
61 int swapmode(int *retavail, int *retfree);
62 static int smpmode;
63 static int namelength;
64 static int cmdlength;
65 static int show_fullcmd;
67 int n_cpus = 0;
70 * needs to be a global symbol, so wrapper can be modified accordingly.
72 static int show_threads = 0;
74 /* get_process_info passes back a handle. This is what it looks like: */
76 struct handle {
77 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
78 int remaining; /* number of pointers remaining */
81 /* declarations for load_avg */
82 #include "loadavg.h"
84 #define PP(pp, field) ((pp)->kp_ ## field)
85 #define LP(pp, field) ((pp)->kp_lwp.kl_ ## field)
86 #define VP(pp, field) ((pp)->kp_vm_ ## field)
88 /* define what weighted cpu is. */
89 #define weighted_cpu(pct, pp) (PP((pp), swtime) == 0 ? 0.0 : \
90 ((pct) / (1.0 - exp(PP((pp), swtime) * logcpu))))
92 /* what we consider to be process size: */
93 #define PROCSIZE(pp) (VP((pp), map_size) / 1024)
96 * These definitions control the format of the per-process area
99 static char smp_header[] =
100 " PID %-*.*s PRI NICE SIZE RES PRES STATE C TIME CTIME CPU COMMAND";
102 #define smp_Proc_format \
103 "%5d %-*.*s %3d %3d%7s %6s %6s %-6.6s %1x%7s %7s %5.2f%% %.*s"
105 static char up_header[] =
106 " PID %-*.*s PRI NICE SIZE RES PRES STATE TIME CTIME CPU COMMAND";
108 #define up_Proc_format \
109 "%5d %-*.*s %3d %3d%7s %6s %6s %-6.6s%.0d%7s%7s %5.2f%% %.*s"
114 /* process state names for the "STATE" column of the display */
116 * the extra nulls in the string "run" are for adding a slash and the
117 * processor number when needed
120 const char *state_abbrev[] = {
121 "", "RUN\0\0\0", "STOP", "SLEEP",
125 static kvm_t *kd;
127 /* values that we stash away in _init and use in later routines */
129 static double logcpu;
131 static long lastpid;
132 static int ccpu;
134 /* these are for calculating cpu state percentages */
136 static struct kinfo_cputime *cp_time, *cp_old;
138 /* these are for detailing the process states */
140 int process_states[6];
141 char *procstatenames[] = {
142 " running, ", " idle, ", " active, ", " stopped, ", " zombie, ",
143 NULL
146 /* these are for detailing the cpu states */
147 #define CPU_STATES 5
148 int *cpu_states;
149 char *cpustatenames[CPU_STATES + 1] = {
150 "user", "nice", "system", "interrupt", "idle", NULL
153 /* these are for detailing the memory statistics */
155 long memory_stats[7];
156 char *memorynames[] = {
157 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
158 NULL
161 long swap_stats[7];
162 char *swapnames[] = {
163 /* 0 1 2 3 4 5 */
164 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
165 NULL
169 /* these are for keeping track of the proc array */
171 static int nproc;
172 static int onproc = -1;
173 static int pref_len;
174 static struct kinfo_proc *pbase;
175 static struct kinfo_proc **pref;
177 /* these are for getting the memory statistics */
179 static int pageshift; /* log base 2 of the pagesize */
181 /* define pagetok in terms of pageshift */
183 #define pagetok(size) ((size) << pageshift)
185 /* sorting orders. first is default */
186 char *ordernames[] = {
187 "cpu", "size", "res", "time", "pri", "thr", "pid", "ctime", "pres", NULL
190 /* compare routines */
191 int proc_compare (struct kinfo_proc **, struct kinfo_proc **);
192 int compare_size (struct kinfo_proc **, struct kinfo_proc **);
193 int compare_res (struct kinfo_proc **, struct kinfo_proc **);
194 int compare_time (struct kinfo_proc **, struct kinfo_proc **);
195 int compare_ctime (struct kinfo_proc **, struct kinfo_proc **);
196 int compare_prio(struct kinfo_proc **, struct kinfo_proc **);
197 int compare_thr (struct kinfo_proc **, struct kinfo_proc **);
198 int compare_pid (struct kinfo_proc **, struct kinfo_proc **);
199 int compare_pres(struct kinfo_proc **, struct kinfo_proc **);
201 int (*proc_compares[]) (struct kinfo_proc **,struct kinfo_proc **) = {
202 proc_compare,
203 compare_size,
204 compare_res,
205 compare_time,
206 compare_prio,
207 compare_thr,
208 compare_pid,
209 compare_ctime,
210 compare_pres,
211 NULL
214 static void
215 cputime_percentages(int out[CPU_STATES], struct kinfo_cputime *new,
216 struct kinfo_cputime *old)
218 struct kinfo_cputime diffs;
219 uint64_t total_change, half_total;
221 /* initialization */
222 total_change = 0;
224 diffs.cp_user = new->cp_user - old->cp_user;
225 diffs.cp_nice = new->cp_nice - old->cp_nice;
226 diffs.cp_sys = new->cp_sys - old->cp_sys;
227 diffs.cp_intr = new->cp_intr - old->cp_intr;
228 diffs.cp_idle = new->cp_idle - old->cp_idle;
229 total_change = diffs.cp_user + diffs.cp_nice + diffs.cp_sys +
230 diffs.cp_intr + diffs.cp_idle;
231 old->cp_user = new->cp_user;
232 old->cp_nice = new->cp_nice;
233 old->cp_sys = new->cp_sys;
234 old->cp_intr = new->cp_intr;
235 old->cp_idle = new->cp_idle;
237 /* avoid divide by zero potential */
238 if (total_change == 0)
239 total_change = 1;
241 /* calculate percentages based on overall change, rounding up */
242 half_total = total_change >> 1;
244 out[0] = ((diffs.cp_user * 1000LL + half_total) / total_change);
245 out[1] = ((diffs.cp_nice * 1000LL + half_total) / total_change);
246 out[2] = ((diffs.cp_sys * 1000LL + half_total) / total_change);
247 out[3] = ((diffs.cp_intr * 1000LL + half_total) / total_change);
248 out[4] = ((diffs.cp_idle * 1000LL + half_total) / total_change);
252 machine_init(struct statics *statics)
254 int pagesize;
255 size_t modelen;
256 struct passwd *pw;
257 struct timeval boottime;
259 if (n_cpus < 1) {
260 if (kinfo_get_cpus(&n_cpus))
261 err(1, "kinfo_get_cpus failed");
263 /* get boot time */
264 modelen = sizeof(boottime);
265 if (sysctlbyname("kern.boottime", &boottime, &modelen, NULL, 0) == -1) {
266 /* we have no boottime to report */
267 boottime.tv_sec = -1;
269 modelen = sizeof(smpmode);
270 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
271 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
272 modelen != sizeof(smpmode))
273 smpmode = 0;
275 while ((pw = getpwent()) != NULL) {
276 if ((int)strlen(pw->pw_name) > namelength)
277 namelength = strlen(pw->pw_name);
279 if (namelength < 8)
280 namelength = 8;
281 if (smpmode && namelength > 13)
282 namelength = 13;
283 else if (namelength > 15)
284 namelength = 15;
286 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
287 return -1;
289 if (kinfo_get_sched_ccpu(&ccpu)) {
290 fprintf(stderr, "top: kinfo_get_sched_ccpu failed\n");
291 return (-1);
293 /* this is used in calculating WCPU -- calculate it ahead of time */
294 logcpu = log(loaddouble(ccpu));
296 pbase = NULL;
297 pref = NULL;
298 nproc = 0;
299 onproc = -1;
301 * get the page size with "getpagesize" and calculate pageshift from
302 * it
304 pagesize = getpagesize();
305 pageshift = 0;
306 while (pagesize > 1) {
307 pageshift++;
308 pagesize >>= 1;
311 /* we only need the amount of log(2)1024 for our conversion */
312 pageshift -= LOG1024;
314 /* fill in the statics information */
315 statics->procstate_names = procstatenames;
316 statics->cpustate_names = cpustatenames;
317 statics->memory_names = memorynames;
318 statics->boottime = boottime.tv_sec;
319 statics->swap_names = swapnames;
320 statics->order_names = ordernames;
321 /* we need kvm descriptor in order to show full commands */
322 statics->flags.fullcmds = kd != NULL;
324 /* all done! */
325 return (0);
328 char *
329 format_header(char *uname_field)
331 static char Header[128];
333 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
334 namelength, namelength, uname_field);
336 if (screen_width <= 79)
337 cmdlength = 80;
338 else
339 cmdlength = screen_width;
341 cmdlength = cmdlength - strlen(Header) + 6;
343 return Header;
346 static int swappgsin = -1;
347 static int swappgsout = -1;
348 extern struct timeval timeout;
350 void
351 get_system_info(struct system_info *si)
353 size_t len;
354 int cpu;
356 if (cpu_states == NULL) {
357 cpu_states = malloc(sizeof(*cpu_states) * CPU_STATES * n_cpus);
358 if (cpu_states == NULL)
359 err(1, "malloc");
360 bzero(cpu_states, sizeof(*cpu_states) * CPU_STATES * n_cpus);
362 if (cp_time == NULL) {
363 cp_time = malloc(2 * n_cpus * sizeof(cp_time[0]));
364 if (cp_time == NULL)
365 err(1, "cp_time");
366 cp_old = cp_time + n_cpus;
367 len = n_cpus * sizeof(cp_old[0]);
368 bzero(cp_time, len);
369 if (sysctlbyname("kern.cputime", cp_old, &len, NULL, 0))
370 err(1, "kern.cputime");
372 len = n_cpus * sizeof(cp_time[0]);
373 bzero(cp_time, len);
374 if (sysctlbyname("kern.cputime", cp_time, &len, NULL, 0))
375 err(1, "kern.cputime");
377 getloadavg(si->load_avg, 3);
379 lastpid = 0;
381 /* convert cp_time counts to percentages */
382 for (cpu = 0; cpu < n_cpus; ++cpu) {
383 cputime_percentages(cpu_states + cpu * CPU_STATES,
384 &cp_time[cpu], &cp_old[cpu]);
387 /* sum memory & swap statistics */
389 struct vmmeter vmm;
390 struct vmstats vms;
391 size_t vms_size = sizeof(vms);
392 size_t vmm_size = sizeof(vmm);
393 static unsigned int swap_delay = 0;
394 static int swapavail = 0;
395 static int swapfree = 0;
396 static int bufspace = 0;
398 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0))
399 err(1, "sysctlbyname: vm.vmstats");
401 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0))
402 err(1, "sysctlbyname: vm.vmmeter");
404 if (kinfo_get_vfs_bufspace(&bufspace))
405 err(1, "kinfo_get_vfs_bufspace");
407 /* convert memory stats to Kbytes */
408 memory_stats[0] = pagetok(vms.v_active_count);
409 memory_stats[1] = pagetok(vms.v_inactive_count);
410 memory_stats[2] = pagetok(vms.v_wire_count);
411 memory_stats[3] = pagetok(vms.v_cache_count);
412 memory_stats[4] = bufspace / 1024;
413 memory_stats[5] = pagetok(vms.v_free_count);
414 memory_stats[6] = -1;
416 /* first interval */
417 if (swappgsin < 0) {
418 swap_stats[4] = 0;
419 swap_stats[5] = 0;
421 /* compute differences between old and new swap statistic */
422 else {
423 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin)));
424 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout)));
427 swappgsin = vmm.v_swappgsin;
428 swappgsout = vmm.v_swappgsout;
430 /* call CPU heavy swapmode() only for changes */
431 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
432 swap_stats[3] = swapmode(&swapavail, &swapfree);
433 swap_stats[0] = swapavail;
434 swap_stats[1] = swapavail - swapfree;
435 swap_stats[2] = swapfree;
437 swap_delay = 1;
438 swap_stats[6] = -1;
441 /* set arrays and strings */
442 si->cpustates = cpu_states;
443 si->memory = memory_stats;
444 si->swap = swap_stats;
447 if (lastpid > 0) {
448 si->last_pid = lastpid;
449 } else {
450 si->last_pid = -1;
455 static struct handle handle;
457 caddr_t
458 get_process_info(struct system_info *si, struct process_select *sel,
459 int compare_index)
461 int i;
462 int total_procs;
463 int active_procs;
464 struct kinfo_proc **prefp;
465 struct kinfo_proc *pp;
467 /* these are copied out of sel for speed */
468 int show_idle;
469 int show_system;
470 int show_uid;
473 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
474 if (nproc > onproc)
475 pref = (struct kinfo_proc **)realloc(pref, sizeof(struct kinfo_proc *)
476 * (onproc = nproc));
477 if (pref == NULL || pbase == NULL) {
478 (void)fprintf(stderr, "top: Out of memory.\n");
479 quit(23);
481 /* get a pointer to the states summary array */
482 si->procstates = process_states;
484 /* set up flags which define what we are going to select */
485 show_idle = sel->idle;
486 show_system = sel->system;
487 show_uid = sel->uid != -1;
488 show_fullcmd = sel->fullcmd;
490 /* count up process states and get pointers to interesting procs */
491 total_procs = 0;
492 active_procs = 0;
493 memset((char *)process_states, 0, sizeof(process_states));
494 prefp = pref;
495 for (pp = pbase, i = 0; i < nproc; pp++, i++) {
497 * Place pointers to each valid proc structure in pref[].
498 * Process slots that are actually in use have a non-zero
499 * status field. Processes with P_SYSTEM set are system
500 * processes---these get ignored unless show_sysprocs is set.
502 if ((show_threads && (LP(pp, pid) == -1)) ||
503 (show_system || ((PP(pp, flags) & P_SYSTEM) == 0))) {
504 total_procs++;
505 if (LP(pp, stat) == LSRUN)
506 process_states[0]++;
507 process_states[PP(pp, stat)]++;
508 if ((show_threads && (LP(pp, pid) == -1)) ||
509 (show_idle || (LP(pp, pctcpu) != 0) ||
510 (LP(pp, stat) == LSRUN)) &&
511 (!show_uid || PP(pp, ruid) == (uid_t) sel->uid)) {
512 *prefp++ = pp;
513 active_procs++;
518 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *),
519 (int (*)(const void *, const void *))proc_compares[compare_index]);
521 /* remember active and total counts */
522 si->p_total = total_procs;
523 si->p_active = pref_len = active_procs;
525 /* pass back a handle */
526 handle.next_proc = pref;
527 handle.remaining = active_procs;
528 return ((caddr_t) & handle);
531 char fmt[MAX_COLS]; /* static area where result is built */
533 char *
534 format_next_process(caddr_t xhandle, char *(*get_userid) (int))
536 struct kinfo_proc *pp;
537 long cputime;
538 long ccputime;
539 double pct;
540 struct handle *hp;
541 char status[16];
542 int state;
543 int xnice;
544 char **comm_full;
545 char *comm;
546 char cputime_fmt[10], ccputime_fmt[10];
548 /* find and remember the next proc structure */
549 hp = (struct handle *)xhandle;
550 pp = *(hp->next_proc++);
551 hp->remaining--;
553 /* get the process's command name */
554 if (show_fullcmd) {
555 if ((comm_full = kvm_getargv(kd, pp, 0)) == NULL) {
556 return (fmt);
559 else {
560 comm = PP(pp, comm);
564 * Convert the process's runtime from microseconds to seconds. This
565 * time includes the interrupt time to be in compliance with ps output.
567 cputime = (LP(pp, uticks) + LP(pp, sticks) + LP(pp, iticks)) / 1000000;
568 ccputime = cputime + PP(pp, cru).ru_stime.tv_sec + PP(pp, cru).ru_utime.tv_sec;
569 format_time(cputime, cputime_fmt, sizeof(cputime_fmt));
570 format_time(ccputime, ccputime_fmt, sizeof(ccputime_fmt));
572 /* calculate the base for cpu percentages */
573 pct = pctdouble(LP(pp, pctcpu));
575 /* generate "STATE" field */
576 switch (state = LP(pp, stat)) {
577 case LSRUN:
578 if (smpmode && LP(pp, tdflags) & TDF_RUNNING)
579 sprintf(status, "CPU%d", LP(pp, cpuid));
580 else
581 strcpy(status, "RUN");
582 break;
583 case LSSLEEP:
584 if (LP(pp, wmesg) != NULL) {
585 sprintf(status, "%.6s", LP(pp, wmesg));
586 break;
588 /* fall through */
589 default:
591 if (state >= 0 &&
592 (unsigned)state < sizeof(state_abbrev) / sizeof(*state_abbrev))
593 sprintf(status, "%.6s", state_abbrev[(unsigned char)state]);
594 else
595 sprintf(status, "?%5d", state);
596 break;
599 if (PP(pp, stat) == SZOMB)
600 strcpy(status, "ZOMB");
603 * idle time 0 - 31 -> nice value +21 - +52 normal time -> nice
604 * value -20 - +20 real time 0 - 31 -> nice value -52 - -21 thread
605 * 0 - 31 -> nice value -53 -
607 switch (LP(pp, rtprio.type)) {
608 case RTP_PRIO_REALTIME:
609 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio);
610 break;
611 case RTP_PRIO_IDLE:
612 xnice = PRIO_MAX + 1 + LP(pp, rtprio.prio);
613 break;
614 case RTP_PRIO_THREAD:
615 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio);
616 break;
617 default:
618 xnice = PP(pp, nice);
619 break;
622 /* format this entry */
623 snprintf(fmt, sizeof(fmt),
624 smpmode ? smp_Proc_format : up_Proc_format,
625 (int)PP(pp, pid),
626 namelength, namelength,
627 get_userid(PP(pp, ruid)),
628 (int)((show_threads && (LP(pp, pid) == -1)) ?
629 LP(pp, tdprio) : LP(pp, prio)),
630 (int)xnice,
631 format_k(PROCSIZE(pp)),
632 format_k(pagetok(VP(pp, rssize))),
633 format_k(pagetok(VP(pp, prssize))),
634 status,
635 (int)(smpmode ? LP(pp, cpuid) : 0),
636 cputime_fmt,
637 ccputime_fmt,
638 100.0 * pct,
639 cmdlength,
640 show_fullcmd ? *comm_full : comm);
642 /* return the result */
643 return (fmt);
646 /* comparison routines for qsort */
649 * proc_compare - comparison function for "qsort"
650 * Compares the resource consumption of two processes using five
651 * distinct keys. The keys (in descending order of importance) are:
652 * percent cpu, cpu ticks, state, resident set size, total virtual
653 * memory usage. The process states are ordered as follows (from least
654 * to most important): WAIT, zombie, sleep, stop, start, run. The
655 * array declaration below maps a process state index into a number
656 * that reflects this ordering.
659 static unsigned char sorted_state[] =
661 0, /* not used */
662 3, /* sleep */
663 1, /* ABANDONED (WAIT) */
664 6, /* run */
665 5, /* start */
666 2, /* zombie */
667 4 /* stop */
671 #define ORDERKEY_PCTCPU \
672 if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \
673 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
675 #define CPTICKS(p) (LP(p, uticks) + LP(p, sticks) + LP(p, iticks))
677 #define ORDERKEY_CPTICKS \
678 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
679 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
681 #define CTIME(p) (((LP(p, uticks) + LP(p, sticks) + LP(p, iticks))/1000000) + \
682 PP(p, cru).ru_stime.tv_sec + PP(p, cru).ru_utime.tv_sec)
684 #define ORDERKEY_CTIME \
685 if ((result = CTIME(p2) > CTIME(p1) ? 1 : \
686 CTIME(p2) < CTIME(p1) ? -1 : 0) == 0)
688 #define ORDERKEY_STATE \
689 if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \
690 sorted_state[(unsigned char) PP(p1, stat)]) == 0)
692 #define ORDERKEY_PRIO \
693 if ((result = LP(p2, prio) - LP(p1, prio)) == 0)
695 #define ORDERKEY_KTHREADS \
696 if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0)
698 #define ORDERKEY_KTHREADS_PRIO \
699 if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0)
701 #define ORDERKEY_RSSIZE \
702 if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0)
704 #define ORDERKEY_MEM \
705 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
707 #define ORDERKEY_PID \
708 if ( (result = PP(p1, pid) - PP(p2, pid)) == 0)
710 #define ORDERKEY_PRSSIZE \
711 if((result = VP(p2, prssize) - VP(p1, prssize)) == 0)
713 /* compare_cpu - the comparison function for sorting by cpu percentage */
716 proc_compare(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
718 struct kinfo_proc *p1;
719 struct kinfo_proc *p2;
720 int result;
721 pctcpu lresult;
723 /* remove one level of indirection */
724 p1 = *(struct kinfo_proc **) pp1;
725 p2 = *(struct kinfo_proc **) pp2;
727 ORDERKEY_PCTCPU
728 ORDERKEY_CPTICKS
729 ORDERKEY_STATE
730 ORDERKEY_PRIO
731 ORDERKEY_RSSIZE
732 ORDERKEY_MEM
735 return (result);
738 /* compare_size - the comparison function for sorting by total memory usage */
741 compare_size(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
743 struct kinfo_proc *p1;
744 struct kinfo_proc *p2;
745 int result;
746 pctcpu lresult;
748 /* remove one level of indirection */
749 p1 = *(struct kinfo_proc **) pp1;
750 p2 = *(struct kinfo_proc **) pp2;
752 ORDERKEY_MEM
753 ORDERKEY_RSSIZE
754 ORDERKEY_PCTCPU
755 ORDERKEY_CPTICKS
756 ORDERKEY_STATE
757 ORDERKEY_PRIO
760 return (result);
763 /* compare_res - the comparison function for sorting by resident set size */
766 compare_res(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
768 struct kinfo_proc *p1;
769 struct kinfo_proc *p2;
770 int result;
771 pctcpu lresult;
773 /* remove one level of indirection */
774 p1 = *(struct kinfo_proc **) pp1;
775 p2 = *(struct kinfo_proc **) pp2;
777 ORDERKEY_RSSIZE
778 ORDERKEY_MEM
779 ORDERKEY_PCTCPU
780 ORDERKEY_CPTICKS
781 ORDERKEY_STATE
782 ORDERKEY_PRIO
785 return (result);
788 /* compare_pres - the comparison function for sorting by proportional resident set size */
791 compare_pres(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
793 struct kinfo_proc *p1;
794 struct kinfo_proc *p2;
795 int result;
796 pctcpu lresult;
798 /* remove one level of indirection */
799 p1 = *(struct kinfo_proc **) pp1;
800 p2 = *(struct kinfo_proc **) pp2;
802 ORDERKEY_PRSSIZE
803 ORDERKEY_RSSIZE
804 ORDERKEY_MEM
805 ORDERKEY_PCTCPU
806 ORDERKEY_CPTICKS
807 ORDERKEY_STATE
808 ORDERKEY_PRIO
811 return (result);
814 /* compare_time - the comparison function for sorting by total cpu time */
817 compare_time(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
819 struct kinfo_proc *p1;
820 struct kinfo_proc *p2;
821 int result;
822 pctcpu lresult;
824 /* remove one level of indirection */
825 p1 = *(struct kinfo_proc **) pp1;
826 p2 = *(struct kinfo_proc **) pp2;
828 ORDERKEY_CPTICKS
829 ORDERKEY_PCTCPU
830 ORDERKEY_KTHREADS
831 ORDERKEY_KTHREADS_PRIO
832 ORDERKEY_STATE
833 ORDERKEY_PRIO
834 ORDERKEY_RSSIZE
835 ORDERKEY_MEM
838 return (result);
842 compare_ctime(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
844 struct kinfo_proc *p1;
845 struct kinfo_proc *p2;
846 int result;
847 pctcpu lresult;
849 /* remove one level of indirection */
850 p1 = *(struct kinfo_proc **) pp1;
851 p2 = *(struct kinfo_proc **) pp2;
853 ORDERKEY_CTIME
854 ORDERKEY_PCTCPU
855 ORDERKEY_KTHREADS
856 ORDERKEY_KTHREADS_PRIO
857 ORDERKEY_STATE
858 ORDERKEY_PRIO
859 ORDERKEY_RSSIZE
860 ORDERKEY_MEM
863 return (result);
866 /* compare_prio - the comparison function for sorting by cpu percentage */
869 compare_prio(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
871 struct kinfo_proc *p1;
872 struct kinfo_proc *p2;
873 int result;
874 pctcpu lresult;
876 /* remove one level of indirection */
877 p1 = *(struct kinfo_proc **) pp1;
878 p2 = *(struct kinfo_proc **) pp2;
880 ORDERKEY_KTHREADS
881 ORDERKEY_KTHREADS_PRIO
882 ORDERKEY_PRIO
883 ORDERKEY_CPTICKS
884 ORDERKEY_PCTCPU
885 ORDERKEY_STATE
886 ORDERKEY_RSSIZE
887 ORDERKEY_MEM
890 return (result);
894 compare_thr(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
896 struct kinfo_proc *p1;
897 struct kinfo_proc *p2;
898 int result;
899 pctcpu lresult;
901 /* remove one level of indirection */
902 p1 = *(struct kinfo_proc **)pp1;
903 p2 = *(struct kinfo_proc **)pp2;
905 ORDERKEY_KTHREADS
906 ORDERKEY_KTHREADS_PRIO
907 ORDERKEY_CPTICKS
908 ORDERKEY_PCTCPU
909 ORDERKEY_STATE
910 ORDERKEY_RSSIZE
911 ORDERKEY_MEM
914 return (result);
917 /* compare_pid - the comparison function for sorting by process id */
920 compare_pid(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
922 struct kinfo_proc *p1;
923 struct kinfo_proc *p2;
924 int result;
926 /* remove one level of indirection */
927 p1 = *(struct kinfo_proc **) pp1;
928 p2 = *(struct kinfo_proc **) pp2;
930 ORDERKEY_PID
933 return(result);
937 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
938 * the process does not exist.
939 * It is EXTREMLY IMPORTANT that this function work correctly.
940 * If top runs setuid root (as in SVR4), then this function
941 * is the only thing that stands in the way of a serious
942 * security problem. It validates requests for the "kill"
943 * and "renice" commands.
947 proc_owner(int pid)
949 int xcnt;
950 struct kinfo_proc **prefp;
951 struct kinfo_proc *pp;
953 prefp = pref;
954 xcnt = pref_len;
955 while (--xcnt >= 0) {
956 pp = *prefp++;
957 if (PP(pp, pid) == (pid_t) pid) {
958 return ((int)PP(pp, ruid));
961 return (-1);
966 * swapmode is based on a program called swapinfo written
967 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
970 swapmode(int *retavail, int *retfree)
972 int n;
973 int pagesize = getpagesize();
974 struct kvm_swap swapary[1];
976 *retavail = 0;
977 *retfree = 0;
979 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
981 n = kvm_getswapinfo(kd, swapary, 1, 0);
982 if (n < 0 || swapary[0].ksw_total == 0)
983 return (0);
985 *retavail = CONVERT(swapary[0].ksw_total);
986 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
988 n = (int)((double)swapary[0].ksw_used * 100.0 /
989 (double)swapary[0].ksw_total);
990 return (n);