2 * top - a top users display for Unix
4 * SYNOPSIS: For DragonFly 2.x and later
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
14 * DragonFly 2.x and above
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 $
26 #include <sys/types.h>
28 #include <sys/signal.h>
29 #include <sys/param.h>
38 #include <sys/errno.h>
39 #include <sys/sysctl.h>
41 #include <sys/vmmeter.h>
42 #include <sys/resource.h>
43 #include <sys/rtprio.h>
50 #include <osreldate.h> /* for changes in kernel structures */
52 #include <sys/kinfo.h>
60 int swapmode(int *retavail
, int *retfree
);
61 static int namelength
;
63 static int show_fullcmd
;
67 /* get_process_info passes back a handle. This is what it looks like: */
70 struct kinfo_proc
**next_proc
; /* points to next valid proc pointer */
71 int remaining
; /* number of pointers remaining */
75 /* declarations for load_avg */
78 #define PP(pp, field) ((pp)->kp_ ## field)
79 #define LP(pp, field) ((pp)->kp_lwp.kl_ ## field)
80 #define VP(pp, field) ((pp)->kp_vm_ ## field)
82 /* what we consider to be process size: */
83 #define PROCSIZE(pp) (VP((pp), map_size) / 1024)
86 * These definitions control the format of the per-process area
89 static char smp_header
[] =
90 " PID %-*.*s NICE SIZE RES STATE C TIME CTIME CPU COMMAND";
92 #define smp_Proc_format \
93 "%6d %-*.*s %3d%7s %6s %8.8s %3d %6s %7s %5.2f%% %.*s"
95 /* process state names for the "STATE" column of the display */
97 * the extra nulls in the string "run" are for adding a slash and the
98 * processor number when needed
101 const char *state_abbrev
[] = {
102 "", "RUN\0\0\0", "STOP", "SLEEP",
108 /* values that we stash away in _init and use in later routines */
112 /* these are for calculating cpu state percentages */
114 static struct kinfo_cputime
*cp_time
, *cp_old
;
116 /* these are for detailing the process states */
120 int process_states
[MAXPSTATES
];
122 char *procstatenames
[] = {
123 " running, ", " idle, ", " active, ", " stopped, ", " zombie, ",
127 /* these are for detailing the cpu states */
131 char *cpustatenames
[CPU_STATES
+ 1] = {
132 "user", "nice", "system", "interrupt", "idle", NULL
135 /* these are for detailing the memory statistics */
137 long memory_stats
[7];
138 char *memorynames
[] = {
139 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
144 char *swapnames
[] = {
146 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
151 /* these are for keeping track of the proc array */
154 static int onproc
= -1;
156 static struct kinfo_proc
*pbase
;
157 static struct kinfo_proc
**pref
;
159 static uint64_t prev_pbase_time
; /* unit: us */
160 static struct kinfo_proc
*prev_pbase
;
161 static int prev_pbase_alloc
;
162 static int prev_nproc
;
165 /* these are for getting the memory statistics */
167 static int pageshift
; /* log base 2 of the pagesize */
169 /* define pagetok in terms of pageshift */
171 #define pagetok(size) ((size) << pageshift)
173 /* sorting orders. first is default */
174 char *ordernames
[] = {
175 "cpu", "size", "res", "time", "pri", "thr", "pid", "ctime", "pres", NULL
178 /* compare routines */
179 int proc_compare (struct kinfo_proc
**, struct kinfo_proc
**);
180 int compare_size (struct kinfo_proc
**, struct kinfo_proc
**);
181 int compare_res (struct kinfo_proc
**, struct kinfo_proc
**);
182 int compare_time (struct kinfo_proc
**, struct kinfo_proc
**);
183 int compare_ctime (struct kinfo_proc
**, struct kinfo_proc
**);
184 int compare_prio(struct kinfo_proc
**, struct kinfo_proc
**);
185 int compare_thr (struct kinfo_proc
**, struct kinfo_proc
**);
186 int compare_pid (struct kinfo_proc
**, struct kinfo_proc
**);
187 int compare_pres(struct kinfo_proc
**, struct kinfo_proc
**);
189 int (*proc_compares
[]) (struct kinfo_proc
**,struct kinfo_proc
**) = {
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
;
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)
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
)
243 size_t modelen
, prmlen
;
245 struct timeval boottime
;
248 if (kinfo_get_cpus(&n_cpus
))
249 err(1, "kinfo_get_cpus failed");
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;
258 prmlen
= sizeof(fscale
);
259 if (sysctlbyname("kern.fscale", &fscale
, &prmlen
, NULL
, 0) == -1)
260 err(1, "sysctl kern.fscale failed");
262 while ((pw
= getpwent()) != NULL
) {
263 if ((int)strlen(pw
->pw_name
) > namelength
)
264 namelength
= strlen(pw
->pw_name
);
271 if ((kd
= kvm_open(NULL
, NULL
, NULL
, O_RDONLY
, NULL
)) == NULL
)
279 prev_pbase_alloc
= 0;
283 * get the page size with "getpagesize" and calculate pageshift from
286 pagesize
= getpagesize();
288 while (pagesize
> 1) {
293 /* we only need the amount of log(2)1024 for our conversion */
294 pageshift
-= LOG1024
;
296 /* fill in the statics information */
297 statics
->procstate_names
= procstatenames
;
298 statics
->cpustate_names
= cpustatenames
;
299 statics
->memory_names
= memorynames
;
300 statics
->boottime
= boottime
.tv_sec
;
301 statics
->swap_names
= swapnames
;
302 statics
->order_names
= ordernames
;
303 /* we need kvm descriptor in order to show full commands */
304 statics
->flags
.fullcmds
= kd
!= NULL
;
305 statics
->flags
.threads
= 1;
312 format_header(char *uname_field
)
314 static char Header
[128];
316 snprintf(Header
, sizeof(Header
), smp_header
,
317 namelength
, namelength
, uname_field
);
319 if (screen_width
<= 79)
322 cmdlength
= screen_width
;
324 cmdlength
= cmdlength
- strlen(Header
) + 6;
329 static int swappgsin
= -1;
330 static int swappgsout
= -1;
331 extern struct timeval timeout
;
334 get_system_info(struct system_info
*si
)
339 if (cpu_states
== NULL
) {
340 cpu_states
= malloc(sizeof(*cpu_states
) * CPU_STATES
* n_cpus
);
341 if (cpu_states
== NULL
)
343 bzero(cpu_states
, sizeof(*cpu_states
) * CPU_STATES
* n_cpus
);
345 if (cp_time
== NULL
) {
346 cp_time
= malloc(2 * n_cpus
* sizeof(cp_time
[0]));
349 cp_old
= cp_time
+ n_cpus
;
350 len
= n_cpus
* sizeof(cp_old
[0]);
352 if (sysctlbyname("kern.cputime", cp_old
, &len
, NULL
, 0))
353 err(1, "kern.cputime");
355 len
= n_cpus
* sizeof(cp_time
[0]);
357 if (sysctlbyname("kern.cputime", cp_time
, &len
, NULL
, 0))
358 err(1, "kern.cputime");
360 getloadavg(si
->load_avg
, 3);
364 /* convert cp_time counts to percentages */
365 int combine_cpus
= (enable_ncpus
== 0 && n_cpus
> 1);
366 for (cpu
= 0; cpu
< n_cpus
; ++cpu
) {
367 cputime_percentages(cpu_states
+ cpu
* CPU_STATES
,
368 &cp_time
[cpu
], &cp_old
[cpu
]);
371 if (cpu_averages
== NULL
) {
372 cpu_averages
= malloc(sizeof(*cpu_averages
) * CPU_STATES
);
373 if (cpu_averages
== NULL
)
374 err(1, "cpu_averages");
376 bzero(cpu_averages
, sizeof(*cpu_averages
) * CPU_STATES
);
377 for (cpu
= 0; cpu
< n_cpus
; ++cpu
) {
379 cpu_averages
[0] += *(cpu_states
+ ((cpu
* CPU_STATES
) + j
++) );
380 cpu_averages
[1] += *(cpu_states
+ ((cpu
* CPU_STATES
) + j
++) );
381 cpu_averages
[2] += *(cpu_states
+ ((cpu
* CPU_STATES
) + j
++) );
382 cpu_averages
[3] += *(cpu_states
+ ((cpu
* CPU_STATES
) + j
++) );
383 cpu_averages
[4] += *(cpu_states
+ ((cpu
* CPU_STATES
) + j
++) );
385 for (int i
= 0; i
< CPU_STATES
; ++i
)
386 cpu_averages
[i
] /= n_cpus
;
389 /* sum memory & swap statistics */
393 size_t vms_size
= sizeof(vms
);
394 size_t vmm_size
= sizeof(vmm
);
395 static unsigned int swap_delay
= 0;
396 static int swapavail
= 0;
397 static int swapfree
= 0;
398 static long bufspace
= 0;
400 if (sysctlbyname("vm.vmstats", &vms
, &vms_size
, NULL
, 0))
401 err(1, "sysctlbyname: vm.vmstats");
403 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0))
404 err(1, "sysctlbyname: vm.vmmeter");
406 if (kinfo_get_vfs_bufspace(&bufspace
))
407 err(1, "kinfo_get_vfs_bufspace");
409 /* convert memory stats to Kbytes */
410 memory_stats
[0] = pagetok(vms
.v_active_count
);
411 memory_stats
[1] = pagetok(vms
.v_inactive_count
);
412 memory_stats
[2] = pagetok(vms
.v_wire_count
);
413 memory_stats
[3] = pagetok(vms
.v_cache_count
);
414 memory_stats
[4] = bufspace
/ 1024;
415 memory_stats
[5] = pagetok(vms
.v_free_count
);
416 memory_stats
[6] = -1;
423 /* compute differences between old and new swap statistic */
425 swap_stats
[4] = pagetok(((vmm
.v_swappgsin
- swappgsin
)));
426 swap_stats
[5] = pagetok(((vmm
.v_swappgsout
- swappgsout
)));
429 swappgsin
= vmm
.v_swappgsin
;
430 swappgsout
= vmm
.v_swappgsout
;
432 /* call CPU heavy swapmode() only for changes */
433 if (swap_stats
[4] > 0 || swap_stats
[5] > 0 || swap_delay
== 0) {
434 swap_stats
[3] = swapmode(&swapavail
, &swapfree
);
435 swap_stats
[0] = swapavail
;
436 swap_stats
[1] = swapavail
- swapfree
;
437 swap_stats
[2] = swapfree
;
443 /* set arrays and strings */
444 si
->cpustates
= combine_cpus
== 1 ?
445 cpu_averages
: cpu_states
;
446 si
->memory
= memory_stats
;
447 si
->swap
= swap_stats
;
451 si
->last_pid
= lastpid
;
458 static struct handle handle
;
461 fixup_pctcpu(struct kinfo_proc
*fixit
, uint64_t d
)
463 struct kinfo_proc
*pp
;
467 if (prev_nproc
== 0 || d
== 0)
470 if (LP(fixit
, pid
) == -1) {
471 /* Skip kernel "idle" threads */
472 if (PP(fixit
, stat
) == SIDL
)
474 for (pp
= prev_pbase
, i
= 0; i
< prev_nproc
; pp
++, i
++) {
475 if (LP(pp
, pid
) == -1 &&
476 PP(pp
, ktaddr
) == PP(fixit
, ktaddr
))
480 for (pp
= prev_pbase
, i
= 0; i
< prev_nproc
; pp
++, i
++) {
481 if (LP(pp
, pid
) == LP(fixit
, pid
) &&
482 LP(pp
, tid
) == LP(fixit
, tid
)) {
483 if (PP(pp
, paddr
) != PP(fixit
, paddr
)) {
484 /* pid/tid are reused */
491 if (i
== prev_nproc
|| pp
== NULL
)
494 ticks
= LP(fixit
, iticks
) - LP(pp
, iticks
);
495 ticks
+= LP(fixit
, sticks
) - LP(pp
, sticks
);
496 ticks
+= LP(fixit
, uticks
) - LP(pp
, uticks
);
499 LP(fixit
, pctcpu
) = (ticks
* (uint64_t)fscale
) / d
;
503 get_process_info(struct system_info
*si
, struct process_select
*sel
,
512 struct kinfo_proc
**prefp
;
513 struct kinfo_proc
*pp
;
515 /* these are copied out of sel for speed */
522 show_threads
= sel
->threads
;
524 pbase
= kvm_getprocs(kd
,
525 KERN_PROC_ALL
| (show_threads
? KERN_PROC_FLAG_LWP
: 0), 0, &nproc
);
527 pref
= (struct kinfo_proc
**)realloc(pref
, sizeof(struct kinfo_proc
*)
529 if (pref
== NULL
|| pbase
== NULL
) {
530 (void)fprintf(stderr
, "top: Out of memory.\n");
534 clock_gettime(CLOCK_MONOTONIC_PRECISE
, &tv
);
535 t
= (tv
.tv_sec
* 1000000ULL) + (tv
.tv_nsec
/ 1000ULL);
536 if (prev_pbase_time
> 0 && t
> prev_pbase_time
)
537 d
= t
- prev_pbase_time
;
539 /* get a pointer to the states summary array */
540 si
->procstates
= process_states
;
542 /* set up flags which define what we are going to select */
543 show_idle
= sel
->idle
;
544 show_system
= sel
->system
;
545 show_uid
= sel
->uid
!= -1;
546 show_fullcmd
= sel
->fullcmd
;
547 match_command
= sel
->command
;
549 /* count up process states and get pointers to interesting procs */
552 memset((char *)process_states
, 0, sizeof(process_states
));
554 for (pp
= pbase
, i
= 0; i
< nproc
; pp
++, i
++) {
556 * Place pointers to each valid proc structure in pref[].
557 * Process slots that are actually in use have a non-zero
558 * status field. Processes with P_SYSTEM set are system
559 * processes---these get ignored unless show_sysprocs is set.
561 if ((show_system
&& (LP(pp
, pid
) == -1)) ||
562 (show_system
|| ((PP(pp
, flags
) & P_SYSTEM
) == 0))) {
563 int lpstate
= LP(pp
, stat
);
564 int pstate
= PP(pp
, stat
);
567 if (lpstate
== LSRUN
)
569 if (pstate
>= 0 && pstate
< MAXPSTATES
- 1)
570 process_states
[pstate
]++;
572 if (match_command
!= NULL
&&
573 strstr(PP(pp
, comm
), match_command
) == NULL
) {
574 /* Command does not match */
578 if (show_uid
&& PP(pp
, ruid
) != (uid_t
)sel
->uid
) {
579 /* UID does not match */
583 if (!show_system
&& LP(pp
, pid
) == -1) {
584 /* Don't show system processes */
588 /* Fix up pctcpu before show_idle test */
591 if (!show_idle
&& LP(pp
, pctcpu
) == 0 &&
593 /* Don't show idle processes */
603 * Save kinfo_procs for later pctcpu fixup.
605 if (prev_pbase_alloc
< nproc
) {
606 prev_pbase_alloc
= nproc
;
607 prev_pbase
= realloc(prev_pbase
,
608 prev_pbase_alloc
* sizeof(struct kinfo_proc
));
609 if (prev_pbase
== NULL
) {
610 fprintf(stderr
, "top: Out of memory.\n");
616 memcpy(prev_pbase
, pbase
, nproc
* sizeof(struct kinfo_proc
));
618 qsort((char *)pref
, active_procs
, sizeof(struct kinfo_proc
*),
619 (int (*)(const void *, const void *))proc_compares
[compare_index
]);
621 /* remember active and total counts */
622 si
->p_total
= total_procs
;
623 si
->p_active
= pref_len
= active_procs
;
625 /* pass back a handle */
626 handle
.next_proc
= pref
;
627 handle
.remaining
= active_procs
;
628 handle
.show_threads
= show_threads
;
629 return ((caddr_t
) & handle
);
632 char fmt
[MAX_COLS
]; /* static area where result is built */
635 format_next_process(caddr_t xhandle
, char *(*get_userid
) (int))
637 struct kinfo_proc
*pp
;
646 char cputime_fmt
[10], ccputime_fmt
[10];
648 /* find and remember the next proc structure */
649 hp
= (struct handle
*)xhandle
;
650 pp
= *(hp
->next_proc
++);
653 /* get the process's command name */
655 char **comm_full
= kvm_getargv(kd
, pp
, 0);
665 /* the actual field to display */
666 char cmdfield
[MAX_COLS
];
668 if (PP(pp
, flags
) & P_SYSTEM
) {
670 snprintf(cmdfield
, sizeof cmdfield
, "[%s]", comm
);
671 } else if (hp
->show_threads
&& PP(pp
, nthreads
) > 1) {
672 /* display it as a thread */
673 if (strcmp(PP(pp
, comm
), LP(pp
, comm
)) == 0) {
674 snprintf(cmdfield
, sizeof cmdfield
, "%s{%d}", comm
,
677 /* show thread name in addition to tid */
678 snprintf(cmdfield
, sizeof cmdfield
, "%s{%d/%s}", comm
,
679 LP(pp
, tid
), LP(pp
, comm
));
682 snprintf(cmdfield
, sizeof cmdfield
, "%s", comm
);
686 * Convert the process's runtime from microseconds to seconds. This
687 * time includes the interrupt time to be in compliance with ps output.
689 cputime
= (LP(pp
, uticks
) + LP(pp
, sticks
) + LP(pp
, iticks
)) / 1000000;
690 ccputime
= cputime
+ PP(pp
, cru
).ru_stime
.tv_sec
+ PP(pp
, cru
).ru_utime
.tv_sec
;
691 format_time(cputime
, cputime_fmt
, sizeof(cputime_fmt
));
692 format_time(ccputime
, ccputime_fmt
, sizeof(ccputime_fmt
));
694 /* calculate the base for cpu percentages */
695 pct
= pctdouble(LP(pp
, pctcpu
));
697 /* generate "STATE" field */
698 switch (state
= LP(pp
, stat
)) {
700 if (LP(pp
, tdflags
) & TDF_RUNNING
)
701 sprintf(status
, "CPU%d", LP(pp
, cpuid
));
703 strcpy(status
, "RUN");
706 if (LP(pp
, wmesg
) != NULL
) {
707 sprintf(status
, "%.8s", LP(pp
, wmesg
)); /* WMESGLEN */
713 if (state
>= 0 && (unsigned)state
< NELEM(state_abbrev
))
714 sprintf(status
, "%.6s", state_abbrev
[(unsigned char)state
]);
716 sprintf(status
, "?%5d", state
);
720 if (PP(pp
, stat
) == SZOMB
)
721 strcpy(status
, "ZOMB");
724 * idle time 0 - 31 -> nice value +21 - +52 normal time -> nice
725 * value -20 - +20 real time 0 - 31 -> nice value -52 - -21 thread
726 * 0 - 31 -> nice value -53 -
728 switch (LP(pp
, rtprio
.type
)) {
729 case RTP_PRIO_REALTIME
:
730 xnice
= PRIO_MIN
- 1 - RTP_PRIO_MAX
+ LP(pp
, rtprio
.prio
);
733 xnice
= PRIO_MAX
+ 1 + LP(pp
, rtprio
.prio
);
735 case RTP_PRIO_THREAD
:
736 xnice
= PRIO_MIN
- 1 - RTP_PRIO_MAX
- LP(pp
, rtprio
.prio
);
739 xnice
= PP(pp
, nice
);
743 /* format this entry */
744 snprintf(fmt
, sizeof(fmt
),
747 namelength
, namelength
,
748 get_userid(PP(pp
, ruid
)),
750 format_k(PROCSIZE(pp
)),
751 format_k(pagetok(VP(pp
, rssize
))),
760 /* return the result */
764 /* comparison routines for qsort */
767 * proc_compare - comparison function for "qsort"
768 * Compares the resource consumption of two processes using five
769 * distinct keys. The keys (in descending order of importance) are:
770 * percent cpu, cpu ticks, state, resident set size, total virtual
771 * memory usage. The process states are ordered as follows (from least
772 * to most important): WAIT, zombie, sleep, stop, start, run. The
773 * array declaration below maps a process state index into a number
774 * that reflects this ordering.
777 static unsigned char sorted_state
[] =
781 1, /* ABANDONED (WAIT) */
789 #define ORDERKEY_PCTCPU \
790 if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \
791 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
793 #define CPTICKS(p) (LP(p, uticks) + LP(p, sticks) + LP(p, iticks))
795 #define ORDERKEY_CPTICKS \
796 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
797 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
799 #define CTIME(p) (((LP(p, uticks) + LP(p, sticks) + LP(p, iticks))/1000000) + \
800 PP(p, cru).ru_stime.tv_sec + PP(p, cru).ru_utime.tv_sec)
802 #define ORDERKEY_CTIME \
803 if ((result = CTIME(p2) > CTIME(p1) ? 1 : \
804 CTIME(p2) < CTIME(p1) ? -1 : 0) == 0)
806 #define ORDERKEY_STATE \
807 if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \
808 sorted_state[(unsigned char) PP(p1, stat)]) == 0)
810 #define ORDERKEY_PRIO \
811 if ((result = LP(p2, prio) - LP(p1, prio)) == 0)
813 #define ORDERKEY_KTHREADS \
814 if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0)
816 #define ORDERKEY_KTHREADS_PRIO \
817 if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0)
819 #define ORDERKEY_RSSIZE \
820 if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0)
822 #define ORDERKEY_MEM \
823 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
825 #define ORDERKEY_PID \
826 if ( (result = PP(p1, pid) - PP(p2, pid)) == 0)
828 #define ORDERKEY_PRSSIZE \
829 if((result = VP(p2, prssize) - VP(p1, prssize)) == 0)
832 orderkey_kernidle(const struct kinfo_proc
*p1
, const struct kinfo_proc
*p2
)
834 int p1_kidle
= 0, p2_kidle
= 0;
836 if (LP(p1
, pid
) == -1 && PP(p1
, stat
) == SIDL
)
838 if (LP(p2
, pid
) == -1 && PP(p2
, stat
) == SIDL
)
841 if (!p2_kidle
&& p1_kidle
)
843 if (p2_kidle
&& !p1_kidle
)
848 #define ORDERKEY_KIDLE if ((result = orderkey_kernidle(p1, p2)) == 0)
850 /* compare_cpu - the comparison function for sorting by cpu percentage */
853 proc_compare(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
855 struct kinfo_proc
*p1
;
856 struct kinfo_proc
*p2
;
860 /* remove one level of indirection */
861 p1
= *(struct kinfo_proc
**) pp1
;
862 p2
= *(struct kinfo_proc
**) pp2
;
876 /* compare_size - the comparison function for sorting by total memory usage */
879 compare_size(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
881 struct kinfo_proc
*p1
;
882 struct kinfo_proc
*p2
;
886 /* remove one level of indirection */
887 p1
= *(struct kinfo_proc
**) pp1
;
888 p2
= *(struct kinfo_proc
**) pp2
;
902 /* compare_res - the comparison function for sorting by resident set size */
905 compare_res(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
907 struct kinfo_proc
*p1
;
908 struct kinfo_proc
*p2
;
912 /* remove one level of indirection */
913 p1
= *(struct kinfo_proc
**) pp1
;
914 p2
= *(struct kinfo_proc
**) pp2
;
928 /* compare_pres - the comparison function for sorting by proportional resident set size */
931 compare_pres(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
933 struct kinfo_proc
*p1
;
934 struct kinfo_proc
*p2
;
938 /* remove one level of indirection */
939 p1
= *(struct kinfo_proc
**) pp1
;
940 p2
= *(struct kinfo_proc
**) pp2
;
955 /* compare_time - the comparison function for sorting by total cpu time */
958 compare_time(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
960 struct kinfo_proc
*p1
;
961 struct kinfo_proc
*p2
;
965 /* remove one level of indirection */
966 p1
= *(struct kinfo_proc
**) pp1
;
967 p2
= *(struct kinfo_proc
**) pp2
;
973 ORDERKEY_KTHREADS_PRIO
984 compare_ctime(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
986 struct kinfo_proc
*p1
;
987 struct kinfo_proc
*p2
;
991 /* remove one level of indirection */
992 p1
= *(struct kinfo_proc
**) pp1
;
993 p2
= *(struct kinfo_proc
**) pp2
;
999 ORDERKEY_KTHREADS_PRIO
1009 /* compare_prio - the comparison function for sorting by cpu percentage */
1012 compare_prio(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
1014 struct kinfo_proc
*p1
;
1015 struct kinfo_proc
*p2
;
1019 /* remove one level of indirection */
1020 p1
= *(struct kinfo_proc
**) pp1
;
1021 p2
= *(struct kinfo_proc
**) pp2
;
1024 ORDERKEY_KTHREADS_PRIO
1038 compare_thr(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
1040 struct kinfo_proc
*p1
;
1041 struct kinfo_proc
*p2
;
1045 /* remove one level of indirection */
1046 p1
= *(struct kinfo_proc
**)pp1
;
1047 p2
= *(struct kinfo_proc
**)pp2
;
1050 ORDERKEY_KTHREADS_PRIO
1062 /* compare_pid - the comparison function for sorting by process id */
1065 compare_pid(struct kinfo_proc
**pp1
, struct kinfo_proc
**pp2
)
1067 struct kinfo_proc
*p1
;
1068 struct kinfo_proc
*p2
;
1071 /* remove one level of indirection */
1072 p1
= *(struct kinfo_proc
**) pp1
;
1073 p2
= *(struct kinfo_proc
**) pp2
;
1082 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
1083 * the process does not exist.
1084 * It is EXTREMLY IMPORTANT that this function work correctly.
1085 * If top runs setuid root (as in SVR4), then this function
1086 * is the only thing that stands in the way of a serious
1087 * security problem. It validates requests for the "kill"
1088 * and "renice" commands.
1095 struct kinfo_proc
**prefp
;
1096 struct kinfo_proc
*pp
;
1100 while (--xcnt
>= 0) {
1102 if (PP(pp
, pid
) == (pid_t
) pid
) {
1103 return ((int)PP(pp
, ruid
));
1111 * swapmode is based on a program called swapinfo written
1112 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
1115 swapmode(int *retavail
, int *retfree
)
1118 int pagesize
= getpagesize();
1119 struct kvm_swap swapary
[1];
1124 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
1126 n
= kvm_getswapinfo(kd
, swapary
, 1, 0);
1127 if (n
< 0 || swapary
[0].ksw_total
== 0)
1130 *retavail
= CONVERT(swapary
[0].ksw_total
);
1131 *retfree
= CONVERT(swapary
[0].ksw_total
- swapary
[0].ksw_used
);
1133 n
= (int)((double)swapary
[0].ksw_used
* 100.0 /
1134 (double)swapary
[0].ksw_total
);