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