2 * top - a top users display for Unix
4 * SYNOPSIS: For DragonFly 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 DragonFly 2.5.1
14 * DragonFly 2.x and above
18 * AUTHOR: Jan Lentfer <Jan.Lentfer@web.de>
19 * This module has been put together from different sources and is based on the
20 * work of many other people, e.g. Matthew Dillon, Simon Schubert, Jordan Gordeev.
22 * $FreeBSD: src/usr.bin/top/machine.c,v 1.29.2.2 2001/07/31 20:27:05 tmm Exp $
26 #include <sys/types.h>
28 #include <sys/signal.h>
29 #include <sys/param.h>
38 #include <sys/errno.h>
39 #include <sys/sysctl.h>
41 #include <sys/vmmeter.h>
42 #include <sys/resource.h>
43 #include <sys/rtprio.h>
49 #include <osreldate.h> /* for changes in kernel structures */
51 #include <sys/kinfo.h>
59 int swapmode(int *retavail, int *retfree);
61 static int namelength;
63 static int show_fullcmd;
67 /* get_process_info passes back a handle. This is what it looks like: */
70 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
71 int remaining; /* number of pointers remaining */
74 /* declarations for load_avg */
77 #define PP(pp, field) ((pp)->kp_ ## field)
78 #define LP(pp, field) ((pp)->kp_lwp.kl_ ## field)
79 #define VP(pp, field) ((pp)->kp_vm_ ## field)
81 /* what we consider to be process size: */
82 #define PROCSIZE(pp) (VP((pp), map_size) / 1024)
85 * These definitions control the format of the per-process area
88 static char smp_header[] =
89 " PID %-*.*s NICE SIZE RES STATE CPU TIME CTIME CPU COMMAND";
91 #define smp_Proc_format \
92 "%5d %-*.*s %3d%7s %6s %8.8s %2d %6s %7s %5.2f%% %.*s"
94 static char up_header[] =
95 " PID %-*.*s NICE SIZE RES STATE TIME CTIME CPU COMMAND";
97 #define up_Proc_format \
98 "%5d %-*.*s %3d%7s %6s %8.8s%.0d %7s %7s %5.2f%% %.*s"
101 /* process state names for the "STATE" column of the display */
103 * the extra nulls in the string "run" are for adding a slash and the
104 * processor number when needed
107 const char *state_abbrev[] = {
108 "", "RUN\0\0\0", "STOP", "SLEEP",
114 /* values that we stash away in _init and use in later routines */
118 /* these are for calculating cpu state percentages */
120 static struct kinfo_cputime *cp_time, *cp_old;
122 /* these are for detailing the process states */
126 int process_states[MAXPSTATES];
128 char *procstatenames[] = {
129 " running, ", " idle, ", " active, ", " stopped, ", " zombie, ",
133 /* these are for detailing the cpu states */
136 char *cpustatenames[CPU_STATES + 1] = {
137 "user", "nice", "system", "interrupt", "idle", NULL
140 /* these are for detailing the memory statistics */
142 long memory_stats[7];
143 char *memorynames[] = {
144 "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
149 char *swapnames[] = {
151 "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
156 /* these are for keeping track of the proc array */
159 static int onproc = -1;
161 static struct kinfo_proc *pbase;
162 static struct kinfo_proc **pref;
164 /* these are for getting the memory statistics */
166 static int pageshift; /* log base 2 of the pagesize */
168 /* define pagetok in terms of pageshift */
170 #define pagetok(size) ((size) << pageshift)
172 /* sorting orders. first is default */
173 char *ordernames[] = {
174 "cpu", "size", "res", "time", "pri", "thr", "pid", "ctime", "pres", NULL
177 /* compare routines */
178 int proc_compare (struct kinfo_proc **, struct kinfo_proc **);
179 int compare_size (struct kinfo_proc **, struct kinfo_proc **);
180 int compare_res (struct kinfo_proc **, struct kinfo_proc **);
181 int compare_time (struct kinfo_proc **, struct kinfo_proc **);
182 int compare_ctime (struct kinfo_proc **, struct kinfo_proc **);
183 int compare_prio(struct kinfo_proc **, struct kinfo_proc **);
184 int compare_thr (struct kinfo_proc **, struct kinfo_proc **);
185 int compare_pid (struct kinfo_proc **, struct kinfo_proc **);
186 int compare_pres(struct kinfo_proc **, struct kinfo_proc **);
188 int (*proc_compares[]) (struct kinfo_proc **,struct kinfo_proc **) = {
202 cputime_percentages(int out[CPU_STATES], struct kinfo_cputime *new,
203 struct kinfo_cputime *old)
205 struct kinfo_cputime diffs;
206 uint64_t total_change, half_total;
211 diffs.cp_user = new->cp_user - old->cp_user;
212 diffs.cp_nice = new->cp_nice - old->cp_nice;
213 diffs.cp_sys = new->cp_sys - old->cp_sys;
214 diffs.cp_intr = new->cp_intr - old->cp_intr;
215 diffs.cp_idle = new->cp_idle - old->cp_idle;
216 total_change = diffs.cp_user + diffs.cp_nice + diffs.cp_sys +
217 diffs.cp_intr + diffs.cp_idle;
218 old->cp_user = new->cp_user;
219 old->cp_nice = new->cp_nice;
220 old->cp_sys = new->cp_sys;
221 old->cp_intr = new->cp_intr;
222 old->cp_idle = new->cp_idle;
224 /* avoid divide by zero potential */
225 if (total_change == 0)
228 /* calculate percentages based on overall change, rounding up */
229 half_total = total_change >> 1;
231 out[0] = ((diffs.cp_user * 1000LL + half_total) / total_change);
232 out[1] = ((diffs.cp_nice * 1000LL + half_total) / total_change);
233 out[2] = ((diffs.cp_sys * 1000LL + half_total) / total_change);
234 out[3] = ((diffs.cp_intr * 1000LL + half_total) / total_change);
235 out[4] = ((diffs.cp_idle * 1000LL + half_total) / total_change);
239 machine_init(struct statics *statics)
244 struct timeval boottime;
247 if (kinfo_get_cpus(&n_cpus))
248 err(1, "kinfo_get_cpus failed");
251 modelen = sizeof(boottime);
252 if (sysctlbyname("kern.boottime", &boottime, &modelen, NULL, 0) == -1) {
253 /* we have no boottime to report */
254 boottime.tv_sec = -1;
256 modelen = sizeof(smpmode);
257 if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
258 sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
259 modelen != sizeof(smpmode))
262 while ((pw = getpwent()) != NULL) {
263 if ((int)strlen(pw->pw_name) > namelength)
264 namelength = strlen(pw->pw_name);
268 if (smpmode && namelength > 13)
270 else if (namelength > 15)
273 if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL)
281 * get the page size with "getpagesize" and calculate pageshift from
284 pagesize = getpagesize();
286 while (pagesize > 1) {
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->boottime = boottime.tv_sec;
299 statics->swap_names = swapnames;
300 statics->order_names = ordernames;
301 /* we need kvm descriptor in order to show full commands */
302 statics->flags.fullcmds = kd != NULL;
309 format_header(char *uname_field)
311 static char Header[128];
313 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
314 namelength, namelength, uname_field);
316 if (screen_width <= 79)
319 cmdlength = screen_width;
321 cmdlength = cmdlength - strlen(Header) + 6;
326 static int swappgsin = -1;
327 static int swappgsout = -1;
328 extern struct timeval timeout;
331 get_system_info(struct system_info *si)
336 if (cpu_states == NULL) {
337 cpu_states = malloc(sizeof(*cpu_states) * CPU_STATES * n_cpus);
338 if (cpu_states == NULL)
340 bzero(cpu_states, sizeof(*cpu_states) * CPU_STATES * n_cpus);
342 if (cp_time == NULL) {
343 cp_time = malloc(2 * n_cpus * sizeof(cp_time[0]));
346 cp_old = cp_time + n_cpus;
347 len = n_cpus * sizeof(cp_old[0]);
349 if (sysctlbyname("kern.cputime", cp_old, &len, NULL, 0))
350 err(1, "kern.cputime");
352 len = n_cpus * sizeof(cp_time[0]);
354 if (sysctlbyname("kern.cputime", cp_time, &len, NULL, 0))
355 err(1, "kern.cputime");
357 getloadavg(si->load_avg, 3);
361 /* convert cp_time counts to percentages */
362 for (cpu = 0; cpu < n_cpus; ++cpu) {
363 cputime_percentages(cpu_states + cpu * CPU_STATES,
364 &cp_time[cpu], &cp_old[cpu]);
367 /* sum memory & swap statistics */
371 size_t vms_size = sizeof(vms);
372 size_t vmm_size = sizeof(vmm);
373 static unsigned int swap_delay = 0;
374 static int swapavail = 0;
375 static int swapfree = 0;
376 static long bufspace = 0;
378 if (sysctlbyname("vm.vmstats", &vms, &vms_size, NULL, 0))
379 err(1, "sysctlbyname: vm.vmstats");
381 if (sysctlbyname("vm.vmmeter", &vmm, &vmm_size, NULL, 0))
382 err(1, "sysctlbyname: vm.vmmeter");
384 if (kinfo_get_vfs_bufspace(&bufspace))
385 err(1, "kinfo_get_vfs_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;
401 /* compute differences between old and new swap statistic */
403 swap_stats[4] = pagetok(((vmm.v_swappgsin - swappgsin)));
404 swap_stats[5] = pagetok(((vmm.v_swappgsout - swappgsout)));
407 swappgsin = vmm.v_swappgsin;
408 swappgsout = vmm.v_swappgsout;
410 /* call CPU heavy swapmode() only for changes */
411 if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
412 swap_stats[3] = swapmode(&swapavail, &swapfree);
413 swap_stats[0] = swapavail;
414 swap_stats[1] = swapavail - swapfree;
415 swap_stats[2] = swapfree;
421 /* set arrays and strings */
422 si->cpustates = cpu_states;
423 si->memory = memory_stats;
424 si->swap = swap_stats;
428 si->last_pid = lastpid;
435 static struct handle handle;
438 get_process_info(struct system_info *si, struct process_select *sel,
444 struct kinfo_proc **prefp;
445 struct kinfo_proc *pp;
447 /* these are copied out of sel for speed */
453 show_threads = sel->threads;
456 pbase = kvm_getprocs(kd,
457 KERN_PROC_ALL | (show_threads ? KERN_PROC_FLAG_LWP : 0), 0, &nproc);
459 pref = (struct kinfo_proc **)realloc(pref, sizeof(struct kinfo_proc *)
461 if (pref == NULL || pbase == NULL) {
462 (void)fprintf(stderr, "top: Out of memory.\n");
465 /* get a pointer to the states summary array */
466 si->procstates = process_states;
468 /* set up flags which define what we are going to select */
469 show_idle = sel->idle;
470 show_system = sel->system;
471 show_uid = sel->uid != -1;
472 show_fullcmd = sel->fullcmd;
474 /* count up process states and get pointers to interesting procs */
477 memset((char *)process_states, 0, sizeof(process_states));
479 for (pp = pbase, i = 0; i < nproc; pp++, i++) {
481 * Place pointers to each valid proc structure in pref[].
482 * Process slots that are actually in use have a non-zero
483 * status field. Processes with P_SYSTEM set are system
484 * processes---these get ignored unless show_sysprocs is set.
486 if ((show_system && (LP(pp, pid) == -1)) ||
487 (show_system || ((PP(pp, flags) & P_SYSTEM) == 0))) {
488 int pstate = LP(pp, stat);
493 if (pstate >= 0 && pstate < MAXPSTATES)
494 process_states[pstate]++;
495 if ((show_system && (LP(pp, pid) == -1)) ||
496 (show_idle || (LP(pp, pctcpu) != 0) ||
497 (pstate == LSRUN)) &&
498 (!show_uid || PP(pp, ruid) == (uid_t) sel->uid)) {
505 qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *),
506 (int (*)(const void *, const void *))proc_compares[compare_index]);
508 /* remember active and total counts */
509 si->p_total = total_procs;
510 si->p_active = pref_len = active_procs;
512 /* pass back a handle */
513 handle.next_proc = pref;
514 handle.remaining = active_procs;
515 return ((caddr_t) & handle);
518 char fmt[MAX_COLS]; /* static area where result is built */
521 format_next_process(caddr_t xhandle, char *(*get_userid) (int))
523 struct kinfo_proc *pp;
533 char cputime_fmt[10], ccputime_fmt[10];
535 /* find and remember the next proc structure */
536 hp = (struct handle *)xhandle;
537 pp = *(hp->next_proc++);
540 /* get the process's command name */
542 if ((comm_full = kvm_getargv(kd, pp, 0)) == NULL) {
551 * Convert the process's runtime from microseconds to seconds. This
552 * time includes the interrupt time to be in compliance with ps output.
554 cputime = (LP(pp, uticks) + LP(pp, sticks) + LP(pp, iticks)) / 1000000;
555 ccputime = cputime + PP(pp, cru).ru_stime.tv_sec + PP(pp, cru).ru_utime.tv_sec;
556 format_time(cputime, cputime_fmt, sizeof(cputime_fmt));
557 format_time(ccputime, ccputime_fmt, sizeof(ccputime_fmt));
559 /* calculate the base for cpu percentages */
560 pct = pctdouble(LP(pp, pctcpu));
562 /* generate "STATE" field */
563 switch (state = LP(pp, stat)) {
565 if (smpmode && LP(pp, tdflags) & TDF_RUNNING)
566 sprintf(status, "CPU%d", LP(pp, cpuid));
568 strcpy(status, "RUN");
571 if (LP(pp, wmesg) != NULL) {
572 sprintf(status, "%.8s", LP(pp, wmesg)); /* WMESGLEN */
579 (unsigned)state < sizeof(state_abbrev) / sizeof(*state_abbrev))
580 sprintf(status, "%.6s", state_abbrev[(unsigned char)state]);
582 sprintf(status, "?%5d", state);
586 if (PP(pp, stat) == SZOMB)
587 strcpy(status, "ZOMB");
590 * idle time 0 - 31 -> nice value +21 - +52 normal time -> nice
591 * value -20 - +20 real time 0 - 31 -> nice value -52 - -21 thread
592 * 0 - 31 -> nice value -53 -
594 switch (LP(pp, rtprio.type)) {
595 case RTP_PRIO_REALTIME:
596 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX + LP(pp, rtprio.prio);
599 xnice = PRIO_MAX + 1 + LP(pp, rtprio.prio);
601 case RTP_PRIO_THREAD:
602 xnice = PRIO_MIN - 1 - RTP_PRIO_MAX - LP(pp, rtprio.prio);
605 xnice = PP(pp, nice);
609 /* format this entry */
610 snprintf(fmt, sizeof(fmt),
611 smpmode ? smp_Proc_format : up_Proc_format,
613 namelength, namelength,
614 get_userid(PP(pp, ruid)),
616 format_k(PROCSIZE(pp)),
617 format_k(pagetok(VP(pp, rssize))),
619 (int)(smpmode ? LP(pp, cpuid) : 0),
624 show_fullcmd ? *comm_full : comm);
626 /* return the result */
630 /* comparison routines for qsort */
633 * proc_compare - comparison function for "qsort"
634 * Compares the resource consumption of two processes using five
635 * distinct keys. The keys (in descending order of importance) are:
636 * percent cpu, cpu ticks, state, resident set size, total virtual
637 * memory usage. The process states are ordered as follows (from least
638 * to most important): WAIT, zombie, sleep, stop, start, run. The
639 * array declaration below maps a process state index into a number
640 * that reflects this ordering.
643 static unsigned char sorted_state[] =
647 1, /* ABANDONED (WAIT) */
655 #define ORDERKEY_PCTCPU \
656 if (lresult = (long) LP(p2, pctcpu) - (long) LP(p1, pctcpu), \
657 (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
659 #define CPTICKS(p) (LP(p, uticks) + LP(p, sticks) + LP(p, iticks))
661 #define ORDERKEY_CPTICKS \
662 if ((result = CPTICKS(p2) > CPTICKS(p1) ? 1 : \
663 CPTICKS(p2) < CPTICKS(p1) ? -1 : 0) == 0)
665 #define CTIME(p) (((LP(p, uticks) + LP(p, sticks) + LP(p, iticks))/1000000) + \
666 PP(p, cru).ru_stime.tv_sec + PP(p, cru).ru_utime.tv_sec)
668 #define ORDERKEY_CTIME \
669 if ((result = CTIME(p2) > CTIME(p1) ? 1 : \
670 CTIME(p2) < CTIME(p1) ? -1 : 0) == 0)
672 #define ORDERKEY_STATE \
673 if ((result = sorted_state[(unsigned char) PP(p2, stat)] - \
674 sorted_state[(unsigned char) PP(p1, stat)]) == 0)
676 #define ORDERKEY_PRIO \
677 if ((result = LP(p2, prio) - LP(p1, prio)) == 0)
679 #define ORDERKEY_KTHREADS \
680 if ((result = (LP(p1, pid) == 0) - (LP(p2, pid) == 0)) == 0)
682 #define ORDERKEY_KTHREADS_PRIO \
683 if ((result = LP(p2, tdprio) - LP(p1, tdprio)) == 0)
685 #define ORDERKEY_RSSIZE \
686 if ((result = VP(p2, rssize) - VP(p1, rssize)) == 0)
688 #define ORDERKEY_MEM \
689 if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
691 #define ORDERKEY_PID \
692 if ( (result = PP(p1, pid) - PP(p2, pid)) == 0)
694 #define ORDERKEY_PRSSIZE \
695 if((result = VP(p2, prssize) - VP(p1, prssize)) == 0)
697 /* compare_cpu - the comparison function for sorting by cpu percentage */
700 proc_compare(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
702 struct kinfo_proc *p1;
703 struct kinfo_proc *p2;
707 /* remove one level of indirection */
708 p1 = *(struct kinfo_proc **) pp1;
709 p2 = *(struct kinfo_proc **) pp2;
722 /* compare_size - the comparison function for sorting by total memory usage */
725 compare_size(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
727 struct kinfo_proc *p1;
728 struct kinfo_proc *p2;
732 /* remove one level of indirection */
733 p1 = *(struct kinfo_proc **) pp1;
734 p2 = *(struct kinfo_proc **) pp2;
747 /* compare_res - the comparison function for sorting by resident set size */
750 compare_res(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
752 struct kinfo_proc *p1;
753 struct kinfo_proc *p2;
757 /* remove one level of indirection */
758 p1 = *(struct kinfo_proc **) pp1;
759 p2 = *(struct kinfo_proc **) pp2;
772 /* compare_pres - the comparison function for sorting by proportional resident set size */
775 compare_pres(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
777 struct kinfo_proc *p1;
778 struct kinfo_proc *p2;
782 /* remove one level of indirection */
783 p1 = *(struct kinfo_proc **) pp1;
784 p2 = *(struct kinfo_proc **) pp2;
798 /* compare_time - the comparison function for sorting by total cpu time */
801 compare_time(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
803 struct kinfo_proc *p1;
804 struct kinfo_proc *p2;
808 /* remove one level of indirection */
809 p1 = *(struct kinfo_proc **) pp1;
810 p2 = *(struct kinfo_proc **) pp2;
815 ORDERKEY_KTHREADS_PRIO
826 compare_ctime(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
828 struct kinfo_proc *p1;
829 struct kinfo_proc *p2;
833 /* remove one level of indirection */
834 p1 = *(struct kinfo_proc **) pp1;
835 p2 = *(struct kinfo_proc **) pp2;
840 ORDERKEY_KTHREADS_PRIO
850 /* compare_prio - the comparison function for sorting by cpu percentage */
853 compare_prio(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
855 struct kinfo_proc *p1;
856 struct kinfo_proc *p2;
860 /* remove one level of indirection */
861 p1 = *(struct kinfo_proc **) pp1;
862 p2 = *(struct kinfo_proc **) pp2;
865 ORDERKEY_KTHREADS_PRIO
878 compare_thr(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
880 struct kinfo_proc *p1;
881 struct kinfo_proc *p2;
885 /* remove one level of indirection */
886 p1 = *(struct kinfo_proc **)pp1;
887 p2 = *(struct kinfo_proc **)pp2;
890 ORDERKEY_KTHREADS_PRIO
901 /* compare_pid - the comparison function for sorting by process id */
904 compare_pid(struct kinfo_proc **pp1, struct kinfo_proc **pp2)
906 struct kinfo_proc *p1;
907 struct kinfo_proc *p2;
910 /* remove one level of indirection */
911 p1 = *(struct kinfo_proc **) pp1;
912 p2 = *(struct kinfo_proc **) pp2;
921 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
922 * the process does not exist.
923 * It is EXTREMLY IMPORTANT that this function work correctly.
924 * If top runs setuid root (as in SVR4), then this function
925 * is the only thing that stands in the way of a serious
926 * security problem. It validates requests for the "kill"
927 * and "renice" commands.
934 struct kinfo_proc **prefp;
935 struct kinfo_proc *pp;
939 while (--xcnt >= 0) {
941 if (PP(pp, pid) == (pid_t) pid) {
942 return ((int)PP(pp, ruid));
950 * swapmode is based on a program called swapinfo written
951 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
954 swapmode(int *retavail, int *retfree)
957 int pagesize = getpagesize();
958 struct kvm_swap swapary[1];
963 #define CONVERT(v) ((quad_t)(v) * pagesize / 1024)
965 n = kvm_getswapinfo(kd, swapary, 1, 0);
966 if (n < 0 || swapary[0].ksw_total == 0)
969 *retavail = CONVERT(swapary[0].ksw_total);
970 *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used);
972 n = (int)((double)swapary[0].ksw_used * 100.0 /
973 (double)swapary[0].ksw_total);