top - style cleanup and removal of unnecessary code
[dragonfly.git] / usr.bin / top / m_dragonfly.c
blob4629a98439ca4f365073abae63539a9a68c5963e
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/time.h>
27 #include <sys/types.h>
28 #include <sys/signal.h>
29 #include <sys/param.h>
31 #include "os.h"
32 #include <err.h>
33 #include <kvm.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <math.h>
37 #include <pwd.h>
38 #include <sys/errno.h>
39 #include <sys/sysctl.h>
40 #include <sys/file.h>
41 #include <sys/time.h>
42 #include <sys/user.h>
43 #include <sys/vmmeter.h>
44 #include <sys/resource.h>
45 #include <sys/rtprio.h>
47 /* Swap */
48 #include <stdlib.h>
49 #include <stdio.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 "display.h"
58 #include "machine.h"
59 #include "screen.h"
60 #include "utils.h"
62 int swapmode(int *retavail, int *retfree);
63 static int smpmode;
64 static int namelength;
65 static int cmdlength;
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 STATE C TIME WCPU CPU COMMAND";
102 #define smp_Proc_format \
103 "%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
105 static char up_header[] =
106 " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
108 #define up_Proc_format \
109 "%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s"
113 /* process state names for the "STATE" column of the display */
115 * the extra nulls in the string "run" are for adding a slash and the
116 * processor number when needed
119 const char *state_abbrev[] = {
120 "", "RUN\0\0\0", "STOP", "SLEEP",
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 int ccpu;
133 /* these are for calculating cpu state percentages */
135 static struct kinfo_cputime *cp_time, *cp_old;
137 /* these are for detailing the process states */
139 int process_states[6];
140 char *procstatenames[] = {
141 "", " starting, ", " running, ", " sleeping, ", " stopped, ",
142 " 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", NULL
190 /* compare routines */
191 int proc_compare(), compare_size(), compare_res(), compare_time(), compare_prio(), compare_thr();
193 int (*proc_compares[]) () = {
194 proc_compare,
195 compare_size,
196 compare_res,
197 compare_time,
198 compare_prio,
199 NULL
202 static void
203 cputime_percentages(int out[CPU_STATES], struct kinfo_cputime *new,
204 struct kinfo_cputime *old)
206 struct kinfo_cputime diffs;
207 uint64_t total_change, half_total;
209 /* initialization */
210 total_change = 0;
212 diffs.cp_user = new->cp_user - old->cp_user;
213 diffs.cp_nice = new->cp_nice - old->cp_nice;
214 diffs.cp_sys = new->cp_sys - old->cp_sys;
215 diffs.cp_intr = new->cp_intr - old->cp_intr;
216 diffs.cp_idle = new->cp_idle - old->cp_idle;
217 total_change = diffs.cp_user + diffs.cp_nice + diffs.cp_sys +
218 diffs.cp_intr + diffs.cp_idle;
219 old->cp_user = new->cp_user;
220 old->cp_nice = new->cp_nice;
221 old->cp_sys = new->cp_sys;
222 old->cp_intr = new->cp_intr;
223 old->cp_idle = new->cp_idle;
225 /* avoid divide by zero potential */
226 if (total_change == 0)
227 total_change = 1;
229 /* calculate percentages based on overall change, rounding up */
230 half_total = total_change >> 1;
232 out[0] = ((diffs.cp_user * 1000LL + half_total) / total_change);
233 out[1] = ((diffs.cp_nice * 1000LL + half_total) / total_change);
234 out[2] = ((diffs.cp_sys * 1000LL + half_total) / total_change);
235 out[3] = ((diffs.cp_intr * 1000LL + half_total) / total_change);
236 out[4] = ((diffs.cp_idle * 1000LL + half_total) / total_change);
240 machine_init(struct statics *statics)
242 int pagesize;
243 size_t modelen;
244 struct passwd *pw;
245 struct timeval boottime;
247 if (n_cpus < 1) {
248 if (kinfo_get_cpus(&n_cpus))
249 err(1, "kinfo_get_cpus failed");
251 /* get boot time */
252 modelen = sizeof(boottime);
253 if (sysctlbyname("kern.boottime", &boottime, &modelen, NULL, 0) == -1) {
254 /* we have no boottime to report */
255 boottime.tv_sec = -1;
257 modelen = sizeof(smpmode);
258 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
259 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
260 modelen != sizeof(smpmode))
261 smpmode = 0;
263 while ((pw = getpwent()) != NULL) {
264 if ((int)strlen(pw->pw_name) > namelength)
265 namelength = strlen(pw->pw_name);
267 if (namelength < 8)
268 namelength = 8;
269 if (smpmode && namelength > 13)
270 namelength = 13;
271 else if (namelength > 15)
272 namelength = 15;
274 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
275 return -1;
277 if (kinfo_get_sched_ccpu(&ccpu)) {
278 fprintf(stderr, "top: kinfo_get_sched_ccpu failed\n");
279 return (-1);
281 /* this is used in calculating WCPU -- calculate it ahead of time */
282 logcpu = log(loaddouble(ccpu));
284 pbase = NULL;
285 pref = NULL;
286 nproc = 0;
287 onproc = -1;
289 * get the page size with "getpagesize" and calculate pageshift from
290 * it
292 pagesize = getpagesize();
293 pageshift = 0;
294 while (pagesize > 1) {
295 pageshift++;
296 pagesize >>= 1;
299 /* we only need the amount of log(2)1024 for our conversion */
300 pageshift -= LOG1024;
302 /* fill in the statics information */
303 statics->procstate_names = procstatenames;
304 statics->cpustate_names = cpustatenames;
305 statics->memory_names = memorynames;
306 statics->boottime = boottime.tv_sec;
307 statics->swap_names = swapnames;
308 statics->order_names = ordernames;
310 /* all done! */
311 return (0);
314 char *
315 format_header(char *uname_field)
317 static char Header[128];
319 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
320 namelength, namelength, uname_field);
322 if (screen_width <= 79)
323 cmdlength = 80;
324 else
325 cmdlength = 89;
327 cmdlength = cmdlength - strlen(Header) + 6;
329 return Header;
332 static int swappgsin = -1;
333 static int swappgsout = -1;
334 extern struct timeval timeout;
336 void
337 get_system_info(struct system_info *si)
339 size_t len;
340 int cpu;
342 if (cpu_states == NULL) {
343 cpu_states = malloc(sizeof(*cpu_states) * CPU_STATES * n_cpus);
344 if (cpu_states == NULL)
345 err(1, "malloc");
346 bzero(cpu_states, sizeof(*cpu_states) * CPU_STATES * n_cpus);
348 if (cp_time == NULL) {
349 cp_time = malloc(2 * n_cpus * sizeof(cp_time[0]));
350 if (cp_time == NULL)
351 err(1, "cp_time");
352 cp_old = cp_time + n_cpus;
354 len = n_cpus * sizeof(cp_old[0]);
355 bzero(cp_time, len);
356 if (sysctlbyname("kern.cputime", cp_old, &len, NULL, 0))
357 err(1, "kern.cputime");
359 len = n_cpus * sizeof(cp_time[0]);
360 bzero(cp_time, len);
361 if (sysctlbyname("kern.cputime", cp_time, &len, NULL, 0))
362 err(1, "kern.cputime");
364 getloadavg(si->load_avg, 3);
366 lastpid = 0;
368 /* convert cp_time counts to percentages */
369 for (cpu = 0; cpu < n_cpus; ++cpu) {
370 cputime_percentages(cpu_states + cpu * CPU_STATES,
371 &cp_time[cpu], &cp_old[cpu]);
374 /* sum memory & swap statistics */
376 struct vmmeter vmm;
377 struct vmstats vms;
378 size_t vms_size = sizeof(vms);
379 size_t vmm_size = sizeof(vmm);
380 static unsigned int swap_delay = 0;
381 static int swapavail = 0;
382 static int swapfree = 0;
383 static int bufspace = 0;
385 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0))
386 err(1, "sysctlbyname: vm.vmstats");
388 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0))
389 err(1, "sysctlbyname: vm.vmmeter");
391 if (kinfo_get_vfs_bufspace(&bufspace))
392 err(1, "kinfo_get_vfs_bufspace");
394 /* convert memory stats to Kbytes */
395 memory_stats[0] = pagetok(vms.v_active_count);
396 memory_stats[1] = pagetok(vms.v_inactive_count);
397 memory_stats[2] = pagetok(vms.v_wire_count);
398 memory_stats[3] = pagetok(vms.v_cache_count);
399 memory_stats[4] = bufspace / 1024;
400 memory_stats[5] = pagetok(vms.v_free_count);
401 memory_stats[6] = -1;
403 /* first interval */
404 if (swappgsin < 0) {
405 swap_stats[4] = 0;
406 swap_stats[5] = 0;
408 /* compute differences between old and new swap statistic */
409 else {
410 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin)));
411 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout)));
414 swappgsin = vmm.v_swappgsin;
415 swappgsout = vmm.v_swappgsout;
417 /* call CPU heavy swapmode() only for changes */
418 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
419 swap_stats[3] = swapmode(&swapavail, &swapfree);
420 swap_stats[0] = swapavail;
421 swap_stats[1] = swapavail - swapfree;
422 swap_stats[2] = swapfree;
424 swap_delay = 1;
425 swap_stats[6] = -1;
428 /* set arrays and strings */
429 si->cpustates = cpu_states;
430 si->memory = memory_stats;
431 si->swap = swap_stats;
434 if (lastpid > 0) {
435 si->last_pid = lastpid;
436 } else {
437 si->last_pid = -1;
442 static struct handle handle;
444 caddr_t
445 get_process_info(struct system_info *si, struct process_select *sel,
446 int compare_index)
448 int i;
449 int total_procs;
450 int active_procs;
451 struct kinfo_proc **prefp;
452 struct kinfo_proc *pp;
454 /* these are copied out of sel for speed */
455 int show_idle;
456 int show_system;
457 int show_uid;
460 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
461 if (nproc > onproc)
462 pref = (struct kinfo_proc **)realloc(pref, sizeof(struct kinfo_proc *)
463 * (onproc = nproc));
464 if (pref == NULL || pbase == NULL) {
465 (void)fprintf(stderr, "top: Out of memory.\n");
466 quit(23);
468 /* get a pointer to the states summary array */
469 si->procstates = process_states;
471 /* set up flags which define what we are going to select */
472 show_idle = sel->idle;
473 show_system = sel->system;
474 show_uid = sel->uid != -1;
476 /* count up process states and get pointers to interesting procs */
477 total_procs = 0;
478 active_procs = 0;
479 memset((char *)process_states, 0, sizeof(process_states));
480 prefp = pref;
481 for (pp = pbase, i = 0; i < nproc; pp++, i++) {
483 * Place pointers to each valid proc structure in pref[].
484 * Process slots that are actually in use have a non-zero
485 * status field. Processes with P_SYSTEM set are system
486 * processes---these get ignored unless show_sysprocs is set.
488 if ((show_threads && (LP(pp, pid) == -1)) ||
489 (show_system || ((PP(pp, flags) & P_SYSTEM) == 0))) {
490 total_procs++;
491 process_states[(unsigned char)PP(pp, stat)]++;
492 if ((show_threads && (LP(pp, pid) == -1)) ||
493 (show_idle || (LP(pp, pctcpu) != 0) ||
494 (LP(pp, stat) == LSRUN)) &&
495 (!show_uid || PP(pp, ruid) == (uid_t) sel->uid)) {
496 *prefp++ = pp;
497 active_procs++;
502 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *),
503 proc_compares[compare_index]);
505 /* remember active and total counts */
506 si->p_total = total_procs;
507 si->p_active = pref_len = active_procs;
509 /* pass back a handle */
510 handle.next_proc = pref;
511 handle.remaining = active_procs;
512 return ((caddr_t) & handle);
515 char fmt[128]; /* static area where result is built */
517 char *
518 format_next_process(caddr_t xhandle, char *(*get_userid) (int))
520 struct kinfo_proc *pp;
521 long cputime;
522 double pct;
523 struct handle *hp;
524 char status[16];
525 char const *wrapper;
526 int state;
527 int xnice;
529 /* find and remember the next proc structure */
530 hp = (struct handle *)xhandle;
531 pp = *(hp->next_proc++);
532 hp->remaining--;
534 /* set the wrapper for the process/thread name */
535 if ((PP(pp, flags) & P_SWAPPEDOUT))
536 wrapper = "[]"; /* swapped process [pname] */
537 else if (((PP(pp, flags) & P_SYSTEM) != 0) && (LP(pp, pid) > 0))
538 wrapper = "()"; /* system process (pname) */
539 else if (show_threads && (LP(pp, pid) == -1))
540 wrapper = "<>"; /* pure kernel threads <thread> */
541 else
542 wrapper = NULL;
544 /* get the process's command name */
545 if (wrapper != NULL) {
546 char *comm = PP(pp, comm);
547 #define COMSIZ sizeof(PP(pp, comm))
548 char buf[COMSIZ];
549 (void)strncpy(buf, comm, COMSIZ);
550 comm[0] = wrapper[0];
551 (void)strncpy(&comm[1], buf, COMSIZ - 2);
552 comm[COMSIZ - 2] = '\0';
553 (void)strncat(comm, &wrapper[1], COMSIZ - 1);
554 comm[COMSIZ - 1] = '\0';
557 * Convert the process's runtime from microseconds to seconds. This
558 * time includes the interrupt time although that is not wanted here.
559 * ps(1) is similarly sloppy.
561 cputime = (LP(pp, uticks) + LP(pp, sticks)) / 1000000;
563 /* calculate the base for cpu percentages */
564 pct = pctdouble(LP(pp, pctcpu));
566 /* generate "STATE" field */
567 switch (state = LP(pp, stat)) {
568 case LSRUN:
569 if (smpmode && LP(pp, tdflags) & TDF_RUNNING)
570 sprintf(status, "CPU%d", LP(pp, cpuid));
571 else
572 strcpy(status, "RUN");
573 break;
574 case LSSLEEP:
575 if (LP(pp, wmesg) != NULL) {
576 sprintf(status, "%.6s", LP(pp, wmesg));
577 break;
579 /* fall through */
580 default:
582 if (state >= 0 &&
583 (unsigned)state < sizeof(state_abbrev) / sizeof(*state_abbrev))
584 sprintf(status, "%.6s", state_abbrev[(unsigned char)state]);
585 else
586 sprintf(status, "?%5d", state);
587 break;
590 if (PP(pp, stat) == SZOMB)
591 strcpy(status, "ZOMB");
594 * idle time 0 - 31 -> nice value +21 - +52 normal time -> nice
595 * value -20 - +20 real time 0 - 31 -> nice value -52 - -21 thread
596 * 0 - 31 -> nice value -53 -
598 switch (LP(pp, rtprio.type)) {
599 case RTP_PRIO_REALTIME:
600 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio);
601 break;
602 case RTP_PRIO_IDLE:
603 xnice = PRIO_MAX + 1 + LP(pp, rtprio.prio);
604 break;
605 case RTP_PRIO_THREAD:
606 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio);
607 break;
608 default:
609 xnice = PP(pp, nice);
610 break;
613 /* format this entry */
614 snprintf(fmt, sizeof(fmt),
615 smpmode ? smp_Proc_format : up_Proc_format,
616 (int)PP(pp, pid),
617 namelength, namelength,
618 get_userid(PP(pp, ruid)),
619 (int)((show_threads && (LP(pp, pid) == -1)) ?
620 LP(pp, tdprio) : LP(pp, prio)),
621 (int)xnice,
622 format_k(PROCSIZE(pp)),
623 format_k(pagetok(VP(pp, rssize))),
624 status,
625 (int)(smpmode ? LP(pp, cpuid) : 0),
626 format_time(cputime),
627 100.0 * weighted_cpu(pct, pp),
628 100.0 * pct,
629 cmdlength,
630 printable(PP(pp, comm)));
632 /* return the result */
633 return (fmt);
636 /* comparison routines for qsort */
639 * proc_compare - comparison function for "qsort"
640 * Compares the resource consumption of two processes using five
641 * distinct keys. The keys (in descending order of importance) are:
642 * percent cpu, cpu ticks, state, resident set size, total virtual
643 * memory usage. The process states are ordered as follows (from least
644 * to most important): WAIT, zombie, sleep, stop, start, run. The
645 * array declaration below maps a process state index into a number
646 * that reflects this ordering.
649 static unsigned char sorted_state[] =
651 0, /* not used */
652 3, /* sleep */
653 1, /* ABANDONED (WAIT) */
654 6, /* run */
655 5, /* start */
656 2, /* zombie */
657 4 /* stop */
661 #define ORDERKEY_PCTCPU \
662 if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \
663 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
665 #define CPTICKS(p) (LP(p, uticks) + LP(p, sticks))
667 #define ORDERKEY_CPTICKS \
668 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
669 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
671 #define ORDERKEY_STATE \
672 if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \
673 sorted_state[(unsigned char) PP(p1, stat)]) == 0)
675 #define ORDERKEY_PRIO \
676 if ((result = LP(p2, prio) - LP(p1, prio)) == 0)
678 #define ORDERKEY_KTHREADS \
679 if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0)
681 #define ORDERKEY_KTHREADS_PRIO \
682 if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0)
684 #define ORDERKEY_RSSIZE \
685 if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0)
687 #define ORDERKEY_MEM \
688 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
690 /* compare_cpu - the comparison function for sorting by cpu percentage */
693 proc_compare(const void *arg1, const void *arg2)
695 const struct proc *const *pp1 = arg1;
696 const struct proc *const *pp2 = arg2;
697 const struct kinfo_proc *p1;
698 const struct kinfo_proc *p2;
699 int result;
700 pctcpu lresult;
702 /* remove one level of indirection */
703 p1 = *(const struct kinfo_proc *const *)pp1;
704 p2 = *(const struct kinfo_proc *const *)pp2;
706 ORDERKEY_PCTCPU
707 ORDERKEY_CPTICKS
708 ORDERKEY_STATE
709 ORDERKEY_PRIO
710 ORDERKEY_RSSIZE
711 ORDERKEY_MEM
714 return (result);
717 /* compare_size - the comparison function for sorting by total memory usage */
720 compare_size(const void *arg1, const void *arg2)
722 struct proc *const *pp1 = arg1;
723 struct proc *const *pp2 = arg2;
724 struct kinfo_proc *p1;
725 struct kinfo_proc *p2;
726 int result;
727 pctcpu lresult;
729 /* remove one level of indirection */
730 p1 = *(struct kinfo_proc *const *)pp1;
731 p2 = *(struct kinfo_proc *const *)pp2;
733 ORDERKEY_MEM
734 ORDERKEY_RSSIZE
735 ORDERKEY_PCTCPU
736 ORDERKEY_CPTICKS
737 ORDERKEY_STATE
738 ORDERKEY_PRIO
741 return (result);
744 /* compare_res - the comparison function for sorting by resident set size */
747 compare_res(const void *arg1, const void *arg2)
749 struct proc *const *pp1 = arg1;
750 struct proc *const *pp2 = arg2;
751 struct kinfo_proc *p1;
752 struct kinfo_proc *p2;
753 int result;
754 pctcpu lresult;
756 /* remove one level of indirection */
757 p1 = *(struct kinfo_proc *const *)pp1;
758 p2 = *(struct kinfo_proc *const *)pp2;
760 ORDERKEY_RSSIZE
761 ORDERKEY_MEM
762 ORDERKEY_PCTCPU
763 ORDERKEY_CPTICKS
764 ORDERKEY_STATE
765 ORDERKEY_PRIO
768 return (result);
771 /* compare_time - the comparison function for sorting by total cpu time */
774 compare_time(const void *arg1, const void *arg2)
776 struct proc *const *pp1 = arg1;
777 struct proc *const *pp2 = arg2;
778 const struct kinfo_proc *p1;
779 const struct kinfo_proc *p2;
780 int result;
781 pctcpu lresult;
783 /* remove one level of indirection */
784 p1 = *(struct kinfo_proc *const *)pp1;
785 p2 = *(struct kinfo_proc *const *)pp2;
787 ORDERKEY_CPTICKS
788 ORDERKEY_PCTCPU
789 ORDERKEY_KTHREADS
790 ORDERKEY_KTHREADS_PRIO
791 ORDERKEY_STATE
792 ORDERKEY_PRIO
793 ORDERKEY_RSSIZE
794 ORDERKEY_MEM
797 return (result);
800 /* compare_prio - the comparison function for sorting by cpu percentage */
803 compare_prio(const void *arg1, const void *arg2)
805 struct proc *const *pp1 = arg1;
806 struct proc *const *pp2 = arg2;
807 const struct kinfo_proc *p1;
808 const struct kinfo_proc *p2;
809 int result;
810 pctcpu lresult;
812 /* remove one level of indirection */
813 p1 = *(struct kinfo_proc *const *)pp1;
814 p2 = *(struct kinfo_proc *const *)pp2;
816 ORDERKEY_KTHREADS
817 ORDERKEY_KTHREADS_PRIO
818 ORDERKEY_PRIO
819 ORDERKEY_CPTICKS
820 ORDERKEY_PCTCPU
821 ORDERKEY_STATE
822 ORDERKEY_RSSIZE
823 ORDERKEY_MEM
826 return (result);
830 compare_thr(const void *arg1, const void *arg2)
832 struct proc *const *pp1 = arg1;
833 struct proc *const *pp2 = arg2;
834 const struct kinfo_proc *p1;
835 const struct kinfo_proc *p2;
836 int result;
837 pctcpu lresult;
839 /* remove one level of indirection */
840 p1 = *(struct kinfo_proc *const *)pp1;
841 p2 = *(struct kinfo_proc *const *)pp2;
843 ORDERKEY_KTHREADS
844 ORDERKEY_KTHREADS_PRIO
845 ORDERKEY_CPTICKS
846 ORDERKEY_PCTCPU
847 ORDERKEY_STATE
848 ORDERKEY_RSSIZE
849 ORDERKEY_MEM
852 return (result);
856 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
857 * the process does not exist.
858 * It is EXTREMLY IMPORTANT that this function work correctly.
859 * If top runs setuid root (as in SVR4), then this function
860 * is the only thing that stands in the way of a serious
861 * security problem. It validates requests for the "kill"
862 * and "renice" commands.
866 proc_owner(int pid)
868 int xcnt;
869 struct kinfo_proc **prefp;
870 struct kinfo_proc *pp;
872 prefp = pref;
873 xcnt = pref_len;
874 while (--xcnt >= 0) {
875 pp = *prefp++;
876 if (PP(pp, pid) == (pid_t) pid) {
877 return ((int)PP(pp, ruid));
880 return (-1);
885 * swapmode is based on a program called swapinfo written
886 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
889 swapmode(int *retavail, int *retfree)
891 int n;
892 int pagesize = getpagesize();
893 struct kvm_swap swapary[1];
895 *retavail = 0;
896 *retfree = 0;
898 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
900 n = kvm_getswapinfo(kd, swapary, 1, 0);
901 if (n < 0 || swapary[0].ksw_total == 0)
902 return (0);
904 *retavail = CONVERT(swapary[0].ksw_total);
905 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
907 n = (int)((double)swapary[0].ksw_used * 100.0 /
908 (double)swapary[0].ksw_total);
909 return (n);