2 * top - a top users display for Unix
4 * SYNOPSIS: For FreeBSD-2.x and later
7 * Originally written for BSD4.4 system by Christos Zoulas.
8 * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider
9 * Order support hacked in from top-3.5beta6/machine/m_aix41.c
10 * by Monte Mitzelfelt (for latest top see http://www.groupsys.com/topinfo/)
12 * This is the machine-dependent module for FreeBSD 2.2
14 * FreeBSD 2.2.x, 3.x, 4.x, and probably FreeBSD 2.1.x
18 * AUTHOR: Christos Zoulas <christos@ee.cornell.edu>
19 * Steven Wallace <swallace@freebsd.org>
20 * Wolfram Schneider <wosch@FreeBSD.org>
21 * Hiten Pandya <hmp@backplane.com>
23 * $FreeBSD: src/usr.bin/top/machine.c,v 1.29.2.2 2001/07/31 20:27:05 tmm Exp $
24 * $DragonFly: src/usr.bin/top/machine.c,v 1.26 2008/10/16 01:52:33 swildner Exp $
29 #include <sys/types.h>
30 #include <sys/signal.h>
31 #include <sys/param.h>
40 #include <sys/errno.h>
41 #include <sys/sysctl.h>
45 #include <sys/vmmeter.h>
46 #include <sys/resource.h>
47 #include <sys/rtprio.h>
54 #include <osreldate.h> /* for changes in kernel structures */
56 #include <sys/kinfo.h>
63 static int check_nlist(struct nlist *);
64 static int getkval(unsigned long, int *, int, char *);
66 extern char* printable(char *);
67 int swapmode(int *retavail, int *retfree);
69 static int namelength;
73 * needs to be a global symbol, so wrapper can be
74 * modified accordingly.
76 static int show_threads = 0;
78 /* get_process_info passes back a handle. This is what it looks like: */
82 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
83 int remaining; /* number of pointers remaining */
86 /* declarations for load_avg */
89 #define PP(pp, field) ((pp)->kp_ ## field)
90 #define LP(pp, field) ((pp)->kp_lwp.kl_ ## field)
91 #define VP(pp, field) ((pp)->kp_vm_ ## field)
93 /* define what weighted cpu is. */
94 #define weighted_cpu(pct, pp) (PP((pp), swtime) == 0 ? 0.0 : \
95 ((pct) / (1.0 - exp(PP((pp), swtime) * logcpu))))
97 /* what we consider to be process size: */
98 #define PROCSIZE(pp) (VP((pp), map_size) / 1024)
101 * These definitions control the format of the per-process area
104 static char smp_header[] =
105 " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND";
107 #define smp_Proc_format \
108 "%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
110 static char up_header[] =
111 " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
113 #define up_Proc_format \
114 "%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s"
118 /* process state names for the "STATE" column of the display */
119 /* the extra nulls in the string "run" are for adding a slash and
120 the processor number when needed */
122 const char *state_abbrev[] =
124 "", "RUN\0\0\0", "STOP", "SLEEP",
130 /* values that we stash away in _init and use in later routines */
132 static double logcpu;
137 /* these are for calculating cpu state percentages */
139 static struct kinfo_cputime *cp_time, *cp_old;
141 /* these are for detailing the process states */
143 int process_states[6];
144 const char *procstatenames[] = {
145 "", " starting, ", " running, ", " sleeping, ", " stopped, ",
150 /* these are for detailing the cpu states */
153 const char *cpustatenames[CPU_STATES + 1] = {
154 "user", "nice", "system", "interrupt", "idle", NULL
157 /* these are for detailing the memory statistics */
160 const char *memorynames[] = {
161 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
166 const char *swapnames[] = {
168 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
173 /* these are for keeping track of the proc array */
176 static int onproc = -1;
178 static struct kinfo_proc *pbase;
179 static struct kinfo_proc **pref;
181 /* these are for getting the memory statistics */
183 static int pageshift; /* log base 2 of the pagesize */
185 /* define pagetok in terms of pageshift */
187 #define pagetok(size) ((size) << pageshift)
190 /* sorting orders. first is default */
191 const char *ordernames[] = {
192 "cpu", "size", "res", "time", "pri", "thr", NULL
197 cputime_percentages(int out[CPU_STATES], struct kinfo_cputime *new,
198 struct kinfo_cputime *old)
200 struct kinfo_cputime diffs;
201 uint64_t total_change, half_total;
206 diffs.cp_user = new->cp_user - old->cp_user;
207 diffs.cp_nice = new->cp_nice - old->cp_nice;
208 diffs.cp_sys = new->cp_sys - old->cp_sys;
209 diffs.cp_intr = new->cp_intr - old->cp_intr;
210 diffs.cp_idle = new->cp_idle - old->cp_idle;
211 total_change = diffs.cp_user + diffs.cp_nice + diffs.cp_sys +
212 diffs.cp_intr + diffs.cp_idle;
213 old->cp_user = new->cp_user;
214 old->cp_nice = new->cp_nice;
215 old->cp_sys = new->cp_sys;
216 old->cp_intr = new->cp_intr;
217 old->cp_idle = new->cp_idle;
219 /* avoid divide by zero potential */
220 if (total_change == 0)
223 /* calculate percentages based on overall change, rounding up */
224 half_total = total_change >> 1;
226 out[0] = ((diffs.cp_user * 1000LL + half_total) / total_change);
227 out[1] = ((diffs.cp_nice * 1000LL + half_total) / total_change);
228 out[2] = ((diffs.cp_sys * 1000LL + half_total) / total_change);
229 out[3] = ((diffs.cp_intr * 1000LL + half_total) / total_change);
230 out[4] = ((diffs.cp_idle * 1000LL + half_total) / total_change);
234 machine_init(struct statics *statics)
241 if (kinfo_get_cpus(&n_cpus))
242 err(1, "kinfo_get_cpus failed");
244 modelen = sizeof(smpmode);
245 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
246 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
247 modelen != sizeof(smpmode))
250 while ((pw = getpwent()) != NULL) {
251 if ((int)strlen(pw->pw_name) > namelength)
252 namelength = strlen(pw->pw_name);
256 if (smpmode && namelength > 13)
258 else if (namelength > 15)
261 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
264 if (kinfo_get_sched_ccpu(&ccpu)) {
265 fprintf(stderr, "top: kinfo_get_sched_ccpu failed\n");
269 /* this is used in calculating WCPU -- calculate it ahead of time */
270 logcpu = log(loaddouble(ccpu));
276 /* get the page size with "getpagesize" and calculate pageshift from it */
277 pagesize = getpagesize();
285 /* we only need the amount of log(2)1024 for our conversion */
286 pageshift -= LOG1024;
288 /* fill in the statics information */
289 statics->procstate_names = procstatenames;
290 statics->cpustate_names = cpustatenames;
291 statics->memory_names = memorynames;
292 statics->swap_names = swapnames;
294 statics->order_names = ordernames;
302 format_header(const char *uname_field)
304 static char Header[128];
306 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
307 namelength, namelength, uname_field);
309 if (screen_width <= 79)
314 cmdlength = cmdlength - strlen(Header) + 6;
319 static int swappgsin = -1;
320 static int swappgsout = -1;
321 extern struct timeval timeout;
324 get_system_info(struct system_info *si)
327 struct timeval boottime;
332 if (cpu_states == NULL) {
333 cpu_states = malloc(sizeof(*cpu_states) * CPU_STATES * n_cpus);
334 if (cpu_states == NULL)
336 bzero(cpu_states, sizeof(*cpu_states) * CPU_STATES * n_cpus);
338 if (cp_time == NULL) {
339 cp_time = malloc(2 * n_cpus * sizeof(cp_time[0]));
342 cp_old = cp_time + n_cpus;
344 len = n_cpus * sizeof(cp_old[0]);
346 if (sysctlbyname("kern.cputime", cp_old, &len, NULL, 0))
347 err(1, "kern.cputime");
350 len = n_cpus * sizeof(cp_time[0]);
352 if (sysctlbyname("kern.cputime", cp_time, &len, NULL, 0))
353 err(1, "kern.cputime");
355 getloadavg(si->load_avg, 3);
359 /* convert cp_time counts to percentages */
360 for (cpu = 0; cpu < n_cpus; ++cpu) {
361 cputime_percentages(cpu_states + cpu * CPU_STATES,
362 &cp_time[cpu], &cp_old[cpu]);
365 /* sum memory & swap statistics */
369 size_t vms_size = sizeof(vms);
370 size_t vmm_size = sizeof(vmm);
371 static unsigned int swap_delay = 0;
372 static int swapavail = 0;
373 static int swapfree = 0;
374 static int bufspace = 0;
376 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0))
377 err(1, "sysctlbyname: vm.vmstats");
379 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0))
380 err(1, "sysctlbyname: vm.vmmeter");
382 if (kinfo_get_vfs_bufspace(&bufspace))
383 err(1, "kinfo_get_vfs_bufspace");
385 /* convert memory stats to Kbytes */
386 memory_stats[0] = pagetok(vms.v_active_count);
387 memory_stats[1] = pagetok(vms.v_inactive_count);
388 memory_stats[2] = pagetok(vms.v_wire_count);
389 memory_stats[3] = pagetok(vms.v_cache_count);
390 memory_stats[4] = bufspace / 1024;
391 memory_stats[5] = pagetok(vms.v_free_count);
392 memory_stats[6] = -1;
400 /* compute differences between old and new swap statistic */
402 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin)));
403 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout)));
406 swappgsin = vmm.v_swappgsin;
407 swappgsout = vmm.v_swappgsout;
409 /* call CPU heavy swapmode() only for changes */
410 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
411 swap_stats[3] = swapmode(&swapavail, &swapfree);
412 swap_stats[0] = swapavail;
413 swap_stats[1] = swapavail - swapfree;
414 swap_stats[2] = swapfree;
420 /* set arrays and strings */
421 si->cpustates = cpu_states;
422 si->memory = memory_stats;
423 si->swap = swap_stats;
427 si->last_pid = lastpid;
433 * Print how long system has been up.
434 * (Found by looking getting "boottime" from the kernel)
437 mib[1] = KERN_BOOTTIME;
438 bt_size = sizeof(boottime);
439 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 &&
440 boottime.tv_sec != 0) {
441 si->boottime = boottime;
443 si->boottime.tv_sec = -1;
447 static struct handle handle;
449 caddr_t get_process_info(struct system_info *si, struct process_select *sel,
450 int (*compare)(const void *, const void *))
455 struct kinfo_proc **prefp;
456 struct kinfo_proc *pp;
458 /* these are copied out of sel for speed */
462 int show_only_threads;
467 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
469 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
471 if (pref == NULL || pbase == NULL) {
472 (void) fprintf(stderr, "top: Out of memory.\n");
475 /* get a pointer to the states summary array */
476 si->procstates = process_states;
478 /* set up flags which define what we are going to select */
479 show_idle = sel->idle;
480 show_self = sel->self;
481 show_system = sel->system;
482 show_threads = sel->threads || sel->only_threads;
483 show_only_threads = sel->only_threads;
484 show_uid = sel->uid != -1;
485 show_command = sel->command != NULL;
487 /* count up process states and get pointers to interesting procs */
490 memset((char *)process_states, 0, sizeof(process_states));
492 for (pp = pbase, i = 0; i < nproc; pp++, i++)
495 * Place pointers to each valid proc structure in pref[].
496 * Process slots that are actually in use have a non-zero
497 * status field. Processes with P_SYSTEM set are system
498 * processes---these get ignored unless show_sysprocs is set.
500 if ((show_threads && (LP(pp, pid) == -1)) ||
501 (!show_only_threads && (PP(pp, stat) != 0 &&
502 (show_self != PP(pp, pid)) &&
503 (show_system || ((PP(pp, flags) & P_SYSTEM) == 0)))))
506 process_states[(unsigned char) PP(pp, stat)]++;
507 if ((show_threads && (LP(pp, pid) == -1)) ||
508 (!show_only_threads && PP(pp, stat) != SZOMB &&
509 (show_idle || (LP(pp, pctcpu) != 0) ||
510 (LP(pp, stat) == LSRUN)) &&
511 (!show_uid || PP(pp, ruid) == (uid_t)sel->uid)))
519 /* if requested, sort the "interesting" processes */
522 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
525 /* remember active and total counts */
526 si->p_total = total_procs;
527 si->p_active = pref_len = active_procs;
529 /* pass back a handle */
530 handle.next_proc = pref;
531 handle.remaining = active_procs;
532 return((caddr_t)&handle);
535 char fmt[128]; /* static area where result is built */
538 format_next_process(caddr_t xhandle, char *(*get_userid)(long))
540 struct kinfo_proc *pp;
549 /* find and remember the next proc structure */
550 hp = (struct handle *)xhandle;
551 pp = *(hp->next_proc++);
554 /* set the wrapper for the process/thread name */
555 if ((PP(pp, flags) & P_SWAPPEDOUT))
556 wrapper = "[]"; /* swapped process [pname] */
557 else if (((PP(pp, flags) & P_SYSTEM) != 0) && (LP(pp, pid) > 0))
558 wrapper = "()"; /* system process (pname) */
559 else if (show_threads && (LP(pp, pid) == -1))
560 wrapper = "<>"; /* pure kernel threads <thread> */
564 /* get the process's command name */
565 if (wrapper != NULL) {
566 char *comm = PP(pp, comm);
567 #define COMSIZ sizeof(PP(pp, comm))
569 (void) strncpy(buf, comm, COMSIZ);
570 comm[0] = wrapper[0];
571 (void) strncpy(&comm[1], buf, COMSIZ - 2);
572 comm[COMSIZ - 2] = '\0';
573 (void) strncat(comm, &wrapper[1], COMSIZ - 1);
574 comm[COMSIZ - 1] = '\0';
578 * Convert the process's runtime from microseconds to seconds. This
579 * time includes the interrupt time although that is not wanted here.
580 * ps(1) is similarly sloppy.
582 cputime = (LP(pp, uticks) + LP(pp, sticks)) / 1000000;
584 /* calculate the base for cpu percentages */
585 pct = pctdouble(LP(pp, pctcpu));
587 /* generate "STATE" field */
588 switch (state = LP(pp, stat)) {
590 if (smpmode && LP(pp, tdflags) & TDF_RUNNING)
591 sprintf(status, "CPU%d", LP(pp, cpuid));
593 strcpy(status, "RUN");
596 if (LP(pp, wmesg) != NULL) {
597 sprintf(status, "%.6s", LP(pp, wmesg));
604 (unsigned)state < sizeof(state_abbrev) / sizeof(*state_abbrev))
605 sprintf(status, "%.6s", state_abbrev[(unsigned char) state]);
607 sprintf(status, "?%5d", state);
611 if (PP(pp, stat) == SZOMB)
612 strcpy(status, "ZOMB");
615 * idle time 0 - 31 -> nice value +21 - +52
616 * normal time -> nice value -20 - +20
617 * real time 0 - 31 -> nice value -52 - -21
618 * thread 0 - 31 -> nice value -53 -
620 switch(LP(pp, rtprio.type)) {
621 case RTP_PRIO_REALTIME:
622 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio);
625 xnice = PRIO_MAX + 1 + LP(pp, rtprio.prio);
627 case RTP_PRIO_THREAD:
628 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio);
631 xnice = PP(pp, nice);
635 /* format this entry */
636 snprintf(fmt, sizeof(fmt),
637 smpmode ? smp_Proc_format : up_Proc_format,
639 namelength, namelength,
640 get_userid(PP(pp, ruid)),
641 (int)((show_threads && (LP(pp, pid) == -1)) ?
642 LP(pp, tdprio) : LP(pp, prio)),
644 format_k2(PROCSIZE(pp)),
645 format_k2(pagetok(VP(pp, rssize))),
647 (int)(smpmode ? LP(pp, cpuid) : 0),
648 format_time(cputime),
649 100.0 * weighted_cpu(pct, pp),
652 printable(PP(pp, comm)));
654 /* return the result */
660 * check_nlist(nlst) - checks the nlist to see if any symbols were not
661 * found. For every symbol that was not found, a one-line
662 * message is printed to stderr. The routine returns the
663 * number of symbols NOT found.
666 check_nlist(struct nlist *nlst)
670 /* check to see if we got ALL the symbols we requested */
671 /* this will write one line to stderr for every symbol not found */
674 while (nlst->n_name != NULL)
676 if (nlst->n_type == 0)
678 /* this one wasn't found */
679 (void) fprintf(stderr, "kernel: no symbol named `%s'\n",
690 /* comparison routines for qsort */
693 * proc_compare - comparison function for "qsort"
694 * Compares the resource consumption of two processes using five
695 * distinct keys. The keys (in descending order of importance) are:
696 * percent cpu, cpu ticks, state, resident set size, total virtual
697 * memory usage. The process states are ordered as follows (from least
698 * to most important): WAIT, zombie, sleep, stop, start, run. The
699 * array declaration below maps a process state index into a number
700 * that reflects this ordering.
703 static unsigned char sorted_state[] =
707 1, /* ABANDONED (WAIT) */
715 #define ORDERKEY_PCTCPU \
716 if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \
717 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
719 #define CPTICKS(p) (LP(p, uticks) + LP(p, sticks))
721 #define ORDERKEY_CPTICKS \
722 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
723 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
725 #define ORDERKEY_STATE \
726 if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \
727 sorted_state[(unsigned char) PP(p1, stat)]) == 0)
729 #define ORDERKEY_PRIO \
730 if ((result = LP(p2, prio) - LP(p1, prio)) == 0)
732 #define ORDERKEY_KTHREADS \
733 if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0)
735 #define ORDERKEY_KTHREADS_PRIO \
736 if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0)
738 #define ORDERKEY_RSSIZE \
739 if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0)
741 #define ORDERKEY_MEM \
742 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
744 /* compare_cpu - the comparison function for sorting by cpu percentage */
748 compare_cpu(const void *arg1, const void *arg2)
750 proc_compare(const void *arg1, const void *arg2)
753 const struct proc *const*pp1 = arg1;
754 const struct proc *const*pp2 = arg2;
755 const struct kinfo_proc *p1;
756 const struct kinfo_proc *p2;
760 /* remove one level of indirection */
761 p1 = *(const struct kinfo_proc *const *) pp1;
762 p2 = *(const struct kinfo_proc *const *) pp2;
776 /* compare routines */
777 int compare_size(const void *, const void *);
778 int compare_res(const void *, const void *);
779 int compare_time(const void *, const void *);
780 int compare_prio(const void *, const void *);
781 int compare_thr(const void *, const void *);
783 int (*proc_compares[])(const void *, const void *) = {
793 /* compare_size - the comparison function for sorting by total memory usage */
796 compare_size(const void *arg1, const void *arg2)
798 struct proc *const *pp1 = arg1;
799 struct proc *const *pp2 = arg2;
800 struct kinfo_proc *p1;
801 struct kinfo_proc *p2;
805 /* remove one level of indirection */
806 p1 = *(struct kinfo_proc *const*) pp1;
807 p2 = *(struct kinfo_proc *const*) pp2;
820 /* compare_res - the comparison function for sorting by resident set size */
823 compare_res(const void *arg1, const void *arg2)
825 struct proc *const *pp1 = arg1;
826 struct proc *const *pp2 = arg2;
827 struct kinfo_proc *p1;
828 struct kinfo_proc *p2;
832 /* remove one level of indirection */
833 p1 = *(struct kinfo_proc *const*) pp1;
834 p2 = *(struct kinfo_proc *const*) pp2;
847 /* compare_time - the comparison function for sorting by total cpu time */
850 compare_time(const void *arg1, const void *arg2)
852 struct proc *const *pp1 = arg1;
853 struct proc *const *pp2 = arg2;
854 const struct kinfo_proc *p1;
855 const struct kinfo_proc *p2;
859 /* remove one level of indirection */
860 p1 = *(struct kinfo_proc *const*) pp1;
861 p2 = *(struct kinfo_proc *const*) pp2;
866 ORDERKEY_KTHREADS_PRIO
876 /* compare_prio - the comparison function for sorting by cpu percentage */
879 compare_prio(const void *arg1, const void *arg2)
881 struct proc *const *pp1 = arg1;
882 struct proc *const *pp2 = arg2;
883 const struct kinfo_proc *p1;
884 const struct kinfo_proc *p2;
888 /* remove one level of indirection */
889 p1 = *(struct kinfo_proc *const*) pp1;
890 p2 = *(struct kinfo_proc *const*) pp2;
893 ORDERKEY_KTHREADS_PRIO
906 compare_thr(const void *arg1, const void *arg2)
908 struct proc *const *pp1 = arg1;
909 struct proc *const *pp2 = arg2;
910 const struct kinfo_proc *p1;
911 const struct kinfo_proc *p2;
915 /* remove one level of indirection */
916 p1 = *(struct kinfo_proc *const*) pp1;
917 p2 = *(struct kinfo_proc *const*) pp2;
920 ORDERKEY_KTHREADS_PRIO
935 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
936 * the process does not exist.
937 * It is EXTREMLY IMPORTANT that this function work correctly.
938 * If top runs setuid root (as in SVR4), then this function
939 * is the only thing that stands in the way of a serious
940 * security problem. It validates requests for the "kill"
941 * and "renice" commands.
948 struct kinfo_proc **prefp;
949 struct kinfo_proc *pp;
956 if (PP(pp, pid) == (pid_t)pid)
958 return((int)PP(pp, ruid));
966 * swapmode is based on a program called swapinfo written
967 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
970 swapmode(int *retavail, int *retfree)
973 int pagesize = getpagesize();
974 struct kvm_swap swapary[1];
979 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
981 n = kvm_getswapinfo(kd, swapary, 1, 0);
982 if (n < 0 || swapary[0].ksw_total == 0)
985 *retavail = CONVERT(swapary[0].ksw_total);
986 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
988 n = (int)((double)swapary[0].ksw_used * 100.0 /
989 (double)swapary[0].ksw_total);