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 */
73 static int cmdlengthdelta;
75 /* Prototypes for top internals */
78 /* get_process_info passes back a handle. This is what it looks like: */
81 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
82 int remaining; /* number of pointers remaining */
85 /* declarations for load_avg */
88 /* define what weighted cpu is. */
89 #define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \
90 ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu))))
92 /* what we consider to be process size: */
93 #define PROCSIZE(pp) ((pp)->ki_size / 1024)
95 #define RU(pp) (&(pp)->ki_rusage)
97 (RU(pp)->ru_inblock + RU(pp)->ru_oublock + RU(pp)->ru_majflt)
99 #define PCTCPU(pp) (pcpu[pp - pbase])
101 /* definitions for indices in the nlist array */
104 * These definitions control the format of the per-process area
107 static char io_header[] =
108 " PID%*s %-*.*s VCSW IVCSW READ WRITE FAULT TOTAL PERCENT COMMAND";
110 #define io_Proc_format \
111 "%5d%*s %-*.*s %6ld %6ld %6ld %6ld %6ld %6ld %6.2f%% %.*s"
113 static char smp_header_thr[] =
114 " PID%*s %-*.*s THR PRI NICE SIZE RES STATE C TIME %7s COMMAND";
115 static char smp_header[] =
116 " PID%*s %-*.*s " "PRI NICE SIZE RES STATE C TIME %7s COMMAND";
118 #define smp_Proc_format \
119 "%5d%*s %-*.*s %s%3d %4s%7s %6s %-6.6s %2d%7s %6.2f%% %.*s"
121 static char up_header_thr[] =
122 " PID%*s %-*.*s THR PRI NICE SIZE RES STATE TIME %7s COMMAND";
123 static char up_header[] =
124 " PID%*s %-*.*s " "PRI NICE SIZE RES STATE TIME %7s COMMAND";
126 #define up_Proc_format \
127 "%5d%*s %-*.*s %s%3d %4s%7s %6s %-6.6s%.0d%7s %6.2f%% %.*s"
130 /* process state names for the "STATE" column of the display */
131 /* the extra nulls in the string "run" are for adding a slash and
132 the processor number when needed */
134 char *state_abbrev[] = {
135 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB", "WAIT", "LOCK"
141 /* values that we stash away in _init and use in later routines */
143 static double logcpu;
145 /* these are retrieved from the kernel in _init */
147 static load_avg ccpu;
149 /* these are used in the get_ functions */
153 /* these are for calculating cpu state percentages */
155 static long cp_time[CPUSTATES];
156 static long cp_old[CPUSTATES];
157 static long cp_diff[CPUSTATES];
159 /* these are for detailing the process states */
161 int process_states[8];
162 char *procstatenames[] = {
163 "", " starting, ", " running, ", " sleeping, ", " stopped, ",
164 " zombie, ", " waiting, ", " lock, ",
168 /* these are for detailing the cpu states */
170 int cpu_states[CPUSTATES];
171 char *cpustatenames[] = {
172 "user", "nice", "system", "interrupt", "idle", NULL
175 /* these are for detailing the memory statistics */
178 char *memorynames[] = {
179 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ",
185 "K Total, ", "K MFU, ", "K MRU, ", "K Anon, ", "K Header, ", "K Other",
190 char *swapnames[] = {
191 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
196 /* these are for keeping track of the proc array */
199 static int onproc = -1;
201 static struct kinfo_proc *pbase;
202 static struct kinfo_proc **pref;
203 static struct kinfo_proc *previous_procs;
204 static struct kinfo_proc **previous_pref;
205 static int previous_proc_count = 0;
206 static int previous_proc_count_max = 0;
207 static int previous_thread;
209 /* data used for recalculating pctcpu */
211 static struct timespec proc_uptime;
212 static struct timeval proc_wall_time;
213 static struct timeval previous_wall_time;
214 static uint64_t previous_interval = 0;
216 /* total number of io operations */
217 static long total_inblock;
218 static long total_oublock;
219 static long total_majflt;
221 /* these are for getting the memory statistics */
223 static int arc_enabled;
224 static int pageshift; /* log base 2 of the pagesize */
226 /* define pagetok in terms of pageshift */
228 #define pagetok(size) ((size) << pageshift)
230 /* useful externals */
235 * Sorting orders. The first element is the default.
237 char *ordernames[] = {
238 "cpu", "size", "res", "time", "pri", "threads",
239 "total", "read", "write", "fault", "vcsw", "ivcsw",
244 /* Per-cpu time states */
248 static u_long cpumask;
250 static long *pcpu_cp_time;
251 static long *pcpu_cp_old;
252 static long *pcpu_cp_diff;
253 static int *pcpu_cpu_states;
255 static int compare_jid(const void *a, const void *b);
256 static int compare_pid(const void *a, const void *b);
257 static int compare_tid(const void *a, const void *b);
258 static const char *format_nice(const struct kinfo_proc *pp);
259 static void getsysctl(const char *name, void *ptr, size_t len);
260 static int swapmode(int *retavail, int *retfree);
261 static void update_layout(void);
264 toggle_pcpustats(void)
272 /* Adjust display based on ncpus and the ARC state. */
279 y_swap = 4 + arc_enabled;
280 y_idlecursor = 5 + arc_enabled;
281 y_message = 5 + arc_enabled;
282 y_header = 6 + arc_enabled;
283 y_procs = 7 + arc_enabled;
284 Header_lines = 7 + arc_enabled;
290 y_idlecursor += ncpus - 1;
291 y_message += ncpus - 1;
292 y_header += ncpus - 1;
293 y_procs += ncpus - 1;
294 Header_lines += ncpus - 1;
299 machine_init(struct statics *statics, char do_unames)
301 int i, j, empty, pagesize;
306 size = sizeof(smpmode);
307 if ((sysctlbyname("machdep.smp_active", &smpmode, &size,
309 sysctlbyname("kern.smp.active", &smpmode, &size,
311 size != sizeof(smpmode))
314 size = sizeof(arc_size);
315 if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arc_size, &size,
316 NULL, 0) == 0 && arc_size != 0)
320 while ((pw = getpwent()) != NULL) {
321 if (strlen(pw->pw_name) > namelength)
322 namelength = strlen(pw->pw_name);
325 if (smpmode && namelength > SMPUNAMELEN)
326 namelength = SMPUNAMELEN;
327 else if (namelength > UPUNAMELEN)
328 namelength = UPUNAMELEN;
330 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
334 GETSYSCTL("kern.ccpu", ccpu);
336 /* this is used in calculating WCPU -- calculate it ahead of time */
337 logcpu = log(loaddouble(ccpu));
345 /* get the page size and calculate pageshift from it */
346 pagesize = getpagesize();
348 while (pagesize > 1) {
353 /* we only need the amount of log(2)1024 for our conversion */
354 pageshift -= LOG1024;
356 /* fill in the statics information */
357 statics->procstate_names = procstatenames;
358 statics->cpustate_names = cpustatenames;
359 statics->memory_names = memorynames;
361 statics->arc_names = arcnames;
363 statics->arc_names = NULL;
364 statics->swap_names = swapnames;
366 statics->order_names = ordernames;
369 /* Allocate state for per-CPU stats. */
372 GETSYSCTL("kern.smp.maxcpus", maxcpu);
373 size = sizeof(long) * maxcpu * CPUSTATES;
374 times = malloc(size);
376 err(1, "malloc %zd bytes", size);
377 if (sysctlbyname("kern.cp_times", times, &size, NULL, 0) == -1)
378 err(1, "sysctlbyname kern.cp_times");
379 pcpu_cp_time = calloc(1, size);
380 maxid = (size / CPUSTATES / sizeof(long)) - 1;
381 for (i = 0; i <= maxid; i++) {
383 for (j = 0; empty && j < CPUSTATES; j++) {
384 if (times[i * CPUSTATES + j] != 0)
388 cpumask |= (1ul << i);
392 size = sizeof(long) * ncpus * CPUSTATES;
393 pcpu_cp_old = calloc(1, size);
394 pcpu_cp_diff = calloc(1, size);
395 pcpu_cpu_states = calloc(1, size);
396 statics->ncpus = ncpus;
405 format_header(char *uname_field)
407 static char Header[128];
411 jidlength = TOP_JID_LEN + 1; /* +1 for extra left space. */
415 switch (displaymode) {
418 * The logic of picking the right header format seems reverse
419 * here because we only want to display a THR column when
420 * "thread mode" is off (and threads are not listed as
424 (ps.thread ? smp_header : smp_header_thr) :
425 (ps.thread ? up_header : up_header_thr);
426 snprintf(Header, sizeof(Header), prehead,
427 jidlength, ps.jail ? " JID" : "",
428 namelength, namelength, uname_field,
429 ps.wcpu ? "WCPU" : "CPU");
433 snprintf(Header, sizeof(Header), prehead,
434 jidlength, ps.jail ? " JID" : "",
435 namelength, namelength, uname_field);
438 cmdlengthdelta = strlen(Header) - 7;
442 static int swappgsin = -1;
443 static int swappgsout = -1;
444 extern struct timeval timeout;
448 get_system_info(struct system_info *si)
451 struct loadavg sysload;
453 struct timeval boottime;
454 uint64_t arc_stat, arc_stat2;
458 /* get the CPU stats */
459 size = (maxid + 1) * CPUSTATES * sizeof(long);
460 if (sysctlbyname("kern.cp_times", pcpu_cp_time, &size, NULL, 0) == -1)
461 err(1, "sysctlbyname kern.cp_times");
462 GETSYSCTL("kern.cp_time", cp_time);
463 GETSYSCTL("vm.loadavg", sysload);
464 GETSYSCTL("kern.lastpid", lastpid);
466 /* convert load averages to doubles */
467 for (i = 0; i < 3; i++)
468 si->load_avg[i] = (double)sysload.ldavg[i] / sysload.fscale;
470 /* convert cp_time counts to percentages */
471 for (i = j = 0; i <= maxid; i++) {
472 if ((cpumask & (1ul << i)) == 0)
474 percentages(CPUSTATES, &pcpu_cpu_states[j * CPUSTATES],
475 &pcpu_cp_time[j * CPUSTATES],
476 &pcpu_cp_old[j * CPUSTATES],
477 &pcpu_cp_diff[j * CPUSTATES]);
480 percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
482 /* sum memory & swap statistics */
484 static unsigned int swap_delay = 0;
485 static int swapavail = 0;
486 static int swapfree = 0;
487 static long bufspace = 0;
488 static int nspgsin, nspgsout;
490 GETSYSCTL("vfs.bufspace", bufspace);
491 GETSYSCTL("vm.stats.vm.v_active_count", memory_stats[0]);
492 GETSYSCTL("vm.stats.vm.v_inactive_count", memory_stats[1]);
493 GETSYSCTL("vm.stats.vm.v_wire_count", memory_stats[2]);
494 GETSYSCTL("vm.stats.vm.v_cache_count", memory_stats[3]);
495 GETSYSCTL("vm.stats.vm.v_free_count", memory_stats[5]);
496 GETSYSCTL("vm.stats.vm.v_swappgsin", nspgsin);
497 GETSYSCTL("vm.stats.vm.v_swappgsout", nspgsout);
498 /* convert memory stats to Kbytes */
499 memory_stats[0] = pagetok(memory_stats[0]);
500 memory_stats[1] = pagetok(memory_stats[1]);
501 memory_stats[2] = pagetok(memory_stats[2]);
502 memory_stats[3] = pagetok(memory_stats[3]);
503 memory_stats[4] = bufspace / 1024;
504 memory_stats[5] = pagetok(memory_stats[5]);
505 memory_stats[6] = -1;
513 /* compute differences between old and new swap statistic */
515 swap_stats[4] = pagetok(((nspgsin - swappgsin)));
516 swap_stats[5] = pagetok(((nspgsout - swappgsout)));
520 swappgsout = nspgsout;
522 /* call CPU heavy swapmode() only for changes */
523 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
524 swap_stats[3] = swapmode(&swapavail, &swapfree);
525 swap_stats[0] = swapavail;
526 swap_stats[1] = swapavail - swapfree;
527 swap_stats[2] = swapfree;
534 GETSYSCTL("kstat.zfs.misc.arcstats.size", arc_stat);
535 arc_stats[0] = arc_stat >> 10;
536 GETSYSCTL("vfs.zfs.mfu_size", arc_stat);
537 arc_stats[1] = arc_stat >> 10;
538 GETSYSCTL("vfs.zfs.mru_size", arc_stat);
539 arc_stats[2] = arc_stat >> 10;
540 GETSYSCTL("vfs.zfs.anon_size", arc_stat);
541 arc_stats[3] = arc_stat >> 10;
542 GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc_stat);
543 GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc_stat2);
544 arc_stats[4] = arc_stat + arc_stat2 >> 10;
545 GETSYSCTL("kstat.zfs.misc.arcstats.other_size", arc_stat);
546 arc_stats[5] = arc_stat >> 10;
550 /* set arrays and strings */
552 si->cpustates = pcpu_cpu_states;
555 si->cpustates = cpu_states;
558 si->memory = memory_stats;
559 si->swap = swap_stats;
563 si->last_pid = lastpid;
569 * Print how long system has been up.
570 * (Found by looking getting "boottime" from the kernel)
573 mib[1] = KERN_BOOTTIME;
574 size = sizeof(boottime);
575 if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
576 boottime.tv_sec != 0) {
577 si->boottime = boottime;
579 si->boottime.tv_sec = -1;
583 #define NOPROC ((void *)-1)
586 * We need to compare data from the old process entry with the new
588 * To facilitate doing this quickly we stash a pointer in the kinfo_proc
589 * structure to cache the mapping. We also use a negative cache pointer
590 * of NOPROC to avoid duplicate lookups.
591 * XXX: this could be done when the actual processes are fetched, we do
592 * it here out of laziness.
594 const struct kinfo_proc *
595 get_old_proc(struct kinfo_proc *pp)
597 struct kinfo_proc **oldpp, *oldp;
600 * If this is the first fetch of the kinfo_procs then we don't have
601 * any previous entries.
603 if (previous_proc_count == 0)
605 /* negative cache? */
606 if (pp->ki_udata == NOPROC)
609 if (pp->ki_udata != NULL)
610 return (pp->ki_udata);
613 * 1) look up based on pid.
614 * 2) compare process start.
615 * If we fail here, then setup a negative cache entry, otherwise
618 oldpp = bsearch(&pp, previous_pref, previous_proc_count,
619 sizeof(*previous_pref), ps.thread ? compare_tid : compare_pid);
621 pp->ki_udata = NOPROC;
625 if (bcmp(&oldp->ki_start, &pp->ki_start, sizeof(pp->ki_start)) != 0) {
626 pp->ki_udata = NOPROC;
634 * Return the total amount of IO done in blocks in/out and faults.
635 * store the values individually in the pointers passed in.
638 get_io_stats(struct kinfo_proc *pp, long *inp, long *oup, long *flp,
639 long *vcsw, long *ivcsw)
641 const struct kinfo_proc *oldp;
642 static struct kinfo_proc dummy;
645 oldp = get_old_proc(pp);
647 bzero(&dummy, sizeof(dummy));
650 *inp = RU(pp)->ru_inblock - RU(oldp)->ru_inblock;
651 *oup = RU(pp)->ru_oublock - RU(oldp)->ru_oublock;
652 *flp = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
653 *vcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
654 *ivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
656 (RU(pp)->ru_inblock - RU(oldp)->ru_inblock) +
657 (RU(pp)->ru_oublock - RU(oldp)->ru_oublock) +
658 (RU(pp)->ru_majflt - RU(oldp)->ru_majflt);
663 * If there was a previous update, use the delta in ki_runtime over
664 * the previous interval to calculate pctcpu. Otherwise, fall back
665 * to using the kernel's ki_pctcpu.
668 proc_calc_pctcpu(struct kinfo_proc *pp)
670 const struct kinfo_proc *oldp;
672 if (previous_interval != 0) {
673 oldp = get_old_proc(pp);
675 return ((double)(pp->ki_runtime - oldp->ki_runtime)
676 / previous_interval);
679 * If this process/thread was created during the previous
680 * interval, charge it's total runtime to the previous
683 else if (pp->ki_start.tv_sec > previous_wall_time.tv_sec ||
684 (pp->ki_start.tv_sec == previous_wall_time.tv_sec &&
685 pp->ki_start.tv_usec >= previous_wall_time.tv_usec))
686 return ((double)pp->ki_runtime / previous_interval);
688 return (pctdouble(pp->ki_pctcpu));
692 * Return true if this process has used any CPU time since the
696 proc_used_cpu(struct kinfo_proc *pp)
698 const struct kinfo_proc *oldp;
700 oldp = get_old_proc(pp);
702 return (PCTCPU(pp) != 0);
703 return (pp->ki_runtime != oldp->ki_runtime ||
704 RU(pp)->ru_nvcsw != RU(oldp)->ru_nvcsw ||
705 RU(pp)->ru_nivcsw != RU(oldp)->ru_nivcsw);
709 * Return the total number of block in/out and faults by a process.
712 get_io_total(struct kinfo_proc *pp)
716 return (get_io_stats(pp, &dummy, &dummy, &dummy, &dummy, &dummy));
719 static struct handle handle;
722 get_process_info(struct system_info *si, struct process_select *sel,
723 int (*compare)(const void *, const void *))
728 long p_inblock, p_oublock, p_majflt, p_vcsw, p_ivcsw;
731 struct kinfo_proc **prefp;
732 struct kinfo_proc *pp;
733 struct timespec previous_proc_uptime;
735 /* these are copied out of sel for speed */
745 * If thread state was toggled, don't cache the previous processes.
747 if (previous_thread != sel->thread)
749 previous_thread = sel->thread;
752 * Save the previous process info.
754 if (previous_proc_count_max < nproc) {
755 free(previous_procs);
756 previous_procs = malloc(nproc * sizeof(*previous_procs));
758 previous_pref = malloc(nproc * sizeof(*previous_pref));
759 if (previous_procs == NULL || previous_pref == NULL) {
760 (void) fprintf(stderr, "top: Out of memory.\n");
763 previous_proc_count_max = nproc;
766 for (i = 0; i < nproc; i++)
767 previous_pref[i] = &previous_procs[i];
768 bcopy(pbase, previous_procs, nproc * sizeof(*previous_procs));
769 qsort(previous_pref, nproc, sizeof(*previous_pref),
770 ps.thread ? compare_tid : compare_pid);
772 previous_proc_count = nproc;
773 previous_proc_uptime = proc_uptime;
774 previous_wall_time = proc_wall_time;
775 previous_interval = 0;
777 pbase = kvm_getprocs(kd, sel->thread ? KERN_PROC_ALL : KERN_PROC_PROC,
779 (void)gettimeofday(&proc_wall_time, NULL);
780 if (clock_gettime(CLOCK_UPTIME, &proc_uptime) != 0)
781 memset(&proc_uptime, 0, sizeof(proc_uptime));
782 else if (previous_proc_uptime.tv_sec != 0 &&
783 previous_proc_uptime.tv_nsec != 0) {
784 previous_interval = (proc_uptime.tv_sec -
785 previous_proc_uptime.tv_sec) * 1000000;
786 nsec = proc_uptime.tv_nsec - previous_proc_uptime.tv_nsec;
788 previous_interval -= 1000000;
791 previous_interval += nsec / 1000;
793 if (nproc > onproc) {
794 pref = realloc(pref, sizeof(*pref) * nproc);
795 pcpu = realloc(pcpu, sizeof(*pcpu) * nproc);
798 if (pref == NULL || pbase == NULL || pcpu == NULL) {
799 (void) fprintf(stderr, "top: Out of memory.\n");
802 /* get a pointer to the states summary array */
803 si->procstates = process_states;
805 /* set up flags which define what we are going to select */
806 show_idle = sel->idle;
807 show_jid = sel->jid != -1;
808 show_self = sel->self == -1;
809 show_system = sel->system;
810 show_uid = sel->uid != -1;
811 show_command = sel->command != NULL;
812 show_kidle = sel->kidle;
814 /* count up process states and get pointers to interesting procs */
820 memset((char *)process_states, 0, sizeof(process_states));
822 for (pp = pbase, i = 0; i < nproc; pp++, i++) {
824 if (pp->ki_stat == 0)
828 if (!show_self && pp->ki_pid == sel->self)
832 if (!show_system && (pp->ki_flag & P_SYSTEM))
833 /* skip system process */
836 p_io = get_io_stats(pp, &p_inblock, &p_oublock, &p_majflt,
838 total_inblock += p_inblock;
839 total_oublock += p_oublock;
840 total_majflt += p_majflt;
842 process_states[pp->ki_stat]++;
844 if (pp->ki_stat == SZOMB)
848 if (!show_kidle && pp->ki_tdflags & TDF_IDLETD)
849 /* skip kernel idle process */
852 PCTCPU(pp) = proc_calc_pctcpu(pp);
853 if (sel->thread && PCTCPU(pp) > 1.0)
855 if (displaymode == DISP_CPU && !show_idle &&
856 (!proc_used_cpu(pp) ||
857 pp->ki_stat == SSTOP || pp->ki_stat == SIDL))
858 /* skip idle or non-running processes */
861 if (displaymode == DISP_IO && !show_idle && p_io == 0)
862 /* skip processes that aren't doing I/O */
865 if (show_jid && pp->ki_jid != sel->jid)
866 /* skip proc. that don't belong to the selected JID */
869 if (show_uid && pp->ki_ruid != (uid_t)sel->uid)
870 /* skip proc. that don't belong to the selected UID */
877 /* if requested, sort the "interesting" processes */
879 qsort(pref, active_procs, sizeof(*pref), compare);
881 /* remember active and total counts */
882 si->p_total = total_procs;
883 si->p_active = pref_len = active_procs;
885 /* pass back a handle */
886 handle.next_proc = pref;
887 handle.remaining = active_procs;
888 return ((caddr_t)&handle);
891 static char fmt[512]; /* static area where result is built */
894 format_next_process(caddr_t handle, char *(*get_userid)(int), int flags)
896 struct kinfo_proc *pp;
897 const struct kinfo_proc *oldp;
903 struct rusage ru, *rup;
905 char *proc_fmt, thr_buf[6], jid_buf[TOP_JID_LEN + 1];
908 const int cmdlen = 128;
910 /* find and remember the next proc structure */
911 hp = (struct handle *)handle;
912 pp = *(hp->next_proc++);
915 /* get the process's command name */
916 if ((pp->ki_flag & P_INMEM) == 0) {
918 * Print swapped processes as <pname>
922 len = strlen(pp->ki_comm);
923 if (len > sizeof(pp->ki_comm) - 3)
924 len = sizeof(pp->ki_comm) - 3;
925 memmove(pp->ki_comm + 1, pp->ki_comm, len);
926 pp->ki_comm[0] = '<';
927 pp->ki_comm[len + 1] = '>';
928 pp->ki_comm[len + 2] = '\0';
932 * Convert the process's runtime from microseconds to seconds. This
933 * time includes the interrupt time although that is not wanted here.
934 * ps(1) is similarly sloppy.
936 cputime = (pp->ki_runtime + 500000) / 1000000;
938 /* calculate the base for cpu percentages */
941 /* generate "STATE" field */
942 switch (state = pp->ki_stat) {
944 if (smpmode && pp->ki_oncpu != NOCPU)
945 sprintf(status, "CPU%d", pp->ki_oncpu);
947 strcpy(status, "RUN");
950 if (pp->ki_kiflag & KI_LOCKBLOCK) {
951 sprintf(status, "*%.6s", pp->ki_lockname);
956 if (pp->ki_wmesg != NULL) {
957 sprintf(status, "%.6s", pp->ki_wmesg);
964 state < sizeof(state_abbrev) / sizeof(*state_abbrev))
965 sprintf(status, "%.6s", state_abbrev[state]);
967 sprintf(status, "?%5d", state);
971 cmdbuf = (char *)malloc(cmdlen + 1);
972 if (cmdbuf == NULL) {
973 warn("malloc(%d)", cmdlen + 1);
977 if (!(flags & FMT_SHOWARGS)) {
978 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
980 snprintf(cmdbuf, cmdlen, "%s{%s}", pp->ki_comm,
983 snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm);
986 if (pp->ki_flag & P_SYSTEM ||
987 pp->ki_args == NULL ||
988 (args = kvm_getargv(kd, pp, cmdlen)) == NULL ||
990 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
992 snprintf(cmdbuf, cmdlen,
993 "[%s{%s}]", pp->ki_comm, pp->ki_tdname);
995 snprintf(cmdbuf, cmdlen,
996 "[%s]", pp->ki_comm);
999 char *src, *dst, *argbuf;
1004 argbuflen = cmdlen * 4;
1005 argbuf = (char *)malloc(argbuflen + 1);
1006 if (argbuf == NULL) {
1007 warn("malloc(%zd)", argbuflen + 1);
1014 /* Extract cmd name from argv */
1015 cmd = strrchr(*args, '/');
1021 for (; (src = *args++) != NULL; ) {
1024 len = (argbuflen - (dst - argbuf) - 1) / 4;
1026 strlen(src) < len ? strlen(src) : len,
1027 VIS_NL | VIS_CSTYLE);
1028 while (*dst != '\0')
1030 if ((argbuflen - (dst - argbuf) - 1) / 4 > 0)
1031 *dst++ = ' '; /* add delimiting space */
1033 if (dst != argbuf && dst[-1] == ' ')
1037 if (strcmp(cmd, pp->ki_comm) != 0) {
1038 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1040 snprintf(cmdbuf, cmdlen,
1041 "%s (%s){%s}", argbuf, pp->ki_comm,
1044 snprintf(cmdbuf, cmdlen,
1045 "%s (%s)", argbuf, pp->ki_comm);
1047 if (ps.thread && pp->ki_flag & P_HADTHREADS &&
1049 snprintf(cmdbuf, cmdlen,
1050 "%s{%s}", argbuf, pp->ki_tdname);
1052 strlcpy(cmdbuf, argbuf, cmdlen);
1061 snprintf(jid_buf, sizeof(jid_buf), "%*d",
1062 jidlength - 1, pp->ki_jid);
1064 if (displaymode == DISP_IO) {
1065 oldp = get_old_proc(pp);
1067 ru.ru_inblock = RU(pp)->ru_inblock -
1068 RU(oldp)->ru_inblock;
1069 ru.ru_oublock = RU(pp)->ru_oublock -
1070 RU(oldp)->ru_oublock;
1071 ru.ru_majflt = RU(pp)->ru_majflt - RU(oldp)->ru_majflt;
1072 ru.ru_nvcsw = RU(pp)->ru_nvcsw - RU(oldp)->ru_nvcsw;
1073 ru.ru_nivcsw = RU(pp)->ru_nivcsw - RU(oldp)->ru_nivcsw;
1078 p_tot = rup->ru_inblock + rup->ru_oublock + rup->ru_majflt;
1079 s_tot = total_inblock + total_oublock + total_majflt;
1081 snprintf(fmt, sizeof(fmt), io_Proc_format,
1084 namelength, namelength, (*get_userid)(pp->ki_ruid),
1091 s_tot == 0 ? 0.0 : (p_tot * 100.0 / s_tot),
1092 screen_width > cmdlengthdelta ?
1093 screen_width - cmdlengthdelta : 0,
1101 /* format this entry */
1103 if (state == SRUN && pp->ki_oncpu != NOCPU)
1106 cpu = pp->ki_lastcpu;
1109 proc_fmt = smpmode ? smp_Proc_format : up_Proc_format;
1113 snprintf(thr_buf, sizeof(thr_buf), "%*d ",
1114 (int)(sizeof(thr_buf) - 2), pp->ki_numthreads);
1116 snprintf(fmt, sizeof(fmt), proc_fmt,
1119 namelength, namelength, (*get_userid)(pp->ki_ruid),
1121 pp->ki_pri.pri_level - PZERO,
1123 format_k2(PROCSIZE(pp)),
1124 format_k2(pagetok(pp->ki_rssize)),
1127 format_time(cputime),
1128 ps.wcpu ? 100.0 * weighted_cpu(pct, pp) : 100.0 * pct,
1129 screen_width > cmdlengthdelta ? screen_width - cmdlengthdelta : 0,
1134 /* return the result */
1139 getsysctl(const char *name, void *ptr, size_t len)
1143 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
1144 fprintf(stderr, "top: sysctl(%s...) failed: %s\n", name,
1149 fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n",
1150 name, (unsigned long)len, (unsigned long)nlen);
1156 format_nice(const struct kinfo_proc *pp)
1158 const char *fifo, *kthread;
1160 static char nicebuf[4 + 1];
1162 fifo = PRI_NEED_RR(pp->ki_pri.pri_class) ? "" : "F";
1163 kthread = (pp->ki_flag & P_KTHREAD) ? "k" : "";
1164 switch (PRI_BASE(pp->ki_pri.pri_class)) {
1169 * XXX: the kernel doesn't tell us the original rtprio and
1170 * doesn't really know what it was, so to recover it we
1171 * must be more chummy with the implementation than the
1172 * implementation is with itself. pri_user gives a
1173 * constant "base" priority, but is only initialized
1174 * properly for user threads. pri_native gives what the
1175 * kernel calls the "base" priority, but it isn't constant
1176 * since it is changed by priority propagation. pri_native
1177 * also isn't properly initialized for all threads, but it
1178 * is properly initialized for kernel realtime and idletime
1179 * threads. Thus we use pri_user for the base priority of
1180 * user threads (it is always correct) and pri_native for
1181 * the base priority of kernel realtime and idletime threads
1182 * (there is nothing better, and it is usually correct).
1184 * The field width and thus the buffer are too small for
1185 * values like "kr31F", but such values shouldn't occur,
1186 * and if they do then the tailing "F" is not displayed.
1188 rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native :
1189 pp->ki_pri.pri_user) - PRI_MIN_REALTIME;
1190 snprintf(nicebuf, sizeof(nicebuf), "%sr%d%s",
1191 kthread, rtpri, fifo);
1194 if (pp->ki_flag & P_KTHREAD)
1196 snprintf(nicebuf, sizeof(nicebuf), "%d", pp->ki_nice - NZERO);
1199 /* XXX: as above. */
1200 rtpri = ((pp->ki_flag & P_KTHREAD) ? pp->ki_pri.pri_native :
1201 pp->ki_pri.pri_user) - PRI_MIN_IDLE;
1202 snprintf(nicebuf, sizeof(nicebuf), "%si%d%s",
1203 kthread, rtpri, fifo);
1211 /* comparison routines for qsort */
1214 compare_pid(const void *p1, const void *p2)
1216 const struct kinfo_proc * const *pp1 = p1;
1217 const struct kinfo_proc * const *pp2 = p2;
1219 if ((*pp2)->ki_pid < 0 || (*pp1)->ki_pid < 0)
1222 return ((*pp1)->ki_pid - (*pp2)->ki_pid);
1226 compare_tid(const void *p1, const void *p2)
1228 const struct kinfo_proc * const *pp1 = p1;
1229 const struct kinfo_proc * const *pp2 = p2;
1231 if ((*pp2)->ki_tid < 0 || (*pp1)->ki_tid < 0)
1234 return ((*pp1)->ki_tid - (*pp2)->ki_tid);
1238 * proc_compare - comparison function for "qsort"
1239 * Compares the resource consumption of two processes using five
1240 * distinct keys. The keys (in descending order of importance) are:
1241 * percent cpu, cpu ticks, state, resident set size, total virtual
1242 * memory usage. The process states are ordered as follows (from least
1243 * to most important): WAIT, zombie, sleep, stop, start, run. The
1244 * array declaration below maps a process state index into a number
1245 * that reflects this ordering.
1248 static int sorted_state[] = {
1251 1, /* ABANDONED (WAIT) */
1259 #define ORDERKEY_PCTCPU(a, b) do { \
1262 diff = weighted_cpu(PCTCPU((b)), (b)) - \
1263 weighted_cpu(PCTCPU((a)), (a)); \
1265 diff = PCTCPU((b)) - PCTCPU((a)); \
1267 return (diff > 0 ? 1 : -1); \
1270 #define ORDERKEY_CPTICKS(a, b) do { \
1271 int64_t diff = (int64_t)(b)->ki_runtime - (int64_t)(a)->ki_runtime; \
1273 return (diff > 0 ? 1 : -1); \
1276 #define ORDERKEY_STATE(a, b) do { \
1277 int diff = sorted_state[(b)->ki_stat] - sorted_state[(a)->ki_stat]; \
1279 return (diff > 0 ? 1 : -1); \
1282 #define ORDERKEY_PRIO(a, b) do { \
1283 int diff = (int)(b)->ki_pri.pri_level - (int)(a)->ki_pri.pri_level; \
1285 return (diff > 0 ? 1 : -1); \
1288 #define ORDERKEY_THREADS(a, b) do { \
1289 int diff = (int)(b)->ki_numthreads - (int)(a)->ki_numthreads; \
1291 return (diff > 0 ? 1 : -1); \
1294 #define ORDERKEY_RSSIZE(a, b) do { \
1295 long diff = (long)(b)->ki_rssize - (long)(a)->ki_rssize; \
1297 return (diff > 0 ? 1 : -1); \
1300 #define ORDERKEY_MEM(a, b) do { \
1301 long diff = (long)PROCSIZE((b)) - (long)PROCSIZE((a)); \
1303 return (diff > 0 ? 1 : -1); \
1306 #define ORDERKEY_JID(a, b) do { \
1307 int diff = (int)(b)->ki_jid - (int)(a)->ki_jid; \
1309 return (diff > 0 ? 1 : -1); \
1312 /* compare_cpu - the comparison function for sorting by cpu percentage */
1316 compare_cpu(void *arg1, void *arg2)
1318 proc_compare(void *arg1, void *arg2)
1321 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1322 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1324 ORDERKEY_PCTCPU(p1, p2);
1325 ORDERKEY_CPTICKS(p1, p2);
1326 ORDERKEY_STATE(p1, p2);
1327 ORDERKEY_PRIO(p1, p2);
1328 ORDERKEY_RSSIZE(p1, p2);
1329 ORDERKEY_MEM(p1, p2);
1335 /* "cpu" compare routines */
1336 int compare_size(), compare_res(), compare_time(), compare_prio(),
1340 * "io" compare routines. Context switches aren't i/o, but are displayed
1341 * on the "io" display.
1343 int compare_iototal(), compare_ioread(), compare_iowrite(), compare_iofault(),
1344 compare_vcsw(), compare_ivcsw();
1346 int (*compares[])() = {
1363 /* compare_size - the comparison function for sorting by total memory usage */
1366 compare_size(void *arg1, void *arg2)
1368 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1369 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1371 ORDERKEY_MEM(p1, p2);
1372 ORDERKEY_RSSIZE(p1, p2);
1373 ORDERKEY_PCTCPU(p1, p2);
1374 ORDERKEY_CPTICKS(p1, p2);
1375 ORDERKEY_STATE(p1, p2);
1376 ORDERKEY_PRIO(p1, p2);
1381 /* compare_res - the comparison function for sorting by resident set size */
1384 compare_res(void *arg1, void *arg2)
1386 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1387 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1389 ORDERKEY_RSSIZE(p1, p2);
1390 ORDERKEY_MEM(p1, p2);
1391 ORDERKEY_PCTCPU(p1, p2);
1392 ORDERKEY_CPTICKS(p1, p2);
1393 ORDERKEY_STATE(p1, p2);
1394 ORDERKEY_PRIO(p1, p2);
1399 /* compare_time - the comparison function for sorting by total cpu time */
1402 compare_time(void *arg1, void *arg2)
1404 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1405 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1407 ORDERKEY_CPTICKS(p1, p2);
1408 ORDERKEY_PCTCPU(p1, p2);
1409 ORDERKEY_STATE(p1, p2);
1410 ORDERKEY_PRIO(p1, p2);
1411 ORDERKEY_RSSIZE(p1, p2);
1412 ORDERKEY_MEM(p1, p2);
1417 /* compare_prio - the comparison function for sorting by priority */
1420 compare_prio(void *arg1, void *arg2)
1422 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1423 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1425 ORDERKEY_PRIO(p1, p2);
1426 ORDERKEY_CPTICKS(p1, p2);
1427 ORDERKEY_PCTCPU(p1, p2);
1428 ORDERKEY_STATE(p1, p2);
1429 ORDERKEY_RSSIZE(p1, p2);
1430 ORDERKEY_MEM(p1, p2);
1435 /* compare_threads - the comparison function for sorting by threads */
1437 compare_threads(void *arg1, void *arg2)
1439 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1440 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1442 ORDERKEY_THREADS(p1, p2);
1443 ORDERKEY_PCTCPU(p1, p2);
1444 ORDERKEY_CPTICKS(p1, p2);
1445 ORDERKEY_STATE(p1, p2);
1446 ORDERKEY_PRIO(p1, p2);
1447 ORDERKEY_RSSIZE(p1, p2);
1448 ORDERKEY_MEM(p1, p2);
1453 /* compare_jid - the comparison function for sorting by jid */
1455 compare_jid(const void *arg1, const void *arg2)
1457 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1458 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1460 ORDERKEY_JID(p1, p2);
1461 ORDERKEY_PCTCPU(p1, p2);
1462 ORDERKEY_CPTICKS(p1, p2);
1463 ORDERKEY_STATE(p1, p2);
1464 ORDERKEY_PRIO(p1, p2);
1465 ORDERKEY_RSSIZE(p1, p2);
1466 ORDERKEY_MEM(p1, p2);
1472 /* assorted comparison functions for sorting by i/o */
1476 compare_iototal(void *arg1, void *arg2)
1478 io_compare(void *arg1, void *arg2)
1481 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1482 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1484 return (get_io_total(p2) - get_io_total(p1));
1489 compare_ioread(void *arg1, void *arg2)
1491 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1492 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1493 long dummy, inp1, inp2;
1495 (void) get_io_stats(p1, &inp1, &dummy, &dummy, &dummy, &dummy);
1496 (void) get_io_stats(p2, &inp2, &dummy, &dummy, &dummy, &dummy);
1498 return (inp2 - inp1);
1502 compare_iowrite(void *arg1, void *arg2)
1504 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1505 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1506 long dummy, oup1, oup2;
1508 (void) get_io_stats(p1, &dummy, &oup1, &dummy, &dummy, &dummy);
1509 (void) get_io_stats(p2, &dummy, &oup2, &dummy, &dummy, &dummy);
1511 return (oup2 - oup1);
1515 compare_iofault(void *arg1, void *arg2)
1517 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1518 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1519 long dummy, flp1, flp2;
1521 (void) get_io_stats(p1, &dummy, &dummy, &flp1, &dummy, &dummy);
1522 (void) get_io_stats(p2, &dummy, &dummy, &flp2, &dummy, &dummy);
1524 return (flp2 - flp1);
1528 compare_vcsw(void *arg1, void *arg2)
1530 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1531 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1532 long dummy, flp1, flp2;
1534 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &flp1, &dummy);
1535 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &flp2, &dummy);
1537 return (flp2 - flp1);
1541 compare_ivcsw(void *arg1, void *arg2)
1543 struct kinfo_proc *p1 = *(struct kinfo_proc **)arg1;
1544 struct kinfo_proc *p2 = *(struct kinfo_proc **)arg2;
1545 long dummy, flp1, flp2;
1547 (void) get_io_stats(p1, &dummy, &dummy, &dummy, &dummy, &flp1);
1548 (void) get_io_stats(p2, &dummy, &dummy, &dummy, &dummy, &flp2);
1550 return (flp2 - flp1);
1555 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
1556 * the process does not exist.
1557 * It is EXTREMELY IMPORTANT that this function work correctly.
1558 * If top runs setuid root (as in SVR4), then this function
1559 * is the only thing that stands in the way of a serious
1560 * security problem. It validates requests for the "kill"
1561 * and "renice" commands.
1568 struct kinfo_proc **prefp;
1569 struct kinfo_proc *pp;
1573 while (--cnt >= 0) {
1575 if (pp->ki_pid == (pid_t)pid)
1576 return ((int)pp->ki_ruid);
1582 swapmode(int *retavail, int *retfree)
1585 int pagesize = getpagesize();
1586 struct kvm_swap swapary[1];
1591 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
1593 n = kvm_getswapinfo(kd, swapary, 1, 0);
1594 if (n < 0 || swapary[0].ksw_total == 0)
1597 *retavail = CONVERT(swapary[0].ksw_total);
1598 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
1600 n = (int)(swapary[0].ksw_used * 100.0 / swapary[0].ksw_total);