2 * top - a top users display for Unix
5 * Originally written for BSD4.4 system by Christos Zoulas.
6 * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider
7 * Order support hacked in from top-3.5beta6/machine/m_aix41.c
8 * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/)
10 * AUTHOR: Christos Zoulas <christos@ee.cornell.edu>
11 * Steven Wallace <swallace@freebsd.org>
12 * Wolfram Schneider <wosch@FreeBSD.org>
13 * Thomas Moestl <tmoestl@gmx.net>
18 #include <sys/param.h>
19 #include <sys/errno.h>
22 #include <sys/resource.h>
23 #include <sys/rtprio.h>
24 #include <sys/signal.h>
25 #include <sys/sysctl.h>
28 #include <sys/vmmeter.h>
53 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
54 #define SMPUNAMELEN 13
57 extern struct timeval timeout;
59 enum displaymodes displaymode;
60 static int namelength = 8;
61 /* TOP_JID_LEN based on max of 999999 */
63 #define TOP_SWAP_LEN 6
65 static int swaplength;
66 static int cmdlengthdelta;
68 /* get_process_info passes back a handle. This is what it looks like: */
71 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
72 int remaining; /* number of pointers remaining */
76 /* define what weighted cpu is. */
77 #define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \
78 ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu))))
80 /* what we consider to be process size: */
81 #define PROCSIZE(pp) ((pp)->ki_size / 1024)
83 #define RU(pp) (&(pp)->ki_rusage)
85 (RU(pp)->ru_inblock + RU(pp)->ru_oublock + RU(pp)->ru_majflt)
87 #define PCTCPU(pp) (pcpu[pp - pbase])
89 /* definitions for indices in the nlist array */
92 * These definitions control the format of the per-process area
95 static char io_header[] =
96 " PID%*s %-*.*s VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND";
98 #define io_Proc_format \
99 "%5d%*s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s"
101 static char smp_header_thr_and_pid[] =
102 " PID%*s %-*.*s THR PRI NICE SIZE RES%*s STATE C TIME %7s COMMAND";
103 static char smp_header_tid_only[] =
104 " THR%*s %-*.*s " "PRI NICE SIZE RES%*s STATE C TIME %7s COMMAND";
106 #define smp_Proc_format \
107 "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s %2d%7s %6.2f%% %.*s"
109 static char up_header_thr_and_pid[] =
110 " PID%*s %-*.*s THR PRI NICE SIZE RES%*s STATE TIME %7s COMMAND";
111 static char up_header_tid_only[] =
112 " THR%*s %-*.*s " "PRI NICE SIZE RES%*s STATE TIME %7s COMMAND";
114 #define up_Proc_format \
115 "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s%.0d%7s %6.2f%% %.*s"
118 /* process state names for the "STATE" column of the display */
119 /* the extra nulls in the string "run" are for adding a slash and
120 the processor number when needed */
122 static const char *state_abbrev[] = {
123 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK"
129 /* values that we stash away in _init and use in later routines */
131 static double logcpu;
133 /* these are retrieved from the kernel in _init */
135 static load_avg ccpu;
137 /* these are used in the get_ functions */
141 /* these are for calculating cpu state percentages */
143 static long cp_time[CPUSTATES];
144 static long cp_old[CPUSTATES];
145 static long cp_diff[CPUSTATES];
147 /* these are for detailing the process states */
149 static int process_states[8];
150 static char *procstatenames[] = {
151 "", " starting, ", " running, ", " sleeping, ", " stopped, ",
152 " zombie, ", " waiting, ", " lock, ",
156 /* these are for detailing the cpu states */
158 static int cpu_states[CPUSTATES];
159 static char *cpustatenames[] = {
160 "user", "nice", "system", "interrupt", "idle", NULL
163 /* these are for detailing the memory statistics */
165 static int memory_stats[7];
166 static char *memorynames[] = {
167 "K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ",
171 static int arc_stats[7];
172 static char *arcnames[] = {
173 "K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other",
177 static int carc_stats[4];
178 static char *carcnames[] = {
179 "K Compressed, ", "K Uncompressed, ", ":1 Ratio, ",
183 static int swap_stats[7];
184 static char *swapnames[] = {
185 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
190 /* these are for keeping track of the proc array */
193 static int onproc = -1;
195 static struct kinfo_proc *pbase;
196 static struct kinfo_proc **pref;
197 static struct kinfo_proc *previous_procs;
198 static struct kinfo_proc **previous_pref;
199 static int previous_proc_count = 0;
200 static int previous_proc_count_max = 0;
201 static int previous_thread;
203 /* data used for recalculating pctcpu */
205 static struct timespec proc_uptime;
206 static struct timeval proc_wall_time;
207 static struct timeval previous_wall_time;
208 static uint64_t previous_interval = 0;
210 /* total number of io operations */
211 static long total_inblock;
212 static long total_oublock;
213 static long total_majflt;
215 /* these are for getting the memory statistics */
217 static int arc_enabled;
218 static int carc_enabled;
219 static int pageshift; /* log base 2 of the pagesize */
221 /* define pagetok in terms of pageshift */
223 #define pagetok(size) ((size) << pageshift)
226 #define ki_swap(kip) \
227 ((kip)->ki_swrss > (kip)->ki_rssize ? (kip)->ki_swrss - (kip)->ki_rssize : 0)
230 * Sorting orders. The first element is the default.
232 char *ordernames[] = {
233 "cpu", "size", "res", "time", "pri", "threads",
234 "total", "read", "write", "fault", "vcsw", "ivcsw",
235 "jid", "swap", "pid", NULL
238 /* Per-cpu time states */
242 static u_long cpumask;
244 static long *pcpu_cp_time;
245 static long *pcpu_cp_old;
246 static long *pcpu_cp_diff;
247 static int *pcpu_cpu_states;
249 static int compare_swap(const void *a, const void *b);
250 static int compare_jid(const void *a, const void *b);
251 static int compare_pid(const void *a, const void *b);
252 static int compare_tid(const void *a, const void *b);
253 static const char *format_nice(const struct kinfo_proc *pp);
254 static void getsysctl(const char *name, void *ptr, size_t len);
255 static int swapmode(int *retavail, int *retfree);
256 static void update_layout(void);
257 static int find_uid(uid_t needle, int *haystack);
260 find_uid(uid_t needle, int *haystack)
264 for (; i < TOP_MAX_UIDS; ++i)
265 if ((uid_t)haystack[i] == needle)
271 toggle_pcpustats(void)
279 /* Adjust display based on ncpus and the ARC state. */
287 y_swap = 4 + arc_enabled + carc_enabled;
288 y_idlecursor = 5 + arc_enabled + carc_enabled;
289 y_message = 5 + arc_enabled + carc_enabled;
290 y_header = 6 + arc_enabled + carc_enabled;
291 y_procs = 7 + arc_enabled + carc_enabled;
292 Header_lines = 7 + arc_enabled + carc_enabled;
299 y_idlecursor += ncpus - 1;
300 y_message += ncpus - 1;
301 y_header += ncpus - 1;
302 y_procs += ncpus - 1;
303 Header_lines += ncpus - 1;
308 machine_init(struct statics *statics)
310 int i, j, empty, pagesize;
315 size = sizeof(smpmode);
316 if ((sysctlbyname("machdep.smp_active", &smpmode, &size,
318 sysctlbyname("kern.smp.active", &smpmode, &size,
320 size != sizeof(smpmode))
323 size = sizeof(arc_size);
324 if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc_size, &size,
325 NULL, 0) == 0 && arc_size != 0)
327 size = sizeof(carc_en);
329 sysctlbyname("vfs.zfs.compressed_arc_enabled", &carc_en, &size,
330 NULL, 0) == 0 && carc_en == 1)
333 namelength = MAXLOGNAME;
334 if (smpmode && namelength > SMPUNAMELEN)
335 namelength = SMPUNAMELEN;
336 else if (namelength > UPUNAMELEN)
337 namelength = UPUNAMELEN;
339 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
343 GETSYSCTL("kern.ccpu", ccpu);
345 /* this is used in calculating WCPU -- calculate it ahead of time */
346 logcpu = log(loaddouble(ccpu));
354 /* get the page size and calculate pageshift from it */
355 pagesize = getpagesize();
357 while (pagesize > 1) {
362 /* we only need the amount of log(2)1024 for our conversion */
363 pageshift -= LOG1024;
365 /* fill in the statics information */
366 statics->procstate_names = procstatenames;
367 statics->cpustate_names = cpustatenames;
368 statics->memory_names = memorynames;
370 statics->arc_names = arcnames;
372 statics->arc_names = NULL;
374 statics->carc_names = carcnames;
376 statics->carc_names = NULL;
377 statics->swap_names = swapnames;
378 statics->order_names = ordernames;
380 /* Allocate state for per-CPU stats. */
383 GETSYSCTL("kern.smp.maxcpus", maxcpu);
384 size = sizeof(long) * maxcpu * CPUSTATES;
385 times = calloc(size, 1);
387 err(1, "calloc %zu bytes", size);
388 if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1)
389 err(1, "sysctlbyname kern.cp_times");
390 pcpu_cp_time = calloc(1, size);
391 maxid = (size / CPUSTATES / sizeof(long)) - 1;
392 for (i = 0; i <= maxid; i++) {
394 for (j = 0; empty && j < CPUSTATES; j++) {
395 if (times[i * CPUSTATES + j] != 0)
399 cpumask |= (1ul << i);
403 size = sizeof(long) * ncpus * CPUSTATES;
405 pcpu_cp_old = calloc(1, size);
406 pcpu_cp_diff = calloc(1, size);
407 pcpu_cpu_states = calloc(1, size);
408 statics->ncpus = ncpus;
417 format_header(char *uname_field)
419 static char Header[128];
423 jidlength = TOP_JID_LEN + 1; /* +1 for extra left space. */
428 swaplength = TOP_SWAP_LEN + 1; /* +1 for extra left space */
432 switch (displaymode) {
435 * The logic of picking the right header format seems reverse
436 * here because we only want to display a THR column when
437 * "thread mode" is off (and threads are not listed as
441 (ps.thread ? smp_header_tid_only : smp_header_thr_and_pid) :
442 (ps.thread ? up_header_tid_only : up_header_thr_and_pid);
443 snprintf(Header, sizeof(Header), prehead,
444 jidlength, ps.jail ? " JID" : "",
445 namelength, namelength, uname_field,
446 swaplength, ps.swap ? " SWAP" : "",
447 ps.wcpu ? "WCPU" : "CPU");
451 snprintf(Header, sizeof(Header), prehead,
452 jidlength, ps.jail ? " JID" : "",
453 namelength, namelength, uname_field);
456 assert("displaymode must not be set to DISP_MAX");
458 cmdlengthdelta = strlen(Header) - 7;
462 static int swappgsin = -1;
463 static int swappgsout = -1;
467 get_system_info(struct system_info *si)
469 struct loadavg sysload;
471 struct timeval boottime;
472 uint64_t arc_stat, arc_stat2;
476 /* get the CPU stats */
477 size = (maxid + 1) * CPUSTATES * sizeof(long);
478 if (sysctlbyname("kern.cp_times", pcpu_cp_time, &size, NULL, 0) == -1)
479 err(1, "sysctlbyname kern.cp_times");
480 GETSYSCTL("kern.cp_time", cp_time);
481 GETSYSCTL("vm.loadavg", sysload);
482 GETSYSCTL("kern.lastpid", lastpid);
484 /* convert load averages to doubles */
485 for (i = 0; i < 3; i++)
486 si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale;
488 /* convert cp_time counts to percentages */
489 for (i = j = 0; i <= maxid; i++) {
490 if ((cpumask & (1ul << i)) == 0)
492 percentages(CPUSTATES, &pcpu_cpu_states[j * CPUSTATES],
493 &pcpu_cp_time[j * CPUSTATES],
494 &pcpu_cp_old[j * CPUSTATES],
495 &pcpu_cp_diff[j * CPUSTATES]);
498 percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
500 /* sum memory & swap statistics */
502 static unsigned int swap_delay = 0;
503 static int swapavail = 0;
504 static int swapfree = 0;
505 static long bufspace = 0;
506 static uint64_t nspgsin, nspgsout;
508 GETSYSCTL("vfs.bufspace", bufspace);
509 GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]);
510 GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]);
511 GETSYSCTL("vm.stats.vm.v_laundry_count", memory_stats[2]);
512 GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[3]);
513 GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]);
514 GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin);
515 GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout);
516 /* convert memory stats to Kbytes */
517 memory_stats[0] = pagetok(memory_stats[0]);
518 memory_stats[1] = pagetok(memory_stats[1]);
519 memory_stats[2] = pagetok(memory_stats[2]);
520 memory_stats[3] = pagetok(memory_stats[3]);
521 memory_stats[4] = bufspace / 1024;
522 memory_stats[5] = pagetok(memory_stats[5]);
523 memory_stats[6] = -1;
531 /* compute differences between old and new swap statistic */
533 swap_stats[4] = pagetok(((nspgsin - swappgsin)));
534 swap_stats[5] = pagetok(((nspgsout - swappgsout)));
538 swappgsout = nspgsout;
540 /* call CPU heavy swapmode() only for changes */
541 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
542 swap_stats[3] = swapmode(&swapavail, &swapfree);
543 swap_stats[0] = swapavail;
544 swap_stats[1] = swapavail - swapfree;
545 swap_stats[2] = swapfree;
552 GETSYSCTL("kstat.zfs.misc.arcstats.size", arc_stat);
553 arc_stats[0] = arc_stat >> 10;
554 GETSYSCTL("vfs.zfs.mfu_size", arc_stat);
555 arc_stats[1] = arc_stat >> 10;
556 GETSYSCTL("vfs.zfs.mru_size", arc_stat);
557 arc_stats[2] = arc_stat >> 10;
558 GETSYSCTL("vfs.zfs.anon_size", arc_stat);
559 arc_stats[3] = arc_stat >> 10;
560 GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc_stat);
561 GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc_stat2);
562 arc_stats[4] = (arc_stat + arc_stat2) >> 10;
563 GETSYSCTL("kstat.zfs.misc.arcstats.other_size", arc_stat);
564 arc_stats[5] = arc_stat >> 10;
568 GETSYSCTL("kstat.zfs.misc.arcstats.compressed_size", arc_stat);
569 carc_stats[0] = arc_stat >> 10;
570 carc_stats[2] = arc_stat >> 10; /* For ratio */
571 GETSYSCTL("kstat.zfs.misc.arcstats.uncompressed_size", arc_stat);
572 carc_stats[1] = arc_stat >> 10;
573 si->carc = carc_stats;
576 /* set arrays and strings */
578 si->cpustates = pcpu_cpu_states;
581 si->cpustates = cpu_states;
584 si->memory = memory_stats;
585 si->swap = swap_stats;
589 si->last_pid = lastpid;
595 * Print how long system has been up.
596 * (Found by looking getting "boottime" from the kernel)
599 mib[1] = KERN_BOOTTIME;
600 size = sizeof(boottime);
601 if (sysctl(mib, nitems(mib), &boottime, &size, NULL, 0) != -1 &&
602 boottime.tv_sec != 0) {
603 si->boottime = boottime;
605 si->boottime.tv_sec = -1;
609 #define NOPROC ((void *)-1)
612 * We need to compare data from the old process entry with the new
614 * To facilitate doing this quickly we stash a pointer in the kinfo_proc
615 * structure to cache the mapping. We also use a negative cache pointer
616 * of NOPROC to avoid duplicate lookups.
617 * XXX: this could be done when the actual processes are fetched, we do
618 * it here out of laziness.
620 static const struct kinfo_proc *
621 get_old_proc(struct kinfo_proc *pp)
623 struct kinfo_proc **oldpp, *oldp;
626 * If this is the first fetch of the kinfo_procs then we don't have
627 * any previous entries.
629 if (previous_proc_count == 0)
631 /* negative cache? */
632 if (pp->ki_udata == NOPROC)
635 if (pp->ki_udata != NULL)
636 return (pp->ki_udata);
639 * 1) look up based on pid.
640 * 2) compare process start.
641 * If we fail here, then setup a negative cache entry, otherwise
644 oldpp = bsearch(&pp, previous_pref, previous_proc_count,
645 sizeof(*previous_pref), ps.thread ? compare_tid : compare_pid);
647 pp->ki_udata = NOPROC;
651 if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) {
652 pp->ki_udata = NOPROC;
660 * Return the total amount of IO done in blocks in/out and faults.
661 * store the values individually in the pointers passed in.
664 get_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp,
665 long *vcsw, long *ivcsw)
667 const struct kinfo_proc *oldp;
668 static struct kinfo_proc dummy;
671 oldp = get_old_proc(pp);
673 bzero(&dummy, sizeof(dummy));
676 *inp = RU(pp)->ru_inblock - RU(oldp)->ru_inblock;
677 *oup = RU(pp)->ru_oublock - RU(oldp)->ru_oublock;
678 *flp = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
679 *vcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
680 *ivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
682 (RU(pp)->ru_inblock - RU(oldp)->ru_inblock) +
683 (RU(pp)->ru_oublock - RU(oldp)->ru_oublock) +
684 (RU(pp)->ru_majflt - RU(oldp)->ru_majflt);
689 * If there was a previous update, use the delta in ki_runtime over
690 * the previous interval to calculate pctcpu. Otherwise, fall back
691 * to using the kernel's ki_pctcpu.
694 proc_calc_pctcpu(struct kinfo_proc *pp)
696 const struct kinfo_proc *oldp;
698 if (previous_interval != 0) {
699 oldp = get_old_proc(pp);
701 return ((double)(pp->ki_runtime - oldp->ki_runtime)
702 / previous_interval);
705 * If this process/thread was created during the previous
706 * interval, charge it's total runtime to the previous
709 else if (pp->ki_start.tv_sec > previous_wall_time.tv_sec ||
710 (pp->ki_start.tv_sec == previous_wall_time.tv_sec &&
711 pp->ki_start.tv_usec >= previous_wall_time.tv_usec))
712 return ((double)pp->ki_runtime / previous_interval);
714 return (pctdouble(pp->ki_pctcpu));
718 * Return true if this process has used any CPU time since the
722 proc_used_cpu(struct kinfo_proc *pp)
724 const struct kinfo_proc *oldp;
726 oldp = get_old_proc(pp);
728 return (PCTCPU(pp) != 0);
729 return (pp->ki_runtime != oldp->ki_runtime ||
730 RU(pp)->ru_nvcsw != RU(oldp)->ru_nvcsw ||
731 RU(pp)->ru_nivcsw != RU(oldp)->ru_nivcsw);
735 * Return the total number of block in/out and faults by a process.
738 get_io_total(struct kinfo_proc *pp)
742 return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy));
745 static struct handle handle;
748 get_process_info(struct system_info *si, struct process_select *sel,
749 int (*compare)(const void *, const void *))
754 long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw;
757 struct kinfo_proc **prefp;
758 struct kinfo_proc *pp;
759 struct timespec previous_proc_uptime;
761 /* these are copied out of sel for speed */
771 * If thread state was toggled, don't cache the previous processes.
773 if (previous_thread != sel->thread)
775 previous_thread = sel->thread;
778 * Save the previous process info.
780 if (previous_proc_count_max < nproc) {
781 free(previous_procs);
782 previous_procs = calloc(nproc, sizeof(*previous_procs));
784 previous_pref = calloc(nproc, sizeof(*previous_pref));
785 if (previous_procs == NULL || previous_pref == NULL) {
786 fprintf(stderr, "top: Out of memory.\n");
787 quit(TOP_EX_SYS_ERROR);
789 previous_proc_count_max = nproc;
792 for (i = 0; i < nproc; i++)
793 previous_pref[i] = &previous_procs[i];
794 bcopy(pbase, previous_procs, nproc * sizeof(*previous_procs));
795 qsort(previous_pref, nproc, sizeof(*previous_pref),
796 ps.thread ? compare_tid : compare_pid);
798 previous_proc_count = nproc;
799 previous_proc_uptime = proc_uptime;
800 previous_wall_time = proc_wall_time;
801 previous_interval = 0;
803 pbase = kvm_getprocs(kd, sel->thread ? KERN_PROC_ALL : KERN_PROC_PROC,
805 (void)gettimeofday(&proc_wall_time, NULL);
806 if (clock_gettime(CLOCK_UPTIME, &proc_uptime) != 0)
807 memset(&proc_uptime, 0, sizeof(proc_uptime));
808 else if (previous_proc_uptime.tv_sec != 0 &&
809 previous_proc_uptime.tv_nsec != 0) {
810 previous_interval = (proc_uptime.tv_sec -
811 previous_proc_uptime.tv_sec) * 1000000;
812 nsec = proc_uptime.tv_nsec - previous_proc_uptime.tv_nsec;
814 previous_interval -= 1000000;
817 previous_interval += nsec / 1000;
819 if (nproc > onproc) {
820 pref = realloc(pref, sizeof(*pref) * nproc);
821 pcpu = realloc(pcpu, sizeof(*pcpu) * nproc);
824 if (pref == NULL || pbase == NULL || pcpu == NULL) {
825 (void) fprintf(stderr, "top: Out of memory.\n");
826 quit(TOP_EX_SYS_ERROR);
828 /* get a pointer to the states summary array */
829 si->procstates = process_states;
831 /* set up flags which define what we are going to select */
832 show_idle = sel->idle;
833 show_jid = sel->jid != -1;
834 show_self = sel->self == -1;
835 show_system = sel->system;
836 show_uid = sel->uid[0] != -1;
837 show_pid = sel->pid != -1;
838 show_kidle = sel->kidle;
840 /* count up process states and get pointers to interesting procs */
846 memset(process_states, 0, sizeof(process_states));
848 for (pp = pbase, i = 0; i < nproc; pp++, i++) {
850 if (pp->ki_stat == 0)
854 if (!show_self && pp->ki_pid == sel->self)
858 if (!show_system && (pp->ki_flag & P_SYSTEM))
859 /* skip system process */
862 p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt,
864 total_inblock += p_inblock;
865 total_oublock += p_oublock;
866 total_majflt += p_majflt;
868 process_states[(unsigned char)pp->ki_stat]++;
870 if (pp->ki_stat == SZOMB)
874 if (!show_kidle && pp->ki_tdflags & TDF_IDLETD)
875 /* skip kernel idle process */
878 PCTCPU(pp) = proc_calc_pctcpu(pp);
879 if (sel->thread && PCTCPU(pp) > 1.0)
881 if (displaymode == DISP_CPU && !show_idle &&
882 (!proc_used_cpu(pp) ||
883 pp->ki_stat == SSTOP || pp->ki_stat == SIDL))
884 /* skip idle or non-running processes */
887 if (displaymode == DISP_IO && !show_idle && p_io == 0)
888 /* skip processes that aren't doing I/O */
891 if (show_jid && pp->ki_jid != sel->jid)
892 /* skip proc. that don't belong to the selected JID */
895 if (show_uid && !find_uid(pp->ki_ruid, sel->uid))
896 /* skip proc. that don't belong to the selected UID */
899 if (show_pid && pp->ki_pid != sel->pid)
906 /* if requested, sort the "interesting" processes */
908 qsort(pref, active_procs, sizeof(*pref), compare);
910 /* remember active and total counts */
911 si->p_total = total_procs;
912 si->p_pactive = pref_len = active_procs;
914 /* pass back a handle */
915 handle.next_proc = pref;
916 handle.remaining = active_procs;
917 return ((caddr_t)&handle);
920 static char fmt[512]; /* static area where result is built */
923 format_next_process(caddr_t xhandle, char *(*get_userid)(uid_t), int flags)
925 struct kinfo_proc *pp;
926 const struct kinfo_proc *oldp;
933 struct rusage ru, *rup;
937 char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1];
940 const int cmdlen = 128;
942 /* find and remember the next proc structure */
943 hp = (struct handle *)xhandle;
944 pp = *(hp->next_proc++);
947 /* get the process's command name */
948 if ((pp->ki_flag & P_INMEM) == 0) {
950 * Print swapped processes as <pname>
954 len = strlen(pp->ki_comm);
955 if (len > sizeof(pp->ki_comm) - 3)
956 len = sizeof(pp->ki_comm) - 3;
957 memmove(pp->ki_comm + 1, pp->ki_comm, len);
958 pp->ki_comm[0] = '<';
959 pp->ki_comm[len + 1] = '>';
960 pp->ki_comm[len + 2] = '\0';
964 * Convert the process's runtime from microseconds to seconds. This
965 * time includes the interrupt time although that is not wanted here.
966 * ps(1) is similarly sloppy.
968 cputime = (pp->ki_runtime + 500000) / 1000000;
970 /* calculate the base for cpu percentages */
973 /* generate "STATE" field */
974 switch (state = pp->ki_stat) {
976 if (smpmode && pp->ki_oncpu != NOCPU)
977 sprintf(status, "CPU%d", pp->ki_oncpu);
979 strcpy(status, "RUN");
982 if (pp->ki_kiflag & KI_LOCKBLOCK) {
983 sprintf(status, "*%.6s", pp->ki_lockname);
988 sprintf(status, "%.6s", pp->ki_wmesg);
992 if (state < sizeof(state_abbrev) / sizeof(*state_abbrev))
993 sprintf(status, "%.6s", state_abbrev[state]);
995 sprintf(status, "?%5zu", state);
999 cmdbuf = calloc(cmdlen + 1, 1);
1000 if (cmdbuf == NULL) {
1001 warn("calloc(%d)", cmdlen + 1);
1005 if (!(flags & FMT_SHOWARGS)) {
1006 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1008 snprintf(cmdbuf, cmdlen, "%s{%s%s}", pp->ki_comm,
1009 pp->ki_tdname, pp->ki_moretdname);
1011 snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm);
1014 if (pp->ki_flag & P_SYSTEM ||
1015 pp->ki_args == NULL ||
1016 (args = kvm_getargv(kd, pp, cmdlen)) == NULL ||
1018 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1020 snprintf(cmdbuf, cmdlen,
1021 "[%s{%s%s}]", pp->ki_comm, pp->ki_tdname,
1024 snprintf(cmdbuf, cmdlen,
1025 "[%s]", pp->ki_comm);
1028 char *src, *dst, *argbuf;
1033 argbuflen = cmdlen * 4;
1034 argbuf = calloc(argbuflen + 1, 1);
1035 if (argbuf == NULL) {
1036 warn("calloc(%zu)", argbuflen + 1);
1043 /* Extract cmd name from argv */
1044 cmd = strrchr(*args, '/');
1050 for (; (src = *args++) != NULL; ) {
1053 len = (argbuflen - (dst - argbuf) - 1) / 4;
1055 MIN(strlen(src), len),
1056 VIS_NL | VIS_CSTYLE);
1057 while (*dst != '\0')
1059 if ((argbuflen - (dst - argbuf) - 1) / 4 > 0)
1060 *dst++ = ' '; /* add delimiting space */
1062 if (dst != argbuf && dst[-1] == ' ')
1066 if (strcmp(cmd, pp->ki_comm) != 0) {
1067 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1069 snprintf(cmdbuf, cmdlen,
1070 "%s (%s){%s%s}", argbuf,
1071 pp->ki_comm, pp->ki_tdname,
1074 snprintf(cmdbuf, cmdlen,
1075 "%s (%s)", argbuf, pp->ki_comm);
1077 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1079 snprintf(cmdbuf, cmdlen,
1080 "%s{%s%s}", argbuf, pp->ki_tdname,
1083 strlcpy(cmdbuf, argbuf, cmdlen);
1092 snprintf(jid_buf, sizeof(jid_buf), "%*d",
1093 jidlength - 1, pp->ki_jid);
1098 snprintf(swap_buf, sizeof(swap_buf), "%*s",
1100 format_k2(pagetok(ki_swap(pp)))); /* XXX */
1102 if (displaymode == DISP_IO) {
1103 oldp = get_old_proc(pp);
1105 ru.ru_inblock = RU(pp)->ru_inblock -
1106 RU(oldp)->ru_inblock;
1107 ru.ru_oublock = RU(pp)->ru_oublock -
1108 RU(oldp)->ru_oublock;
1109 ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
1110 ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
1111 ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
1116 p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt;
1117 s_tot = total_inblock + total_oublock + total_majflt;
1119 snprintf(fmt, sizeof(fmt), io_Proc_format,
1122 namelength, namelength, (*get_userid)(pp->ki_ruid),
1129 s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot),
1130 screen_width > cmdlengthdelta ?
1131 screen_width - cmdlengthdelta : 0,
1139 /* format this entry */
1141 if (state == SRUN && pp->ki_oncpu != NOCPU)
1144 cpu = pp->ki_lastcpu;
1147 proc_fmt = smpmode ? smp_Proc_format : up_Proc_format;
1151 snprintf(thr_buf, sizeof(thr_buf), "%*d ",
1152 (int)(sizeof(thr_buf) - 2), pp->ki_numthreads);
1154 snprintf(fmt, sizeof(fmt), proc_fmt,
1155 (ps.thread) ? pp->ki_tid : pp->ki_pid,
1157 namelength, namelength, (*get_userid)(pp->ki_ruid),
1159 pp->ki_pri.pri_level - PZERO,
1161 format_k2(PROCSIZE(pp)),
1162 format_k2(pagetok(pp->ki_rssize)),
1163 swaplength, swaplength, swap_buf,
1166 format_time(cputime),
1167 ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct,
1168 screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0,
1173 /* return the result */
1178 getsysctl(const char *name, void *ptr, size_t len)
1182 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
1183 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name,
1185 quit(TOP_EX_SYS_ERROR);
1188 fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n",
1189 name, (unsigned long)len, (unsigned long)nlen);
1190 quit(TOP_EX_SYS_ERROR);
1195 format_nice(const struct kinfo_proc *pp)
1197 const char *fifo, *kproc;
1199 static char nicebuf[4 + 1];
1201 fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F";
1202 kproc = (pp->ki_flag & P_KPROC) ? "k" : "";
1203 switch (PRI_BASE(pp->ki_pri.pri_class)) {
1208 * XXX: the kernel doesn't tell us the original rtprio and
1209 * doesn't really know what it was, so to recover it we
1210 * must be more chummy with the implementation than the
1211 * implementation is with itself. pri_user gives a
1212 * constant "base" priority, but is only initialized
1213 * properly for user threads. pri_native gives what the
1214 * kernel calls the "base" priority, but it isn't constant
1215 * since it is changed by priority propagation. pri_native
1216 * also isn't properly initialized for all threads, but it
1217 * is properly initialized for kernel realtime and idletime
1218 * threads. Thus we use pri_user for the base priority of
1219 * user threads (it is always correct) and pri_native for
1220 * the base priority of kernel realtime and idletime threads
1221 * (there is nothing better, and it is usually correct).
1223 * The field width and thus the buffer are too small for
1224 * values like "kr31F", but such values shouldn't occur,
1225 * and if they do then the tailing "F" is not displayed.
1227 rtpri = ((pp->ki_flag & P_KPROC) ? pp->ki_pri.pri_native :
1228 pp->ki_pri.pri_user) - PRI_MIN_REALTIME;
1229 snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s",
1230 kproc, rtpri, fifo);
1233 if (pp->ki_flag & P_KPROC)
1235 snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO);
1238 /* XXX: as above. */
1239 rtpri = ((pp->ki_flag & P_KPROC) ? pp->ki_pri.pri_native :
1240 pp->ki_pri.pri_user) - PRI_MIN_IDLE;
1241 snprintf(nicebuf, sizeof(nicebuf), "%si%d%s",
1242 kproc, rtpri, fifo);
1250 /* comparison routines for qsort */
1253 compare_pid(const void *p1, const void *p2)
1255 const struct kinfo_proc * const *pp1 = p1;
1256 const struct kinfo_proc * const *pp2 = p2;
1258 if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0)
1261 return ((*pp1)->ki_pid - (*pp2)->ki_pid);
1265 compare_tid(const void *p1, const void *p2)
1267 const struct kinfo_proc * const *pp1 = p1;
1268 const struct kinfo_proc * const *pp2 = p2;
1270 if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0)
1273 return ((*pp1)->ki_tid - (*pp2)->ki_tid);
1277 * proc_compare - comparison function for "qsort"
1278 * Compares the resource consumption of two processes using five
1279 * distinct keys. The keys (in descending order of importance) are:
1280 * percent cpu, cpu ticks, state, resident set size, total virtual
1281 * memory usage. The process states are ordered as follows (from least
1282 * to most important): WAIT, zombie, sleep, stop, start, run. The
1283 * array declaration below maps a process state index into a number
1284 * that reflects this ordering.
1287 static int sorted_state[] = {
1290 1, /* ABANDONED (WAIT) */
1298 #define ORDERKEY_PCTCPU(a, b) do { \
1301 diff = weighted_cpu(PCTCPU((b)), (b)) - \
1302 weighted_cpu(PCTCPU((a)), (a)); \
1304 diff = PCTCPU((b)) - PCTCPU((a)); \
1306 return (diff > 0 ? 1 : -1); \
1309 #define ORDERKEY_CPTICKS(a, b) do { \
1310 int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \
1312 return (diff > 0 ? 1 : -1); \
1315 #define ORDERKEY_STATE(a, b) do { \
1316 int diff = sorted_state[(unsigned char)(b)->ki_stat] - sorted_state[(unsigned char)(a)->ki_stat]; \
1318 return (diff > 0 ? 1 : -1); \
1321 #define ORDERKEY_PRIO(a, b) do { \
1322 int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \
1324 return (diff > 0 ? 1 : -1); \
1327 #define ORDERKEY_THREADS(a, b) do { \
1328 int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \
1330 return (diff > 0 ? 1 : -1); \
1333 #define ORDERKEY_RSSIZE(a, b) do { \
1334 long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \
1336 return (diff > 0 ? 1 : -1); \
1339 #define ORDERKEY_MEM(a, b) do { \
1340 long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \
1342 return (diff > 0 ? 1 : -1); \
1345 #define ORDERKEY_JID(a, b) do { \
1346 int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \
1348 return (diff > 0 ? 1 : -1); \
1351 #define ORDERKEY_SWAP(a, b) do { \
1352 int diff = (int)ki_swap(b) - (int)ki_swap(a); \
1354 return (diff > 0 ? 1 : -1); \
1357 /* compare_cpu - the comparison function for sorting by cpu percentage */
1360 compare_cpu(const void *arg1, const void *arg2)
1362 const struct kinfo_proc *p1 = *(const struct kinfo_proc * const *)arg1;
1363 const struct kinfo_proc *p2 = *(const struct kinfo_proc * const *)arg2;
1365 ORDERKEY_PCTCPU(p1, p2);
1366 ORDERKEY_CPTICKS(p1, p2);
1367 ORDERKEY_STATE(p1, p2);
1368 ORDERKEY_PRIO(p1, p2);
1369 ORDERKEY_RSSIZE(p1, p2);
1370 ORDERKEY_MEM(p1, p2);
1375 /* "cpu" compare routines */
1376 static int compare_size(const void *arg1, const void *arg2);
1377 static int compare_res(const void *arg1, const void *arg2);
1378 static int compare_time(const void *arg1, const void *arg2);
1379 static int compare_prio(const void *arg1, const void *arg2);
1380 static int compare_threads(const void *arg1, const void *arg2);
1383 * "io" compare routines. Context switches aren't i/o, but are displayed
1384 * on the "io" display.
1386 static int compare_iototal(const void *arg1, const void *arg2);
1387 static int compare_ioread(const void *arg1, const void *arg2);
1388 static int compare_iowrite(const void *arg1, const void *arg2);
1389 static int compare_iofault(const void *arg1, const void *arg2);
1390 static int compare_vcsw(const void *arg1, const void *arg2);
1391 static int compare_ivcsw(const void *arg1, const void *arg2);
1393 int (*compares[])(const void *arg1, const void *arg2) = {
1411 /* compare_size - the comparison function for sorting by total memory usage */
1414 compare_size(const void *arg1, const void *arg2)
1416 const struct kinfo_proc *p1 = *(const struct kinfo_proc * const *)arg1;
1417 const struct kinfo_proc *p2 = *(const struct kinfo_proc * const *)arg2;
1419 ORDERKEY_MEM(p1, p2);
1420 ORDERKEY_RSSIZE(p1, p2);
1421 ORDERKEY_PCTCPU(p1, p2);
1422 ORDERKEY_CPTICKS(p1, p2);
1423 ORDERKEY_STATE(p1, p2);
1424 ORDERKEY_PRIO(p1, p2);
1429 /* compare_res - the comparison function for sorting by resident set size */
1432 compare_res(const void *arg1, const void *arg2)
1434 const struct kinfo_proc *p1 = *(const struct kinfo_proc * const *)arg1;
1435 const struct kinfo_proc *p2 = *(const struct kinfo_proc * const *)arg2;
1437 ORDERKEY_RSSIZE(p1, p2);
1438 ORDERKEY_MEM(p1, p2);
1439 ORDERKEY_PCTCPU(p1, p2);
1440 ORDERKEY_CPTICKS(p1, p2);
1441 ORDERKEY_STATE(p1, p2);
1442 ORDERKEY_PRIO(p1, p2);
1447 /* compare_time - the comparison function for sorting by total cpu time */
1450 compare_time(const void *arg1, const void *arg2)
1452 const struct kinfo_proc *p1 = *(const struct kinfo_proc * const *)arg1;
1453 const struct kinfo_proc *p2 = *(const struct kinfo_proc * const *) arg2;
1455 ORDERKEY_CPTICKS(p1, p2);
1456 ORDERKEY_PCTCPU(p1, p2);
1457 ORDERKEY_STATE(p1, p2);
1458 ORDERKEY_PRIO(p1, p2);
1459 ORDERKEY_RSSIZE(p1, p2);
1460 ORDERKEY_MEM(p1, p2);
1465 /* compare_prio - the comparison function for sorting by priority */
1468 compare_prio(const void *arg1, const void *arg2)
1470 const struct kinfo_proc *p1 = *(const struct kinfo_proc * const *)arg1;
1471 const struct kinfo_proc *p2 = *(const struct kinfo_proc * const *)arg2;
1473 ORDERKEY_PRIO(p1, p2);
1474 ORDERKEY_CPTICKS(p1, p2);
1475 ORDERKEY_PCTCPU(p1, p2);
1476 ORDERKEY_STATE(p1, p2);
1477 ORDERKEY_RSSIZE(p1, p2);
1478 ORDERKEY_MEM(p1, p2);
1483 /* compare_threads - the comparison function for sorting by threads */
1485 compare_threads(const void *arg1, const void *arg2)
1487 const struct kinfo_proc *p1 = *(const struct kinfo_proc * const *)arg1;
1488 const struct kinfo_proc *p2 = *(const struct kinfo_proc * const *)arg2;
1490 ORDERKEY_THREADS(p1, p2);
1491 ORDERKEY_PCTCPU(p1, p2);
1492 ORDERKEY_CPTICKS(p1, p2);
1493 ORDERKEY_STATE(p1, p2);
1494 ORDERKEY_PRIO(p1, p2);
1495 ORDERKEY_RSSIZE(p1, p2);
1496 ORDERKEY_MEM(p1, p2);
1501 /* compare_jid - the comparison function for sorting by jid */
1503 compare_jid(const void *arg1, const void *arg2)
1505 const struct kinfo_proc *p1 = *(const struct kinfo_proc * const *)arg1;
1506 const struct kinfo_proc *p2 = *(const struct kinfo_proc * const *)arg2;
1508 ORDERKEY_JID(p1, p2);
1509 ORDERKEY_PCTCPU(p1, p2);
1510 ORDERKEY_CPTICKS(p1, p2);
1511 ORDERKEY_STATE(p1, p2);
1512 ORDERKEY_PRIO(p1, p2);
1513 ORDERKEY_RSSIZE(p1, p2);
1514 ORDERKEY_MEM(p1, p2);
1519 /* compare_swap - the comparison function for sorting by swap */
1521 compare_swap(const void *arg1, const void *arg2)
1523 const struct kinfo_proc *p1 = *(const struct kinfo_proc **)arg1;
1524 const struct kinfo_proc *p2 = *(const struct kinfo_proc **)arg2;
1526 ORDERKEY_SWAP(p1, p2);
1527 ORDERKEY_PCTCPU(p1, p2);
1528 ORDERKEY_CPTICKS(p1, p2);
1529 ORDERKEY_STATE(p1, p2);
1530 ORDERKEY_PRIO(p1, p2);
1531 ORDERKEY_RSSIZE(p1, p2);
1532 ORDERKEY_MEM(p1, p2);
1537 /* assorted comparison functions for sorting by i/o */
1540 compare_iototal(const void *arg1, const void *arg2)
1542 struct kinfo_proc * const p1 = *(struct kinfo_proc **)arg1;
1543 struct kinfo_proc * const p2 = *(struct kinfo_proc **)arg2;
1545 return (get_io_total(p2) - get_io_total(p1));
1549 compare_ioread(const void *arg1, const void *arg2)
1551 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1552 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1553 long dummy, inp1, inp2;
1555 (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy);
1556 (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy);
1558 return (inp2 - inp1);
1562 compare_iowrite(const void *arg1, const void *arg2)
1564 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1565 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1566 long dummy, oup1, oup2;
1568 (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy);
1569 (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy);
1571 return (oup2 - oup1);
1575 compare_iofault(const void *arg1, const void *arg2)
1577 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1578 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1579 long dummy, flp1, flp2;
1581 (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy);
1582 (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy);
1584 return (flp2 - flp1);
1588 compare_vcsw(const void *arg1, const void *arg2)
1590 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1591 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1592 long dummy, flp1, flp2;
1594 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy);
1595 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy);
1597 return (flp2 - flp1);
1601 compare_ivcsw(const void *arg1, const void *arg2)
1603 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1604 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1605 long dummy, flp1, flp2;
1607 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1);
1608 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2);
1610 return (flp2 - flp1);
1614 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
1615 * the process does not exist.
1616 * It is EXTREMELY IMPORTANT that this function work correctly.
1617 * If top runs setuid root (as in SVR4), then this function
1618 * is the only thing that stands in the way of a serious
1619 * security problem. It validates requests for the "kill"
1620 * and "renice" commands.
1627 struct kinfo_proc **prefp;
1628 struct kinfo_proc *pp;
1632 while (--cnt >= 0) {
1634 if (pp->ki_pid == (pid_t)pid)
1635 return ((int)pp->ki_ruid);
1641 swapmode(int *retavail, int *retfree)
1644 struct kvm_swap swapary[1];
1645 static int pagesize = 0;
1646 static u_long swap_maxpages = 0;
1651 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
1653 n = kvm_getswapinfo(kd, swapary, 1, 0);
1654 if (n < 0 || swapary[0].ksw_total == 0)
1658 pagesize = getpagesize();
1659 if (swap_maxpages == 0)
1660 GETSYSCTL("vm.swap_maxpages", swap_maxpages);
1662 /* ksw_total contains the total size of swap all devices which may
1663 exceed the maximum swap size allocatable in the system */
1664 if ( swapary[0].ksw_total > swap_maxpages )
1665 swapary[0].ksw_total = swap_maxpages;
1667 *retavail = CONVERT(swapary[0].ksw_total);
1668 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
1670 n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total);