When using visibility macros, sys/cdefs.h must be included first.
[dragonfly/netmp.git] / usr.bin / top / machine.c
blobfefa0f4c70bf6f1b3764394760946b3f82546ad0
1 /*
2 * top - a top users display for Unix
4 * SYNOPSIS: For FreeBSD-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 FreeBSD 2.2
13 * Works for:
14 * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x
16 * LIBS: -lkvm
18 * AUTHOR: Christos Zoulas <christos@ee.cornell.edu>
19 * Steven Wallace <swallace@freebsd.org>
20 * Wolfram Schneider <wosch@FreeBSD.org>
21 * Hiten Pandya <hmp@backplane.com>
23 * $FreeBSD: src/usr.bin/top/machine.c,v 1.29.2.2 2001/07/31 20:27:05 tmm Exp $
24 * $DragonFly: src/usr.bin/top/machine.c,v 1.18 2005/11/14 18:50:18 dillon Exp $
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/signal.h>
31 #include <sys/param.h>
33 #include "os.h"
34 #include <err.h>
35 #include <kvm.h>
36 #include <stdio.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/time.h>
43 #include <sys/user.h>
44 #include <sys/vmmeter.h>
45 #include <sys/resource.h>
46 #include <sys/rtprio.h>
48 /* Swap */
49 #include <stdlib.h>
50 #include <sys/conf.h>
52 #include <osreldate.h> /* for changes in kernel structures */
54 #include <sys/kinfo.h>
55 #include <kinfo.h>
56 #include "top.h"
57 #include "machine.h"
59 static int check_nlist(struct nlist *);
60 static int getkval(unsigned long, int *, int, char *);
61 extern char* printable(char *);
62 int swapmode(int *retavail, int *retfree);
63 static int smpmode;
64 static int namelength;
65 static int cmdlength;
67 /*
68 * needs to be a global symbol, so wrapper can be
69 * modified accordingly.
71 static int show_threads = 0;
73 /* get_process_info passes back a handle. This is what it looks like: */
75 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_proc . field)
85 #define EP(pp, field) ((pp)->kp_eproc . field)
86 #define TP(pp, field) ((pp)->kp_thread . field)
87 #define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
89 /* define what weighted cpu is. */
90 #define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \
91 ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu))))
93 /* what we consider to be process size: */
94 #define PROCSIZE(pp) (VP((pp), vm_map.size) / 1024)
97 * These definitions control the format of the per-process area
100 static char smp_header[] =
101 " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND";
103 #define smp_Proc_format \
104 "%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
106 static char up_header[] =
107 " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
109 #define up_Proc_format \
110 "%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s"
114 /* process state names for the "STATE" column of the display */
115 /* the extra nulls in the string "run" are for adding a slash and
116 the processor number when needed */
118 char *state_abbrev[] =
120 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB",
124 static kvm_t *kd;
126 /* values that we stash away in _init and use in later routines */
128 static double logcpu;
130 static long lastpid;
131 static long cnt;
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 "", " starting, ", " running, ", " sleeping, ", " stopped, ",
143 " zombie, ",
144 NULL
147 /* these are for detailing the cpu states */
148 #define CPU_STATES 5
149 int cpu_states[CPU_STATES];
150 char *cpustatenames[CPU_STATES + 1] = {
151 "user", "nice", "system", "interrupt", "idle", NULL
154 /* these are for detailing the memory statistics */
156 int memory_stats[7];
157 char *memorynames[] = {
158 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
159 NULL
162 int swap_stats[7];
163 char *swapnames[] = {
164 /* 0 1 2 3 4 5 */
165 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
166 NULL
170 /* these are for keeping track of the proc array */
172 static int nproc;
173 static int onproc = -1;
174 static int pref_len;
175 static struct kinfo_proc *pbase;
176 static struct kinfo_proc **pref;
178 /* these are for getting the memory statistics */
180 static int pageshift; /* log base 2 of the pagesize */
182 /* define pagetok in terms of pageshift */
184 #define pagetok(size) ((size) << pageshift)
186 #ifdef ORDER
187 /* sorting orders. first is default */
188 char *ordernames[] = {
189 "cpu", "size", "res", "time", "pri", "thr", NULL
191 #endif
193 static void
194 cputime_percentages(int out[CPU_STATES], struct kinfo_cputime *new,
195 struct kinfo_cputime *old)
197 struct kinfo_cputime diffs;
198 int i;
199 uint64_t total_change, half_total;
201 /* initialization */
202 total_change = 0;
204 diffs.cp_user = new->cp_user - old->cp_user;
205 diffs.cp_nice = new->cp_nice - old->cp_nice;
206 diffs.cp_sys = new->cp_sys - old->cp_sys;
207 diffs.cp_intr = new->cp_intr - old->cp_intr;
208 diffs.cp_idle = new->cp_idle - old->cp_idle;
209 total_change = diffs.cp_user + diffs.cp_nice + diffs.cp_sys +
210 diffs.cp_intr + diffs.cp_idle;
211 old->cp_user = new->cp_user;
212 old->cp_nice = new->cp_nice;
213 old->cp_sys = new->cp_sys;
214 old->cp_intr = new->cp_intr;
215 old->cp_idle = new->cp_idle;
217 /* avoid divide by zero potential */
218 if (total_change == 0)
219 total_change = 1;
221 /* calculate percentages based on overall change, rounding up */
222 half_total = total_change >> 1;
224 out[0] = ((diffs.cp_user * 1000LL + half_total) / total_change);
225 out[1] = ((diffs.cp_nice * 1000LL + half_total) / total_change);
226 out[2] = ((diffs.cp_sys * 1000LL + half_total) / total_change);
227 out[3] = ((diffs.cp_intr * 1000LL + half_total) / total_change);
228 out[4] = ((diffs.cp_idle * 1000LL + half_total) / total_change);
232 machine_init(struct statics *statics)
234 register int i = 0;
235 register int pagesize;
236 size_t modelen;
237 struct passwd *pw;
239 modelen = sizeof(smpmode);
240 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
241 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
242 modelen != sizeof(smpmode))
243 smpmode = 0;
245 while ((pw = getpwent()) != NULL) {
246 if (strlen(pw->pw_name) > namelength)
247 namelength = strlen(pw->pw_name);
249 if (namelength < 8)
250 namelength = 8;
251 if (smpmode && namelength > 13)
252 namelength = 13;
253 else if (namelength > 15)
254 namelength = 15;
256 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
257 return -1;
259 if (kinfo_get_sched_ccpu(&ccpu)) {
260 fprintf(stderr, "top: kinfo_get_sched_ccpu failed\n");
261 return(-1);
264 /* this is used in calculating WCPU -- calculate it ahead of time */
265 logcpu = log(loaddouble(ccpu));
267 pbase = NULL;
268 pref = NULL;
269 nproc = 0;
270 onproc = -1;
271 /* get the page size with "getpagesize" and calculate pageshift from it */
272 pagesize = getpagesize();
273 pageshift = 0;
274 while (pagesize > 1)
276 pageshift++;
277 pagesize >>= 1;
280 /* we only need the amount of log(2)1024 for our conversion */
281 pageshift -= LOG1024;
283 /* fill in the statics information */
284 statics->procstate_names = procstatenames;
285 statics->cpustate_names = cpustatenames;
286 statics->memory_names = memorynames;
287 statics->swap_names = swapnames;
288 #ifdef ORDER
289 statics->order_names = ordernames;
290 #endif
292 /* all done! */
293 return(0);
296 char *format_header(register char *uname_field)
298 register char *ptr;
299 static char Header[128];
301 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
302 namelength, namelength, uname_field);
304 if (screen_width <= 79)
305 cmdlength = 80;
306 else
307 cmdlength = 89;
309 cmdlength = cmdlength - strlen(Header) + 6;
311 return Header;
314 static int swappgsin = -1;
315 static int swappgsout = -1;
316 extern struct timeval timeout;
318 void
319 get_system_info(struct system_info *si)
321 long total;
322 int mib[2];
323 struct timeval boottime;
324 size_t bt_size;
326 if (kinfo_get_sched_cputime(&cp_time))
327 err(1, "kinfo_get_sched_cputime failed");
329 getloadavg(si->load_avg, 3);
331 lastpid = 0;
333 /* convert cp_time counts to percentages */
334 cputime_percentages(cpu_states, &cp_time, &cp_old);
336 /* sum memory & swap statistics */
338 struct vmmeter vmm;
339 struct vmstats vms;
340 int vms_size = sizeof(vms);
341 int vmm_size = sizeof(vmm);
342 static unsigned int swap_delay = 0;
343 static int swapavail = 0;
344 static int swapfree = 0;
345 static int bufspace = 0;
347 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0))
348 err(1, "sysctlbyname: vm.vmstats");
350 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0))
351 err(1, "sysctlbyname: vm.vmmeter");
353 if (kinfo_get_vfs_bufspace(&bufspace))
354 err(1, "kinfo_get_vfs_bufspace");
356 /* convert memory stats to Kbytes */
357 memory_stats[0] = pagetok(vms.v_active_count);
358 memory_stats[1] = pagetok(vms.v_inactive_count);
359 memory_stats[2] = pagetok(vms.v_wire_count);
360 memory_stats[3] = pagetok(vms.v_cache_count);
361 memory_stats[4] = bufspace / 1024;
362 memory_stats[5] = pagetok(vms.v_free_count);
363 memory_stats[6] = -1;
365 /* first interval */
366 if (swappgsin < 0) {
367 swap_stats[4] = 0;
368 swap_stats[5] = 0;
371 /* compute differences between old and new swap statistic */
372 else {
373 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin)));
374 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout)));
377 swappgsin = vmm.v_swappgsin;
378 swappgsout = vmm.v_swappgsout;
380 /* call CPU heavy swapmode() only for changes */
381 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
382 swap_stats[3] = swapmode(&swapavail, &swapfree);
383 swap_stats[0] = swapavail;
384 swap_stats[1] = swapavail - swapfree;
385 swap_stats[2] = swapfree;
387 swap_delay = 1;
388 swap_stats[6] = -1;
391 /* set arrays and strings */
392 si->cpustates = cpu_states;
393 si->memory = memory_stats;
394 si->swap = swap_stats;
397 if(lastpid > 0) {
398 si->last_pid = lastpid;
399 } else {
400 si->last_pid = -1;
404 * Print how long system has been up.
405 * (Found by looking getting "boottime" from the kernel)
407 mib[0] = CTL_KERN;
408 mib[1] = KERN_BOOTTIME;
409 bt_size = sizeof(boottime);
410 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 &&
411 boottime.tv_sec != 0) {
412 si->boottime = boottime;
413 } else {
414 si->boottime.tv_sec = -1;
418 static struct handle handle;
420 caddr_t get_process_info(struct system_info *si, struct process_select *sel,
421 int (*compare)())
423 register int i;
424 register int total_procs;
425 register int active_procs;
426 register struct kinfo_proc **prefp;
427 register struct kinfo_proc *pp;
429 /* these are copied out of sel for speed */
430 int show_idle;
431 int show_self;
432 int show_system;
433 int show_only_threads;
434 int show_uid;
435 int show_command;
438 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
439 if (nproc > onproc)
440 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
441 * (onproc = nproc));
442 if (pref == NULL || pbase == NULL) {
443 (void) fprintf(stderr, "top: Out of memory.\n");
444 quit(23);
446 /* get a pointer to the states summary array */
447 si->procstates = process_states;
449 /* set up flags which define what we are going to select */
450 show_idle = sel->idle;
451 show_self = sel->self;
452 show_system = sel->system;
453 show_threads = sel->threads;
454 show_only_threads = sel->only_threads;
455 show_uid = sel->uid != -1;
456 show_command = sel->command != NULL;
458 /* count up process states and get pointers to interesting procs */
459 total_procs = 0;
460 active_procs = 0;
461 memset((char *)process_states, 0, sizeof(process_states));
462 prefp = pref;
463 for (pp = pbase, i = 0; i < nproc; pp++, i++)
466 * Place pointers to each valid proc structure in pref[].
467 * Process slots that are actually in use have a non-zero
468 * status field. Processes with P_SYSTEM set are system
469 * processes---these get ignored unless show_sysprocs is set.
471 if ((show_threads && (TP(pp, td_proc) == NULL)) ||
472 (!show_only_threads && (PP(pp, p_stat) != 0 &&
473 (show_self != PP(pp, p_pid)) &&
474 (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0)))))
476 total_procs++;
477 process_states[(unsigned char) PP(pp, p_stat)]++;
478 if ((show_threads && (TP(pp, td_proc) == NULL)) ||
479 (!show_only_threads && (PP(pp, p_stat) != SZOMB) &&
480 (show_idle || (PP(pp, p_pctcpu) != 0) ||
481 (PP(pp, p_stat) == SRUN)) &&
482 (!show_uid || EP(pp, e_ucred.cr_ruid) == (uid_t)sel->uid)))
484 *prefp++ = pp;
485 active_procs++;
490 /* if requested, sort the "interesting" processes */
491 if (compare != NULL)
493 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
496 /* remember active and total counts */
497 si->p_total = total_procs;
498 si->p_active = pref_len = active_procs;
500 /* pass back a handle */
501 handle.next_proc = pref;
502 handle.remaining = active_procs;
503 return((caddr_t)&handle);
506 char fmt[128]; /* static area where result is built */
508 char *format_next_process(caddr_t handle, char *(*get_userid)())
510 struct kinfo_proc *pp;
511 long cputime;
512 double pct;
513 struct handle *hp;
514 char status[16];
515 char const *wrapper;
516 int state;
517 int nice;
519 /* find and remember the next proc structure */
520 hp = (struct handle *)handle;
521 pp = *(hp->next_proc++);
522 hp->remaining--;
524 /* set the wrapper for the process/thread name */
525 if ((PP(pp, p_flag) & P_SWAPPEDOUT))
526 wrapper = "[]"; /* swapped process [pname] */
527 else if (((PP(pp, p_flag) & P_SYSTEM) != 0) && (TP(pp, td_proc) != NULL))
528 wrapper = "()"; /* system process (pname) */
529 else if (show_threads && (TP(pp, td_proc) == NULL))
530 wrapper = "<>"; /* pure kernel threads <thread> */
531 else
532 wrapper = NULL;
534 /* get the process's command name */
535 if (wrapper != NULL) {
536 char *comm = TP(pp, td_comm);
537 #define COMSIZ sizeof(TP(pp, td_comm))
538 char buf[COMSIZ];
539 (void) strncpy(buf, comm, COMSIZ);
540 comm[0] = wrapper[0];
541 (void) strncpy(&comm[1], buf, COMSIZ - 2);
542 comm[COMSIZ - 2] = '\0';
543 (void) strncat(comm, &wrapper[1], COMSIZ - 1);
544 comm[COMSIZ - 1] = '\0';
548 * Convert the process's runtime from microseconds to seconds. This
549 * time includes the interrupt time although that is not wanted here.
550 * ps(1) is similarly sloppy.
552 cputime = (EP(pp, e_uticks) + EP(pp, e_sticks)) / 1000000;
554 /* calculate the base for cpu percentages */
555 pct = pctdouble(PP(pp, p_pctcpu));
557 /* generate "STATE" field */
558 switch (state = PP(pp, p_stat)) {
559 case SRUN:
560 if (smpmode && TP(pp, td_flags) & TDF_RUNNING)
561 sprintf(status, "CPU%d", EP(pp, e_cpuid));
562 else
563 strcpy(status, "RUN");
564 break;
565 case SSLEEP:
566 if (TP(pp, td_wmesg) != NULL) {
567 sprintf(status, "%.6s", EP(pp, e_wmesg));
568 break;
570 /* fall through */
571 default:
573 if (state >= 0 &&
574 state < sizeof(state_abbrev) / sizeof(*state_abbrev))
575 sprintf(status, "%.6s", state_abbrev[(unsigned char) state]);
576 else
577 sprintf(status, "?%5d", state);
578 break;
582 * idle time 0 - 31 -> nice value +21 - +52
583 * normal time -> nice value -20 - +20
584 * real time 0 - 31 -> nice value -52 - -21
585 * thread 0 - 31 -> nice value -53 -
587 switch(PP(pp, p_rtprio.type)) {
588 case RTP_PRIO_REALTIME:
589 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio);
590 break;
591 case RTP_PRIO_IDLE:
592 nice = PRIO_MAX + 1 + PP(pp, p_rtprio.prio);
593 break;
594 case RTP_PRIO_THREAD:
595 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - PP(pp, p_rtprio.prio);
596 break;
597 default:
598 nice = PP(pp, p_nice);
599 break;
603 /* format this entry */
604 snprintf(fmt, sizeof(fmt),
605 smpmode ? smp_Proc_format : up_Proc_format,
606 PP(pp, p_pid),
607 namelength, namelength,
608 (*get_userid)(EP(pp, e_ucred.cr_ruid)),
609 (show_threads && (TP(pp, td_proc) == NULL)) ? TP(pp, td_pri) :
610 PP(pp, p_usdata.bsd4.priority),
611 nice,
612 format_k2(PROCSIZE(pp)),
613 format_k2(pagetok(VP(pp, vm_rssize))),
614 status,
615 smpmode ? EP(pp, e_cpuid) : 0,
616 format_time(cputime),
617 100.0 * weighted_cpu(pct, pp),
618 100.0 * pct,
619 cmdlength,
620 printable(TP(pp, td_comm)));
622 /* return the result */
623 return(fmt);
628 * check_nlist(nlst) - checks the nlist to see if any symbols were not
629 * found. For every symbol that was not found, a one-line
630 * message is printed to stderr. The routine returns the
631 * number of symbols NOT found.
634 static int check_nlist(register struct nlist *nlst)
636 register int i;
638 /* check to see if we got ALL the symbols we requested */
639 /* this will write one line to stderr for every symbol not found */
641 i = 0;
642 while (nlst->n_name != NULL)
644 if (nlst->n_type == 0)
646 /* this one wasn't found */
647 (void) fprintf(stderr, "kernel: no symbol named `%s'\n",
648 nlst->n_name);
649 i = 1;
651 nlst++;
654 return(i);
657 /* comparison routines for qsort */
660 * proc_compare - comparison function for "qsort"
661 * Compares the resource consumption of two processes using five
662 * distinct keys. The keys (in descending order of importance) are:
663 * percent cpu, cpu ticks, state, resident set size, total virtual
664 * memory usage. The process states are ordered as follows (from least
665 * to most important): WAIT, zombie, sleep, stop, start, run. The
666 * array declaration below maps a process state index into a number
667 * that reflects this ordering.
670 static unsigned char sorted_state[] =
672 0, /* not used */
673 3, /* sleep */
674 1, /* ABANDONED (WAIT) */
675 6, /* run */
676 5, /* start */
677 2, /* zombie */
678 4 /* stop */
682 #define ORDERKEY_PCTCPU \
683 if (lresult = (long) PP(p2, p_pctcpu) - (long) PP(p1, p_pctcpu), \
684 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
686 #define CPTICKS(p) (EP(p, e_uticks) + EP(p, e_sticks))
688 #define ORDERKEY_CPTICKS \
689 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
690 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
692 #define ORDERKEY_STATE \
693 if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
694 sorted_state[(unsigned char) PP(p1, p_stat)]) == 0)
696 #define ORDERKEY_PRIO \
697 if ((result = PP(p2, p_usdata.bsd4.priority) - PP(p1, p_usdata.bsd4.priority)) == 0)
699 #define ORDERKEY_KTHREADS \
700 if ((result = (TP(p1, td_proc) == NULL) - (TP(p2, td_proc) == NULL)) == 0)
702 #define ORDERKEY_KTHREADS_PRIO \
703 if ((result = TP(p2, td_pri) - TP(p1, td_pri)) == 0)
705 #define ORDERKEY_RSSIZE \
706 if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
708 #define ORDERKEY_MEM \
709 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
711 /* compare_cpu - the comparison function for sorting by cpu percentage */
714 #ifdef ORDER
715 compare_cpu(struct proc **pp1, struct proc **pp2)
716 #else
717 proc_compare(struct proc **pp1, struct proc **pp2)
718 #endif
720 register struct kinfo_proc *p1;
721 register struct kinfo_proc *p2;
722 register int result;
723 register pctcpu lresult;
725 /* remove one level of indirection */
726 p1 = *(struct kinfo_proc **) pp1;
727 p2 = *(struct kinfo_proc **) pp2;
729 ORDERKEY_PCTCPU
730 ORDERKEY_CPTICKS
731 ORDERKEY_STATE
732 ORDERKEY_PRIO
733 ORDERKEY_RSSIZE
734 ORDERKEY_MEM
737 return(result);
740 #ifdef ORDER
741 /* compare routines */
742 int compare_size(), compare_res(), compare_time(), compare_prio(), compare_thr();
744 int (*proc_compares[])() = {
745 compare_cpu,
746 compare_size,
747 compare_res,
748 compare_time,
749 compare_prio,
750 compare_thr,
751 NULL
754 /* compare_size - the comparison function for sorting by total memory usage */
757 compare_size(struct proc **pp1, struct proc **pp2)
759 register struct kinfo_proc *p1;
760 register struct kinfo_proc *p2;
761 register int result;
762 register pctcpu lresult;
764 /* remove one level of indirection */
765 p1 = *(struct kinfo_proc **) pp1;
766 p2 = *(struct kinfo_proc **) pp2;
768 ORDERKEY_MEM
769 ORDERKEY_RSSIZE
770 ORDERKEY_PCTCPU
771 ORDERKEY_CPTICKS
772 ORDERKEY_STATE
773 ORDERKEY_PRIO
776 return(result);
779 /* compare_res - the comparison function for sorting by resident set size */
782 compare_res(struct proc **pp1, struct proc **pp2)
784 register struct kinfo_proc *p1;
785 register struct kinfo_proc *p2;
786 register int result;
787 register pctcpu lresult;
789 /* remove one level of indirection */
790 p1 = *(struct kinfo_proc **) pp1;
791 p2 = *(struct kinfo_proc **) pp2;
793 ORDERKEY_RSSIZE
794 ORDERKEY_MEM
795 ORDERKEY_PCTCPU
796 ORDERKEY_CPTICKS
797 ORDERKEY_STATE
798 ORDERKEY_PRIO
801 return(result);
804 /* compare_time - the comparison function for sorting by total cpu time */
807 compare_time(struct proc **pp1, struct proc **pp2)
809 register struct kinfo_proc *p1;
810 register struct kinfo_proc *p2;
811 register int result;
812 register pctcpu lresult;
814 /* remove one level of indirection */
815 p1 = *(struct kinfo_proc **) pp1;
816 p2 = *(struct kinfo_proc **) pp2;
818 ORDERKEY_CPTICKS
819 ORDERKEY_PCTCPU
820 ORDERKEY_KTHREADS
821 ORDERKEY_KTHREADS_PRIO
822 ORDERKEY_STATE
823 ORDERKEY_PRIO
824 ORDERKEY_RSSIZE
825 ORDERKEY_MEM
828 return(result);
831 /* compare_prio - the comparison function for sorting by cpu percentage */
834 compare_prio(struct proc **pp1, struct proc **pp2)
836 register struct kinfo_proc *p1;
837 register struct kinfo_proc *p2;
838 register int result;
839 register pctcpu lresult;
841 /* remove one level of indirection */
842 p1 = *(struct kinfo_proc **) pp1;
843 p2 = *(struct kinfo_proc **) pp2;
845 ORDERKEY_KTHREADS
846 ORDERKEY_KTHREADS_PRIO
847 ORDERKEY_PRIO
848 ORDERKEY_CPTICKS
849 ORDERKEY_PCTCPU
850 ORDERKEY_STATE
851 ORDERKEY_RSSIZE
852 ORDERKEY_MEM
855 return(result);
859 compare_thr(struct proc **pp1, struct proc **pp2)
861 register struct kinfo_proc *p1;
862 register struct kinfo_proc *p2;
863 register int result;
864 register pctcpu lresult;
866 /* remove one level of indirection */
867 p1 = *(struct kinfo_proc **) pp1;
868 p2 = *(struct kinfo_proc **) pp2;
870 ORDERKEY_KTHREADS
871 ORDERKEY_KTHREADS_PRIO
872 ORDERKEY_CPTICKS
873 ORDERKEY_PCTCPU
874 ORDERKEY_STATE
875 ORDERKEY_RSSIZE
876 ORDERKEY_MEM
879 return(result);
883 #endif
886 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
887 * the process does not exist.
888 * It is EXTREMLY IMPORTANT that this function work correctly.
889 * If top runs setuid root (as in SVR4), then this function
890 * is the only thing that stands in the way of a serious
891 * security problem. It validates requests for the "kill"
892 * and "renice" commands.
895 int proc_owner(int pid)
897 register int cnt;
898 register struct kinfo_proc **prefp;
899 register struct kinfo_proc *pp;
901 prefp = pref;
902 cnt = pref_len;
903 while (--cnt >= 0)
905 pp = *prefp++;
906 if (PP(pp, p_pid) == (pid_t)pid)
908 return((int)EP(pp, e_ucred.cr_ruid));
911 return(-1);
916 * swapmode is based on a program called swapinfo written
917 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
920 swapmode(int *retavail, int *retfree)
922 int n;
923 int pagesize = getpagesize();
924 struct kvm_swap swapary[1];
926 *retavail = 0;
927 *retfree = 0;
929 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
931 n = kvm_getswapinfo(kd, swapary, 1, 0);
932 if (n < 0 || swapary[0].ksw_total == 0)
933 return(0);
935 *retavail = CONVERT(swapary[0].ksw_total);
936 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
938 n = (int)((double)swapary[0].ksw_used * 100.0 /
939 (double)swapary[0].ksw_total);
940 return(n);