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