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