xref: /freebsd/usr.bin/top/machine.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
1 /*
2  * top - a top users display for Unix
3  *
4  * SYNOPSIS:  For FreeBSD-2.x system
5  *
6  * DESCRIPTION:
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/)
11  *
12  * This is the machine-dependent module for FreeBSD 2.2
13  * Works for:
14  *	FreeBSD 2.2, and probably FreeBSD 2.1.x
15  *
16  * LIBS: -lkvm
17  *
18  * AUTHOR:  Christos Zoulas <christos@ee.cornell.edu>
19  *          Steven Wallace  <swallace@freebsd.org>
20  *          Wolfram Schneider <wosch@FreeBSD.org>
21  *
22  * $Id: machine.c,v 1.17 1998/11/26 12:59:21 bde Exp $
23  */
24 
25 
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/signal.h>
29 #include <sys/param.h>
30 
31 #include "os.h"
32 #include <stdio.h>
33 #include <nlist.h>
34 #include <math.h>
35 #include <kvm.h>
36 #include <pwd.h>
37 #include <sys/errno.h>
38 #include <sys/sysctl.h>
39 #include <sys/dkstat.h>
40 #include <sys/file.h>
41 #include <sys/time.h>
42 #include <sys/proc.h>
43 #include <sys/user.h>
44 #include <sys/vmmeter.h>
45 #include <sys/resource.h>
46 #include <sys/rtprio.h>
47 
48 /* Swap */
49 #include <stdlib.h>
50 #include <sys/rlist.h>
51 #include <sys/conf.h>
52 
53 #include <osreldate.h> /* for changes in kernel structures */
54 
55 #include "top.h"
56 #include "machine.h"
57 
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));
62 static int smpmode;
63 static int namelength;
64 static int cmdlength;
65 
66 
67 /* get_process_info passes back a handle.  This is what it looks like: */
68 
69 struct handle
70 {
71     struct kinfo_proc **next_proc;	/* points to next valid proc pointer */
72     int remaining;		/* number of pointers remaining */
73 };
74 
75 /* declarations for load_avg */
76 #include "loadavg.h"
77 
78 #define PP(pp, field) ((pp)->kp_proc . field)
79 #define EP(pp, field) ((pp)->kp_eproc . field)
80 #define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
81 
82 /* define what weighted cpu is.  */
83 #define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \
84 			 ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu))))
85 
86 /* what we consider to be process size: */
87 #define PROCSIZE(pp) (VP((pp), vm_map.size) / 1024)
88 
89 /* definitions for indices in the nlist array */
90 
91 static struct nlist nlst[] = {
92 #define X_CCPU		0
93     { "_ccpu" },
94 #define X_CP_TIME	1
95     { "_cp_time" },
96 #define X_AVENRUN	2
97     { "_averunnable" },
98 
99 /* Swap */
100 #define VM_SWAPLIST	3
101 	{ "_swaplist" },/* list of free swap areas */
102 #define VM_SWDEVT	4
103 	{ "_swdevt" },	/* list of swap devices and sizes */
104 #define VM_NSWAP	5
105 	{ "_nswap" },	/* size of largest swap device */
106 #define VM_NSWDEV	6
107 	{ "_nswdev" },	/* number of swap devices */
108 #define VM_DMMAX	7
109 	{ "_dmmax" },	/* maximum size of a swap block */
110 #define X_BUFSPACE	8
111 	{ "_bufspace" },	/* K in buffer cache */
112 #define X_CNT           9
113     { "_cnt" },		        /* struct vmmeter cnt */
114 
115 /* Last pid */
116 #define X_LASTPID	10
117     { "_nextpid" },
118     { 0 }
119 };
120 
121 /*
122  *  These definitions control the format of the per-process area
123  */
124 
125 static char smp_header[] =
126   "  PID %-*.*s PRI NICE  SIZE    RES STATE  C   TIME   WCPU    CPU COMMAND";
127 
128 #define smp_Proc_format \
129 	"%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
130 
131 static char up_header[] =
132   "  PID %-*.*s PRI NICE  SIZE    RES STATE    TIME   WCPU    CPU COMMAND";
133 
134 #define up_Proc_format \
135 	"%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s"
136 
137 
138 
139 /* process state names for the "STATE" column of the display */
140 /* the extra nulls in the string "run" are for adding a slash and
141    the processor number when needed */
142 
143 char *state_abbrev[] =
144 {
145     "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB",
146 };
147 
148 
149 static kvm_t *kd;
150 
151 /* values that we stash away in _init and use in later routines */
152 
153 static double logcpu;
154 
155 /* these are retrieved from the kernel in _init */
156 
157 static load_avg  ccpu;
158 
159 /* these are offsets obtained via nlist and used in the get_ functions */
160 
161 static unsigned long cp_time_offset;
162 static unsigned long avenrun_offset;
163 static unsigned long lastpid_offset;
164 static long lastpid;
165 static unsigned long cnt_offset;
166 static unsigned long bufspace_offset;
167 static long cnt;
168 
169 /* these are for calculating cpu state percentages */
170 
171 static long cp_time[CPUSTATES];
172 static long cp_old[CPUSTATES];
173 static long cp_diff[CPUSTATES];
174 
175 /* these are for detailing the process states */
176 
177 int process_states[6];
178 char *procstatenames[] = {
179     "", " starting, ", " running, ", " sleeping, ", " stopped, ",
180     " zombie, ",
181     NULL
182 };
183 
184 /* these are for detailing the cpu states */
185 
186 int cpu_states[CPUSTATES];
187 char *cpustatenames[] = {
188     "user", "nice", "system", "interrupt", "idle", NULL
189 };
190 
191 /* these are for detailing the memory statistics */
192 
193 int memory_stats[7];
194 char *memorynames[] = {
195     "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
196     NULL
197 };
198 
199 int swap_stats[7];
200 char *swapnames[] = {
201 /*   0           1            2           3            4       5 */
202     "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
203     NULL
204 };
205 
206 
207 /* these are for keeping track of the proc array */
208 
209 static int nproc;
210 static int onproc = -1;
211 static int pref_len;
212 static struct kinfo_proc *pbase;
213 static struct kinfo_proc **pref;
214 
215 /* these are for getting the memory statistics */
216 
217 static int pageshift;		/* log base 2 of the pagesize */
218 
219 /* define pagetok in terms of pageshift */
220 
221 #define pagetok(size) ((size) << pageshift)
222 
223 /* useful externals */
224 long percentages();
225 
226 #ifdef ORDER
227 /* sorting orders. first is default */
228 char *ordernames[] = {
229     "cpu", "size", "res", "time", "pri", NULL
230 };
231 #endif
232 
233 int
234 machine_init(statics)
235 
236 struct statics *statics;
237 
238 {
239     register int i = 0;
240     register int pagesize;
241     int modelen;
242     struct passwd *pw;
243 
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))
248 	    smpmode = 0;
249 
250     while ((pw = getpwent()) != NULL) {
251 	if (strlen(pw->pw_name) > namelength)
252 	    namelength = strlen(pw->pw_name);
253     }
254     if (namelength < 8)
255 	namelength = 8;
256     if (namelength > 16)
257 	namelength = 16;
258 
259     if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
260 	return -1;
261 
262 
263     /* get the list of symbols we want to access in the kernel */
264     (void) kvm_nlist(kd, nlst);
265     if (nlst[0].n_type == 0)
266     {
267 	fprintf(stderr, "top: nlist failed\n");
268 	return(-1);
269     }
270 
271     /* make sure they were all found */
272     if (i > 0 && check_nlist(nlst) > 0)
273     {
274 	return(-1);
275     }
276 
277     (void) getkval(nlst[X_CCPU].n_value,   (int *)(&ccpu),	sizeof(ccpu),
278 	    nlst[X_CCPU].n_name);
279 
280     /* stash away certain offsets for later use */
281     cp_time_offset = nlst[X_CP_TIME].n_value;
282     avenrun_offset = nlst[X_AVENRUN].n_value;
283     lastpid_offset =  nlst[X_LASTPID].n_value;
284     cnt_offset = nlst[X_CNT].n_value;
285     bufspace_offset = nlst[X_BUFSPACE].n_value;
286 
287     /* this is used in calculating WCPU -- calculate it ahead of time */
288     logcpu = log(loaddouble(ccpu));
289 
290     pbase = NULL;
291     pref = NULL;
292     nproc = 0;
293     onproc = -1;
294     /* get the page size with "getpagesize" and calculate pageshift from it */
295     pagesize = getpagesize();
296     pageshift = 0;
297     while (pagesize > 1)
298     {
299 	pageshift++;
300 	pagesize >>= 1;
301     }
302 
303     /* we only need the amount of log(2)1024 for our conversion */
304     pageshift -= LOG1024;
305 
306     /* fill in the statics information */
307     statics->procstate_names = procstatenames;
308     statics->cpustate_names = cpustatenames;
309     statics->memory_names = memorynames;
310     statics->swap_names = swapnames;
311 #ifdef ORDER
312     statics->order_names = ordernames;
313 #endif
314 
315     /* all done! */
316     return(0);
317 }
318 
319 char *format_header(uname_field)
320 
321 register char *uname_field;
322 
323 {
324     register char *ptr;
325     static char Header[128];
326 
327     snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
328 	     namelength, namelength, uname_field);
329 
330     cmdlength = 80 - strlen(Header) + 6;
331 
332     return Header;
333 }
334 
335 static int swappgsin = -1;
336 static int swappgsout = -1;
337 extern struct timeval timeout;
338 
339 void
340 get_system_info(si)
341 
342 struct system_info *si;
343 
344 {
345     long total;
346     load_avg avenrun[3];
347     int mib[2];
348     struct timeval boottime;
349     size_t bt_size;
350 
351     /* get the cp_time array */
352     (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
353 		   nlst[X_CP_TIME].n_name);
354     (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
355 		   nlst[X_AVENRUN].n_name);
356 
357     (void) getkval(lastpid_offset, (int *)(&lastpid), sizeof(lastpid),
358 		   "!");
359 
360     /* convert load averages to doubles */
361     {
362 	register int i;
363 	register double *infoloadp;
364 	load_avg *avenrunp;
365 
366 #ifdef notyet
367 	struct loadavg sysload;
368 	int size;
369 	getkerninfo(KINFO_LOADAVG, &sysload, &size, 0);
370 #endif
371 
372 	infoloadp = si->load_avg;
373 	avenrunp = avenrun;
374 	for (i = 0; i < 3; i++)
375 	{
376 #ifdef notyet
377 	    *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
378 #endif
379 	    *infoloadp++ = loaddouble(*avenrunp++);
380 	}
381     }
382 
383     /* convert cp_time counts to percentages */
384     total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
385 
386     /* sum memory & swap statistics */
387     {
388 	struct vmmeter sum;
389 	static unsigned int swap_delay = 0;
390 	static int swapavail = 0;
391 	static int swapfree = 0;
392 	static int bufspace = 0;
393 
394         (void) getkval(cnt_offset, (int *)(&sum), sizeof(sum),
395 		   "_cnt");
396         (void) getkval(bufspace_offset, (int *)(&bufspace), sizeof(bufspace),
397 		   "_bufspace");
398 
399 	/* convert memory stats to Kbytes */
400 	memory_stats[0] = pagetok(sum.v_active_count);
401 	memory_stats[1] = pagetok(sum.v_inactive_count);
402 	memory_stats[2] = pagetok(sum.v_wire_count);
403 	memory_stats[3] = pagetok(sum.v_cache_count);
404 	memory_stats[4] = bufspace / 1024;
405 	memory_stats[5] = pagetok(sum.v_free_count);
406 	memory_stats[6] = -1;
407 
408 	/* first interval */
409         if (swappgsin < 0) {
410 	    swap_stats[4] = 0;
411 	    swap_stats[5] = 0;
412 	}
413 
414 	/* compute differences between old and new swap statistic */
415 	else {
416 	    swap_stats[4] = pagetok(((sum.v_swappgsin - swappgsin)));
417 	    swap_stats[5] = pagetok(((sum.v_swappgsout - swappgsout)));
418 	}
419 
420         swappgsin = sum.v_swappgsin;
421 	swappgsout = sum.v_swappgsout;
422 
423 	/* call CPU heavy swapmode() only for changes */
424         if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
425 	    swap_stats[3] = swapmode(&swapavail, &swapfree);
426 	    swap_stats[0] = swapavail;
427 	    swap_stats[1] = swapavail - swapfree;
428 	    swap_stats[2] = swapfree;
429 	}
430         swap_delay = 1;
431 	swap_stats[6] = -1;
432     }
433 
434     /* set arrays and strings */
435     si->cpustates = cpu_states;
436     si->memory = memory_stats;
437     si->swap = swap_stats;
438 
439 
440     if(lastpid > 0) {
441 	si->last_pid = lastpid;
442     } else {
443 	si->last_pid = -1;
444     }
445 
446     /*
447      * Print how long system has been up.
448      * (Found by looking getting "boottime" from the kernel)
449      */
450     mib[0] = CTL_KERN;
451     mib[1] = KERN_BOOTTIME;
452     bt_size = sizeof(boottime);
453     if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 &&
454 	boottime.tv_sec != 0) {
455 	si->boottime = boottime;
456     } else {
457 	si->boottime.tv_sec = -1;
458     }
459 }
460 
461 static struct handle handle;
462 
463 caddr_t get_process_info(si, sel, compare)
464 
465 struct system_info *si;
466 struct process_select *sel;
467 int (*compare)();
468 
469 {
470     register int i;
471     register int total_procs;
472     register int active_procs;
473     register struct kinfo_proc **prefp;
474     register struct kinfo_proc *pp;
475 
476     /* these are copied out of sel for speed */
477     int show_idle;
478     int show_self;
479     int show_system;
480     int show_uid;
481     int show_command;
482 
483 
484     pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
485     if (nproc > onproc)
486 	pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
487 		* (onproc = nproc));
488     if (pref == NULL || pbase == NULL) {
489 	(void) fprintf(stderr, "top: Out of memory.\n");
490 	quit(23);
491     }
492     /* get a pointer to the states summary array */
493     si->procstates = process_states;
494 
495     /* set up flags which define what we are going to select */
496     show_idle = sel->idle;
497     show_self = sel->self;
498     show_system = sel->system;
499     show_uid = sel->uid != -1;
500     show_command = sel->command != NULL;
501 
502     /* count up process states and get pointers to interesting procs */
503     total_procs = 0;
504     active_procs = 0;
505     memset((char *)process_states, 0, sizeof(process_states));
506     prefp = pref;
507     for (pp = pbase, i = 0; i < nproc; pp++, i++)
508     {
509 	/*
510 	 *  Place pointers to each valid proc structure in pref[].
511 	 *  Process slots that are actually in use have a non-zero
512 	 *  status field.  Processes with P_SYSTEM set are system
513 	 *  processes---these get ignored unless show_sysprocs is set.
514 	 */
515 	if (PP(pp, p_stat) != 0 &&
516 	    (show_self != PP(pp, p_pid)) &&
517 	    (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0)))
518 	{
519 	    total_procs++;
520 	    process_states[(unsigned char) PP(pp, p_stat)]++;
521 	    if ((PP(pp, p_stat) != SZOMB) &&
522 		(show_idle || (PP(pp, p_pctcpu) != 0) ||
523 		 (PP(pp, p_stat) == SRUN)) &&
524 		(!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t)sel->uid))
525 	    {
526 		*prefp++ = pp;
527 		active_procs++;
528 	    }
529 	}
530     }
531 
532     /* if requested, sort the "interesting" processes */
533     if (compare != NULL)
534     {
535 	qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
536     }
537 
538     /* remember active and total counts */
539     si->p_total = total_procs;
540     si->p_active = pref_len = active_procs;
541 
542     /* pass back a handle */
543     handle.next_proc = pref;
544     handle.remaining = active_procs;
545     return((caddr_t)&handle);
546 }
547 
548 char fmt[128];		/* static area where result is built */
549 
550 char *format_next_process(handle, get_userid)
551 
552 caddr_t handle;
553 char *(*get_userid)();
554 
555 {
556     register struct kinfo_proc *pp;
557     register long cputime;
558     register double pct;
559     struct handle *hp;
560     char status[16];
561 
562     /* find and remember the next proc structure */
563     hp = (struct handle *)handle;
564     pp = *(hp->next_proc++);
565     hp->remaining--;
566 
567 
568     /* get the process's user struct and set cputime */
569     if ((PP(pp, p_flag) & P_INMEM) == 0) {
570 	/*
571 	 * Print swapped processes as <pname>
572 	 */
573 	char *comm = PP(pp, p_comm);
574 #define COMSIZ sizeof(PP(pp, p_comm))
575 	char buf[COMSIZ];
576 	(void) strncpy(buf, comm, COMSIZ);
577 	comm[0] = '<';
578 	(void) strncpy(&comm[1], buf, COMSIZ - 2);
579 	comm[COMSIZ - 2] = '\0';
580 	(void) strncat(comm, ">", COMSIZ - 1);
581 	comm[COMSIZ - 1] = '\0';
582     }
583 
584 #if 0
585     /* This does not produce the correct results */
586     cputime = PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks);
587 #endif
588     /* This does not count interrupts */
589     cputime = (PP(pp, p_runtime) / 1000 + 500) / 1000;
590 
591     /* calculate the base for cpu percentages */
592     pct = pctdouble(PP(pp, p_pctcpu));
593 
594     /* generate "STATE" field */
595     switch (PP(pp, p_stat)) {
596 	case SRUN:
597 	    if (smpmode && PP(pp, p_oncpu) >= 0)
598 		sprintf(status, "CPU%d", PP(pp, p_oncpu));
599 	    else
600 		strcpy(status, "RUN");
601 	    break;
602 	case SSLEEP:
603 	    if (PP(pp, p_wmesg) != NULL) {
604 		sprintf(status, "%.6s", EP(pp, e_wmesg));
605 		break;
606 	    }
607 	    /* fall through */
608 	default:
609 	    sprintf(status, "%.6s", state_abbrev[(unsigned char) PP(pp, p_stat)]);
610 	    break;
611     }
612 
613     /* format this entry */
614     sprintf(fmt,
615 	    smpmode ? smp_Proc_format : up_Proc_format,
616 	    PP(pp, p_pid),
617 	    namelength, namelength,
618 	    (*get_userid)(EP(pp, e_pcred.p_ruid)),
619 	    PP(pp, p_priority) - PZERO,
620 
621 	    /*
622 	     * normal time      -> nice value -20 - +20
623 	     * real time 0 - 31 -> nice value -52 - -21
624 	     * idle time 0 - 31 -> nice value +21 - +52
625 	     */
626 	    (PP(pp, p_rtprio.type) ==  RTP_PRIO_NORMAL ?
627 	    	PP(pp, p_nice) - NZERO :
628 	    	(PP(pp, p_rtprio.type) ==  RTP_PRIO_REALTIME ?
629 		    (PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio)) :
630 		    (PRIO_MAX + 1 + PP(pp, p_rtprio.prio)))),
631 	    format_k2(PROCSIZE(pp)),
632 	    format_k2(pagetok(VP(pp, vm_rssize))),
633 	    status,
634 	    smpmode ? PP(pp, p_lastcpu) : 0,
635 	    format_time(cputime),
636 	    100.0 * weighted_cpu(pct, pp),
637 	    100.0 * pct,
638 	    cmdlength,
639 	    printable(PP(pp, p_comm)));
640 
641     /* return the result */
642     return(fmt);
643 }
644 
645 
646 /*
647  * check_nlist(nlst) - checks the nlist to see if any symbols were not
648  *		found.  For every symbol that was not found, a one-line
649  *		message is printed to stderr.  The routine returns the
650  *		number of symbols NOT found.
651  */
652 
653 static int check_nlist(nlst)
654 
655 register struct nlist *nlst;
656 
657 {
658     register int i;
659 
660     /* check to see if we got ALL the symbols we requested */
661     /* this will write one line to stderr for every symbol not found */
662 
663     i = 0;
664     while (nlst->n_name != NULL)
665     {
666 	if (nlst->n_type == 0)
667 	{
668 	    /* this one wasn't found */
669 	    (void) fprintf(stderr, "kernel: no symbol named `%s'\n",
670 			   nlst->n_name);
671 	    i = 1;
672 	}
673 	nlst++;
674     }
675 
676     return(i);
677 }
678 
679 
680 /*
681  *  getkval(offset, ptr, size, refstr) - get a value out of the kernel.
682  *	"offset" is the byte offset into the kernel for the desired value,
683  *  	"ptr" points to a buffer into which the value is retrieved,
684  *  	"size" is the size of the buffer (and the object to retrieve),
685  *  	"refstr" is a reference string used when printing error meessages,
686  *	    if "refstr" starts with a '!', then a failure on read will not
687  *  	    be fatal (this may seem like a silly way to do things, but I
688  *  	    really didn't want the overhead of another argument).
689  *
690  */
691 
692 static int getkval(offset, ptr, size, refstr)
693 
694 unsigned long offset;
695 int *ptr;
696 int size;
697 char *refstr;
698 
699 {
700     if (kvm_read(kd, offset, (char *) ptr, size) != size)
701     {
702 	if (*refstr == '!')
703 	{
704 	    return(0);
705 	}
706 	else
707 	{
708 	    fprintf(stderr, "top: kvm_read for %s: %s\n",
709 		refstr, strerror(errno));
710 	    quit(23);
711 	}
712     }
713     return(1);
714 }
715 
716 /* comparison routines for qsort */
717 
718 /*
719  *  proc_compare - comparison function for "qsort"
720  *	Compares the resource consumption of two processes using five
721  *  	distinct keys.  The keys (in descending order of importance) are:
722  *  	percent cpu, cpu ticks, state, resident set size, total virtual
723  *  	memory usage.  The process states are ordered as follows (from least
724  *  	to most important):  WAIT, zombie, sleep, stop, start, run.  The
725  *  	array declaration below maps a process state index into a number
726  *  	that reflects this ordering.
727  */
728 
729 static unsigned char sorted_state[] =
730 {
731     0,	/* not used		*/
732     3,	/* sleep		*/
733     1,	/* ABANDONED (WAIT)	*/
734     6,	/* run			*/
735     5,	/* start		*/
736     2,	/* zombie		*/
737     4	/* stop			*/
738 };
739 
740 
741 #define ORDERKEY_PCTCPU \
742   if (lresult = (long) PP(p2, p_pctcpu) - (long) PP(p1, p_pctcpu), \
743      (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
744 
745 #define ORDERKEY_CPTICKS \
746   if ((result = PP(p2, p_runtime) - PP(p1, p_runtime)) == 0)
747 
748 #define ORDERKEY_STATE \
749   if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
750                 sorted_state[(unsigned char) PP(p1, p_stat)]) == 0)
751 
752 #define ORDERKEY_PRIO \
753   if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
754 
755 #define ORDERKEY_RSSIZE \
756   if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
757 
758 #define ORDERKEY_MEM \
759   if ( (result = PROCSIZE(p2) - PROCSIZE(p1)) == 0 )
760 
761 /* compare_cpu - the comparison function for sorting by cpu percentage */
762 
763 int
764 #ifdef ORDER
765 compare_cpu(pp1, pp2)
766 #else
767 proc_compare(pp1, pp2)
768 #endif
769 
770 struct proc **pp1;
771 struct proc **pp2;
772 
773 {
774     register struct kinfo_proc *p1;
775     register struct kinfo_proc *p2;
776     register int result;
777     register pctcpu lresult;
778 
779     /* remove one level of indirection */
780     p1 = *(struct kinfo_proc **) pp1;
781     p2 = *(struct kinfo_proc **) pp2;
782 
783     ORDERKEY_PCTCPU
784     ORDERKEY_CPTICKS
785     ORDERKEY_STATE
786     ORDERKEY_PRIO
787     ORDERKEY_RSSIZE
788     ORDERKEY_MEM
789     ;
790 
791     return(result);
792 }
793 
794 #ifdef ORDER
795 /* compare routines */
796 int compare_size(), compare_res(), compare_time(), compare_prio();
797 
798 int (*proc_compares[])() = {
799     compare_cpu,
800     compare_size,
801     compare_res,
802     compare_time,
803     compare_prio,
804     NULL
805 };
806 
807 /* compare_size - the comparison function for sorting by total memory usage */
808 
809 int
810 compare_size(pp1, pp2)
811 
812 struct proc **pp1;
813 struct proc **pp2;
814 
815 {
816     register struct kinfo_proc *p1;
817     register struct kinfo_proc *p2;
818     register int result;
819     register pctcpu lresult;
820 
821     /* remove one level of indirection */
822     p1 = *(struct kinfo_proc **) pp1;
823     p2 = *(struct kinfo_proc **) pp2;
824 
825     ORDERKEY_MEM
826     ORDERKEY_RSSIZE
827     ORDERKEY_PCTCPU
828     ORDERKEY_CPTICKS
829     ORDERKEY_STATE
830     ORDERKEY_PRIO
831     ;
832 
833     return(result);
834 }
835 
836 /* compare_res - the comparison function for sorting by resident set size */
837 
838 int
839 compare_res(pp1, pp2)
840 
841 struct proc **pp1;
842 struct proc **pp2;
843 
844 {
845     register struct kinfo_proc *p1;
846     register struct kinfo_proc *p2;
847     register int result;
848     register pctcpu lresult;
849 
850     /* remove one level of indirection */
851     p1 = *(struct kinfo_proc **) pp1;
852     p2 = *(struct kinfo_proc **) pp2;
853 
854     ORDERKEY_RSSIZE
855     ORDERKEY_MEM
856     ORDERKEY_PCTCPU
857     ORDERKEY_CPTICKS
858     ORDERKEY_STATE
859     ORDERKEY_PRIO
860     ;
861 
862     return(result);
863 }
864 
865 /* compare_time - the comparison function for sorting by total cpu time */
866 
867 int
868 compare_time(pp1, pp2)
869 
870 struct proc **pp1;
871 struct proc **pp2;
872 
873 {
874     register struct kinfo_proc *p1;
875     register struct kinfo_proc *p2;
876     register int result;
877     register pctcpu lresult;
878 
879     /* remove one level of indirection */
880     p1 = *(struct kinfo_proc **) pp1;
881     p2 = *(struct kinfo_proc **) pp2;
882 
883     ORDERKEY_CPTICKS
884     ORDERKEY_PCTCPU
885     ORDERKEY_STATE
886     ORDERKEY_PRIO
887     ORDERKEY_RSSIZE
888     ORDERKEY_MEM
889     ;
890 
891       return(result);
892   }
893 
894 /* compare_prio - the comparison function for sorting by cpu percentage */
895 
896 int
897 compare_prio(pp1, pp2)
898 
899 struct proc **pp1;
900 struct proc **pp2;
901 
902 {
903     register struct kinfo_proc *p1;
904     register struct kinfo_proc *p2;
905     register int result;
906     register pctcpu lresult;
907 
908     /* remove one level of indirection */
909     p1 = *(struct kinfo_proc **) pp1;
910     p2 = *(struct kinfo_proc **) pp2;
911 
912     ORDERKEY_PRIO
913     ORDERKEY_CPTICKS
914     ORDERKEY_PCTCPU
915     ORDERKEY_STATE
916     ORDERKEY_RSSIZE
917     ORDERKEY_MEM
918     ;
919 
920     return(result);
921 }
922 #endif
923 
924 /*
925  * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
926  *		the process does not exist.
927  *		It is EXTREMLY IMPORTANT that this function work correctly.
928  *		If top runs setuid root (as in SVR4), then this function
929  *		is the only thing that stands in the way of a serious
930  *		security problem.  It validates requests for the "kill"
931  *		and "renice" commands.
932  */
933 
934 int proc_owner(pid)
935 
936 int pid;
937 
938 {
939     register int cnt;
940     register struct kinfo_proc **prefp;
941     register struct kinfo_proc *pp;
942 
943     prefp = pref;
944     cnt = pref_len;
945     while (--cnt >= 0)
946     {
947 	pp = *prefp++;
948 	if (PP(pp, p_pid) == (pid_t)pid)
949 	{
950 	    return((int)EP(pp, e_pcred.p_ruid));
951 	}
952     }
953     return(-1);
954 }
955 
956 
957 /*
958  * swapmode is based on a program called swapinfo written
959  * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
960  */
961 
962 #define	SVAR(var) __STRING(var)	/* to force expansion */
963 #define	KGET(idx, var)							\
964 	KGET1(idx, &var, sizeof(var), SVAR(var))
965 #define	KGET1(idx, p, s, msg)						\
966 	KGET2(nlst[idx].n_value, p, s, msg)
967 #define	KGET2(addr, p, s, msg)						\
968 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {		        \
969 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));       \
970 		return (0);                                             \
971        }
972 #define	KGETRET(addr, p, s, msg)					\
973 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
974 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
975 		return (0);						\
976 	}
977 
978 
979 int
980 swapmode(retavail, retfree)
981 	int *retavail;
982 	int *retfree;
983 {
984 	char *header;
985 	int hlen, nswap, nswdev, dmmax;
986 	int i, div, avail, nfree, npfree, used;
987 	struct swdevt *sw;
988 	long blocksize, *perdev;
989 	u_long ptr;
990 	struct rlist head;
991 #if __FreeBSD_version >= 220000
992 	struct rlisthdr swaplist;
993 #else
994 	struct rlist *swaplist;
995 #endif
996 	struct rlist *swapptr;
997 
998 	/*
999 	 * Counter for error messages. If we reach the limit,
1000 	 * stop reading information from swap devices and
1001 	 * return zero. This prevent endless 'bad address'
1002 	 * messages.
1003 	 */
1004 	static warning = 10;
1005 
1006 	if (warning <= 0) {
1007 	    /* a single warning */
1008 	    if (!warning) {
1009 		warning--;
1010 		fprintf(stderr,
1011 			"Too much errors, stop reading swap devices ...\n");
1012 		(void)sleep(3);
1013 	    }
1014 	    return(0);
1015 	}
1016 	warning--; /* decrease counter, see end of function */
1017 
1018 	KGET(VM_NSWAP, nswap);
1019 	if (!nswap) {
1020 		fprintf(stderr, "No swap space available\n");
1021 		return(0);
1022 	}
1023 
1024 	KGET(VM_NSWDEV, nswdev);
1025 	KGET(VM_DMMAX, dmmax);
1026 	KGET1(VM_SWAPLIST, &swaplist, sizeof(swaplist), "swaplist");
1027 	if ((sw = (struct swdevt *)malloc(nswdev * sizeof(*sw))) == NULL ||
1028 	    (perdev = (long *)malloc(nswdev * sizeof(*perdev))) == NULL)
1029 		err(1, "malloc");
1030 	KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt");
1031 	KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt");
1032 
1033 	/* Count up swap space. */
1034 	nfree = 0;
1035 	memset(perdev, 0, nswdev * sizeof(*perdev));
1036 #if  __FreeBSD_version >= 220000
1037 	swapptr = swaplist.rlh_list;
1038 	while (swapptr) {
1039 #else
1040 	while (swaplist) {
1041 #endif
1042 		int	top, bottom, next_block;
1043 #if  __FreeBSD_version >= 220000
1044 		KGET2(swapptr, &head, sizeof(struct rlist), "swapptr");
1045 #else
1046 		KGET2(swaplist, &head, sizeof(struct rlist), "swaplist");
1047 #endif
1048 
1049 		top = head.rl_end;
1050 		bottom = head.rl_start;
1051 
1052 		nfree += top - bottom + 1;
1053 
1054 		/*
1055 		 * Swap space is split up among the configured disks.
1056 		 *
1057 		 * For interleaved swap devices, the first dmmax blocks
1058 		 * of swap space some from the first disk, the next dmmax
1059 		 * blocks from the next, and so on up to nswap blocks.
1060 		 *
1061 		 * The list of free space joins adjacent free blocks,
1062 		 * ignoring device boundries.  If we want to keep track
1063 		 * of this information per device, we'll just have to
1064 		 * extract it ourselves.
1065 		 */
1066 		while (top / dmmax != bottom / dmmax) {
1067 			next_block = ((bottom + dmmax) / dmmax);
1068 			perdev[(bottom / dmmax) % nswdev] +=
1069 				next_block * dmmax - bottom;
1070 			bottom = next_block * dmmax;
1071 		}
1072 		perdev[(bottom / dmmax) % nswdev] +=
1073 			top - bottom + 1;
1074 
1075 #if  __FreeBSD_version >= 220000
1076 		swapptr = head.rl_next;
1077 #else
1078 		swaplist = head.rl_next;
1079 #endif
1080 	}
1081 
1082 	header = getbsize(&hlen, &blocksize);
1083 	div = blocksize / 512;
1084 	avail = npfree = 0;
1085 	for (i = 0; i < nswdev; i++) {
1086 		int xsize, xfree;
1087 
1088 		/*
1089 		 * Don't report statistics for partitions which have not
1090 		 * yet been activated via swapon(8).
1091 		 */
1092 		if (!(sw[i].sw_flags & SW_FREED))
1093 			continue;
1094 
1095 		/* The first dmmax is never allocated to avoid trashing of
1096 		 * disklabels
1097 		 */
1098 		xsize = sw[i].sw_nblks - dmmax;
1099 		xfree = perdev[i];
1100 		used = xsize - xfree;
1101 		npfree++;
1102 		avail += xsize;
1103 	}
1104 
1105 	/*
1106 	 * If only one partition has been set up via swapon(8), we don't
1107 	 * need to bother with totals.
1108 	 */
1109 	*retavail = avail / 2;
1110 	*retfree = nfree / 2;
1111 	used = avail - nfree;
1112 	free(sw); free(perdev);
1113 
1114 	/* increase counter, no errors occurs */
1115 	warning++;
1116 
1117 	return  (int)(((double)used / (double)avail * 100.0) + 0.5);
1118 }
1119