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