2 * top - a top users display for Unix
4 * SYNOPSIS: For FreeBSD-2.x and later
7 * Originally written for BSD4.4 system by Christos Zoulas.
8 * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider
9 * Order support hacked in from top-3.5beta6/machine/m_aix41.c
10 * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/)
12 * This is the machine-dependent module for FreeBSD 2.2
14 * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x
18 * AUTHOR: Christos Zoulas <christos@ee.cornell.edu>
19 * Steven Wallace <swallace@freebsd.org>
20 * Wolfram Schneider <wosch@FreeBSD.org>
21 * Thomas Moestl <tmoestl@gmx.net>
26 #include <sys/param.h>
27 #include <sys/errno.h>
30 #include <sys/resource.h>
31 #include <sys/rtprio.h>
32 #include <sys/signal.h>
33 #include <sys/sysctl.h>
36 #include <sys/vmmeter.h>
57 #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
58 #define SMPUNAMELEN 13
61 extern struct process_select ps;
62 extern char* printable(char *);
64 enum displaymodes displaymode;
65 #ifdef TOP_USERNAME_LEN
66 static int namelength = TOP_USERNAME_LEN;
68 static int namelength = 8;
70 /* TOP_JID_LEN based on max of 999999 */
72 #define TOP_SWAP_LEN 6
74 static int swaplength;
75 static int cmdlengthdelta;
77 /* Prototypes for top internals */
80 /* get_process_info passes back a handle. This is what it looks like: */
83 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
84 int remaining; /* number of pointers remaining */
87 /* declarations for load_avg */
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))))
94 /* what we consider to be process size: */
95 #define PROCSIZE(pp) ((pp)->ki_size / 1024)
97 #define RU(pp) (&(pp)->ki_rusage)
99 (RU(pp)->ru_inblock + RU(pp)->ru_oublock + RU(pp)->ru_majflt)
101 #define PCTCPU(pp) (pcpu[pp - pbase])
103 /* definitions for indices in the nlist array */
106 * These definitions control the format of the per-process area
109 static char io_header[] =
110 " PID%*s %-*.*s VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND";
112 #define io_Proc_format \
113 "%5d%*s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s"
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";
120 #define smp_Proc_format \
121 "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s %2d%7s %6.2f%% %.*s"
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";
128 #define up_Proc_format \
129 "%5d%*s %-*.*s %s%3d %4s%7s %6s%*.*s %-6.6s%.0d%7s %6.2f%% %.*s"
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 */
136 char *state_abbrev[] = {
137 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK"
143 /* values that we stash away in _init and use in later routines */
145 static double logcpu;
147 /* these are retrieved from the kernel in _init */
149 static load_avg ccpu;
151 /* these are used in the get_ functions */
155 /* these are for calculating cpu state percentages */
157 static long cp_time[CPUSTATES];
158 static long cp_old[CPUSTATES];
159 static long cp_diff[CPUSTATES];
161 /* these are for detailing the process states */
163 int process_states[8];
164 char *procstatenames[] = {
165 "", " starting, ", " running, ", " sleeping, ", " stopped, ",
166 " zombie, ", " waiting, ", " lock, ",
170 /* these are for detailing the cpu states */
172 int cpu_states[CPUSTATES];
173 char *cpustatenames[] = {
174 "user", "nice", "system", "interrupt", "idle", NULL
177 /* these are for detailing the memory statistics */
180 char *memorynames[] = {
181 "K Active, ", "K Inact, ", "K Laundry, ", "K Wired, ", "K Buf, ",
187 "K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other",
192 char *swapnames[] = {
193 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
198 /* these are for keeping track of the proc array */
201 static int onproc = -1;
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;
211 /* data used for recalculating pctcpu */
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;
218 /* total number of io operations */
219 static long total_inblock;
220 static long total_oublock;
221 static long total_majflt;
223 /* these are for getting the memory statistics */
225 static int arc_enabled;
226 static int pageshift; /* log base 2 of the pagesize */
228 /* define pagetok in terms of pageshift */
230 #define pagetok(size) ((size) << pageshift)
233 #define ki_swap(kip) \
234 ((kip)->ki_swrss > (kip)->ki_rssize ? (kip)->ki_swrss - (kip)->ki_rssize : 0)
236 /* useful externals */
241 * Sorting orders. The first element is the default.
243 char *ordernames[] = {
244 "cpu", "size", "res", "time", "pri", "threads",
245 "total", "read", "write", "fault", "vcsw", "ivcsw",
246 "jid", "swap", "pid", NULL
250 /* Per-cpu time states */
254 static u_long cpumask;
256 static long *pcpu_cp_time;
257 static long *pcpu_cp_old;
258 static long *pcpu_cp_diff;
259 static int *pcpu_cpu_states;
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);
271 toggle_pcpustats(void)
279 /* Adjust display based on ncpus and the ARC state. */
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;
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;
306 machine_init(struct statics *statics, char do_unames)
308 int i, j, empty, pagesize;
313 size = sizeof(smpmode);
314 if ((sysctlbyname("machdep.smp_active", &smpmode, &size,
316 sysctlbyname("kern.smp.active", &smpmode, &size,
318 size != sizeof(smpmode))
321 size = sizeof(arc_size);
322 if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc_size, &size,
323 NULL, 0) == 0 && arc_size != 0)
327 while ((pw = getpwent()) != NULL) {
328 if (strlen(pw->pw_name) > namelength)
329 namelength = strlen(pw->pw_name);
332 if (smpmode && namelength > SMPUNAMELEN)
333 namelength = SMPUNAMELEN;
334 else if (namelength > UPUNAMELEN)
335 namelength = UPUNAMELEN;
337 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
341 GETSYSCTL("kern.ccpu", ccpu);
343 /* this is used in calculating WCPU -- calculate it ahead of time */
344 logcpu = log(loaddouble(ccpu));
352 /* get the page size and calculate pageshift from it */
353 pagesize = getpagesize();
355 while (pagesize > 1) {
360 /* we only need the amount of log(2)1024 for our conversion */
361 pageshift -= LOG1024;
363 /* fill in the statics information */
364 statics->procstate_names = procstatenames;
365 statics->cpustate_names = cpustatenames;
366 statics->memory_names = memorynames;
368 statics->arc_names = arcnames;
370 statics->arc_names = NULL;
371 statics->swap_names = swapnames;
373 statics->order_names = ordernames;
376 /* Allocate state for per-CPU stats. */
379 GETSYSCTL("kern.smp.maxcpus", maxcpu);
380 size = sizeof(long) * maxcpu * CPUSTATES;
381 times = malloc(size);
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++) {
390 for (j = 0; empty && j < CPUSTATES; j++) {
391 if (times[i * CPUSTATES + j] != 0)
395 cpumask |= (1ul << i);
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;
412 format_header(char *uname_field)
414 static char Header[128];
418 jidlength = TOP_JID_LEN + 1; /* +1 for extra left space. */
423 swaplength = TOP_SWAP_LEN + 1; /* +1 for extra left space */
427 switch (displaymode) {
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
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");
446 snprintf(Header, sizeof(Header), prehead,
447 jidlength, ps.jail ? " JID" : "",
448 namelength, namelength, uname_field);
451 cmdlengthdelta = strlen(Header) - 7;
455 static int swappgsin = -1;
456 static int swappgsout = -1;
457 extern struct timeval timeout;
461 get_system_info(struct system_info *si)
464 struct loadavg sysload;
466 struct timeval boottime;
467 uint64_t arc_stat, arc_stat2;
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);
479 /* convert load averages to doubles */
480 for (i = 0; i < 3; i++)
481 si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale;
483 /* convert cp_time counts to percentages */
484 for (i = j = 0; i <= maxid; i++) {
485 if ((cpumask & (1ul << i)) == 0)
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]);
493 percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
495 /* sum memory & swap statistics */
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;
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;
526 /* compute differences between old and new swap statistic */
528 swap_stats[4] = pagetok(((nspgsin - swappgsin)));
529 swap_stats[5] = pagetok(((nspgsout - swappgsout)));
533 swappgsout = nspgsout;
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;
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;
563 /* set arrays and strings */
565 si->cpustates = pcpu_cpu_states;
568 si->cpustates = cpu_states;
571 si->memory = memory_stats;
572 si->swap = swap_stats;
576 si->last_pid = lastpid;
582 * Print how long system has been up.
583 * (Found by looking getting "boottime" from the kernel)
586 mib[1] = KERN_BOOTTIME;
587 size = sizeof(boottime);
588 if (sysctl(mib, nitems(mib), &boottime, &size, NULL, 0) != -1 &&
589 boottime.tv_sec != 0) {
590 si->boottime = boottime;
592 si->boottime.tv_sec = -1;
596 #define NOPROC ((void *)-1)
599 * We need to compare data from the old process entry with the new
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.
607 const struct kinfo_proc *
608 get_old_proc(struct kinfo_proc *pp)
610 struct kinfo_proc **oldpp, *oldp;
613 * If this is the first fetch of the kinfo_procs then we don't have
614 * any previous entries.
616 if (previous_proc_count == 0)
618 /* negative cache? */
619 if (pp->ki_udata == NOPROC)
622 if (pp->ki_udata != NULL)
623 return (pp->ki_udata);
626 * 1) look up based on pid.
627 * 2) compare process start.
628 * If we fail here, then setup a negative cache entry, otherwise
631 oldpp = bsearch(&pp, previous_pref, previous_proc_count,
632 sizeof(*previous_pref), ps.thread ? compare_tid : compare_pid);
634 pp->ki_udata = NOPROC;
638 if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) {
639 pp->ki_udata = NOPROC;
647 * Return the total amount of IO done in blocks in/out and faults.
648 * store the values individually in the pointers passed in.
651 get_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp,
652 long *vcsw, long *ivcsw)
654 const struct kinfo_proc *oldp;
655 static struct kinfo_proc dummy;
658 oldp = get_old_proc(pp);
660 bzero(&dummy, sizeof(dummy));
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;
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);
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.
681 proc_calc_pctcpu(struct kinfo_proc *pp)
683 const struct kinfo_proc *oldp;
685 if (previous_interval != 0) {
686 oldp = get_old_proc(pp);
688 return ((double)(pp->ki_runtime - oldp->ki_runtime)
689 / previous_interval);
692 * If this process/thread was created during the previous
693 * interval, charge it's total runtime to the previous
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);
701 return (pctdouble(pp->ki_pctcpu));
705 * Return true if this process has used any CPU time since the
709 proc_used_cpu(struct kinfo_proc *pp)
711 const struct kinfo_proc *oldp;
713 oldp = get_old_proc(pp);
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);
722 * Return the total number of block in/out and faults by a process.
725 get_io_total(struct kinfo_proc *pp)
729 return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy));
732 static struct handle handle;
735 get_process_info(struct system_info *si, struct process_select *sel,
736 int (*compare)(const void *, const void *))
741 long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw;
744 struct kinfo_proc **prefp;
745 struct kinfo_proc *pp;
746 struct timespec previous_proc_uptime;
748 /* these are copied out of sel for speed */
758 * If thread state was toggled, don't cache the previous processes.
760 if (previous_thread != sel->thread)
762 previous_thread = sel->thread;
765 * Save the previous process info.
767 if (previous_proc_count_max < nproc) {
768 free(previous_procs);
769 previous_procs = malloc(nproc * sizeof(*previous_procs));
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");
776 previous_proc_count_max = 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);
785 previous_proc_count = nproc;
786 previous_proc_uptime = proc_uptime;
787 previous_wall_time = proc_wall_time;
788 previous_interval = 0;
790 pbase = kvm_getprocs(kd, sel->thread ? KERN_PROC_ALL : KERN_PROC_PROC,
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;
801 previous_interval -= 1000000;
804 previous_interval += nsec / 1000;
806 if (nproc > onproc) {
807 pref = realloc(pref, sizeof(*pref) * nproc);
808 pcpu = realloc(pcpu, sizeof(*pcpu) * nproc);
811 if (pref == NULL || pbase == NULL || pcpu == NULL) {
812 (void) fprintf(stderr, "top: Out of memory.\n");
815 /* get a pointer to the states summary array */
816 si->procstates = process_states;
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;
827 /* count up process states and get pointers to interesting procs */
833 memset((char *)process_states, 0, sizeof(process_states));
835 for (pp = pbase, i = 0; i < nproc; pp++, i++) {
837 if (pp->ki_stat == 0)
841 if (!show_self && pp->ki_pid == sel->self)
845 if (!show_system && (pp->ki_flag & P_SYSTEM))
846 /* skip system process */
849 p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt,
851 total_inblock += p_inblock;
852 total_oublock += p_oublock;
853 total_majflt += p_majflt;
855 process_states[pp->ki_stat]++;
857 if (pp->ki_stat == SZOMB)
861 if (!show_kidle && pp->ki_tdflags & TDF_IDLETD)
862 /* skip kernel idle process */
865 PCTCPU(pp) = proc_calc_pctcpu(pp);
866 if (sel->thread && 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 */
874 if (displaymode == DISP_IO && !show_idle && p_io == 0)
875 /* skip processes that aren't doing I/O */
878 if (show_jid && pp->ki_jid != sel->jid)
879 /* skip proc. that don't belong to the selected JID */
882 if (show_uid && pp->ki_ruid != (uid_t)sel->uid)
883 /* skip proc. that don't belong to the selected UID */
890 /* if requested, sort the "interesting" processes */
892 qsort(pref, active_procs, sizeof(*pref), compare);
894 /* remember active and total counts */
895 si->p_total = total_procs;
896 si->p_active = pref_len = active_procs;
898 /* pass back a handle */
899 handle.next_proc = pref;
900 handle.remaining = active_procs;
901 return ((caddr_t)&handle);
904 static char fmt[512]; /* static area where result is built */
907 format_next_process(caddr_t handle, char *(*get_userid)(int), int flags)
909 struct kinfo_proc *pp;
910 const struct kinfo_proc *oldp;
916 struct rusage ru, *rup;
918 char *proc_fmt, thr_buf[6];
919 char jid_buf[TOP_JID_LEN + 1], swap_buf[TOP_SWAP_LEN + 1];
922 const int cmdlen = 128;
924 /* find and remember the next proc structure */
925 hp = (struct handle *)handle;
926 pp = *(hp->next_proc++);
929 /* get the process's command name */
930 if ((pp->ki_flag & P_INMEM) == 0) {
932 * Print swapped processes as <pname>
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';
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.
950 cputime = (pp->ki_runtime + 500000) / 1000000;
952 /* calculate the base for cpu percentages */
955 /* generate "STATE" field */
956 switch (state = pp->ki_stat) {
958 if (smpmode && pp->ki_oncpu != NOCPU)
959 sprintf(status, "CPU%d", pp->ki_oncpu);
961 strcpy(status, "RUN");
964 if (pp->ki_kiflag & KI_LOCKBLOCK) {
965 sprintf(status, "*%.6s", pp->ki_lockname);
970 if (pp->ki_wmesg != NULL) {
971 sprintf(status, "%.6s", pp->ki_wmesg);
978 state < sizeof(state_abbrev) / sizeof(*state_abbrev))
979 sprintf(status, "%.6s", state_abbrev[state]);
981 sprintf(status, "?%5d", state);
985 cmdbuf = (char *)malloc(cmdlen + 1);
986 if (cmdbuf == NULL) {
987 warn("malloc(%d)", cmdlen + 1);
991 if (!(flags & FMT_SHOWARGS)) {
992 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
994 snprintf(cmdbuf, cmdlen, "%s{%s%s}", pp->ki_comm,
995 pp->ki_tdname, pp->ki_moretdname);
997 snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm);
1000 if (pp->ki_flag & P_SYSTEM ||
1001 pp->ki_args == NULL ||
1002 (args = kvm_getargv(kd, pp, cmdlen)) == NULL ||
1004 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1006 snprintf(cmdbuf, cmdlen,
1007 "[%s{%s%s}]", pp->ki_comm, pp->ki_tdname,
1010 snprintf(cmdbuf, cmdlen,
1011 "[%s]", pp->ki_comm);
1014 char *src, *dst, *argbuf;
1019 argbuflen = cmdlen * 4;
1020 argbuf = (char *)malloc(argbuflen + 1);
1021 if (argbuf == NULL) {
1022 warn("malloc(%zu)", argbuflen + 1);
1029 /* Extract cmd name from argv */
1030 cmd = strrchr(*args, '/');
1036 for (; (src = *args++) != NULL; ) {
1039 len = (argbuflen - (dst - argbuf) - 1) / 4;
1041 MIN(strlen(src), len),
1042 VIS_NL | VIS_CSTYLE);
1043 while (*dst != '\0')
1045 if ((argbuflen - (dst - argbuf) - 1) / 4 > 0)
1046 *dst++ = ' '; /* add delimiting space */
1048 if (dst != argbuf && dst[-1] == ' ')
1052 if (strcmp(cmd, pp->ki_comm) != 0) {
1053 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1055 snprintf(cmdbuf, cmdlen,
1056 "%s (%s){%s%s}", argbuf,
1057 pp->ki_comm, pp->ki_tdname,
1060 snprintf(cmdbuf, cmdlen,
1061 "%s (%s)", argbuf, pp->ki_comm);
1063 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1065 snprintf(cmdbuf, cmdlen,
1066 "%s{%s%s}", argbuf, pp->ki_tdname,
1069 strlcpy(cmdbuf, argbuf, cmdlen);
1078 snprintf(jid_buf, sizeof(jid_buf), "%*d",
1079 jidlength - 1, pp->ki_jid);
1084 snprintf(swap_buf, sizeof(swap_buf), "%*s",
1086 format_k2(pagetok(ki_swap(pp)))); /* XXX */
1088 if (displaymode == DISP_IO) {
1089 oldp = get_old_proc(pp);
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;
1102 p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt;
1103 s_tot = total_inblock + total_oublock + total_majflt;
1105 snprintf(fmt, sizeof(fmt), io_Proc_format,
1108 namelength, namelength, (*get_userid)(pp->ki_ruid),
1115 s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot),
1116 screen_width > cmdlengthdelta ?
1117 screen_width - cmdlengthdelta : 0,
1125 /* format this entry */
1127 if (state == SRUN && pp->ki_oncpu != NOCPU)
1130 cpu = pp->ki_lastcpu;
1133 proc_fmt = smpmode ? smp_Proc_format : up_Proc_format;
1137 snprintf(thr_buf, sizeof(thr_buf), "%*d ",
1138 (int)(sizeof(thr_buf) - 2), pp->ki_numthreads);
1140 snprintf(fmt, sizeof(fmt), proc_fmt,
1143 namelength, namelength, (*get_userid)(pp->ki_ruid),
1145 pp->ki_pri.pri_level - PZERO,
1147 format_k2(PROCSIZE(pp)),
1148 format_k2(pagetok(pp->ki_rssize)),
1149 swaplength, swaplength, swap_buf,
1152 format_time(cputime),
1153 ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct,
1154 screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0,
1159 /* return the result */
1164 getsysctl(const char *name, void *ptr, size_t len)
1168 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
1169 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name,
1174 fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n",
1175 name, (unsigned long)len, (unsigned long)nlen);
1181 format_nice(const struct kinfo_proc *pp)
1183 const char *fifo, *kproc;
1185 static char nicebuf[4 + 1];
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)) {
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).
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.
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);
1219 if (pp->ki_flag & P_KPROC)
1221 snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO);
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);
1236 /* comparison routines for qsort */
1239 compare_pid(const void *p1, const void *p2)
1241 const struct kinfo_proc * const *pp1 = p1;
1242 const struct kinfo_proc * const *pp2 = p2;
1244 if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0)
1247 return ((*pp1)->ki_pid - (*pp2)->ki_pid);
1251 compare_tid(const void *p1, const void *p2)
1253 const struct kinfo_proc * const *pp1 = p1;
1254 const struct kinfo_proc * const *pp2 = p2;
1256 if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0)
1259 return ((*pp1)->ki_tid - (*pp2)->ki_tid);
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.
1273 static int sorted_state[] = {
1276 1, /* ABANDONED (WAIT) */
1284 #define ORDERKEY_PCTCPU(a, b) do { \
1287 diff = weighted_cpu(PCTCPU((b)), (b)) - \
1288 weighted_cpu(PCTCPU((a)), (a)); \
1290 diff = PCTCPU((b)) - PCTCPU((a)); \
1292 return (diff > 0 ? 1 : -1); \
1295 #define ORDERKEY_CPTICKS(a, b) do { \
1296 int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \
1298 return (diff > 0 ? 1 : -1); \
1301 #define ORDERKEY_STATE(a, b) do { \
1302 int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \
1304 return (diff > 0 ? 1 : -1); \
1307 #define ORDERKEY_PRIO(a, b) do { \
1308 int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \
1310 return (diff > 0 ? 1 : -1); \
1313 #define ORDERKEY_THREADS(a, b) do { \
1314 int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \
1316 return (diff > 0 ? 1 : -1); \
1319 #define ORDERKEY_RSSIZE(a, b) do { \
1320 long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \
1322 return (diff > 0 ? 1 : -1); \
1325 #define ORDERKEY_MEM(a, b) do { \
1326 long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \
1328 return (diff > 0 ? 1 : -1); \
1331 #define ORDERKEY_JID(a, b) do { \
1332 int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \
1334 return (diff > 0 ? 1 : -1); \
1337 #define ORDERKEY_SWAP(a, b) do { \
1338 int diff = (int)ki_swap(b) - (int)ki_swap(a); \
1340 return (diff > 0 ? 1 : -1); \
1343 /* compare_cpu - the comparison function for sorting by cpu percentage */
1347 compare_cpu(void *arg1, void *arg2)
1349 proc_compare(void *arg1, void *arg2)
1352 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1353 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1366 /* "cpu" compare routines */
1367 int compare_size(), compare_res(), compare_time(), compare_prio(),
1371 * "io" compare routines. Context switches aren't i/o, but are displayed
1372 * on the "io" display.
1374 int compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(),
1375 compare_vcsw(), compare_ivcsw();
1377 int (*compares[])() = {
1395 /* compare_size - the comparison function for sorting by total memory usage */
1398 compare_size(void *arg1, void *arg2)
1400 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1401 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1413 /* compare_res - the comparison function for sorting by resident set size */
1416 compare_res(void *arg1, void *arg2)
1418 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1419 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1431 /* compare_time - the comparison function for sorting by total cpu time */
1434 compare_time(void *arg1, void *arg2)
1436 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1437 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1449 /* compare_prio - the comparison function for sorting by priority */
1452 compare_prio(void *arg1, void *arg2)
1454 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1455 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1467 /* compare_threads - the comparison function for sorting by threads */
1469 compare_threads(void *arg1, void *arg2)
1471 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1472 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1485 /* compare_jid - the comparison function for sorting by jid */
1487 compare_jid(const void *arg1, const void *arg2)
1489 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1490 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1503 /* compare_swap - the comparison function for sorting by swap */
1505 compare_swap(const void *arg1, const void *arg2)
1507 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1508 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
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);
1522 /* assorted comparison functions for sorting by i/o */
1526 compare_iototal(void *arg1, void *arg2)
1528 io_compare(void *arg1, void *arg2)
1531 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1532 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1534 return (get_io_total(p2) - get_io_total(p1));
1539 compare_ioread(void *arg1, void *arg2)
1541 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1542 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1543 long dummy, inp1, inp2;
1545 (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy);
1546 (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy);
1548 return (inp2 - inp1);
1552 compare_iowrite(void *arg1, void *arg2)
1554 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1555 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1556 long dummy, oup1, oup2;
1558 (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy);
1559 (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy);
1561 return (oup2 - oup1);
1565 compare_iofault(void *arg1, void *arg2)
1567 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1568 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1569 long dummy, flp1, flp2;
1571 (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy);
1572 (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy);
1574 return (flp2 - flp1);
1578 compare_vcsw(void *arg1, void *arg2)
1580 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1581 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1582 long dummy, flp1, flp2;
1584 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy);
1585 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy);
1587 return (flp2 - flp1);
1591 compare_ivcsw(void *arg1, void *arg2)
1593 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1594 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1595 long dummy, flp1, flp2;
1597 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1);
1598 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2);
1600 return (flp2 - flp1);
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.
1618 struct kinfo_proc **prefp;
1619 struct kinfo_proc *pp;
1623 while (--cnt >= 0) {
1625 if (pp->ki_pid == (pid_t)pid)
1626 return ((int)pp->ki_ruid);
1632 swapmode(int *retavail, int *retfree)
1635 int pagesize = getpagesize();
1636 struct kvm_swap swapary[1];
1641 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
1643 n = kvm_getswapinfo(kd, swapary, 1, 0);
1644 if (n < 0 || swapary[0].ksw_total == 0)
1647 *retavail = CONVERT(swapary[0].ksw_total);
1648 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
1650 n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total);