Annotation of src/usr.bin/top/machine.c, Revision 1.64
1.64 ! deraadt 1: /* $OpenBSD: machine.c,v 1.63 2007/11/01 19:19:48 otto 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/param.h>
38: #include <stdio.h>
39: #include <stdlib.h>
1.3 downsj 40: #include <string.h>
1.1 downsj 41: #include <unistd.h>
42: #include <sys/sysctl.h>
43: #include <sys/dkstat.h>
1.15 weingart 44: #include <sys/swap.h>
1.1 downsj 45: #include <err.h>
1.51 millert 46: #include <errno.h>
1.1 downsj 47:
48: #include "top.h"
1.3 downsj 49: #include "display.h"
1.1 downsj 50: #include "machine.h"
51: #include "utils.h"
1.31 deraadt 52: #include "loadavg.h"
53:
1.38 deraadt 54: static int swapmode(int *, int *);
1.58 otto 55: static char *state_abbr(struct kinfo_proc2 *);
56: static char *format_comm(struct kinfo_proc2 *);
1.1 downsj 57:
58: /* get_process_info passes back a handle. This is what it looks like: */
59:
1.20 deraadt 60: struct handle {
1.37 millert 61: struct kinfo_proc2 **next_proc; /* points to next valid proc pointer */
1.38 deraadt 62: int remaining; /* number of pointers remaining */
1.1 downsj 63: };
64:
65: /* what we consider to be process size: */
1.37 millert 66: #define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize)
1.1 downsj 67:
68: /*
69: * These definitions control the format of the per-process area
70: */
1.38 deraadt 71: static char header[] =
1.64 ! deraadt 72: " PID X PRI NICE SIZE RES STATE WAIT TIME CPU COMMAND";
1.31 deraadt 73:
1.1 downsj 74: /* 0123456 -- field to fill in starts at header+6 */
75: #define UNAME_START 6
76:
77: #define Proc_format \
1.64 ! deraadt 78: "%5d %-8.8s %3d %4d %5s %5s %-9s %-7.7s %6s %5.2f%% %s"
1.1 downsj 79:
80: /* process state names for the "STATE" column of the display */
1.30 deraadt 81: /*
82: * the extra nulls in the string "run" are for adding a slash and the
83: * processor number when needed
84: */
1.1 downsj 85:
1.30 deraadt 86: char *state_abbrev[] = {
1.40 deraadt 87: "", "start", "run", "sleep", "stop", "zomb", "dead", "onproc"
1.1 downsj 88: };
89:
1.30 deraadt 90: static int stathz;
1.1 downsj 91:
92: /* these are for calculating cpu state percentages */
1.48 millert 93: static int64_t **cp_time;
94: static int64_t **cp_old;
95: static int64_t **cp_diff;
1.1 downsj 96:
97: /* these are for detailing the process states */
1.45 markus 98: int process_states[8];
1.30 deraadt 99: char *procstatenames[] = {
100: "", " starting, ", " running, ", " idle, ",
1.47 markus 101: " stopped, ", " zombie, ", " dead, ", " on processor, ",
1.20 deraadt 102: NULL
1.1 downsj 103: };
104:
105: /* these are for detailing the cpu states */
1.48 millert 106: int64_t *cpu_states;
1.30 deraadt 107: char *cpustatenames[] = {
1.20 deraadt 108: "user", "nice", "system", "interrupt", "idle", NULL
1.1 downsj 109: };
110:
111: /* these are for detailing the memory statistics */
1.30 deraadt 112: int memory_stats[8];
113: char *memorynames[] = {
1.20 deraadt 114: "Real: ", "K/", "K act/tot ", "Free: ", "K ",
115: "Swap: ", "K/", "K used/tot",
116: NULL
1.1 downsj 117: };
118:
1.11 kstailey 119: /* these are names given to allowed sorting orders -- first is default */
1.31 deraadt 120: char *ordernames[] = {
121: "cpu", "size", "res", "time", "pri", NULL
122: };
1.11 kstailey 123:
1.1 downsj 124: /* these are for keeping track of the proc array */
1.30 deraadt 125: static int nproc;
126: static int onproc = -1;
127: static int pref_len;
1.37 millert 128: static struct kinfo_proc2 *pbase;
129: static struct kinfo_proc2 **pref;
1.1 downsj 130:
131: /* these are for getting the memory statistics */
1.30 deraadt 132: static int pageshift; /* log base 2 of the pagesize */
1.1 downsj 133:
134: /* define pagetok in terms of pageshift */
135: #define pagetok(size) ((size) << pageshift)
136:
1.41 deraadt 137: int ncpu;
138:
1.33 millert 139: unsigned int maxslp;
1.26 art 140:
1.36 deraadt 141: static int
1.29 pvalchev 142: getstathz(void)
1.18 deraadt 143: {
144: struct clockinfo cinf;
1.30 deraadt 145: size_t size = sizeof(cinf);
146: int mib[2];
1.18 deraadt 147:
148: mib[0] = CTL_KERN;
149: mib[1] = KERN_CLOCKRATE;
150: if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
151: return (-1);
152: return (cinf.stathz);
153: }
154:
155: int
1.29 pvalchev 156: machine_init(struct statics *statics)
1.1 downsj 157: {
1.41 deraadt 158: size_t size = sizeof(ncpu);
1.48 millert 159: int mib[2], pagesize, cpu;
1.41 deraadt 160:
161: mib[0] = CTL_HW;
162: mib[1] = HW_NCPU;
163: if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1)
164: return (-1);
1.62 deraadt 165: cpu_states = calloc(ncpu, CPUSTATES * sizeof(int64_t));
1.48 millert 166: if (cpu_states == NULL)
167: err(1, NULL);
1.62 deraadt 168: cp_time = calloc(ncpu, sizeof(int64_t *));
169: cp_old = calloc(ncpu, sizeof(int64_t *));
170: cp_diff = calloc(ncpu, sizeof(int64_t *));
1.48 millert 171: if (cp_time == NULL || cp_old == NULL || cp_diff == NULL)
172: err(1, NULL);
173: for (cpu = 0; cpu < ncpu; cpu++) {
1.52 otto 174: cp_time[cpu] = calloc(CPUSTATES, sizeof(int64_t));
175: cp_old[cpu] = calloc(CPUSTATES, sizeof(int64_t));
176: cp_diff[cpu] = calloc(CPUSTATES, sizeof(int64_t));
1.48 millert 177: if (cp_time[cpu] == NULL || cp_old[cpu] == NULL ||
178: cp_diff[cpu] == NULL)
179: err(1, NULL);
180: }
1.20 deraadt 181:
182: stathz = getstathz();
183: if (stathz == -1)
184: return (-1);
185:
186: pbase = NULL;
187: pref = NULL;
188: onproc = -1;
189: nproc = 0;
190:
1.30 deraadt 191: /*
192: * get the page size with "getpagesize" and calculate pageshift from
193: * it
194: */
1.20 deraadt 195: pagesize = getpagesize();
196: pageshift = 0;
197: while (pagesize > 1) {
198: pageshift++;
199: pagesize >>= 1;
200: }
201:
202: /* we only need the amount of log(2)1024 for our conversion */
203: pageshift -= LOG1024;
204:
205: /* fill in the statics information */
206: statics->procstate_names = procstatenames;
207: statics->cpustate_names = cpustatenames;
208: statics->memory_names = memorynames;
209: statics->order_names = ordernames;
210: return (0);
1.1 downsj 211: }
212:
1.20 deraadt 213: char *
1.29 pvalchev 214: format_header(char *uname_field)
1.1 downsj 215: {
1.20 deraadt 216: char *ptr;
1.1 downsj 217:
1.20 deraadt 218: ptr = header + UNAME_START;
1.30 deraadt 219: while (*uname_field != '\0')
1.20 deraadt 220: *ptr++ = *uname_field++;
221: return (header);
1.1 downsj 222: }
223:
224: void
1.31 deraadt 225: get_system_info(struct system_info *si)
1.1 downsj 226: {
1.20 deraadt 227: static int sysload_mib[] = {CTL_VM, VM_LOADAVG};
228: static int vmtotal_mib[] = {CTL_VM, VM_METER};
1.1 downsj 229: struct loadavg sysload;
1.20 deraadt 230: struct vmtotal vmtotal;
231: double *infoloadp;
1.30 deraadt 232: size_t size;
1.35 deraadt 233: int i;
1.48 millert 234: int64_t *tmpstate;
1.30 deraadt 235:
1.48 millert 236: if (ncpu > 1) {
237: size = CPUSTATES * sizeof(int64_t);
238: for (i = 0; i < ncpu; i++) {
239: int cp_time_mib[] = {CTL_KERN, KERN_CPTIME2, i};
240: tmpstate = cpu_states + (CPUSTATES * i);
241: if (sysctl(cp_time_mib, 3, cp_time[i], &size, NULL, 0) < 0)
242: warn("sysctl kern.cp_time2 failed");
243: /* convert cp_time2 counts to percentages */
244: (void) percentages(CPUSTATES, tmpstate, cp_time[i],
245: cp_old[i], cp_diff[i]);
246: }
247: } else {
248: int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
249: long cp_time_tmp[CPUSTATES];
250:
251: size = sizeof(cp_time_tmp);
252: if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0)
253: warn("sysctl kern.cp_time failed");
254: for (i = 0; i < CPUSTATES; i++)
255: cp_time[0][i] = cp_time_tmp[i];
256: /* convert cp_time counts to percentages */
257: (void) percentages(CPUSTATES, cpu_states, cp_time[0],
258: cp_old[0], cp_diff[0]);
259: }
1.35 deraadt 260:
1.20 deraadt 261: size = sizeof(sysload);
1.35 deraadt 262: if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0)
1.20 deraadt 263: warn("sysctl failed");
1.1 downsj 264: infoloadp = si->load_avg;
265: for (i = 0; i < 3; i++)
1.20 deraadt 266: *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
1.1 downsj 267:
268:
269: /* get total -- systemwide main memory usage structure */
1.20 deraadt 270: size = sizeof(vmtotal);
271: if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) {
272: warn("sysctl failed");
273: bzero(&vmtotal, sizeof(vmtotal));
1.1 downsj 274: }
275: /* convert memory stats to Kbytes */
276: memory_stats[0] = -1;
1.20 deraadt 277: memory_stats[1] = pagetok(vmtotal.t_arm);
278: memory_stats[2] = pagetok(vmtotal.t_rm);
1.1 downsj 279: memory_stats[3] = -1;
1.20 deraadt 280: memory_stats[4] = pagetok(vmtotal.t_free);
1.1 downsj 281: memory_stats[5] = -1;
1.31 deraadt 282:
1.1 downsj 283: if (!swapmode(&memory_stats[6], &memory_stats[7])) {
1.20 deraadt 284: memory_stats[6] = 0;
285: memory_stats[7] = 0;
1.1 downsj 286: }
287:
1.20 deraadt 288: /* set arrays and strings */
289: si->cpustates = cpu_states;
290: si->memory = memory_stats;
291: si->last_pid = -1;
1.1 downsj 292: }
293:
294: static struct handle handle;
295:
1.44 otto 296: struct kinfo_proc2 *
1.29 pvalchev 297: getprocs(int op, int arg, int *cnt)
1.22 deraadt 298: {
1.37 millert 299: size_t size;
300: int mib[6] = {CTL_KERN, KERN_PROC2, 0, 0, sizeof(struct kinfo_proc2), 0};
1.26 art 301: static int maxslp_mib[] = {CTL_VM, VM_MAXSLP};
1.37 millert 302: static struct kinfo_proc2 *procbase;
1.24 angelos 303: int st;
1.22 deraadt 304:
1.31 deraadt 305: mib[2] = op;
306: mib[3] = arg;
307:
1.26 art 308: size = sizeof(maxslp);
309: if (sysctl(maxslp_mib, 2, &maxslp, &size, NULL, 0) < 0) {
310: warn("sysctl vm.maxslp failed");
311: return (0);
312: }
1.37 millert 313: retry:
314: free(procbase);
315: st = sysctl(mib, 6, NULL, &size, NULL, 0);
1.22 deraadt 316: if (st == -1) {
1.37 millert 317: /* _kvm_syserr(kd, kd->program, "kvm_getproc2"); */
1.22 deraadt 318: return (0);
319: }
1.37 millert 320: size = 5 * size / 4; /* extra slop */
321: if ((procbase = malloc(size)) == NULL)
1.22 deraadt 322: return (0);
1.37 millert 323: mib[5] = (int)(size / sizeof(struct kinfo_proc2));
324: st = sysctl(mib, 6, procbase, &size, NULL, 0);
1.22 deraadt 325: if (st == -1) {
1.37 millert 326: if (errno == ENOMEM)
327: goto retry;
328: /* _kvm_syserr(kd, kd->program, "kvm_getproc2"); */
1.22 deraadt 329: return (0);
330: }
1.37 millert 331: *cnt = (int)(size / sizeof(struct kinfo_proc2));
1.22 deraadt 332: return (procbase);
333: }
334:
1.30 deraadt 335: caddr_t
1.29 pvalchev 336: get_process_info(struct system_info *si, struct process_select *sel,
1.30 deraadt 337: int (*compare) (const void *, const void *))
1.20 deraadt 338: {
1.56 otto 339: int show_idle, show_system, show_threads, show_uid, show_pid, show_cmd;
1.46 pat 340: int total_procs, active_procs;
1.37 millert 341: struct kinfo_proc2 **prefp, *pp;
1.20 deraadt 342:
1.22 deraadt 343: if ((pbase = getprocs(KERN_PROC_KTHREAD, 0, &nproc)) == NULL) {
344: /* warnx("%s", kvm_geterr(kd)); */
1.20 deraadt 345: quit(23);
346: }
347: if (nproc > onproc)
1.37 millert 348: pref = (struct kinfo_proc2 **)realloc(pref,
349: sizeof(struct kinfo_proc2 *) * (onproc = nproc));
1.20 deraadt 350: if (pref == NULL) {
351: warnx("Out of memory.");
352: quit(23);
353: }
354: /* get a pointer to the states summary array */
355: si->procstates = process_states;
1.1 downsj 356:
1.20 deraadt 357: /* set up flags which define what we are going to select */
358: show_idle = sel->idle;
359: show_system = sel->system;
1.50 tedu 360: show_threads = sel->threads;
1.33 millert 361: show_uid = sel->uid != (uid_t)-1;
1.44 otto 362: show_pid = sel->pid != (pid_t)-1;
1.56 otto 363: show_cmd = sel->command != NULL;
1.20 deraadt 364:
365: /* count up process states and get pointers to interesting procs */
366: total_procs = 0;
367: active_procs = 0;
368: memset((char *) process_states, 0, sizeof(process_states));
369: prefp = pref;
1.46 pat 370: for (pp = pbase; pp < &pbase[nproc]; pp++) {
1.20 deraadt 371: /*
372: * Place pointers to each valid proc structure in pref[].
373: * Process slots that are actually in use have a non-zero
1.59 otto 374: * status field. Processes with P_SYSTEM set are system
375: * processes---these get ignored unless show_system is set.
1.20 deraadt 376: */
1.37 millert 377: if (pp->p_stat != 0 &&
1.50 tedu 378: (show_system || (pp->p_flag & P_SYSTEM) == 0) &&
379: (show_threads || (pp->p_flag & P_THREAD) == 0)) {
1.20 deraadt 380: total_procs++;
1.37 millert 381: process_states[(unsigned char) pp->p_stat]++;
382: if (pp->p_stat != SZOMB &&
383: (show_idle || pp->p_pctcpu != 0 ||
384: pp->p_stat == SRUN) &&
1.44 otto 385: (!show_uid || pp->p_ruid == sel->uid) &&
1.56 otto 386: (!show_pid || pp->p_pid == sel->pid) &&
387: (!show_cmd || strstr(pp->p_comm,
388: sel->command))) {
1.20 deraadt 389: *prefp++ = pp;
390: active_procs++;
391: }
392: }
393: }
394:
395: /* if requested, sort the "interesting" processes */
1.30 deraadt 396: if (compare != NULL)
397: qsort((char *) pref, active_procs,
1.37 millert 398: sizeof(struct kinfo_proc2 *), compare);
1.20 deraadt 399: /* remember active and total counts */
400: si->p_total = total_procs;
401: si->p_active = pref_len = active_procs;
402:
403: /* pass back a handle */
404: handle.next_proc = pref;
405: handle.remaining = active_procs;
406: return ((caddr_t) & handle);
407: }
408:
1.30 deraadt 409: char fmt[MAX_COLS]; /* static area where result is built */
1.20 deraadt 410:
1.58 otto 411: static char *
1.40 deraadt 412: state_abbr(struct kinfo_proc2 *pp)
413: {
414: static char buf[10];
415:
1.42 deraadt 416: if (ncpu > 1 && pp->p_cpuid != KI_NOCPU)
1.48 millert 417: snprintf(buf, sizeof buf, "%s/%llu",
1.41 deraadt 418: state_abbrev[(unsigned char)pp->p_stat], pp->p_cpuid);
419: else
420: snprintf(buf, sizeof buf, "%s",
421: state_abbrev[(unsigned char)pp->p_stat]);
1.40 deraadt 422: return buf;
423: }
424:
1.58 otto 425: static char *
1.49 markus 426: format_comm(struct kinfo_proc2 *kp)
427: {
1.63 otto 428: static char **s, buf[MAX_COLS];
1.49 markus 429: size_t siz = 100;
430: char **p;
431: int mib[4];
432: extern int show_args;
433:
434: if (!show_args)
435: return (kp->p_comm);
436:
437: for (;; siz *= 2) {
438: if ((s = realloc(s, siz)) == NULL)
439: err(1, NULL);
440: mib[0] = CTL_KERN;
441: mib[1] = KERN_PROC_ARGS;
442: mib[2] = kp->p_pid;
443: mib[3] = KERN_PROC_ARGV;
444: if (sysctl(mib, 4, s, &siz, NULL, 0) == 0)
445: break;
446: if (errno != ENOMEM)
447: return (kp->p_comm);
448: }
449: buf[0] = '\0';
450: for (p = s; *p != NULL; p++) {
451: if (p != s)
452: strlcat(buf, " ", sizeof(buf));
453: strlcat(buf, *p, sizeof(buf));
454: }
455: if (buf[0] == '\0')
456: return (kp->p_comm);
457: return (buf);
458: }
459:
460: char *
1.61 otto 461: format_next_process(caddr_t handle, char *(*get_userid)(uid_t), pid_t *pid)
1.20 deraadt 462: {
1.30 deraadt 463: char *p_wait, waddr[sizeof(void *) * 2 + 3]; /* Hexify void pointer */
1.37 millert 464: struct kinfo_proc2 *pp;
1.20 deraadt 465: struct handle *hp;
466: int cputime;
467: double pct;
468:
469: /* find and remember the next proc structure */
470: hp = (struct handle *) handle;
471: pp = *(hp->next_proc++);
472: hp->remaining--;
473:
1.37 millert 474: cputime = (pp->p_uticks + pp->p_sticks + pp->p_iticks) / stathz;
1.20 deraadt 475:
476: /* calculate the base for cpu percentages */
1.37 millert 477: pct = pctdouble(pp->p_pctcpu);
1.20 deraadt 478:
1.37 millert 479: if (pp->p_wchan) {
480: if (pp->p_wmesg)
481: p_wait = pp->p_wmesg;
1.20 deraadt 482: else {
1.37 millert 483: snprintf(waddr, sizeof(waddr), "%llx",
1.57 otto 484: (unsigned long long)(pp->p_wchan & ~KERNBASE));
1.20 deraadt 485: p_wait = waddr;
486: }
1.30 deraadt 487: } else
1.20 deraadt 488: p_wait = "-";
489:
490: /* format this entry */
1.30 deraadt 491: snprintf(fmt, sizeof fmt, Proc_format,
1.37 millert 492: pp->p_pid, (*get_userid)(pp->p_ruid),
493: pp->p_priority - PZERO, pp->p_nice - NZERO,
1.1 downsj 494: format_k(pagetok(PROCSIZE(pp))),
1.37 millert 495: format_k(pagetok(pp->p_vm_rssize)),
496: (pp->p_stat == SSLEEP && pp->p_slptime > maxslp) ?
1.40 deraadt 497: "idle" : state_abbr(pp),
1.30 deraadt 498: p_wait, format_time(cputime), 100.0 * pct,
1.49 markus 499: printable(format_comm(pp)));
1.1 downsj 500:
1.61 otto 501: *pid = pp->p_pid;
1.20 deraadt 502: /* return the result */
503: return (fmt);
1.1 downsj 504: }
505:
506: /* comparison routine for qsort */
1.11 kstailey 507: static unsigned char sorted_state[] =
508: {
1.20 deraadt 509: 0, /* not used */
510: 4, /* start */
511: 5, /* run */
512: 2, /* sleep */
513: 3, /* stop */
514: 1 /* zombie */
1.11 kstailey 515: };
516:
517: /*
518: * proc_compares - comparison functions for "qsort"
519: */
520:
521: /*
522: * First, the possible comparison keys. These are defined in such a way
523: * that they can be merely listed in the source code to define the actual
524: * desired ordering.
525: */
526:
527: #define ORDERKEY_PCTCPU \
1.37 millert 528: if (lresult = (pctcpu)p2->p_pctcpu - (pctcpu)p1->p_pctcpu, \
1.22 deraadt 529: (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
1.11 kstailey 530: #define ORDERKEY_CPUTIME \
1.37 millert 531: if ((result = p2->p_rtime_sec - p1->p_rtime_sec) == 0) \
532: if ((result = p2->p_rtime_usec - p1->p_rtime_usec) == 0)
1.11 kstailey 533: #define ORDERKEY_STATE \
1.37 millert 534: if ((result = sorted_state[(unsigned char)p2->p_stat] - \
535: sorted_state[(unsigned char)p1->p_stat]) == 0)
1.11 kstailey 536: #define ORDERKEY_PRIO \
1.37 millert 537: if ((result = p2->p_priority - p1->p_priority) == 0)
1.11 kstailey 538: #define ORDERKEY_RSSIZE \
1.37 millert 539: if ((result = p2->p_vm_rssize - p1->p_vm_rssize) == 0)
1.11 kstailey 540: #define ORDERKEY_MEM \
541: if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
542:
543: /* compare_cpu - the comparison function for sorting by cpu percentage */
1.36 deraadt 544: static int
1.29 pvalchev 545: compare_cpu(const void *v1, const void *v2)
1.11 kstailey 546: {
1.20 deraadt 547: struct proc **pp1 = (struct proc **) v1;
548: struct proc **pp2 = (struct proc **) v2;
1.37 millert 549: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 550: pctcpu lresult;
1.20 deraadt 551: int result;
552:
553: /* remove one level of indirection */
1.37 millert 554: p1 = *(struct kinfo_proc2 **) pp1;
555: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 556:
557: ORDERKEY_PCTCPU
1.30 deraadt 558: ORDERKEY_CPUTIME
559: ORDERKEY_STATE
560: ORDERKEY_PRIO
561: ORDERKEY_RSSIZE
562: ORDERKEY_MEM
563: ;
1.20 deraadt 564: return (result);
1.11 kstailey 565: }
566:
567: /* compare_size - the comparison function for sorting by total memory usage */
1.36 deraadt 568: static int
1.29 pvalchev 569: compare_size(const void *v1, const void *v2)
1.11 kstailey 570: {
1.20 deraadt 571: struct proc **pp1 = (struct proc **) v1;
572: struct proc **pp2 = (struct proc **) v2;
1.37 millert 573: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 574: pctcpu lresult;
1.20 deraadt 575: int result;
576:
577: /* remove one level of indirection */
1.37 millert 578: p1 = *(struct kinfo_proc2 **) pp1;
579: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 580:
581: ORDERKEY_MEM
1.30 deraadt 582: ORDERKEY_RSSIZE
583: ORDERKEY_PCTCPU
584: ORDERKEY_CPUTIME
585: ORDERKEY_STATE
586: ORDERKEY_PRIO
587: ;
1.20 deraadt 588: return (result);
1.11 kstailey 589: }
590:
591: /* compare_res - the comparison function for sorting by resident set size */
1.36 deraadt 592: static int
1.29 pvalchev 593: compare_res(const void *v1, const void *v2)
1.11 kstailey 594: {
1.20 deraadt 595: struct proc **pp1 = (struct proc **) v1;
596: struct proc **pp2 = (struct proc **) v2;
1.37 millert 597: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 598: pctcpu lresult;
1.20 deraadt 599: int result;
600:
601: /* remove one level of indirection */
1.37 millert 602: p1 = *(struct kinfo_proc2 **) pp1;
603: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 604:
605: ORDERKEY_RSSIZE
1.30 deraadt 606: ORDERKEY_MEM
607: ORDERKEY_PCTCPU
608: ORDERKEY_CPUTIME
609: ORDERKEY_STATE
610: ORDERKEY_PRIO
611: ;
1.20 deraadt 612: return (result);
1.11 kstailey 613: }
614:
615: /* compare_time - the comparison function for sorting by CPU time */
1.36 deraadt 616: static int
1.29 pvalchev 617: compare_time(const void *v1, const void *v2)
1.11 kstailey 618: {
1.20 deraadt 619: struct proc **pp1 = (struct proc **) v1;
620: struct proc **pp2 = (struct proc **) v2;
1.37 millert 621: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 622: pctcpu lresult;
1.20 deraadt 623: int result;
624:
625: /* remove one level of indirection */
1.37 millert 626: p1 = *(struct kinfo_proc2 **) pp1;
627: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 628:
629: ORDERKEY_CPUTIME
1.30 deraadt 630: ORDERKEY_PCTCPU
631: ORDERKEY_STATE
632: ORDERKEY_PRIO
633: ORDERKEY_MEM
634: ORDERKEY_RSSIZE
635: ;
1.20 deraadt 636: return (result);
1.11 kstailey 637: }
638:
639: /* compare_prio - the comparison function for sorting by CPU time */
1.36 deraadt 640: static int
1.29 pvalchev 641: compare_prio(const void *v1, const void *v2)
1.11 kstailey 642: {
1.30 deraadt 643: struct proc **pp1 = (struct proc **) v1;
644: struct proc **pp2 = (struct proc **) v2;
1.37 millert 645: struct kinfo_proc2 *p1, *p2;
1.30 deraadt 646: pctcpu lresult;
1.20 deraadt 647: int result;
648:
649: /* remove one level of indirection */
1.37 millert 650: p1 = *(struct kinfo_proc2 **) pp1;
651: p2 = *(struct kinfo_proc2 **) pp2;
1.20 deraadt 652:
653: ORDERKEY_PRIO
1.30 deraadt 654: ORDERKEY_PCTCPU
655: ORDERKEY_CPUTIME
656: ORDERKEY_STATE
657: ORDERKEY_RSSIZE
658: ORDERKEY_MEM
659: ;
1.20 deraadt 660: return (result);
661: }
662:
1.31 deraadt 663: int (*proc_compares[])(const void *, const void *) = {
1.20 deraadt 664: compare_cpu,
665: compare_size,
666: compare_res,
667: compare_time,
668: compare_prio,
669: NULL
1.11 kstailey 670: };
1.30 deraadt 671:
1.1 downsj 672: /*
673: * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
674: * the process does not exist.
1.43 otto 675: * It is EXTREMELY IMPORTANT that this function work correctly.
1.1 downsj 676: * If top runs setuid root (as in SVR4), then this function
677: * is the only thing that stands in the way of a serious
678: * security problem. It validates requests for the "kill"
679: * and "renice" commands.
680: */
1.33 millert 681: uid_t
1.29 pvalchev 682: proc_owner(pid_t pid)
1.20 deraadt 683: {
1.37 millert 684: struct kinfo_proc2 **prefp, *pp;
1.20 deraadt 685: int cnt;
686:
687: prefp = pref;
688: cnt = pref_len;
689: while (--cnt >= 0) {
690: pp = *prefp++;
1.37 millert 691: if (pp->p_pid == pid)
692: return ((uid_t)pp->p_ruid);
1.1 downsj 693: }
1.34 jfb 694: return (uid_t)(-1);
1.1 downsj 695: }
1.30 deraadt 696:
1.1 downsj 697: /*
1.17 todd 698: * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
1.15 weingart 699: * to be based on the new swapctl(2) system call.
1.1 downsj 700: */
701: static int
1.29 pvalchev 702: swapmode(int *used, int *total)
1.1 downsj 703: {
1.15 weingart 704: struct swapent *swdev;
1.30 deraadt 705: int nswap, rnswap, i;
1.1 downsj 706:
1.15 weingart 707: nswap = swapctl(SWAP_NSWAP, 0, 0);
1.20 deraadt 708: if (nswap == 0)
1.15 weingart 709: return 0;
710:
1.62 deraadt 711: swdev = calloc(nswap, sizeof(*swdev));
1.20 deraadt 712: if (swdev == NULL)
1.15 weingart 713: return 0;
714:
715: rnswap = swapctl(SWAP_STATS, swdev, nswap);
1.53 ray 716: if (rnswap == -1) {
717: free(swdev);
1.15 weingart 718: return 0;
1.53 ray 719: }
1.15 weingart 720:
721: /* if rnswap != nswap, then what? */
722:
723: /* Total things up */
724: *total = *used = 0;
725: for (i = 0; i < nswap; i++) {
726: if (swdev[i].se_flags & SWF_ENABLE) {
1.20 deraadt 727: *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
728: *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
1.1 downsj 729: }
730: }
1.20 deraadt 731: free(swdev);
1.1 downsj 732: return 1;
733: }