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