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>
22 * $FreeBSD: src/usr.bin/top/machine.c,v 1.29.2.2 2001/07/31 20:27:05 tmm Exp $
23 * $DragonFly: src/usr.bin/top/machine.c,v 1.8 2003/07/25 05:28:54 dillon Exp $
28 #include <sys/types.h>
29 #include <sys/signal.h>
30 #include <sys/param.h>
38 #include <sys/errno.h>
39 #include <sys/sysctl.h>
40 #include <sys/dkstat.h>
45 #include <sys/vmmeter.h>
46 #include <sys/resource.h>
47 #include <sys/rtprio.h>
53 #include <osreldate.h> /* for changes in kernel structures */
58 static int check_nlist __P((struct nlist *));
59 static int getkval __P((unsigned long, int *, int, char *));
60 extern char* printable __P((char *));
61 int swapmode __P((int *retavail, int *retfree));
63 static int namelength;
67 /* get_process_info passes back a handle. This is what it looks like: */
71 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
72 int remaining; /* number of pointers remaining */
75 /* declarations for load_avg */
78 #define PP(pp, field) ((pp)->kp_proc . field)
79 #define EP(pp, field) ((pp)->kp_eproc . field)
80 #define TP(pp, field) ((pp)->kp_thread . field)
81 #define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
83 /* define what weighted cpu is. */
84 #define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \
85 ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu))))
87 /* what we consider to be process size: */
88 #define PROCSIZE(pp) (VP((pp), vm_map.size) / 1024)
90 /* definitions for indices in the nlist array */
92 static struct nlist nlst[] = {
101 { "_bufspace" }, /* K in buffer cache */
110 * These definitions control the format of the per-process area
113 static char smp_header[] =
114 " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND";
116 #define smp_Proc_format \
117 "%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
119 static char up_header[] =
120 " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
122 #define up_Proc_format \
123 "%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s"
127 /* process state names for the "STATE" column of the display */
128 /* the extra nulls in the string "run" are for adding a slash and
129 the processor number when needed */
131 char *state_abbrev[] =
133 "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB",
139 /* values that we stash away in _init and use in later routines */
141 static double logcpu;
143 /* these are retrieved from the kernel in _init */
145 static load_avg ccpu;
147 /* these are offsets obtained via nlist and used in the get_ functions */
149 static unsigned long cp_time_offset;
150 static unsigned long avenrun_offset;
151 static unsigned long lastpid_offset;
153 static unsigned long bufspace_offset;
156 /* these are for calculating cpu state percentages */
158 static long cp_time[CPUSTATES];
159 static long cp_old[CPUSTATES];
160 static long cp_diff[CPUSTATES];
162 /* these are for detailing the process states */
164 int process_states[6];
165 char *procstatenames[] = {
166 "", " starting, ", " running, ", " sleeping, ", " stopped, ",
171 /* these are for detailing the cpu states */
173 int cpu_states[CPUSTATES];
174 char *cpustatenames[] = {
175 "user", "nice", "system", "interrupt", "idle", NULL
178 /* these are for detailing the memory statistics */
181 char *memorynames[] = {
182 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
187 char *swapnames[] = {
189 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
194 /* these are for keeping track of the proc array */
197 static int onproc = -1;
199 static struct kinfo_proc *pbase;
200 static struct kinfo_proc **pref;
202 /* these are for getting the memory statistics */
204 static int pageshift; /* log base 2 of the pagesize */
206 /* define pagetok in terms of pageshift */
208 #define pagetok(size) ((size) << pageshift)
210 /* useful externals */
214 /* sorting orders. first is default */
215 char *ordernames[] = {
216 "cpu", "size", "res", "time", "pri", NULL
221 machine_init(statics)
223 struct statics *statics;
227 register int pagesize;
231 modelen = sizeof(smpmode);
232 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
233 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
234 modelen != sizeof(smpmode))
237 while ((pw = getpwent()) != NULL) {
238 if (strlen(pw->pw_name) > namelength)
239 namelength = strlen(pw->pw_name);
243 if (smpmode && namelength > 13)
245 else if (namelength > 15)
248 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
252 /* get the list of symbols we want to access in the kernel */
253 (void) kvm_nlist(kd, nlst);
254 if (nlst[0].n_type == 0)
256 fprintf(stderr, "top: nlist failed\n");
260 /* make sure they were all found */
261 if (i > 0 && check_nlist(nlst) > 0)
266 (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu),
267 nlst[X_CCPU].n_name);
269 /* stash away certain offsets for later use */
270 cp_time_offset = nlst[X_CP_TIME].n_value;
271 avenrun_offset = nlst[X_AVENRUN].n_value;
272 lastpid_offset = nlst[X_LASTPID].n_value;
273 bufspace_offset = nlst[X_BUFSPACE].n_value;
275 /* this is used in calculating WCPU -- calculate it ahead of time */
276 logcpu = log(loaddouble(ccpu));
282 /* get the page size with "getpagesize" and calculate pageshift from it */
283 pagesize = getpagesize();
291 /* we only need the amount of log(2)1024 for our conversion */
292 pageshift -= LOG1024;
294 /* fill in the statics information */
295 statics->procstate_names = procstatenames;
296 statics->cpustate_names = cpustatenames;
297 statics->memory_names = memorynames;
298 statics->swap_names = swapnames;
300 statics->order_names = ordernames;
307 char *format_header(uname_field)
309 register char *uname_field;
313 static char Header[128];
315 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
316 namelength, namelength, uname_field);
318 cmdlength = 80 - strlen(Header) + 6;
323 static int swappgsin = -1;
324 static int swappgsout = -1;
325 extern struct timeval timeout;
330 struct system_info *si;
336 struct timeval boottime;
339 /* get the cp_time array */
340 (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
341 nlst[X_CP_TIME].n_name);
342 (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
343 nlst[X_AVENRUN].n_name);
345 (void) getkval(lastpid_offset, (int *)(&lastpid), sizeof(lastpid),
348 /* convert load averages to doubles */
351 register double *infoloadp;
355 struct loadavg sysload;
357 getkerninfo(KINFO_LOADAVG, &sysload, &size, 0);
360 infoloadp = si->load_avg;
362 for (i = 0; i < 3; i++)
365 *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
367 *infoloadp++ = loaddouble(*avenrunp++);
371 /* convert cp_time counts to percentages */
372 total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
374 /* sum memory & swap statistics */
378 int vms_size = sizeof(vms);
379 int vmm_size = sizeof(vmm);
380 static unsigned int swap_delay = 0;
381 static int swapavail = 0;
382 static int swapfree = 0;
383 static int bufspace = 0;
385 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0)) {
386 perror("sysctlbyname: vm.vmstats");
389 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
390 perror("sysctlbyname: vm.vmstats");
393 (void) getkval(bufspace_offset, (int *)(&bufspace), sizeof(bufspace),
396 /* convert memory stats to Kbytes */
397 memory_stats[0] = pagetok(vms.v_active_count);
398 memory_stats[1] = pagetok(vms.v_inactive_count);
399 memory_stats[2] = pagetok(vms.v_wire_count);
400 memory_stats[3] = pagetok(vms.v_cache_count);
401 memory_stats[4] = bufspace / 1024;
402 memory_stats[5] = pagetok(vms.v_free_count);
403 memory_stats[6] = -1;
411 /* compute differences between old and new swap statistic */
413 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin)));
414 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout)));
417 swappgsin = vmm.v_swappgsin;
418 swappgsout = vmm.v_swappgsout;
420 /* call CPU heavy swapmode() only for changes */
421 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
422 swap_stats[3] = swapmode(&swapavail, &swapfree);
423 swap_stats[0] = swapavail;
424 swap_stats[1] = swapavail - swapfree;
425 swap_stats[2] = swapfree;
431 /* set arrays and strings */
432 si->cpustates = cpu_states;
433 si->memory = memory_stats;
434 si->swap = swap_stats;
438 si->last_pid = lastpid;
444 * Print how long system has been up.
445 * (Found by looking getting "boottime" from the kernel)
448 mib[1] = KERN_BOOTTIME;
449 bt_size = sizeof(boottime);
450 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 &&
451 boottime.tv_sec != 0) {
452 si->boottime = boottime;
454 si->boottime.tv_sec = -1;
458 static struct handle handle;
460 caddr_t get_process_info(si, sel, compare)
462 struct system_info *si;
463 struct process_select *sel;
468 register int total_procs;
469 register int active_procs;
470 register struct kinfo_proc **prefp;
471 register struct kinfo_proc *pp;
473 /* these are copied out of sel for speed */
481 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
483 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
485 if (pref == NULL || pbase == NULL) {
486 (void) fprintf(stderr, "top: Out of memory.\n");
489 /* get a pointer to the states summary array */
490 si->procstates = process_states;
492 /* set up flags which define what we are going to select */
493 show_idle = sel->idle;
494 show_self = sel->self;
495 show_system = sel->system;
496 show_uid = sel->uid != -1;
497 show_command = sel->command != NULL;
499 /* count up process states and get pointers to interesting procs */
502 memset((char *)process_states, 0, sizeof(process_states));
504 for (pp = pbase, i = 0; i < nproc; pp++, i++)
507 * Place pointers to each valid proc structure in pref[].
508 * Process slots that are actually in use have a non-zero
509 * status field. Processes with P_SYSTEM set are system
510 * processes---these get ignored unless show_sysprocs is set.
512 if (PP(pp, p_stat) != 0 &&
513 (show_self != PP(pp, p_pid)) &&
514 (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0)))
517 process_states[(unsigned char) PP(pp, p_stat)]++;
518 if ((PP(pp, p_stat) != SZOMB) &&
519 (show_idle || (PP(pp, p_pctcpu) != 0) ||
520 (PP(pp, p_stat) == SRUN)) &&
521 (!show_uid || EP(pp, e_ucred.cr_ruid) == (uid_t)sel->uid))
529 /* if requested, sort the "interesting" processes */
532 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
535 /* remember active and total counts */
536 si->p_total = total_procs;
537 si->p_active = pref_len = active_procs;
539 /* pass back a handle */
540 handle.next_proc = pref;
541 handle.remaining = active_procs;
542 return((caddr_t)&handle);
545 char fmt[128]; /* static area where result is built */
547 char *format_next_process(handle, get_userid)
550 char *(*get_userid)();
553 struct kinfo_proc *pp;
561 /* find and remember the next proc structure */
562 hp = (struct handle *)handle;
563 pp = *(hp->next_proc++);
566 /* get the process's command name */
567 if ((PP(pp, p_flag) & P_INMEM) == 0) {
569 * Print swapped processes as <pname>
571 char *comm = TP(pp, td_comm);
572 #define COMSIZ sizeof(TP(pp, td_comm))
574 (void) strncpy(buf, comm, COMSIZ);
576 (void) strncpy(&comm[1], buf, COMSIZ - 2);
577 comm[COMSIZ - 2] = '\0';
578 (void) strncat(comm, ">", COMSIZ - 1);
579 comm[COMSIZ - 1] = '\0';
583 * Convert the process's runtime from microseconds to seconds. This
584 * time includes the interrupt time although that is not wanted here.
585 * ps(1) is similarly sloppy.
587 cputime = (EP(pp, e_uticks) + EP(pp, e_sticks)) / 1000000;
589 /* calculate the base for cpu percentages */
590 pct = pctdouble(PP(pp, p_pctcpu));
592 /* generate "STATE" field */
593 switch (state = PP(pp, p_stat)) {
595 if (smpmode && TP(pp, td_flags) & TDF_RUNNING)
596 sprintf(status, "CPU%d", EP(pp, e_cpuid));
598 strcpy(status, "RUN");
601 if (TP(pp, td_wmesg) != NULL) {
602 sprintf(status, "%.6s", EP(pp, e_wmesg));
609 state < sizeof(state_abbrev) / sizeof(*state_abbrev))
610 sprintf(status, "%.6s", state_abbrev[(unsigned char) state]);
612 sprintf(status, "?%5d", state);
617 * idle time 0 - 31 -> nice value +21 - +52
618 * normal time -> nice value -20 - +20
619 * real time 0 - 31 -> nice value -52 - -21
620 * thread 0 - 31 -> nice value -53 -
622 switch(PP(pp, p_rtprio.type)) {
623 case RTP_PRIO_REALTIME:
624 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio);
627 nice = PRIO_MAX + 1 + PP(pp, p_rtprio.prio);
629 case RTP_PRIO_THREAD:
630 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - PP(pp, p_rtprio.prio);
633 nice = PP(pp, p_nice);
638 /* format this entry */
640 smpmode ? smp_Proc_format : up_Proc_format,
642 namelength, namelength,
643 (*get_userid)(EP(pp, e_ucred.cr_ruid)),
646 format_k2(PROCSIZE(pp)),
647 format_k2(pagetok(VP(pp, vm_rssize))),
649 smpmode ? EP(pp, e_cpuid) : 0,
650 format_time(cputime),
651 100.0 * weighted_cpu(pct, pp),
654 printable(TP(pp, td_comm)));
656 /* return the result */
662 * check_nlist(nlst) - checks the nlist to see if any symbols were not
663 * found. For every symbol that was not found, a one-line
664 * message is printed to stderr. The routine returns the
665 * number of symbols NOT found.
668 static int check_nlist(nlst)
670 register struct nlist *nlst;
675 /* check to see if we got ALL the symbols we requested */
676 /* this will write one line to stderr for every symbol not found */
679 while (nlst->n_name != NULL)
681 if (nlst->n_type == 0)
683 /* this one wasn't found */
684 (void) fprintf(stderr, "kernel: no symbol named `%s'\n",
696 * getkval(offset, ptr, size, refstr) - get a value out of the kernel.
697 * "offset" is the byte offset into the kernel for the desired value,
698 * "ptr" points to a buffer into which the value is retrieved,
699 * "size" is the size of the buffer (and the object to retrieve),
700 * "refstr" is a reference string used when printing error meessages,
701 * if "refstr" starts with a '!', then a failure on read will not
702 * be fatal (this may seem like a silly way to do things, but I
703 * really didn't want the overhead of another argument).
707 static int getkval(offset, ptr, size, refstr)
709 unsigned long offset;
715 if (kvm_read(kd, offset, (char *) ptr, size) != size)
723 fprintf(stderr, "top: kvm_read for %s: %s\n",
724 refstr, strerror(errno));
731 /* comparison routines for qsort */
734 * proc_compare - comparison function for "qsort"
735 * Compares the resource consumption of two processes using five
736 * distinct keys. The keys (in descending order of importance) are:
737 * percent cpu, cpu ticks, state, resident set size, total virtual
738 * memory usage. The process states are ordered as follows (from least
739 * to most important): WAIT, zombie, sleep, stop, start, run. The
740 * array declaration below maps a process state index into a number
741 * that reflects this ordering.
744 static unsigned char sorted_state[] =
748 1, /* ABANDONED (WAIT) */
756 #define ORDERKEY_PCTCPU \
757 if (lresult = (long) PP(p2, p_pctcpu) - (long) PP(p1, p_pctcpu), \
758 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
760 #define CPTICKS(p) (EP(p, e_uticks) + EP(p, e_sticks))
762 #define ORDERKEY_CPTICKS \
763 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
764 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
766 #define ORDERKEY_STATE \
767 if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
768 sorted_state[(unsigned char) PP(p1, p_stat)]) == 0)
770 #define ORDERKEY_PRIO \
771 if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
773 #define ORDERKEY_RSSIZE \
774 if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
776 #define ORDERKEY_MEM \
777 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
779 /* compare_cpu - the comparison function for sorting by cpu percentage */
783 compare_cpu(pp1, pp2)
785 proc_compare(pp1, pp2)
792 register struct kinfo_proc *p1;
793 register struct kinfo_proc *p2;
795 register pctcpu lresult;
797 /* remove one level of indirection */
798 p1 = *(struct kinfo_proc **) pp1;
799 p2 = *(struct kinfo_proc **) pp2;
813 /* compare routines */
814 int compare_size(), compare_res(), compare_time(), compare_prio();
816 int (*proc_compares[])() = {
825 /* compare_size - the comparison function for sorting by total memory usage */
828 compare_size(pp1, pp2)
834 register struct kinfo_proc *p1;
835 register struct kinfo_proc *p2;
837 register pctcpu lresult;
839 /* remove one level of indirection */
840 p1 = *(struct kinfo_proc **) pp1;
841 p2 = *(struct kinfo_proc **) pp2;
854 /* compare_res - the comparison function for sorting by resident set size */
857 compare_res(pp1, pp2)
863 register struct kinfo_proc *p1;
864 register struct kinfo_proc *p2;
866 register pctcpu lresult;
868 /* remove one level of indirection */
869 p1 = *(struct kinfo_proc **) pp1;
870 p2 = *(struct kinfo_proc **) pp2;
883 /* compare_time - the comparison function for sorting by total cpu time */
886 compare_time(pp1, pp2)
892 register struct kinfo_proc *p1;
893 register struct kinfo_proc *p2;
895 register pctcpu lresult;
897 /* remove one level of indirection */
898 p1 = *(struct kinfo_proc **) pp1;
899 p2 = *(struct kinfo_proc **) pp2;
912 /* compare_prio - the comparison function for sorting by cpu percentage */
915 compare_prio(pp1, pp2)
921 register struct kinfo_proc *p1;
922 register struct kinfo_proc *p2;
924 register pctcpu lresult;
926 /* remove one level of indirection */
927 p1 = *(struct kinfo_proc **) pp1;
928 p2 = *(struct kinfo_proc **) pp2;
943 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
944 * the process does not exist.
945 * It is EXTREMLY IMPORTANT that this function work correctly.
946 * If top runs setuid root (as in SVR4), then this function
947 * is the only thing that stands in the way of a serious
948 * security problem. It validates requests for the "kill"
949 * and "renice" commands.
958 register struct kinfo_proc **prefp;
959 register struct kinfo_proc *pp;
966 if (PP(pp, p_pid) == (pid_t)pid)
968 return((int)EP(pp, e_ucred.cr_ruid));
976 * swapmode is based on a program called swapinfo written
977 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
980 #define SVAR(var) __STRING(var) /* to force expansion */
981 #define KGET(idx, var) \
982 KGET1(idx, &var, sizeof(var), SVAR(var))
983 #define KGET1(idx, p, s, msg) \
984 KGET2(nlst[idx].n_value, p, s, msg)
985 #define KGET2(addr, p, s, msg) \
986 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
987 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
990 #define KGETRET(addr, p, s, msg) \
991 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
992 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
998 swapmode(retavail, retfree)
1003 int pagesize = getpagesize();
1004 struct kvm_swap swapary[1];
1009 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
1011 n = kvm_getswapinfo(kd, swapary, 1, 0);
1012 if (n < 0 || swapary[0].ksw_total == 0)
1015 *retavail = CONVERT(swapary[0].ksw_total);
1016 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
1018 n = (int)((double)swapary[0].ksw_used * 100.0 /
1019 (double)swapary[0].ksw_total);