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.9 2003/10/04 20:36:53 hmp 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(struct nlist *);
59 static int getkval(unsigned long, int *, int, char *);
60 extern char* printable(char *);
61 int swapmode(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(struct statics *statics)
224 register int pagesize;
228 modelen = sizeof(smpmode);
229 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
230 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
231 modelen != sizeof(smpmode))
234 while ((pw = getpwent()) != NULL) {
235 if (strlen(pw->pw_name) > namelength)
236 namelength = strlen(pw->pw_name);
240 if (smpmode && namelength > 13)
242 else if (namelength > 15)
245 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
249 /* get the list of symbols we want to access in the kernel */
250 (void) kvm_nlist(kd, nlst);
251 if (nlst[0].n_type == 0)
253 fprintf(stderr, "top: nlist failed\n");
257 /* make sure they were all found */
258 if (i > 0 && check_nlist(nlst) > 0)
263 (void) getkval(nlst[X_CCPU].n_value, (int *)(&ccpu), sizeof(ccpu),
264 nlst[X_CCPU].n_name);
266 /* stash away certain offsets for later use */
267 cp_time_offset = nlst[X_CP_TIME].n_value;
268 avenrun_offset = nlst[X_AVENRUN].n_value;
269 lastpid_offset = nlst[X_LASTPID].n_value;
270 bufspace_offset = nlst[X_BUFSPACE].n_value;
272 /* this is used in calculating WCPU -- calculate it ahead of time */
273 logcpu = log(loaddouble(ccpu));
279 /* get the page size with "getpagesize" and calculate pageshift from it */
280 pagesize = getpagesize();
288 /* we only need the amount of log(2)1024 for our conversion */
289 pageshift -= LOG1024;
291 /* fill in the statics information */
292 statics->procstate_names = procstatenames;
293 statics->cpustate_names = cpustatenames;
294 statics->memory_names = memorynames;
295 statics->swap_names = swapnames;
297 statics->order_names = ordernames;
304 char *format_header(register char *uname_field)
307 static char Header[128];
309 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
310 namelength, namelength, uname_field);
312 cmdlength = 80 - strlen(Header) + 6;
317 static int swappgsin = -1;
318 static int swappgsout = -1;
319 extern struct timeval timeout;
322 get_system_info(struct system_info *si)
327 struct timeval boottime;
330 /* get the cp_time array */
331 (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
332 nlst[X_CP_TIME].n_name);
333 (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
334 nlst[X_AVENRUN].n_name);
336 (void) getkval(lastpid_offset, (int *)(&lastpid), sizeof(lastpid),
339 /* convert load averages to doubles */
342 register double *infoloadp;
346 struct loadavg sysload;
348 getkerninfo(KINFO_LOADAVG, &sysload, &size, 0);
351 infoloadp = si->load_avg;
353 for (i = 0; i < 3; i++)
356 *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
358 *infoloadp++ = loaddouble(*avenrunp++);
362 /* convert cp_time counts to percentages */
363 total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
365 /* sum memory & swap statistics */
369 int vms_size = sizeof(vms);
370 int 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 perror("sysctlbyname: vm.vmstats");
380 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0)) {
381 perror("sysctlbyname: vm.vmstats");
384 (void) getkval(bufspace_offset, (int *)(&bufspace), sizeof(bufspace),
387 /* convert memory stats to Kbytes */
388 memory_stats[0] = pagetok(vms.v_active_count);
389 memory_stats[1] = pagetok(vms.v_inactive_count);
390 memory_stats[2] = pagetok(vms.v_wire_count);
391 memory_stats[3] = pagetok(vms.v_cache_count);
392 memory_stats[4] = bufspace / 1024;
393 memory_stats[5] = pagetok(vms.v_free_count);
394 memory_stats[6] = -1;
402 /* compute differences between old and new swap statistic */
404 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin)));
405 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout)));
408 swappgsin = vmm.v_swappgsin;
409 swappgsout = vmm.v_swappgsout;
411 /* call CPU heavy swapmode() only for changes */
412 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
413 swap_stats[3] = swapmode(&swapavail, &swapfree);
414 swap_stats[0] = swapavail;
415 swap_stats[1] = swapavail - swapfree;
416 swap_stats[2] = swapfree;
422 /* set arrays and strings */
423 si->cpustates = cpu_states;
424 si->memory = memory_stats;
425 si->swap = swap_stats;
429 si->last_pid = lastpid;
435 * Print how long system has been up.
436 * (Found by looking getting "boottime" from the kernel)
439 mib[1] = KERN_BOOTTIME;
440 bt_size = sizeof(boottime);
441 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 &&
442 boottime.tv_sec != 0) {
443 si->boottime = boottime;
445 si->boottime.tv_sec = -1;
449 static struct handle handle;
451 caddr_t get_process_info(struct system_info *si, struct process_select *sel,
455 register int total_procs;
456 register int active_procs;
457 register struct kinfo_proc **prefp;
458 register struct kinfo_proc *pp;
460 /* these are copied out of sel for speed */
468 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
470 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
472 if (pref == NULL || pbase == NULL) {
473 (void) fprintf(stderr, "top: Out of memory.\n");
476 /* get a pointer to the states summary array */
477 si->procstates = process_states;
479 /* set up flags which define what we are going to select */
480 show_idle = sel->idle;
481 show_self = sel->self;
482 show_system = sel->system;
483 show_uid = sel->uid != -1;
484 show_command = sel->command != NULL;
486 /* count up process states and get pointers to interesting procs */
489 memset((char *)process_states, 0, sizeof(process_states));
491 for (pp = pbase, i = 0; i < nproc; pp++, i++)
494 * Place pointers to each valid proc structure in pref[].
495 * Process slots that are actually in use have a non-zero
496 * status field. Processes with P_SYSTEM set are system
497 * processes---these get ignored unless show_sysprocs is set.
499 if (PP(pp, p_stat) != 0 &&
500 (show_self != PP(pp, p_pid)) &&
501 (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0)))
504 process_states[(unsigned char) PP(pp, p_stat)]++;
505 if ((PP(pp, p_stat) != SZOMB) &&
506 (show_idle || (PP(pp, p_pctcpu) != 0) ||
507 (PP(pp, p_stat) == SRUN)) &&
508 (!show_uid || EP(pp, e_ucred.cr_ruid) == (uid_t)sel->uid))
516 /* if requested, sort the "interesting" processes */
519 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
522 /* remember active and total counts */
523 si->p_total = total_procs;
524 si->p_active = pref_len = active_procs;
526 /* pass back a handle */
527 handle.next_proc = pref;
528 handle.remaining = active_procs;
529 return((caddr_t)&handle);
532 char fmt[128]; /* static area where result is built */
534 char *format_next_process(caddr_t handle, char *(*get_userid)())
536 struct kinfo_proc *pp;
544 /* find and remember the next proc structure */
545 hp = (struct handle *)handle;
546 pp = *(hp->next_proc++);
549 /* get the process's command name */
550 if ((PP(pp, p_flag) & P_INMEM) == 0) {
552 * Print swapped processes as <pname>
554 char *comm = TP(pp, td_comm);
555 #define COMSIZ sizeof(TP(pp, td_comm))
557 (void) strncpy(buf, comm, COMSIZ);
559 (void) strncpy(&comm[1], buf, COMSIZ - 2);
560 comm[COMSIZ - 2] = '\0';
561 (void) strncat(comm, ">", COMSIZ - 1);
562 comm[COMSIZ - 1] = '\0';
566 * Convert the process's runtime from microseconds to seconds. This
567 * time includes the interrupt time although that is not wanted here.
568 * ps(1) is similarly sloppy.
570 cputime = (EP(pp, e_uticks) + EP(pp, e_sticks)) / 1000000;
572 /* calculate the base for cpu percentages */
573 pct = pctdouble(PP(pp, p_pctcpu));
575 /* generate "STATE" field */
576 switch (state = PP(pp, p_stat)) {
578 if (smpmode && TP(pp, td_flags) & TDF_RUNNING)
579 sprintf(status, "CPU%d", EP(pp, e_cpuid));
581 strcpy(status, "RUN");
584 if (TP(pp, td_wmesg) != NULL) {
585 sprintf(status, "%.6s", EP(pp, e_wmesg));
592 state < sizeof(state_abbrev) / sizeof(*state_abbrev))
593 sprintf(status, "%.6s", state_abbrev[(unsigned char) state]);
595 sprintf(status, "?%5d", state);
600 * idle time 0 - 31 -> nice value +21 - +52
601 * normal time -> nice value -20 - +20
602 * real time 0 - 31 -> nice value -52 - -21
603 * thread 0 - 31 -> nice value -53 -
605 switch(PP(pp, p_rtprio.type)) {
606 case RTP_PRIO_REALTIME:
607 nice = PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio);
610 nice = PRIO_MAX + 1 + PP(pp, p_rtprio.prio);
612 case RTP_PRIO_THREAD:
613 nice = PRIO_MIN - 1 - RTP_PRIO_MAX - PP(pp, p_rtprio.prio);
616 nice = PP(pp, p_nice);
621 /* format this entry */
623 smpmode ? smp_Proc_format : up_Proc_format,
625 namelength, namelength,
626 (*get_userid)(EP(pp, e_ucred.cr_ruid)),
629 format_k2(PROCSIZE(pp)),
630 format_k2(pagetok(VP(pp, vm_rssize))),
632 smpmode ? EP(pp, e_cpuid) : 0,
633 format_time(cputime),
634 100.0 * weighted_cpu(pct, pp),
637 printable(TP(pp, td_comm)));
639 /* return the result */
645 * check_nlist(nlst) - checks the nlist to see if any symbols were not
646 * found. For every symbol that was not found, a one-line
647 * message is printed to stderr. The routine returns the
648 * number of symbols NOT found.
651 static int check_nlist(register struct nlist *nlst)
655 /* check to see if we got ALL the symbols we requested */
656 /* this will write one line to stderr for every symbol not found */
659 while (nlst->n_name != NULL)
661 if (nlst->n_type == 0)
663 /* this one wasn't found */
664 (void) fprintf(stderr, "kernel: no symbol named `%s'\n",
676 * getkval(offset, ptr, size, refstr) - get a value out of the kernel.
677 * "offset" is the byte offset into the kernel for the desired value,
678 * "ptr" points to a buffer into which the value is retrieved,
679 * "size" is the size of the buffer (and the object to retrieve),
680 * "refstr" is a reference string used when printing error meessages,
681 * if "refstr" starts with a '!', then a failure on read will not
682 * be fatal (this may seem like a silly way to do things, but I
683 * really didn't want the overhead of another argument).
687 static int getkval(unsigned long offset, int *ptr, int size, char *refstr)
689 if (kvm_read(kd, offset, (char *) ptr, size) != size)
697 fprintf(stderr, "top: kvm_read for %s: %s\n",
698 refstr, strerror(errno));
705 /* comparison routines for qsort */
708 * proc_compare - comparison function for "qsort"
709 * Compares the resource consumption of two processes using five
710 * distinct keys. The keys (in descending order of importance) are:
711 * percent cpu, cpu ticks, state, resident set size, total virtual
712 * memory usage. The process states are ordered as follows (from least
713 * to most important): WAIT, zombie, sleep, stop, start, run. The
714 * array declaration below maps a process state index into a number
715 * that reflects this ordering.
718 static unsigned char sorted_state[] =
722 1, /* ABANDONED (WAIT) */
730 #define ORDERKEY_PCTCPU \
731 if (lresult = (long) PP(p2, p_pctcpu) - (long) PP(p1, p_pctcpu), \
732 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
734 #define CPTICKS(p) (EP(p, e_uticks) + EP(p, e_sticks))
736 #define ORDERKEY_CPTICKS \
737 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
738 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
740 #define ORDERKEY_STATE \
741 if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
742 sorted_state[(unsigned char) PP(p1, p_stat)]) == 0)
744 #define ORDERKEY_PRIO \
745 if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
747 #define ORDERKEY_RSSIZE \
748 if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
750 #define ORDERKEY_MEM \
751 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
753 /* compare_cpu - the comparison function for sorting by cpu percentage */
757 compare_cpu(struct proc **pp1, struct proc **pp2)
759 proc_compare(struct proc **pp1, struct proc **pp2)
762 register struct kinfo_proc *p1;
763 register struct kinfo_proc *p2;
765 register pctcpu lresult;
767 /* remove one level of indirection */
768 p1 = *(struct kinfo_proc **) pp1;
769 p2 = *(struct kinfo_proc **) pp2;
783 /* compare routines */
784 int compare_size(), compare_res(), compare_time(), compare_prio();
786 int (*proc_compares[])() = {
795 /* compare_size - the comparison function for sorting by total memory usage */
798 compare_size(struct proc **pp1, struct proc **pp2)
800 register struct kinfo_proc *p1;
801 register struct kinfo_proc *p2;
803 register pctcpu lresult;
805 /* remove one level of indirection */
806 p1 = *(struct kinfo_proc **) pp1;
807 p2 = *(struct kinfo_proc **) pp2;
820 /* compare_res - the comparison function for sorting by resident set size */
823 compare_res(struct proc **pp1, struct proc **pp2)
825 register struct kinfo_proc *p1;
826 register struct kinfo_proc *p2;
828 register pctcpu lresult;
830 /* remove one level of indirection */
831 p1 = *(struct kinfo_proc **) pp1;
832 p2 = *(struct kinfo_proc **) pp2;
845 /* compare_time - the comparison function for sorting by total cpu time */
848 compare_time(struct proc **pp1, struct proc **pp2)
850 register struct kinfo_proc *p1;
851 register struct kinfo_proc *p2;
853 register pctcpu lresult;
855 /* remove one level of indirection */
856 p1 = *(struct kinfo_proc **) pp1;
857 p2 = *(struct kinfo_proc **) pp2;
870 /* compare_prio - the comparison function for sorting by cpu percentage */
873 compare_prio(struct proc **pp1, struct proc **pp2)
875 register struct kinfo_proc *p1;
876 register struct kinfo_proc *p2;
878 register pctcpu lresult;
880 /* remove one level of indirection */
881 p1 = *(struct kinfo_proc **) pp1;
882 p2 = *(struct kinfo_proc **) pp2;
897 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
898 * the process does not exist.
899 * It is EXTREMLY IMPORTANT that this function work correctly.
900 * If top runs setuid root (as in SVR4), then this function
901 * is the only thing that stands in the way of a serious
902 * security problem. It validates requests for the "kill"
903 * and "renice" commands.
906 int proc_owner(int pid)
909 register struct kinfo_proc **prefp;
910 register struct kinfo_proc *pp;
917 if (PP(pp, p_pid) == (pid_t)pid)
919 return((int)EP(pp, e_ucred.cr_ruid));
927 * swapmode is based on a program called swapinfo written
928 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
931 #define SVAR(var) __STRING(var) /* to force expansion */
932 #define KGET(idx, var) \
933 KGET1(idx, &var, sizeof(var), SVAR(var))
934 #define KGET1(idx, p, s, msg) \
935 KGET2(nlst[idx].n_value, p, s, msg)
936 #define KGET2(addr, p, s, msg) \
937 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
938 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
941 #define KGETRET(addr, p, s, msg) \
942 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
943 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
949 swapmode(int *retavail, int *retfree)
952 int pagesize = getpagesize();
953 struct kvm_swap swapary[1];
958 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
960 n = kvm_getswapinfo(kd, swapary, 1, 0);
961 if (n < 0 || swapary[0].ksw_total == 0)
964 *retavail = CONVERT(swapary[0].ksw_total);
965 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
967 n = (int)((double)swapary[0].ksw_used * 100.0 /
968 (double)swapary[0].ksw_total);