Annotation of src/usr.bin/top/machine.c, Revision 1.41
1.41 ! deraadt 1: /* $OpenBSD: machine.c,v 1.40 2004/06/11 01:32:11 deraadt Exp $ */
1.28 tholo 2:
3: /*-
4: * Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com>
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. The name of the author may not be used to endorse or promote products
16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 downsj 28: *
29: * AUTHOR: Thorsten Lockert <tholo@sigmasoft.com>
30: * Adapted from BSD4.4 by Christos Zoulas <christos@ee.cornell.edu>
31: * Patch for process wait display by Jarl F. Greipsland <jarle@idt.unit.no>
1.11 kstailey 32: * Patch for -DORDER by Kenneth Stailey <kstailey@disclosure.com>
1.15 weingart 33: * Patch for new swapctl(2) by Tobias Weingartner <weingart@openbsd.org>
1.1 downsj 34: */
35:
36: #include <sys/types.h>
37: #include <sys/signal.h>
38: #include <sys/param.h>
39: #include <stdio.h>
40: #include <stdlib.h>
1.3 downsj 41: #include <string.h>
1.6 millert 42: #include <limits.h>
43: #include <err.h>
1.1 downsj 44: #include <math.h>
45: #include <unistd.h>
46: #include <sys/errno.h>
47: #include <sys/sysctl.h>
48: #include <sys/dir.h>
49: #include <sys/dkstat.h>
50: #include <sys/file.h>
51: #include <sys/time.h>
52: #include <sys/resource.h>
1.15 weingart 53: #include <sys/swap.h>
1.1 downsj 54: #include <err.h>
55:
56: #include "top.h"
1.3 downsj 57: #include "display.h"
1.1 downsj 58: #include "machine.h"
59: #include "utils.h"
1.31 deraadt 60: #include "loadavg.h"
61:
1.38 deraadt 62: static int swapmode(int *, int *);
1.1 downsj 63:
64: /* get_process_info passes back a handle. This is what it looks like: */
65:
1.20 deraadt 66: struct handle {
1.37 millert 67: struct kinfo_proc2 **next_proc; /* points to next valid proc pointer */
1.38 deraadt 68: int remaining; /* number of pointers remaining */
1.1 downsj 69: };
70:
71: /* what we consider to be process size: */
1.37 millert 72: #define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize)
1.1 downsj 73:
74: /*
75: * These definitions control the format of the per-process area
76: */
1.38 deraadt 77: static char header[] =
1.40 deraadt 78: " PID X PRI NICE SIZE RES STATE WAIT TIME CPU COMMAND";
1.31 deraadt 79:
1.1 downsj 80: /* 0123456 -- field to fill in starts at header+6 */
81: #define UNAME_START 6
82:
83: #define Proc_format \
1.40 deraadt 84: "%5d %-8.8s %3d %4d %5s %5s %-8s %-6.6s %6s %5.2f%% %.11s"
1.1 downsj 85:
86: /* process state names for the "STATE" column of the display */
1.30 deraadt 87: /*
88: * the extra nulls in the string "run" are for adding a slash and the
89: * processor number when needed
90: */
1.1 downsj 91:
1.30 deraadt 92: char *state_abbrev[] = {
1.40 deraadt 93: "", "start", "run", "sleep", "stop", "zomb", "dead", "onproc"
1.1 downsj 94: };
95:
1.30 deraadt 96: static int stathz;
1.1 downsj 97:
98: /* these are for calculating cpu state percentages */
1.30 deraadt 99: static long cp_time[CPUSTATES];
100: static long cp_old[CPUSTATES];
101: static long cp_diff[CPUSTATES];
1.1 downsj 102:
103: /* these are for detailing the process states */
1.30 deraadt 104: int process_states[7];
105: char *procstatenames[] = {
106: "", " starting, ", " running, ", " idle, ",
107: " stopped, ", " zombie, ",
1.20 deraadt 108: NULL
1.1 downsj 109: };
110:
111: /* these are for detailing the cpu states */
1.30 deraadt 112: int cpu_states[CPUSTATES];
113: char *cpustatenames[] = {
1.20 deraadt 114: "user", "nice", "system", "interrupt", "idle", NULL
1.1 downsj 115: };
116:
117: /* these are for detailing the memory statistics */
1.30 deraadt 118: int memory_stats[8];
119: char *memorynames[] = {
1.20 deraadt 120: "Real: ", "K/", "K act/tot ", "Free: ", "K ",
121: "Swap: ", "K/", "K used/tot",
122: NULL
1.1 downsj 123: };
124:
1.11 kstailey 125: /* these are names given to allowed sorting orders -- first is default */
1.31 deraadt 126: char *ordernames[] = {
127: "cpu", "size", "res", "time", "pri", NULL
128: };
1.11 kstailey 129:
1.1 downsj 130: /* these are for keeping track of the proc array */
1.30 deraadt 131: static int nproc;
132: static int onproc = -1;
133: static int pref_len;
1.37 millert 134: static struct kinfo_proc2 *pbase;
135: static struct kinfo_proc2 **pref;
1.1 downsj 136:
137: /* these are for getting the memory statistics */
1.30 deraadt 138: static int pageshift; /* log base 2 of the pagesize */
1.1 downsj 139:
140: /* define pagetok in terms of pageshift */
141: #define pagetok(size) ((size) << pageshift)
142:
1.41 ! deraadt 143: int ncpu;
! 144:
1.33 millert 145: unsigned int maxslp;
1.26 art 146:
1.36 deraadt 147: static int
1.29 pvalchev 148: getstathz(void)
1.18 deraadt 149: {
150: struct clockinfo cinf;
1.30 deraadt 151: size_t size = sizeof(cinf);
152: int mib[2];
1.18 deraadt 153:
154: mib[0] = CTL_KERN;
155: mib[1] = KERN_CLOCKRATE;
156: if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
157: return (-1);
158: return (cinf.stathz);
159: }
160:
161: int
1.29 pvalchev 162: machine_init(struct statics *statics)
1.1 downsj 163: {
1.41 ! deraadt 164: size_t size = sizeof(ncpu);
! 165: int mib[2], pagesize;
! 166:
! 167: mib[0] = CTL_HW;
! 168: mib[1] = HW_NCPU;
! 169: if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1)
! 170: return (-1);
1.20 deraadt 171:
172: stathz = getstathz();
173: if (stathz == -1)
174: return (-1);
175:
176: pbase = NULL;
177: pref = NULL;
178: onproc = -1;
179: nproc = 0;
180:
1.30 deraadt 181: /*
182: * get the page size with "getpagesize" and calculate pageshift from
183: * it
184: */
1.20 deraadt 185: pagesize = getpagesize();
186: pageshift = 0;
187: while (pagesize > 1) {
188: pageshift++;
189: pagesize >>= 1;
190: }
191:
192: /* we only need the amount of log(2)1024 for our conversion */
193: pageshift -= LOG1024;
194:
195: /* fill in the statics information */
196: statics->procstate_names = procstatenames;
197: statics->cpustate_names = cpustatenames;
198: statics->memory_names = memorynames;
199: statics->order_names = ordernames;
200: return (0);
1.1 downsj 201: }
202:
1.20 deraadt 203: char *
1.29 pvalchev 204: format_header(char *uname_field)
1.1 downsj 205: {
1.20 deraadt 206: char *ptr;
1.1 downsj 207:
1.20 deraadt 208: ptr = header + UNAME_START;
1.30 deraadt 209: while (*uname_field != '\0')
1.20 deraadt 210: *ptr++ = *uname_field++;
211: return (header);
1.1 downsj 212: }
213:
214: void
1.31 deraadt 215: get_system_info(struct system_info *si)
1.1 downsj 216: {
1.20 deraadt 217: static int sysload_mib[] = {CTL_VM, VM_LOADAVG};
218: static int vmtotal_mib[] = {CTL_VM, VM_METER};
1.30 deraadt 219: static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
1.1 downsj 220: struct loadavg sysload;
1.20 deraadt 221: struct vmtotal vmtotal;
222: double *infoloadp;
1.30 deraadt 223: size_t size;
1.35 deraadt 224: int i;
1.30 deraadt 225:
1.21 deraadt 226: size = sizeof(cp_time);
1.35 deraadt 227: if (sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0) < 0)
1.21 deraadt 228: warn("sysctl kern.cp_time failed");
1.35 deraadt 229:
1.20 deraadt 230: size = sizeof(sysload);
1.35 deraadt 231: if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0)
1.20 deraadt 232: warn("sysctl failed");
1.1 downsj 233: infoloadp = si->load_avg;
234: for (i = 0; i < 3; i++)
1.20 deraadt 235: *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
1.1 downsj 236:
1.20 deraadt 237: /* convert cp_time counts to percentages */
1.35 deraadt 238: (void) percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
1.1 downsj 239:
240: /* get total -- systemwide main memory usage structure */
1.20 deraadt 241: size = sizeof(vmtotal);
242: if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) {
243: warn("sysctl failed");
244: bzero(&vmtotal, sizeof(vmtotal));
1.1 downsj 245: }
246: /* convert memory stats to Kbytes */
247: memory_stats[0] = -1;
1.20 deraadt 248: memory_stats[1] = pagetok(vmtotal.t_arm);
249: memory_stats[2] = pagetok(vmtotal.t_rm);
1.1 downsj 250: memory_stats[3] = -1;
1.20 deraadt 251: memory_stats[4] = pagetok(vmtotal.t_free);
1.1 downsj 252: memory_stats[5] = -1;
1.31 deraadt 253:
1.1 downsj 254: if (!swapmode(&memory_stats[6], &memory_stats[7])) {
1.20 deraadt 255: memory_stats[6] = 0;
256: memory_stats[7] = 0;
1.1 downsj 257: }
258:
1.20 deraadt 259: /* set arrays and strings */
260: si->cpustates = cpu_states;
261: si->memory = memory_stats;
262: si->last_pid = -1;
1.1 downsj 263: }
264:
265: static struct handle handle;
266:
1.37 millert 267: static struct kinfo_proc2 *
1.29 pvalchev 268: getprocs(int op, int arg, int *cnt)
1.22 deraadt 269: {
1.37 millert 270: size_t size;
271: int mib[6] = {CTL_KERN, KERN_PROC2, 0, 0, sizeof(struct kinfo_proc2), 0};
1.26 art 272: static int maxslp_mib[] = {CTL_VM, VM_MAXSLP};
1.37 millert 273: static struct kinfo_proc2 *procbase;
1.24 angelos 274: int st;
1.22 deraadt 275:
1.31 deraadt 276: mib[2] = op;
277: mib[3] = arg;
278:
1.26 art 279: size = sizeof(maxslp);
280: if (sysctl(maxslp_mib, 2, &maxslp, &size, NULL, 0) < 0) {
281: warn("sysctl vm.maxslp failed");
282: return (0);
283: }
1.37 millert 284: retry:
285: free(procbase);
286: st = sysctl(mib, 6, NULL, &size, NULL, 0);
1.22 deraadt 287: if (st == -1) {
1.37 millert 288: /* _kvm_syserr(kd, kd->program, "kvm_getproc2"); */
1.22 deraadt 289: return (0);
290: }
1.37 millert 291: size = 5 * size / 4; /* extra slop */
292: if ((procbase = malloc(size)) == NULL)
1.22 deraadt 293: return (0);
1.37 millert 294: mib[5] = (int)(size / sizeof(struct kinfo_proc2));
295: st = sysctl(mib, 6, procbase, &size, NULL, 0);
1.22 deraadt 296: if (st == -1) {
1.37 millert 297: if (errno == ENOMEM)
298: goto retry;
299: /* _kvm_syserr(kd, kd->program, "kvm_getproc2"); */
1.22 deraadt 300: return (0);
301: }
1.37 millert 302: *cnt = (int)(size / sizeof(struct kinfo_proc2));
1.22 deraadt 303: return (procbase);
304: }
305:
1.30 deraadt 306: caddr_t
1.29 pvalchev 307: get_process_info(struct system_info *si, struct process_select *sel,
1.30 deraadt 308: int (*compare) (const void *, const void *))
1.20 deraadt 309: {
1.33 millert 310: int show_idle, show_system, show_uid;
1.20 deraadt 311: int total_procs, active_procs, i;
1.37 millert 312: struct kinfo_proc2 **prefp, *pp;
1.20 deraadt 313:
1.22 deraadt 314: if ((pbase = getprocs(KERN_PROC_KTHREAD, 0, &nproc)) == NULL) {
315: /* warnx("%s", kvm_geterr(kd)); */
1.20 deraadt 316: quit(23);
317: }
318: if (nproc > onproc)
1.37 millert 319: pref = (struct kinfo_proc2 **)realloc(pref,
320: sizeof(struct kinfo_proc2 *) * (onproc = nproc));
1.20 deraadt 321: if (pref == NULL) {
322: warnx("Out of memory.");
323: quit(23);
324: }
325: /* get a pointer to the states summary array */
326: si->procstates = process_states;
1.1 downsj 327:
1.20 deraadt 328: /* set up flags which define what we are going to select */
329: show_idle = sel->idle;
330: show_system = sel->system;
1.33 millert 331: show_uid = sel->uid != (uid_t)-1;
1.20 deraadt 332:
333: /* count up process states and get pointers to interesting procs */
334: total_procs = 0;
335: active_procs = 0;
336: memset((char *) process_states, 0, sizeof(process_states));
337: prefp = pref;
338: for (pp = pbase, i = 0; i < nproc; pp++, i++) {
339: /*
340: * Place pointers to each valid proc structure in pref[].
341: * Process slots that are actually in use have a non-zero
342: * status field. Processes with SSYS set are system
343: * processes---these get ignored unless show_sysprocs is set.
344: */
1.37 millert 345: if (pp->p_stat != 0 &&
346: (show_system || (pp->p_flag & P_SYSTEM) == 0)) {
1.20 deraadt 347: total_procs++;
1.37 millert 348: process_states[(unsigned char) pp->p_stat]++;
349: if (pp->p_stat != SZOMB &&
350: (show_idle || pp->p_pctcpu != 0 ||
351: pp->p_stat == SRUN) &&
352: (!show_uid || pp->p_ruid == sel->uid)) {
1.20 deraadt 353: *prefp++ = pp;
354: active_procs++;
355: }
356: }
357: }
358:
359: /* if requested, sort the "interesting" processes */
1.30 deraadt 360: if (compare != NULL)
361: qsort((char *) pref, active_procs,
1.37 millert 362: sizeof(struct kinfo_proc2 *), compare);
1.20 deraadt 363: /* remember active and total counts */
364: si->p_total = total_procs;
365: si->p_active = pref_len = active_procs;
366:
367: /* pass back a handle */
368: handle.next_proc = pref;
369: handle.remaining = active_procs;
370: return ((caddr_t) & handle);
371: }
372:
1.30 deraadt 373: char fmt[MAX_COLS]; /* static area where result is built */
1.20 deraadt 374:
375: char *
1.40 deraadt 376: state_abbr(struct kinfo_proc2 *pp)
377: {
378: static char buf[10];
379:
1.41 ! deraadt 380: if (ncpu > 1)
! 381: snprintf(buf, sizeof buf, "%s/%d",
! 382: state_abbrev[(unsigned char)pp->p_stat], pp->p_cpuid);
! 383: else
! 384: snprintf(buf, sizeof buf, "%s",
! 385: state_abbrev[(unsigned char)pp->p_stat]);
1.40 deraadt 386: return buf;
387: }
388:
389: char *
1.36 deraadt 390: format_next_process(caddr_t handle, char *(*get_userid)(uid_t))
1.20 deraadt 391: {
1.30 deraadt 392: char *p_wait, waddr[sizeof(void *) * 2 + 3]; /* Hexify void pointer */
1.37 millert 393: struct kinfo_proc2 *pp;
1.20 deraadt 394: struct handle *hp;
395: int cputime;
396: double pct;
397:
398: /* find and remember the next proc structure */
399: hp = (struct handle *) handle;
400: pp = *(hp->next_proc++);
401: hp->remaining--;
402:
1.37 millert 403: if ((pp->p_flag & P_INMEM) == 0) {
1.20 deraadt 404: /*
405: * Print swapped processes as <pname>
406: */
1.37 millert 407: char buf[sizeof(pp->p_comm)];
1.30 deraadt 408:
1.37 millert 409: (void) strlcpy(buf, pp->p_comm, sizeof(buf));
410: (void) snprintf(pp->p_comm, sizeof(pp->p_comm), "<%s>", buf);
1.20 deraadt 411: }
1.37 millert 412: cputime = (pp->p_uticks + pp->p_sticks + pp->p_iticks) / stathz;
1.20 deraadt 413:
414: /* calculate the base for cpu percentages */
1.37 millert 415: pct = pctdouble(pp->p_pctcpu);
1.20 deraadt 416:
1.37 millert 417: if (pp->p_wchan) {
418: if (pp->p_wmesg)
419: p_wait = pp->p_wmesg;
1.20 deraadt 420: else {
1.37 millert 421: snprintf(waddr, sizeof(waddr), "%llx",
422: pp->p_wchan & ~KERNBASE);
1.20 deraadt 423: p_wait = waddr;
424: }
1.30 deraadt 425: } else
1.20 deraadt 426: p_wait = "-";
427:
428: /* format this entry */
1.30 deraadt 429: snprintf(fmt, sizeof fmt, Proc_format,
1.37 millert 430: pp->p_pid, (*get_userid)(pp->p_ruid),
431: pp->p_priority - PZERO, pp->p_nice - NZERO,
1.1 downsj 432: format_k(pagetok(PROCSIZE(pp))),
1.37 millert 433: format_k(pagetok(pp->p_vm_rssize)),
434: (pp->p_stat == SSLEEP && pp->p_slptime > maxslp) ?
1.40 deraadt 435: "idle" : state_abbr(pp),
1.30 deraadt 436: p_wait, format_time(cputime), 100.0 * pct,
1.37 millert 437: printable(pp->p_comm));
1.1 downsj 438:
1.20 deraadt 439: /* return the result */
440: return (fmt);
1.1 downsj 441: }
442:
443: /* comparison routine for qsort */
1.11 kstailey 444: static unsigned char sorted_state[] =
445: {
1.20 deraadt 446: 0, /* not used */
447: 4, /* start */
448: 5, /* run */
449: 2, /* sleep */
450: 3, /* stop */
451: 1 /* zombie */
1.11 kstailey 452: };
453:
454: /*
455: * proc_compares - comparison functions for "qsort"
456: */
457:
458: /*
459: * First, the possible comparison keys. These are defined in such a way
460: * that they can be merely listed in the source code to define the actual
461: * desired ordering.
462: */
463:
464: #define ORDERKEY_PCTCPU \
1.37 millert 465: if (lresult = (pctcpu)p2->p_pctcpu - (pctcpu)p1->p_pctcpu, \
1.22 deraadt 466: (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
1.11 kstailey 467: #define ORDERKEY_CPUTIME \
1.37 millert 468: if ((result = p2->p_rtime_sec - p1->p_rtime_sec) == 0) \
469: if ((result = p2->p_rtime_usec - p1->p_rtime_usec) == 0)
1.11 kstailey 470: #define ORDERKEY_STATE \
1.37 millert 471: if ((result = sorted_state[(unsigned char)p2->p_stat] - \
472: sorted_state[(unsigned char)p1->p_stat]) == 0)
1.11 kstailey 473: #define ORDERKEY_PRIO \
1.37 millert 474: if ((result = p2->p_priority - p1->p_priority) == 0)
1.11 kstailey 475: #define ORDERKEY_RSSIZE \
1.37 millert 476: if ((result = p2->p_vm_rssize - p1->p_vm_rssize) == 0)
1.11 kstailey 477: #define ORDERKEY_MEM \
478: if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
479:
480: /* compare_cpu - the comparison function for sorting by cpu percentage */
1.36 deraadt 481: static int
1.29 pvalchev 482: compare_cpu(const void *v1, const void *v2)
1.11 kstailey 483: {
1.20 deraadt 484: struct proc **pp1 = (struct proc **) v1;
485: struct proc **pp2 = (struct proc **) v2;
1.37 millert 486: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 487: pctcpu lresult;
1.20 deraadt 488: int result;
489:
490: /* remove one level of indirection */
1.37 millert 491: p1 = *(struct kinfo_proc2 **) pp1;
492: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 493:
494: ORDERKEY_PCTCPU
1.30 deraadt 495: ORDERKEY_CPUTIME
496: ORDERKEY_STATE
497: ORDERKEY_PRIO
498: ORDERKEY_RSSIZE
499: ORDERKEY_MEM
500: ;
1.20 deraadt 501: return (result);
1.11 kstailey 502: }
503:
504: /* compare_size - the comparison function for sorting by total memory usage */
1.36 deraadt 505: static int
1.29 pvalchev 506: compare_size(const void *v1, const void *v2)
1.11 kstailey 507: {
1.20 deraadt 508: struct proc **pp1 = (struct proc **) v1;
509: struct proc **pp2 = (struct proc **) v2;
1.37 millert 510: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 511: pctcpu lresult;
1.20 deraadt 512: int result;
513:
514: /* remove one level of indirection */
1.37 millert 515: p1 = *(struct kinfo_proc2 **) pp1;
516: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 517:
518: ORDERKEY_MEM
1.30 deraadt 519: ORDERKEY_RSSIZE
520: ORDERKEY_PCTCPU
521: ORDERKEY_CPUTIME
522: ORDERKEY_STATE
523: ORDERKEY_PRIO
524: ;
1.20 deraadt 525: return (result);
1.11 kstailey 526: }
527:
528: /* compare_res - the comparison function for sorting by resident set size */
1.36 deraadt 529: static int
1.29 pvalchev 530: compare_res(const void *v1, const void *v2)
1.11 kstailey 531: {
1.20 deraadt 532: struct proc **pp1 = (struct proc **) v1;
533: struct proc **pp2 = (struct proc **) v2;
1.37 millert 534: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 535: pctcpu lresult;
1.20 deraadt 536: int result;
537:
538: /* remove one level of indirection */
1.37 millert 539: p1 = *(struct kinfo_proc2 **) pp1;
540: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 541:
542: ORDERKEY_RSSIZE
1.30 deraadt 543: ORDERKEY_MEM
544: ORDERKEY_PCTCPU
545: ORDERKEY_CPUTIME
546: ORDERKEY_STATE
547: ORDERKEY_PRIO
548: ;
1.20 deraadt 549: return (result);
1.11 kstailey 550: }
551:
552: /* compare_time - the comparison function for sorting by CPU time */
1.36 deraadt 553: static int
1.29 pvalchev 554: compare_time(const void *v1, const void *v2)
1.11 kstailey 555: {
1.20 deraadt 556: struct proc **pp1 = (struct proc **) v1;
557: struct proc **pp2 = (struct proc **) v2;
1.37 millert 558: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 559: pctcpu lresult;
1.20 deraadt 560: int result;
561:
562: /* remove one level of indirection */
1.37 millert 563: p1 = *(struct kinfo_proc2 **) pp1;
564: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 565:
566: ORDERKEY_CPUTIME
1.30 deraadt 567: ORDERKEY_PCTCPU
568: ORDERKEY_STATE
569: ORDERKEY_PRIO
570: ORDERKEY_MEM
571: ORDERKEY_RSSIZE
572: ;
1.20 deraadt 573: return (result);
1.11 kstailey 574: }
575:
576: /* compare_prio - the comparison function for sorting by CPU time */
1.36 deraadt 577: static int
1.29 pvalchev 578: compare_prio(const void *v1, const void *v2)
1.11 kstailey 579: {
1.30 deraadt 580: struct proc **pp1 = (struct proc **) v1;
581: struct proc **pp2 = (struct proc **) v2;
1.37 millert 582: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 583: pctcpu lresult;
1.20 deraadt 584: int result;
585:
586: /* remove one level of indirection */
1.37 millert 587: p1 = *(struct kinfo_proc2 **) pp1;
588: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 589:
590: ORDERKEY_PRIO
1.30 deraadt 591: ORDERKEY_PCTCPU
592: ORDERKEY_CPUTIME
593: ORDERKEY_STATE
594: ORDERKEY_RSSIZE
595: ORDERKEY_MEM
596: ;
1.20 deraadt 597: return (result);
598: }
599:
1.31 deraadt 600: int (*proc_compares[])(const void *, const void *) = {
1.20 deraadt 601: compare_cpu,
602: compare_size,
603: compare_res,
604: compare_time,
605: compare_prio,
606: NULL
1.11 kstailey 607: };
1.30 deraadt 608:
1.1 downsj 609: /*
610: * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
611: * the process does not exist.
612: * It is EXTREMLY IMPORTANT that this function work correctly.
613: * If top runs setuid root (as in SVR4), then this function
614: * is the only thing that stands in the way of a serious
615: * security problem. It validates requests for the "kill"
616: * and "renice" commands.
617: */
1.33 millert 618: uid_t
1.29 pvalchev 619: proc_owner(pid_t pid)
1.20 deraadt 620: {
1.37 millert 621: struct kinfo_proc2 **prefp, *pp;
1.20 deraadt 622: int cnt;
623:
624: prefp = pref;
625: cnt = pref_len;
626: while (--cnt >= 0) {
627: pp = *prefp++;
1.37 millert 628: if (pp->p_pid == pid)
629: return ((uid_t)pp->p_ruid);
1.1 downsj 630: }
1.34 jfb 631: return (uid_t)(-1);
1.1 downsj 632: }
1.30 deraadt 633:
1.1 downsj 634: /*
1.17 todd 635: * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
1.15 weingart 636: * to be based on the new swapctl(2) system call.
1.1 downsj 637: */
638: static int
1.29 pvalchev 639: swapmode(int *used, int *total)
1.1 downsj 640: {
1.15 weingart 641: struct swapent *swdev;
1.30 deraadt 642: int nswap, rnswap, i;
1.1 downsj 643:
1.15 weingart 644: nswap = swapctl(SWAP_NSWAP, 0, 0);
1.20 deraadt 645: if (nswap == 0)
1.15 weingart 646: return 0;
647:
648: swdev = malloc(nswap * sizeof(*swdev));
1.20 deraadt 649: if (swdev == NULL)
1.15 weingart 650: return 0;
651:
652: rnswap = swapctl(SWAP_STATS, swdev, nswap);
1.20 deraadt 653: if (rnswap == -1)
1.15 weingart 654: return 0;
655:
656: /* if rnswap != nswap, then what? */
657:
658: /* Total things up */
659: *total = *used = 0;
660: for (i = 0; i < nswap; i++) {
661: if (swdev[i].se_flags & SWF_ENABLE) {
1.20 deraadt 662: *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
663: *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
1.1 downsj 664: }
665: }
1.20 deraadt 666: free(swdev);
1.1 downsj 667: return 1;
668: }