2 * top - a top users display for Unix
4 * SYNOPSIS: For FreeBSD-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 FreeBSD 2.2
14 * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x
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.26 2008/10/16 01:52:33 swildner Exp $
29 #include <sys/types.h>
30 #include <sys/signal.h>
31 #include <sys/param.h>
39 #include <sys/errno.h>
40 #include <sys/sysctl.h>
44 #include <sys/vmmeter.h>
45 #include <sys/resource.h>
46 #include <sys/rtprio.h>
52 #include <osreldate.h> /* for changes in kernel structures */
54 #include <sys/kinfo.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
);
64 static int namelength
;
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: */
77 struct kinfo_proc
**next_proc
; /* points to next valid proc pointer */
78 int remaining
; /* number of pointers remaining */
81 /* declarations for load_avg */
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 */
114 /* the extra nulls in the string "run" are for adding a slash and
115 the processor number when needed */
117 char *state_abbrev
[] =
119 "", "RUN\0\0\0", "STOP", "SLEEP",
125 /* values that we stash away in _init and use in later routines */
127 static double logcpu
;
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, ",
146 /* these are for detailing the cpu states */
149 char *cpustatenames
[CPU_STATES
+ 1] = {
150 "user", "nice", "system", "interrupt", "idle", NULL
153 /* these are for detailing the memory statistics */
156 char *memorynames
[] = {
157 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
162 char *swapnames
[] = {
164 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
169 /* these are for keeping track of the proc array */
172 static int onproc
= -1;
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)
186 /* sorting orders. first is default */
187 char *ordernames
[] = {
188 "cpu", "size", "res", "time", "pri", "thr", NULL
193 cputime_percentages(int out
[CPU_STATES
], struct kinfo_cputime
*new,
194 struct kinfo_cputime
*old
)
196 struct kinfo_cputime diffs
;
197 uint64_t total_change
, half_total
;
202 diffs
.cp_user
= new->cp_user
- old
->cp_user
;
203 diffs
.cp_nice
= new->cp_nice
- old
->cp_nice
;
204 diffs
.cp_sys
= new->cp_sys
- old
->cp_sys
;
205 diffs
.cp_intr
= new->cp_intr
- old
->cp_intr
;
206 diffs
.cp_idle
= new->cp_idle
- old
->cp_idle
;
207 total_change
= diffs
.cp_user
+ diffs
.cp_nice
+ diffs
.cp_sys
+
208 diffs
.cp_intr
+ diffs
.cp_idle
;
209 old
->cp_user
= new->cp_user
;
210 old
->cp_nice
= new->cp_nice
;
211 old
->cp_sys
= new->cp_sys
;
212 old
->cp_intr
= new->cp_intr
;
213 old
->cp_idle
= new->cp_idle
;
215 /* avoid divide by zero potential */
216 if (total_change
== 0)
219 /* calculate percentages based on overall change, rounding up */
220 half_total
= total_change
>> 1;
222 out
[0] = ((diffs
.cp_user
* 1000LL + half_total
) / total_change
);
223 out
[1] = ((diffs
.cp_nice
* 1000LL + half_total
) / total_change
);
224 out
[2] = ((diffs
.cp_sys
* 1000LL + half_total
) / total_change
);
225 out
[3] = ((diffs
.cp_intr
* 1000LL + half_total
) / total_change
);
226 out
[4] = ((diffs
.cp_idle
* 1000LL + half_total
) / total_change
);
230 machine_init(struct statics
*statics
)
238 if (kinfo_get_cpus(&n_cpus
))
239 err(1, "kinfo_get_cpus failed");
241 modelen
= sizeof(smpmode
);
242 if ((sysctlbyname("machdep.smp_active", &smpmode
, &modelen
, NULL
, 0) < 0 &&
243 sysctlbyname("smp.smp_active", &smpmode
, &modelen
, NULL
, 0) < 0) ||
244 modelen
!= sizeof(smpmode
))
247 while ((pw
= getpwent()) != NULL
) {
248 if (strlen(pw
->pw_name
) > namelength
)
249 namelength
= strlen(pw
->pw_name
);
253 if (smpmode
&& namelength
> 13)
255 else if (namelength
> 15)
258 if ((kd
= kvm_open(NULL
, NULL
, NULL
, O_RDONLY
, "kvm_open")) == NULL
)
261 if (kinfo_get_sched_ccpu(&ccpu
)) {
262 fprintf(stderr
, "top: kinfo_get_sched_ccpu failed\n");
266 /* this is used in calculating WCPU -- calculate it ahead of time */
267 logcpu
= log(loaddouble(ccpu
));
273 /* get the page size with "getpagesize" and calculate pageshift from it */
274 pagesize
= getpagesize();
282 /* we only need the amount of log(2)1024 for our conversion */
283 pageshift
-= LOG1024
;
285 /* fill in the statics information */
286 statics
->procstate_names
= procstatenames
;
287 statics
->cpustate_names
= cpustatenames
;
288 statics
->memory_names
= memorynames
;
289 statics
->swap_names
= swapnames
;
291 statics
->order_names
= ordernames
;
298 char *format_header(char *uname_field
)
301 static char Header
[128];
303 snprintf(Header
, sizeof(Header
), smpmode
? smp_header
: up_header
,
304 namelength
, namelength
, uname_field
);
306 if (screen_width
<= 79)
311 cmdlength
= cmdlength
- strlen(Header
) + 6;
316 static int swappgsin
= -1;
317 static int swappgsout
= -1;
318 extern struct timeval timeout
;
321 get_system_info(struct system_info
*si
)
325 struct timeval boottime
;
330 if (cpu_states
== NULL
) {
331 cpu_states
= malloc(sizeof(*cpu_states
) * CPU_STATES
* n_cpus
);
332 if (cpu_states
== NULL
)
334 bzero(cpu_states
, sizeof(*cpu_states
) * CPU_STATES
* n_cpus
);
336 if (cp_time
== NULL
) {
337 cp_time
= malloc(2 * n_cpus
* sizeof(cp_time
[0]));
340 cp_old
= cp_time
+ n_cpus
;
342 len
= n_cpus
* sizeof(cp_old
[0]);
344 if (sysctlbyname("kern.cputime", cp_old
, &len
, NULL
, 0))
345 err(1, "kern.cputime");
348 len
= n_cpus
* sizeof(cp_time
[0]);
350 if (sysctlbyname("kern.cputime", cp_time
, &len
, NULL
, 0))
351 err(1, "kern.cputime");
353 getloadavg(si
->load_avg
, 3);
357 /* convert cp_time counts to percentages */
358 for (cpu
= 0; cpu
< n_cpus
; ++cpu
) {
359 cputime_percentages(cpu_states
+ cpu
* CPU_STATES
,
360 &cp_time
[cpu
], &cp_old
[cpu
]);
363 /* sum memory & swap statistics */
367 size_t vms_size
= sizeof(vms
);
368 size_t vmm_size
= sizeof(vmm
);
369 static unsigned int swap_delay
= 0;
370 static int swapavail
= 0;
371 static int swapfree
= 0;
372 static int bufspace
= 0;
374 if (sysctlbyname("vm.vmstats", &vms
, &vms_size
, NULL
, 0))
375 err(1, "sysctlbyname: vm.vmstats");
377 if (sysctlbyname("vm.vmmeter", &vmm
, &vmm_size
, NULL
, 0))
378 err(1, "sysctlbyname: vm.vmmeter");
380 if (kinfo_get_vfs_bufspace(&bufspace
))
381 err(1, "kinfo_get_vfs_bufspace");
383 /* convert memory stats to Kbytes */
384 memory_stats
[0] = pagetok(vms
.v_active_count
);
385 memory_stats
[1] = pagetok(vms
.v_inactive_count
);
386 memory_stats
[2] = pagetok(vms
.v_wire_count
);
387 memory_stats
[3] = pagetok(vms
.v_cache_count
);
388 memory_stats
[4] = bufspace
/ 1024;
389 memory_stats
[5] = pagetok(vms
.v_free_count
);
390 memory_stats
[6] = -1;
398 /* compute differences between old and new swap statistic */
400 swap_stats
[4] = pagetok(((vmm
.v_swappgsin
- swappgsin
)));
401 swap_stats
[5] = pagetok(((vmm
.v_swappgsout
- swappgsout
)));
404 swappgsin
= vmm
.v_swappgsin
;
405 swappgsout
= vmm
.v_swappgsout
;
407 /* call CPU heavy swapmode() only for changes */
408 if (swap_stats
[4] > 0 || swap_stats
[5] > 0 || swap_delay
== 0) {
409 swap_stats
[3] = swapmode(&swapavail
, &swapfree
);
410 swap_stats
[0] = swapavail
;
411 swap_stats
[1] = swapavail
- swapfree
;
412 swap_stats
[2] = swapfree
;
418 /* set arrays and strings */
419 si
->cpustates
= cpu_states
;
420 si
->memory
= memory_stats
;
421 si
->swap
= swap_stats
;
425 si
->last_pid
= lastpid
;
431 * Print how long system has been up.
432 * (Found by looking getting "boottime" from the kernel)
435 mib
[1] = KERN_BOOTTIME
;
436 bt_size
= sizeof(boottime
);
437 if (sysctl(mib
, 2, &boottime
, &bt_size
, NULL
, 0) != -1 &&
438 boottime
.tv_sec
!= 0) {
439 si
->boottime
= boottime
;
441 si
->boottime
.tv_sec
= -1;
445 static struct handle handle
;
447 caddr_t
get_process_info(struct system_info
*si
, struct process_select
*sel
,
453 struct kinfo_proc
**prefp
;
454 struct kinfo_proc
*pp
;
456 /* these are copied out of sel for speed */
460 int show_only_threads
;
465 pbase
= kvm_getprocs(kd
, KERN_PROC_ALL
, 0, &nproc
);
467 pref
= (struct kinfo_proc
**) realloc(pref
, sizeof(struct kinfo_proc
*)
469 if (pref
== NULL
|| pbase
== NULL
) {
470 (void) fprintf(stderr
, "top: Out of memory.\n");
473 /* get a pointer to the states summary array */
474 si
->procstates
= process_states
;
476 /* set up flags which define what we are going to select */
477 show_idle
= sel
->idle
;
478 show_self
= sel
->self
;
479 show_system
= sel
->system
;
480 show_threads
= sel
->threads
|| sel
->only_threads
;
481 show_only_threads
= sel
->only_threads
;
482 show_uid
= sel
->uid
!= -1;
483 show_command
= sel
->command
!= NULL
;
485 /* count up process states and get pointers to interesting procs */
488 memset((char *)process_states
, 0, sizeof(process_states
));
490 for (pp
= pbase
, i
= 0; i
< nproc
; pp
++, i
++)
493 * Place pointers to each valid proc structure in pref[].
494 * Process slots that are actually in use have a non-zero
495 * status field. Processes with P_SYSTEM set are system
496 * processes---these get ignored unless show_sysprocs is set.
498 if ((show_threads
&& (LP(pp
, pid
) == -1)) ||
499 (!show_only_threads
&& (PP(pp
, stat
) != 0 &&
500 (show_self
!= PP(pp
, pid
)) &&
501 (show_system
|| ((PP(pp
, flags
) & P_SYSTEM
) == 0)))))
504 process_states
[(unsigned char) PP(pp
, stat
)]++;
505 if ((show_threads
&& (LP(pp
, pid
) == -1)) ||
506 (!show_only_threads
&& PP(pp
, stat
) != SZOMB
&&
507 (show_idle
|| (LP(pp
, pctcpu
) != 0) ||
508 (LP(pp
, stat
) == LSRUN
)) &&
509 (!show_uid
|| PP(pp
, ruid
) == (uid_t
)sel
->uid
)))
517 /* if requested, sort the "interesting" processes */
520 qsort((char *)pref
, active_procs
, sizeof(struct kinfo_proc
*), compare
);
523 /* remember active and total counts */
524 si
->p_total
= total_procs
;
525 si
->p_active
= pref_len
= active_procs
;
527 /* pass back a handle */
528 handle
.next_proc
= pref
;
529 handle
.remaining
= active_procs
;
530 return((caddr_t
)&handle
);
533 char fmt
[128]; /* static area where result is built */
535 char *format_next_process(caddr_t handle
, char *(*get_userid
)())
537 struct kinfo_proc
*pp
;
546 /* find and remember the next proc structure */
547 hp
= (struct handle
*)handle
;
548 pp
= *(hp
->next_proc
++);
551 /* set the wrapper for the process/thread name */
552 if ((PP(pp
, flags
) & P_SWAPPEDOUT
))
553 wrapper
= "[]"; /* swapped process [pname] */
554 else if (((PP(pp
, flags
) & P_SYSTEM
) != 0) && (LP(pp
, pid
) > 0))
555 wrapper
= "()"; /* system process (pname) */
556 else if (show_threads
&& (LP(pp
, pid
) == -1))
557 wrapper
= "<>"; /* pure kernel threads <thread> */
561 /* get the process's command name */
562 if (wrapper
!= NULL
) {
563 char *comm
= PP(pp
, comm
);
564 #define COMSIZ sizeof(PP(pp, comm))
566 (void) strncpy(buf
, comm
, COMSIZ
);
567 comm
[0] = wrapper
[0];
568 (void) strncpy(&comm
[1], buf
, COMSIZ
- 2);
569 comm
[COMSIZ
- 2] = '\0';
570 (void) strncat(comm
, &wrapper
[1], COMSIZ
- 1);
571 comm
[COMSIZ
- 1] = '\0';
575 * Convert the process's runtime from microseconds to seconds. This
576 * time includes the interrupt time although that is not wanted here.
577 * ps(1) is similarly sloppy.
579 cputime
= (LP(pp
, uticks
) + LP(pp
, sticks
)) / 1000000;
581 /* calculate the base for cpu percentages */
582 pct
= pctdouble(LP(pp
, pctcpu
));
584 /* generate "STATE" field */
585 switch (state
= LP(pp
, stat
)) {
587 if (smpmode
&& LP(pp
, tdflags
) & TDF_RUNNING
)
588 sprintf(status
, "CPU%d", LP(pp
, cpuid
));
590 strcpy(status
, "RUN");
593 if (LP(pp
, wmesg
) != NULL
) {
594 sprintf(status
, "%.6s", LP(pp
, wmesg
));
601 state
< sizeof(state_abbrev
) / sizeof(*state_abbrev
))
602 sprintf(status
, "%.6s", state_abbrev
[(unsigned char) state
]);
604 sprintf(status
, "?%5d", state
);
608 if (PP(pp
, stat
) == SZOMB
)
609 strcpy(status
, "ZOMB");
612 * idle time 0 - 31 -> nice value +21 - +52
613 * normal time -> nice value -20 - +20
614 * real time 0 - 31 -> nice value -52 - -21
615 * thread 0 - 31 -> nice value -53 -
617 switch(LP(pp
, rtprio
.type
)) {
618 case RTP_PRIO_REALTIME
:
619 nice
= PRIO_MIN
- 1 - RTP_PRIO_MAX
+ LP(pp
, rtprio
.prio
);
622 nice
= PRIO_MAX
+ 1 + LP(pp
, rtprio
.prio
);
624 case RTP_PRIO_THREAD
:
625 nice
= PRIO_MIN
- 1 - RTP_PRIO_MAX
- LP(pp
, rtprio
.prio
);
633 /* format this entry */
634 snprintf(fmt
, sizeof(fmt
),
635 smpmode
? smp_Proc_format
: up_Proc_format
,
637 namelength
, namelength
,
638 (*get_userid
)(PP(pp
, ruid
)),
639 (show_threads
&& (LP(pp
, pid
) == -1)) ? LP(pp
, tdprio
) :
642 format_k2(PROCSIZE(pp
)),
643 format_k2(pagetok(VP(pp
, rssize
))),
645 smpmode
? LP(pp
, cpuid
) : 0,
646 format_time(cputime
),
647 100.0 * weighted_cpu(pct
, pp
),
650 printable(PP(pp
, comm
)));
652 /* return the result */
658 * check_nlist(nlst) - checks the nlist to see if any symbols were not
659 * found. For every symbol that was not found, a one-line
660 * message is printed to stderr. The routine returns the
661 * number of symbols NOT found.
664 static int check_nlist(struct nlist
*nlst
)
668 /* check to see if we got ALL the symbols we requested */
669 /* this will write one line to stderr for every symbol not found */
672 while (nlst
->n_name
!= NULL
)
674 if (nlst
->n_type
== 0)
676 /* this one wasn't found */
677 (void) fprintf(stderr
, "kernel: no symbol named `%s'\n",
687 /* comparison routines for qsort */
690 * proc_compare - comparison function for "qsort"
691 * Compares the resource consumption of two processes using five
692 * distinct keys. The keys (in descending order of importance) are:
693 * percent cpu, cpu ticks, state, resident set size, total virtual
694 * memory usage. The process states are ordered as follows (from least
695 * to most important): WAIT, zombie, sleep, stop, start, run. The
696 * array declaration below maps a process state index into a number
697 * that reflects this ordering.
700 static unsigned char sorted_state
[] =
704 1, /* ABANDONED (WAIT) */
712 #define ORDERKEY_PCTCPU \
713 if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \
714 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
716 #define CPTICKS(p) (LP(p, uticks) + LP(p, sticks))
718 #define ORDERKEY_CPTICKS \
719 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
720 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
722 #define ORDERKEY_STATE \
723 if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \
724 sorted_state[(unsigned char) PP(p1, stat)]) == 0)
726 #define ORDERKEY_PRIO \
727 if ((result = LP(p2, prio) - LP(p1, prio)) == 0)
729 #define ORDERKEY_KTHREADS \
730 if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0)
732 #define ORDERKEY_KTHREADS_PRIO \
733 if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0)
735 #define ORDERKEY_RSSIZE \
736 if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0)
738 #define ORDERKEY_MEM \
739 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
741 /* compare_cpu - the comparison function for sorting by cpu percentage */
745 compare_cpu(struct proc
**pp1
, struct proc
**pp2
)
747 proc_compare(struct proc
**pp1
, struct proc
**pp2
)
750 struct kinfo_proc
*p1
;
751 struct kinfo_proc
*p2
;
755 /* remove one level of indirection */
756 p1
= *(struct kinfo_proc
**) pp1
;
757 p2
= *(struct kinfo_proc
**) pp2
;
771 /* compare routines */
772 int compare_size(), compare_res(), compare_time(), compare_prio(), compare_thr();
774 int (*proc_compares
[])() = {
784 /* compare_size - the comparison function for sorting by total memory usage */
787 compare_size(struct proc
**pp1
, struct proc
**pp2
)
789 struct kinfo_proc
*p1
;
790 struct kinfo_proc
*p2
;
794 /* remove one level of indirection */
795 p1
= *(struct kinfo_proc
**) pp1
;
796 p2
= *(struct kinfo_proc
**) pp2
;
809 /* compare_res - the comparison function for sorting by resident set size */
812 compare_res(struct proc
**pp1
, struct proc
**pp2
)
814 struct kinfo_proc
*p1
;
815 struct kinfo_proc
*p2
;
819 /* remove one level of indirection */
820 p1
= *(struct kinfo_proc
**) pp1
;
821 p2
= *(struct kinfo_proc
**) pp2
;
834 /* compare_time - the comparison function for sorting by total cpu time */
837 compare_time(struct proc
**pp1
, struct proc
**pp2
)
839 struct kinfo_proc
*p1
;
840 struct kinfo_proc
*p2
;
844 /* remove one level of indirection */
845 p1
= *(struct kinfo_proc
**) pp1
;
846 p2
= *(struct kinfo_proc
**) pp2
;
851 ORDERKEY_KTHREADS_PRIO
861 /* compare_prio - the comparison function for sorting by cpu percentage */
864 compare_prio(struct proc
**pp1
, struct proc
**pp2
)
866 struct kinfo_proc
*p1
;
867 struct kinfo_proc
*p2
;
871 /* remove one level of indirection */
872 p1
= *(struct kinfo_proc
**) pp1
;
873 p2
= *(struct kinfo_proc
**) pp2
;
876 ORDERKEY_KTHREADS_PRIO
889 compare_thr(struct proc
**pp1
, struct proc
**pp2
)
891 struct kinfo_proc
*p1
;
892 struct kinfo_proc
*p2
;
896 /* remove one level of indirection */
897 p1
= *(struct kinfo_proc
**) pp1
;
898 p2
= *(struct kinfo_proc
**) pp2
;
901 ORDERKEY_KTHREADS_PRIO
916 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
917 * the process does not exist.
918 * It is EXTREMLY IMPORTANT that this function work correctly.
919 * If top runs setuid root (as in SVR4), then this function
920 * is the only thing that stands in the way of a serious
921 * security problem. It validates requests for the "kill"
922 * and "renice" commands.
925 int proc_owner(int pid
)
928 struct kinfo_proc
**prefp
;
929 struct kinfo_proc
*pp
;
936 if (PP(pp
, pid
) == (pid_t
)pid
)
938 return((int)PP(pp
, ruid
));
946 * swapmode is based on a program called swapinfo written
947 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
950 swapmode(int *retavail
, int *retfree
)
953 int pagesize
= getpagesize();
954 struct kvm_swap swapary
[1];
959 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
961 n
= kvm_getswapinfo(kd
, swapary
, 1, 0);
962 if (n
< 0 || swapary
[0].ksw_total
== 0)
965 *retavail
= CONVERT(swapary
[0].ksw_total
);
966 *retfree
= CONVERT(swapary
[0].ksw_total
- swapary
[0].ksw_used
);
968 n
= (int)((double)swapary
[0].ksw_used
* 100.0 /
969 (double)swapary
[0].ksw_total
);