]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/top/machine.c
Fix double-dollar typo in lld Makefile.
[FreeBSD/FreeBSD.git] / usr.bin / top / machine.c
1 /*
2  * top - a top users display for Unix
3  *
4  * SYNOPSIS:  For FreeBSD-2.x and later
5  *
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/)
11  *
12  * This is the machine-dependent module for FreeBSD 2.2
13  * Works for:
14  *      FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x
15  *
16  * LIBS: -lkvm
17  *
18  * AUTHOR:  Christos Zoulas <christos@ee.cornell.edu>
19  *          Steven Wallace  <swallace@freebsd.org>
20  *          Wolfram Schneider <wosch@FreeBSD.org>
21  *          Thomas Moestl <tmoestl@gmx.net>
22  *
23  * $FreeBSD$
24  */
25
26 #include <sys/param.h>
27 #include <sys/errno.h>
28 #include <sys/file.h>
29 #include <sys/proc.h>
30 #include <sys/resource.h>
31 #include <sys/rtprio.h>
32 #include <sys/signal.h>
33 #include <sys/sysctl.h>
34 #include <sys/time.h>
35 #include <sys/user.h>
36 #include <sys/vmmeter.h>
37
38 #include <err.h>
39 #include <kvm.h>
40 #include <math.h>
41 #include <nlist.h>
42 #include <paths.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <unistd.h>
49 #include <vis.h>
50
51 #include "top.h"
52 #include "machine.h"
53 #include "screen.h"
54 #include "utils.h"
55 #include "layout.h"
56
57 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
58 #define SMPUNAMELEN     13
59 #define UPUNAMELEN      15
60
61 extern struct process_select ps;
62 extern char* printable(char *);
63 static int smpmode;
64 enum displaymodes displaymode;
65 #ifdef TOP_USERNAME_LEN
66 static int namelength = TOP_USERNAME_LEN;
67 #else
68 static int namelength = 8;
69 #endif
70 /* TOP_JID_LEN based on max of 999999 */
71 #define TOP_JID_LEN 7
72 #define TOP_SWAP_LEN 6
73 static int jidlength;
74 static int swaplength;
75 static int cmdlengthdelta;
76
77 /* Prototypes for top internals */
78 void quit(int);
79
80 /* get_process_info passes back a handle.  This is what it looks like: */
81
82 struct handle {
83         struct kinfo_proc **next_proc;  /* points to next valid proc pointer */
84         int remaining;                  /* number of pointers remaining */
85 };
86
87 /* declarations for load_avg */
88 #include "loadavg.h"
89
90 /* define what weighted cpu is.  */
91 #define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \
92                          ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu))))
93
94 /* what we consider to be process size: */
95 #define PROCSIZE(pp) ((pp)->ki_size / 1024)
96
97 #define RU(pp)  (&(pp)->ki_rusage)
98 #define RUTOT(pp) \
99         (RU(pp)->ru_inblock + RU(pp)->ru_oublock + RU(pp)->ru_majflt)
100
101 #define PCTCPU(pp) (pcpu[pp - pbase])
102
103 /* definitions for indices in the nlist array */
104
105 /*
106  *  These definitions control the format of the per-process area
107  */
108
109 static char io_header[] =
110     "  PID%*s %-*.*s   VCSW  IVCSW   READ  WRITE  FAULT  TOTAL PERCENT COMMAND";
111
112 #define io_Proc_format \
113     "%5d%*s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s"
114
115 static char smp_header_thr[] =
116     "  PID%*s %-*.*s  THR PRI NICE   SIZE    RES%*s STATE   C   TIME %7s COMMAND";
117 static char smp_header[] =
118     "  PID%*s %-*.*s "   "PRI NICE   SIZE    RES%*s STATE   C   TIME %7s COMMAND";
119
120 #define smp_Proc_format \
121     "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s %2d%7s %6.2f%% %.*s"
122
123 static char up_header_thr[] =
124     "  PID%*s %-*.*s  THR PRI NICE   SIZE    RES%*s STATE    TIME %7s COMMAND";
125 static char up_header[] =
126     "  PID%*s %-*.*s "   "PRI NICE   SIZE    RES%*s STATE    TIME %7s COMMAND";
127
128 #define up_Proc_format \
129     "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s%.0d%7s %6.2f%% %.*s"
130
131
132 /* process state names for the "STATE" column of the display */
133 /* the extra nulls in the string "run" are for adding a slash and
134    the processor number when needed */
135
136 char *state_abbrev[] = {
137         "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK"
138 };
139
140
141 static kvm_t *kd;
142
143 /* values that we stash away in _init and use in later routines */
144
145 static double logcpu;
146
147 /* these are retrieved from the kernel in _init */
148
149 static load_avg  ccpu;
150
151 /* these are used in the get_ functions */
152
153 static int lastpid;
154
155 /* these are for calculating cpu state percentages */
156
157 static long cp_time[CPUSTATES];
158 static long cp_old[CPUSTATES];
159 static long cp_diff[CPUSTATES];
160
161 /* these are for detailing the process states */
162
163 int process_states[8];
164 char *procstatenames[] = {
165         "", " starting, ", " running, ", " sleeping, ", " stopped, ",
166         " zombie, ", " waiting, ", " lock, ",
167         NULL
168 };
169
170 /* these are for detailing the cpu states */
171
172 int cpu_states[CPUSTATES];
173 char *cpustatenames[] = {
174         "user", "nice", "system", "interrupt", "idle", NULL
175 };
176
177 /* these are for detailing the memory statistics */
178
179 int memory_stats[7];
180 char *memorynames[] = {
181         "K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ",
182         "K Free", NULL
183 };
184
185 int arc_stats[7];
186 char *arcnames[] = {
187         "K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other",
188         NULL
189 };
190
191 int swap_stats[7];
192 char *swapnames[] = {
193         "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
194         NULL
195 };
196
197
198 /* these are for keeping track of the proc array */
199
200 static int nproc;
201 static int onproc = -1;
202 static int pref_len;
203 static struct kinfo_proc *pbase;
204 static struct kinfo_proc **pref;
205 static struct kinfo_proc *previous_procs;
206 static struct kinfo_proc **previous_pref;
207 static int previous_proc_count = 0;
208 static int previous_proc_count_max = 0;
209 static int previous_thread;
210
211 /* data used for recalculating pctcpu */
212 static double *pcpu;
213 static struct timespec proc_uptime;
214 static struct timeval proc_wall_time;
215 static struct timeval previous_wall_time;
216 static uint64_t previous_interval = 0;
217
218 /* total number of io operations */
219 static long total_inblock;
220 static long total_oublock;
221 static long total_majflt;
222
223 /* these are for getting the memory statistics */
224
225 static int arc_enabled;
226 static int pageshift;           /* log base 2 of the pagesize */
227
228 /* define pagetok in terms of pageshift */
229
230 #define pagetok(size) ((size) << pageshift)
231
232 /* swap usage */
233 #define ki_swap(kip) \
234     ((kip)->ki_swrss > (kip)->ki_rssize ? (kip)->ki_swrss - (kip)->ki_rssize : 0)
235
236 /* useful externals */
237 long percentages();
238
239 #ifdef ORDER
240 /*
241  * Sorting orders.  The first element is the default.
242  */
243 char *ordernames[] = {
244         "cpu", "size", "res", "time", "pri", "threads",
245         "total", "read", "write", "fault", "vcsw", "ivcsw",
246         "jid", "swap", "pid", NULL
247 };
248 #endif
249
250 /* Per-cpu time states */
251 static int maxcpu;
252 static int maxid;
253 static int ncpus;
254 static u_long cpumask;
255 static long *times;
256 static long *pcpu_cp_time;
257 static long *pcpu_cp_old;
258 static long *pcpu_cp_diff;
259 static int *pcpu_cpu_states;
260
261 static int compare_swap(const void *a, const void *b);
262 static int compare_jid(const void *a, const void *b);
263 static int compare_pid(const void *a, const void *b);
264 static int compare_tid(const void *a, const void *b);
265 static const char *format_nice(const struct kinfo_proc *pp);
266 static void getsysctl(const char *name, void *ptr, size_t len);
267 static int swapmode(int *retavail, int *retfree);
268 static void update_layout(void);
269
270 void
271 toggle_pcpustats(void)
272 {
273
274         if (ncpus == 1)
275                 return;
276         update_layout();
277 }
278
279 /* Adjust display based on ncpus and the ARC state. */
280 static void
281 update_layout(void)
282 {
283
284         y_mem = 3;
285         y_arc = 4;
286         y_swap = 4 + arc_enabled;
287         y_idlecursor = 5 + arc_enabled;
288         y_message = 5 + arc_enabled;
289         y_header = 6 + arc_enabled;
290         y_procs = 7 + arc_enabled;
291         Header_lines = 7 + arc_enabled;
292
293         if (pcpu_stats) {
294                 y_mem += ncpus - 1;
295                 y_arc += ncpus - 1;
296                 y_swap += ncpus - 1;
297                 y_idlecursor += ncpus - 1;
298                 y_message += ncpus - 1;
299                 y_header += ncpus - 1;
300                 y_procs += ncpus - 1;
301                 Header_lines += ncpus - 1;
302         }
303 }
304
305 int
306 machine_init(struct statics *statics, char do_unames)
307 {
308         int i, j, empty, pagesize;
309         uint64_t arc_size;
310         size_t size;
311         struct passwd *pw;
312
313         size = sizeof(smpmode);
314         if ((sysctlbyname("machdep.smp_active", &smpmode, &size,
315             NULL, 0) != 0 &&
316             sysctlbyname("kern.smp.active", &smpmode, &size,
317             NULL, 0) != 0) ||
318             size != sizeof(smpmode))
319                 smpmode = 0;
320
321         size = sizeof(arc_size);
322         if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc_size, &size,
323             NULL, 0) == 0 && arc_size != 0)
324                 arc_enabled = 1;
325
326         if (do_unames) {
327             while ((pw = getpwent()) != NULL) {
328                 if (strlen(pw->pw_name) > namelength)
329                         namelength = strlen(pw->pw_name);
330             }
331         }
332         if (smpmode && namelength > SMPUNAMELEN)
333                 namelength = SMPUNAMELEN;
334         else if (namelength > UPUNAMELEN)
335                 namelength = UPUNAMELEN;
336
337         kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
338         if (kd == NULL)
339                 return (-1);
340
341         GETSYSCTL("kern.ccpu", ccpu);
342
343         /* this is used in calculating WCPU -- calculate it ahead of time */
344         logcpu = log(loaddouble(ccpu));
345
346         pbase = NULL;
347         pref = NULL;
348         pcpu = NULL;
349         nproc = 0;
350         onproc = -1;
351
352         /* get the page size and calculate pageshift from it */
353         pagesize = getpagesize();
354         pageshift = 0;
355         while (pagesize > 1) {
356                 pageshift++;
357                 pagesize >>= 1;
358         }
359
360         /* we only need the amount of log(2)1024 for our conversion */
361         pageshift -= LOG1024;
362
363         /* fill in the statics information */
364         statics->procstate_names = procstatenames;
365         statics->cpustate_names = cpustatenames;
366         statics->memory_names = memorynames;
367         if (arc_enabled)
368                 statics->arc_names = arcnames;
369         else
370                 statics->arc_names = NULL;
371         statics->swap_names = swapnames;
372 #ifdef ORDER
373         statics->order_names = ordernames;
374 #endif
375
376         /* Allocate state for per-CPU stats. */
377         cpumask = 0;
378         ncpus = 0;
379         GETSYSCTL("kern.smp.maxcpus", maxcpu);
380         size = sizeof(long) * maxcpu * CPUSTATES;
381         times = malloc(size);
382         if (times == NULL)
383                 err(1, "malloc %zu bytes", size);
384         if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1)
385                 err(1, "sysctlbyname kern.cp_times");
386         pcpu_cp_time = calloc(1, size);
387         maxid = (size / CPUSTATES / sizeof(long)) - 1;
388         for (i = 0; i <= maxid; i++) {
389                 empty = 1;
390                 for (j = 0; empty && j < CPUSTATES; j++) {
391                         if (times[i * CPUSTATES + j] != 0)
392                                 empty = 0;
393                 }
394                 if (!empty) {
395                         cpumask |= (1ul << i);
396                         ncpus++;
397                 }
398         }
399         size = sizeof(long) * ncpus * CPUSTATES;
400         pcpu_cp_old = calloc(1, size);
401         pcpu_cp_diff = calloc(1, size);
402         pcpu_cpu_states = calloc(1, size);
403         statics->ncpus = ncpus;
404
405         update_layout();
406
407         /* all done! */
408         return (0);
409 }
410
411 char *
412 format_header(char *uname_field)
413 {
414         static char Header[128];
415         const char *prehead;
416         
417         if (ps.jail)
418                 jidlength = TOP_JID_LEN + 1;    /* +1 for extra left space. */
419         else
420                 jidlength = 0;
421
422         if (ps.swap)
423                 swaplength = TOP_SWAP_LEN + 1;  /* +1 for extra left space */
424         else
425                 swaplength = 0;
426
427         switch (displaymode) {
428         case DISP_CPU:
429                 /*
430                  * The logic of picking the right header format seems reverse
431                  * here because we only want to display a THR column when
432                  * "thread mode" is off (and threads are not listed as
433                  * separate lines).
434                  */
435                 prehead = smpmode ?
436                     (ps.thread ? smp_header : smp_header_thr) :
437                     (ps.thread ? up_header : up_header_thr);
438                 snprintf(Header, sizeof(Header), prehead,
439                     jidlength, ps.jail ? " JID" : "",
440                     namelength, namelength, uname_field,
441                     swaplength, ps.swap ? " SWAP" : "",
442                     ps.wcpu ? "WCPU" : "CPU");
443                 break;
444         case DISP_IO:
445                 prehead = io_header;
446                 snprintf(Header, sizeof(Header), prehead,
447                     jidlength, ps.jail ? " JID" : "",
448                     namelength, namelength, uname_field);
449                 break;
450         }
451         cmdlengthdelta = strlen(Header) - 7;
452         return (Header);
453 }
454
455 static int swappgsin = -1;
456 static int swappgsout = -1;
457 extern struct timeval timeout;
458
459
460 void
461 get_system_info(struct system_info *si)
462 {
463         long total;
464         struct loadavg sysload;
465         int mib[2];
466         struct timeval boottime;
467         uint64_t arc_stat, arc_stat2;
468         int i, j;
469         size_t size;
470
471         /* get the CPU stats */
472         size = (maxid + 1) * CPUSTATES * sizeof(long);
473         if (sysctlbyname("kern.cp_times", pcpu_cp_time, &size, NULL, 0) == -1)
474                 err(1, "sysctlbyname kern.cp_times");
475         GETSYSCTL("kern.cp_time", cp_time);
476         GETSYSCTL("vm.loadavg", sysload);
477         GETSYSCTL("kern.lastpid", lastpid);
478
479         /* convert load averages to doubles */
480         for (i = 0; i < 3; i++)
481                 si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale;
482
483         /* convert cp_time counts to percentages */
484         for (i = j = 0; i <= maxid; i++) {
485                 if ((cpumask & (1ul << i)) == 0)
486                         continue;
487                 percentages(CPUSTATES, &pcpu_cpu_states[j * CPUSTATES],
488                     &pcpu_cp_time[j * CPUSTATES],
489                     &pcpu_cp_old[j * CPUSTATES],
490                     &pcpu_cp_diff[j * CPUSTATES]);
491                 j++;
492         }
493         percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
494
495         /* sum memory & swap statistics */
496         {
497                 static unsigned int swap_delay = 0;
498                 static int swapavail = 0;
499                 static int swapfree = 0;
500                 static long bufspace = 0;
501                 static int nspgsin, nspgsout;
502
503                 GETSYSCTL("vfs.bufspace", bufspace);
504                 GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]);
505                 GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]);
506                 GETSYSCTL("vm.stats.vm.v_laundry_count", memory_stats[2]);
507                 GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[3]);
508                 GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]);
509                 GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin);
510                 GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout);
511                 /* convert memory stats to Kbytes */
512                 memory_stats[0] = pagetok(memory_stats[0]);
513                 memory_stats[1] = pagetok(memory_stats[1]);
514                 memory_stats[2] = pagetok(memory_stats[2]);
515                 memory_stats[3] = pagetok(memory_stats[3]);
516                 memory_stats[4] = bufspace / 1024;
517                 memory_stats[5] = pagetok(memory_stats[5]);
518                 memory_stats[6] = -1;
519
520                 /* first interval */
521                 if (swappgsin < 0) {
522                         swap_stats[4] = 0;
523                         swap_stats[5] = 0;
524                 }
525
526                 /* compute differences between old and new swap statistic */
527                 else {
528                         swap_stats[4] = pagetok(((nspgsin - swappgsin)));
529                         swap_stats[5] = pagetok(((nspgsout - swappgsout)));
530                 }
531
532                 swappgsin = nspgsin;
533                 swappgsout = nspgsout;
534
535                 /* call CPU heavy swapmode() only for changes */
536                 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
537                         swap_stats[3] = swapmode(&swapavail, &swapfree);
538                         swap_stats[0] = swapavail;
539                         swap_stats[1] = swapavail - swapfree;
540                         swap_stats[2] = swapfree;
541                 }
542                 swap_delay = 1;
543                 swap_stats[6] = -1;
544         }
545
546         if (arc_enabled) {
547                 GETSYSCTL("kstat.zfs.misc.arcstats.size", arc_stat);
548                 arc_stats[0] = arc_stat >> 10;
549                 GETSYSCTL("vfs.zfs.mfu_size", arc_stat);
550                 arc_stats[1] = arc_stat >> 10;
551                 GETSYSCTL("vfs.zfs.mru_size", arc_stat);
552                 arc_stats[2] = arc_stat >> 10;
553                 GETSYSCTL("vfs.zfs.anon_size", arc_stat);
554                 arc_stats[3] = arc_stat >> 10;
555                 GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc_stat);
556                 GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc_stat2);
557                 arc_stats[4] = arc_stat + arc_stat2 >> 10;
558                 GETSYSCTL("kstat.zfs.misc.arcstats.other_size", arc_stat);
559                 arc_stats[5] = arc_stat >> 10;
560                 si->arc = arc_stats;
561         }
562                     
563         /* set arrays and strings */
564         if (pcpu_stats) {
565                 si->cpustates = pcpu_cpu_states;
566                 si->ncpus = ncpus;
567         } else {
568                 si->cpustates = cpu_states;
569                 si->ncpus = 1;
570         }
571         si->memory = memory_stats;
572         si->swap = swap_stats;
573
574
575         if (lastpid > 0) {
576                 si->last_pid = lastpid;
577         } else {
578                 si->last_pid = -1;
579         }
580
581         /*
582          * Print how long system has been up.
583          * (Found by looking getting "boottime" from the kernel)
584          */
585         mib[0] = CTL_KERN;
586         mib[1] = KERN_BOOTTIME;
587         size = sizeof(boottime);
588         if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
589             boottime.tv_sec != 0) {
590                 si->boottime = boottime;
591         } else {
592                 si->boottime.tv_sec = -1;
593         }
594 }
595
596 #define NOPROC  ((void *)-1)
597
598 /*
599  * We need to compare data from the old process entry with the new
600  * process entry.
601  * To facilitate doing this quickly we stash a pointer in the kinfo_proc
602  * structure to cache the mapping.  We also use a negative cache pointer
603  * of NOPROC to avoid duplicate lookups.
604  * XXX: this could be done when the actual processes are fetched, we do
605  * it here out of laziness.
606  */
607 const struct kinfo_proc *
608 get_old_proc(struct kinfo_proc *pp)
609 {
610         struct kinfo_proc **oldpp, *oldp;
611
612         /*
613          * If this is the first fetch of the kinfo_procs then we don't have
614          * any previous entries.
615          */
616         if (previous_proc_count == 0)
617                 return (NULL);
618         /* negative cache? */
619         if (pp->ki_udata == NOPROC)
620                 return (NULL);
621         /* cached? */
622         if (pp->ki_udata != NULL)
623                 return (pp->ki_udata);
624         /*
625          * Not cached,
626          * 1) look up based on pid.
627          * 2) compare process start.
628          * If we fail here, then setup a negative cache entry, otherwise
629          * cache it.
630          */
631         oldpp = bsearch(&pp, previous_pref, previous_proc_count,
632             sizeof(*previous_pref), ps.thread ? compare_tid : compare_pid);
633         if (oldpp == NULL) {
634                 pp->ki_udata = NOPROC;
635                 return (NULL);
636         }
637         oldp = *oldpp;
638         if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) {
639                 pp->ki_udata = NOPROC;
640                 return (NULL);
641         }
642         pp->ki_udata = oldp;
643         return (oldp);
644 }
645
646 /*
647  * Return the total amount of IO done in blocks in/out and faults.
648  * store the values individually in the pointers passed in.
649  */
650 long
651 get_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp,
652     long *vcsw, long *ivcsw)
653 {
654         const struct kinfo_proc *oldp;
655         static struct kinfo_proc dummy;
656         long ret;
657
658         oldp = get_old_proc(pp);
659         if (oldp == NULL) {
660                 bzero(&dummy, sizeof(dummy));
661                 oldp = &dummy;
662         }
663         *inp = RU(pp)->ru_inblock - RU(oldp)->ru_inblock;
664         *oup = RU(pp)->ru_oublock - RU(oldp)->ru_oublock;
665         *flp = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
666         *vcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
667         *ivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
668         ret =
669             (RU(pp)->ru_inblock - RU(oldp)->ru_inblock) +
670             (RU(pp)->ru_oublock - RU(oldp)->ru_oublock) +
671             (RU(pp)->ru_majflt - RU(oldp)->ru_majflt);
672         return (ret);
673 }
674
675 /*
676  * If there was a previous update, use the delta in ki_runtime over
677  * the previous interval to calculate pctcpu.  Otherwise, fall back
678  * to using the kernel's ki_pctcpu.
679  */
680 static double
681 proc_calc_pctcpu(struct kinfo_proc *pp)
682 {
683         const struct kinfo_proc *oldp;
684
685         if (previous_interval != 0) {
686                 oldp = get_old_proc(pp);
687                 if (oldp != NULL)
688                         return ((double)(pp->ki_runtime - oldp->ki_runtime)
689                             / previous_interval);
690
691                 /*
692                  * If this process/thread was created during the previous
693                  * interval, charge it's total runtime to the previous
694                  * interval.
695                  */
696                 else if (pp->ki_start.tv_sec > previous_wall_time.tv_sec ||
697                     (pp->ki_start.tv_sec == previous_wall_time.tv_sec &&
698                     pp->ki_start.tv_usec >= previous_wall_time.tv_usec))
699                         return ((double)pp->ki_runtime / previous_interval);
700         }
701         return (pctdouble(pp->ki_pctcpu));
702 }
703
704 /*
705  * Return true if this process has used any CPU time since the
706  * previous update.
707  */
708 static int
709 proc_used_cpu(struct kinfo_proc *pp)
710 {
711         const struct kinfo_proc *oldp;
712
713         oldp = get_old_proc(pp);
714         if (oldp == NULL)
715                 return (PCTCPU(pp) != 0);
716         return (pp->ki_runtime != oldp->ki_runtime ||
717             RU(pp)->ru_nvcsw != RU(oldp)->ru_nvcsw ||
718             RU(pp)->ru_nivcsw != RU(oldp)->ru_nivcsw);
719 }
720
721 /*
722  * Return the total number of block in/out and faults by a process.
723  */
724 long
725 get_io_total(struct kinfo_proc *pp)
726 {
727         long dummy;
728
729         return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy));
730 }
731
732 static struct handle handle;
733
734 caddr_t
735 get_process_info(struct system_info *si, struct process_select *sel,
736     int (*compare)(const void *, const void *))
737 {
738         int i;
739         int total_procs;
740         long p_io;
741         long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw;
742         long nsec;
743         int active_procs;
744         struct kinfo_proc **prefp;
745         struct kinfo_proc *pp;
746         struct timespec previous_proc_uptime;
747
748         /* these are copied out of sel for speed */
749         int show_idle;
750         int show_jid;
751         int show_self;
752         int show_system;
753         int show_uid;
754         int show_command;
755         int show_kidle;
756
757         /*
758          * If thread state was toggled, don't cache the previous processes.
759          */
760         if (previous_thread != sel->thread)
761                 nproc = 0;
762         previous_thread = sel->thread;
763
764         /*
765          * Save the previous process info.
766          */
767         if (previous_proc_count_max < nproc) {
768                 free(previous_procs);
769                 previous_procs = malloc(nproc * sizeof(*previous_procs));
770                 free(previous_pref);
771                 previous_pref = malloc(nproc * sizeof(*previous_pref));
772                 if (previous_procs == NULL || previous_pref == NULL) {
773                         (void) fprintf(stderr, "top: Out of memory.\n");
774                         quit(23);
775                 }
776                 previous_proc_count_max = nproc;
777         }
778         if (nproc) {
779                 for (i = 0; i < nproc; i++)
780                         previous_pref[i] = &previous_procs[i];
781                 bcopy(pbase, previous_procs, nproc * sizeof(*previous_procs));
782                 qsort(previous_pref, nproc, sizeof(*previous_pref),
783                     ps.thread ? compare_tid : compare_pid);
784         }
785         previous_proc_count = nproc;
786         previous_proc_uptime = proc_uptime;
787         previous_wall_time = proc_wall_time;
788         previous_interval = 0;
789
790         pbase = kvm_getprocs(kd, sel->thread ? KERN_PROC_ALL : KERN_PROC_PROC,
791             0, &nproc);
792         (void)gettimeofday(&proc_wall_time, NULL);
793         if (clock_gettime(CLOCK_UPTIME, &proc_uptime) != 0)
794                 memset(&proc_uptime, 0, sizeof(proc_uptime));
795         else if (previous_proc_uptime.tv_sec != 0 &&
796             previous_proc_uptime.tv_nsec != 0) {
797                 previous_interval = (proc_uptime.tv_sec -
798                     previous_proc_uptime.tv_sec) * 1000000;
799                 nsec = proc_uptime.tv_nsec - previous_proc_uptime.tv_nsec;
800                 if (nsec < 0) {
801                         previous_interval -= 1000000;
802                         nsec += 1000000000;
803                 }
804                 previous_interval += nsec / 1000;
805         }
806         if (nproc > onproc) {
807                 pref = realloc(pref, sizeof(*pref) * nproc);
808                 pcpu = realloc(pcpu, sizeof(*pcpu) * nproc);
809                 onproc = nproc;
810         }
811         if (pref == NULL || pbase == NULL || pcpu == NULL) {
812                 (void) fprintf(stderr, "top: Out of memory.\n");
813                 quit(23);
814         }
815         /* get a pointer to the states summary array */
816         si->procstates = process_states;
817
818         /* set up flags which define what we are going to select */
819         show_idle = sel->idle;
820         show_jid = sel->jid != -1;
821         show_self = sel->self == -1;
822         show_system = sel->system;
823         show_uid = sel->uid != -1;
824         show_command = sel->command != NULL;
825         show_kidle = sel->kidle;
826
827         /* count up process states and get pointers to interesting procs */
828         total_procs = 0;
829         active_procs = 0;
830         total_inblock = 0;
831         total_oublock = 0;
832         total_majflt = 0;
833         memset((char *)process_states, 0, sizeof(process_states));
834         prefp = pref;
835         for (pp = pbase, i = 0; i < nproc; pp++, i++) {
836
837                 if (pp->ki_stat == 0)
838                         /* not in use */
839                         continue;
840
841                 if (!show_self && pp->ki_pid == sel->self)
842                         /* skip self */
843                         continue;
844
845                 if (!show_system && (pp->ki_flag & P_SYSTEM))
846                         /* skip system process */
847                         continue;
848
849                 p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt,
850                     &p_vcsw, &p_ivcsw);
851                 total_inblock += p_inblock;
852                 total_oublock += p_oublock;
853                 total_majflt += p_majflt;
854                 total_procs++;
855                 process_states[pp->ki_stat]++;
856
857                 if (pp->ki_stat == SZOMB)
858                         /* skip zombies */
859                         continue;
860
861                 if (!show_kidle && pp->ki_tdflags & TDF_IDLETD)
862                         /* skip kernel idle process */
863                         continue;
864
865                 PCTCPU(pp) = proc_calc_pctcpu(pp);
866                 if (sel->thread && PCTCPU(pp) > 1.0)
867                         PCTCPU(pp) = 1.0;
868                 if (displaymode == DISP_CPU && !show_idle &&
869                     (!proc_used_cpu(pp) ||
870                      pp->ki_stat == SSTOP || pp->ki_stat == SIDL))
871                         /* skip idle or non-running processes */
872                         continue;
873
874                 if (displaymode == DISP_IO && !show_idle && p_io == 0)
875                         /* skip processes that aren't doing I/O */
876                         continue;
877
878                 if (show_jid && pp->ki_jid != sel->jid)
879                         /* skip proc. that don't belong to the selected JID */
880                         continue;
881
882                 if (show_uid && pp->ki_ruid != (uid_t)sel->uid)
883                         /* skip proc. that don't belong to the selected UID */
884                         continue;
885
886                 *prefp++ = pp;
887                 active_procs++;
888         }
889
890         /* if requested, sort the "interesting" processes */
891         if (compare != NULL)
892                 qsort(pref, active_procs, sizeof(*pref), compare);
893
894         /* remember active and total counts */
895         si->p_total = total_procs;
896         si->p_active = pref_len = active_procs;
897
898         /* pass back a handle */
899         handle.next_proc = pref;
900         handle.remaining = active_procs;
901         return ((caddr_t)&handle);
902 }
903
904 static char fmt[512];   /* static area where result is built */
905
906 char *
907 format_next_process(caddr_t handle, char *(*get_userid)(int), int flags)
908 {
909         struct kinfo_proc *pp;
910         const struct kinfo_proc *oldp;
911         long cputime;
912         double pct;
913         struct handle *hp;
914         char status[16];
915         int cpu, state;
916         struct rusage ru, *rup;
917         long p_tot, s_tot;
918         char *proc_fmt, thr_buf[6];
919         char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1];
920         char *cmdbuf = NULL;
921         char **args;
922         const int cmdlen = 128;
923
924         /* find and remember the next proc structure */
925         hp = (struct handle *)handle;
926         pp = *(hp->next_proc++);
927         hp->remaining--;
928
929         /* get the process's command name */
930         if ((pp->ki_flag & P_INMEM) == 0) {
931                 /*
932                  * Print swapped processes as <pname>
933                  */
934                 size_t len;
935
936                 len = strlen(pp->ki_comm);
937                 if (len > sizeof(pp->ki_comm) - 3)
938                         len = sizeof(pp->ki_comm) - 3;
939                 memmove(pp->ki_comm + 1, pp->ki_comm, len);
940                 pp->ki_comm[0] = '<';
941                 pp->ki_comm[len + 1] = '>';
942                 pp->ki_comm[len + 2] = '\0';
943         }
944
945         /*
946          * Convert the process's runtime from microseconds to seconds.  This
947          * time includes the interrupt time although that is not wanted here.
948          * ps(1) is similarly sloppy.
949          */
950         cputime = (pp->ki_runtime + 500000) / 1000000;
951
952         /* calculate the base for cpu percentages */
953         pct = PCTCPU(pp);
954
955         /* generate "STATE" field */
956         switch (state = pp->ki_stat) {
957         case SRUN:
958                 if (smpmode && pp->ki_oncpu != NOCPU)
959                         sprintf(status, "CPU%d", pp->ki_oncpu);
960                 else
961                         strcpy(status, "RUN");
962                 break;
963         case SLOCK:
964                 if (pp->ki_kiflag & KI_LOCKBLOCK) {
965                         sprintf(status, "*%.6s", pp->ki_lockname);
966                         break;
967                 }
968                 /* fall through */
969         case SSLEEP:
970                 if (pp->ki_wmesg != NULL) {
971                         sprintf(status, "%.6s", pp->ki_wmesg);
972                         break;
973                 }
974                 /* FALLTHROUGH */
975         default:
976
977                 if (state >= 0 &&
978                     state < sizeof(state_abbrev) / sizeof(*state_abbrev))
979                         sprintf(status, "%.6s", state_abbrev[state]);
980                 else
981                         sprintf(status, "?%5d", state);
982                 break;
983         }
984
985         cmdbuf = (char *)malloc(cmdlen + 1);
986         if (cmdbuf == NULL) {
987                 warn("malloc(%d)", cmdlen + 1);
988                 return NULL;
989         }
990
991         if (!(flags & FMT_SHOWARGS)) {
992                 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
993                     pp->ki_tdname[0]) {
994                         snprintf(cmdbuf, cmdlen, "%s{%s%s}", pp->ki_comm,
995                             pp->ki_tdname, pp->ki_moretdname);
996                 } else {
997                         snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm);
998                 }
999         } else {
1000                 if (pp->ki_flag & P_SYSTEM ||
1001                     pp->ki_args == NULL ||
1002                     (args = kvm_getargv(kd, pp, cmdlen)) == NULL ||
1003                     !(*args)) {
1004                         if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1005                             pp->ki_tdname[0]) {
1006                                 snprintf(cmdbuf, cmdlen,
1007                                     "[%s{%s%s}]", pp->ki_comm, pp->ki_tdname,
1008                                     pp->ki_moretdname);
1009                         } else {
1010                                 snprintf(cmdbuf, cmdlen,
1011                                     "[%s]", pp->ki_comm);
1012                         }
1013                 } else {
1014                         char *src, *dst, *argbuf;
1015                         char *cmd;
1016                         size_t argbuflen;
1017                         size_t len;
1018
1019                         argbuflen = cmdlen * 4;
1020                         argbuf = (char *)malloc(argbuflen + 1);
1021                         if (argbuf == NULL) {
1022                                 warn("malloc(%zu)", argbuflen + 1);
1023                                 free(cmdbuf);
1024                                 return NULL;
1025                         }
1026
1027                         dst = argbuf;
1028
1029                         /* Extract cmd name from argv */
1030                         cmd = strrchr(*args, '/');
1031                         if (cmd == NULL)
1032                                 cmd = *args;
1033                         else
1034                                 cmd++;
1035
1036                         for (; (src = *args++) != NULL; ) {
1037                                 if (*src == '\0')
1038                                         continue;
1039                                 len = (argbuflen - (dst - argbuf) - 1) / 4;
1040                                 strvisx(dst, src,
1041                                     MIN(strlen(src), len),
1042                                     VIS_NL | VIS_CSTYLE);
1043                                 while (*dst != '\0')
1044                                         dst++;
1045                                 if ((argbuflen - (dst - argbuf) - 1) / 4 > 0)
1046                                         *dst++ = ' '; /* add delimiting space */
1047                         }
1048                         if (dst != argbuf && dst[-1] == ' ')
1049                                 dst--;
1050                         *dst = '\0';
1051
1052                         if (strcmp(cmd, pp->ki_comm) != 0) {
1053                                 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1054                                     pp->ki_tdname[0])
1055                                         snprintf(cmdbuf, cmdlen,
1056                                             "%s (%s){%s%s}", argbuf,
1057                                             pp->ki_comm, pp->ki_tdname,
1058                                             pp->ki_moretdname);
1059                                 else
1060                                         snprintf(cmdbuf, cmdlen,
1061                                             "%s (%s)", argbuf, pp->ki_comm);
1062                         } else {
1063                                 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1064                                     pp->ki_tdname[0])
1065                                         snprintf(cmdbuf, cmdlen,
1066                                             "%s{%s%s}", argbuf, pp->ki_tdname,
1067                                             pp->ki_moretdname);
1068                                 else
1069                                         strlcpy(cmdbuf, argbuf, cmdlen);
1070                         }
1071                         free(argbuf);
1072                 }
1073         }
1074
1075         if (ps.jail == 0) 
1076                 jid_buf[0] = '\0';
1077         else
1078                 snprintf(jid_buf, sizeof(jid_buf), "%*d",
1079                     jidlength - 1, pp->ki_jid);
1080
1081         if (ps.swap == 0)
1082                 swap_buf[0] = '\0';
1083         else
1084                 snprintf(swap_buf, sizeof(swap_buf), "%*s",
1085                     swaplength - 1,
1086                     format_k2(pagetok(ki_swap(pp)))); /* XXX */
1087
1088         if (displaymode == DISP_IO) {
1089                 oldp = get_old_proc(pp);
1090                 if (oldp != NULL) {
1091                         ru.ru_inblock = RU(pp)->ru_inblock -
1092                             RU(oldp)->ru_inblock;
1093                         ru.ru_oublock = RU(pp)->ru_oublock -
1094                             RU(oldp)->ru_oublock;
1095                         ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
1096                         ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
1097                         ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
1098                         rup = &ru;
1099                 } else {
1100                         rup = RU(pp);
1101                 }
1102                 p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt;
1103                 s_tot = total_inblock + total_oublock + total_majflt;
1104
1105                 snprintf(fmt, sizeof(fmt), io_Proc_format,
1106                     pp->ki_pid,
1107                     jidlength, jid_buf,
1108                     namelength, namelength, (*get_userid)(pp->ki_ruid),
1109                     rup->ru_nvcsw,
1110                     rup->ru_nivcsw,
1111                     rup->ru_inblock,
1112                     rup->ru_oublock,
1113                     rup->ru_majflt,
1114                     p_tot,
1115                     s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot),
1116                     screen_width > cmdlengthdelta ?
1117                     screen_width - cmdlengthdelta : 0,
1118                     printable(cmdbuf));
1119
1120                 free(cmdbuf);
1121
1122                 return (fmt);
1123         }
1124
1125         /* format this entry */
1126         if (smpmode) {
1127                 if (state == SRUN && pp->ki_oncpu != NOCPU)
1128                         cpu = pp->ki_oncpu;
1129                 else
1130                         cpu = pp->ki_lastcpu;
1131         } else
1132                 cpu = 0;
1133         proc_fmt = smpmode ? smp_Proc_format : up_Proc_format;
1134         if (ps.thread != 0)
1135                 thr_buf[0] = '\0';
1136         else
1137                 snprintf(thr_buf, sizeof(thr_buf), "%*d ",
1138                     (int)(sizeof(thr_buf) - 2), pp->ki_numthreads);
1139
1140         snprintf(fmt, sizeof(fmt), proc_fmt,
1141             pp->ki_pid,
1142             jidlength, jid_buf,
1143             namelength, namelength, (*get_userid)(pp->ki_ruid),
1144             thr_buf,
1145             pp->ki_pri.pri_level - PZERO,
1146             format_nice(pp),
1147             format_k2(PROCSIZE(pp)),
1148             format_k2(pagetok(pp->ki_rssize)),
1149             swaplength, swaplength, swap_buf,
1150             status,
1151             cpu,
1152             format_time(cputime),
1153             ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct,
1154             screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0,
1155             printable(cmdbuf));
1156
1157         free(cmdbuf);
1158
1159         /* return the result */
1160         return (fmt);
1161 }
1162
1163 static void
1164 getsysctl(const char *name, void *ptr, size_t len)
1165 {
1166         size_t nlen = len;
1167
1168         if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
1169                 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name,
1170                     strerror(errno));
1171                 quit(23);
1172         }
1173         if (nlen != len) {
1174                 fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n",
1175                     name, (unsigned long)len, (unsigned long)nlen);
1176                 quit(23);
1177         }
1178 }
1179
1180 static const char *
1181 format_nice(const struct kinfo_proc *pp)
1182 {
1183         const char *fifo, *kproc;
1184         int rtpri;
1185         static char nicebuf[4 + 1];
1186
1187         fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F";
1188         kproc = (pp->ki_flag & P_KPROC) ? "k" : "";
1189         switch (PRI_BASE(pp->ki_pri.pri_class)) {
1190         case PRI_ITHD:
1191                 return ("-");
1192         case PRI_REALTIME:
1193                 /*
1194                  * XXX: the kernel doesn't tell us the original rtprio and
1195                  * doesn't really know what it was, so to recover it we
1196                  * must be more chummy with the implementation than the
1197                  * implementation is with itself.  pri_user gives a
1198                  * constant "base" priority, but is only initialized
1199                  * properly for user threads.  pri_native gives what the
1200                  * kernel calls the "base" priority, but it isn't constant
1201                  * since it is changed by priority propagation.  pri_native
1202                  * also isn't properly initialized for all threads, but it
1203                  * is properly initialized for kernel realtime and idletime
1204                  * threads.  Thus we use pri_user for the base priority of
1205                  * user threads (it is always correct) and pri_native for
1206                  * the base priority of kernel realtime and idletime threads
1207                  * (there is nothing better, and it is usually correct).
1208                  *
1209                  * The field width and thus the buffer are too small for
1210                  * values like "kr31F", but such values shouldn't occur,
1211                  * and if they do then the tailing "F" is not displayed.
1212                  */
1213                 rtpri = ((pp->ki_flag & P_KPROC) ? pp->ki_pri.pri_native :
1214                     pp->ki_pri.pri_user) - PRI_MIN_REALTIME;
1215                 snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s",
1216                     kproc, rtpri, fifo);
1217                 break;
1218         case PRI_TIMESHARE:
1219                 if (pp->ki_flag & P_KPROC)
1220                         return ("-");
1221                 snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO);
1222                 break;
1223         case PRI_IDLE:
1224                 /* XXX: as above. */
1225                 rtpri = ((pp->ki_flag & P_KPROC) ? pp->ki_pri.pri_native :
1226                     pp->ki_pri.pri_user) - PRI_MIN_IDLE;
1227                 snprintf(nicebuf, sizeof(nicebuf), "%si%d%s",
1228                     kproc, rtpri, fifo);
1229                 break;
1230         default:
1231                 return ("?");
1232         }
1233         return (nicebuf);
1234 }
1235
1236 /* comparison routines for qsort */
1237
1238 static int
1239 compare_pid(const void *p1, const void *p2)
1240 {
1241         const struct kinfo_proc * const *pp1 = p1;
1242         const struct kinfo_proc * const *pp2 = p2;
1243
1244         if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0)
1245                 abort();
1246
1247         return ((*pp1)->ki_pid - (*pp2)->ki_pid);
1248 }
1249
1250 static int
1251 compare_tid(const void *p1, const void *p2)
1252 {
1253         const struct kinfo_proc * const *pp1 = p1;
1254         const struct kinfo_proc * const *pp2 = p2;
1255
1256         if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0)
1257                 abort();
1258
1259         return ((*pp1)->ki_tid - (*pp2)->ki_tid);
1260 }
1261
1262 /*
1263  *  proc_compare - comparison function for "qsort"
1264  *      Compares the resource consumption of two processes using five
1265  *      distinct keys.  The keys (in descending order of importance) are:
1266  *      percent cpu, cpu ticks, state, resident set size, total virtual
1267  *      memory usage.  The process states are ordered as follows (from least
1268  *      to most important):  WAIT, zombie, sleep, stop, start, run.  The
1269  *      array declaration below maps a process state index into a number
1270  *      that reflects this ordering.
1271  */
1272
1273 static int sorted_state[] = {
1274         0,      /* not used             */
1275         3,      /* sleep                */
1276         1,      /* ABANDONED (WAIT)     */
1277         6,      /* run                  */
1278         5,      /* start                */
1279         2,      /* zombie               */
1280         4       /* stop                 */
1281 };
1282
1283
1284 #define ORDERKEY_PCTCPU(a, b) do { \
1285         double diff; \
1286         if (ps.wcpu) \
1287                 diff = weighted_cpu(PCTCPU((b)), (b)) - \
1288                     weighted_cpu(PCTCPU((a)), (a)); \
1289         else \
1290                 diff = PCTCPU((b)) - PCTCPU((a)); \
1291         if (diff != 0) \
1292                 return (diff > 0 ? 1 : -1); \
1293 } while (0)
1294
1295 #define ORDERKEY_CPTICKS(a, b) do { \
1296         int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \
1297         if (diff != 0) \
1298                 return (diff > 0 ? 1 : -1); \
1299 } while (0)
1300
1301 #define ORDERKEY_STATE(a, b) do { \
1302         int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \
1303         if (diff != 0) \
1304                 return (diff > 0 ? 1 : -1); \
1305 } while (0)
1306
1307 #define ORDERKEY_PRIO(a, b) do { \
1308         int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \
1309         if (diff != 0) \
1310                 return (diff > 0 ? 1 : -1); \
1311 } while (0)
1312
1313 #define ORDERKEY_THREADS(a, b) do { \
1314         int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \
1315         if (diff != 0) \
1316                 return (diff > 0 ? 1 : -1); \
1317 } while (0)
1318
1319 #define ORDERKEY_RSSIZE(a, b) do { \
1320         long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \
1321         if (diff != 0) \
1322                 return (diff > 0 ? 1 : -1); \
1323 } while (0)
1324
1325 #define ORDERKEY_MEM(a, b) do { \
1326         long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \
1327         if (diff != 0) \
1328                 return (diff > 0 ? 1 : -1); \
1329 } while (0)
1330
1331 #define ORDERKEY_JID(a, b) do { \
1332         int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \
1333         if (diff != 0) \
1334                 return (diff > 0 ? 1 : -1); \
1335 } while (0)
1336
1337 #define ORDERKEY_SWAP(a, b) do { \
1338         int diff = (int)ki_swap(b) - (int)ki_swap(a); \
1339         if (diff != 0) \
1340                 return (diff > 0 ? 1 : -1); \
1341 } while (0)
1342
1343 /* compare_cpu - the comparison function for sorting by cpu percentage */
1344
1345 int
1346 #ifdef ORDER
1347 compare_cpu(void *arg1, void *arg2)
1348 #else
1349 proc_compare(void *arg1, void *arg2)
1350 #endif
1351 {
1352         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1353         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1354
1355         ORDERKEY_PCTCPU(p1, p2);
1356         ORDERKEY_CPTICKS(p1, p2);
1357         ORDERKEY_STATE(p1, p2);
1358         ORDERKEY_PRIO(p1, p2);
1359         ORDERKEY_RSSIZE(p1, p2);
1360         ORDERKEY_MEM(p1, p2);
1361
1362         return (0);
1363 }
1364
1365 #ifdef ORDER
1366 /* "cpu" compare routines */
1367 int compare_size(), compare_res(), compare_time(), compare_prio(),
1368     compare_threads();
1369
1370 /*
1371  * "io" compare routines.  Context switches aren't i/o, but are displayed
1372  * on the "io" display.
1373  */
1374 int compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(),
1375     compare_vcsw(), compare_ivcsw();
1376
1377 int (*compares[])() = {
1378         compare_cpu,
1379         compare_size,
1380         compare_res,
1381         compare_time,
1382         compare_prio,
1383         compare_threads,
1384         compare_iototal,
1385         compare_ioread,
1386         compare_iowrite,
1387         compare_iofault,
1388         compare_vcsw,
1389         compare_ivcsw,
1390         compare_jid,
1391         compare_swap,
1392         NULL
1393 };
1394
1395 /* compare_size - the comparison function for sorting by total memory usage */
1396
1397 int
1398 compare_size(void *arg1, void *arg2)
1399 {
1400         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1401         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1402
1403         ORDERKEY_MEM(p1, p2);
1404         ORDERKEY_RSSIZE(p1, p2);
1405         ORDERKEY_PCTCPU(p1, p2);
1406         ORDERKEY_CPTICKS(p1, p2);
1407         ORDERKEY_STATE(p1, p2);
1408         ORDERKEY_PRIO(p1, p2);
1409
1410         return (0);
1411 }
1412
1413 /* compare_res - the comparison function for sorting by resident set size */
1414
1415 int
1416 compare_res(void *arg1, void *arg2)
1417 {
1418         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1419         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1420
1421         ORDERKEY_RSSIZE(p1, p2);
1422         ORDERKEY_MEM(p1, p2);
1423         ORDERKEY_PCTCPU(p1, p2);
1424         ORDERKEY_CPTICKS(p1, p2);
1425         ORDERKEY_STATE(p1, p2);
1426         ORDERKEY_PRIO(p1, p2);
1427
1428         return (0);
1429 }
1430
1431 /* compare_time - the comparison function for sorting by total cpu time */
1432
1433 int
1434 compare_time(void *arg1, void *arg2)
1435 {
1436         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1437         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1438
1439         ORDERKEY_CPTICKS(p1, p2);
1440         ORDERKEY_PCTCPU(p1, p2);
1441         ORDERKEY_STATE(p1, p2);
1442         ORDERKEY_PRIO(p1, p2);
1443         ORDERKEY_RSSIZE(p1, p2);
1444         ORDERKEY_MEM(p1, p2);
1445
1446         return (0);
1447 }
1448
1449 /* compare_prio - the comparison function for sorting by priority */
1450
1451 int
1452 compare_prio(void *arg1, void *arg2)
1453 {
1454         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1455         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1456
1457         ORDERKEY_PRIO(p1, p2);
1458         ORDERKEY_CPTICKS(p1, p2);
1459         ORDERKEY_PCTCPU(p1, p2);
1460         ORDERKEY_STATE(p1, p2);
1461         ORDERKEY_RSSIZE(p1, p2);
1462         ORDERKEY_MEM(p1, p2);
1463
1464         return (0);
1465 }
1466
1467 /* compare_threads - the comparison function for sorting by threads */
1468 int
1469 compare_threads(void *arg1, void *arg2)
1470 {
1471         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1472         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1473
1474         ORDERKEY_THREADS(p1, p2);
1475         ORDERKEY_PCTCPU(p1, p2);
1476         ORDERKEY_CPTICKS(p1, p2);
1477         ORDERKEY_STATE(p1, p2);
1478         ORDERKEY_PRIO(p1, p2);
1479         ORDERKEY_RSSIZE(p1, p2);
1480         ORDERKEY_MEM(p1, p2);
1481
1482         return (0);
1483 }
1484
1485 /* compare_jid - the comparison function for sorting by jid */
1486 static int
1487 compare_jid(const void *arg1, const void *arg2)
1488 {
1489         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1490         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1491
1492         ORDERKEY_JID(p1, p2);
1493         ORDERKEY_PCTCPU(p1, p2);
1494         ORDERKEY_CPTICKS(p1, p2);
1495         ORDERKEY_STATE(p1, p2);
1496         ORDERKEY_PRIO(p1, p2);
1497         ORDERKEY_RSSIZE(p1, p2);
1498         ORDERKEY_MEM(p1, p2);
1499
1500         return (0);
1501 }
1502
1503 /* compare_swap - the comparison function for sorting by swap */
1504 static int
1505 compare_swap(const void *arg1, const void *arg2)
1506 {
1507         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1508         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1509
1510         ORDERKEY_SWAP(p1, p2);
1511         ORDERKEY_PCTCPU(p1, p2);
1512         ORDERKEY_CPTICKS(p1, p2);
1513         ORDERKEY_STATE(p1, p2);
1514         ORDERKEY_PRIO(p1, p2);
1515         ORDERKEY_RSSIZE(p1, p2);
1516         ORDERKEY_MEM(p1, p2);
1517
1518         return (0);
1519 }
1520 #endif /* ORDER */
1521
1522 /* assorted comparison functions for sorting by i/o */
1523
1524 int
1525 #ifdef ORDER
1526 compare_iototal(void *arg1, void *arg2)
1527 #else
1528 io_compare(void *arg1, void *arg2)
1529 #endif
1530 {
1531         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1532         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1533
1534         return (get_io_total(p2) - get_io_total(p1));
1535 }
1536
1537 #ifdef ORDER
1538 int
1539 compare_ioread(void *arg1, void *arg2)
1540 {
1541         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1542         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1543         long dummy, inp1, inp2;
1544
1545         (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy);
1546         (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy);
1547
1548         return (inp2 - inp1);
1549 }
1550
1551 int
1552 compare_iowrite(void *arg1, void *arg2)
1553 {
1554         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1555         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1556         long dummy, oup1, oup2;
1557
1558         (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy);
1559         (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy);
1560
1561         return (oup2 - oup1);
1562 }
1563
1564 int
1565 compare_iofault(void *arg1, void *arg2)
1566 {
1567         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1568         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1569         long dummy, flp1, flp2;
1570
1571         (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy);
1572         (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy);
1573
1574         return (flp2 - flp1);
1575 }
1576
1577 int
1578 compare_vcsw(void *arg1, void *arg2)
1579 {
1580         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1581         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1582         long dummy, flp1, flp2;
1583
1584         (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy);
1585         (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy);
1586
1587         return (flp2 - flp1);
1588 }
1589
1590 int
1591 compare_ivcsw(void *arg1, void *arg2)
1592 {
1593         struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1594         struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1595         long dummy, flp1, flp2;
1596
1597         (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1);
1598         (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2);
1599
1600         return (flp2 - flp1);
1601 }
1602 #endif /* ORDER */
1603
1604 /*
1605  * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
1606  *              the process does not exist.
1607  *              It is EXTREMELY IMPORTANT that this function work correctly.
1608  *              If top runs setuid root (as in SVR4), then this function
1609  *              is the only thing that stands in the way of a serious
1610  *              security problem.  It validates requests for the "kill"
1611  *              and "renice" commands.
1612  */
1613
1614 int
1615 proc_owner(int pid)
1616 {
1617         int cnt;
1618         struct kinfo_proc **prefp;
1619         struct kinfo_proc *pp;
1620
1621         prefp = pref;
1622         cnt = pref_len;
1623         while (--cnt >= 0) {
1624                 pp = *prefp++;
1625                 if (pp->ki_pid == (pid_t)pid)
1626                         return ((int)pp->ki_ruid);
1627         }
1628         return (-1);
1629 }
1630
1631 static int
1632 swapmode(int *retavail, int *retfree)
1633 {
1634         int n;
1635         int pagesize = getpagesize();
1636         struct kvm_swap swapary[1];
1637
1638         *retavail = 0;
1639         *retfree = 0;
1640
1641 #define CONVERT(v)      ((quad_t)(v) * pagesize / 1024)
1642
1643         n = kvm_getswapinfo(kd, swapary, 1, 0);
1644         if (n < 0 || swapary[0].ksw_total == 0)
1645                 return (0);
1646
1647         *retavail = CONVERT(swapary[0].ksw_total);
1648         *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
1649
1650         n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total);
1651         return (n);
1652 }